示例#1
0
/*
 *  appTeardown()
 *
 *    Teardown all modules, close all files, and tidy up all
 *    application state.
 *
 *    This function is idempotent.
 */
static void
appTeardown(
    void)
{
    static int teardownFlag = 0;

    if (teardownFlag) {
        return;
    }
    teardownFlag = 1;

    if (ipset) {
        skIPSetDestroy(&ipset);
    }
    skStreamDestroy(&in_stream);
    skStreamDestroy(&out_stream);

    skIPSetOptionsTeardown();
    skAppUnregister();
}
示例#2
0
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);
}