]> sjero.net Git - dccp2tcp/blobdiff - dccp2tcp.c
Option to only show Ack Vectors/SACKS with loss
[dccp2tcp] / dccp2tcp.c
index a3d7218bc90b31b72fd2981174ddd71e81307914..0ed79a50fed94afe1ef42c6929ea5701ace9d266 100644 (file)
@@ -1,7 +1,7 @@
 /******************************************************************************
 Author: Samuel Jero
 
-Date: 1/2011
+Date: 5/2011
 
 Description: Program to convert a DCCP flow to a TCP flow for DCCP analysis via
                tcptrace.
@@ -23,7 +23,7 @@ int yellow=0; /*tcptrace yellow line as currently acked packet*/
 int green=0;   /*tcptrace green line as currently acked packet*/
 int sack=0;            /*add TCP SACKS*/
 
-pcap_t*                in;                     /*libpcap input file discriptor*/
+pcap_t*                        in;                     /*libpcap input file discriptor*/
 pcap_dumper_t  *out;   /*libpcap output file discriptor*/
 struct seq_num *s1;    /*sequence number structure for side one of connection*/
 struct seq_num *s2;    /*sequence number structure for side two of connection*/
@@ -33,10 +33,10 @@ struct seq_num      *s2;    /*sequence number structure for side two of connection*/
 void PcapSavePacket(struct pcap_pkthdr *h, u_char *data);
 void process_packets();
 void handle_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes);
-void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata);
+int convert_packet(struct pcap_pkthdr *h, u_char **nptr, int *nlength, const u_char **optr, int *length);
 unsigned int interp_ack_vect(u_char* hdr);
 u_int32_t initialize_seq(struct seq_num **seq, __be16 source, __be32 initial);
-u_int32_t add_new_seq(struct seq_num *seq, __be32 num, int ack);
+u_int32_t add_new_seq(struct seq_num *seq, __be32 num, int size, enum dccp_pkt_type type);
 u_int32_t convert_ack(struct seq_num *seq, __be32 num);
 int acked_packet_size(struct seq_num *seq, __be32 num);
 void ack_vect2sack(struct seq_num *seq, struct tcphdr *tcph, u_char* tcpopts, u_char* dccphdr, __be32 dccpack);
@@ -80,7 +80,7 @@ int main(int argc, char *argv[])
                                green=1;
                        }
                        if(argv[i][1]=='s' && strlen(argv[i])==2){ /*sack option*/
-                               sack=1;
+                               sack++;
                        }
                }
        }
