From 0bd47deab8307bfc680011cd0f70019d49238d94 Mon Sep 17 00:00:00 2001 From: Samuel Jero Date: Tue, 5 Feb 2013 22:05:26 -0500 Subject: [PATCH] Allow Pretty printing IP Addresses in connection information. Bugfix encapsulation system. --- dccp2tcp.c | 96 ++++++++++++++++++++++++++++++++++++++---------------- dccp2tcp.h | 2 ++ encap.c | 80 ++++++++++++++++++++++++++++++++++++++++++--- encap.h | 8 +++++ 4 files changed, 154 insertions(+), 32 deletions(-) diff --git a/dccp2tcp.c b/dccp2tcp.c index 6697681..afe462f 100644 --- a/dccp2tcp.c +++ b/dccp2tcp.c @@ -53,10 +53,12 @@ int handle_reset(struct packet* new, const struct const_packet* old, struct hcon int handle_sync(struct packet* new, const struct const_packet* old, struct hcon* h1, struct hcon* h2); int handle_syncack(struct packet* new, const struct const_packet* old, struct hcon* h1, struct hcon* h2); int handle_data(struct packet* new, const struct const_packet* old, struct hcon* h1, struct hcon* h2); -int parse_options(const u_char* opt_start, int len, struct hcon* A, struct hcon* B); -int process_feature(const u_char* feat, int len, int confirm, int L, struct hcon* A, struct hcon* B); -void ack_vect2sack(struct hcon *seq, struct tcphdr *tcph, u_char* tcpopts, u_char* dccphdr, - d_seq_num dccpack, struct hcon* o_hcn); +int parse_options(const u_char* opt_start, int len, + const struct const_packet* pkt, struct hcon* A, struct hcon* B); +int process_feature(const u_char* feat, int len, int confirm, int L, + const struct const_packet* pkt, struct hcon* A, struct hcon* B); +void ack_vect2sack(struct hcon *seq, struct tcphdr *tcph, + u_char* tcpopts, u_char* dccphdr, d_seq_num dccpack, struct hcon* o_hcn); void version(); void usage(); @@ -220,6 +222,8 @@ int convert_packet(struct packet *new, const struct const_packet* old) struct dccp_hdr_ext *dccphex; struct hcon *h1=NULL; struct hcon *h2=NULL; + char buf1[100]; + char buf2[100]; /*Safety checks*/ if(!new || !old || !new->data || !old->data || !new->h || !old->h){ @@ -258,8 +262,14 @@ int convert_packet(struct packet *new, const struct const_packet* old) return 0; } if(h1->state==IGNORE || h2->state==IGNORE){ - dbgprintf(2, "Ignoring packet between %i and %i\n", - ntohs(dccph->dccph_sport), ntohs(dccph->dccph_dport)); + if(old->print_id){ + dbgprintf(2,"Ignoring packet between %s:%i and %s:%i\n", + old->print_id(buf1,100,old->src_id,old->id_len),ntohs(dccph->dccph_sport), + old->print_id(buf2,100,old->dest_id,old->id_len), ntohs(dccph->dccph_dport)); + }else{ + dbgprintf(2,"Ignoring packet between %i and %i\n", + ntohs(dccph->dccph_sport), ntohs(dccph->dccph_dport)); + } return 0; } @@ -385,7 +395,7 @@ int handle_request(struct packet* new, const struct const_packet* old, struct hc /*Process DCCP Options*/ dccpopt=old->data + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext)+sizeof(struct dccp_hdr_request); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr)-sizeof(struct dccp_hdr_ext)-sizeof(struct dccp_hdr_request); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -445,7 +455,7 @@ int handle_response(struct packet* new, const struct const_packet* old, struct h sizeof(struct dccp_hdr_ack_bits) + sizeof(struct dccp_hdr_request); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits) - sizeof(struct dccp_hdr_request); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -509,7 +519,7 @@ int handle_dataack(struct packet* new, const struct const_packet* old, struct hc /*Process DCCP Options*/ dccpopt=old->data + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -566,7 +576,7 @@ int handle_ack(struct packet* new, const struct const_packet* old, struct hcon* /*Process DCCP Options*/ dccpopt=old->data + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -623,7 +633,7 @@ int handle_closereq(struct packet* new, const struct const_packet* old, struct h /*Process DCCP Options*/ dccpopt=old->data + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -677,7 +687,7 @@ int handle_close(struct packet* new, const struct const_packet* old, struct hcon /*Process DCCP Options*/ dccpopt=old->data + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -732,7 +742,7 @@ int handle_reset(struct packet* new, const struct const_packet* old, struct hcon /*Process DCCP Options*/ dccpopt=old->data + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -789,7 +799,7 @@ int handle_sync(struct packet* new, const struct const_packet* old, struct hcon* /*Process DCCP Options*/ dccpopt=old->data + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -845,7 +855,7 @@ int handle_syncack(struct packet* new, const struct const_packet* old, struct hc /*Process DCCP Options*/ dccpopt=old->data + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -906,7 +916,7 @@ int handle_data(struct packet* new, const struct const_packet* old, struct hcon* /*Process DCCP Options*/ dccpopt=old->data + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext); optlen=dccph->dccph_doff*4 - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext); - if(!parse_options(dccpopt,optlen,h1,h2)){ + if(!parse_options(dccpopt,optlen,old,h1,h2)){ return 0; } @@ -927,11 +937,14 @@ int handle_data(struct packet* new, const struct const_packet* old, struct hcon* return 1; } -int parse_options(const u_char* opt_start, int len, struct hcon* A, struct hcon* B) +int parse_options(const u_char* opt_start, int len, const struct const_packet* pkt, + struct hcon* A, struct hcon* B) { int optlen; int length; const u_char* opt; + char buf1[100]; + char buf2[100]; /*setup pointer to DCCP options and determine how long the options are*/ optlen=len; @@ -961,8 +974,14 @@ int parse_options(const u_char* opt_start, int len, struct hcon* A, struct hcon* if(*opt==38 || *opt==39){ if(B->type==UNKNOWN){ B->type=CCID2; - dbgprintf(1,"Half-connection from port %i to %i probably using CCID 2\n", - ntohs(B->port),ntohs(A->port)); + if(pkt->print_id){ + dbgprintf(1,"Half-connection from %s:%i to %s:%i probably using CCID 2\n", + pkt->print_id(buf1,100,pkt->dest_id,pkt->id_len),ntohs(B->port), + pkt->print_id(buf2,100,pkt->src_id,pkt->id_len), ntohs(A->port)); + }else{ + dbgprintf(1,"Half-connection from port %i to %i probably using CCID 2\n", + ntohs(B->port),ntohs(A->port)); + } } } @@ -970,33 +989,39 @@ int parse_options(const u_char* opt_start, int len, struct hcon* A, struct hcon* if(*opt==37){ if(B->type==UNKNOWN){ B->type=CCID3; - dbgprintf(1,"Half-connection from port %i to %i probably using CCID 3\n", - ntohs(B->port),ntohs(A->port)); + if(pkt->print_id){ + dbgprintf(1,"Half-connection from %s:%i to %s:%i probably using CCID 3\n", + pkt->print_id(buf1,100,pkt->dest_id,pkt->id_len),ntohs(B->port), + pkt->print_id(buf2,100,pkt->src_id,pkt->id_len), ntohs(A->port)); + }else{ + dbgprintf(1,"Half-connection from port %i to %i probably using CCID 3\n", + ntohs(B->port),ntohs(A->port)); + } } } /*Feature negotation*/ if(*opt==32){ /*Change L*/ - if(!process_feature(opt+2,length-2,FALSE,TRUE,A,B)){ + if(!process_feature(opt+2,length-2,FALSE,TRUE,pkt,A,B)){ return 0; } } if(*opt==33){ /*Confirm L*/ - if(!process_feature(opt+2,length-2,TRUE,TRUE,A,B)){ + if(!process_feature(opt+2,length-2,TRUE,TRUE,pkt,A,B)){ return 0; } } if(*opt==34){ /*Change R*/ - if(!process_feature(opt+2,length-2,FALSE,FALSE,A,B)){ + if(!process_feature(opt+2,length-2,FALSE,FALSE,pkt,A,B)){ return 0; } } if(*opt==35){ /*Confirm R*/ - if(!process_feature(opt+2,length-2,TRUE,FALSE,A,B)){ + if(!process_feature(opt+2,length-2,TRUE,FALSE,pkt,A,B)){ return 0; } } @@ -1008,10 +1033,13 @@ int parse_options(const u_char* opt_start, int len, struct hcon* A, struct hcon* return 1; } -int process_feature(const u_char* feat, int len, int confirm, int L, struct hcon* A, struct hcon* B) +int process_feature(const u_char* feat, int len, int confirm, int L, + const struct const_packet* pkt, struct hcon* A, struct hcon* B) { const u_char* val; int ccid; + char buf1[100]; + char buf2[100]; val=feat+1; @@ -1032,12 +1060,24 @@ int process_feature(const u_char* feat, int len, int confirm, int L, struct hcon } if(L==TRUE){ B->type=ccid; - dbgprintf(1,"Half-connection from port %i to %i using CCID %i\n", + if(pkt->print_id){ + dbgprintf(1,"Half-connection from %s:%i to %s:%i using CCID %i\n", + pkt->print_id(buf1,100,pkt->dest_id,pkt->id_len),ntohs(B->port), + pkt->print_id(buf2,100,pkt->src_id,pkt->id_len), ntohs(A->port), *val); + }else{ + dbgprintf(1,"Half-connection from port %i to %i using CCID %i\n", ntohs(B->port),ntohs(A->port), *val); + } }else{ A->type=ccid; - dbgprintf(1,"Half-connection from port %i to %i using CCID %i\n", + if(pkt->print_id){ + dbgprintf(1,"Half-connection from %s:%i to %s:%i using CCID %i\n", + pkt->print_id(buf1,100,pkt->src_id,pkt->id_len),ntohs(A->port), + pkt->print_id(buf2,100,pkt->dest_id,pkt->id_len), ntohs(B->port), *val); + }else{ + dbgprintf(1,"Half-connection from port %i to %i using CCID %i\n", ntohs(A->port),ntohs(B->port), *val); + } } } break; diff --git a/dccp2tcp.h b/dccp2tcp.h index 23b5af0..d8615c2 100644 --- a/dccp2tcp.h +++ b/dccp2tcp.h @@ -70,6 +70,7 @@ struct packet{ int id_len; /*Length of IDs*/ u_char *src_id; /*Source ID of packet*/ u_char *dest_id;/*Destination ID of packet*/ + char* (*print_id)(char* buf, int len, u_char* id, int id_len); /*Function to print ID*/ }; /*Constant Packet structure*/ @@ -80,6 +81,7 @@ struct const_packet{ int id_len; /*Length of IDs*/ u_char *src_id; /*Source ID of packet*/ u_char *dest_id;/*Destination ID of packet*/ + char* (*print_id)(char* buf, int len, u_char* id, int id_len); /*Function to print ID*/ }; /*Connection states*/ diff --git a/encap.c b/encap.c index 4cda535..6f79307 100644 --- a/encap.c +++ b/encap.c @@ -25,6 +25,7 @@ Date: 11/2012 #include "checksums.h" #include #include +#include /*Encapsulation start point and link layer selector*/ int do_encap(int link, struct packet *new, const struct const_packet *old) @@ -92,6 +93,14 @@ int ethernet_encap(struct packet *new, const struct const_packet *old) nnew.length= new->length - sizeof(struct ether_header); nnew.h=new->h; nold.h=old->h; + nnew.print_id=NULL; + nnew.dest_id=NULL; + nnew.src_id=NULL; + nnew.id_len=0; + nold.print_id=NULL; + nold.dest_id=NULL; + nold.src_id=NULL; + nold.id_len=0; /*Select Next Protocol*/ switch(ntohs(ethh->ether_type)){ @@ -146,6 +155,10 @@ int ipv6_encap(struct packet *new, const struct const_packet *old) nnew.length= new->length - sizeof(struct ip6_hdr); nnew.h=new->h; nold.h=old->h; + nnew.print_id=print_ipv6; + nold.print_id=print_ipv6; + nnew.id_len=16; + nold.id_len=16; /*Confirm that this is IPv6*/ if((ntohl(iph->ip6_ctlun.ip6_un1.ip6_un1_flow) & (0xF0000000)) == (60000000)){ @@ -157,15 +170,19 @@ int ipv6_encap(struct packet *new, const struct const_packet *old) switch(iph->ip6_ctlun.ip6_un1.ip6_un1_nxt){ case 33: /*DCCP*/ - nnew.id_len=16; nnew.src_id=malloc(nnew.id_len); nnew.dest_id=malloc(nnew.id_len); - if(nnew.src_id==NULL||nnew.dest_id==NULL){ + nold.src_id=malloc(nold.id_len); + nold.dest_id=malloc(nold.id_len); + if(nnew.src_id==NULL||nnew.dest_id==NULL || + nold.src_id==NULL||nold.dest_id==NULL){ dbgprintf(0,"Error: Couldn't allocate Memory\n"); exit(1); } memcpy(nnew.src_id,&iph->ip6_src,nnew.id_len); memcpy(nnew.dest_id,&iph->ip6_dst,nnew.id_len); + memcpy(nold.src_id,&iph->ip6_src,nold.id_len); + memcpy(nold.dest_id,&iph->ip6_dst,nold.id_len); if(!convert_packet(&nnew, &nold)){ return 0; } @@ -194,6 +211,8 @@ int ipv6_encap(struct packet *new, const struct const_packet *old) /*Cleanup*/ free(nnew.dest_id); free(nnew.src_id); + free(nold.dest_id); + free(nold.src_id); return 1; } @@ -227,6 +246,10 @@ int ipv4_encap(struct packet *new, const struct const_packet *old) nnew.length= new->length -iph->ihl*4; nnew.h=new->h; nold.h=old->h; + nnew.print_id=print_ipv4; + nold.print_id=print_ipv4; + nnew.id_len=4; + nold.id_len=4; /*Confirm that this is IPv4*/ if(iph->version!=4){ @@ -238,15 +261,19 @@ int ipv4_encap(struct packet *new, const struct const_packet *old) switch(iph->protocol){ case 33: /*DCCP*/ - nnew.id_len=4; nnew.src_id=malloc(nnew.id_len); nnew.dest_id=malloc(nnew.id_len); - if(nnew.src_id==NULL||nnew.dest_id==NULL){ + nold.src_id=malloc(nold.id_len); + nold.dest_id=malloc(nold.id_len); + if(nnew.src_id==NULL||nnew.dest_id==NULL|| + nold.src_id==NULL||nold.dest_id==NULL){ dbgprintf(0,"Error: Couldn't allocate Memory\n"); exit(1); } memcpy(nnew.src_id,&iph->saddr,nnew.id_len); memcpy(nnew.dest_id,&iph->daddr,nnew.id_len); + memcpy(nold.src_id,&iph->saddr,nold.id_len); + memcpy(nold.dest_id,&iph->daddr,nold.id_len); if(!convert_packet(&nnew, &nold)){ return 0; } @@ -280,6 +307,8 @@ int ipv4_encap(struct packet *new, const struct const_packet *old) /*Cleanup*/ free(nnew.src_id); free(nnew.dest_id); + free(nold.src_id); + free(nold.dest_id); return 1; } @@ -313,6 +342,14 @@ int linux_cooked_encap(struct packet *new, const struct const_packet *old) nnew.length= new->length- sizeof(struct sll_header); nnew.h=new->h; nold.h=old->h; + nnew.print_id=NULL; + nnew.dest_id=NULL; + nnew.src_id=NULL; + nnew.id_len=0; + nold.print_id=NULL; + nold.dest_id=NULL; + nold.src_id=NULL; + nold.id_len=0; /*Confirm that this is SLL*/ if(ntohs(slh->sll_pkttype) > 4){ @@ -342,3 +379,38 @@ int linux_cooked_encap(struct packet *new, const struct const_packet *old) new->length=nnew.length + sizeof(struct sll_header); return 1; } + + +char *print_ipv6(char* buf, int len, u_char* id, int id_len) +{ + struct sockaddr_in6 sa; + + if(buf==NULL){ + return NULL; + } + + memcpy(&sa.sin6_addr, id, id_len); + sa.sin6_family=AF_INET6; + if(getnameinfo((struct sockaddr*)&sa, sizeof(struct sockaddr_in6), + buf, len, NULL,0,NI_NUMERICHOST)<0){ + return NULL; + } + return buf; +} + +char *print_ipv4(char* buf, int len, u_char* id, int id_len) +{ + struct sockaddr_in sa; + + if(buf==NULL){ + return NULL; + } + + memcpy(&sa.sin_addr, id, id_len); + sa.sin_family=AF_INET; + if(getnameinfo((struct sockaddr*)&sa, sizeof(struct sockaddr_in), + buf, len, NULL,0,NI_NUMERICHOST)<0){ + return NULL; + } + return buf; +} diff --git a/encap.h b/encap.h index 5628f79..890335a 100644 --- a/encap.h +++ b/encap.h @@ -52,6 +52,10 @@ Date: 11/2012 * need to set this field. Typically this would be an * IP address. * + * char* (*print_id)(char* buf, int len, u_char* id, int id_len): + * This is a function to pretty-print the destination or + * source ID to the given buffer. + * * struct const_packet *old: The Old packet. It contains the following fields. * * u_char* data: This is a pointer to the buffer containing the @@ -76,4 +80,8 @@ int linux_cooked_encap(struct packet *new, const struct const_packet *old); int ipv4_encap(struct packet *new, const struct const_packet *old); int ipv6_encap(struct packet *new, const struct const_packet *old); +/*Standard Print Functions*/ +char* print_ipv6(char* buf, int len, u_char* id, int id_len); +char* print_ipv4(char* buf, int len, u_char* id, int id_len); + #endif /* ENCAP_H_ */ -- 2.39.2