- /*set default values*/
- seq->cur=0;
- seq->size=TBL_SZ;
-
- /*allocate table*/
- seq->table=(struct tbl*)malloc(sizeof(struct tbl)*TBL_SZ);
- if(seq->table==NULL){
- dbgprintf(0,"Can't Allocate Memory!\n");
- exit(1);
- }
-
- /*add first sequence number*/
- seq->table[0].old=initial;
- seq->table[0].new=initial;
- seq->table[0].type=DCCP_PKT_REQUEST;
- seq->table[0].size=0;
- update_state(seq,OPEN);
-return initial;
-}
-
-
-/*Convert Sequence Numbers*/
-u_int32_t add_new_seq(struct host *seq, __be32 num, int size, enum dccp_pkt_type type)
-{
- int prev;
- if(seq==NULL){
- dbgprintf(0,"ERROR NULL POINTER!\n");
- exit(1);
- }
-
- if(seq->table==NULL){
- dbgprintf(1, "Warning: Connection uninitialized\n");
- return initialize_seq(seq, num);
- }
-
- /*account for missing packets*/
- if(num - seq->table[seq->cur].old +1 >=100){
- dbgprintf(1,"Missing more than 100 packets!\n");
- }
- while(seq->table[seq->cur].old +1 < num && seq->table[seq->cur].old +1 > 0){
- prev=seq->cur;
- if(num - seq->table[seq->cur].old +1 <100){
- dbgprintf(1,"Missing Packet: %X\n",seq->table[prev].new+1);
- }
- seq->cur=(seq->cur+1)%(seq->size);/*find next available table slot*/
- 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].size=size;
- 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;
-}
-
-
-/*Convert Ack Numbers*/
-u_int32_t convert_ack(struct host *seq, __be32 num)
-{
- if(seq==NULL){
- dbgprintf(0,"ERROR NULL POINTER!\n");
- exit(1);
- }
-
- if(seq->table==NULL){
- dbgprintf(1, "Warning: Connection uninitialized\n");
- initialize_seq(seq, 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 + seq->table[i].size + 1; /*TCP acks the sequence number plus 1*/
- }
- }
-
- dbgprintf(1, "Error: Address Not Found! looking for: %X\n", num);
-return 0;
-}
-
-
-/* Get size of packet being acked*/
-int acked_packet_size(struct host *seq, __be32 num)
-{
- if(seq==NULL){
- dbgprintf(0,"ERROR NULL POINTER!\n");
- exit(1);
- }
-
- if(seq->table==NULL){
- dbgprintf(1, "Warning: Connection uninitialized\n");
- initialize_seq(seq, 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].size;
- }
- }
-
- dbgprintf(1, "Error: Address Not Found! looking for: %X\n", num);
-return 0;
-}
-
-
-/*Ack Vector to SACK Option*/
-void ack_vect2sack(struct host *seq, struct tcphdr *tcph, u_char* tcpopts, u_char* dccphdr, __be32 dccpack)
-{
- int hdrlen=((struct dccp_hdr*)dccphdr)->dccph_doff*4;
- int optlen;
- int len;
- int tmp;
- __be32 bp;
- u_char* temp;
- u_char* opt;
- u_char* cur;
- u_char* tlen;
- u_int32_t bL=0;
- u_int32_t bR;
- u_int32_t* pL;
- u_int32_t* pR;
- int num_blocks;
- int cont;
- int isopt;
-
- /*setup pointer to DCCP options and determine how long the options are*/
- optlen=hdrlen-sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext) - sizeof(struct dccp_hdr_ack_bits);
- opt=dccphdr + sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext) + sizeof(struct dccp_hdr_ack_bits);
-
- /*setup tcp pointers*/
- num_blocks=4;
- *tcpopts=5;
- tlen=tcpopts+1;
- temp=tlen;
- temp++;
- pL=(u_int32_t*)temp;
- pR=pL+1;
-
- /*setup tcp control variables*/
- bp=dccpack;
- cont=0;
- *tlen=2;
- isopt=0;
-
- /*parse options*/
- while(optlen > 0){
-
- /*One byte options (no length)*/
- if(*opt< 32){
- optlen--;
- opt++;
- continue;
- }
-
- len=*(opt+1);
- if(len > optlen){
- dbgprintf(0, "Warning: Option would extend into packet data\n");
- break;
- }
-
- /*Ack Vector Option*/
- if(*opt==38 || *opt==39){
- tmp=len-2;
- cur=opt+2;
- /*loop through Vector*/
- while(tmp > 0){
- /*ack vector works BACKWARDS through time*/
-
- if((*cur & 0xC0)==0xC0 || (*cur & 0xC0)==0x40){ //lost packet
- if(cont){ /*end a SACK run, if one is started*/
- bR=convert_ack(seq, bp);
- cont=0;
- num_blocks--;
- *pR=htonl(bR);
- *pL=htonl(bL);
- tcph->doff+=2;
- *tlen+=8;
- pL=pR+1;
- pR=pL+1;
- }
- bp= bp - (*cur & 0x3F)- 1;
- }
-
- if((*cur & 0xC0)==0x00){ //received packet
- if(!cont){ /*if no SACK run and we can start another one, do so*/
- if(num_blocks>0){
- bL=convert_ack(seq, bp);
- isopt=1;
- cont=1;
-
- }
- }
- bp = bp -(*cur & 0x3F)- 1;
- }
- tmp--;
- cur++;
- }
- }
-
- optlen-=len;
- opt+=len;
- }
-
- /*if we are in the middle of a SACK run, close it*/
- if(cont){
- bR=convert_ack(seq, bp);
- *pR=htonl(bR);
- *pL=htonl(bL);
- tcph->doff+=2;
- *tlen+=8;
- cont=0;
- }
-
- /*adjust length if the option is actually added*/
- if(isopt){
- tcph->doff+=1;
- }
-return;