@@ -139,24 +139,36 @@ return 0;
 /*call back function for pcap_loop--do basic packet handling*/
 void handle_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
 {
-       u_char *ndata;
-       struct pcap_pkthdr nh;
+       u_char                          *ndata;
+       u_char                          *nptr;
+       int                                     length;
+       int                                     nlength;
+       struct pcap_pkthdr      nh;
+       int                                     link_type;
+
+       /*Determine the link type for this packet*/
+       link_type=pcap_datalink(in);
 
        /*create new libpcap header*/
        memcpy(&nh, h, sizeof(struct pcap_pkthdr));
+       length=h->caplen;
+       nlength=MAX_PACKET;
 
        /*create buffer for new packet*/
-       ndata=malloc(MAX_PACKET);
+       nptr=ndata=malloc(MAX_PACKET);
        if(ndata==NULL){
                dbgprintf(0,"Error: Couldn't allocate Memory\n");
                exit(1);
        }
 
        /*make sure the packet is all zero*/
-       memset(ndata, 0, MAX_PACKET);   
+       memset(nptr, 0, MAX_PACKET);
        
        /*do all the fancy conversions*/
-       convert_packet(&nh, bytes, ndata);
+       if(!do_encap(link_type, &nh, &nptr, &nlength, &bytes, &length)){
+               free(ndata);
+               return;
+       }
 
        /*save packet*/
        pcap_dump(user,&nh, ndata);
@@ -167,32 +179,20 @@ return;
 
 
 /*do all the dccp to tcp conversions*/
-void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
+int convert_packet(struct pcap_pkthdr *h, u_char **nptr, int *nlength, const u_char **optr, int *length)
 {      
-       u_char* ncur=ndata;
-       const u_char* ocur=odata;
-       int length=h->caplen;
-       struct iphdr *iph;
+       u_char* ncur=*nptr;
+       const u_char* ocur=*optr;
        struct tcphdr *tcph;
        struct dccp_hdr *dccph;
        struct dccp_hdr_ext *dccphex;
        struct dccp_hdr_ack_bits *dccphack;
        int datalength;
+       int     len=0;
        const u_char* pd;
        u_char* npd;
        u_char* tcpopt;
 
-       /*copy ethernet and ip headers over*/
-       memcpy(ncur, ocur, sizeof(struct ether_header)+sizeof(struct iphdr) );
-       ocur+=sizeof(struct ether_header)+ sizeof(struct iphdr);
-       ncur+=sizeof(struct ether_header) +sizeof(struct iphdr);
-       length-=sizeof(struct ether_header) +sizeof(struct iphdr);
-
-       /*set ip to indicate that tcp is next protocol*/
-       iph= (struct iphdr *) (ncur - sizeof(struct iphdr));
-       iph->protocol=6;
-       iph->check=htonl(0);
-
        /*cast header pointers*/
        tcph=(struct tcphdr*)ncur;
        tcpopt=ncur+ sizeof(struct tcphdr);
@@ -203,8 +203,8 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
        dbgprintf(2,"Sequence Number: %llu\n", (unsigned long long)(((unsigned long)ntohs(dccph->dccph_seq)<<32) + ntohl(dccphex->dccph_seq_low)));
 
        /*determine data length*/
-       datalength=ntohs(iph->tot_len) - sizeof(struct iphdr) - dccph->dccph_doff*4;
-       pd=odata + sizeof(struct ether_header)+sizeof(struct iphdr) + dccph->dccph_doff*4;
+       datalength=*length - dccph->dccph_doff*4;
+       pd=*optr + dccph->dccph_doff*4;
 
        /*set tcp standard features*/
        tcph->source=dccph->dccph_sport;
@@ -218,61 +218,61 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
                tcph->window=htons(30000);
        }
 
+       /*Only accept the first connection*/
+       if(s1 && s2 && dccph->dccph_sport!=s1->addr && dccph->dccph_dport!=s1->addr){
+               return 0;
+       }
+
        /*make changes by packet type*/
        if(dccph->dccph_type==DCCP_PKT_REQUEST){//DCCP REQUEST -->TCP SYN
                dbgprintf(2,"Packet Type: Request\n");
+               if(!s1){
+                       if(yellow){
+                               tcph->window=htons(0);
+                       }
+                       tcph->ack_seq=htonl(0);
+                       tcph->seq=htonl(initialize_seq(&s1, dccph->dccph_sport, ntohl(dccphex->dccph_seq_low)));
+                       tcph->syn=1;
+                       tcph->ack=0;
+                       tcph->fin=0;
+                       tcph->rst=0;
 
-               if(yellow){
-                       tcph->window=htons(0);
-               }
-               tcph->ack_seq=htonl(0);
-               tcph->seq=htonl(initialize_seq(&s1, dccph->dccph_sport, ntohl(dccphex->dccph_seq_low)));
-               tcph->syn=1;
-               tcph->ack=0;
-               tcph->fin=0;
-               tcph->rst=0;
+                       /* add Sack-permitted option, if relevant*/
+                       if(sack){
+                               *tcpopt=4;
+                               tcpopt++;
+                               *tcpopt=2;
+                               tcph->doff++;
+                       }
 
-               /* add Sack-permitted option, if relevant*/
-               if(sack){
-                       *tcpopt=4;
-                       tcpopt++;
-                       *tcpopt=2;
-                       tcph->doff++;
+                       /*calculate length*/
+                       len=tcph->doff*4;
                }
-
-               /*set libpcap header lengths*/
-               h->len=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4;
-               h->caplen=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4;
-
-               /*set length in ip header*/
-               iph->tot_len=htons(sizeof(struct iphdr) + tcph->doff*4);
        }
 
        if(dccph->dccph_type==DCCP_PKT_RESPONSE){//DCCP RESPONSE-->TCP SYN,ACK
                dbgprintf(2,"Packet Type: Response\n");
-               tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)));
-               if(yellow){
-                       tcph->window=htons(0);
-               }
-               tcph->seq=htonl(initialize_seq(&s2, dccph->dccph_sport, ntohl(dccphex->dccph_seq_low)));
-               tcph->syn=1;
-               tcph->ack=1;
-               tcph->fin=0;
-               tcph->rst=0;
-
-               /* add Sack-permitted option, if relevant*/
-               if(sack){
-                       *tcpopt=4;
-                       *(tcpopt+1)=2;
-                       tcph->doff++;
-               }
+               if(s1 && !s2){
+                       tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)));
+                       if(yellow){
+                               tcph->window=htons(0);
+                       }
+                       tcph->seq=htonl(initialize_seq(&s2, dccph->dccph_sport, ntohl(dccphex->dccph_seq_low)));
+                       tcph->syn=1;
+                       tcph->ack=1;
+                       tcph->fin=0;
+                       tcph->rst=0;
 
