Commit 55f5bd68 authored by robertdavidgraham's avatar robertdavidgraham
Browse files

#68 --rotate-size option added

parent 996d99a5
Loading
Loading
Loading
Loading
+54 −1
Original line number Diff line number Diff line
@@ -257,6 +257,7 @@ masscan_echo(struct Masscan *masscan, FILE *fp)
    fprintf(fp, "rotate = %u\n", masscan->output.rotate.timeout);
    fprintf(fp, "rotate-dir = %s\n", masscan->output.rotate.directory);
    fprintf(fp, "rotate-offset = %u\n", masscan->output.rotate.offset);
    fprintf(fp, "rotate-filesize = %" PRIu64 "\n", masscan->output.rotate.filesize);
    fprintf(fp, "pcap = %s\n", masscan->pcap_filename);

    /*
@@ -593,6 +594,55 @@ parseTime(const char *value)
    return num;
}

/***************************************************************************
 * Parses a size integer, which can be suffixed with "tera", "giga", 
 * "mega", and "kilo". These numbers are in units of 1024 so suck it.
 ***************************************************************************/
static uint64_t
parseSize(const char *value)
{
    uint64_t num = 0;

    while (isdigit(value[0]&0xFF)) {
        num = num*10 + (value[0] - '0');
        value++;
    }
    while (ispunct(value[0]) || isspace(value[0]))
        value++;

    if (isalpha(value[0]) && num == 0)
        num = 1;

    if (value[0] == '\0')
        return num;

    switch (tolower(value[0])) {
    case 'k': /* kilobyte */
        num *= 1024ULL;
        break;
    case 'm': /* megabyte */
        num *= 1024ULL * 1024ULL;
        break;
    case 'g': /* gigabyte */
        num *= 1024ULL * 1024ULL * 1024ULL;
        break;
    case 't': /* terabyte, 'cause we roll that way */
        num *=  1024ULL * 1024ULL * 1024ULL * 1024ULL;
        break;
    case 'p': /* petabyte, 'cause we are awesome */
        num *=  1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL;
        break;
    case 'e': /* exabyte, now that's just silly */
        num *=  1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL;
        break;
    default:
        fprintf(stderr, "--rotate-size: unknown character\n");
        exit(1);
    }
    return num;
}


/***************************************************************************
 ***************************************************************************/
static int
@@ -1217,10 +1267,13 @@ masscan_set_parameter(struct Masscan *masscan,
        } else {
            masscan->retries = x;
        }
    } else if (EQUALS("rotate-output", name) || EQUALS("rotate", name) || EQUALS("ouput-rotate", name)) {
    } else if (EQUALS("rotate-output", name) || EQUALS("rotate", name) 
        || EQUALS("ouput-rotate", name) || EQUALS("rotate-time", name) ) {
        masscan->output.rotate.timeout = (unsigned)parseTime(value);
    } else if (EQUALS("rotate-offset", name) || EQUALS("ouput-rotate-offset", name)) {
        masscan->output.rotate.offset = (unsigned)parseTime(value);
    } else if (EQUALS("rotate-size", name) || EQUALS("rotate-filesize", name)) {
        masscan->output.rotate.filesize = parseSize(value);
    } else if (EQUALS("rotate-dir", name) || EQUALS("rotate-directory", name) || EQUALS("ouput-rotate-dir", name)) {
        char *p;
        strcpy_s(   masscan->output.rotate.directory,
+1 −1
Original line number Diff line number Diff line
@@ -278,7 +278,7 @@ struct Masscan
            /**
             * Instead of rotating by timeout, we can rotate by filesize 
             */
            size_t filesize;
            uint64_t filesize;
            
            /**
             * The directory to which we store rotated files
+5 −0
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ binary_out_open(struct Output *out, FILE *fp)
        perror("output");
        exit(1);
    }

    out->rotate.bytes_written += bytes_written;
}


@@ -43,6 +45,7 @@ binary_out_close(struct Output *out, FILE *fp)
        perror("output");
        exit(1);
    }
    out->rotate.bytes_written += bytes_written;
}

/****************************************************************************
@@ -100,6 +103,7 @@ binary_out_status(struct Output *out, FILE *fp, time_t timestamp,
        perror("output");
        exit(1);
    }
    out->rotate.bytes_written += bytes_written;
}


@@ -159,6 +163,7 @@ binary_out_banner(struct Output *out, FILE *fp, time_t timestamp,
        perror("output");
        exit(1);
    }
    out->rotate.bytes_written += bytes_written;
}


+22 −3
Original line number Diff line number Diff line
@@ -565,8 +565,12 @@ again:
     * Set the next rotate time, which is the current time plus the period
     * length
     */
    out->rotate.bytes_written = 0;

    if (out->rotate.period) {
        out->rotate.next = next_rotate_time(time(0),
                                        out->rotate.period, out->rotate.offset);
    }

    LOG(1, "rotated: %s\n", new_filename);
    free(new_filename);
@@ -592,6 +596,21 @@ again:
    return out->fp;
}

/***************************************************************************
 ***************************************************************************/
static int
is_rotate_time(const struct Output *out, time_t now)
{
    if (out->is_virgin_file)
        return 0;
    if (now >= out->rotate.next)
        return 1;
    if (out->rotate.filesize != 0 &&
        out->rotate.bytes_written >= out->rotate.filesize)
        return 1;
    return 0;
}


/***************************************************************************
 * Report simply "open" or "closed", with little additional information.
@@ -647,7 +666,7 @@ output_report_status(struct Output *out, time_t timestamp, int status,
     * file, rather than in a separate thread right at the time interval.
     * Thus, if results are coming in slowly, the rotation won't happen
     * on precise boundaries */
    if (now >= out->rotate.next && !out->is_virgin_file) {
    if (is_rotate_time(out, now)) {
        fp = output_do_rotate(out, 0);
        if (fp == NULL)
            return;
@@ -764,7 +783,7 @@ output_report_banner(struct Output *out, time_t now,
     * file, rather than in a separate thread right at the time interval.
     * Thus, if results are coming in slowly, the rotation won't happen
     * on precise boundaries */
    if (now >= out->rotate.next && !out->is_virgin_file) {
    if (is_rotate_time(out, now)) {
        fp = output_do_rotate(out, 0);
        if (fp == NULL)
            return;
+11 −1
Original line number Diff line number Diff line
@@ -47,7 +47,16 @@ struct Output
    const struct OutputType *funcs;
    unsigned format;

    /**
     * The timestamp when this scan started. This is preserved in output files
     * because that's what nmap does, and a lot of tools parse this.
     */
    time_t when_scan_started;

    /**
     * Whether we've started writing to a file yet. We are lazy writing the
     * the file header until we've actually go something to write
     */
    unsigned is_virgin_file:1;

    struct {
@@ -55,7 +64,8 @@ struct Output
        time_t last;
        unsigned period;
        unsigned offset;
        size_t filesize;
        uint64_t filesize;
        uint64_t bytes_written;
        char *directory;
    } rotate;