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; }
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; }
/* * 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); 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 */ }
/* * status = processInputFile(filein); * * For every line in 'filein', look up the address in Country Code * map and print out the corresponding country code. There should * be as many lines of output as there are of input. */ static int processInputFile( const char *f_name) { char final_delim[] = {'\0', '\0'}; char line[2048]; skstream_t *stream = NULL; skIPWildcardIterator_t iter; skIPWildcard_t ipwild; skipaddr_t ip; int retval = 1; int rv; int lc = 0; char cc[32]; char ipbuf[SK_NUM2DOT_STRLEN]; if (!app_opt.no_final_delimiter) { final_delim[0] = app_opt.column_separator; } /* open input */ if ((rv = skStreamCreate(&stream, SK_IO_READ, SK_CONTENT_TEXT)) || (rv = skStreamBind(stream, f_name)) || (rv = skStreamSetCommentStart(stream, "#")) || (rv = skStreamOpen(stream))) { skStreamPrintLastErr(stream, rv, &skAppPrintErr); goto END; } /* read until end of file */ while ((rv = skStreamGetLine(stream, line, sizeof(line), &lc)) != SKSTREAM_ERR_EOF) { switch (rv) { case SKSTREAM_OK: /* good, we got our line */ break; case SKSTREAM_ERR_LONG_LINE: /* bad: line was longer than sizeof(line) */ skAppPrintErr("Input line %d too long. ignored", lc); continue; default: /* unexpected error */ skStreamPrintLastErr(stream, rv, &skAppPrintErr); goto END; } /* parse the line: fill in octet_bitmap */ rv = skStringParseIPWildcard(&ipwild, line); if (rv && rv != SKUTILS_ERR_EMPTY) { /* error */ skAppPrintErr("Error on line %d: %s\n", lc, skStringParseStrerror(rv)); goto END; } #if SK_ENABLE_IPV6 if (skIPWildcardIsV6(&ipwild)) { continue; } #endif /* SK_ENABLE_IPV6 */ skIPWildcardIteratorBind(&iter, &ipwild); while (skIPWildcardIteratorNext(&iter, &ip) == SK_ITERATOR_OK) { 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); } } } } retval = 0; END: skStreamDestroy(&stream); return retval; }
/* * 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 */ }
/* * skplugin_err_t pmapfile_handler(const char *opt_arg, void *cbdata) * * Handler for --pmap-file option. Actually registers the filter and * fields. */ static skplugin_err_t pmapfile_handler( const char *opt_arg, void UNUSED(*cbdata)) { /* Whether we have seen any unnamed pmaps */ static int have_unnamed_pmap = 0; skPrefixMapErr_t map_error = SKPREFIXMAP_OK; skstream_t *stream = NULL; skPrefixMap_t *prefix_map = NULL; pmap_data_t *pmap_data = NULL; int ok; const char *filename; const char *sep; const char *mapname; char *prefixed_name = NULL; char *short_prefixed_name; size_t namelen = 0; size_t i; int rv = SKPLUGIN_ERR; skplugin_callbacks_t regdata; /* We can only have one pmap whenever we any any pmap without a * mapname. If we've seen one and enter this function a second * time, it is an error */ if (have_unnamed_pmap) { skAppPrintErr(("Invalid %s: You may use only one prefix map" " when you are\n" "\tusing a prefix map without specifying a mapname"), pmap_file_option); return SKPLUGIN_ERR; } /* Parse the argument into a field name and file name */ sep = strchr(opt_arg, ':'); if (NULL == sep) { /* We do not have a mapname. We'll check for one in the pmap * once we read it. */ mapname = NULL; filename = opt_arg; } else { /* A mapname was supplied on the command line */ if (sep == opt_arg) { skAppPrintErr("Invalid %s: Zero length mapnames are not allowed", pmap_file_option); return SKPLUGIN_ERR; } if (memchr(opt_arg, ',', sep - opt_arg) != NULL) { skAppPrintErr("Invalid %s: The mapname may not include a comma", pmap_file_option); return SKPLUGIN_ERR; } mapname = opt_arg; filename = sep + 1; namelen = sep - opt_arg; } ok = skpinOpenDataInputStream(&stream, SK_CONTENT_SILK, filename); if (ok == -1) { /* problem opening file */ skAppPrintErr("Failed to open the prefix map file '%s'", filename); return SKPLUGIN_ERR; } if (ok == 1) { /* master needs to process the file, since it may contain the * map name we use for creating switches */ if ((rv = skStreamCreate(&stream, SK_IO_READ, SK_CONTENT_SILK)) || (rv = skStreamBind(stream, filename)) || (rv = skStreamOpen(stream))) { skStreamPrintLastErr(stream, rv, &skAppPrintErr); skStreamDestroy(&stream); return SKPLUGIN_ERR; } /* the master can ignore the file for filtering */ ignore_prefix_map = 1; } map_error = skPrefixMapRead(&prefix_map, stream); if (SKPREFIXMAP_OK != map_error) { if (SKPREFIXMAP_ERR_IO == map_error) { skStreamPrintLastErr(stream, skStreamGetLastReturnValue(stream), &skAppPrintErr); } else { skAppPrintErr("Failed to read the prefix map file '%s': %s", opt_arg, skPrefixMapStrerror(map_error)); } skStreamDestroy(&stream); return SKPLUGIN_ERR; } skStreamDestroy(&stream); if (NULL == mapname) { /* No mapname was supplied on the command line. Check for a * mapname insided the pmap. */ mapname = skPrefixMapGetMapName(prefix_map); if (mapname) { /* The pmap supplied a mapname */ namelen = strlen(mapname); } else { /* No mapname. Accept for legacy purposes, unless we have * read any other pmaps */ have_unnamed_pmap = 1; if (skVectorGetCount(pmap_vector) != 0) { skAppPrintErr(("Invalid %s: You may use only one prefix map" " when you are\n" "\t using a prefix map without" " specifying a mapname"), pmap_file_option); goto END; } } } /* Allocate the pmap_data structure */ pmap_data = (pmap_data_t *)calloc(1, sizeof(*pmap_data)); if (pmap_data == NULL) { ERR_NO_MEM(pmap_data); rv = SKPLUGIN_ERR_FATAL; goto END; } /* pmap_data now "owns" prefix_map */ pmap_data->pmap = prefix_map; prefix_map = NULL; /* Cache the content type */ pmap_data->type = skPrefixMapGetContentType(pmap_data->pmap); /* Fill the direction structure for each direction */ pmap_data->sdir.dir = DIR_SOURCE; pmap_data->ddir.dir = DIR_DEST; pmap_data->adir.dir = DIR_ANY; pmap_data->sdir.data = pmap_data; pmap_data->ddir.data = pmap_data; pmap_data->adir.data = pmap_data; /* Record the path to the pmap file */ pmap_data->filepath = strdup(filename); if (NULL == pmap_data->filepath) { ERR_NO_MEM(pmap_data->filepath); rv = SKPLUGIN_ERR_FATAL; goto END; } if (mapname == NULL) { /* Pmap without a mapname. */ /* Add the proper legacy option names to the pmap_data structure */ switch (pmap_data->type) { case SKPREFIXMAP_CONT_ADDR_V4: case SKPREFIXMAP_CONT_ADDR_V6: pmap_data->sdir.filter_option = strdup(pmap_saddress_option); pmap_data->ddir.filter_option = strdup(pmap_daddress_option); pmap_data->adir.filter_option = strdup(pmap_aaddress_option); break; case SKPREFIXMAP_CONT_PROTO_PORT: pmap_data->sdir.filter_option = strdup(pmap_sport_proto_option); pmap_data->ddir.filter_option = strdup(pmap_dport_proto_option); pmap_data->adir.filter_option = strdup(pmap_aport_proto_option); break; } if ((pmap_data->sdir.filter_option == NULL) || (pmap_data->ddir.filter_option == NULL) || (pmap_data->adir.filter_option == NULL)) { ERR_NO_MEM(filter_option); rv = SKPLUGIN_ERR_FATAL; goto END; } pmap_data->mapname = strdup(pmap_title_val); pmap_data->sdir.field_name = strdup(pmap_title_sval); pmap_data->ddir.field_name = strdup(pmap_title_dval); if ((pmap_data->mapname == NULL) || (pmap_data->sdir.field_name == NULL) || (pmap_data->ddir.field_name == NULL)) { ERR_NO_MEM(field_name); rv = SKPLUGIN_ERR_FATAL; goto END; } } else { /* if (mapname == NULL) */ /* Create the field names*/ pmap_data->mapname = (char*)malloc(namelen + 1); if (NULL == pmap_data->mapname) { ERR_NO_MEM(pmap_data->mapname); rv = SKPLUGIN_ERR_FATAL; goto END; } strncpy(pmap_data->mapname, mapname, namelen); pmap_data->mapname[namelen] = '\0'; /* Allocate space for the [pmap-]{src-,dst-}<mapname> string */ prefixed_name = (char*)malloc(namelen + pmap_prefix_len + dir_name_len + 1); if (NULL == prefixed_name) { ERR_NO_MEM(prefixed_name); rv = SKPLUGIN_ERR_FATAL; goto END; } /* Copy in the pmap- prefix */ strncpy(prefixed_name, pmap_prefix, pmap_prefix_len); /* short name (for fields) starts at the {src-,dst-} */ short_prefixed_name = prefixed_name + pmap_prefix_len; /* add in the actual field name, and zero terminate it */ strncpy(short_prefixed_name + dir_name_len, mapname, namelen); short_prefixed_name[namelen + dir_name_len] = '\0'; /* Create the destination-themed names */ strncpy(short_prefixed_name, src_dir_name, dir_name_len); pmap_data->sdir.filter_option = strdup(prefixed_name); pmap_data->sdir.field_name = strdup(short_prefixed_name); if ((pmap_data->sdir.filter_option == NULL) || (pmap_data->sdir.field_name == NULL)) { ERR_NO_MEM(pmap_data->sdir); rv = SKPLUGIN_ERR_FATAL; goto END; } strncpy(short_prefixed_name, dst_dir_name, dir_name_len); pmap_data->ddir.filter_option = strdup(prefixed_name); pmap_data->ddir.field_name = strdup(short_prefixed_name); if ((pmap_data->ddir.filter_option == NULL) || (pmap_data->ddir.field_name == NULL)) { ERR_NO_MEM(pmap_data->ddir); rv = SKPLUGIN_ERR_FATAL; goto END; } strncpy(short_prefixed_name, any_dir_name, dir_name_len); pmap_data->adir.filter_option = strdup(prefixed_name); if (pmap_data->adir.filter_option == NULL) { ERR_NO_MEM(pmap_data->adir); rv = SKPLUGIN_ERR_FATAL; goto END; } /* Free the temporary name buffer */ free(prefixed_name); prefixed_name = NULL; } /* if (mapname == NULL) */ /* Verify unique field names */ for (i = 0; i < skVectorGetCount(pmap_vector); i++) { pmap_data_t *p; skVectorGetValue(&p, pmap_vector, i); if ((strcmp(pmap_data->mapname, p->mapname) == 0) || (strcmp(pmap_data->sdir.field_name, p->sdir.field_name) == 0) || (strcmp(pmap_data->ddir.field_name, p->ddir.field_name) == 0)) { skAppPrintErr(("Invalid %s: Multiple pmaps use the mapname '%s':\n" "\t%s\n\t%s"), pmap_file_option, pmap_data->mapname, p->filepath, pmap_data->filepath); rv = SKPLUGIN_ERR; goto END; } } /* Register fields and filter options */ memset(®data, 0, sizeof(regdata)); regdata.init = pmap_field_init; regdata.column_width = 0; regdata.bin_bytes = 4; regdata.rec_to_text = pmap_text_fn; regdata.rec_to_bin = pmap_bin_fn; regdata.bin_to_text = pmap_bin_to_text_fn; for (i = 0; i < 2; ++i) { directed_pmap_data_t *dir = ((0 == i) ? &pmap_data->sdir : &pmap_data->ddir); skpinRegField(&dir->field, dir->field_name, NULL, ®data, dir); skpinRegOption2(dir->filter_option, REQUIRED_ARG, NULL, &pmap_filter_help, &pmap_handle_filter_option, dir, 1, SKPLUGIN_FN_FILTER); } /* Register the "any" filter separately */ skpinRegOption2(pmap_data->adir.filter_option, REQUIRED_ARG, NULL, &pmap_filter_help, &pmap_handle_filter_option, &pmap_data->adir, 1, SKPLUGIN_FN_FILTER); if (skVectorAppendValue(pmap_vector, &pmap_data)) { rv = SKPLUGIN_ERR_FATAL; goto END; } rv = SKPLUGIN_OK; END: /* Free the temporary name buffer */ if (prefixed_name) { free(prefixed_name); } if (rv != SKPLUGIN_OK) { if (prefix_map) { skPrefixMapDelete(prefix_map); } if (pmap_data) { pmap_data_destroy(pmap_data); } } return (skplugin_err_t)rv; }
static int compareFiles( char **file) { skstream_t *stream[2] = {NULL, NULL}; rwRec rec[2]; int i; int rv; int status = 2; uint64_t rec_count = 0; int eof = -1; memset(stream, 0, sizeof(stream)); memset(rec, 0, sizeof(rec)); for (i = 0; i < 2; ++i) { if ((rv = skStreamCreate(&stream[i], SK_IO_READ, SK_CONTENT_SILK_FLOW)) || (rv = skStreamBind(stream[i], file[i])) || (rv = skStreamOpen(stream[i])) || (rv = skStreamReadSilkHeader(stream[i], NULL))) { /* Give up if we can't read the beginning of the silk header */ if (rv != SKSTREAM_OK) { if (!quiet) { skStreamPrintLastErr(stream[i], rv, &skAppPrintErr); } goto END; } } } while ((rv = skStreamReadRecord(stream[0], &rec[0])) == SKSTREAM_OK) { rv = skStreamReadRecord(stream[1], &rec[1]); if (rv != SKSTREAM_OK) { if (rv == SKSTREAM_ERR_EOF) { /* file 0 longer than file 1 */ status = 1; eof = 1; } else { if (!quiet) { skStreamPrintLastErr(stream[1], rv, &skAppPrintErr); } status = -1; } goto END; } ++rec_count; if (0 != memcmp(&rec[0], &rec[1], sizeof(rwRec))) { status = 1; goto END; } } if (rv != SKSTREAM_ERR_EOF) { if (!quiet) { skStreamPrintLastErr(stream[0], rv, &skAppPrintErr); } } else { rv = skStreamReadRecord(stream[1], &rec[1]); switch (rv) { case SKSTREAM_OK: /* file 1 longer than file 0 */ status = 1; eof = 0; break; case SKSTREAM_ERR_EOF: /* files identical */ status = 0; break; default: if (!quiet) { skStreamPrintLastErr(stream[1], rv, &skAppPrintErr); } break; } } END: for (i = 0; i < 2; ++i) { skStreamDestroy(&stream[i]); } if (1 == status && !quiet) { if (eof != -1) { printf("%s %s differ: EOF %s\n", file[0], file[1], file[eof]); } else { printf(("%s %s differ: record %" PRIu64 "\n"), file[0], file[1], rec_count); #ifdef RWCOMPARE_VERBOSE printRecords(rec); #endif } } return status; }
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); }