-               /*set libpcap header lengths*/
-               h->len=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4;
-               h->caplen=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4;
+                       /* add Sack-permitted option, if relevant*/
+                       if(sack){
+                               *tcpopt=4;
+                               *(tcpopt+1)=2;
+                               tcph->doff++;
+                       }
 
-               /*set length in ip header*/
-               iph->tot_len=htons(sizeof(struct iphdr) + tcph->doff*4);
+                       /*calculate length*/
+                       len=tcph->doff*4;
+               }
        }
 
        if(dccph->dccph_type==DCCP_PKT_DATA){//DCCP DATA----Never seen in packet capture
@@ -282,31 +282,36 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
 
        if(dccph->dccph_type==DCCP_PKT_DATAACK){//DCCP DATAACK-->TCP ACK with data
                dbgprintf(2,"Packet Type: DataAck\n");
-               if(s1 && dccph->dccph_sport==s1->addr){ //determine which side of connection is sending this packet
+               if(s1 && s2 && dccph->dccph_sport==s1->addr){ //determine which side of connection is sending this packet
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),datalength));
+                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),datalength, dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s2, ntohl(dccphack->dccph_ack_nr_low)));
                        }
                        if(sack){
-                               ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
-               }else{
+               }else if(s1 && s2 && dccph->dccph_sport==s2->addr){
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),datalength));
+                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),datalength,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s1, ntohl(dccphack->dccph_ack_nr_low)));
                        }
                        if(sack){
-                               ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+
+                               }
                        }
                }
 
@@ -316,26 +321,22 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
                tcph->rst=0;
 
                /*copy data*/
-               npd=ndata+ sizeof(struct ether_header)+sizeof(struct iphdr)+ tcph->doff*4;
+               npd=*nptr + tcph->doff*4;
                memcpy(npd, pd, datalength);
 
-               /*set libpcap header lengths*/
-               h->len=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4 + datalength;
-               h->caplen=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4 + datalength;
-
-               /*set length in ip header*/
-               iph->tot_len=htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + datalength);  
+               /*calculate length*/
+               len= tcph->doff*4 + datalength;
        }
 
        if(dccph->dccph_type==DCCP_PKT_ACK){ //DCCP ACK -->TCP ACK with no data
                dbgprintf(2,"Packet Type: Ack\n");
-               if(s1 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
+               if(s1 && s2 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),1));
+                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),1,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*1400);
                                if(-interp_ack_vect((u_char*)dccph)*1400 > 65535){
@@ -343,15 +344,17 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
                                }
                        }
                        if(sack){
-                               ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
-               }else{
+               }else if(s1 && s2 && dccph->dccph_sport==s2->addr){
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),1));
+                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),1,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*1400);
                                if(-interp_ack_vect((u_char*)dccph)*1400 > 65535){
@@ -359,7 +362,9 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
                                }
                        }
                        if(sack){
-                               ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
                }
 
@@ -368,12 +373,8 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
                tcph->fin=0;
                tcph->rst=0;
 
