/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); int arg_index; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* register the options */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* parse the options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { /* options parsing should print error */ skAppUsage(); /* never returns */ } /* check for extraneous arguments */ if (arg_index != argc) { skAppPrintErr("Too many arguments or unrecognized switch '%s'", argv[arg_index]); skAppUsage(); /* never returns */ } /* verify logging */ if (sklogOptionsVerify()) { exit(EXIT_FAILURE); } /* write command line */ sklogCommandLine(argc, argv); /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } return; /* OK */ }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); /* verify same number of options and help strings */ assert((sizeof(appHelp) / sizeof(char *)) == (sizeof(appOptions) / sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* register the options */ if (skOptionsRegister(appOptions, & appOptionsHandler, NULL)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* register the teardown hanlder */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { skAppUsage(); /* never returns */ } /* get the pattern */ pattern = argv[arg_index++]; if (NULL == pattern) { skAppPrintErr("No pattern specified"); skAppUsage(); } /* either need name of set file(s) after options or a set file on stdin */ if ((arg_index == argc) && (FILEIsATty(stdin))) { skAppPrintErr("No files on the command line and" " stdin is connected to a terminal"); skAppUsage(); } return; /* OK */ }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* register the options */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL) || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse the options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { /* options parsing should print error */ skAppUsage(); /* never returns */ } /* try to load site config file; if it fails, we will not be able * to resolve flowtype and sensor from input file names */ sksiteConfigure(0); /* arg_index is looking at first file name to process */ if (arg_index + 2 != argc) { skAppPrintErr("Expected two file names on the command line"); skAppUsage(); /* never returns */ } return; /* OK */ }
int main(int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); int arg_index; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* register the options */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL) || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* parse the options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { /* options parsing should print error */ skAppUsage(); /* never returns */ } /* ensure the site config is available */ if (sksiteConfigure(1)) { exit(EXIT_FAILURE); } /* if we are printing more than one sensor and a --print-* switch * was given, set the width of the sensor name fields so the * values align */ if ((argc != arg_index + 1) && (print_descriptions || print_classes)) { sensor_name_width = -1 * (int)sksiteSensorGetMaxNameStrLen(); } if (argc > arg_index) { for ( ; arg_index < argc; ++arg_index) { printByNameOrNumber(argv[arg_index]); } } else { /* no args. print all */ printAllSensors(); } skAppUnregister(); return 0; }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); int arg_index; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); memset(&prefixmap_test_opt, 0, sizeof(prefixmap_test_opt)); /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* register the options */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* parse the options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { /* options parsing should print error */ skAppUsage(); /* never returns */ } if ( (NULL == prefixmap_test_opt.map_file) || (0 == prefixmap_test_opt.have_address) ) { if ( NULL == prefixmap_test_opt.map_file ) { skAppPrintErr("Required argument map-file not provided."); } if ( 0 == prefixmap_test_opt.have_address ) { skAppPrintErr("Required argument address not provided."); } exit(EXIT_FAILURE); } return; /* OK */ }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); unsigned int optctx_flags; sk_file_header_t *silk_hdr; int logmask; int rv; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); optctx_flags = (SK_OPTIONS_CTX_INPUT_BINARY | SK_OPTIONS_CTX_XARGS); /* register the options */ if (skOptionsCtxCreate(&optctx, optctx_flags) || skOptionsCtxOptionsRegister(optctx) || skOptionsRegister(appOptions, &appOptionsHandler, NULL) || skOptionsNotesRegister(NULL) || skCompMethodOptionsRegister(&comp_method)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* enable the logger */ sklogSetup(0); sklogSetDestination("stderr"); sklogSetStampFunction(&logprefix); /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse the options */ rv = skOptionsCtxOptionsParse(optctx, argc, argv); if (rv < 0) { skAppUsage(); /* never returns */ } if ('\0' == log_destination[0]) { strncpy(log_destination, LOG_DESTINATION_DEFAULT, sizeof(log_destination)); } else { sklogSetLevel("debug"); } sklogSetDestination(log_destination); /* default output is "stdout" */ if (!silk_output) { if ((rv =skStreamCreate(&silk_output,SK_IO_WRITE,SK_CONTENT_SILK_FLOW)) || (rv = skStreamBind(silk_output, "-"))) { skStreamPrintLastErr(silk_output, rv, &skAppPrintErr); exit(EXIT_FAILURE); } } /* get the header */ silk_hdr = skStreamGetSilkHeader(silk_output); /* open the output */ if ((rv = skHeaderSetCompressionMethod(silk_hdr, comp_method)) || (rv = skOptionsNotesAddToStream(silk_output)) || (rv = skHeaderAddInvocation(silk_hdr, 1, argc, argv)) || (rv = skStreamOpen(silk_output)) || (rv = skStreamWriteSilkHeader(silk_output))) { skStreamPrintLastErr(silk_output, rv, &skAppPrintErr); exit(EXIT_FAILURE); } if (skpcSetup()) { exit(EXIT_FAILURE); } if (skpcProbeCreate(&probe, PROBE_ENUM_NETFLOW_V5)) { exit(EXIT_FAILURE); } skpcProbeSetName(probe, skAppName()); skpcProbeSetFileSource(probe, "/dev/null"); if (parseLogFlags(log_flags)) { exit(EXIT_FAILURE); } if (skpcProbeVerify(probe, 0)) { exit(EXIT_FAILURE); } /* set level to "warning" to avoid the "Started logging" message */ logmask = sklogGetMask(); sklogSetLevel("warning"); sklogOpen(); sklogSetMask(logmask); return; /* OK */ }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); int arg_index; int rv; /* check that we have the same number of options entries and help*/ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* initialize globals */ shuttingdown = 0; destination_dir = NULL; duplicate_dirs = skDLListCreate(NULL); if (duplicate_dirs == NULL) { skAppPrintOutOfMemory("duplicate directory list"); exit(EXIT_FAILURE); } open_file_list = skDLListCreate(NULL); if (open_file_list == NULL) { skAppPrintOutOfMemory("open file list"); exit(EXIT_FAILURE); } transfers = transferIdentTreeCreate(); if (transfers == NULL) { skAppPrintOutOfMemory("receiver data structure"); exit(EXIT_FAILURE); } #ifdef SK_HAVE_STATVFS { uint64_t tmp_64; rv = skStringParseHumanUint64(&tmp_64, DEFAULT_FREESPACE_MINIMUM, SK_HUMAN_NORMAL); if (rv) { skAppPrintErr("Bad default value for freespace_minimum: '%s': %s", DEFAULT_FREESPACE_MINIMUM,skStringParseStrerror(rv)); exit(EXIT_FAILURE); } freespace_minimum = (int64_t)tmp_64; } #endif /* SK_HAVE_STATVFS */ /* register the options and handler */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL)) { skAppPrintErr("Unable to register application options"); exit(EXIT_FAILURE); } /* Register the other transfer options */ if (transferSetup()) { exit(EXIT_FAILURE); } /* rwreceiver runs as a daemon */ if (skdaemonSetup((SKLOG_FEATURE_LEGACY | SKLOG_FEATURE_SYSLOG), argc, argv)) { exit(EXIT_FAILURE); } /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse the options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { /* options parsing should print error */ skAppUsage(); /* never returns */ } /* Verify the options */ rv = rwreceiverVerifyOptions(); /* verify the required options for logging */ if (skdaemonOptionsVerify()) { rv = -1; } if (rv) { skAppUsage(); /* never returns */ } /* check for extraneous arguments */ if (arg_index != argc) { skAppPrintErr("Too many arguments or unrecognized switch --%s", argv[arg_index]); skAppUsage(); /* never returns */ } /* Identify the main thread */ skthread_init("main"); return; /* OK */ }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); int rv; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* initialize globals */ #error "Initialize any global variables here" /* for example: set global output to NULL */ out_stream = NULL; /* register the options */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL) || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } #error "Do any other module setup here" /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse the options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { /* options parsing should print error */ skAppUsage(); /* never returns */ } /* try to load site config file; if it fails, we will not be able * to resolve flowtype and sensor from input file names */ sksiteConfigure(0); #error "Do any options validation here" #error "If you expect filenames on command line keep this:" /* arg_index is looking at first file name to process */ if (arg_index == argc) { if (FILEIsATty(stdin)) { skAppPrintErr("No input files on command line and" " stdin is connected to a terminal"); skAppUsage(); /* never returns */ } } #error "If you do NOT expect filenames on command line, keep this:" /* check for extraneous arguments */ if (arg_index != argc) { skAppPrintErr("Too many arguments or unrecognized switch '%s'", argv[arg_index]); skAppUsage(); /* never returns */ } #error "Once all options are set, open input and output" /* for example, open a SiLK flow file as an output file */ rv = skStreamOpenSilkFlow(&out_stream, output_path, SK_IO_WRITE); if (rv) { skStreamPrintLastErr(out_stream, rv, &skAppPrintErr); skAppPrintErr("Unable to open output file. Exiting"); skStreamDestroy(&out_stream); exit(EXIT_FAILURE); } return; /* OK */ }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); const char *in_fname; const char *out_fname; int arg_index; int rv; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* initialize globals */ memset(&set_options, 0, sizeof(skipset_options_t)); set_options.argc = argc; set_options.argv = argv; /* register the options */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL) || skIPSetOptionsRegister(&set_options)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse the options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { skAppUsage(); /* never returns */ } /* default is to read from stdin and write to stdout */ in_fname = "-"; out_fname = "-"; /* process files named on the command line */ switch (argc - arg_index) { case 2: in_fname = argv[arg_index++]; out_fname = argv[arg_index++]; break; case 1: in_fname = argv[arg_index++]; break; case 0: /* Do not allow reading from a tty when no input */ if (FILEIsATty(stdin)) { skAppPrintErr("Must specify '-' as the input to read" " from a terminal"); exit(EXIT_FAILURE); } break; default: skAppPrintErr("Too many arguments;" " a maximum of two files may be specified"); skAppUsage(); } /* we should have processed all arguments */ assert(arg_index == argc); /* create the IPset */ if (skIPSetCreate(&ipset, 0)) { EXIT_NO_MEMORY; } skIPSetOptionsBind(ipset, &set_options); /* create input */ if ((rv = skStreamCreate(&in_stream, SK_IO_READ, SK_CONTENT_TEXT)) || (rv = skStreamBind(in_stream, in_fname)) || (rv = skStreamSetCommentStart(in_stream, "#"))) { skStreamPrintLastErr(in_stream, rv, &skAppPrintErr); exit(EXIT_FAILURE); } /* create output */ if ((rv = skStreamCreate(&out_stream, SK_IO_WRITE, SK_CONTENT_SILK)) || (rv = skStreamBind(out_stream, out_fname))) { skStreamPrintLastErr(out_stream, rv, &skAppPrintErr); exit(EXIT_FAILURE); } /* open the streams */ rv = skStreamOpen(in_stream); if (rv) { skStreamPrintLastErr(in_stream, rv, &skAppPrintErr); exit(EXIT_FAILURE); } rv = skStreamOpen(out_stream); if (rv) { skStreamPrintLastErr(out_stream, rv, &skAppPrintErr); exit(EXIT_FAILURE); } }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); unsigned int optctx_flags; int rv; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* initialize globals */ memset(&ab_options, 0, sizeof(sk_aggbag_options_t)); ab_options.existing_silk_files = 1; ab_options.argc = argc; ab_options.argv = argv; optctx_flags = (SK_OPTIONS_CTX_INPUT_SILK_FLOW | SK_OPTIONS_CTX_ALLOW_STDIN | SK_OPTIONS_CTX_XARGS | SK_OPTIONS_CTX_COPY_INPUT | SK_OPTIONS_CTX_PRINT_FILENAMES); /* register the options */ if (skOptionsCtxCreate(&optctx, optctx_flags) || skOptionsCtxOptionsRegister(optctx) || skOptionsRegister(appOptions, &appOptionsHandler, NULL) || skAggBagOptionsRegister(&ab_options) || skIPv6PolicyOptionsRegister(&ipv6_policy) || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); exit(EXIT_FAILURE); } /* parse options */ rv = skOptionsCtxOptionsParse(optctx, argc, argv); if (rv < 0) { skAppUsage(); /* never returns */ } /* try to load site config file; if it fails, we will not be able * to resolve flowtype and sensor from input file names, but we * should not consider it a complete failure */ sksiteConfigure(0); /* make sure the user specified at least one key field and one * counter field */ if (keys_arg == NULL || keys_arg[0] == '\0') { skAppPrintErr("The --%s switch is required", appOptions[OPT_KEYS].name); skAppUsage(); /* never returns */ } if (counters_arg == NULL || counters_arg[0] == '\0') { skAppPrintErr("The --%s switch is required", appOptions[OPT_COUNTERS].name); skAppUsage(); /* never returns */ } /* set up the key_name_map and counter_name_map */ if (createStringmaps()) { exit(EXIT_FAILURE); } /* create the aggregate bag */ if (skAggBagCreate(&ab)) { exit(EXIT_FAILURE); } skAggBagOptionsBind(ab, &ab_options); /* parse the --keys and --counters switches */ if (parseFields(key_name_map, keys_arg, OPT_KEYS)) { exit(EXIT_FAILURE); } if (parseFields(counter_name_map, counters_arg, OPT_COUNTERS)) { exit(EXIT_FAILURE); } /* create output stream to stdout if no --output-path was given */ if (NULL == output) { if ((rv = skStreamCreate(&output, SK_IO_WRITE, SK_CONTENT_SILK)) || (rv = skStreamBind(output, "-"))) { skStreamPrintLastErr(output, rv, &skAppPrintErr); skStreamDestroy(&output); exit(EXIT_FAILURE); } } /* make certain stdout is not being used for multiple outputs */ if (skStreamIsStdout(output) && skOptionsCtxCopyStreamIsStdout(optctx)) { skAppPrintErr("May not use stdout for multiple output streams"); exit(EXIT_FAILURE); } /* open the output stream but do not write anything yet */ rv = skStreamOpen(output); if (rv) { skStreamPrintLastErr(output, rv, &skAppPrintErr); skStreamDestroy(&output); exit(EXIT_FAILURE); } /* open the --copy-input stream */ if (skOptionsCtxOpenStreams(optctx, &skAppPrintErr)) { exit(EXIT_FAILURE); } return; /* OK */ }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); int arg_index; int rv; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* register the options */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* register the teardown hanlder */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse the options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { /* options parsing should print error */ skAppUsage(); /* never returns */ } /* check for extraneous arguments */ if (arg_index != argc) { skAppPrintErr("Too many arguments or unrecognized switch '%s'", argv[arg_index]); skAppUsage(); /* never returns */ } /* check for one and only one of --input-file and --address */ if (NULL != app_opt.input_file) { if (NULL != app_opt.address) { skAppPrintErr("Only one of --%s or --%s may be specified.", appOptions[OPT_ADDRESS].name, appOptions[OPT_INPUT_FILE].name); skAppUsage(); } } else if (NULL == app_opt.address) { skAppPrintErr("Either the --%s or --%s option is required.", appOptions[OPT_ADDRESS].name, appOptions[OPT_INPUT_FILE].name); skAppUsage(); } /* find and load the map file */ if (skCountrySetup(app_opt.map_file, &skAppPrintErr)) { exit(EXIT_FAILURE); } /* use default for print-ips if unset by user */ if (-1 == app_opt.print_ips) { if (app_opt.input_file) { app_opt.print_ips = 1; } else { app_opt.print_ips = 0; } } /* create the output stream. Only page output when processing * input file */ if ((rv = skStreamCreate(&out, SK_IO_WRITE, SK_CONTENT_TEXT)) || (rv = skStreamBind(out, app_opt.output_path))) { skStreamPrintLastErr(out, rv, &skAppPrintErr); exit(EXIT_FAILURE); } if (app_opt.input_file) { rv = skStreamPageOutput(out, app_opt.pager); if (rv) { skStreamPrintLastErr(out, rv, &skAppPrintErr); exit(EXIT_FAILURE); } } rv = skStreamOpen(out); if (rv) { skStreamPrintLastErr(out, rv, &skAppPrintErr); exit(EXIT_FAILURE); } return; /* OK */ }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); unsigned int optctx_flags; int rv; int i; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); assert(PREFIX_COUNT == _FINAL_MASK_); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* initialize variables */ memset(net_mask, 0, sizeof(net_mask)); optctx_flags = (SK_OPTIONS_CTX_INPUT_SILK_FLOW | SK_OPTIONS_CTX_ALLOW_STDIN | SK_OPTIONS_CTX_XARGS | SK_OPTIONS_CTX_PRINT_FILENAMES); /* register the options */ if (skOptionsCtxCreate(&optctx, optctx_flags) || skOptionsCtxOptionsRegister(optctx) || skOptionsRegister(appOptions, &appOptionsHandler, NULL) || skOptionsRegister(legacyOptions, &appOptionsHandler, NULL) || skIPv6PolicyOptionsRegister(&ipv6_policy) || skOptionsNotesRegister(NULL) || skCompMethodOptionsRegister(&comp_method) || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse options */ rv = skOptionsCtxOptionsParse(optctx, argc, argv); if (rv < 0) { skAppUsage(); /* never returns */ } /* make certain at least one mask was specified */ for (i = 0; i < PREFIX_COUNT; ++i) { if (net_mask[i].bits6 || net_mask[i].bits4) { break; } } if (i == PREFIX_COUNT) { skAppPrintErr("Must specify at least one prefix length option"); skAppUsage(); } /* check the output */ if (output_path == NULL) { output_path = "-"; } }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. * * NOTE: Make this extern due to declaration in rwfilter.h. */ void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); int rv; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* register the options */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* Set up the fglob module */ if (fglobSetup()){ skAppPrintErr("Unable to setup fglob module"); exit(EXIT_FAILURE); } /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* Parse the options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { skAppUsage(); } /* try to load site config file; if it fails, we will not be able * to resolve flowtype and sensor from input file names */ sksiteConfigure(0); /* check for extraneous arguments */ if (arg_index != argc) { skAppPrintErr("Too many arguments or unrecognized switch '%s'", argv[arg_index]); skAppUsage(); } /* verify that an fglob option was provided and that everything is * ok with the setup. */ rv = fglobValid(); if (rv == -1) { exit(EXIT_FAILURE); } if (rv == 0) { skAppPrintErr("Must specify at least one file selection switch"); exit(EXIT_FAILURE); } return; /* OK */ }
int main(int argc, char **argv) { char errbuf[2 * PATH_MAX]; const char *filename = NULL; skstream_t *stream = NULL; skIPWildcard_t ipwild; skipset_t *input_set = NULL; skipset_t *wild_set = NULL; char buf[64]; int found_match = 0; /* application return value */ int rv; appSetup(argc, argv); /* never returns on error */ /* Build an IP wildcard from the pattern argument */ rv = skStringParseIPWildcard(&ipwild, pattern); if (rv) { skAppPrintErr("Invalid IP '%s': %s", pattern, skStringParseStrerror(rv)); skAppUsage(); } if (count && !quiet) { /* Create an IPset containing the IPwildcard */ if ((rv = skIPSetCreate(&wild_set, skIPWildcardIsV6(&ipwild))) || (rv = skIPSetInsertIPWildcard(wild_set, &ipwild)) || (rv = skIPSetClean(wild_set))) { skAppPrintErr("Unable to create temporary IPset: %s", skIPSetStrerror(rv)); return EXIT_FAILURE; } } /* Iterate over the set files */ while ((filename = appNextInput(argc, argv)) != NULL) { /* Load the input set */ if ((rv = skStreamCreate(&stream, SK_IO_READ, SK_CONTENT_SILK)) || (rv = skStreamBind(stream, filename)) || (rv = skStreamOpen(stream))) { skStreamLastErrMessage(stream, rv, errbuf, sizeof(errbuf)); skAppPrintErr("Unable to read IPset from '%s': %s", filename, errbuf); skStreamDestroy(&stream); continue; } rv = skIPSetRead(&input_set, stream); if (rv) { if (SKIPSET_ERR_FILEIO == rv) { skStreamLastErrMessage(stream, skStreamGetLastReturnValue(stream), errbuf, sizeof(errbuf)); } else { strncpy(errbuf, skIPSetStrerror(rv), sizeof(errbuf)); } skAppPrintErr("Unable to read IPset from '%s': %s", filename, errbuf); skStreamDestroy(&stream); continue; } skStreamDestroy(&stream); if (quiet || !count) { /* Only need to check for a match */ if (skIPSetCheckIPWildcard(input_set, &ipwild)) { found_match = 1; if (quiet) { goto done; } printf("%s\n", filename); } } else { /* Need a count of IPs, so intersect */ rv = skIPSetIntersect(input_set, wild_set); if (rv) { skAppPrintErr("Unable to intersect IPsets: %s", skIPSetStrerror(rv)); skIPSetDestroy(&input_set); skIPSetDestroy(&wild_set); return EXIT_FAILURE; } printf("%s:%s\n", filename, skIPSetCountIPsString(input_set, buf, sizeof(buf))); if ('0' != buf[0]) { found_match = 1; } } skIPSetDestroy(&input_set); } done: /* done */ skIPSetDestroy(&input_set); skIPSetDestroy(&wild_set); return ((found_match) ? 0 : 1); }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); char errbuf[PCAP_ERRBUF_SIZE]; int arg_index; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char*)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* register the options */ if (skOptionsRegister(appOptions, &appOptionsHandler, NULL)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse options */ arg_index = skOptionsParse(argc, argv); if (arg_index < 0) { skAppUsage(); /* never returns */ } /* verify input file */ if (arg_index >= argc) { skAppPrintErr("No input packet file found"); skAppUsage(); /* never returns */ } /* verify output is not a terminal */ if (FILEIsATty(stdout)) { skAppPrintErr("stdout is connected to a terminal"); skAppUsage(); /* never returns */ } /* open input file */ g_pkt_input = pcap_open_offline(argv[arg_index], errbuf); if (g_pkt_input == NULL) { skAppPrintErr("Unable to open input file %s: %s", argv[arg_index], errbuf); exit(EXIT_FAILURE); } /* open output file (tcpdump format packet file) */ g_output = pcap_open_dead(pcap_datalink(g_pkt_input), pcap_snapshot(g_pkt_input)); if (g_output == NULL) { skAppPrintErr("Error opening stdout: %s", errbuf); exit(EXIT_FAILURE); } g_output_dumper = pcap_dump_open(g_output, "-"); if (g_output_dumper == NULL) { skAppPrintErr("Error opening stdout: %s", pcap_geterr(g_output)); exit(EXIT_FAILURE); } return; /* OK */ }
/* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ void appSetup( int argc, char **argv) { SILK_FEATURES_DEFINE_STRUCT(features); int optctx_flags; sktime_t t; unsigned int end_precision; unsigned int is_epoch; int64_t bin_count; int rv; /* make sure count of option's declarations and help-strings match */ assert((sizeof(appOptions)/sizeof(struct option)) == (sizeof(appHelp)/sizeof(char *))); /* register the application */ skAppRegister(argv[0]); skAppVerifyFeatures(&features, NULL); skOptionsSetUsageCallback(&appUsageLong); /* initialize globals */ memset(&flags, 0, sizeof(flags)); flags.delimiter = '|'; flags.load_scheme = DEFAULT_LOAD_SCHEME; memset(&output, 0, sizeof(output)); output.of_fp = stdout; memset(&bins, 0, sizeof(bins)); bins.start_time = RWCO_UNINIT_START; bins.end_time = RWCO_UNINIT_END; bins.size = DEFAULT_BINSIZE; optctx_flags = (SK_OPTIONS_CTX_INPUT_SILK_FLOW | SK_OPTIONS_CTX_ALLOW_STDIN | SK_OPTIONS_CTX_XARGS | SK_OPTIONS_CTX_PRINT_FILENAMES | SK_OPTIONS_CTX_COPY_INPUT); /* register the options */ if (skOptionsCtxCreate(&optctx, optctx_flags) || skOptionsCtxOptionsRegister(optctx) || skOptionsRegister(appOptions, &appOptionsHandler, NULL) || skOptionsRegister(deprecatedOptions, &appOptionsHandler, NULL) || skOptionsRegister(deprecatedOptionsShort, &appOptionsHandler, NULL) || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE)) { skAppPrintErr("Unable to register options"); exit(EXIT_FAILURE); } /* register the teardown handler */ if (atexit(appTeardown) < 0) { skAppPrintErr("Unable to register appTeardown() with atexit()"); appTeardown(); exit(EXIT_FAILURE); } /* parse options; print usage if error */ rv = skOptionsCtxOptionsParse(optctx, argc, argv); if (rv < 0) { skAppUsage(); } /* try to load site config file; if it fails, we will not be able * to resolve flowtype and sensor from input file names */ sksiteConfigure(0); /* parse the times */ if (start_time) { rv = skStringParseDatetime(&bins.start_time, start_time, NULL); if (rv) { skAppPrintErr("Invalid %s '%s': %s", appOptions[OPT_START_TIME].name, start_time, skStringParseStrerror(rv)); exit(EXIT_FAILURE); } } if (end_time) { rv = skStringParseDatetime(&t, end_time, &end_precision); if (rv) { skAppPrintErr("Invalid %s '%s': %s", appOptions[OPT_END_TIME].name, end_time, skStringParseStrerror(rv)); exit(EXIT_FAILURE); } /* get the precision; treat epoch time as seconds resolution * unless its precision is already seconds or milliseconds */ is_epoch = SK_PARSED_DATETIME_EPOCH & end_precision; end_precision &= SK_PARSED_DATETIME_MASK_PRECISION; if (is_epoch && end_precision < SK_PARSED_DATETIME_SECOND) { end_precision = SK_PARSED_DATETIME_SECOND; } if (start_time) { /* move end-time to its ceiling */ skDatetimeCeiling(&t, &t, end_precision); ++t; /* verify times */ if (t <= bins.start_time) { char buf_s[SKTIMESTAMP_STRLEN]; char buf_e[SKTIMESTAMP_STRLEN]; skAppPrintErr("The %s is less than %s: %s < %s", appOptions[OPT_END_TIME].name, appOptions[OPT_START_TIME].name, sktimestamp_r(buf_e, t, SKTIMESTAMP_NOMSEC), sktimestamp_r(buf_s, bins.start_time, SKTIMESTAMP_NOMSEC)); exit(EXIT_FAILURE); } /* make certain end-time fails on a boundary by computing * the number of bins required to hold the end-time. we * subtract 1 then add 1 to get a valid count whether or * not the division would have a remainder. */ bin_count = 1 + ((t - bins.start_time - 1) / bins.size); bins.end_time = bins.start_time + bins.size * bin_count; } else { /* when only end_time is given, create bins up to its * ceiling value */ bins.end_time = t; skDatetimeCeiling(&t, &t, end_precision); ++t; /* determine the number of bins between the end-time * specified by the user and the ceiling of that end-time, * then increase the end time by that number of bins */ bin_count = 1 + ((t - bins.end_time - 1) / bins.size); bins.end_time += bin_count * bins.size; } } /* make certain stdout is not being used for multiple outputs */ if (skOptionsCtxCopyStreamIsStdout(optctx)) { if ((NULL == output.of_name) || (0 == strcmp(output.of_name, "-")) || (0 == strcmp(output.of_name, "stdout"))) { skAppPrintErr("May not use stdout for multiple output streams"); exit(EXIT_FAILURE); } } /* open the --output-path: the 'of_name' member is non-NULL when * the switch is given */ if (output.of_name) { rv = skFileptrOpen(&output, SK_IO_WRITE); if (rv) { skAppPrintErr("Cannot open '%s': %s", output.of_name, skFileptrStrerror(rv)); exit(EXIT_FAILURE); } } /* looks good, open the --copy-input destination */ if (skOptionsCtxOpenStreams(optctx, &skAppPrintErr)) { exit(EXIT_FAILURE); } return; /* OK */ }