summaryrefslogtreecommitdiffstats
path: root/network_cmds/ecnprobe/history.c
blob: e19b0ba531dfcf94c7b67a72b4bfb8a61229c392 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* 
 Copyright (c) 2000  
 International Computer Science Institute
 All rights reserved.

 This file may contain software code originally developed for the
 Sting project. The Sting software carries the following copyright:

 Copyright (c) 1998, 1999
 Stefan Savage and the University of Washington.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. All advertising materials mentioning features or use of this software
    must display the following acknowledgment:
      This product includes software developed by ACIRI, the AT&T
      Center for Internet Research at ICSI (the International Computer
      Science Institute). This product may also include software developed
      by Stefan Savage at the University of Washington.  
 4. The names of ACIRI, ICSI, Stefan Savage and University of Washington
    may not be used to endorse or promote products derived from this software
    without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY ICSI AND CONTRIBUTORS ``AS IS'' AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED.  IN NO EVENT SHALL ICSI OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "base.h"
#include "inet.h"
#include "session.h"
#include "support.h"
#include "history.h"

extern struct TcpSession session;
struct History history[MAXHSZ]; 

void StorePacket (struct IPPacket *p) {

  uint32 src, dst, seq, ack ;
  uint16 sport, dport, win, urp, datalen, optlen; 
  uint16 ip_optlen;
  uint8 flags;
  
  ReadIPPacket(p, 
	       &src, &dst, 
	       &sport, &dport, 
	       &seq, 
	       &ack, 
	       &flags, 
	       &win,
	       &urp, 
	       &datalen, 
	       &ip_optlen, 
	       &optlen); 

  if (src == session.src) {
    history[session.hsz].type = SENT; 
  } else {
    history[session.hsz].type = RCVD ;
  }

  history[session.hsz].timestamp = GetTime () - session.epochTime; 
  history[session.hsz].seqno = seq; 
  history[session.hsz].nextbyte = seq + datalen; 
  history[session.hsz].ackno = ack ; 
  history[session.hsz].fin = (flags & TCPFLAGS_FIN) ? 1 : 0;
  history[session.hsz].syn = (flags & TCPFLAGS_SYN) ? 1 : 0;
  history[session.hsz].rst = (flags & TCPFLAGS_RST) ? 1 : 0;
  history[session.hsz].psh = (flags & TCPFLAGS_PSH) ? 1 : 0;
  history[session.hsz].ack = (flags & TCPFLAGS_ACK) ? 1 : 0;
  history[session.hsz].urg = (flags & TCPFLAGS_URG) ? 1 : 0;
  history[session.hsz].ecn_echo = (flags & TCPFLAGS_ECN_ECHO) ? 1:0;
  history[session.hsz].cwr = (flags & TCPFLAGS_CWR) ? 1 : 0;
  history[session.hsz].ip_optlen = ip_optlen;
  history[session.hsz].optlen = optlen ;

  /* Grab IP Options from Ip Header - New */
  if (ip_optlen > 0) {
    if ((history[session.hsz].ip_opt = calloc(sizeof(uint8), ip_optlen)) == NULL) {
      printf("StorePacket Error: Could not allocate history memory\nRETURN CODE: %d\n", ERR_MEM_ALLOC);
      Quit (ERR_MEM_ALLOC); 
    }
    memcpy(history[session.hsz].ip_opt, (char *)p->ip + sizeof(struct IpHeader), ip_optlen);
  }


  /* Grab TCP options from TCP Header */
  if (optlen > 0) {
    if ((history[session.hsz].opt = calloc(sizeof(uint8), optlen)) == NULL) {
      Quit (ERR_MEM_ALLOC); 
    }

    memcpy(history[session.hsz].opt, (char *)p->tcp + sizeof(struct TcpHeader), optlen);
  }

  history[session.hsz].dlen = datalen; 
  
  if ((history[session.hsz].data = calloc(sizeof(uint8), datalen)) == NULL) {
    Quit (ERR_MEM_ALLOC); 
  }

  /* Copy data bytes */
  memcpy(history[session.hsz].data, 
	 (char *)p->tcp + sizeof(struct TcpHeader) + optlen, 
	 datalen);

  session.hsz++;

  if (session.hsz >= MAXHSZ) {
    Quit(TOO_MANY_PKTS); 
  }

}

int reordered(struct IPPacket *p) {

  int i;
  int count = 0;	
  double ts = -99999;

  /*
   * This might be either an unwanted packet drop, or just a reordering. 
   * Test: 
   *  If we have not sent three acks for this packet
   *  AND the gap between this packet and previous one is "small" (i.e. not a timeout)
   *  then its a reordering, and not a retransmission. 
   */
  
  /* count the number of (dup) ACKs sent */
  for (i = 0; i < session.hsz; i++) {
    if ((history[i].type == SENT) && 
	(history[i].ack)) {
      if (history[i].ackno == history[session.hsz - 1].seqno) 
	count += 1; 
    }
  }

  if (count > 0) {

    session.num_dup_acks += count - 1;

    switch (count) {
    case 1: /* no dup acks */
      session.num_pkts_0_dup_acks += 1;
      break;

    case 2: /* 1 dup acks */
      session.num_pkts_1_dup_acks += 1;
      break;
      
    case 3: /* 2 dup acks */
      session.num_pkts_2_dup_acks += 1;
      break;
      
    case 4: /* 3 dup acks */
      session.num_pkts_3_dup_acks += 1;
      break;
    
    default:
      session.num_pkts_4_or_more_dup_acks += 1;
      break;
    }
  }

  /* 3 dup acks? => Fast retransmission */
  if (count > 3) {
    printf("Fast retransmit...\n");
    return 3; 
  }

  /* Compute elapsed time between this packet and the previously RCVD packet */
  for (i = (session.hsz - 2); i >= 0; i--) {
    if ((history[i].type == RCVD) && (history[i].dlen > 0)) {
      ts = history[i].timestamp; 
      break; 
    }
  }

  if ((history[session.hsz - 1].timestamp - ts) > RTT_TO_MULT * (session.rtt + PLOTDIFF)) {
    printf ("RTO ===> %f %f\n", history[session.hsz - 1].timestamp, ts);
    return 2;
  }

  printf ("#### Acks %d\n", count);
  printf ("#### reordering detected\n");
  session.num_reordered++;
  
  return 1;

}