File: tcp.c

package info (click to toggle)
ipgrab 0.5-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 268 kB
  • ctags: 169
  • sloc: sh: 1,507; ansic: 1,234; makefile: 56
file content (293 lines) | stat: -rw-r--r-- 5,841 bytes parent folder | download | duplicates (2)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**************************************************************************** 
** File: tcp.c
**
** Author: Mike Borella
**
** Comments: Dump TCP header information.  TCP options section lifted from 
** tcpdump.
**
*****************************************************************************/

#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "config.h"
#include "tcp.h"

#define EXTRACT_16BITS(p) ((u_short) ntohs (*(u_short *)(p)))
#define EXTRACT_32BITS(p) ((u_int32_t) ntohl (*(u_int *)(p)))

/*
 * TCP options
 */

#ifndef TCPOPT_WSCALE
#define TCPOPT_WSCALE           3       /* window scale factor (rfc1072) */
#endif
#ifndef TCPOPT_ECHO
#define TCPOPT_ECHO             6       /* echo (rfc1072) */
#endif
#ifndef TCPOPT_ECHOREPLY
#define TCPOPT_ECHOREPLY        7       /* echo (rfc1072) */
#endif
#ifndef TCPOPT_TIMESTAMP
#define TCPOPT_TIMESTAMP        8       /* timestamps (rfc1323) */
#endif

extern u_char *packet_end;
extern struct arg_t *my_args;

/*----------------------------------------------------------------------------
**
** dump_tcp()
**
** Parse TCP header and dump fields
**
**----------------------------------------------------------------------------
*/

void dump_tcp(u_char *bp, int length)
{
  TCPHdr *tp;
  u_char flags;
  int hlen, total_hlen;
  u_short sport, dport, win, urp;
  u_int seq, ack;
  char *tcpport_string(u_short);
  void dump_payload(u_char *, u_int);

  /*
   * Overlay TCP header
   */

  tp = (TCPHdr *) bp;
  
  /*
   * Dump TCP header announcement
   */
  
  printf("----------------------------------------------------------\n");
  printf("                        TCP Header\n");
  printf("----------------------------------------------------------\n");
  
  if (length < sizeof(TCPHdr)) 
    {
      printf("Truncated TCP header: %d bytes\n", length);
      return;
    }

  /*
   * Grab ports and some fields
   */

  sport = ntohs(tp->th_sport);
  dport = ntohs(tp->th_dport);
  hlen = tp->th_off * 4;
  seq = ntohl(tp->th_seq);
  ack = ntohl(tp->th_ack);
  win = ntohs(tp->th_win);
  urp = ntohs(tp->th_urp);
  flags = tp->th_flags;

  /*
   * Dump TCP header info
   */

  if (!my_args->t)
    {
      printf("Source port:            %d", sport);
      if (sport < 1024) 
	printf(" (%s)\n", tcpport_string(sport));
      else 
	printf("\n");
      
      printf("Destination port:       %d", dport);
      if (dport < 1024) 
	printf(" (%s)\n", tcpport_string(dport));
      else 
	printf("\n");
      
      printf("Sequence number:        %u\n", seq);
      printf("Acknowledgement number: %u\n", ack);
      printf("Header length:          %d\n", hlen);
      printf("Reserved bits:          %d\n", tp->th_x2);
      
      printf("Flags:                  ");
      if (flags & TH_SYN) printf("SYN ");
      if (flags & TH_FIN) printf("FIN ");
      if (flags & TH_RST) printf("RST ");
      if (flags & TH_PUSH) printf("PSH ");
      if (flags & TH_ACK) printf("ACK ");
      if (flags & TH_URG) printf("URG ");
      printf("\n");
      
      printf("Window size:            %d\n", win);
      printf("Checksum:               %d\n", ntohs(tp->th_sum));
      printf("Urgent pointer:         %d\n", urp);
      printf("Options:                ");
    }


  /*
   * Check header length
   */

  if (hlen > length) 
    {
      printf("none\nBad header length\n");
      return;
    }

  /*
   * Header length and payload length bookkeeping
   */

  length -= hlen;
  total_hlen = hlen;
  
  /*
   * Handle any options.
   */
  if ((hlen -= sizeof(TCPHdr)) > 0) 
    {
      u_char *cp;
      int i, opt, len, datalen;
      
      cp = (u_char *) tp + sizeof(TCPHdr);
      while (hlen > 0) 
	{
	  /* 
	   * Check for zero length options
	   */

	  opt = *cp++;
	  if (opt == TCPOPT_EOL || opt == TCPOPT_NOP) 
	    len = 1;
	  else 
	    {
	      len = *cp++;  /* total including type, len */
	      if (len < 2 || len > hlen)
		{
		  printf("Bad option\n");
		  break;
		}
	      
	      /*
	       * account for length byte 
	       */

	      hlen --;
	      length --;
	      
	    } /* else */

	  /* 
	   * account for type byte 
	   */
	  
	  hlen --;
	  length --;

	  /*
	   * Handle the rest of the options
	   */

	  datalen = 0;
	  switch (opt) 
	    {	      
	    case TCPOPT_MAXSEG:
	      datalen = 2;
	      if (!my_args->t)
		{
		  printf("Maximum segment size = ");
		  printf("%u\n", EXTRACT_16BITS(cp));
		}
	      break;
	      
	    case TCPOPT_EOL:
	      if (!my_args->t)
		{
		  printf("End of options list\n");
		}
	      break;
	      
	    case TCPOPT_NOP:
	      if (!my_args->t)
		{
		  printf("No op\n");
		}
	      break;
	      
	    case TCPOPT_WSCALE:
	      datalen = 1;
	      if (!my_args->t)
		{
		  printf("Window scale = ");
		  printf("%u\n", *cp);
		}
	      break;

	    case TCPOPT_ECHO:
	      datalen = 4;
	      if (!my_args->t)
		{
		  printf("Echo = ");
		  printf("%u\n", EXTRACT_32BITS(cp));
		}
	      break;

	    case TCPOPT_ECHOREPLY:
	      datalen = 4;
	      if (!my_args->t)
		{
		  printf("Echo reply = ");
		  printf("%u\n", EXTRACT_32BITS(cp));
		}
	      break;
	      
	    case TCPOPT_TIMESTAMP:
	      datalen = 8;
	      if (!my_args->t)
		{
		  printf("Timestamp = ");
		  printf("%u", EXTRACT_32BITS(cp));
		  printf(" %u", EXTRACT_32BITS(cp + 4));
		}
	      break;

	    default:
	      datalen = len - 2;
	      if (!my_args->t)
		{
		  printf("Option %d:", opt);
		  for (i = 0; i < datalen; ++i) 
		    printf("%02x\n", cp[i]);
		}
	      break;
	    }

	  /* 
	   * Account for data printed 
	   */
           
	  cp += datalen;
	  hlen -= datalen;
        }
    }
  else
    {
      if (!my_args->t)
	printf("none\n");
    }


  /*
   * print payload if there is one
   */

  if (my_args->p && length > 0) 
    dump_payload((u_char *) bp + total_hlen, length);

  return;
  
}