static void test_string_to_datapath_id() { uint64_t datapath_id; uint64_t expected_datapath_id = 18446744073709551615ULL; assert_true( string_to_datapath_id( "18446744073709551615", &datapath_id ) ); assert_memory_equal( &datapath_id, &expected_datapath_id, sizeof( uint64_t ) ); assert_false( string_to_datapath_id( "INVALID DATAPATH ID", &datapath_id ) ); }
static void parse_argv( int *argc, char ***argv ) { assert( argc != NULL ); assert( argv != NULL ); int argc_tmp = *argc; char *new_argv[ *argc ]; run_as_daemon = false; controller.ip = 0x7f000001; controller.port = 6633; for ( int i = 0; i < *argc; ++i ) { new_argv[ i ] = ( *argv )[ i ]; } for ( ;; ) { opterr = 0; int c = getopt_long( *argc, *argv, short_options, long_options, NULL ); if ( c == -1 ) { break; } switch ( c ) { case 'i': if ( optarg != NULL ) { string_to_datapath_id( optarg, &datapath_id ); } break; case 'c': if ( optarg != NULL ) { struct in_addr addr; inet_aton( optarg, &addr ); controller.ip = ntohl( addr.s_addr ); } break; case 'p': if ( optarg != NULL && atoi( optarg ) <= UINT16_MAX ) { controller.port = ( uint16_t ) atoi( optarg ); } break; case 'd': run_as_daemon = true; break; case 'l': set_logging_level( optarg ); break; case 'h': usage(); exit( EXIT_SUCCESS ); break; default: continue; } if ( optarg == NULL || strchr( new_argv[ optind - 1 ], '=' ) != NULL ) { argc_tmp -= 1; new_argv[ optind - 1 ] = NULL; } else { argc_tmp -= 2; new_argv[ optind - 1 ] = NULL; new_argv[ optind - 2 ] = NULL; } } for ( int i = 0, j = 0; i < *argc; ++i ) { if ( new_argv[ i ] != NULL ) { ( *argv )[ j ] = new_argv[ i ]; j++; } } if ( argc_tmp < *argc ) { ( *argv )[ argc_tmp ] = NULL; } *argc = argc_tmp; reset_getopt(); }
static bool parse_argument( int argc, char *argv[] ) { bool type_specified = false; int c; while ( ( c = getopt_long( argc, argv, short_options, long_options, NULL ) ) != -1 ) { switch ( c ) { case 'm': sw_manager = true; operate_on_all = false; break; case 's': sw_manager = false; operate_on_all = false; if ( !string_to_datapath_id( optarg, &dpid ) ) { error( "Invalid dpid '%s' specified. ", optarg ); usage(); exit( EXIT_SUCCESS ); return false; } break; case 't': // add type_specified = true; if ( false ) { } else if ( strcasecmp( "vendor", optarg ) == 0 ) { type = EVENT_FORWARD_TYPE_VENDOR; } else if ( strcasecmp( "packet_in", optarg ) == 0 ) { type = EVENT_FORWARD_TYPE_PACKET_IN; } else if ( strcasecmp( "port_status", optarg ) == 0 ) { type = EVENT_FORWARD_TYPE_PORT_STATUS; } else if ( strcasecmp( "state_notify", optarg ) == 0 ) { type = EVENT_FORWARD_TYPE_STATE_NOTIFY; } else { error( "Invalid type '%s' specified. Must e one of vendor, packet_in, port_status, or state_notify\n", optarg ); usage(); exit( EXIT_SUCCESS ); return false; } break; default: error( "Encountered unknown option." ); usage(); exit( EXIT_SUCCESS ); return false; break; } } if ( !type_specified ) { error( "Event Type was not specified with -t option.\n" ); usage(); exit( EXIT_SUCCESS ); return false; } if ( optind >= argc ) { error( "Service name was not specified.\n" ); usage(); exit( EXIT_FAILURE ); return false; } service_name = argv[ optind ]; return true; }
void _parse_options( struct switch_arguments *args, int argc, char **argv ) { static struct option long_options[] = { { "logging_level", required_argument, 0, 'l' }, { "daemonize", no_argument, 0, 'd' }, { "datapath_id", required_argument, 0, 'i' }, { "max_flow_entries", required_argument, 0, 'm' }, { "server_ip", required_argument, 0, 'c' }, { "server_port", required_argument, 0, 'p' }, { "switch_ports", optional_argument, 0, 'e' }, { "administer", optional_argument, 0, 'a' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; static const char *short_options = "l:di:c:p:e:h"; set_default_opts( args, long_options ); int c, index = 0; optind = 0; while ( 1 ) { c = getopt_long( argc, argv, short_options, args->options, &index ); if ( c == -1 ) { break; } switch ( c ) { case 'h': print_usage( args, 0 ); break; case 'a': args->administer = true; break; case 'l': if ( optarg ) { args->log_level = optarg; set_logging_level( args->log_level ); } break; case 'd': args->run_as_daemon = true; break; case 'i': if ( optarg ) { string_to_datapath_id( optarg, &args->datapath_id ); } break; case 'm': if ( optarg ) { args->max_flow_entries = ( uint16_t ) atoi( optarg ); } break; case 'c': if ( optarg ) { char *save_ptr = NULL; uint32_t temp_addr = 0; char *p = strtok_r( optarg, ".", &save_ptr ); while ( p ) { temp_addr = ( temp_addr << 8 ) | ( uint32_t ) atoi( p ); p = strtok_r( NULL, ".", &save_ptr ); } if ( temp_addr ) { args->server_ip = temp_addr; } } break; case 'p': if ( optarg ) { args->server_port = ( uint16_t ) atoi( optarg ); } break; case 'e': if ( optarg ) { args->datapath_ports = optarg; } break; default: break; } } }