From 1bbfe6080ec64a1d292d863935dc18fec3c6373f Mon Sep 17 00:00:00 2001 From: Samuel Jero Date: Tue, 15 Nov 2011 17:12:41 -0500 Subject: [PATCH] Multiple connection support segfault fixes --- connections.c | 21 +++++++++++---------- dccp2tcp.c | 11 +++++++++-- dccp2tcp.h | 2 +- encap.c | 6 ++++++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/connections.c b/connections.c index 511692f..b5eb112 100644 --- a/connections.c +++ b/connections.c @@ -9,7 +9,7 @@ Description: Functions for differentiating different DCCP connections. #include "dccp2tcp.h" /*Lookup a connection. If it doesn't exist, add a new connection and return it.*/ -int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, struct host *fwd, struct host *rev){ +int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, struct host **fwd, struct host **rev){ struct connection *ptr; /*Empty list*/ @@ -17,8 +17,8 @@ int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, str if(add_connection(src_id, dest_id, src_port, dest_port)==NULL){ return 1; } - fwd=&chead->A; - rev=&chead->B; + *fwd=&chead->A; + *rev=&chead->B; return 0; } @@ -26,13 +26,13 @@ int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, str ptr=chead; while(ptr!=NULL){ if(ptr->A.id==src_id && ptr->A.port==src_port && ptr->A.state!=CLOSE){ - fwd=&ptr->A; - rev=&ptr->B; + *fwd=&ptr->A; + *rev=&ptr->B; return 0; } if(ptr->B.id==src_id && ptr->B.port==src_port && ptr->B.state!=CLOSE){ - fwd=&ptr->B; - rev=&ptr->A; + *fwd=&ptr->B; + *rev=&ptr->A; return 0; } ptr=ptr->next; @@ -43,8 +43,8 @@ int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, str if(ptr==NULL){ return 1; } - fwd=&ptr->A; - rev=&ptr->B; + *fwd=&ptr->A; + *rev=&ptr->B; return 0; } @@ -54,7 +54,7 @@ struct connection *add_connection(uint32_t src_id, uint32_t dest_id, int src_por struct connection *prev; /*Allocate memory*/ - if(chead){ + if(chead==NULL){ ptr=chead=malloc(sizeof(struct connection)); }else{ ptr=chead; @@ -73,6 +73,7 @@ struct connection *add_connection(uint32_t src_id, uint32_t dest_id, int src_por /*Initialize*/ ptr->A.id=src_id; + ptr->A.port=src_port; ptr->A.state=INIT; ptr->B.id=dest_id; ptr->B.port=dest_port; diff --git a/dccp2tcp.c b/dccp2tcp.c index eb913bf..c8cc769 100644 --- a/dccp2tcp.c +++ b/dccp2tcp.c @@ -124,6 +124,7 @@ int main(int argc, char *argv[]) } /*process packets*/ + chead=NULL; u_char *user=(u_char*)out; pcap_loop(in, -1, handle_packet, user); @@ -198,6 +199,7 @@ int convert_packet(struct packet *new, const struct const_packet* old) /*Safety checks*/ if(!new || !old || !new->data || !old->data || !new->h || !old->h){ dbgprintf(0,"Error: Convert Packet Function given bad data!\n"); + exit(1); return 0; } if(old->length < sizeof(struct dccp_hdr) || new->length < sizeof(struct dccp_hdr)){ @@ -215,11 +217,11 @@ int convert_packet(struct packet *new, const struct const_packet* old) dbgprintf(2,"Sequence Number: %llu\n", (unsigned long long)(((unsigned long)ntohs(dccph->dccph_seq)<<32) + ntohl(dccphex->dccph_seq_low))); /*Get Hosts*/ - if(get_host(new->src_id, new->dest_id, dccph->dccph_sport, dccph->dccph_dport, h1, h2)){ + if(get_host(new->src_id, new->dest_id, dccph->dccph_sport, dccph->dccph_dport, &h1, &h2)){ dbgprintf(0,"Error: Can't Get Hosts!\n"); return 0; } - if(!h1 || !h2){ + if(h1==NULL || h2==NULL){ dbgprintf(0, "Error: Can't Get Hosts!\n"); return 0; } @@ -582,6 +584,11 @@ u_int32_t add_new_seq(struct host *seq, __be32 num, int size, enum dccp_pkt_type exit(1); } + if(seq->table==NULL){ + dbgprintf(1, "Warning: Connection initialization incorrect\n"); + return 0; + } + /*account for missing packets*/ while(seq->table[seq->cur].old +1 < num && seq->table[seq->cur].old +1 > 0){ prev=seq->cur; diff --git a/dccp2tcp.h b/dccp2tcp.h index 4b556cf..2a1dbfa 100644 --- a/dccp2tcp.h +++ b/dccp2tcp.h @@ -119,7 +119,7 @@ void dbgprintf(int level, const char *fmt, ...); int do_encap(int link, struct packet *new, const struct const_packet *old); /*Connection functions*/ -int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, struct host *fwd, struct host *rev); +int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, struct host **fwd, struct host **rev); struct connection *add_connection(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port); int update_state(struct host* hst, enum con_state st); diff --git a/encap.c b/encap.c index b932bda..beafa68 100644 --- a/encap.c +++ b/encap.c @@ -78,6 +78,8 @@ int ethernet_encap(struct packet *new, const struct const_packet *old) /*Select Next Protocol*/ switch(ntohs(ethh->ether_type)){ case ETHERTYPE_IP: + nnew.h=new->h; + nold.h=old->h; if(!ipv4_encap(&nnew, &nold)){ return 0; } @@ -132,6 +134,8 @@ int ipv4_encap(struct packet *new, const struct const_packet *old) switch(iph->protocol){ case 0x21: /*DCCP*/ + nnew.h=new->h; + nold.h=old->h; nnew.src_id=iph->saddr; nnew.dest_id=iph->daddr; if(!convert_packet(&nnew, &nold)){ @@ -200,6 +204,8 @@ int linux_cooked_encap(struct packet *new, const struct const_packet *old) /*Select Next Protocol*/ switch(ntohs(slh->sll_protocol)){ case ETHERTYPE_IP: + nnew.h=new->h; + nold.h=old->h; if(!ipv4_encap(&nnew, &nold)){ return 0; } -- 2.39.2