-               /*set libpcap header lengths*/
-               h->len=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4+ 1;
-               h->caplen=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4+ 1;
-
-               /*set length in ip header*/
-               iph->tot_len=htons(sizeof(struct iphdr) + tcph->doff*4+ 1);
+               /*calculate length*/
+               len=tcph->doff*4 + 1;
        }
 
        if(dccph->dccph_type==DCCP_PKT_CLOSEREQ){//DCCP CLOSEREQ----Never seen in packet capture
@@ -383,31 +384,35 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
 
        if(dccph->dccph_type==DCCP_PKT_CLOSE){//DCCP CLOSE-->TCP FIN,ACK
                dbgprintf(2,"Packet Type: Close\n");
-               if(s1 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
+               if(s1 && s2 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),1));
+                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),1,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s2, ntohl(dccphack->dccph_ack_nr_low)));
                        }
                        if(sack){
-                               ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
-               }else{
+               }else if(s1 && s2 && dccph->dccph_sport==s2->addr){
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),1));
+                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),1,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s1, ntohl(dccphack->dccph_ack_nr_low)));
                        }
                        if(sack){
-                               ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
                }
 
@@ -416,41 +421,41 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
                tcph->fin=1;
                tcph->rst=0;
 
-               /*set libpcap header lengths*/
-               h->len=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4;
-               h->caplen=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4;
-
-               /*set length in ip header*/
-               iph->tot_len=htons(sizeof(struct iphdr) + tcph->doff*4);
+               /*calculate length*/
+               len=tcph->doff*4;
        }
 
        if(dccph->dccph_type==DCCP_PKT_RESET){//DCCP RESET-->TCP FIN,ACK (only seen at end of connection as CLOSE ACK)
                dbgprintf(2,"Packet Type: Reset\n");
-               if(s1 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
+               if(s1 && s2 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),1));
+                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),1,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s2, ntohl(dccphack->dccph_ack_nr_low)));
                        }
                        if(sack){
-                               ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
-               }else{
+               }else if(s1 && s2 && dccph->dccph_sport==s2->addr){
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),1));
+                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),1,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s1, ntohl(dccphack->dccph_ack_nr_low)));
                        }
                        if(sack){
-                               ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
                }
 
@@ -459,45 +464,45 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
                tcph->fin=1;
                tcph->rst=0;
 
-               /*set libpcap header lengths*/
-               h->len=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4;
-               h->caplen=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4;
-
-               /*set length in ip header*/
-               iph->tot_len=htons(sizeof(struct iphdr) + tcph->doff*4);
+               /*calculate length*/
+               len=tcph->doff*4;
        }
 
        if(dccph->dccph_type==DCCP_PKT_SYNC){//DCCP SYNC
                dbgprintf(2,"Packet Type: Sync\n");
-               if(s1 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
+               if(s1 && s2 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),0));
+                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),0,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s2, ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->window=htons(0);
                        }
                        if(sack){
-                               ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
-               }else{
+               }else if(s1 && s2 && dccph->dccph_sport==s2->addr){
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),0));
+                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),0,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s1, ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->window=htons(0);
                        }
                        if(sack){
-                               ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
                }
 
@@ -506,45 +511,45 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
                tcph->fin=0;
                tcph->rst=0;
 
-               /*set libpcap header lengths*/
-               h->len=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4+ 0;
-               h->caplen=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4+ 0;
-
-               /*set length in ip header*/
-               iph->tot_len=htons(sizeof(struct iphdr) + tcph->doff*4+0);
+               /*calculate length*/
+               len=tcph->doff*4;
        }
 
        if(dccph->dccph_type==DCCP_PKT_SYNCACK){//DCCP SYNACK
                dbgprintf(2,"Packet Type: SyncAck\n");
-               if(s1 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
+               if(s1 && s2 && dccph->dccph_sport==s1->addr){//determine which side of connection is sending this packet
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),0));
+                       tcph->seq=htonl(add_new_seq(s1, ntohl(dccphex->dccph_seq_low),0,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s2, ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->window=htons(0);
                        }
                        if(sack){
-                               ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s2, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low));
+                               }
                        }
