/** * \brief Safe main() implementation. */ int safe_main(int argc, char **argv, bool noargs) { m_program_name = argv[0]; // process command line options first boost::program_options::options_description cmdline_options; option_validator opt_validator; process_cmdline_options(cmdline_options, opt_validator, argc, argv); if (options().count("help") || (!noargs && argc == 1)) { std::cout << help_header() << cmdline_options << std::endl; return 0; // nothing more to do here } if (options().count("version")) { std::cout << version() << std::endl; return 0; // nothing more to do here } // some basic command line options consistency checks if (running_as_daemon() && m_shell_port == 0) { throw std::runtime_error("please use --shell-port option when running with --daemon"); } moost::utils::assert_absolute_path(m_pidfile, "pidfile"); // service specific checks validator::constraints_map_t opt_constraints; if (running_as_daemon()) { opt_constraints["absolute_filenames"] = "1"; } opt_validator(options(), opt_constraints); m_ownership.validate_options(options()); m_logging.validate_options(options()); validate_options(); // configure logging initialisation verbosity m_logging.set_init_verbose(!running_as_daemon()); // change ownership according to policy m_ownership.change_ownership(options()); run(); return 0; // apparently, we've managed to finish cleanly }
int main(int argc, char *argv[]) { char opt; int ret; int option_index; while ((opt = getopt_long(argc, argv, "d:f:i:lrs", long_options, &option_index)) >= 0) { switch (opt) { case 'f': case 'd': case 'i': flow_cmd = opt; flow_index = strtoul(optarg, NULL, 0); break; case 'l': list = 1; break; case 'r': rate = 1; break; case 's': stats = 1; break; case 0: parse_long_opts(option_index, optarg); break; default: Usage(); } } validate_options(); ret = flow_table_setup(); if (ret < 0) return ret; ret = flow_table_get(); if (ret < 0) return ret; if (list) flow_list(); else if (rate) flow_rate(); else if (stats) flow_stats(); else flow_validate(flow_index, flow_cmd); return 0; }
int main(int argc, char ** argv) { struct swift_context *c = NULL; struct client_options opts; swift_error e; if (!read_options(&opts, argc, argv)) { exit(EXIT_FAILURE); } if (!validate_options(&opts)) { exit(EXIT_FAILURE); } if (opts.filename) { opts.datahandle = setupio(&opts); } else { if (opts.action == ACTION_OBJ_WRITE) { opts.datahandle = stdin; } else { opts.datahandle = stdout; } } if (!opts.datahandle) { exit(EXIT_FAILURE); } swift_init(); e = swift_context_create(&c, opts.url, opts.username, opts.password); if (e) { fprintf(stderr, "Error: %s, line %d\n", swift_errormsg(e), __LINE__); exit(EXIT_FAILURE); } e = execute_action(&opts, c); if (e) { fprintf(stderr, "Error: %s, line %d\n", swift_errormsg(e), __LINE__); exit(EXIT_FAILURE); } swift_context_delete(&c); fclose(opts.datahandle); return 0; }
int main(int argc, char *argv[]) { int ret; int opt; int ind; cl = nl_register_client(); if (!cl) { exit(1); } ret = nl_socket(cl, NETLINK_GENERIC); if (ret <= 0) { exit(1); } if (vrouter_get_family_id(cl) <= 0) { return 0; } while ((opt = getopt_long(argc, argv, "", long_options, &ind)) >= 0) { switch(opt) { case 0: parse_long_opts(ind, optarg); break; default: usage(); } } validate_options(); vr_nh_op(command, type, nh_id, if_id, vrf_id, dst_mac, src_mac, sip, dip, flags); return 0; }
int main(int argc, char *argv[]) { char opt; int ret, option_index; while (((opt = getopt_long(argc, argv, "", long_options, &option_index)) >= 0)) { switch (opt) { case 0: parse_long_opts(option_index, optarg); break; default: Usage(); } } validate_options(); cl = nl_register_client(); if (!cl) { exit(1); } ret = nl_socket(cl, NETLINK_GENERIC); if (ret <= 0) { exit(1); } if (vrouter_get_family_id(cl) <= 0) { return -1; } stats_req.vsr_marker = -1; vr_stats_op(); return 0; }
int main(int argc, char *argv[]) { int ret, opt, option_index; vrouter_ops req; parse_ini_file(); platform = get_platform(); if (argc == 1) { Usage(); } while ((opt = getopt_long(argc, argv, "", long_options, &option_index)) >= 0) { switch (opt) { case 0: parse_long_opts(option_index, optarg); break; case '?': default: Usage(); break; } } validate_options(); cl = vr_get_nl_client(VR_NETLINK_PROTO_DEFAULT); if (!cl) { exit(1); } vr_vrouter_op(cl); return 0; }
/* * MAIN * * assumes the existance of getopt() to clean up the command * line handling */ int main (int ac, char **av) { DSS_HUGE i; table = (1 << CUST) | (1 << SUPP) | (1 << NATION) | (1 << REGION) | (1 << PART_PSUPP) | (1 << ORDER_LINE); force = 0; insert_segments=0; delete_segments=0; insert_orders_segment=0; insert_lineitem_segment=0; delete_segment=0; verbose = 0; set_seeds = 0; scale = 1; flt_scale = 1.0; updates = 0; step = -1; tdefs[ORDER].base *= ORDERS_PER_CUST; /* have to do this after init */ tdefs[LINE].base *= ORDERS_PER_CUST; /* have to do this after init */ tdefs[ORDER_LINE].base *= ORDERS_PER_CUST; /* have to do this after init */ children = 1; d_path = NULL; #ifdef NO_SUPPORT signal (SIGINT, exit); #endif /* NO_SUPPORT */ process_options (ac, av); validate_options(); #if (defined(WIN32)&&!defined(_POSIX_)) for (i = 0; i < ac; i++) { spawn_args[i] = malloc (((int)strlen (av[i]) + 1) * sizeof (char)); MALLOC_CHECK (spawn_args[i]); strcpy (spawn_args[i], av[i]); } spawn_args[ac] = NULL; #endif if (verbose >= 0) { fprintf (stderr, "%s Population Generator (Version %d.%d.%d)\n", NAME, VERSION, RELEASE, PATCH); fprintf (stderr, "Copyright %s %s\n", TPC, C_DATES); } load_dists (); #ifdef RNG_TEST for (i=0; i <= MAX_STREAM; i++) Seed[i].nCalls = 0; #endif /* have to do this after init */ tdefs[NATION].base = nations.count; tdefs[REGION].base = regions.count; /* * updates are never parallelized */ if (updates) { /* * set RNG to start generating rows beyond SF=scale */ set_state (ORDER, scale, 100, 101, &i); rowcnt = (int)(tdefs[ORDER_LINE].base / 10000 * scale * UPD_PCT); if (step > 0) { /* * adjust RNG for any prior update generation */ for (i=1; i < step; i++) { sd_order(0, rowcnt); sd_line(0, rowcnt); } upd_num = step - 1; } else upd_num = 0; while (upd_num < updates) { if (verbose > 0) fprintf (stderr, "Generating update pair #%ld for %s", upd_num + 1, tdefs[ORDER_LINE].comment); insert_orders_segment=0; insert_lineitem_segment=0; delete_segment=0; minrow = upd_num * rowcnt + 1; gen_tbl (ORDER_LINE, minrow, rowcnt, upd_num + 1); if (verbose > 0) fprintf (stderr, "done.\n"); pr_drange (ORDER_LINE, minrow, rowcnt, upd_num + 1); upd_num++; } exit (0); } /** ** actual data generation section starts here **/ /* * traverse the tables, invoking the appropriate data generation routine for any to be built */ for (i = PART; i <= REGION; i++) if (table & (1 << i)) { if (children > 1 && i < NATION) { partial ((int)i, step); } else { minrow = 1; if (i < NATION) rowcnt = tdefs[i].base * scale; else rowcnt = tdefs[i].base; if (verbose > 0) fprintf (stderr, "Generating data for %s", tdefs[i].comment); gen_tbl ((int)i, minrow, rowcnt, upd_num); if (verbose > 0) fprintf (stderr, "done.\n"); } } return (0); }
int main(int argc, char *argv[]) { int ret, opt, option_index; /* * the proto of the socket changes based on whether we are creating an * interface in linux or doing an operation in vrouter */ unsigned int sock_proto = NETLINK_GENERIC; while ((opt = getopt_long(argc, argv, "ba:c:d:g:klm:t:v:p:", long_options, &option_index)) >= 0) { switch (opt) { case 'a': add_set = 1; parse_long_opts(ADD_OPT_INDEX, optarg); break; case 'c': create_set = 1; parse_long_opts(CREATE_OPT_INDEX, optarg); break; case 'd': delete_set = 1; parse_long_opts(DELETE_OPT_INDEX, optarg); break; case 'g': get_set = 1; parse_long_opts(GET_OPT_INDEX, optarg); break; case 'k': parse_long_opts(KINDEX_OPT_INDEX, optarg); kindex_set = 1; break; case 'l': case 'b': list_set = 1; parse_long_opts(LIST_OPT_INDEX, NULL); break; case 'm': mac_set = 1; parse_long_opts(MAC_OPT_INDEX, optarg); break; case 'v': vrf_set = 1; parse_long_opts(VRF_OPT_INDEX, optarg); break; case 'p': policy_set = 1; parse_long_opts(POLICY_OPT_INDEX, NULL); break; case 't': type_set = 1; parse_long_opts(TYPE_OPT_INDEX, optarg); break; case 0: parse_long_opts(option_index, optarg); break; case '?': default: Usage(); } } validate_options(); cl = nl_register_client(); if (!cl) exit(-ENOMEM); if (create_set) sock_proto = NETLINK_ROUTE; ret = nl_socket(cl, sock_proto); if (ret <= 0) exit(ret); if (sock_proto == NETLINK_GENERIC) if (vrouter_get_family_id(cl) <= 0) return -1; if (add_set) { /* * for addition, we need to see whether the interface already * exists in vrouter or not. so, get can return error if the * interface does not exist in vrouter */ ignore_error = true; vr_intf_op(SANDESH_OP_GET); ignore_error = false; } vr_intf_op(vr_op); return 0; }
/* * Handle command-line options */ options_t* parse_options(int argc, char *argv[]) { char optstring[] = "abd:f:hi:M:m:pSsvx:Z:"; int c; opterr = 0; while ((c = getopt(argc, argv, optstring)) != -1) { switch(c) { case 'h': usage(stdout); exit (0); case 'i': if (options.dumpfile) { log_msg(LOG_ERROR, "can't specify -i and -f"); unexpected_exit (-1); } options.interface = optarg; break; case 'v': options.verbose = TRUE; break; case 'b': if (!isatty(1)) log_msg(LOG_WARNING, "can't beep unless standard output is a terminal"); else options.beep = TRUE; break; case 'p': options.promisc = FALSE; break; case 's': options.extract_type |= m_audio; break; case 'S': options.extract_type = m_audio; break; case 'M': options.audio_mpeg_player = optarg; options.mpeg_player_specified = TRUE; break; case 'a': options.adjunct = TRUE; break; case 'm': options.max_tmpfiles = atoi(optarg); if (options.max_tmpfiles <= 0) { log_msg(LOG_ERROR, "`%s' does not make sense for -m", optarg); unexpected_exit (-1); } break; case 'd': options.tmpdir = strdup(optarg); options.tmpdir_especified = TRUE; /* so we don't delete it. */ break; case 'f': if (options.interface) { log_msg(LOG_ERROR, "can't specify -i and -f"); unexpected_exit (-1); } options.dumpfile = optarg; break; #ifndef NO_DISPLAY_WINDOW case 'x': options.savedimgpfx = optarg; options.newpfx = TRUE; break; #endif case 'Z': options.drop_username = strdup(optarg); break; case '?': default: if (strchr(optstring, optopt)) log_msg(LOG_ERROR, "option -%c requires an argument", optopt); else log_msg(LOG_ERROR, "unrecognised option -%c", optopt); usage(stderr); unexpected_exit (1); } } /* Build up filter. */ if (optind < argc) { if (options.dumpfile) log_msg(LOG_WARNING, "filter code ignored with dump file"); else { char **a; int l; for (a = argv + optind, l = sizeof("tcp and ()"); *a; l += strlen(*a) + 1, ++a); options.filterexpr = calloc(l, 1); strcpy(options.filterexpr, "tcp and ("); for (a = argv + optind; *a; ++a) { strcat(options.filterexpr, *a); if (*(a + 1)) strcat(options.filterexpr, " "); } strcat(options.filterexpr, ")"); } } else options.filterexpr = "tcp"; log_msg(LOG_INFO, "using filter expression `%s'", options.filterexpr); #ifndef NO_DISPLAY_WINDOW if (options.newpfx && !options.adjunct) log_msg(LOG_INFO, "using saved image prefix `%s'", options.savedimgpfx); #endif validate_options(&options); return &options; }
/* Initialize program configuration via config file and/or command-line * switches. */ void config_init(fko_srv_options_t *opts, int argc, char **argv) { int cmd_arg, index, is_err; unsigned char got_conf_file = 0, got_override_config = 0; char override_file[MAX_LINE_LEN] = {0}; char *ndx, *cmrk; /* Zero out options and opts_track. */ memset(opts, 0x00, sizeof(fko_srv_options_t)); /* Set some preconfiguration options (i.e. build-time defaults) */ set_preconfig_entries(opts); /* In case this is a re-config. */ optind = 0; /* First, scan the command-line args for -h/--help or an alternate * configuration file. If we find an alternate config file, use it, * otherwise use the default. We also grab any override config files * as well. */ while ((cmd_arg = getopt_long(argc, argv, GETOPTS_OPTION_STRING, cmd_opts, &index)) != -1) { /* If help is wanted, give it and exit. */ switch(cmd_arg) { case 'h': usage(); clean_exit(opts, NO_FW_CLEANUP, EXIT_SUCCESS); break; /* Look for configuration file arg. */ case 'c': set_config_entry(opts, CONF_CONFIG_FILE, optarg); got_conf_file++; /* If we already have the config_override option, we are done. */ if(got_override_config > 0) break; /* Look for override configuration file arg. */ case 'O': set_config_entry(opts, CONF_OVERRIDE_CONFIG, optarg); got_override_config++; /* If we already have the conf_file option, we are done. */ if(got_conf_file > 0) break; } } /* If no alternate configuration file was specified, we use the * default. */ if(opts->config[CONF_CONFIG_FILE] == NULL) set_config_entry(opts, CONF_CONFIG_FILE, DEF_CONFIG_FILE); /* Parse configuration file to populate any params not already specified * via command-line options. */ parse_config_file(opts, opts->config[CONF_CONFIG_FILE]); /* If there are override configuration entries, process them * here. */ if(opts->config[CONF_OVERRIDE_CONFIG] != NULL) { /* Make a copy of the override_config string so we can munge it. */ strlcpy(override_file, opts->config[CONF_OVERRIDE_CONFIG], sizeof(override_file)); ndx = override_file; cmrk = strchr(ndx, ','); if(cmrk == NULL) { /* Only one to process... */ parse_config_file(opts, ndx); } else { /* Walk the string pulling the next config override * at the comma delimiters. */ while(cmrk != NULL) { *cmrk = '\0'; parse_config_file(opts, ndx); ndx = cmrk + 1; cmrk = strchr(ndx, ','); } /* Process the last entry */ parse_config_file(opts, ndx); } } /* Set up the verbosity level according to the value found in the * config files */ if (opts->config[CONF_VERBOSE] != NULL) { opts->verbose = strtol_wrapper(opts->config[CONF_VERBOSE], 0, -1, NO_EXIT_UPON_ERR, &is_err); if(is_err != FKO_SUCCESS) { log_msg(LOG_ERR, "[*] VERBOSE value '%s' not in the range (>0)", opts->config[CONF_VERBOSE]); clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE); } } /* Reset the options index so we can run through them again. */ optind = 0; /* Last, but not least, we process command-line options (some of which * may override configuration file options. */ while ((cmd_arg = getopt_long(argc, argv, GETOPTS_OPTION_STRING, cmd_opts, &index)) != -1) { switch(cmd_arg) { case 'a': set_config_entry(opts, CONF_ACCESS_FILE, optarg); break; case 'c': /* This was handled earlier */ break; case 'C': opts->packet_ctr_limit = strtol_wrapper(optarg, 0, (2 << 30), NO_EXIT_UPON_ERR, &is_err); if(is_err != FKO_SUCCESS) { log_msg(LOG_ERR, "[*] invalid -C packet count limit '%s'", optarg); clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE); } break; case 'd': #if USE_FILE_CACHE set_config_entry(opts, CONF_DIGEST_FILE, optarg); #else set_config_entry(opts, CONF_DIGEST_DB_FILE, optarg); #endif break; case 'D': opts->dump_config = 1; break; case 'f': opts->foreground = 1; break; case FW_LIST: opts->fw_list = 1; break; case FW_LIST_ALL: opts->fw_list = 1; opts->fw_list_all = 1; break; case FW_FLUSH: opts->fw_flush = 1; break; case GPG_HOME_DIR: if (is_valid_dir(optarg)) { set_config_entry(opts, CONF_GPG_HOME_DIR, optarg); } else { log_msg(LOG_ERR, "[*] Directory '%s' could not stat()/does not exist?", optarg); clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE); } break; case 'i': set_config_entry(opts, CONF_PCAP_INTF, optarg); break; case 'K': opts->kill = 1; break; case 'l': set_config_entry(opts, CONF_LOCALE, optarg); break; case 'O': /* This was handled earlier */ break; case 'p': set_config_entry(opts, CONF_FWKNOP_PID_FILE, optarg); break; case 'P': set_config_entry(opts, CONF_PCAP_FILTER, optarg); break; case PCAP_FILE: set_config_entry(opts, CONF_PCAP_FILE, optarg); break; case ENABLE_PCAP_ANY_DIRECTION: opts->pcap_any_direction = 1; break; case ROTATE_DIGEST_CACHE: opts->rotate_digest_cache = 1; break; case 'R': opts->restart = 1; break; case 'S': opts->status = 1; break; /* Verbosity level */ case 'v': opts->verbose++; break; case SYSLOG_ENABLE: opts->syslog_enable = 1; break; case 'V': fprintf(stdout, "fwknopd server %s\n", MY_VERSION); clean_exit(opts, NO_FW_CLEANUP, EXIT_SUCCESS); break; default: usage(); clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE); } } /* Now that we have all of our options set, and we are actually going to * start fwknopd, we can validate them. */ validate_options(opts); return; }
int main(int argc, char **argv) { // Handle command line arguments prog_options p; parse_options(&p,argc,argv); validate_options(&p); // Return value int r; // Init library r = png_init(0, 0); // Read current image png_file pf_current; png_file pf_previous; // Find motion if ( p.file_out != NULL ) { // Run until signal trap is reached signal(SIGINT, &trap); execute = 1; while(execute){ if(p.verbose || p.debug) fprintf(stdout,"\nNew round\n"); if(p.verbose || p.debug) gettimeofday(&tv1_total, NULL); // Read current image if(p.debug) gettimeofday(&tv1, NULL); r = open_png(&pf_current, p.file_current); png_write_error(r); if( r >= PNG_NO_ERROR ) { // Read "previous" image if( pf_previous.is_open != 1 ) { r = open_png(&pf_previous, p.file_current); png_write_error(r); } // Image instance from png motion_image image_current; image_from_png_data (&image_current, pf_current.data, pf_current.png_obj.width, pf_current.png_obj.height); motion_image image_previous; image_from_png_data (&image_previous, pf_previous.data, pf_current.png_obj.width, pf_current.png_obj.height); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Open file:\t\t%f seconds\n", get_timediff(&tv1,&tv2)); // Noise reduction if(p.debug) gettimeofday(&tv1, NULL); filter_noise_reduction (&(image_current.average),1); filter_noise_reduction (&(image_previous.average),1); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Noise reduction:\t%f seconds\n", get_timediff(&tv1,&tv2)); // Intensity correction if(p.debug) gettimeofday(&tv1, NULL); int intensity_current = get_average_intensity(&(image_current.average)); int intensity_previous = get_average_intensity(&(image_previous.average)); int intensity_difference_before = (intensity_current - intensity_previous) / 2; filter_adjust_intensity (&(image_current.average),-intensity_difference_before); filter_adjust_intensity (&(image_previous.average),intensity_difference_before); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Intensity correction:\t%f seconds\n", get_timediff(&tv1,&tv2)); // Background subtraction if(p.debug) gettimeofday(&tv1, NULL); filter_background_subtraction(&(image_current.average),&(image_previous.average)); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Background subtraction:\t%f seconds\n", get_timediff(&tv1,&tv2)); // Binary split if(p.debug) gettimeofday(&tv1, NULL); filter_split_binary(&(image_current.average),p.sensitivity); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Binary split:\t\t%f seconds\n", get_timediff(&tv1,&tv2)); // Blob reduction if(p.debug) gettimeofday(&tv1, NULL); int pixels_left = filter_reduce_blobs(&(image_current.average),p.passes); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Blob reduction:\t\t%f seconds\n", get_timediff(&tv1,&tv2)); if(p.verbose || p.debug) printf ("Pixels left:\t\t%i/10\n", pixels_left); if( pixels_left >= 10 ) { // Save file if(p.debug) gettimeofday(&tv1, NULL); // ToDo: Delegate file saving to thread char * filename = format_time(p.file_out); png_save_file(&pf_current,format_time(p.file_out)); free(filename); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("File save:\t\t%f seconds\n", get_timediff(&tv1,&tv2)); } // png_close_file(&pf_current.png_obj); // pf_current is closed as pf_previous on signal png_close_file(&pf_previous.png_obj); free(pf_previous.data); pf_previous = pf_current; free_image(image_current); free_image(image_previous); if(p.verbose || p.debug) gettimeofday(&tv2_total, NULL); if(p.verbose || p.debug) printf ("Total round time:\t%f seconds\n", get_timediff(&tv1_total,&tv2_total)); } else { pf_previous.is_open = 0; png_close_file(&pf_previous.png_obj); free(pf_previous.data); pf_previous.data = NULL; } } if(p.verbose || p.debug) printf ("\nExecution interrupted, cleaning up...\n"); r = png_close_file(&pf_previous.png_obj); free(pf_previous.data); signal(SIGINT, SIG_DFL); } // Find difference if ( p.file_out_difference != NULL ) { // Fixme: Move calculation to motion.c int change_treshold = 10; int x, y; for( x = 0 ; x < pf_current.png_obj.width ; x++ ) { for( y = 0 ; y < pf_current.png_obj.height ; y++ ) { int pixel_idx = (pf_current.png_obj.width*y*4+x*4); // Check for changes on pixel level int avg_new = (pf_current.data[pixel_idx]+pf_current.data[pixel_idx+1]+pf_current.data[pixel_idx+2])/3; int avg_old = (pf_previous.data[pixel_idx]+pf_previous.data[pixel_idx+1]+pf_previous.data[pixel_idx+2])/3; int difference = (avg_new - avg_old); if(!( abs(pf_current.data[pixel_idx]-pf_previous.data[pixel_idx]) > change_treshold || abs(pf_current.data[pixel_idx+1]-pf_previous.data[pixel_idx+1]) > change_treshold || abs(pf_current.data[pixel_idx+2]-pf_previous.data[pixel_idx+2]) > change_treshold || abs(difference) > change_treshold )) { pf_current.data[pixel_idx] = 0; pf_current.data[pixel_idx+1] = 0; pf_current.data[pixel_idx+2] = 0; pf_current.data[pixel_idx+3] = 0; } } } // Save difference png_save_file(&pf_current,p.file_out_difference); // Output file old r = png_close_file(&pf_current.png_obj); r = png_close_file(&pf_previous.png_obj); free(pf_current.data); free(pf_previous.data); } exit(EXIT_SUCCESS); }
/* Initialize program configuration via config file and/or command-line * switches. */ void config_init(fko_cli_options_t *options, int argc, char **argv) { int cmd_arg, index; /* Zero out options and opts_track. */ memset(options, 0x00, sizeof(fko_cli_options_t)); /* Make sure a few reasonable defaults are set */ set_defaults(options); /* First pass over cmd_line args to see if a named-stanza in the * rc file is used. */ while ((cmd_arg = getopt_long(argc, argv, GETOPTS_OPTION_STRING, cmd_opts, &index)) != -1) { switch(cmd_arg) { case 'h': usage(); exit(EXIT_SUCCESS); case 'n': options->no_save_args = 1; strlcpy(options->use_rc_stanza, optarg, MAX_LINE_LEN); break; case 'v': options->verbose++; break; } } /* First process the .fwknoprc file. */ process_rc(options); /* Reset the options index so we can run through them again. */ optind = 0; while ((cmd_arg = getopt_long(argc, argv, GETOPTS_OPTION_STRING, cmd_opts, &index)) != -1) { switch(cmd_arg) { case 'a': strlcpy(options->allow_ip_str, optarg, MAX_IPV4_STR_LEN); break; case 'A': strlcpy(options->access_str, optarg, MAX_LINE_LEN); break; case 'b': options->save_packet_file_append = 1; break; case 'B': strlcpy(options->save_packet_file, optarg, MAX_PATH_LEN); break; case 'C': strlcpy(options->server_command, optarg, MAX_LINE_LEN); break; case 'D': strlcpy(options->spa_server_str, optarg, MAX_SERVER_STR_LEN); break; case 'f': options->fw_timeout = atoi(optarg); if (options->fw_timeout < 0) { fprintf(stderr, "--fw-timeout must be >= 0\n"); exit(EXIT_FAILURE); } break; case 'g': case GPG_ENCRYPTION: options->use_gpg = 1; break; case 'G': strlcpy(options->get_key_file, optarg, MAX_PATH_LEN); break; case 'h': usage(); exit(EXIT_SUCCESS); case 'H': options->spa_proto = FKO_PROTO_HTTP; strlcpy(options->http_proxy, optarg, MAX_PATH_LEN); break; case SPA_ICMP_TYPE: options->spa_icmp_type = atoi(optarg); if (options->spa_icmp_type < 0 || options->spa_icmp_type > MAX_ICMP_TYPE) { fprintf(stderr, "Unrecognized icmp type value: %s\n", optarg); exit(EXIT_FAILURE); } break; case SPA_ICMP_CODE: options->spa_icmp_code = atoi(optarg); if (options->spa_icmp_code < 0 || options->spa_icmp_code > MAX_ICMP_CODE) { fprintf(stderr, "Unrecognized icmp code value: %s\n", optarg); exit(EXIT_FAILURE); } break; case 'l': options->run_last_command = 1; break; case 'm': case FKO_DIGEST_NAME: if((options->digest_type = digest_strtoint(optarg)) < 0) { fprintf(stderr, "* Invalid digest type: %s\n", optarg); exit(EXIT_FAILURE); } break; case NO_SAVE_ARGS: options->no_save_args = 1; break; case 'n': /* We already handled this earlier, so we do nothing here */ break; case 'N': strlcpy(options->nat_access_str, optarg, MAX_LINE_LEN); break; case 'p': options->spa_dst_port = atoi(optarg); if (options->spa_dst_port < 0 || options->spa_dst_port > MAX_PORT) { fprintf(stderr, "Unrecognized port: %s\n", optarg); exit(EXIT_FAILURE); } break; case 'P': if((options->spa_proto = proto_strtoint(optarg)) < 0) { fprintf(stderr, "Unrecognized protocol: %s\n", optarg); exit(EXIT_FAILURE); } break; case 'Q': strlcpy(options->spoof_ip_src_str, optarg, MAX_IPV4_STR_LEN); break; case 'r': options->rand_port = 1; break; case 'R': options->resolve_ip_http = 1; break; case RESOLVE_URL: if(options->resolve_url != NULL) free(options->resolve_url); options->resolve_url = malloc(strlen(optarg)+1); if(options->resolve_url == NULL) { fprintf(stderr, "Memory allocation error for resolve URL.\n"); exit(EXIT_FAILURE); } strlcpy(options->resolve_url, optarg, strlen(optarg)+1); break; case SHOW_LAST_ARGS: options->show_last_command = 1; break; case 's': strlcpy(options->allow_ip_str, "0.0.0.0", MAX_IPV4_STR_LEN); break; case 'S': options->spa_src_port = atoi(optarg); if (options->spa_src_port < 0 || options->spa_src_port > MAX_PORT) { fprintf(stderr, "Unrecognized port: %s\n", optarg); exit(EXIT_FAILURE); } break; case 'T': options->test = 1; break; case 'u': strlcpy(options->http_user_agent, optarg, HTTP_MAX_USER_AGENT_LEN); break; case 'U': strlcpy(options->spoof_user, optarg, MAX_USERNAME_LEN); break; case 'v': /* Handled earlier. */ break; case 'V': options->version = 1; break; case GPG_RECIP_KEY: options->use_gpg = 1; strlcpy(options->gpg_recipient_key, optarg, MAX_GPG_KEY_ID); break; case GPG_SIGNER_KEY: options->use_gpg = 1; strlcpy(options->gpg_signer_key, optarg, MAX_GPG_KEY_ID); break; case GPG_HOME_DIR: options->use_gpg = 1; strlcpy(options->gpg_home_dir, optarg, MAX_PATH_LEN); break; case GPG_AGENT: options->use_gpg = 1; options->use_gpg_agent = 1; break; case NAT_LOCAL: options->nat_local = 1; break; case NAT_RAND_PORT: options->nat_rand_port = 1; break; case NAT_PORT: options->nat_port = atoi(optarg); if (options->nat_port < 0 || options->nat_port > MAX_PORT) { fprintf(stderr, "Unrecognized port: %s\n", optarg); exit(EXIT_FAILURE); } break; case TIME_OFFSET_PLUS: options->time_offset_plus = parse_time_offset(optarg); break; case TIME_OFFSET_MINUS: options->time_offset_minus = parse_time_offset(optarg); break; default: usage(); exit(EXIT_FAILURE); } } /* Now that we have all of our options set, we can validate them. */ validate_options(options); return; }
int main(int argc, char *argv[]) { int ret, opt, option_index; while ((opt = getopt_long(argc, argv, "bcdgn:l:", long_options, &option_index)) >= 0) { switch (opt) { case 'c': if (vxlan_op >= 0) Usage(); vxlan_op = SANDESH_OP_ADD; create_set = 1; break; case 'd': if (vxlan_op >= 0) Usage(); vxlan_op = SANDESH_OP_DELETE; delete_set = 1; break; case 'g': if (vxlan_op >= 0) Usage(); vxlan_op = SANDESH_OP_GET; get_set = 1; break; case 'b': if (vxlan_op >= 0) Usage(); vxlan_op = SANDESH_OP_DUMP; dump_set = 1; break; case 'n': vxlan_nh = atoi(optarg); nh_set = 1; break; case 'l': vxlan_vnid = strtoul(optarg, NULL, 10); vnid_set = 1; break; case 0: parse_long_opts(option_index, optarg); break; case '?': default: Usage(); } } validate_options(); if ((vxlan_op == SANDESH_OP_DUMP) || (vxlan_op == SANDESH_OP_GET)) { printf("VXLAN Table\n\n"); printf(" VNID NextHop\n"); printf("----------------\n"); } cl = vr_get_nl_client(VR_NETLINK_PROTO_DEFAULT); if (!cl) { exit(1); } vr_vxlan_op(cl); return 0; }
/* ========================================================================= */ struct options *ParseOptions(int argc, char *argv[]) { struct options *o; int c; if (NULL == (o = init_options())) return(o); while ((c = getopt(argc, argv, "+hk:lru:v")) != EOF) { switch (c) { case '+': o->opt_debug = 1; o->opt_verbose = 1; break; case 'h': o->opt_help = 1; break; case 'l': o->opt_action = ACTION_LIST; break; case 'k': if ((optarg[0] > '0') && (optarg[0] <= '9')) { o->opt_action = ACTION_KILL; o->kill_sig = atoi(optarg); } else { fprintf(stderr, "ERROR: Unable to parse -k option of \"%s\".\n", optarg); o->opt_error = 1; } break; case 'r': o->opt_rake = 1; break; case 'u': if ( optarg[0] == '-' ) { o->opt_error = 1; fprintf(stderr, "ERROR: Unable to parse -u user name \"%s\".\n", optarg); } else { if (NULL == (o->ufilter_name = make_string(optarg))) o->opt_error = 1; else o->opt_ufilter = 1; } break; case 'v': o->opt_verbose = 1; break; case '?': o->opt_error = 1; } } /* No memory clean up (we will exit soon anyways). The called function will handle the error message (if appropriate). */ if (validate_options(o)) return(NULL); return(o); }
int main(int argc, char *argv[]) { int ret, opt, option_index; mirror_fill_nl_callbacks(); while ((opt = getopt_long(argc, argv, "bcdgn:m:", long_options, &option_index)) >= 0) { switch (opt) { case 'c': if (mirror_op >= 0) Usage(); create_set = 1; mirror_op = SANDESH_OP_ADD; break; case 'd': if (mirror_op >= 0) Usage(); delete_set = 1; mirror_op = SANDESH_OP_DEL; break; case 'g': if (mirror_op >= 0) Usage(); get_set = 1; mirror_op = SANDESH_OP_GET; break; case 'b': if (mirror_op >= 0) Usage(); dump_set = 1; mirror_op = SANDESH_OP_DUMP; break; case 'n': mirror_nh = atoi(optarg); nh_set = 1; break; case 'm': mirror_index = atoi(optarg); mirror_set = 1; break; case 0: parse_long_opts(option_index, optarg); break; case '?': default: Usage(); break; } } validate_options(); if ((mirror_op == SANDESH_OP_DUMP) || (mirror_op == SANDESH_OP_GET)) { printf("Mirror Table\n\n"); printf("Flags:D=Dynamic Mirroring, Hw=NIC Assisted Mirroring \n\n"); printf("Index NextHop Flags VNI Vlan\n"); printf("------------------------------------------------\n"); } cl = vr_get_nl_client(VR_NETLINK_PROTO_DEFAULT); if (!cl) { exit(1); } vr_mirror_op(cl); return 0; }