Commit 496249e2 authored by robertdavidgraham's avatar robertdavidgraham
Browse files

pcap

parent d82b0724
Loading
Loading
Loading
Loading
+57 −68
Original line number Diff line number Diff line
@@ -377,6 +377,61 @@ parseInt(const char *str)
    return result;
}

static uint64_t
parseTime(const char *value)
{
    uint64_t num = 0;
    unsigned is_negative = 0;

    while (*value == '-') {
        is_negative = 1;
        value++;
    }

    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 0;

    switch (tolower(value[0])) {
    case 's':
        num *= 1;
        break;
    case 'm':
        num *= 60;
        break;
    case 'h':
        num *= 60*60;
        break;
    case 'd':
        num *= 24*60*60;
        break;
    case 'w':
        num *= 24*60*60*7;
        break;
    default:
        fprintf(stderr, "--rotate-offset: unknown character\n");
        exit(1);
    }
    if (num >= 24*60*60) {
        fprintf(stderr, "--rotate-offset: value is greater than 1 day\n");
        exit(1);
    }
    if (is_negative)
        num = 24*60*60 - num;

    return num;
}



int EQUALS(const char *lhs, const char *rhs)
{
@@ -671,75 +726,9 @@ masscan_set_parameter(struct Masscan *masscan, const char *name, const char *val
            masscan->retries = x;
        }
    } else if (EQUALS("rotate-output", name) || EQUALS("rotate", name) || EQUALS("ouput-rotate", name)) {
        switch (tolower(value[0])) {
        case 's':
            masscan->rotate_output = 1;
            return;
        case 'm':
            masscan->rotate_output = 60;
            return;
        case 'h':
            masscan->rotate_output = 60*60;
            return;
        case 'd':
            masscan->rotate_output = 24*60*60;
            return;
        case 'w':
            masscan->rotate_output = 24*60*60*7;
            return;
        default:
            if (isdigit(value[0]&0xFF)) {
                masscan->rotate_output = strtoul(value, 0, 0);
                return;
            }
        }
        fprintf(stderr, "--rotate: unknown value (epected 'minute', 'hour', 'day', 'week')\n");
        exit(1);
        masscan->rotate_output = (unsigned)parseTime(value);
    } else if (EQUALS("rotate-offset", name) || EQUALS("ouput-rotate-offset", name)) {
        uint64_t num = 0;
        unsigned is_negative = 0;
        while (*value == '-') {
            is_negative = 1;
            value++;
        }
        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;

        switch (tolower(value[0])) {
        case 's':
            num *= 1;
            break;
        case 'm':
            num *= 60;
            break;
        case 'h':
            num *= 60*60;
            break;
        case 'd':
            num *= 24*60*60;
            break;
        case 'w':
            num *= 24*60*60*7;
            break;
        default:
            fprintf(stderr, "--rotate-offset: unknown character\n");
            exit(1);
        }
        if (num >= 24*60*60) {
            fprintf(stderr, "--rotate-offset: value is greater than 1 day\n");
            exit(1);
        }
        if (is_negative)
            num = 24*60*60 - num;

        masscan->rotate_offset = (unsigned)num;
        masscan->rotate_offset = (unsigned)parseTime(value);
    } else if (EQUALS("rotate-dir", name) || EQUALS("rotate-directory", name) || EQUALS("ouput-rotate-dir", name)) {
        char *p;
        strcpy_s(   masscan->rotate_directory,
+42 −31
Original line number Diff line number Diff line
@@ -39,6 +39,30 @@ unsigned control_c_pressed = 0;
time_t global_now;


/***************************************************************************
 ***************************************************************************/
void
flush_packets(struct Masscan *masscan)
{
    for (;;) {
        unsigned char *p;
        int err;

        err = rte_ring_sc_dequeue(masscan->pending_packets, (void**)&p);
        if (err)
            break;
        rawsock_send_packet(masscan->adapter, p + sizeof(size_t), (unsigned)*(size_t*)p, 0);
        err = rte_ring_sp_enqueue(masscan->packet_buffers, p);
        again2:
        if (err) {
            LOG(0, "transmit queue full (should be impossible)\n");
            pixie_usleep(10000);
            goto again2;
        }
    }
}


/***************************************************************************
 * This thread spews packets as fast as it can
 *
@@ -62,7 +86,6 @@ transmit_thread(void *v) /*aka. scanning_thread() */
    unsigned packet_trace = masscan->nmap.packet_trace;
    double timestamp_start;
    unsigned *picker;
    struct rte_ring *pending_packets = masscan->pending_packets;
    struct Adapter *adapter = masscan->adapter;

    LOG(1, "xmit: starting transmit thread...\n");
@@ -143,15 +166,7 @@ transmit_thread(void *v) /*aka. scanning_thread() */
        } /* end of batch */

        /* Transmit packets from other thread */
        for (;;) {
            unsigned char *p;
            int err;

            err = rte_ring_sc_dequeue(pending_packets, (void**)&p);
            if (err)
                break;
            rawsock_send_packet(adapter, p + sizeof(size_t), (unsigned)*(size_t*)p, 0);
        }
        flush_packets(masscan);

        /* If the user pressed <ctrl-c>, then we need to exit. but, in case
         * the user wants to resume the scan later, we save the current
@@ -178,16 +193,11 @@ transmit_thread(void *v) /*aka. scanning_thread() */
        for (j=0; j<masscan->wait && !control_c_pressed; j++) {
            unsigned k;
            status_print(&status, i++, m);

            for (k=0; k<1000; k++) {
                for (;;) {
                    unsigned char *p;
                    int err;
                /* Transmit packets from other thread */
                flush_packets(masscan);

                    err = rte_ring_sc_dequeue(pending_packets, (void**)&p);
                    if (err)
                        break;
                    rawsock_send_packet(adapter, p + sizeof(size_t), (unsigned)*(size_t*)p, 0);
                }
                pixie_usleep(1000);
            }
        }
@@ -281,17 +291,6 @@ receive_thread(struct Masscan *masscan,
        if (adapter_ip != dst)
            continue;

        /* Save raw packet (if configured to do so) */
        if (pcapfile) {
            pcapfile_writeframe(
                pcapfile,
                px,
                length,
                length,
                secs,
                usecs);
        }


        /* OOPS: handle arp instead */
        if (parsed.found == FOUND_ARP) {
@@ -312,6 +311,17 @@ receive_thread(struct Masscan *masscan,
        if (adapter_port != parsed.port_dst)
            continue;

        /* Save raw packet (if configured to do so) */
        if (pcapfile) {
            pcapfile_writeframe(
                pcapfile,
                px,
                length,
                length,
                secs,
                usecs);
        }

        /* verify: syn-cookies */
        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));
@@ -492,10 +502,11 @@ main_scan(struct Masscan *masscan)
    masscan->pending_packets = rte_ring_create(256, RING_F_SP_ENQ|RING_F_SC_DEQ);
    {
        unsigned i;
        for (i=0; i<256; i++) {
        for (i=0; i<255 /*TODO: why not 256???*/; i++) {
            char *pkt = (char*)malloc(1600);
            err = rte_ring_sp_enqueue(masscan->packet_buffers, pkt);
            if (err) {
                /* I dunno why but I can't queue all 256 packets, just 255 */
                LOG(0, "packet_buffers: enqueue: error %d\n", err);
            }
        }
+3 −3
Original line number Diff line number Diff line
@@ -216,19 +216,19 @@ int arp_response(
    } *response;
    struct ARP_IncomingRequest request;
    int err;
    size_t offset;


    /* Get a buffer for sending the response packet. This thread doesn't
     * send the packet itself. Instead, it formats a packet, then hands
     * that packet off to a transmit thread for later transmission. */
again:
    err = rte_ring_sc_dequeue(packet_buffers, &response);
    err = rte_ring_sc_dequeue(packet_buffers, (void**)&response);
    if (err != 0) {
        pixie_usleep(100);
        goto again;
    }
    memset(response->px, 0, 64);
    offset = sizeof(size_t);


    memset(&request, 0, sizeof(request));

+0 −1
Original line number Diff line number Diff line
@@ -88,7 +88,6 @@ murmur(uint64_t entropy, ...)

    for (len=0; len<2; len++) {
        unsigned k = va_arg(key, unsigned);

        k = k * c1;
        k = (k << r1) | (k >> (32-r1));
        k = k * c2;
+4 −4
Original line number Diff line number Diff line
@@ -57,9 +57,6 @@
    <ClCompile Include="..\src\rawsock-getip.c">
      <Filter>Source Files</Filter>
    </ClCompile>
    <ClCompile Include="..\src\rawsock-arp.c">
      <Filter>Source Files</Filter>
    </ClCompile>
    <ClCompile Include="..\src\rawsock-getif.c">
      <Filter>Source Files</Filter>
    </ClCompile>
@@ -93,10 +90,13 @@
    <ClCompile Include="..\src\rte-ring.c">
      <Filter>Source Files</Filter>
    </ClCompile>
    <ClCompile Include="..\src\rawsock-pcapfile.c">
      <Filter>Source Files</Filter>
    </ClCompile>
    <ClCompile Include="..\src\proto-tcp.c">
      <Filter>Source Files</Filter>
    </ClCompile>
    <ClCompile Include="..\src\rawsock-pcapfile.c">
    <ClCompile Include="..\src\rawsock-arp.c">
      <Filter>Source Files</Filter>
    </ClCompile>
  </ItemGroup>
+4 −4

File changed.

Contains only whitespace changes.

+9 −9

File changed.

Contains only whitespace changes.

Loading