-               }else{
+               }else if(s1 && s2 && dccph->dccph_sport==s2->addr){
                        if(green){
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->ack_seq=htonl(convert_ack(s1,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
                        }
-                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),0));
+                       tcph->seq=htonl(add_new_seq(s2, ntohl(dccphex->dccph_seq_low),0,dccph->dccph_type));
                        if(yellow){
                                tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(s1, ntohl(dccphack->dccph_ack_nr_low)));
                        }else{
                                tcph->window=htons(0);
                        }
                        if(sack){
-                               ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               if(sack!=2 || interp_ack_vect((u_char*)dccph)){
+                                       ack_vect2sack(s1, tcph, tcpopt, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
+                               }
                        }
                }
 
@@ -553,23 +558,22 @@ void convert_packet(struct pcap_pkthdr *h, const u_char *odata, u_char *ndata)
                tcph->fin=0;
                tcph->rst=0;
 
-               /*set libpcap header lengths*/
-               h->len=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4+0;
-               h->caplen=sizeof(struct ether_header) + sizeof(struct iphdr) + tcph->doff*4+0;
-
-               /*set length in ip header*/
-               iph->tot_len=htons(sizeof(struct iphdr) + tcph->doff*4+0);
+               /*calculate length*/
+               len=tcph->doff*4;
        }
 
        if(dccph->dccph_type==DCCP_PKT_INVALID){//DCCP INVALID----Never seen in packet capture
                dbgprintf(0,"Invalid DCCP Packet!!\n");
-               exit(1);
+               return 0;
        }
-return;
+
+       *nlength=len;
+return 1;
 }
 
 
-/*Parse Ack Vector Options*/
+/*Parse Ack Vector Options
+ * Returns the Number of packets since last recorded loss*/
 unsigned int interp_ack_vect(u_char* hdr)
 {
        int hdrlen=((struct dccp_hdr*)hdr)->dccph_doff*4;
@@ -655,14 +659,14 @@ u_int32_t initialize_seq(struct seq_num **seq, __be16 source, __be32 initial)
        /*add first sequence number*/
        (*seq)->table[0].old=initial;
        (*seq)->table[0].new=initial;
-       (*seq)->table[0].size=1; /* size is actually zero; this is a convient way to get
-                                   these packets counted in the sequence number space*/
+       (*seq)->table[0].type=DCCP_PKT_REQUEST;
+       (*seq)->table[0].size=0;
 return initial;
 }
 
 
 /*Convert Sequence Numbers*/
-u_int32_t add_new_seq(struct seq_num *seq, __be32 num, int size)
+u_int32_t add_new_seq(struct seq_num *seq, __be32 num, int size, enum dccp_pkt_type type)
 {
        int prev;
        if(seq==NULL){
@@ -678,14 +682,29 @@ u_int32_t add_new_seq(struct seq_num *seq, __be32 num, int size)
                seq->table[seq->cur].old=seq->table[prev].old+1;
                seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
                seq->table[seq->cur].size=size;
+               seq->table[seq->cur].type=type;
        }
 
        prev=seq->cur;
        seq->cur=(seq->cur+1)%(seq->size);/*find next available table slot*/
        seq->table[seq->cur].old=num;
-       seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
        seq->table[seq->cur].size=size;
-return seq->table[seq->cur].new;
+       seq->table[seq->cur].type=type;
+       if(seq->table[prev].type==DCCP_PKT_REQUEST || seq->table[prev].type==DCCP_PKT_RESPONSE){
+               seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
+               seq->table[seq->cur].size=1;
+               return seq->table[prev].new + seq->table[prev].size+1;
+       }
+       if(type==DCCP_PKT_DATA || type==DCCP_PKT_DATAACK || type==DCCP_PKT_ACK){
+               seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
+               return seq->table[seq->cur].new+1;
+       }
+       if(type==DCCP_PKT_SYNC || type==DCCP_PKT_SYNCACK){
+               seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
+               return seq->table[seq->cur].new;
+       }
+       seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
+return seq->table[seq->cur].new +1;
 }
 
 
@@ -700,7 +719,7 @@ u_int32_t convert_ack(struct seq_num *seq, __be32 num)
        /*loop through table looking for the DCCP ack number*/
        for(int i=0; i < seq->size; i++){
                if(seq->table[i].old==num){
-                       return  seq->table[i].new +1; /*TCP acks the sequence number plus 1*/
+                       return  seq->table[i].new + seq->table[i].size + 1; /*TCP acks the sequence number plus 1*/
                }
        }