Skip to content

Commit

Permalink
banners
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdavidgraham committed Sep 6, 2013
1 parent f3cc2ea commit e69704b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/event-timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Timeouts {
unsigned mask;

struct TimeoutEntry *freed_list;
struct TimeoutEntry *entries[1024*8*16];
struct TimeoutEntry *slots[1024*1024];
};

/***************************************************************************
Expand All @@ -47,7 +47,7 @@ timeouts_create(uint64_t timestamp)
timeouts = (struct Timeouts *)malloc(sizeof(*timeouts));
memset(timeouts, 0, sizeof(*timeouts));

timeouts->mask = sizeof(timeouts->entries)/sizeof(timeouts->entries[0]) - 1;
timeouts->mask = sizeof(timeouts->slots)/sizeof(timeouts->slots[0]) - 1;

timeouts->current_index = timestamp;

Expand All @@ -72,8 +72,8 @@ timeouts_add(struct Timeouts *timeouts, void *p, uint64_t timestamp, unsigned co
entry->timestamp = timestamp;
entry->pointer = p;
entry->counter = counter;
entry->next = timeouts->entries[index];
timeouts->entries[index] = entry;
entry->next = timeouts->slots[index];
timeouts->slots[index] = entry;
return &entry->counter;
}

Expand All @@ -85,7 +85,7 @@ timeouts_remove(struct Timeouts *timeouts, uint64_t timestamp)
struct TimeoutEvent result;

while (timeouts->current_index <= timestamp) {
struct TimeoutEntry **r_entry = &timeouts->entries[timeouts->current_index & timeouts->mask];
struct TimeoutEntry **r_entry = &timeouts->slots[timeouts->current_index & timeouts->mask];

while (*r_entry && (*r_entry)->timestamp > timestamp)
r_entry = &(*r_entry)->next;
Expand Down
4 changes: 2 additions & 2 deletions src/event-timeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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);

#define TICKS_FROM_SECS(secs) ((secs)*1000ULL)
#define TICKS_FROM_USECS(usecs) ((usecs)/1000ULL)
#define TICKS_FROM_SECS(secs) ((secs)*16384ULL)
#define TICKS_FROM_USECS(usecs) ((usecs)/16384ULL)
#define TICKS_FROM_TV(secs,usecs) (TICKS_FROM_SECS(secs)+TICKS_FROM_USECS(usecs))
#endif
13 changes: 12 additions & 1 deletion src/main-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,19 @@ status_print(struct Status *status, uint64_t count, uint64_t max_count)
double rate = ((double)(count - status->last.count)*1.0/elapsed);
double percent_done = (double)(count*100.0/max_count);
double finished = 0;
status->last_rates[status->last_count++ & 0x7] = rate;
rate = status->last_rates[0]
+ status->last_rates[1]
+ status->last_rates[2]
+ status->last_rates[3]
+ status->last_rates[4]
+ status->last_rates[5]
+ status->last_rates[6]
+ status->last_rates[7]
;
rate /= 8;
if (rate)
finished = (1.0 - percent_done/100.0) * (max_count / rate);
finished = (1.0 - percent_done/100.0) * (max_count / rate);
/* (%u-days %02u:%02u:%02u remaining) */
fprintf(stderr, "rate%6.2f-kpps, %5.2f%% done, %u:%02u:%02u remaining, %llutcbs \r",
rate/1000.0,
Expand Down
3 changes: 3 additions & 0 deletions src/main-status.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ struct Status
} last;
uint64_t timer;
unsigned charcount;

double last_rates[8];
unsigned last_count;
};


Expand Down
21 changes: 11 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,33 +515,34 @@ receive_thread(struct Masscan *masscan,
seqno_me, seqno_them+1);
}

tcpcon_handle(tcpcon, tcb, TCP_WHAT_SYNACK, 0, 0, secs, usecs);
tcpcon_handle(tcpcon, tcb, TCP_WHAT_SYNACK,
0, 0, secs, usecs, seqno_them+1);

} else if (tcb) {
/* If this is an ACK, then handle that first */
if (TCP_IS_ACK(px, parsed.transport_offset)) {
tcpcon_handle(tcpcon, tcb, TCP_WHAT_ACK, 0, seqno_me,
secs, usecs);
tcpcon_handle(tcpcon, tcb, TCP_WHAT_ACK,
0, seqno_me, secs, usecs, seqno_them);
}

/* If this contains payload, handle that */
if (parsed.app_length) {
tcpcon_handle(tcpcon, tcb, TCP_WHAT_DATA,
px + parsed.app_offset, parsed.app_length,
secs, usecs);
secs, usecs, seqno_them);
}

/* If this is a FIN, handle that. Note that ACK + payload + FIN
* can come together */
/* If this is a FIN, handle that. Note that ACK +
* payload + FIN can come together */
if (TCP_IS_FIN(px, parsed.transport_offset)) {
tcpcon_handle(tcpcon, tcb, TCP_WHAT_FIN, 0, seqno_them,
secs, usecs);
tcpcon_handle(tcpcon, tcb, TCP_WHAT_FIN,
0, 0, secs, usecs, seqno_them);
}

