Thread: [Tcpick-project] [PATCH] disordered first payloads / connection incarnations (1)
Status: Beta
Brought to you by:
duskdruid
From: Takashi W. <re...@sp...> - 2009-06-30 10:45:58
|
Hi. I think I managed to do two things: (1) to prevent the flush from skipping the first payload(s) of session packets which were not ordered correctly [CODE] take a look at: (1) while loop cond. : flush_ack() : fragments.c (2) ALL : addfr() : fragments.c (3) max_off variable : established_packet() : verify.c (2) (with the new TCP connection incarnation mechanism) to prevent that stale connections were reused [CODE] take a look at: (1) conn.h (2) def.h (3) tracker.c (4) write.c (5) verify.c (6) debug.c (refactoring stuff) (7) display.c (refactoring stuff) (8) extern.h (refactoring stuff) (9) globals.h (refactoring stuff) (10) timer.c (refactoring stuff) (11) tcpick.c (refactoring stuff/minor changes(confirmFlush())) Also I rewrote some parts of code to remove minor bugs. [CODE] take a look at: (1) break statement : args.c (2) IP_SIZE/ip_size : loop.c (might well be unnecessary) (3) flags.trackonly : tracker.c On the way to develop, I was forgetting to update some pointers and encountered a bus error. To figure out the cause, I unnecesarily replaced some [cm]alloc calls with static variables and the memory footprint might've been a little bigger. You can ignore those parts of this patch if you'd like. [CODE] take a look at: (1) datalink.c (2) def.h (3) display.c (4) globals.h (5) lookup_query.c (6) lookup_tree.c (7) msg.c (8) quit.c (9) tcpick.c (10) time.c (11) timer.c (12) write.c I did: First patch http://cdn.debian.or.jp/debian/pool/main/t/tcpick/tcpick_0.2.1-6.diff.gz Then patch this: ===== file: patch-tcpick-0.2.1-6-watanabe1.24.part1of2 ===== diff -rupN deb-orig/tcpick-0.2.1/src/args.c tcpick-0.2.1/src/args.c --- deb-orig/tcpick-0.2.1/src/args.c 2009-06-30 09:02:57.000000000 +0900 +++ tcpick-0.2.1/src/args.c 2009-06-30 09:04:59.000000000 +0900 @@ -274,7 +274,7 @@ parse_args(int argc, char ** argv) "-F option needs a number\n" "i.e. -F1 or -F2" ); } - + break; case 'h': flags.header = 1; break; diff -rupN deb-orig/tcpick-0.2.1/src/conn.h tcpick-0.2.1/src/conn.h --- deb-orig/tcpick-0.2.1/src/conn.h 2004-12-31 22:53:30.000000000 +0900 +++ tcpick-0.2.1/src/conn.h 2009-06-30 09:04:59.000000000 +0900 @@ -1,6 +1,6 @@ /* * conn.h -- enums and structs that describe a connection: - * enum PART, struct CONN, struct HOST_DESC and enum STATUS + * enum PART, struct CONN_DESC/CONN_INCAR, struct HOST_DESC/HOST_INCAR and enum STATUS * * Part of the tcpick project * @@ -20,25 +20,37 @@ enum PART BOTH }; -struct HOST_DESC { -/* HOST_DESC describes the client or server part of a tcp session */ - u_int16_t port; - struct in_addr ip; - +/* a TCP connection host incarnation */ +struct HOST_INCAR { +/* HOST_INCAR describes the client or server part of a tcp connection incarnation */ u_int32_t sip; - /* why have I called them SIP and not SIN? + /* why have I called them SIP and not SYN? well, SIP means Sequence Initial Pointer - that is exactly the SIN plus one + that is exactly the SYN plus one */ + u_int32_t max_off; + int wlen; + + struct FRAGMENT * unack; + FILE * file; /* NULL means closed */ char * filename; char * lockfilename; + + struct HOST_INCAR * oth; +}; + +struct HOST_DESC { + u_int16_t port; + struct in_addr ip; + enum PART side; - int wlen; - - struct FRAGMENT * unack; struct HOST_DESC * oth; + + /* the same thing as &(conn_desc->lastincar->{client,server}) */ + struct HOST_INCAR * lastincar; + /* for hdesc local variables */ }; enum STATUS { @@ -56,27 +68,46 @@ enum STATUS { EXPIRED }; -struct CONN -/* the struct CONN describes a tcp connection */ -{ +struct CONN_INCAR +/* a TCP connection incarnation */ +{ enum STATUS status; - FILE *both_file; /* FIXME: wtf??? */ - int closer; /* when a connection is int open state this must be set to 0 when the client has sent the closing FIN, then closer=1 when it is the server closer=2 */ + + struct HOST_INCAR client; + struct HOST_INCAR server; + + FILE *both_file; /* FIXME: wtf??? */ + + int cyclenum; + + time_t lasttime; /* time of last packet in connection */ + + struct CONN_INCAR * prev; +}; + +struct CONN_DESC +/* the struct CONN_DESC describes a tcp connection */ +{ + struct HOST_DESC client; + struct HOST_DESC server; + + /* always respect the last one in the case the connection is reused. + * NOT the first one of pending ones because it may well be obsolete already! + */ + struct CONN_INCAR * lastincar; + int num; /* the number of this connection (useful for calculate the color tracked) */ - struct HOST_DESC client; - struct HOST_DESC server; + int count_cycles; - struct CONN * next; /* pointer for the next ring of the chain */ - - time_t lasttime; /* time of last packet in connection */ + struct CONN_DESC * next; /* pointer for the next ring of the chain */ }; diff -rupN deb-orig/tcpick-0.2.1/src/datalink.c tcpick-0.2.1/src/datalink.c --- deb-orig/tcpick-0.2.1/src/datalink.c 2004-12-31 22:53:30.000000000 +0900 +++ tcpick-0.2.1/src/datalink.c 2009-06-30 09:04:59.000000000 +0900 @@ -31,13 +31,7 @@ char *datalink2str(int dl_id) { - char *str = NULL; - if (str) - { - S_free (str); - str = NULL; - } - str = S_calloc(128 , 1); + static char str[STATIC_CHAR_BUF_SIZE]; /* FIXME: [0.2.1-6 20090630] S_malloc related */ debug( "datalink number: %d" , dl_id ); switch(dl_id) { diff -rupN deb-orig/tcpick-0.2.1/src/debug.c tcpick-0.2.1/src/debug.c --- deb-orig/tcpick-0.2.1/src/debug.c 2004-12-31 22:53:30.000000000 +0900 +++ tcpick-0.2.1/src/debug.c 2009-06-30 09:04:59.000000000 +0900 @@ -29,7 +29,7 @@ #include "tcpick.h" void -print_conn_chain (struct CONN * f) +print_conn_chain (struct CONN_DESC * f) { while (f) { printf ("%x->",f); diff -rupN deb-orig/tcpick-0.2.1/src/def.h tcpick-0.2.1/src/def.h --- deb-orig/tcpick-0.2.1/src/def.h 2005-01-01 00:49:03.000000000 +0900 +++ tcpick-0.2.1/src/def.h 2009-06-30 09:30:56.000000000 +0900 @@ -25,7 +25,14 @@ #define S_calloc calloc #define S_malloc malloc -#define S_free free + /* Don't forget to include <stdlib.h> */ + /* Initialize chunks after malloc calls */ +#define S_strndup strndup +#define S_strdup strdup +#define S_free(p){ \ + free(p); \ + p = NULL; \ + } #define SEPARATOR "----------------" @@ -56,3 +63,29 @@ #endif /* TCPICK_DEBUG */ #define IP_SIZE 20 + + +#define STATIC_CHAR_BUF_SIZE 128 + /* FIXME: [0.2.1-6 20090630] S_malloc related */ +#define MAX_IPV4_LENGTH_OF_DOTS_AND_NUMBERS 16 + /* FIXME: [0.2.1-6 20090630] S_malloc related */ + +#define CURRENT_CONN (prev_conn->next) + +#define IS_CLIENT_SENDER \ + CURRENT_CONN->client.ip.s_addr == ippacket->ip_src.s_addr && \ + CURRENT_CONN->server.ip.s_addr == ippacket->ip_dst.s_addr && \ + CURRENT_CONN->client.port == tcppacket->source && \ + CURRENT_CONN->server.port == tcppacket->dest + +#define IS_SERVER_SENDER \ + CURRENT_CONN->client.ip.s_addr == ippacket->ip_dst.s_addr && \ + CURRENT_CONN->server.ip.s_addr == ippacket->ip_src.s_addr && \ + CURRENT_CONN->client.port == tcppacket->dest && \ + CURRENT_CONN->server.port == tcppacket->source + +#define ASSERT_NOT_NULL(f, p){ \ + if( p == NULL ) \ + suicide(f, "The pointer is NULL."); \ + } + diff -rupN deb-orig/tcpick-0.2.1/src/display.c tcpick-0.2.1/src/display.c --- deb-orig/tcpick-0.2.1/src/display.c 2009-06-30 09:02:57.000000000 +0900 +++ tcpick-0.2.1/src/display.c 2009-06-30 09:04:59.000000000 +0900 @@ -35,7 +35,7 @@ char * status2str( enum STATUS status ) /* converts the status flag to a string */ { - char *str = (char *)S_calloc(1,128); + static char str[STATIC_CHAR_BUF_SIZE]; /* FIXME: [0.2.1-6 20090630] S_malloc related */ switch(status) { case SYN_SENT: strcpy( str, "SYN-SENT" ); @@ -76,12 +76,12 @@ status2str( enum STATUS status ) } int -display_status( FILE * out, struct CONN * conn, enum STATUS status ) +display_status( FILE * out, struct CONN_DESC * conn, enum STATUS status ) /* display a "status banner" */ { char *client_name; char *server_name; - char *s_time; + static char s_time[STATIC_CHAR_BUF_SIZE]; /* FIXME: [0.2.1-6 20090630] S_malloc related */ char *status_string; if ( flags.notstatus ) { @@ -91,21 +91,16 @@ display_status( FILE * out, struct CONN client_name=(char *)lookup(conn->client.ip); - s_time = (char *)S_calloc( 128, 1 ); - if( time_ascii( s_time ) ) color( c_TIME, out,"%-16s ",s_time ); /* FIXME: check */ color( c_NUMBERING, out,"%-6d ",conn->num ); - S_free ( s_time ); status_string = status2str( status ); color( _STATUS_COLOR(status), out,"%-14s",status_string ); - S_free( status_string ); - color( c_CLIENTNAME_STATUS, out, " %s:%s ", client_name, getportname(conn->client.port) ); @@ -130,7 +125,7 @@ int display_header( FILE * out ) { /* FIXME: sucks */ char * source = NULL; char * dest = NULL; - char * s_time = (char *)S_calloc(128,1); + static char s_time[STATIC_CHAR_BUF_SIZE]; /* FIXME: [0.2.1-6 20090630] S_malloc related */ source=(char *)lookup(ippacket->ip_src); debug("[display_header]: source(%x)",source); debug("[display_header]: source=%s",source); @@ -138,7 +133,6 @@ int display_header( FILE * out ) if( time_ascii(s_time) ) color ( c_TIME, out,"%-16s ",s_time ); - color( c_CLIENTNAME_HEADER, out,"%s",source ); fprintf(out,":"); diff -rupN deb-orig/tcpick-0.2.1/src/extern.h tcpick-0.2.1/src/extern.h --- deb-orig/tcpick-0.2.1/src/extern.h 2005-01-14 00:34:01.000000000 +0900 +++ tcpick-0.2.1/src/extern.h 2009-06-30 09:04:59.000000000 +0900 @@ -30,7 +30,7 @@ extern int ip_size; extern int tcp_size; /* extern int data_trasl; */ -extern struct CONN * first_conn; -extern struct CONN * last_conn; +extern struct CONN_DESC * first_conn; +extern struct CONN_DESC * last_conn; extern enum EXPIRED_FLAG expired_flag; diff -rupN deb-orig/tcpick-0.2.1/src/fragments.c tcpick-0.2.1/src/fragments.c --- deb-orig/tcpick-0.2.1/src/fragments.c 2005-01-01 00:56:38.000000000 +0900 +++ tcpick-0.2.1/src/fragments.c 2009-06-30 17:18:57.000000000 +0900 @@ -61,28 +61,127 @@ #include "tcpick.h" #include "extern.h" -#define MATCH_OFF(fr, oth) fr->off + fr->len == oth->off +#define THIS_IS_FRAGMENTS_C "fragments.c" +#define MATCH_OFF(prev, this) ( this != NULL ? (prev->off + prev->len == this->off) : 0 ) + +#define ASSERT_FR_NOT_NULL(fr){ \ + ASSERT_NOT_NULL(THIS_IS_FRAGMENTS_C, fr); \ + } + +#define ASSERT_FR_NOT_ZERO(fr){ \ + ASSERT_FR_NOT_NULL(fr); \ + if( fr->len == 0 ) \ + suicide(THIS_IS_FRAGMENTS_C, "The length of the fragment is zero."); \ + } + +/* append a dummy fragment */ +#define APPEND_DUM_TO(fr){ \ + ASSERT_FR_NOT_ZERO(fr); \ + struct FRAGMENT * dum; \ + dum = S_malloc( sizeof *dum ); \ + memset(dum, 0, sizeof *dum ); \ + dum->payload = S_malloc( 0 ); \ + dum->off = fr->off + fr->len; \ + /* memcpy( dum->payload, "", 0 ); */ \ + dum->len = 0; \ + dum->flag = BREAK; \ + \ + if( fr->next && fr->next->off <= dum->off ) \ + suicide(THIS_IS_FRAGMENTS_C, "Illegal dummy appending."); \ + dum->next = fr->next; \ + fr->next = dum; \ + } + +/* + * fr->next == NULL is allowed + */ +#define CHECK_CONTIGUITY(fr){ \ + ASSERT_FR_NOT_NULL(fr); \ + if ( MATCH_OFF(fr, fr->next) ){ \ + fr->flag = CONTINUE; \ + }else{ \ + if( fr->len != 0 ){ \ + fr->flag = CONTINUE; \ + APPEND_DUM_TO(fr); \ + }else{ \ + fr->flag = BREAK; \ + } \ + } \ + } + + +/* the first only */ +#define INSERT_FR_INTO(first, new){ \ + ASSERT_FR_NOT_NULL(first); \ + ASSERT_FR_NOT_NULL(new); \ + new->next = first; \ + first = new; \ + CHECK_CONTIGUITY(first); \ + } + +#define DO_REPLACE_FR(old, new){ \ + ASSERT_FR_NOT_NULL(old); \ + ASSERT_FR_NOT_NULL(new); \ + if( old->len != 0 && new->len == 0 ) \ + suicide(THIS_IS_FRAGMENTS_C, "replacing old->len != 0 with new->len == 0."); \ + new->next = old->next; \ + struct FRAGMENT * tmp; \ + tmp = old; \ + old = new; \ + FREE_FR (tmp); \ + \ + if( new->len != 0 ) \ + CHECK_CONTIGUITY(new); \ + } + +#define REPLACE_FR_AT(prev, old, new){ \ + DO_REPLACE_FR(old, new); \ + /* after the replace happened */ \ + CHECK_CONTIGUITY(prev); \ + } + +/* + * (1) The length of the dummy SYN fragment is zero. + * (2) Under some circumstances, the length of prev is possible to be zero. + * For example, + * ... + * Pkt#X+1 off=1000 len=10 + * Pkt#X+2 off=1030 len=30 + * Pkt#X+3 off=1010 len=20 + * Pkt#X+4 off=1060 len=40 + * ... + */ +#define ASSERT_FR(prev, this){ \ + ASSERT_FR_NOT_NULL(prev); \ + if( prev->next != NULL && this != NULL && prev->next->off <= this->off ) \ + /* prev->next->off == this->off is also not allowed, because this is not called by CHECK_CONTIGUITY */ \ + suicide(THIS_IS_FRAGMENTS_C, "prev->next->off is less than or equal to this->off."); \ + } + +/* + * assuming that prev->next->off is greater than this->off + */ #define ATTACH_FR(prev, this){ \ - \ - this->next = prev->next; \ - prev->next = this; \ - \ - if (MATCH_OFF(this, prev->next)) \ - this->flag = CONTINUE; \ - else \ - this->flag = BREAK; \ - } + ASSERT_FR(prev, this); \ + \ + this->next = prev->next; \ + prev->next = this; \ + \ + CHECK_CONTIGUITY(prev); \ + CHECK_CONTIGUITY(this); \ + } #define FREE_FR(fr) S_free( fr->payload ); S_free( fr ); #define DELETE_FIRST_FR(list) { \ - struct FRAGMENT * tmp; \ - tmp = * list; \ - * list = ( * list )->next; \ - FREE_FR (tmp) \ - } + struct FRAGMENT * tmp; \ + tmp = * list; \ + * list = ( * list )->next; \ + FREE_FR (tmp) \ + } + __inline__ int addfr( struct FRAGMENT ** first, int wlen, @@ -100,83 +199,95 @@ addfr( struct FRAGMENT ** first, { struct FRAGMENT * new; - struct FRAGMENT * curr; + struct FRAGMENT * prev; - if ( data_off >= wlen ) { + if ( data_off < wlen ) return 0; - /* allocate and build the fragment */ - new = ( struct FRAGMENT * ) S_calloc ( 1, sizeof( struct FRAGMENT )); - new->payload = ( u_char * ) S_calloc( 1, payload_len ); - new->off = data_off; - memcpy( new->payload, payload, payload_len ); - new->len = payload_len; - - /* now insert the fragment in the flagged linked list */ + /* allocate and build the fragment */ + new = S_malloc( sizeof *new ); + memset(new, 0, sizeof *new); + new->payload = S_malloc( payload_len * sizeof *new->payload ); + new->off = data_off; + memcpy( new->payload, payload, payload_len ); + new->len = payload_len; + + /* now insert the fragment in the flagged linked list */ + + /* linked list was empty */ + if (! * first ) { + * first = new; + (* first)->next = NULL; + return 1; + } - /* linked list was empty */ - if (! * first ) { - * first = new; - (* first)->next = NULL; + /* insert into the front of the first */ + if ( (* first)->off > new->off) { + INSERT_FR_INTO( (* first), new ); + return 1; + } + /* replace first */ + else if ( (* first)->off == new->off) { + if( (* first)->len == 0 ){ + DO_REPLACE_FR((* first), new); return 1; + }else{ + /* a duplicate : should be logged? */ + FREE_FR (new); + return 0; } + } - /* replace first */ - if ( (* first)->off > new->off) { - new->next = * first; - * first = new; + prev = * first; + + while ( prev ) { + if ( ! prev->next ) { + /* it is the last */ + ATTACH_FR( prev, new ); return 1; } - - curr = * first; - - while ( curr ) { - if( curr->flag == BREAK && - MATCH_OFF(curr, new) ) { - /* the new data fragment is exactly next to curr */ - ATTACH_FR( curr, new ); - return 1; - } - else if ( curr->next ) { - if( curr->next->off > new->off ) { - /* the new data fragment is _not_ exactly next to curr, - but it must be there because the next - fragment has a bigger offset */ - ATTACH_FR( curr, new ); - return 1; - } - } else { - /* it is the last */ - curr->next = new; + + if( prev->next->off > new->off ) { + ATTACH_FR( prev, new ); + return 1; + + }else if( prev->next->off == new->off ){ + if( prev->next->len == 0 ){ + REPLACE_FR_AT(prev, prev->next, new); return 1; + }else{ + /* a duplicate */ + FREE_FR(new); + return 0; } - - curr = curr->next; } - return 0; + + prev = prev->next; } + FREE_FR(new); + return 0; } __inline__ int -flush_ack ( struct HOST_DESC * desc, - struct CONN * conn_ptr, - int ack_num ) +flush_ack ( struct HOST_DESC * hdesc, + struct CONN_DESC * conn_ptr) /* called by established_packet * when a "ack" packet comes to the network device, * data that are unacknowledged will be acknowledged and immediatly * sent to the write engine wrebuild */ { -#define LIST (desc->unack) - +#define LIST (hdesc->lastincar->unack) + register struct HOST_INCAR * hinc = hdesc->lastincar; + while( LIST && - LIST->off <= ack_num ) { - - flowflush( conn_ptr, desc->oth, LIST->payload, LIST->len ); + LIST->off <= hinc->max_off && + LIST->flag == CONTINUE ) { + flowflush( conn_ptr, hdesc->oth, LIST->payload, LIST->len ); - debug("wrote off=%i wlen=%i len=%i ack=%i\n", - LIST->off, desc->wlen, LIST->len, ack_num); + debug("wrote off=%i wlen=%i len=%i ack(offset)=%i\n", + LIST->off, hinc->wlen, LIST->len, hinc->max_off); - desc->wlen += LIST->len; + hinc->wlen += LIST->len; DELETE_FIRST_FR ( & LIST ); } } diff -rupN deb-orig/tcpick-0.2.1/src/globals.h tcpick-0.2.1/src/globals.h --- deb-orig/tcpick-0.2.1/src/globals.h 2005-01-14 00:33:54.000000000 +0900 +++ tcpick-0.2.1/src/globals.h 2009-06-30 09:04:59.000000000 +0900 @@ -25,8 +25,8 @@ int count_opened; char *dl_str = NULL; char *dev = NULL; /* name of the device to use */ char *readfile = NULL; /* name of the file to read */ -char *net = NULL; /* dot notation of the network address */ -char *mask = NULL; /* dot notation of the network mask */ +char net[MAX_IPV4_LENGTH_OF_DOTS_AND_NUMBERS]; /* dot notation of the network address */ +char mask[MAX_IPV4_LENGTH_OF_DOTS_AND_NUMBERS]; /* dot notation of the network mask */ char *filter = NULL; int ret = 0; /* return code*/ @@ -38,7 +38,7 @@ int datalink_size = 0; int ip_size = 0; int tcp_size = 0; -struct CONN * first_conn; -struct CONN * last_conn; +struct CONN_DESC * first_conn; +struct CONN_DESC * last_conn; enum EXPIRED_FLAG expired_flag; diff -rupN deb-orig/tcpick-0.2.1/src/lookup_query.c tcpick-0.2.1/src/lookup_query.c --- deb-orig/tcpick-0.2.1/src/lookup_query.c 2004-12-31 22:53:30.000000000 +0900 +++ tcpick-0.2.1/src/lookup_query.c 2009-06-30 09:04:59.000000000 +0900 @@ -34,6 +34,8 @@ lookup_new(struct in_addr ia) { register struct hostent * he; register struct _l_node * node; + + err("lookup_new function called. (lookup_new contains an _l_alloc function call, which contains a S_strdup call currently)"); /* FIXME: [0.2.1-6 20090630] S_malloc related */ he = gethostbyaddr((char *)&ia, sizeof(struct in_addr), AF_INET); @@ -66,30 +68,21 @@ char * getportname(u_int16_t port) /* FIXME: completely rewrite! */ { - static char *service=NULL; - static char *s_port=NULL; + static char service[STATIC_CHAR_BUF_SIZE]; /* FIXME: [0.2.1-6 20090630] S_malloc related */ + static char s_port[STATIC_CHAR_BUF_SIZE]; /* FIXME: [0.2.1-6 20090630] S_malloc related */ struct servent * ptr; - if(service) { - S_free(service); - service=NULL; - } - if(s_port) { - S_free(s_port); - s_port=NULL; - } - if(!flags.numericalports) if(ptr=(struct servent *)getservbyport(port, "tcp")) if(ptr->s_name) { debug("service name resolved: %s",ptr->s_name); - return service=(char *)strdup(ptr->s_name); + strcpy(service, ptr->s_name); + return service; } debug( "service name NOT resolved: %d",ntohs(port) ); /* \n carachter should be inserted because this messages comes after * a "printf" message not ended in \n in another function */ - s_port=(char *)S_calloc(128,1); sprintf(s_port,"%d",ntohs(port)); return s_port; } diff -rupN deb-orig/tcpick-0.2.1/src/lookup_tree.c tcpick-0.2.1/src/lookup_tree.c --- deb-orig/tcpick-0.2.1/src/lookup_tree.c 2004-12-31 22:53:30.000000000 +0900 +++ tcpick-0.2.1/src/lookup_tree.c 2009-06-30 09:04:59.000000000 +0900 @@ -71,11 +71,11 @@ _l_alloc(struct in_addr ip, char * name) { register struct _l_node * new; - new = (struct _l_node *) S_malloc(sizeof(struct _l_node)); - memset(new, 0, sizeof(struct _l_node)); + new = S_malloc( sizeof *new ); + memset(new, 0, sizeof *new); - new->name = (char *)strdup(name); - /* FIXME: not sure strdup is a good thing*/ + new->name = (char *)S_strdup(name); + /* FIXME: not sure S_strdup is a good thing*/ memcpy(&(new->ip), &ip, sizeof(struct in_addr)); return new; diff -rupN deb-orig/tcpick-0.2.1/src/loop.c tcpick-0.2.1/src/loop.c --- deb-orig/tcpick-0.2.1/src/loop.c 2005-01-17 00:26:56.000000000 +0900 +++ tcpick-0.2.1/src/loop.c 2009-06-30 09:04:59.000000000 +0900 @@ -61,13 +61,21 @@ got_packet (u_char * useless, # error "Please fix <bits/endian.h>" #endif + ip_size = ippacket->ip_hl * 4; + if (ip_size < 20) { + err("[@%d] Invalid IP header length: %u bytes. Ignored.\n", count_packets, ip_size); + goto end; + }else if( ip_size != IP_SIZE ){ + err("[@%d] IP header length is not %u bytes: %u bytes. Continuing.\n", count_packets, IP_SIZE, ip_size); + } + switch(ippacket->ip_p) { case IPPROTO_TCP: - tcppacket = (struct tcphdr *)(packet + datalink_size + IP_SIZE); + tcppacket = (struct tcphdr *)(packet + datalink_size + ip_size); tcp_size = tcppacket->doff * 4; - payload = (u_char *)(packet + datalink_size + IP_SIZE + tcp_size); - payload_len = iplen - IP_SIZE - tcp_size; + payload = (u_char *)(packet + datalink_size + ip_size + tcp_size); + payload_len = iplen - ip_size - tcp_size; #ifdef TCPICK_DEBUG if( payload_len != (hdr->len - (int)( payload - packet ) ) ) { @@ -76,15 +84,15 @@ got_packet (u_char * useless, "differs by %d bytes\n" "hdr->len = %d\n" "datalink_size = %d\n" - "IP_SIZE = %d\n" + "ip_size = %d\n" "iplen = %d\n" "tcp_size = %d\n" - "iplen - IP_SIZE - tcp_size = %d\n" + "iplen - ip_size - tcp_size = %d\n" "(hdr->len - (int)( payload - packet ) = %d\n", payload_len - (hdr->len - (int)( payload - packet )), hdr->len, datalink_size, - IP_SIZE, + ip_size, iplen, tcp_size, payload_len, @@ -109,8 +117,8 @@ got_packet (u_char * useless, break; case IPPROTO_UDP: - udppacket = (struct udphdr *)(packet + datalink_size + IP_SIZE); - payload = (u_char *)(packet + datalink_size + IP_SIZE + sizeof(*udppacket)); + udppacket = (struct udphdr *)(packet + datalink_size + ip_size); + payload = (u_char *)(packet + datalink_size + ip_size + sizeof(*udppacket)); payload_len = hdr->len - (int)( payload - packet ); if( payload_len == 0 || SILENCE ) diff -rupN deb-orig/tcpick-0.2.1/src/msg.c tcpick-0.2.1/src/msg.c --- deb-orig/tcpick-0.2.1/src/msg.c 2005-01-18 21:02:27.000000000 +0900 +++ tcpick-0.2.1/src/msg.c 2009-06-30 09:04:59.000000000 +0900 @@ -90,11 +90,11 @@ sorry ( char * func, char * desc ) color( c_ERR, stderr, "SORRY:\t" ); len = strlen(func) + strlen(desc) + 16; - msg = S_malloc( len ); + msg = S_malloc( len * sizeof *msg ); sprintf( msg, "[%s] %s", func, desc ); perror( msg ); - free(msg); + S_free(msg); } diff -rupN deb-orig/tcpick-0.2.1/src/quit.c tcpick-0.2.1/src/quit.c --- deb-orig/tcpick-0.2.1/src/quit.c 2005-01-18 20:56:27.000000000 +0900 +++ tcpick-0.2.1/src/quit.c 2009-06-30 09:04:59.000000000 +0900 @@ -40,11 +40,12 @@ fault( char * func, char * desc ) color( c_ERR, stderr, "FAULT:\t" ); len = strlen(func) + strlen(desc) + 16; - msg = S_malloc( len ); + msg = S_malloc( len * sizeof *msg ); sprintf( msg, "[%s] %s", func, desc ); perror( msg ); + S_free(msg); exit(TCPICK_FAILURE); } @@ -81,6 +82,7 @@ cleanup() process termination (set by atexit) */ { print_statistics(); + S_free( first_conn ); /* FIXME: unnecessary? */ } void diff -rupN deb-orig/tcpick-0.2.1/src/tcpick.c tcpick-0.2.1/src/tcpick.c --- deb-orig/tcpick-0.2.1/src/tcpick.c 2009-06-30 09:02:57.000000000 +0900 +++ tcpick-0.2.1/src/tcpick.c 2009-06-30 17:33:56.000000000 +0900 @@ -207,12 +207,12 @@ int main(int argc, char **argv) } addr.s_addr = netp; - net = (char *)strdup( inet_ntoa(addr) ); + strcpy(net, inet_ntoa(addr)); if(! net ) fault( "main", "inet_ntoa" ); addr.s_addr = maskp; - mask = (char *)strdup( inet_ntoa(addr) ); + strcpy(mask, inet_ntoa(addr)); if (! mask ) { fault( "main", "inet_ntoa" ); } @@ -239,7 +239,7 @@ int main(int argc, char **argv) &filter_compiled, filter, 0, - (int)net + netp /* FIXME: [0.2.1-6 20090630] S_malloc related */ ) == -1) ) err("error compiling filter \"%s\"",filter); @@ -255,7 +255,8 @@ int main(int argc, char **argv) debug ( "datalink header size: %d", datalink_size ); /* setting entry point of linked-list of connections tracked */ - first_conn = (struct CONN *)S_calloc( sizeof(struct CONN), 1 ); + first_conn = S_malloc( sizeof *first_conn ); + memset(first_conn, 0, sizeof *first_conn ); /* FIXME: unnecessary? */ last_conn = first_conn; /* see timer.c for further details */ @@ -270,6 +271,7 @@ int main(int argc, char **argv) if( readfile ) { msg( 1, c_DONE, "%s: done reading from %s", TCPICK_NAME, readfile ); + confirmFlush(); exit ( TCPICK_SUCCESS ); } else { suicide( "main", "exited pcap loop" ); diff -rupN deb-orig/tcpick-0.2.1/src/time.c tcpick-0.2.1/src/time.c --- deb-orig/tcpick-0.2.1/src/time.c 2005-01-09 09:21:44.000000000 +0900 +++ tcpick-0.2.1/src/time.c 2009-06-30 09:04:59.000000000 +0900 @@ -30,27 +30,23 @@ char * time_ascii(char * ret) { - - struct timeval *tp; - struct timezone *tzp; + struct timeval tp = {}; /* FIXME: [0.2.1-6 20090630] S_malloc related */ + struct timezone tzp = {}; /* FIXME: [0.2.1-6 20090630] S_malloc related */ struct tm * brokentime; if(flags.displaytime == NOTHING_TIME) goto retNULL; - tp = (struct timeval * ) S_malloc( sizeof(struct timeval) ); - tzp = (struct timezone * ) S_malloc( sizeof(struct timezone) ); - - memset(tp, 0, sizeof(struct timeval)); - memset(tzp, 0, sizeof(struct timeval)); + /* memset(&tp, 0, sizeof tp); */ + /* memset(&tzp, 0, sizeof tzp); */ - if(gettimeofday(tp, tzp)) { + if(gettimeofday(&tp, &tzp)) { perror("gettimeofday returned not 0!"); goto retNULL; } else { - brokentime = localtime(&(tp->tv_sec)); + brokentime = localtime(&(tp.tv_sec)); switch (flags.displaytime) { case ONLYTIME: @@ -58,7 +54,7 @@ char * time_ascii(char * ret) brokentime->tm_hour, brokentime->tm_min, brokentime->tm_sec, - tp->tv_usec + tp.tv_usec ); break; @@ -79,7 +75,7 @@ char * time_ascii(char * ret) brokentime->tm_hour, brokentime->tm_min, brokentime->tm_sec, - tp->tv_usec + tp.tv_usec ); break; @@ -93,9 +89,6 @@ char * time_ascii(char * ret) } } - S_free ( tp ); - S_free ( tzp ); - return ret; retNULL: diff -rupN deb-orig/tcpick-0.2.1/src/timer.c tcpick-0.2.1/src/timer.c --- deb-orig/tcpick-0.2.1/src/timer.c 2005-01-18 21:15:46.000000000 +0900 +++ tcpick-0.2.1/src/timer.c 2009-06-30 09:04:59.000000000 +0900 @@ -30,8 +30,6 @@ #include "tcpick.h" #include "extern.h" -#define CURRENT_CONN (prev_conn->next) - void set_timer() { @@ -69,15 +67,14 @@ void set_timer() * expired connections. */ { - struct itimerval * expired_timer = (struct itimerval *) - malloc(sizeof(struct itimerval)); + struct itimerval expired_timer = {}; /* FIXME: [0.2.1-6 20090630] S_malloc related */ - expired_timer->it_interval.tv_sec=1; - expired_timer->it_interval.tv_usec=1; - expired_timer->it_value.tv_sec=1; - expired_timer->it_value.tv_usec=1; + expired_timer.it_interval.tv_sec=1; + expired_timer.it_interval.tv_usec=1; + expired_timer.it_value.tv_sec=1; + expired_timer.it_value.tv_usec=1; - if ( setitimer( ITIMER_REAL, expired_timer, NULL ) == -1 ) { + if ( setitimer( ITIMER_REAL, &expired_timer, NULL ) == -1 ) { sorry("main", "setitimer"); TIMER_ERR_MSG ; } @@ -99,7 +96,7 @@ void check_expired() /* called by sigalrm_callback or got_packet. this is the engine to search expired connections (delete only 1) */ { - register struct CONN * prev_conn; + register struct CONN_DESC * prev_conn; time_t now; now = time(NULL); @@ -107,7 +104,7 @@ void check_expired() prev_conn = first_conn; while ( CURRENT_CONN ) { - if (now - CURRENT_CONN->lasttime > flags.timeout) { + if (now - CURRENT_CONN->lastincar->lasttime > flags.timeout) { status_switch( prev_conn, EXPIRED ); prev_conn = first_conn; continue; |