Commit 2ab807c3 authored by robertdavidgraham's avatar robertdavidgraham
Browse files

syn-cookies

parent 9e1013d8
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ status_print(struct Status *status, uint64_t count, uint64_t max_count)

    /* Get the time. NOTE: this is CLOCK_MONOTONIC_RAW on Linux, not
     * wall-clock time. */
	now = port_gettime();
	now = pixie_gettime();
	elapsed = ((double)now - (double)status->last.clock)/(double)1000000.0;
    if (elapsed == 0)
        return;
+3 −4
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ throttler_start(struct Throttler *throttler, double max_rate)
    throttler->max_rate = max_rate;

    for (i=0; i<sizeof(throttler->buckets)/sizeof(throttler->buckets[0]); i++) {
        throttler->buckets[i].timestamp = port_gettime();
        throttler->buckets[i].timestamp = pixie_gettime();
        throttler->buckets[i].packet_count = 0;
    }

@@ -69,8 +69,7 @@ again:

    /* NOTE: this uses CLOCK_MONOTONIC_RAW on Linux, so the timstamp doesn't
     * move forward when the machine is suspended */
    timestamp = port_gettime();

    timestamp = pixie_gettime();

    /*
     * We record that last 256 buckets, and average the rate over all of
@@ -115,7 +114,7 @@ again:
        if (waittime > 0.1)
            waittime = 0.1;

        port_usleep((uint64_t)(waittime * 1000000.0));
        pixie_usleep((uint64_t)(waittime * 1000000.0));

        throttler->batch_size *= 0.999;
        goto again;
+21 −11
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include "main-status.h"        /* printf() regular status updates */
#include "main-throttle.h"      /* rate limit */
#include "main-dedup.h"         /* ignore duplicate responses */
#include "syn-cookie.h"         /* for SYN-cookies on send */

#include "pixie-timer.h"        /* portable time functions */
#include "pixie-threads.h"      /* portable threads */
@@ -61,7 +62,7 @@ scanning_thread(void *v)
    status_start(&status);
    throttler_start(&throttler, masscan->max_rate);

    timestamp_start = 1.0 * port_gettime() / 1000000.0;
    timestamp_start = 1.0 * pixie_gettime() / 1000000.0;


    /*
@@ -102,8 +103,14 @@ scanning_thread(void *v)
            /* Print packet if debugging */
            if (packet_trace)
                tcpkt_trace(pkt_template, ip, port, timestamp_start);

            /* Send the probe */
			rawsock_send_probe(masscan->adapter, ip, port, pkt_template);
			rawsock_send_probe(
                    masscan->adapter, 
                    ip, 
                    port, 
                    syn_hash(ip, port), 
                    pkt_template);


            i++;
@@ -136,7 +143,7 @@ scanning_thread(void *v)
        unsigned j;
        for (j=0; j<10 && !control_c_pressed; j++) {
            status_print(&status, i++, m);
            port_usleep(1000000);
            pixie_usleep(1000000);
        }
        fprintf(stderr, "                                                                      \r");
    }
