Commit 2f18587b authored by Robert David Graham's avatar Robert David Graham
Browse files

fixes

parent c5f5d337
Loading
Loading
Loading
Loading
+42 −57
Original line number Diff line number Diff line
@@ -10,24 +10,6 @@



/***************************************************************************
 ***************************************************************************/
struct TimeoutEntry {
    /** 
     * In units of 1/10000 of a second
     */
    uint64_t timestamp;

    struct TimeoutEntry *next;

    /**
     * A pointer to our custom data structure 
     */
    void *pointer;

    unsigned counter;
};

/***************************************************************************
 ***************************************************************************/
struct Timeouts {
@@ -57,59 +39,62 @@ timeouts_create(uint64_t timestamp)

/***************************************************************************
 ***************************************************************************/
unsigned *
timeouts_add(struct Timeouts *timeouts, void *p, uint64_t timestamp, unsigned counter)
void
timeouts_add(struct Timeouts *timeouts, struct TimeoutEntry *entry,
             size_t offset, uint64_t timestamp)
{
    struct TimeoutEntry *entry;
    unsigned index = timestamp & timeouts->mask;

    entry = timeouts->freed_list;
    if (entry)
        timeouts->freed_list = entry->next;
    else {
        entry = (struct TimeoutEntry *)malloc(sizeof(*entry));
    }
    unsigned index;

    /* Initialize the new entry */    
    entry->timestamp = timestamp;
    entry->pointer = p;
    entry->counter = counter;
    entry->offset = (unsigned)offset;

    
    /* Unlink from whereas the entry came from */
    timeout_unlink(entry);
    
    
    /* Link it into it's new location */
    index = timestamp & timeouts->mask;
    entry->next = timeouts->slots[index];
    timeouts->slots[index] = entry;
    return &entry->counter;
    entry->prev = &timeouts->slots[index];
}

/***************************************************************************
 ***************************************************************************/
struct TimeoutEvent
void *
timeouts_remove(struct Timeouts *timeouts, uint64_t timestamp)
{
    struct TimeoutEvent result;
    struct TimeoutEntry *entry = NULL;

    /* Search until we find one */
    while (timeouts->current_index <= timestamp) {
        struct TimeoutEntry **r_entry = &timeouts->slots[timeouts->current_index & timeouts->mask];

        while (*r_entry && (*r_entry)->timestamp > timestamp)
            r_entry = &(*r_entry)->next;

        if (*r_entry) {
            struct TimeoutEntry *entry = *r_entry;
            void *p = entry->pointer;
            unsigned counter = entry->counter;
            (*r_entry) = entry->next;
            entry->next = timeouts->freed_list;
            timeouts->freed_list = entry;

            result.p = p;
            result.counter = counter;
            return result;
        } else {
        
        /* Start at the current slot */
        entry = timeouts->slots[timeouts->current_index & timeouts->mask];

        /* enumerate throug the linked list until we find one */
        while (entry && entry->timestamp > timestamp)
            entry = entry->next;
        if (entry)
            break;
    
        /* found nothing at this slot, so move to next slot */
        timeouts->current_index++;
    }

    if (entry == NULL) {
        /* we've caught up to the current time, and there's nothing
         * left to timeout, so return NULL */
        return NULL;
    }
    
    result.p = 0;
    result.counter = 0;
    return result;
    /* unlink this entry from the timeout system */
    timeout_unlink(entry);
 
    /* return a pointer to the structure holding this entry */
    return ((char*)entry) - entry->offset;
}

+32 −5
Original line number Diff line number Diff line
#ifndef EVENT_TIMEOUT_H
#define EVENT_TIMEOUT_H
#include <stdint.h>
#include <stdio.h>
#include <stddef.h> /* offsetof*/

struct Timeouts;
struct TimeoutEntry;

struct TimeoutEvent {
    void *p;
    unsigned counter;
/***************************************************************************
 ***************************************************************************/
struct TimeoutEntry {
    /** 
     * In units of 1/10000 of a second
     */
    uint64_t timestamp;
    struct TimeoutEntry *next;
    struct TimeoutEntry **prev;
    unsigned offset;
};

static inline void
timeout_unlink(struct TimeoutEntry *entry)
{
    *(entry->prev) = entry->next;
    entry->next = 0;
    entry->prev = &entry->next;
}

static inline void
timeout_init(struct TimeoutEntry *entry)
{
    entry->next = 0;
    entry->prev = &entry->next;
}


struct Timeouts *timeouts_create(uint64_t timestamp);
unsigned *timeouts_add(struct Timeouts *timeouts, void *p, uint64_t timestamp, unsigned counter);
struct TimeoutEvent timeouts_remove(struct Timeouts *timeouts, uint64_t timestamp);

void timeouts_add(struct Timeouts *timeouts, struct TimeoutEntry *entry, 
                  size_t offset, uint64_t timestamp);

void *timeouts_remove(struct Timeouts *timeouts, uint64_t timestamp);

#define TICKS_FROM_SECS(secs) ((secs)*16384ULL)
#define TICKS_FROM_USECS(usecs) ((usecs)/16384ULL)
+16 −10
Original line number Diff line number Diff line
@@ -15,7 +15,8 @@
 * in the configuration file.
 ***************************************************************************/
int
masscan_initialize_adapter(struct Masscan *masscan,
masscan_initialize_adapter(
    struct Masscan *masscan,
    unsigned *r_adapter_ip,
    unsigned char *adapter_mac,
    unsigned char *router_mac)
@@ -104,8 +105,11 @@ masscan_initialize_adapter(struct Masscan *masscan,
     * Once we've figured out which adapter to use, we now need to
     * turn it on.
     */
    if (!masscan->is_offline) {
        masscan->adapter = rawsock_init_adapter(ifname, masscan->is_pfring, masscan->is_sendq);
    masscan->adapter = rawsock_init_adapter(    ifname, 
                                            masscan->is_pfring, 
                                            masscan->is_sendq,
                                            masscan->nmap.packet_trace,
                                            masscan->is_offline);
    if (masscan->adapter == 0) {
        fprintf(stderr, "adapter[%s].init: failed\n", ifname);
        return -1;
@@ -113,7 +117,7 @@ masscan_initialize_adapter(struct Masscan *masscan,
    LOG(3, "rawsock: ignoring transmits\n");
    rawsock_ignore_transmits(masscan->adapter, adapter_mac);
    LOG(3, "rawsock: initialization done\n");
    }


    /*
     * ROUTER MAC ADDRESS
@@ -168,6 +172,8 @@ masscan_initialize_adapter(struct Masscan *masscan,
        return -1;
    }



    LOG(1, "adapter initialization done.\n");
    return 0;
}

src/main-ptrace.c

0 → 100644
+118 −0
Original line number Diff line number Diff line
#include "main-ptrace.h"
#include "proto-preprocess.h"
#include "pixie-timer.h"
#include "string_s.h"


double global_timestamp_start;

/***************************************************************************
 * Print packet info, when using nmap-style --packet-trace option
 ***************************************************************************/
void
packet_trace(FILE *fp, const unsigned char *px, size_t length, unsigned is_sent)
{
    unsigned x;
    struct PreprocessedInfo parsed;
    unsigned src_ip;
    unsigned dst_ip;
    char from[32];
    char to[32];
    char sz_type[32];
    unsigned type;
    double timestamp = 1.0 * pixie_gettime() / 1000000.0;
    unsigned offset;
    const char *direction;
    
    if (is_sent)
        direction = "SENT";
    else
        direction = "RCVD";

    /* parse the packet */
    x = preprocess_frame(px, length, 1, &parsed);
    if (!x)
        return;
    offset = parsed.found_offset;
    
    src_ip = px[parsed.ip_offset + 12] << 24
        | px[parsed.ip_offset + 13] << 16
        | px[parsed.ip_offset + 14] << 8
        | px[parsed.ip_offset + 15];
    dst_ip = px[parsed.ip_offset + 16] << 24
        | px[parsed.ip_offset + 17] << 16
        | px[parsed.ip_offset + 18] << 8
        | px[parsed.ip_offset + 19];

    /* format the IP addresses into fixed-width fields */
    sprintf_s(from, sizeof(from), "%u.%u.%u.%u:%u",
              (src_ip>>24)&0xFF, (src_ip>>16)&0xFF,
              (src_ip>>8)&0xFF, (src_ip>>0)&0xFF,
              parsed.port_src);
    
    sprintf_s(to, sizeof(to), "%u.%u.%u.%u:%u",
              (dst_ip>>24)&0xFF, (dst_ip>>16)&0xFF,
              (dst_ip>>8)&0xFF, (dst_ip>>0)&0xFF,
              parsed.port_dst);
    
    switch (parsed.found) {
        case FOUND_ARP:
            type = px[offset+6]<<8 | px[offset+7];
            switch (type) {
                case 0:strcpy_s(sz_type, sizeof(sz_type), "request"); break;
                case 1:strcpy_s(sz_type, sizeof(sz_type), "response"); break;
                default: sprintf_s(sz_type, sizeof(sz_type), "unknown(%u)", type); break;
            }
            fprintf(stderr, "%s (%5.4f) ARP  %-21s > %-21s %s\n", direction,
                    timestamp - global_timestamp_start, from, to, sz_type);
            break;
        case FOUND_DNS:
        case FOUND_UDP:
            fprintf(stderr, "%s (%5.4f) UDP  %-21s > %-21s \n", direction,
                    timestamp - global_timestamp_start, from, to);
            break;
        case FOUND_ICMP:
            fprintf(stderr, "%s (%5.4f) ICMP %-21s > %-21s \n", direction,
                    timestamp - global_timestamp_start, from, to);
            break;
        case FOUND_TCP:
            type = px[offset+13];
            switch (type) {
                case 0x00: strcpy_s(sz_type, sizeof(sz_type), "NULL"); break;
                case 0x01: strcpy_s(sz_type, sizeof(sz_type), "FIN"); break;
                case 0x11: strcpy_s(sz_type, sizeof(sz_type), "FIN-ACK"); break;
                case 0x19: strcpy_s(sz_type, sizeof(sz_type), "FIN-ACK-PSH"); break;
                case 0x02: strcpy_s(sz_type, sizeof(sz_type), "SYN"); break;
                case 0x12: strcpy_s(sz_type, sizeof(sz_type), "SYN-ACK"); break;
                case 0x04: strcpy_s(sz_type, sizeof(sz_type), "RST"); break;
                case 0x14: strcpy_s(sz_type, sizeof(sz_type), "RST-ACK"); break;
                case 0x15: strcpy_s(sz_type, sizeof(sz_type), "RST-FIN-ACK"); break;
                case 0x10: strcpy_s(sz_type, sizeof(sz_type), "ACK"); break;
                case 0x18: strcpy_s(sz_type, sizeof(sz_type), "ACK-PSH"); break;
                default:
                    sprintf_s(sz_type, sizeof(sz_type),
                              "%s%s%s%s%s%s%s%s",
                              (type&0x01)?"FIN":"",
                              (type&0x02)?"SYN":"",
                              (type&0x04)?"RST":"",
                              (type&0x08)?"PSH":"",
                              (type&0x10)?"ACK":"",
                              (type&0x20)?"URG":"",
                              (type&0x40)?"ECE":"",
                              (type&0x80)?"CWR":""
                              );
                    break;
            }
            fprintf(stderr, "%s (%5.4f) TCP  %-21s > %-21s %s\n", direction,
                    timestamp - global_timestamp_start, from, to, sz_type);
            break;
        case FOUND_IPV6:
            break;
        default:
            fprintf(stderr, "%s (%5.4f) UNK  %-21s > %-21s [%u]\n", direction, 
                    timestamp - global_timestamp_start, from, to, parsed.found);
            break;
    }


}

src/main-ptrace.h

0 → 100644
+11 −0
Original line number Diff line number Diff line
#ifndef masscan_main_ptrace_h
#define masscan_main_ptrace_h
#include <stdio.h>
#include <stdint.h>

extern double global_timestamp_start;

void packet_trace(FILE *fp, const unsigned char *px, size_t length, unsigned is_sent);


#endif
Loading