]> sjero.net Git - dccp2tcp/blob - dccp2tcp.c
Ignore directory of CCID 2 captures
[dccp2tcp] / dccp2tcp.c
1 /******************************************************************************
2 Author: Samuel Jero
3
4 Date: 7/2011
5
6 Description: Program to convert a DCCP flow to a TCP flow for DCCP analysis via
7                 tcptrace.
8
9 Notes:
10         1)CCID2 ONLY
11         2)DCCP MUST use 48 bit sequence numbers
12         3)Checksums are not computed (they are zeroed)
13         4)Only implements those packet types normally used in a session
14         5)DCCP Ack packets show up as TCP packets containing one byte
15         6)Very little error checking of packet headers
16 ******************************************************************************/
17 #include "dccp2tcp.h"
18
19
20 int debug=0;    /*set to 1 to turn on debugging information*/
21 int yellow=0;   /*tcptrace yellow line as currently acked packet*/
22 int green=0;    /*tcptrace green line as currently acked packet*/
23 int sack=0;             /*add TCP SACKS*/
24
25 pcap_t*                 in;                     /*libpcap input file discriptor*/
26 pcap_dumper_t   *out;           /*libpcap output file discriptor*/
27 struct connection *chead;       /*connection list*/
28
29
30
31 void PcapSavePacket(struct pcap_pkthdr *h, u_char *data);
32 void process_packets();
33 void handle_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes);
34 int convert_packet(struct packet *new, const struct const_packet* old);
35 unsigned int interp_ack_vect(u_char* hdr);
36 u_int32_t initialize_seq(struct host *seq, __be16 source, __be32 initial);
37 u_int32_t add_new_seq(struct host *seq, __be32 num, int size, enum dccp_pkt_type type);
38 u_int32_t convert_ack(struct host *seq, __be32 num);
39 int acked_packet_size(struct host *seq, __be32 num);
40 void ack_vect2sack(struct host *seq, struct tcphdr *tcph, u_char* tcpopts, u_char* dccphdr, __be32 dccpack);
41
42
43 /*Parse commandline options and open files*/
44 int main(int argc, char *argv[])
45 {
46         char ebuf[200];
47         char *erbuffer=ebuf;
48         char *dfile=NULL;
49         char *tfile=NULL;
50
51         /*parse commandline options*/
52         if(argc<3 || argc > 9){
53                 dbgprintf(0, "Usage: dccp2tcp dccp_file tcp_file [-d] [-y] [-g] [-s]\n");
54                 exit(1);
55         }
56
57         /*loop through commandline options*/
58         for(int i=1; i < argc; i++){
59                 if(argv[i][0]!='-'){
60                         if(dfile==NULL){ /*assign first non-dash argument to the dccp file*/
61                                 dfile=argv[i];
62                         }else{
63                                 if(tfile==NULL){
64                                         tfile=argv[i]; /*assign second non-dash argument to the dccp file*/
65                                 }else{
66                                         dbgprintf(0,"Usage: dccp2tcp dccp_file tcp_file [-d] [-y] [-g] [-s]\n");
67                                         exit(1);
68                                 }
69                         }
70                 }else{
71                         if(argv[i][1]=='d' && strlen(argv[i])==2){ /*debug option*/
72                                 debug++;
73                         }
74                         if(argv[i][1]=='y' && strlen(argv[i])==2){ /*yellow option*/
75                                 yellow=1;
76                         }
77                         if(argv[i][1]=='g' && strlen(argv[i])==2){ /*green option*/
78                                 green=1;
79                         }
80                         if(argv[i][1]=='s' && strlen(argv[i])==2){ /*sack option*/
81                                 sack++;
82                         }
83                 }
84         }
85         
86         if(dfile==NULL || tfile==NULL){
87                 dbgprintf(0,"Usage: dccp2tcp dccp_file tcp_file [-d] [-y] [-g] [-s]\n");
88                 exit(1);
89         }
90
91         /*all options validated*/
92
93         if(debug){
94                 dbgprintf(1,"Debug On\n");
95                 if(green){
96                         dbgprintf(1,"Tcptrace green line at highest acknowledgment\n");
97                 }else{
98                         dbgprintf(1,"Tcptrace green line at highest acknowledged acknowledgment\n");
99                 }
100                 if(yellow){
101                         dbgprintf(1,"Tcptrace yellow line at highest acknowledgment\n");
102                 }else{
103                         dbgprintf(1,"Tcptrace yellow line window value (a made up number)\n");
104                 }
105                 if(sack){
106                         dbgprintf(1,"Adding TCP SACKS\n");
107                 }
108                 dbgprintf(1,"Input file: %s\n", dfile);
109                 dbgprintf(1,"Output file: %s\n", tfile);
110         }
111
112         /*attempt to open input file*/
113         in=pcap_open_offline(dfile, erbuffer);
114         if(in==NULL){
115                 dbgprintf(0,"Error opening input file\n");
116                 exit(1);
117         }
118
119         /*attempt to open output file*/
120         out=pcap_dump_open(in,tfile);
121         if(out==NULL){
122                 dbgprintf(0,"Error opening output file\n");
123                 exit(1);
124         }
125
126         /*process packets*/
127         chead=NULL;
128         u_char *user=(u_char*)out;
129         pcap_loop(in, -1, handle_packet, user); 
130         
131         /*close files*/
132         pcap_close(in);
133         pcap_dump_close(out);
134 return 0;
135 }
136
137
138 /*call back function for pcap_loop--do basic packet handling*/
139 void handle_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
140 {
141         u_char                          *ndata;
142         struct pcap_pkthdr      nh;
143         int                                     link_type;
144         struct packet           new;
145         struct const_packet     old;
146
147         /*Determine the link type for this packet*/
148         link_type=pcap_datalink(in);
149
150         /*create new libpcap header*/
151         memcpy(&nh, h, sizeof(struct pcap_pkthdr));
152
153         /*Setup packet structs*/
154         old.h=h;
155         old.length=h->caplen;
156         old.data=bytes;
157         new.h=&nh;
158         new.length=MAX_PACKET;
159
160         /*create buffer for new packet*/
161         new.data=ndata=malloc(MAX_PACKET);
162         if(ndata==NULL){
163                 dbgprintf(0,"Error: Couldn't allocate Memory\n");
164                 exit(1);
165         }
166
167         /*make sure the packet is all zero*/
168         memset(new.data, 0, MAX_PACKET);
169         
170         /*do all the fancy conversions*/
171         if(!do_encap(link_type, &new, &old)){
172                 free(ndata);
173                 return;
174         }
175
176         /*save packet*/
177         pcap_dump(user,&nh, ndata);
178
179         free(ndata);
180 return;
181 }
182
183
184 /*do all the dccp to tcp conversions*/
185 int convert_packet(struct packet *new, const struct const_packet* old)
186 {       
187         struct tcphdr                           *tcph;
188         struct dccp_hdr                         *dccph;
189         struct dccp_hdr_ext             *dccphex;
190         struct dccp_hdr_ack_bits        *dccphack;
191         struct host                                     *h1=NULL;
192         struct host                                     *h2=NULL;
193         int                                             datalength;
194         int                                                     len=0;
195         const u_char*                           pd;
196         u_char*                                         npd;
197         u_char*                                         tcpopt;
198
199         /*Safety checks*/
200         if(!new || !old || !new->data || !old->data || !new->h || !old->h){
201                 dbgprintf(0,"Error:  Convert Packet Function given bad data!\n");
202                 exit(1);
203                 return 0;
204         }
205         if(old->length < sizeof(struct dccp_hdr) || new->length < sizeof(struct dccp_hdr)){
206                 dbgprintf(0, "Error: Convert Packet Function given packet of wrong size!\n");
207                 return 0;
208         }
209
210         /*cast header pointers*/
211         tcph=(struct tcphdr*)new->data;
212         dccph=(struct dccp_hdr*)old->data;
213         dccphex=(struct dccp_hdr_ext*)(old->data+sizeof(struct dccp_hdr));
214         dccphack=(struct dccp_hdr_ack_bits*)(old->data+ sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext));
215
216         dbgprintf(2,"Sequence Number: %llu\n", (unsigned long long)(((unsigned long)ntohs(dccph->dccph_seq)<<32) + ntohl(dccphex->dccph_seq_low)));
217
218         /*Get Hosts*/
219         if(get_host(new->src_id, new->dest_id, dccph->dccph_sport, dccph->dccph_dport, &h1, &h2)){
220                 dbgprintf(0,"Error: Can't Get Hosts!\n");
221                 return 0;
222         }
223         if(h1==NULL || h2==NULL){
224                 dbgprintf(0, "Error: Can't Get Hosts!\n");
225                 return 0;
226         }
227
228         /*determine data length*/
229         datalength=old->length - dccph->dccph_doff*4;
230         pd=old->data + dccph->dccph_doff*4;
231
232         /*set TCP standard features*/
233         tcph->source=dccph->dccph_sport;
234         tcph->dest=dccph->dccph_dport;
235         tcph->doff=5;
236         tcph->check=htonl(0);
237         tcph->urg_ptr=0;
238
239         /*Adjust TCP advertised window size*/
240         if(!yellow){
241                 tcph->window=htons(30000);
242         }
243
244         /*make changes by packet type*/
245         if(dccph->dccph_type==DCCP_PKT_REQUEST){//DCCP REQUEST -->TCP SYN
246                 dbgprintf(2,"Packet Type: Request\n");
247                 if(h1->state==INIT){
248                         if(yellow){
249                                 tcph->window=htons(0);
250                         }
251                         tcph->ack_seq=htonl(0);
252                         tcph->seq=htonl(initialize_seq(h1, dccph->dccph_sport, ntohl(dccphex->dccph_seq_low)));
253                         tcph->syn=1;
254                         tcph->ack=0;
255                         tcph->fin=0;
256                         tcph->rst=0;
257
258                         /* add Sack-permitted option, if relevant*/
259                         if(sack){
260                                 tcpopt=(u_char*)(new->data + tcph->doff*4);
261                                 *tcpopt=4;
262                                 tcpopt++;
263                                 *tcpopt=2;
264                                 tcph->doff++;
265                         }
266
267                         /*calculate length*/
268                         len=tcph->doff*4;
269                 }
270         }
271
272         if(dccph->dccph_type==DCCP_PKT_RESPONSE){//DCCP RESPONSE-->TCP SYN,ACK
273                 dbgprintf(2,"Packet Type: Response\n");
274                 if(h2->state==OPEN && h1->state==INIT){
275                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)));
276                         if(yellow){
277                                 tcph->window=htons(0);
278                         }
279                         tcph->seq=htonl(initialize_seq(h1, dccph->dccph_sport, ntohl(dccphex->dccph_seq_low)));
280                         tcph->syn=1;
281                         tcph->ack=1;
282                         tcph->fin=0;
283                         tcph->rst=0;
284
285                         /* add Sack-permitted option, if relevant*/
286                         if(sack){
287                                 tcpopt=(u_char*)(new->data + tcph->doff*4);
288                                 *tcpopt=4;
289                                 *(tcpopt+1)=2;
290                                 tcph->doff++;
291                         }
292
293                         /*calculate length*/
294                         len=tcph->doff*4;
295                 }
296         }
297
298         if(dccph->dccph_type==DCCP_PKT_DATA){//DCCP DATA----Never seen in packet capture
299                 dbgprintf(0,"DCCP Data packet not yet implemented\n");
300                 exit(1);
301         }
302
303         if(dccph->dccph_type==DCCP_PKT_DATAACK){//DCCP DATAACK-->TCP ACK with data
304                 dbgprintf(2,"Packet Type: DataAck\n");
305                 if(green){
306                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)));
307                 }else{
308                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
309                 }
310                 tcph->seq=htonl(add_new_seq(h1, ntohl(dccphex->dccph_seq_low),datalength, dccph->dccph_type));
311                 if(yellow){
312                         tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(h2, ntohl(dccphack->dccph_ack_nr_low)));
313                 }
314                 if(sack){
315                         if(sack!=2 || interp_ack_vect((u_char*)dccph)){
316                                 ack_vect2sack(h2, tcph, (u_char*)tcph + tcph->doff*4, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
317                         }
318                 }
319
320                 tcph->syn=0;
321                 tcph->ack=1;
322                 tcph->fin=0;
323                 tcph->rst=0;
324
325                 /*copy data*/
326                 npd=new->data + tcph->doff*4;
327                 memcpy(npd, pd, datalength);
328
329                 /*calculate length*/
330                 len= tcph->doff*4 + datalength;
331         }
332
333         if(dccph->dccph_type==DCCP_PKT_ACK){ //DCCP ACK -->TCP ACK with no data
334                 dbgprintf(2,"Packet Type: Ack\n");
335                 if(green){
336                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)));
337                 }else{
338                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
339                 }
340                 tcph->seq=htonl(add_new_seq(h1, ntohl(dccphex->dccph_seq_low),1,dccph->dccph_type));
341                 if(yellow){
342                         tcph->window=htons(-interp_ack_vect((u_char*)dccph)*1400);
343                         if(-interp_ack_vect((u_char*)dccph)*1400 > 65535){
344                                 printf("Note: TCP Window Overflow @ %d.%d\n", (int)old->h->ts.tv_sec, (int)old->h->ts.tv_usec);
345                         }
346                 }
347                 if(sack){
348                         if(sack!=2 || interp_ack_vect((u_char*)dccph)){
349                                 ack_vect2sack(h2, tcph, (u_char*)tcph + tcph->doff*4, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
350                         }
351                 }
352
353                 tcph->syn=0;
354                 tcph->ack=1;
355                 tcph->fin=0;
356                 tcph->rst=0;
357
358                 /*calculate length*/
359                 len=tcph->doff*4 + 1;
360         }
361
362         if(dccph->dccph_type==DCCP_PKT_CLOSEREQ){//DCCP CLOSEREQ----Never seen in packet capture
363                 dbgprintf(0,"DCCP CloseReq not yet implemented\n");
364                 exit(1);
365         }
366
367         if(dccph->dccph_type==DCCP_PKT_CLOSE){//DCCP CLOSE-->TCP FIN,ACK
368                 dbgprintf(2,"Packet Type: Close\n");
369                 update_state(h1,CLOSE);
370                 if(green){
371                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)));
372                 }else{
373                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
374                 }
375                 tcph->seq=htonl(add_new_seq(h1, ntohl(dccphex->dccph_seq_low),1,dccph->dccph_type));
376                 if(yellow){
377                         tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(h2, ntohl(dccphack->dccph_ack_nr_low)));
378                 }
379                 if(sack){
380                         if(sack!=2 || interp_ack_vect((u_char*)dccph)){
381                                 ack_vect2sack(h2, tcph, (u_char*)tcph + tcph->doff*4, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
382                         }
383                 }
384
385                 tcph->syn=0;
386                 tcph->ack=1;
387                 tcph->fin=1;
388                 tcph->rst=0;
389
390                 /*calculate length*/
391                 len=tcph->doff*4;
392         }
393
394         if(dccph->dccph_type==DCCP_PKT_RESET){//DCCP RESET-->TCP FIN,ACK (only seen at end of connection as CLOSE ACK)
395                 if(h2->state==CLOSE){
396                         update_state(h1,CLOSE);
397                 }
398                 dbgprintf(2,"Packet Type: Reset\n");
399                 if(green){
400                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)));
401                 }else{
402                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
403                 }
404                 tcph->seq=htonl(add_new_seq(h1, ntohl(dccphex->dccph_seq_low),1,dccph->dccph_type));
405                 if(yellow){
406                         tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(h2, ntohl(dccphack->dccph_ack_nr_low)));
407                 }
408                 if(sack){
409                         if(sack!=2 || interp_ack_vect((u_char*)dccph)){
410                                 ack_vect2sack(h2, tcph, (u_char*)tcph + tcph->doff*4, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
411                         }
412                 }
413
414                 tcph->syn=0;
415                 tcph->ack=1;
416                 tcph->fin=1;
417                 tcph->rst=0;
418
419                 /*calculate length*/
420                 len=tcph->doff*4;
421         }
422
423         if(dccph->dccph_type==DCCP_PKT_SYNC){//DCCP SYNC
424                 dbgprintf(2,"Packet Type: Sync\n");
425                 if(green){
426                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)));
427                 }else{
428                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
429                 }
430                 tcph->seq=htonl(add_new_seq(h1, ntohl(dccphex->dccph_seq_low),0,dccph->dccph_type));
431                 if(yellow){
432                         tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(h2, ntohl(dccphack->dccph_ack_nr_low)));
433                 }else{
434                         tcph->window=htons(0);
435                 }
436                 if(sack){
437                         if(sack!=2 || interp_ack_vect((u_char*)dccph)){
438                                 ack_vect2sack(h2, tcph, (u_char*)tcph + tcph->doff*4, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low) );
439                         }
440                 }
441
442                 tcph->syn=0;
443                 tcph->ack=1;
444                 tcph->fin=0;
445                 tcph->rst=0;
446
447                 /*calculate length*/
448                 len=tcph->doff*4;
449         }
450
451         if(dccph->dccph_type==DCCP_PKT_SYNCACK){//DCCP SYNACK
452                 dbgprintf(2,"Packet Type: SyncAck\n");
453                 if(green){
454                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)));
455                 }else{
456                         tcph->ack_seq=htonl(convert_ack(h2,ntohl(dccphack->dccph_ack_nr_low)+interp_ack_vect((u_char*)dccph)));
457                 }
458                 tcph->seq=htonl(add_new_seq(h1, ntohl(dccphex->dccph_seq_low),0,dccph->dccph_type));
459                 if(yellow){
460                         tcph->window=htons(-interp_ack_vect((u_char*)dccph)*acked_packet_size(h2, ntohl(dccphack->dccph_ack_nr_low)));
461                 }else{
462                         tcph->window=htons(0);
463                 }
464                 if(sack){
465                         if(sack!=2 || interp_ack_vect((u_char*)dccph)){
466                                 ack_vect2sack(h2, tcph, (u_char*)tcph + tcph->doff*4, (u_char*)dccph, ntohl(dccphack->dccph_ack_nr_low));
467                         }
468                 }
469
470                 tcph->syn=0;
471                 tcph->ack=1;
472                 tcph->fin=0;
473                 tcph->rst=0;
474
475                 /*calculate length*/
476                 len=tcph->doff*4;
477         }
478
479         if(dccph->dccph_type==DCCP_PKT_INVALID){//DCCP INVALID----Never seen in packet capture
480                 dbgprintf(0,"Invalid DCCP Packet!!\n");
481                 return 0;
482         }
483
484         new->length=len;
485 return 1;
486 }
487
488
489 /*Parse Ack Vector Options
490  * Returns the Number of packets since last recorded loss*/
491 unsigned int interp_ack_vect(u_char* hdr)
492 {
493         int hdrlen=((struct dccp_hdr*)hdr)->dccph_doff*4;
494         //struct dccp_hdr_ext* e=(struct dccp_hdr_ext*)hdr + sizeof(struct dccp_hdr);
495         int optlen;
496         int len;
497         int tmp;
498         int bp=0;
499         int additional=0;
500         u_char* opt;
501         u_char* cur;
502
503         /*setup pointer to DCCP options and determine how long the options are*/
504         optlen=hdrlen-sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits);
505         opt=hdr + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits);
506
507         /*parse options*/
508         while(optlen > 0){
509                 len=*(opt+1);
510
511                 /*One byte options (no length)*/
512                 if(*opt< 32){
513                         optlen--;
514                         opt++;
515                         continue;
516                 }
517
518                 /*Ack Vector Option*/
519                 if(*opt==38 || *opt==39){
520                         tmp=len-2;
521                         cur=opt+2;
522                         /*loop through Vector*/
523                         while(tmp > 0){
524                                 /*ack vector works BACKWARDS through time*/
525
526                                 /*keep track of total packets recieved and if
527                                 a packet is lost, subtract all packets received
528                                 after that*/
529                                 if((*cur & 0xC0)==0xC0 || (*cur & 0xC0)==0x40){ //lost packet
530                                         bp+=(*cur & 0x3F)+1;
531                                         additional= -bp;
532                                 }
533                                         
534                                 if((*cur & 0xC0)==0x00){ //received packet
535                                         bp+= (*cur & 0x3F)+1;
536                                 }
537
538                                 if(((*cur& 0xC0)!= 0xC0) && ((*cur& 0xC0)!= 0x00) && ((*cur& 0xC0)!= 0x40)){
539                                         dbgprintf(1, "Warning: Invalid Ack Vector!! (Linux will handle poorly!) -- %X\n", *cur);
540                                 }
541                                 tmp--;
542                                 cur++;
543                         }
544                 }
545                 
546                 optlen-=len;
547                 opt+=len;
548         }
549
550         dbgprintf(2,"Ack vector adding: %i\n", additional);
551 return additional;
552 }
553
554
555 /* Setup Sequence Number Structure*/
556 u_int32_t initialize_seq(struct host *seq, __be16 source, __be32 initial)
557 {
558         /*set default values*/
559         seq->cur=0;
560         seq->size=TBL_SZ;
561         
562         /*allocate table*/
563         seq->table=(struct tbl*)malloc(sizeof(struct tbl)*TBL_SZ);
564         if(seq->table==NULL){
565                 dbgprintf(0,"Can't Allocate Memory!\n");
566                 exit(1);
567         }
568
569         /*add first sequence number*/
570         seq->table[0].old=initial;
571         seq->table[0].new=initial;
572         seq->table[0].type=DCCP_PKT_REQUEST;
573         seq->table[0].size=0;
574         update_state(seq,OPEN);
575 return initial;
576 }
577
578
579 /*Convert Sequence Numbers*/
580 u_int32_t add_new_seq(struct host *seq, __be32 num, int size, enum dccp_pkt_type type)
581 {
582         int prev;
583         if(seq==NULL){
584                 dbgprintf(0,"ERROR NULL POINTER!\n");
585                 exit(1);
586         }
587         
588         if(seq->table==NULL){
589                 dbgprintf(1, "Warning: Connection uninitialized\n");
590                 return initialize_seq(seq, 0, num);
591         }
592
593         /*account for missing packets*/
594         while(seq->table[seq->cur].old +1 < num && seq->table[seq->cur].old +1 > 0){
595                 prev=seq->cur;
596                 dbgprintf(1,"Missing Packet: %X\n",seq->table[prev].new+1);
597                 seq->cur=(seq->cur+1)%(seq->size);/*find next available table slot*/
598                 seq->table[seq->cur].old=seq->table[prev].old+1;
599                 seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
600                 seq->table[seq->cur].size=size;
601                 seq->table[seq->cur].type=type;
602         }
603
604         prev=seq->cur;
605         seq->cur=(seq->cur+1)%(seq->size);/*find next available table slot*/
606         seq->table[seq->cur].old=num;
607         seq->table[seq->cur].size=size;
608         seq->table[seq->cur].type=type;
609         if(seq->table[prev].type==DCCP_PKT_REQUEST || seq->table[prev].type==DCCP_PKT_RESPONSE){
610                 seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
611                 seq->table[seq->cur].size=1;
612                 return seq->table[prev].new + seq->table[prev].size+1;
613         }
614         if(type==DCCP_PKT_DATA || type==DCCP_PKT_DATAACK || type==DCCP_PKT_ACK){
615                 seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
616                 return seq->table[seq->cur].new+1;
617         }
618         if(type==DCCP_PKT_SYNC || type==DCCP_PKT_SYNCACK){
619                 seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
620                 return seq->table[seq->cur].new;
621         }
622         seq->table[seq->cur].new=seq->table[prev].new + seq->table[prev].size;
623 return seq->table[seq->cur].new +1;
624 }
625
626
627 /*Convert Ack Numbers*/
628 u_int32_t convert_ack(struct host *seq, __be32 num)
629 {
630         if(seq==NULL){
631                 dbgprintf(0,"ERROR NULL POINTER!\n");
632                 exit(1);
633         }
634
635         if(seq->table==NULL){
636                 dbgprintf(1, "Warning: Connection uninitialized\n");
637                 initialize_seq(seq, 0, num);
638         }
639
640         /*loop through table looking for the DCCP ack number*/
641         for(int i=0; i < seq->size; i++){
642                 if(seq->table[i].old==num){
643                         return  seq->table[i].new + seq->table[i].size + 1; /*TCP acks the sequence number plus 1*/
644                 }
645         }
646         
647         dbgprintf(1, "Error: Address Not Found! looking for: %X\n", num);
648 return 0;
649 }
650
651
652 /* Get size of packet being acked*/
653 int acked_packet_size(struct host *seq, __be32 num)
654 {
655         if(seq==NULL){
656                 dbgprintf(0,"ERROR NULL POINTER!\n");
657                 exit(1);
658         }
659
660         if(seq->table==NULL){
661                 dbgprintf(1, "Warning: Connection uninitialized\n");
662                 initialize_seq(seq, 0, num);
663         }
664
665         /*loop through table looking for the DCCP ack number*/
666         for(int i=0; i < seq->size; i++){
667                 if(seq->table[i].old==num){
668                         return  seq->table[i].size;
669                 }
670         }
671         
672         dbgprintf(1, "Error: Address Not Found! looking for: %X\n", num);
673 return 0;
674 }
675
676
677 /*Ack Vector to SACK Option*/
678 void ack_vect2sack(struct host *seq, struct tcphdr *tcph, u_char* tcpopts, u_char* dccphdr, __be32 dccpack)
679 {
680         int hdrlen=((struct dccp_hdr*)dccphdr)->dccph_doff*4;
681         int optlen;
682         int len;
683         int tmp;
684         __be32 bp;
685         u_char* temp;
686         u_char* opt;
687         u_char* cur;
688         u_char* tlen;
689         u_int32_t bL=0;
690         u_int32_t bR;
691         u_int32_t* pL;
692         u_int32_t* pR;
693         int num_blocks;
694         int cont;
695         int isopt;
696         
697         /*setup pointer to DCCP options and determine how long the options are*/
698         optlen=hdrlen-sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits);
699         opt=dccphdr + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits);
700
701         /*setup tcp pointers*/
702         num_blocks=4;
703         *tcpopts=5;
704         tlen=tcpopts+1;
705         temp=tlen;
706         temp++;
707         pL=(u_int32_t*)temp;
708         pR=pL+1;
709
710         /*setup tcp control variables*/
711         bp=dccpack;
712         cont=0;
713         *tlen=2;
714         isopt=0;
715
716         /*parse options*/
717         while(optlen > 0){
718                 len=*(opt+1);
719
720                 /*One byte options (no length)*/
721                 if(*opt< 32){
722                         optlen--;
723                         opt++;
724                         continue;
725                 }
726
727                 /*Ack Vector Option*/
728                 if(*opt==38 || *opt==39){
729                         tmp=len-2;
730                         cur=opt+2;
731                         /*loop through Vector*/
732                         while(tmp > 0){
733                                 /*ack vector works BACKWARDS through time*/
734
735                                 if((*cur & 0xC0)==0xC0 || (*cur & 0xC0)==0x40){ //lost packet
736                                         if(cont){ /*end a SACK run, if one is started*/
737                                                 bR=convert_ack(seq, bp);
738                                                 cont=0;
739                                                 num_blocks--;
740                                                 *pR=htonl(bR);
741                                                 *pL=htonl(bL);
742                                                 tcph->doff+=2;
743                                                 *tlen+=8;
744                                                 pL=pR+1;
745                                                 pR=pL+1;                        
746                                         }
747                                         bp= bp - (*cur & 0x3F)- 1;
748                                 }
749                                         
750                                 if((*cur & 0xC0)==0x00){ //received packet
751                                         if(!cont){ /*if no SACK run and we can start another one, do so*/
752                                                 if(num_blocks>0){
753                                                         bL=convert_ack(seq, bp);
754                                                         isopt=1;
755                                                         cont=1;
756
757                                                 }
758                                         }
759                                         bp =  bp -(*cur & 0x3F)- 1;
760                                 }
761                                 tmp--;
762                                 cur++;
763                         }
764                 }
765                 
766                 optlen-=len;
767                 opt+=len;
768         }
769
770         /*if we are in the middle of a SACK run, close it*/
771         if(cont){
772                 bR=convert_ack(seq, bp);
773                 *pR=htonl(bR);
774                 *pL=htonl(bL);
775                 tcph->doff+=2;
776                 *tlen+=8;
777                 cont=0;
778         }
779
780         /*adjust length if the option is actually added*/
781         if(isopt){
782                 tcph->doff+=1;
783         }
784 return;
785 }
786
787 /*Debug Printf*/
788 void dbgprintf(int level, const char *fmt, ...)
789 {
790     va_list args;
791     if(debug>=level){
792         va_start(args, fmt);
793         vfprintf(stderr, fmt, args);
794         va_end(args);
795     }
796 }