/* If this is a RST, then we'll be closing the connection */
if (TCP_IS_RST(px, parsed.transport_offset)) {
tcpcon_handle(tcpcon, tcb, TCP_WHAT_RST, 0, 0,
secs, usecs);
tcpcon_handle(tcpcon, tcb, TCP_WHAT_RST,
0, 0, secs, usecs, seqno_them);
}
} else if (TCP_IS_FIN(px, parsed.transport_offset)) {
/*
Expand Down
44 changes: 34 additions & 10 deletions src/proto-tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ tcpcon_timeouts(struct TCP_ConnectionTable *tcpcon, unsigned secs, unsigned usec
tcb,
TCP_WHAT_TIMEOUT,
0, 0,
secs, usecs);
secs, usecs,
0);

}
}
Expand Down Expand Up @@ -521,9 +522,11 @@ handle_ack(
***************************************************************************/
void
tcpcon_handle(struct TCP_ConnectionTable *tcpcon, struct TCP_Control_Block *tcb,
int what, const void *payload, size_t payload_length,
unsigned secs, unsigned usecs)
int what, const void *vpayload, size_t payload_length,
unsigned secs, unsigned usecs,
unsigned seqno_them)
{
const unsigned char *payload = (const unsigned char *)vpayload;
if (tcb == NULL)
return;

Expand Down Expand Up @@ -568,9 +571,10 @@ tcpcon_handle(struct TCP_ConnectionTable *tcpcon, struct TCP_Control_Block *tcb,
break;

case STATE_READY_TO_SEND<<8 | TCP_WHAT_ACK:
/* There's actually nothing that goes on in this state. We are just waiting
* for the timer to expire. In the meanwhile, though, the other side is
* might acknowledge that we sent a SYN-ACK */
/* There's actually nothing that goes on in this state. We are
* just waiting for the timer to expire. In the meanwhile,
* though, the other side is might acknowledge that we sent
* a SYN-ACK */

/* NOTE: the arg 'payload_length' was overloaded here to be the
* 'ackno' instead */
Expand Down Expand Up @@ -605,7 +609,8 @@ tcpcon_handle(struct TCP_ConnectionTable *tcpcon, struct TCP_Control_Block *tcb,
0x18,
x, x_len);
LOG(4, "%u.%u.%u.%u - sending payload %u bytes\n",
(tcb->ip_them>>24)&0xFF, (tcb->ip_them>>16)&0xFF, (tcb->ip_them>>8)&0xFF, (tcb->ip_them>>0)&0xFF,
(tcb->ip_them>>24)&0xFF, (tcb->ip_them>>16)&0xFF,
(tcb->ip_them>>8)&0xFF, (tcb->ip_them>>0)&0xFF,
x_len);

/* Increment our sequence number */
Expand Down Expand Up @@ -633,11 +638,29 @@ tcpcon_handle(struct TCP_ConnectionTable *tcpcon, struct TCP_Control_Block *tcb,
{
unsigned err;

/* If this packet skips over a lost packet on the Internet,
* then we need to discard it */
if (seqno_them - tcb->seqno_them < 10000
&& 0 < seqno_them - tcb->seqno_them)
return;

/* If this payload overlaps something we've already seen, then
* shrink the payload length */
if (tcb->seqno_them - seqno_them < 10000) {
unsigned already_received = tcb->seqno_them - seqno_them;
if (already_received >= payload_length)
return;
else {
payload += already_received;
payload_length -= already_received;
}
}

/* extract a banner if we can */
err = parse_banner(
tcpcon,
tcb,
(const unsigned char*)payload,
payload,
payload_length);

/* move their sequence number forward */
Expand All @@ -659,7 +682,7 @@ tcpcon_handle(struct TCP_ConnectionTable *tcpcon, struct TCP_Control_Block *tcb,
break;

case STATE_READY_TO_SEND<<8 | TCP_WHAT_FIN:
tcb->seqno_them = (uint32_t)payload_length + 1;
tcb->seqno_them = seqno_them + 1;
tcpcon_send_packet(tcpcon, tcb,
0x14, /*reset */
0, 0);
Expand Down Expand Up @@ -737,10 +760,11 @@ tcpcon_handle(struct TCP_ConnectionTable *tcpcon, struct TCP_Control_Block *tcb,


case STATE_WAITING_FOR_RESPONSE<<8 | TCP_WHAT_FIN:
tcb->seqno_them = (uint32_t)payload_length + 1;
tcb->seqno_them = seqno_them + 1;
tcpcon_send_packet(tcpcon, tcb,
0x11,
0, 0);
tcb->seqno_me++;
break;
case STATE_WAITING_FOR_RESPONSE<<8 | TCP_WHAT_TIMEOUT:
tcpcon_send_packet(tcpcon, tcb,
Expand Down
3 changes: 2 additions & 1 deletion src/proto-tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ enum TCP_What {
void
tcpcon_handle(struct TCP_ConnectionTable *tcpcon, struct TCP_Control_Block *entry,
int what, const void *p, size_t length,
unsigned secs, unsigned usecs);
unsigned secs, unsigned usecs,
unsigned seqno_them);


/**
Expand Down

0 comments on commit e69704b

Please sign in to comment.