/* * status = verifyCommandString(command); * * Verify the the command string specified in 'command' does not * contain unknown conversions. If 'command' is valid, return 0. * * If 'command' is not valid, print an error and return -1. */ static int verifyCommandString( const char *command) { const char *cp = command; while (NULL != (cp = strchr(cp, '%'))) { ++cp; switch (*cp) { case '%': case 'I': case 's': ++cp; break; case '\0': skAppPrintErr(("Invalid %s '%s':" " '%%' appears at end of string"), appOptions[OPT_POST_COMMAND].name, command); return -1; default: skAppPrintErr("Invalid %s '%s': Unknown conversion '%%%c'", appOptions[OPT_POST_COMMAND].name, command, *cp); return -1; } } return 0; }
/* * status = ipv6routingioPrepare(stream); * * Sets the record version to the default if it is unspecified, * checks that the record format supports the requested record * version, sets the record length, and sets the pack and unpack * functions for this record format and version. */ int ipv6routingioPrepare( skstream_t *stream) { #define FILE_FORMAT "FT_RWIPV6ROUTING" sk_file_header_t *hdr = stream->silk_hdr; int rv = SKSTREAM_OK; /* return value */ /* Set version if none was selected by caller */ if ((stream->io_mode == SK_IO_WRITE) && (skHeaderGetRecordVersion(hdr) == SK_RECORD_VERSION_ANY)) { skHeaderSetRecordVersion(hdr, DEFAULT_RECORD_VERSION); } /* version check; set values based on version */ switch (skHeaderGetRecordVersion(hdr)) { case 3: stream->rwUnpackFn = &ipv6routingioRecordUnpack_V3; stream->rwPackFn = &ipv6routingioRecordPack_V3; break; case 2: stream->rwUnpackFn = &ipv6routingioRecordUnpack_V2; stream->rwPackFn = &ipv6routingioRecordPack_V1; break; case 1: stream->rwUnpackFn = &ipv6routingioRecordUnpack_V1; stream->rwPackFn = &ipv6routingioRecordPack_V1; break; case 0: default: rv = SKSTREAM_ERR_UNSUPPORT_VERSION; goto END; } stream->recLen = ipv6routingioGetRecLen(skHeaderGetRecordVersion(hdr)); /* verify lengths */ if (stream->recLen == 0) { skAppPrintErr("Record length not set for %s version %u", FILE_FORMAT, (unsigned)skHeaderGetRecordVersion(hdr)); skAbort(); } if (stream->recLen != skHeaderGetRecordLength(hdr)) { if (0 == skHeaderGetRecordLength(hdr)) { skHeaderSetRecordLength(hdr, stream->recLen); } else { skAppPrintErr(("Record length mismatch for %s version %u\n" "\tcode = %" PRIu16 " bytes; header = %lu bytes"), FILE_FORMAT, (unsigned)skHeaderGetRecordVersion(hdr), stream->recLen, (unsigned long)skHeaderGetRecordLength(hdr)); skAbort(); } } END: return rv; }
/* * status = appOptionsHandler(cData, opt_index, opt_arg); * * This function is passed to skOptionsRegister(); it will be called * by skOptionsParse() for each user-specified switch that the * application has registered; it should handle the switch as * required---typically by setting global variables---and return 1 * if the switch processing failed or 0 if it succeeded. Returning * a non-zero from from the handler causes skOptionsParse() to return * a negative value. * * The clientData in 'cData' is typically ignored; 'opt_index' is * the index number that was specified as the last value for each * struct option in appOptions[]; 'opt_arg' is the user's argument * to the switch for options that have a REQUIRED_ARG or an * OPTIONAL_ARG. */ static int appOptionsHandler( clientData UNUSED(cData), int opt_index, char *opt_arg) { uint32_t val; int rv; switch ((appOptionsEnum)opt_index) { case OPT_TEST_FEATURES: rv = skStringParseUint32(&val, opt_arg, 0, (SKLOG_FEATURE_LEGACY | SKLOG_FEATURE_SYSLOG)); if (rv) { goto PARSE_ERROR; } if (sklogSetup((int)val)) { skAppPrintErr("Unable to setup log"); exit(EXIT_FAILURE); } break; case OPT_TEST_EMERG: test_emerg = 1; break; case OPT_TEST_THREADED: test_threaded = 1; sklogEnableThreadedLogging(); break; case OPT_REPEAT_COUNT: rv = skStringParseUint32(&val, opt_arg, 0, 0); if (rv) { goto PARSE_ERROR; } repeat_count = val; break; case OPT_REPEAT_DELAY: rv = skStringParseUint32(&val, opt_arg, 0, 0); if (rv) { goto PARSE_ERROR; } repeat_delay = val; break; } return 0; /* OK */ PARSE_ERROR: skAppPrintErr("Invalid %s '%s': %s", appOptions[opt_index].name, opt_arg, skStringParseStrerror(rv)); return 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); 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); 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 */ }
/* * Parse the argument to the --log-flags switch. */ static int parseLogFlags( const char *log_flags_str) { char *log_flags_copy = NULL; char *flag_next; char *flag; int rv = -1; skpcProbeClearLogFlags(probe); if (NULL == log_flags_str) { return 0; } /* create a copy of the input string and maintain a reference to * it so we can free it */ log_flags_copy = strdup(log_flags_str); flag_next = log_flags_copy; if (NULL == log_flags_copy) { skAppPrintOutOfMemory(NULL); goto END; } /* parse the flags as a comma separated list of tokens */ while ((flag = strsep(&flag_next, ",")) != NULL) { /* check for empty token (e.g., double comma) */ if ('\0' == *flag) { continue; } switch (skpcProbeAddLogFlag(probe, flag)) { case 0: break; case -1: skAppPrintErr("Invalid %s: Unrecognized value '%s'", appOptions[OPT_LOG_FLAGS].name, flag); goto END; case -2: skAppPrintErr("Invalid %s: Cannot mix 'none' with other value", appOptions[OPT_LOG_FLAGS].name); goto END; default: skAppPrintErr("Bad return value from skpcProbeAddLogFlag()"); skAbort(); } } rv = 0; END: free(log_flags_copy); return rv; }
/* * printByNameOrNumber(sensor); * * Look up the sensor that has the name or the ID specified in the * string 'sensor' and print it to OUT_FH. Also print its * class(es) if requested. */ static void printByNameOrNumber( const char *sensor) { char sensor_name[SK_MAX_STRLEN_SENSOR+1]; int rv; uint32_t temp; sensorID_t sid; int count; sensor_iter_t si; /* try to parse as a number */ rv = skStringParseUint32(&temp, sensor, 0, SK_INVALID_SENSOR-1); if (rv < 0 && rv != SKUTILS_ERR_BAD_CHAR) { skAppPrintErr("Invalid Sensor Number '%s': %s", sensor, skStringParseStrerror(rv)); return; } if (rv == 0) { /* got a clean parse */ sid = (sensorID_t)temp; if ( !sksiteSensorExists(sid) ) { skAppPrintErr("Number '%s' is not a valid sensor number", sensor); return; } printSensor(sid, MAP_NUM_TO_NAME); return; } /* didn't get a clean parse. try to treat as a name */ sid = sksiteSensorLookup(sensor); if (sid != SK_INVALID_SENSOR) { printSensor(sid, MAP_NAME_TO_NUM); return; } /* try a case-insensitive search, manually iterating over all * the sensors */ count = 0; sksiteSensorIterator(&si); while (sksiteSensorIteratorNext(&si, &sid)) { sksiteSensorGetName(sensor_name, sizeof(sensor_name), sid); if (0 == strcasecmp(sensor_name, sensor)) { printSensor(sid, MAP_NAME_TO_NUM); ++count; } } if (count == 0) { skAppPrintErr("Name '%s' is not a valid sensor name", sensor); } }
/* * ok = createStringmaps(); * * Create the string-maps to assist in parsing the --keys and * --counters switches. */ static int createStringmaps( void) { sk_stringmap_t *map; sk_stringmap_status_t sm_err; sk_stringmap_entry_t sm_entry; sk_aggbag_type_iter_t iter; sk_aggbag_type_t type; const char *names[] = {"key", "counter"}; unsigned int i; memset(&sm_entry, 0, sizeof(sm_entry)); for (i = 0; i < 2; ++i) { /* create the string-map of field identifiers */ if (0 == i) { sm_err = skStringMapCreate(&key_name_map); map = key_name_map; } else { sm_err = skStringMapCreate(&counter_name_map); map = counter_name_map; } if (sm_err) { skAppPrintErr("Unable to create string map for %ss", names[i]); return -1; } skAggBagFieldTypeIteratorBind( &iter, ((0 == i) ? SK_AGGBAG_KEY : SK_AGGBAG_COUNTER)); while ((sm_entry.name = skAggBagFieldTypeIteratorNext(&iter, &type)) != NULL) { sm_entry.id = type; sm_err = skStringMapAddEntries(map, 1, &sm_entry); if (sm_err) { skAppPrintErr("Unable to add %s field named '%s': %s", names[i], sm_entry.name, skStringMapStrerror(sm_err)); return -1; } if (SKAGGBAG_FIELD_NHIPv6 == type || SKAGGBAG_FIELD_SUM_ELAPSED == type) { break; } } } 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); /* 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 */ }
/* * status = appOptionsHandler(cData, opt_index, opt_arg); * * Called by skOptionsParse(), this handles a user-specified switch * that the application has registered, typically by setting global * variables. Returns 1 if the switch processing failed or 0 if it * succeeded. Returning a non-zero from from the handler causes * skOptionsParse() to return a negative value. * * The clientData in 'cData' is typically ignored; 'opt_index' is * the index number that was specified as the last value for each * struct option in appOptions[]; 'opt_arg' is the user's argument * to the switch for options that have a REQUIRED_ARG or an * OPTIONAL_ARG. */ static int appOptionsHandler( clientData UNUSED(cData), int opt_index, char *opt_arg) { int rv; switch ((appOptionsEnum)opt_index) { #if 0 case OPT_HELP_FIELDS: helpFields(USAGE_FH); exit(EXIT_SUCCESS); #endif /* #if 0 */ case OPT_KEYS: if (keys_arg) { skAppPrintErr("Invalid %s: Switch used multiple times", appOptions[opt_index].name); return 1; } keys_arg = opt_arg; break; case OPT_COUNTERS: if (counters_arg) { skAppPrintErr("Invalid %s: Switch used multiple times", appOptions[opt_index].name); return 1; } counters_arg = opt_arg; break; case OPT_OUTPUT_PATH: if (output) { skAppPrintErr("Invalid %s: Switch used multiple times", appOptions[opt_index].name); return 1; } if ((rv = skStreamCreate(&output, SK_IO_WRITE, SK_CONTENT_SILK)) || (rv = skStreamBind(output, opt_arg))) { skStreamPrintLastErr(output, rv, &skAppPrintErr); skStreamDestroy(&output); return 1; } break; } return 0; /* 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 */ }
/* * status = optionsHandler(opt_arg, &index); * * Handles options for the plugin. 'opt_arg' is the argument, or * NULL if no argument was given. 'index' is the enum passed in * when the option was registered. * * Returns SKPLUGIN_OK on success, or SKPLUGIN_ERR if there was a * problem. */ static skplugin_err_t optionsHandler( const char *opt_arg, void *cbdata) { skplugin_callbacks_t regdata; unsigned int opt_index = *((unsigned int*)cbdata); uint32_t opt_val; int rv; rv = skStringParseUint32(&opt_val, opt_arg, ADDRTYPE_NONROUTABLE, ADDRTYPE_NONINTERNAL); if (rv != 0) { skAppPrintErr("Invalid %s '%s': %s", plugin_options[opt_index].name, opt_arg, skStringParseStrerror(rv)); return SKPLUGIN_ERR; } switch (opt_index) { case ADDRTYPE_STYPE: if (stype != ADDRTYPE_UNSET) { skAppPrintErr("Invalid %s: Switch used multiple times", plugin_options[opt_index].name); return SKPLUGIN_ERR; } stype = (addrtype_t)opt_val; break; case ADDRTYPE_DTYPE: if (dtype != ADDRTYPE_UNSET) { skAppPrintErr("Invalid %s: Switch used multiple times", plugin_options[opt_index].name); return SKPLUGIN_ERR; } dtype = (addrtype_t)opt_val; break; default: return SKPLUGIN_ERR_FATAL; } memset(®data, 0, sizeof(regdata)); regdata.init = addrtypeInit; regdata.cleanup = addrtypeCleanup; regdata.filter = addrtypeFilter; return skpinRegFilter(NULL, ®data, cbdata); }
/* * status = appOptionsHandler(cData, opt_index, opt_arg); * * This function is passed to skOptionsRegister(); it will be called * by skOptionsParse() for each user-specified switch that the * application has registered; it should handle the switch as * required---typically by setting global variables---and return 1 * if the switch processing failed or 0 if it succeeded. Returning * a non-zero from from the handler causes skOptionsParse() to return * a negative value. * * The clientData in 'cData' is typically ignored; 'opt_index' is * the index number that was specified as the last value for each * struct option in appOptions[]; 'opt_arg' is the user's argument * to the switch for options that have a REQUIRED_ARG or an * OPTIONAL_ARG. */ static int appOptionsHandler( clientData UNUSED(cData), int opt_index, char *opt_arg) { int rv; switch ((appOptionsEnum)opt_index) { #error "Add options handling here. Return 1 on failure." case OPT_FIRST: /* remove when real options added */ /* do something with the argument to the switch; for example, * parse as an integer */ rv = skStringParseUint32(&value, opt_arg, 0, 0); if (rv) { goto PARSE_ERROR; } break; case OPT_SECOND: /* remove when real options added */ /* set a flag based on this option */ flag = 1; break; } return 0; /* OK */ PARSE_ERROR: skAppPrintErr("Invalid %s '%s': %s", appOptions[opt_index].name, opt_arg, skStringParseStrerror(rv)); return 1; }
int main(int argc, char **argv) { appSetup(argc, argv); /* never returns on error */ if (sklogOpen()) { skAppPrintErr("Unable to open log"); } if (!test_threaded) { write_msg_thread((void*)"main"); } else { pthread_t p1; pthread_t p2; pthread_create(&p1, NULL, &write_msg_thread, (void*)"p1"); pthread_create(&p2, NULL, &write_msg_thread, (void*)"p2"); logToAllLevels((void*)"main"); pthread_join(p2, NULL); pthread_join(p1, NULL); } sklogClose(); return 0; }
/* * status = appOptionsHandler(cData, opt_index, opt_arg); * * This function is passed to skOptionsRegister(); it will be called * by skOptionsParse() for each user-specified switch that the * application has registered; it should handle the switch as * required---typically by setting global variables---and return 1 * if the switch processing failed or 0 if it succeeded. Returning * a non-zero from from the handler causes skOptionsParse() to return * a negative value. * * The clientData in 'cData' is typically ignored; 'opt_index' is * the index number that was specified as the last value for each * struct option in appOptions[]; 'opt_arg' is the user's argument * to the switch for options that have a REQUIRED_ARG or an * OPTIONAL_ARG. */ static int appOptionsHandler( clientData UNUSED(cData), int opt_index, char *opt_arg) { int rv; switch ((appOptionsEnum)opt_index) { case OPT_MAP_FILE: prefixmap_test_opt.map_file = opt_arg; break; case OPT_ADDRESS: rv = skStringParseIP(&prefixmap_test_opt.address, opt_arg); if (rv) { skAppPrintErr("Invalid %s '%s': %s", appOptions[opt_index].name, opt_arg, skStringParseStrerror(rv)); exit(EXIT_FAILURE); } prefixmap_test_opt.have_address = 1; break; case OPT_STRING: prefixmap_test_opt.string = 1; break; } return 0; /* OK */ }
static int processOneAddress( const char *addr) { char final_delim[] = {'\0', '\0'}; char cc[16]; char ipbuf[SK_NUM2DOT_STRLEN]; skipaddr_t ip; int rv; if (!app_opt.no_final_delimiter) { final_delim[0] = app_opt.column_separator; } rv = skStringParseIP(&ip, addr); if (rv) { skAppPrintErr("Invalid %s '%s': %s", appOptions[OPT_ADDRESS].name, addr, skStringParseStrerror(rv)); return 1; } #if SK_ENABLE_IPV6 if (skipaddrIsV6(&ip)) { skAppPrintErr("Invalid %s '%s': IPv6 addresses not supported", appOptions[OPT_ADDRESS].name, addr); return 1; } #endif /* SK_ENABLE_IPV6 */ skCountryLookupName(&ip, cc, sizeof(cc)); if (!app_opt.print_ips) { skStreamPrint(out, "%s\n", cc); } else { skipaddrString(ipbuf, &ip, ip_flags); if (app_opt.no_columns) { skStreamPrint(out, "%s%c%s%s\n", ipbuf, app_opt.column_separator, cc, final_delim); } else { skStreamPrint(out, "%15s%c%2s%s\n", ipbuf, app_opt.column_separator, cc, final_delim); } } return 0; }
int main(int argc, char **argv) { skstream_t *stream = NULL; ssize_t rv; /* Global setup */ appSetup(argc, argv); /* process input */ while ((rv = skOptionsCtxNextSilkFile(optctx, &stream, &skAppPrintErr)) == 0) { skStreamSetIPv6Policy(stream, ipv6_policy); if (0 != processFile(stream)) { skAppPrintErr("Error processing input from %s", skStreamGetPathname(stream)); skStreamDestroy(&stream); return EXIT_FAILURE; } skStreamDestroy(&stream); } if (rv < 0) { exit(EXIT_FAILURE); } rv = skAggBagWrite(ab, output); if (rv) { if (SKAGGBAG_E_WRITE == rv) { skStreamPrintLastErr(output, skStreamGetLastReturnValue(output), &skAppPrintErr); } else { skAppPrintErr("Error writing Aggregate Bag to '%s': %s", skStreamGetPathname(output), skAggBagStrerror(rv)); } exit(EXIT_FAILURE); } skAggBagDestroy(&ab); /* Done, do cleanup */ appTeardown(); return 0; }
/* * Verify contents of silk.conf file matches the values we set here * and set any globals we require. * * Invoked from rwflowpack by packlogic->setup_fn */ static int packLogicSetup( void) { const size_t count = (sizeof(filetypeFormats)/sizeof(filetypeFormats[0])); uint32_t i; #define FT_ASSERT(flowtype_id, flowtype_name) \ sksiteFlowtypeAssert(plugin_path, (flowtype_id), "all", (flowtype_name)) /* Make sure flowtype definitions match config file */ FT_ASSERT(RW_IN, "in"); FT_ASSERT(RW_OUT, "out"); FT_ASSERT(RW_IN_WEB, "inweb"); FT_ASSERT(RW_OUT_WEB, "outweb"); FT_ASSERT(RW_IN_NULL, "innull"); FT_ASSERT(RW_OUT_NULL, "outnull"); /* Confirm that number of flowtypes is not greater than the size * of the filetypeFormats[] array; abort if it is. Complain if * the array is too large, but continue processing. */ if (count <= sksiteFlowtypeGetMaxID()) { skAppPrintErr(("File formats not specified for some flowtypes.\n" "\tModify filetypeFormats[] in %s,\n" "\trecompile and try running again."), plugin_path); skAbort(); } else if (count != (1u + sksiteFlowtypeGetMaxID())) { skAppPrintErr(("Warning: Number of flowtypes does not equal number\n" "\tof file formats in filetypeFormats[] in %s"), plugin_path); } /* Define all of our networks */ for (i = 0; i < NUM_NETWORKS; ++i) { if (skpcNetworkAdd(i, net_names[i])) { skAppPrintErr("Unable to add network %" PRIu32 "->%s", i, net_names[i]); return -1; } } return 0; }
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; }
int ValidateSet(char *cidr, BGPDATA BGP) { skIPWildcard_t ipwild; int rv = skStringParseIPWildcard(&ipwild,cidr); if (rv != 0) { skAppPrintErr("Invalid IP Wildcard: %s", skStringParseStrerror(rv)); return 0; } rv = skIPSetCheckIPWildcard(BGP.ipset, &ipwild); return rv; }
/* * status = readerSetup(&out_daemon_mode, probe_vector, options); * * Invoked by input_mode_type->setup_fn(); */ static int readerSetup( fp_daemon_mode_t *is_daemon, const sk_vector_t *probe_vec, reader_options_t *options) { size_t count = skVectorGetCount(probe_vec); const char *netflow_file = options->pdu_file.netflow_file; skpc_probe_t *p; /* this function should only be called if we actually have probes * to process */ if (count == 0) { skAppPrintErr("readerSetup() called with zero length probe vector"); return 1; } if (count > 1) { skAppPrintErr("The " INPUT_MODE_TYPE_NAME "only supports one file-based probe."); return -1; } if (NULL != netflow_file) { /* Modify the probe to have the file name given on the command * line. */ if (0 == skVectorGetValue(&p, probe_vec, 0)) { if (skpcProbeSetFileSource(p, netflow_file)) { skAppPrintErr("Cannot change file source of probe"); return 1; } } } /* Not a deamon */ *is_daemon = FP_DAEMON_OFF; return 0; }
int main(int argc, char **argv) { skstream_t *inputFile; skPrefixMap_t *prefixMap; skPrefixMapErr_t map_error = SKPREFIXMAP_OK; char buf[DICTIONARY_ENTRY_BUFLEN]; int rv; appSetup(argc, argv); /* never returns on error */ /* Okay. Now we should open the prefixmap file, read it in, and */ /* then look up our address! */ if ((rv = skStreamCreate(&inputFile, SK_IO_READ, SK_CONTENT_SILK)) || (rv = skStreamBind(inputFile, prefixmap_test_opt.map_file)) || (rv = skStreamOpen(inputFile))) { skStreamPrintLastErr(inputFile, rv, &skAppPrintErr); skStreamDestroy(&inputFile); exit(EXIT_FAILURE); } map_error = skPrefixMapRead(&prefixMap, inputFile); skStreamDestroy(&inputFile); if ( SKPREFIXMAP_OK != map_error ) { skAppPrintErr("Failed to read map file: %s", skPrefixMapStrerror(map_error)); exit(EXIT_FAILURE); } if ( prefixmap_test_opt.string ) { int v = skPrefixMapFindString(prefixMap, &prefixmap_test_opt.address, buf, sizeof(buf)); if ( v < 0 ) { strncpy(buf, "(null)", sizeof(buf)); } printf("%s\n", buf); } else { printf("%d\n", skPrefixMapFindValue(prefixMap, &prefixmap_test_opt.address)); } skPrefixMapDelete(prefixMap); /* done */ appTeardown(); return 0; }
/* * skplugin_err_t pmap_column_width_handler(const char *opt_arg, * void *cbdata); * * Handles the pmap column width command line option. */ static skplugin_err_t pmap_column_width_handler( const char *opt_arg, void UNUSED(*cbdata)) { uint32_t tmp32; int rv; if (max_column_width > 0) { skAppPrintErr("Invalid %s: Switch used multiple times", pmap_column_width_option); return SKPLUGIN_ERR; } rv = skStringParseUint32(&tmp32, opt_arg, 1, INT32_MAX); if (rv) { skAppPrintErr("Invalid %s '%s': %s", pmap_column_width_option, opt_arg, skStringParseStrerror(rv)); return SKPLUGIN_ERR; } max_column_width = tmp32; return SKPLUGIN_OK; }
/* * legacyOptionsSetup(void); * * Register the legacy options. */ int legacyOptionsSetup( clientData cData) { assert((sizeof(legacyHelp)/sizeof(char *)) == (sizeof(legacyOptions)/sizeof(struct option))); /* register the options */ if (skOptionsRegister(legacyOptions, &legacyOptionsHandler, cData)) { skAppPrintErr("Unable to register legacy options"); return 1; } return 0; }
static int rwreceiverVerifyOptions( void) { int rv; /* Verify the transfer options */ rv = transferVerifyOptions(); /* Check for the existance of destination_dir */ if (destination_dir == NULL) { skAppPrintErr("A destination directory is required"); rv = -1; } return rv; }
/* * fp = getOutputHandle(); * * Return the file handle to use for output. */ FILE * getOutputHandle( void) { int rv; /* only invoke the pager when the user has not specified the * output-path, even if output-path is stdout */ if (NULL == output.of_name) { rv = skFileptrOpenPager(&output, pager); if (rv && rv != SK_FILEPTR_PAGER_IGNORED) { skAppPrintErr("Unable to invoke pager"); } } return output.of_fp; }
char * get_ipa_config( void) { skstream_t *conf_stream = NULL; char filename[PATH_MAX]; char line[IPA_CONFIG_LINE_LENGTH]; char *ipa_url = NULL; int rv; /* Read in the data file */ if (NULL == skFindFile(IPA_CONFIG_FILE, filename, sizeof(filename), 1)) { skAppPrintErr("Could not locate config file '%s'.", IPA_CONFIG_FILE); return NULL; } /* open input */ if ((rv = skStreamCreate(&conf_stream, SK_IO_READ, SK_CONTENT_TEXT)) || (rv = skStreamBind(conf_stream, filename)) || (rv = skStreamSetCommentStart(conf_stream, "#")) || (rv = skStreamOpen(conf_stream))) { skStreamPrintLastErr(conf_stream, rv, &skAppPrintErr); skStreamDestroy(&conf_stream); exit(EXIT_FAILURE); } while (skStreamGetLine(conf_stream, line, sizeof(line), NULL) == SKSTREAM_OK) { /* FIXME: smarter config file reading, please */ if (strlen(line) > 0) { ipa_url = strdup(line); break; } } skStreamDestroy(&conf_stream); /* Should be free()d by the caller */ return ipa_url; }
/* * status = appOptionsHandler(cData, opt_index, opt_arg); * * This function is passed to skOptionsRegister(); it will be called * by skOptionsParse() for each user-specified switch that the * application has registered; it should handle the switch as * required---typically by setting global variables---and return 1 * if the switch processing failed or 0 if it succeeded. Returning * a non-zero from from the handler causes skOptionsParse() to return * a negative value. * * The clientData in 'cData' is typically ignored; 'opt_index' is * the index number that was specified as the last value for each * struct option in appOptions[]; 'opt_arg' is the user's argument * to the switch for options that have a REQUIRED_ARG or an * OPTIONAL_ARG. */ static int appOptionsHandler( clientData UNUSED(cData), int opt_index, char *opt_arg) { switch ((appOptionsEnum)opt_index) { case OPT_IP_RANGES: ip_ranges = 1; if (opt_arg) { const char *char_name; switch (opt_arg[0]) { case '#': char_name = "comment start('#')"; break; case '\n': char_name = "newline"; break; case '\r': char_name = "carriage return"; break; case '\0': char_name = "end-of-string"; break; default: char_name = NULL; break; } if (char_name) { skAppPrintErr( "Invalid %s: Separator may not be the %s character", appOptions[opt_index].name, char_name); return 1; } delimiter = opt_arg[0]; } break; } return 0; }
int main(int argc, char **argv) { int rv; appSetup(argc, argv); /* read input file */ if (ip_ranges) { if (buildIPSetRanges(in_stream)) { return 1; } } else { if (buildIPSetWildcards(in_stream)) { return 1; } } skIPSetClean(ipset); /* write output to stream */ rv = skIPSetWrite(ipset, out_stream); if (rv) { if (SKIPSET_ERR_FILEIO == rv) { skStreamPrintLastErr(out_stream, skStreamGetLastReturnValue(out_stream), &skAppPrintErr); } else { skAppPrintErr("Unable to write IPset to '%s': %s", skStreamGetPathname(out_stream),skIPSetStrerror(rv)); } return 1; } skStreamDestroy(&in_stream); skStreamDestroy(&out_stream); /* done */ return 0; }
/* * status = appOptionsHandler(cData, opt_index, opt_arg); * * This function is passed to skOptionsRegister(); it will be called * by skOptionsParse() for each user-specified switch that the * application has registered; it should handle the switch as * required---typically by setting global variables---and return 1 * if the switch processing failed or 0 if it succeeded. Returning * a non-zero from from the handler causes skOptionsParse() to return * a negative value. * * The clientData in 'cData' is typically ignored; 'opt_index' is * the index number that was specified as the last value for each * struct option in appOptions[]; 'opt_arg' is the user's argument * to the switch for options that have a REQUIRED_ARG or an * OPTIONAL_ARG. */ static int appOptionsHandler( clientData UNUSED(cData), int opt_index, char *opt_arg) { size_t sz; int rv; switch ((appOptionsEnum)opt_index) { case OPT_SILK_OUTPUT: if (silk_output) { skAppPrintErr("Invalid %s: Switch used multiple times", appOptions[opt_index].name); return 1; } if ((rv =skStreamCreate(&silk_output,SK_IO_WRITE,SK_CONTENT_SILK_FLOW)) || (rv = skStreamBind(silk_output, opt_arg))) { skStreamPrintLastErr(silk_output, rv, &skAppPrintErr); exit(EXIT_FAILURE); } break; case OPT_PRINT_STATISTICS: print_statistics = 1; break; case OPT_LOG_DESTINATION: if ('\0' != log_destination[0]) { skAppPrintErr("Invalid %s: Switch used multiple times", appOptions[opt_index].name); } if ('\0' == opt_arg[0]) { skAppPrintErr("Invalid %s: Path name is required", appOptions[opt_index].name); return 1; } if (0 == strcmp("stdout", opt_arg) || 0 == strcmp("stderr", opt_arg) || 0 == strcmp("none", opt_arg)) { strncpy(log_destination, opt_arg, sizeof(log_destination)); break; } if ('/' == opt_arg[0]) { if (strlen(opt_arg) >= sizeof(log_destination)) { skAppPrintErr("Invalid %s: Name is too long", appOptions[opt_index].name); return 1; } strncpy(log_destination, opt_arg, sizeof(log_destination)); break; } if (NULL == getcwd(log_destination, sizeof(log_destination))) { skAppPrintSyserror("Unable to get current directory"); return 1; } sz = strlen(log_destination); if (sz + strlen(opt_arg) + 1 >= sizeof(log_destination)) { skAppPrintErr("Invalid %s: Name is too long", appOptions[opt_index].name); return 1; } snprintf(log_destination + sz, sizeof(log_destination) - sz, "/%s", opt_arg); break; case OPT_LOG_FLAGS: if (log_flags) { skAppPrintErr("Invaild %s: Switch used multiple times", appOptions[opt_index].name); return 1; } log_flags = opt_arg; break; } return 0; /* OK */ }