@@ -461,6 +468,7 @@ main_scan(struct Masscan *masscan)
        struct PreprocessedInfo parsed;
        unsigned dst;
        unsigned src;
        unsigned seqno;

        err = rawsock_recv_packet(
                    masscan->adapter,
@@ -483,6 +491,9 @@ main_scan(struct Masscan *masscan)
            | parsed.ip_dst[2]<< 8 | parsed.ip_dst[3]<<0;
        src = parsed.ip_src[0]<<24 | parsed.ip_src[1]<<16
            | parsed.ip_src[2]<< 8 | parsed.ip_src[3]<<0;
        seqno = px[parsed.transport_offset+8]<<24 | px[parsed.transport_offset+9]<<16 
              | px[parsed.transport_offset+10]<<8 | px[parsed.transport_offset+11];
        seqno -= 1;

        /* verify: my IP address */
        if (adapter_ip != dst)
@@ -499,16 +510,14 @@ main_scan(struct Masscan *masscan)
        if (parsed.found != FOUND_TCP)
            continue;

        /* verify: my IP address */
        dst = parsed.ip_dst[0]<<24 | parsed.ip_dst[1]<<16
            | parsed.ip_dst[2]<< 8 | parsed.ip_dst[3]<<0;
        if (adapter_ip != dst)
            continue;

        /* verify: my port number */
        if (adapter_port != parsed.port_dst)
            continue;

        if (syn_hash(src, parsed.port_src) != seqno) {
            LOG(1, "bad packet: ackno=0x%08x expected=0x%08x\n", seqno, syn_hash(src, parsed.port_src));
        }

        /* verify: ignore duplicates */
        if (dedup_is_duplicate(dedup, src, parsed.port_src))
            continue;
@@ -592,7 +601,8 @@ int main(int argc, char *argv[])
     * for Windows and PF_RING. */
	rawsock_init();


    /* Set randomization seed for SYN-cookies */
    syn_set_entropy();

    /*
     * Apply excludes
@@ -668,7 +678,7 @@ int main(int argc, char *argv[])
            x += randlcg_selftest();
            x += tcpkt_selftest();
            x += ranges_selftest();
            x += port_time_selftest();
            x += pixie_time_selftest();

            if (x != 0) {
                /* one of the selftests failed, so return error */
+44 −12
Original line number Diff line number Diff line
@@ -89,7 +89,7 @@ clock_gettime(int X, struct timeval *tv)


uint64_t
port_gettime()
pixie_gettime()
{
    //struct timeval tv;
    //clock_gettime(0, &tv);
@@ -106,9 +106,19 @@ port_gettime()

    //return (uint64_t)tv.tv_sec * 1000000UL + tv.tv_usec;
}
uint64_t
pixie_nanotime()
{
    uint64_t time1 = 0, freq = 0;
    double seconds;
    QueryPerformanceCounter((LARGE_INTEGER *) &time1);
    QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
    seconds = (double)time1/(double)freq;
    return (uint64_t)(seconds * 1000000000.0);
}

void
port_usleep(uint64_t waitTime)
pixie_usleep(uint64_t waitTime)
{
    /*
    uint64_t time1 = 0, time2 = 0, freq = 0;
@@ -123,16 +133,16 @@ port_usleep(uint64_t waitTime)

    uint64_t start;

    start = port_gettime();
    start = pixie_gettime();

    while (port_gettime() - start < waitTime)
    while (pixie_gettime() - start < waitTime)
        ;
}
#elif defined(CLOCK_MONOTONIC)
#include <unistd.h>

void
port_usleep(uint64_t microseconds)
pixie_usleep(uint64_t microseconds)
{
    struct timespec ts;
    struct timespec remaining;
@@ -151,7 +161,7 @@ again:
    //usleep(microseconds);
}
uint64_t
port_gettime()
pixie_gettime()
{
    int x;
    struct timespec tv;
@@ -167,30 +177,52 @@ port_gettime()

    return tv.tv_sec * 1000000 + tv.tv_nsec/1000;
}
uint64_t
pixie_nanotime()
{
    int x;
    struct timespec tv;

#ifdef CLOCK_MONOTONIC_RAW
    x = clock_gettime(CLOCK_MONOTONIC_RAW, &tv);
#else
    x = clock_gettime(CLOCK_MONOTONIC, &tv);
#endif
    if (x != 0) {
        printf("clock_gettime() err %d\n", errno);
    }

    return tv.tv_sec * 1000000000 + tv.tv_nsec;
}
#elif defined(__MACH__) /* works for Apple */
#include <unistd.h>
#include <mach/mach_time.h>

void port_usleep(uint64_t microseconds)
void pixie_usleep(uint64_t microseconds)
{
    usleep(microseconds);
}
uint64_t
port_gettime()
pixie_gettime()
{
    return mach_absolute_time()/1000;
}
uint64_t
pixie_nanotime()
{
    return mach_absolute_time();
}
#endif

int port_time_selftest()
int pixie_time_selftest()
{
    static const uint64_t duration = 123456;
    uint64_t start, stop, elapsed;
    

    start = port_gettime();
    port_usleep(duration);
    stop = port_gettime();
    start = pixie_gettime();
    pixie_usleep(duration);
    stop = pixie_gettime();
    elapsed = stop - start;

    if (elapsed < 0.9*duration || 1.1*duration < elapsed) {
+10 −5
Original line number Diff line number Diff line
#ifndef PORT_TIMER_H
#define PORT_TIMER_H
#ifndef TIMER_H
#define PIXIE_TIMER_H
#include <stdint.h>

/**
 * The current time, in microseconds
 */
uint64_t port_gettime();
uint64_t pixie_gettime();

/**
 * The current time, in nanoseconds
 */
uint64_t pixie_nanotime();

/**
 * Wait the specified number of microseconds
 */
void port_usleep(uint64_t usec);
void pixie_usleep(uint64_t usec);

int port_time_selftest();
int pixie_time_selftest();



Loading