Move the include of "snprintf.h" after a bunch of other includes, so
[obnox/wireshark/wip.git] / packet-tcp.c
1 /* packet-tcp.c
2  * Routines for TCP packet disassembly
3  *
4  * $Id: packet-tcp.c,v 1.17 1999/03/23 03:58:59 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
36 #endif
37
38 #include <stdio.h>
39 #include <glib.h>
40 #include "packet.h"
41 #include "resolv.h"
42 #include "follow.h"
43 #include "util.h"
44
45 #ifdef NEED_SNPRINTF_H
46 # ifdef HAVE_STDARG_H
47 #  include <stdarg.h>
48 # else
49 #  include <varargs.h>
50 # endif
51 # include "snprintf.h"
52 #endif
53
54 #ifndef __PACKET_IP_H__
55 #include "packet-ip.h"
56 #endif
57
58 extern FILE* data_out_file;
59 extern packet_info pi;
60
61 static gchar info_str[COL_MAX_LEN];
62 static int   info_len;
63
64 /* TCP Ports */
65
66 #define TCP_PORT_HTTP     80
67 #define TCP_PORT_PRINTER  515
68 #define TCP_ALT_PORT_HTTP 8080
69
70 /* TCP structs and definitions */
71
72 typedef struct _e_tcphdr {
73   guint16 th_sport;
74   guint16 th_dport;
75   guint32 th_seq;
76   guint32 th_ack;
77   guint8  th_off_x2; /* combines th_off and th_x2 */
78   guint8  th_flags;
79 #define TH_FIN  0x01
80 #define TH_SYN  0x02
81 #define TH_RST  0x04
82 #define TH_PUSH 0x08
83 #define TH_ACK  0x10
84 #define TH_URG  0x20
85   guint16 th_win;
86   guint16 th_sum;
87   guint16 th_urp;
88 } e_tcphdr;
89
90 /*
91  *      TCP option
92  */
93  
94 #define TCPOPT_NOP              1       /* Padding */
95 #define TCPOPT_EOL              0       /* End of options */
96 #define TCPOPT_MSS              2       /* Segment size negotiating */
97 #define TCPOPT_WINDOW           3       /* Window scaling */
98 #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
99 #define TCPOPT_SACK             5       /* SACK Block */
100 #define TCPOPT_ECHO             6
101 #define TCPOPT_ECHOREPLY        7
102 #define TCPOPT_TIMESTAMP        8       /* Better RTT estimations/PAWS */
103 #define TCPOPT_CC               11
104 #define TCPOPT_CCNEW            12
105 #define TCPOPT_CCECHO           13
106
107 /*
108  *     TCP option lengths
109  */
110
111 #define TCPOLEN_MSS            4
112 #define TCPOLEN_WINDOW         3
113 #define TCPOLEN_SACK_PERM      2
114 #define TCPOLEN_SACK_MIN       2
115 #define TCPOLEN_ECHO           6
116 #define TCPOLEN_ECHOREPLY      6
117 #define TCPOLEN_TIMESTAMP      10
118 #define TCPOLEN_CC             6
119 #define TCPOLEN_CCNEW          6
120 #define TCPOLEN_CCECHO         6
121
122 static void
123 tcp_info_append_uint(const char *abbrev, guint32 val) {
124   int add_len = 0;
125   
126   if (info_len > 0)
127   if(info_len > 0)
128     add_len = snprintf(&info_str[info_len], COL_MAX_LEN - info_len, " %s=%u",
129       abbrev, val);
130   if (add_len > 0)
131     info_len += add_len;
132 }
133
134 static void
135 dissect_tcpopt_maxseg(proto_tree *opt_tree, const char *name, const u_char *opd,
136     int offset, guint optlen)
137 {
138   proto_tree_add_item(opt_tree, offset,      optlen,
139     "%s: %u bytes", name, pntohs(opd));
140   tcp_info_append_uint("MSS", pntohs(opd));
141 }
142
143 static void
144 dissect_tcpopt_wscale(proto_tree *opt_tree, const char *name, const u_char *opd,
145     int offset, guint optlen)
146 {
147   proto_tree_add_item(opt_tree, offset,      optlen,
148     "%s: %u bytes", name, *opd);
149   tcp_info_append_uint("WS", *opd);
150 }
151
152 static void
153 dissect_tcpopt_sack(proto_tree *opt_tree, const char *name, const u_char *opd,
154     int offset, guint optlen)
155 {
156   proto_tree *field_tree = NULL;
157   proto_item *tf;
158   guint leftedge, rightedge;
159
160   tf = proto_tree_add_item(opt_tree, offset,      optlen, "%s:", name);
161   offset += 2;  /* skip past type and length */
162   optlen -= 2;  /* subtract size of type and length */
163   while (optlen > 0) {
164     if (field_tree == NULL) {
165       /* Haven't yet made a subtree out of this option.  Do so. */
166       field_tree = proto_tree_new();
167       proto_item_add_subtree(tf, field_tree, ETT_TCP_OPTION_SACK);
168     }
169     if (optlen < 4) {
170       proto_tree_add_item(field_tree, offset,      optlen,
171         "(suboption would go past end of option)");
172       break;
173     }
174     /* XXX - check whether it goes past end of packet */
175     leftedge = pntohl(opd);
176     opd += 4;
177     optlen -= 4;
178     if (optlen < 4) {
179       proto_tree_add_item(field_tree, offset,      optlen,
180         "(suboption would go past end of option)");
181       break;
182     }
183     /* XXX - check whether it goes past end of packet */
184     rightedge = pntohl(opd);
185     opd += 4;
186     optlen -= 4;
187     proto_tree_add_item(field_tree, offset,      8,
188         "left edge = %u, right edge = %u", leftedge, rightedge);
189     tcp_info_append_uint("SLE", leftedge);
190     tcp_info_append_uint("SRE", rightedge);
191     offset += 8;
192   }
193 }
194
195 static void
196 dissect_tcpopt_echo(proto_tree *opt_tree, const char *name, const u_char *opd,
197     int offset, guint optlen)
198 {
199   proto_tree_add_item(opt_tree, offset,      optlen,
200     "%s: %u", name, pntohl(opd));
201   tcp_info_append_uint("ECHO", pntohl(opd));
202 }
203
204 static void
205 dissect_tcpopt_timestamp(proto_tree *opt_tree, const char *name,
206     const u_char *opd, int offset, guint optlen)
207 {
208   proto_tree_add_item(opt_tree, offset,      optlen,
209     "%s: tsval %u, tsecr %u", name, pntohl(opd), pntohl(opd + 4));
210   tcp_info_append_uint("TSV", pntohl(opd));
211   tcp_info_append_uint("TSER", pntohl(opd + 4));
212 }
213
214 static void
215 dissect_tcpopt_cc(proto_tree *opt_tree, const char *name, const u_char *opd,
216     int offset, guint optlen)
217 {
218   proto_tree_add_item(opt_tree, offset,      optlen,
219     "%s: %u", name, pntohl(opd));
220   tcp_info_append_uint("CC", pntohl(opd));
221 }
222
223 static ip_tcp_opt tcpopts[] = {
224   {
225     TCPOPT_EOL,
226     "EOL",
227     NO_LENGTH,
228     0,
229     NULL,
230   },
231   {
232     TCPOPT_NOP,
233     "NOP",
234     NO_LENGTH,
235     0,
236     NULL,
237   },
238   {
239     TCPOPT_MSS,
240     "Maximum segment size",
241     FIXED_LENGTH,
242     TCPOLEN_MSS,
243     dissect_tcpopt_maxseg
244   },
245   {
246     TCPOPT_WINDOW,
247     "Window scale",
248     FIXED_LENGTH,
249     TCPOLEN_WINDOW,
250     dissect_tcpopt_wscale
251   },
252   {
253     TCPOPT_SACK_PERM,
254     "SACK permitted",
255     FIXED_LENGTH,
256     TCPOLEN_SACK_PERM,
257     NULL,
258   },
259   {
260     TCPOPT_SACK,
261     "SACK",
262     VARIABLE_LENGTH,
263     TCPOLEN_SACK_MIN,
264     dissect_tcpopt_sack
265   },
266   {
267     TCPOPT_ECHO,
268     "Echo",
269     FIXED_LENGTH,
270     TCPOLEN_ECHO,
271     dissect_tcpopt_echo
272   },
273   {
274     TCPOPT_ECHOREPLY,
275     "Echo reply",
276     FIXED_LENGTH,
277     TCPOLEN_ECHOREPLY,
278     dissect_tcpopt_echo
279   },
280   {
281     TCPOPT_TIMESTAMP,
282     "Time stamp",
283     FIXED_LENGTH,
284     TCPOLEN_TIMESTAMP,
285     dissect_tcpopt_timestamp
286   },
287   {
288     TCPOPT_CC,
289     "CC",
290     FIXED_LENGTH,
291     TCPOLEN_CC,
292     dissect_tcpopt_cc
293   },
294   {
295     TCPOPT_CCNEW,
296     "CC.NEW",
297     FIXED_LENGTH,
298     TCPOPT_CCNEW,
299     dissect_tcpopt_cc
300   },
301   {
302     TCPOPT_CCECHO,
303     "CC.ECHO",
304     FIXED_LENGTH,
305     TCPOLEN_CCECHO,
306     dissect_tcpopt_cc
307   }
308 };
309
310 #define N_TCP_OPTS      (sizeof tcpopts / sizeof tcpopts[0])
311
312 void
313 dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
314   e_tcphdr   th;
315   proto_tree *tcp_tree = NULL, *field_tree = NULL;
316   proto_item *ti, *tf;
317   gchar      flags[64] = "<None>";
318   gchar     *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
319   gint       fpos = 0, i;
320   guint      bpos;
321   guint      hlen;
322   guint      optlen;
323
324   /* To do: Check for {cap len,pkt len} < struct len */
325   /* Avoids alignment problems on many architectures. */
326   memcpy(&th, &pd[offset], sizeof(e_tcphdr));
327   th.th_sport = ntohs(th.th_sport);
328   th.th_dport = ntohs(th.th_dport);
329   th.th_win   = ntohs(th.th_win);
330   th.th_sum   = ntohs(th.th_sum);
331   th.th_urp   = ntohs(th.th_urp);
332   th.th_seq   = ntohl(th.th_seq);
333   th.th_ack   = ntohl(th.th_ack);
334   
335   info_len = 0;
336
337   if (check_col(fd, COL_PROTOCOL) || tree) {  
338     for (i = 0; i < 6; i++) {
339       bpos = 1 << i;
340       if (th.th_flags & bpos) {
341         if (fpos) {
342           strcpy(&flags[fpos], ", ");
343           fpos += 2;
344         }
345         strcpy(&flags[fpos], fstr[i]);
346         fpos += 3;
347       }
348     }
349     flags[fpos] = '\0';
350   }
351   
352   hlen = hi_nibble(th.th_off_x2) * 4;  /* TCP header length, in bytes */
353
354   if (check_col(fd, COL_RES_SRC_PORT))
355     col_add_str(fd, COL_RES_SRC_PORT, get_tcp_port(th.th_sport));
356   if (check_col(fd, COL_UNRES_SRC_PORT))
357     col_add_fstr(fd, COL_UNRES_SRC_PORT, "%u", th.th_sport);
358   if (check_col(fd, COL_RES_DST_PORT))
359     col_add_str(fd, COL_RES_DST_PORT, get_tcp_port(th.th_dport));
360   if (check_col(fd, COL_UNRES_DST_PORT))
361     col_add_fstr(fd, COL_UNRES_DST_PORT, "%u", th.th_dport);
362   if (check_col(fd, COL_PROTOCOL))
363     col_add_str(fd, COL_PROTOCOL, "TCP");
364   if (check_col(fd, COL_INFO)) {
365     /* Copy the data into info_str in case one of the option handling
366        routines needs to append to it. */
367     if (th.th_flags & TH_URG)
368       info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u",
369         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
370         th.th_seq, th.th_ack, th.th_win, th.th_urp);
371     else
372       info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u",
373         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
374         th.th_seq, th.th_ack, th.th_win);
375     /* The info column is actually written after the options are decoded */
376   }
377   
378   if (tree) {
379     ti = proto_tree_add_item(tree, offset, hlen,
380       "Transmission Control Protocol");
381     tcp_tree = proto_tree_new();
382     proto_item_add_subtree(ti, tcp_tree, ETT_TCP);
383     proto_tree_add_item(tcp_tree, offset,      2, "Source port: %s (%u)",
384       get_tcp_port(th.th_sport), th.th_sport);
385     proto_tree_add_item(tcp_tree, offset +  2, 2, "Destination port: %s (%u)",
386       get_tcp_port(th.th_dport), th.th_dport);
387     proto_tree_add_item(tcp_tree, offset +  4, 4, "Sequence number: %u",
388       th.th_seq);
389     if (th.th_flags & TH_ACK)
390       proto_tree_add_item(tcp_tree, offset +  8, 4, "Acknowledgement number: %u",
391         th.th_ack);
392     proto_tree_add_item(tcp_tree, offset + 12, 1, "Header length: %u bytes", hlen);
393      tf = proto_tree_add_item(tcp_tree, offset + 13, 1, "Flags: 0x%x", th.th_flags);
394      field_tree = proto_tree_new();
395      proto_item_add_subtree(tf, field_tree, ETT_TCP_FLAGS);
396      proto_tree_add_item(field_tree, offset + 13, 1, "%s",
397        decode_boolean_bitfield(th.th_flags, TH_URG, sizeof (th.th_flags)*8,
398                          "Urgent pointer", "No urgent pointer"));
399      proto_tree_add_item(field_tree, offset + 13, 1, "%s",
400        decode_boolean_bitfield(th.th_flags, TH_ACK, sizeof (th.th_flags)*8,
401                          "Acknowledgment", "No acknowledgment"));
402      proto_tree_add_item(field_tree, offset + 13, 1, "%s",
403        decode_boolean_bitfield(th.th_flags, TH_PUSH, sizeof (th.th_flags)*8,
404                          "Push", "No push"));
405      proto_tree_add_item(field_tree, offset + 13, 1, "%s",
406        decode_boolean_bitfield(th.th_flags, TH_RST, sizeof (th.th_flags)*8,
407                          "Reset", "No reset"));
408      proto_tree_add_item(field_tree, offset + 13, 1, "%s",
409        decode_boolean_bitfield(th.th_flags, TH_SYN, sizeof (th.th_flags)*8,
410                          "Syn", "No Syn"));
411      proto_tree_add_item(field_tree, offset + 13, 1, "%s",
412        decode_boolean_bitfield(th.th_flags, TH_FIN, sizeof (th.th_flags)*8,
413                          "Fin", "No Fin"));
414     proto_tree_add_item(tcp_tree, offset + 14, 2, "Window size: %u", th.th_win);
415     proto_tree_add_item(tcp_tree, offset + 16, 2, "Checksum: 0x%04x", th.th_sum);
416     if (th.th_flags & TH_URG)
417       proto_tree_add_item(tcp_tree, offset + 18, 2, "Urgent pointer: 0x%04x",
418         th.th_urp);
419   }
420
421   /* Decode TCP options, if any. */
422   if (hlen > sizeof (e_tcphdr)) {
423     /* There's more than just the fixed-length header.  Decode the
424        options. */
425     optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
426     if (tree) {
427       tf = proto_tree_add_item(tcp_tree, offset +  20, optlen,
428         "Options: (%d bytes)", optlen);
429       field_tree = proto_tree_new();
430       proto_item_add_subtree(tf, field_tree, ETT_TCP_OPTIONS);
431     }
432     dissect_ip_tcp_options(field_tree, &pd[offset + 20], offset + 20, optlen,
433        tcpopts, N_TCP_OPTS, TCPOPT_EOL);
434   }
435
436   if (check_col(fd, COL_INFO))
437     col_add_str(fd, COL_INFO, info_str);
438
439   /* Skip over header + options */
440   offset += hlen;
441
442   /* Check the packet length to see if there's more data
443      (it could be an ACK-only packet) */
444   if (fd->cap_len > offset) {
445     switch(MIN(th.th_sport, th.th_dport)) {
446       case TCP_PORT_PRINTER:
447         dissect_lpd(pd, offset, fd, tree);
448         break;
449       case TCP_PORT_HTTP:
450       case TCP_ALT_PORT_HTTP:
451         dissect_http(pd, offset, fd, tree);
452         break;
453       default:
454         /* check existence of high level protocols */
455
456         if (memcmp(&pd[offset], "GIOP",  4) == 0) {
457           dissect_giop(pd, offset, fd, tree);
458         }
459         else {
460           dissect_data(pd, offset, fd, tree);
461         }
462     }
463   }
464  
465   pi.srcport = th.th_sport;
466   pi.destport = th.th_dport;
467   
468   if( data_out_file ) {
469     reassemble_tcp( th.th_seq, /* sequence number */
470         ( pi.iplen -( pi.iphdrlen * 4 )-( hi_nibble(th.th_off_x2) * 4 ) ), /* length */
471         ( pd+offset ), /* data */
472         ( th.th_flags & 0x02 ), /* is syn set? */
473         pi.ip_src ); /* src ip */
474   }
475 }