Include the "-D" flag in the usage message.
[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.56 2000/01/16 02:54:49 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 "globals.h"
41 #include "resolv.h"
42 #include "follow.h"
43
44 #ifdef NEED_SNPRINTF_H
45 # ifdef HAVE_STDARG_H
46 #  include <stdarg.h>
47 # else
48 #  include <varargs.h>
49 # endif
50 # include "snprintf.h"
51 #endif
52
53 #include "plugins.h"
54
55 #ifndef __PACKET_IP_H__
56 #include "packet-ip.h"
57 #endif
58
59 extern FILE* data_out_file;
60
61 static gchar info_str[COL_MAX_LEN];
62 static int   info_len;
63
64 static int proto_tcp = -1;
65 static int hf_tcp_srcport = -1;
66 static int hf_tcp_dstport = -1;
67 static int hf_tcp_port = -1;
68 static int hf_tcp_seq = -1;
69 static int hf_tcp_ack = -1;
70 static int hf_tcp_hdr_len = -1;
71 static int hf_tcp_flags = -1;
72 static int hf_tcp_flags_urg = -1;
73 static int hf_tcp_flags_ack = -1;
74 static int hf_tcp_flags_push = -1;
75 static int hf_tcp_flags_reset = -1;
76 static int hf_tcp_flags_syn = -1;
77 static int hf_tcp_flags_fin = -1;
78 static int hf_tcp_window_size = -1;
79 static int hf_tcp_checksum = -1;
80 static int hf_tcp_urgent_pointer = -1;
81
82 static gint ett_tcp = -1;
83 static gint ett_tcp_flags = -1;
84 static gint ett_tcp_options = -1;
85 static gint ett_tcp_option_sack = -1;
86
87 /* TCP Ports */
88
89 #define TCP_PORT_FTPDATA                20
90 #define TCP_PORT_FTP                    21
91 #define TCP_PORT_TELNET                 23
92 #define TCP_PORT_SMTP                   25
93 #define TCP_PORT_HTTP                   80
94 #define TCP_PORT_TACACS                 49
95 #define TCP_PORT_POP                    110
96 #define TCP_PORT_NNTP                   119
97 #define TCP_PORT_NTP                    123
98 #define TCP_PORT_NBSS                   139
99 #define TCP_PORT_IMAP                   143
100 #define TCP_PORT_BGP                    179
101 #define TCP_PORT_LDAP                   389
102 #define TCP_PORT_SRVLOC                 427
103 #define TCP_PORT_PRINTER                515
104 #define TCP_PORT_NCP                    524
105 #define TCP_PORT_RTSP                   554
106 #define TCP_PORT_MAPI                   1065
107 #define TCP_PORT_TNS                    1521
108 #define TCP_PORT_PPTP                   1723
109 #define TCP_PORT_PROXY_HTTP             3128
110 #define TCP_PORT_PROXY_ADMIN_HTTP       3132
111 #define TCP_PORT_YHOO                   5050
112 #define TCP_ALT_PORT_HTTP               8080
113 #define TCP_PORT_IRC                    6667
114         /* good candidate for dynamic port specification */
115
116 /* TCP structs and definitions */
117
118 typedef struct _e_tcphdr {
119   guint16 th_sport;
120   guint16 th_dport;
121   guint32 th_seq;
122   guint32 th_ack;
123   guint8  th_off_x2; /* combines th_off and th_x2 */
124   guint8  th_flags;
125 #define TH_FIN  0x01
126 #define TH_SYN  0x02
127 #define TH_RST  0x04
128 #define TH_PUSH 0x08
129 #define TH_ACK  0x10
130 #define TH_URG  0x20
131   guint16 th_win;
132   guint16 th_sum;
133   guint16 th_urp;
134 } e_tcphdr;
135
136 /*
137  *      TCP option
138  */
139  
140 #define TCPOPT_NOP              1       /* Padding */
141 #define TCPOPT_EOL              0       /* End of options */
142 #define TCPOPT_MSS              2       /* Segment size negotiating */
143 #define TCPOPT_WINDOW           3       /* Window scaling */
144 #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
145 #define TCPOPT_SACK             5       /* SACK Block */
146 #define TCPOPT_ECHO             6
147 #define TCPOPT_ECHOREPLY        7
148 #define TCPOPT_TIMESTAMP        8       /* Better RTT estimations/PAWS */
149 #define TCPOPT_CC               11
150 #define TCPOPT_CCNEW            12
151 #define TCPOPT_CCECHO           13
152
153 /*
154  *     TCP option lengths
155  */
156
157 #define TCPOLEN_MSS            4
158 #define TCPOLEN_WINDOW         3
159 #define TCPOLEN_SACK_PERM      2
160 #define TCPOLEN_SACK_MIN       2
161 #define TCPOLEN_ECHO           6
162 #define TCPOLEN_ECHOREPLY      6
163 #define TCPOLEN_TIMESTAMP      10
164 #define TCPOLEN_CC             6
165 #define TCPOLEN_CCNEW          6
166 #define TCPOLEN_CCECHO         6
167
168 static void
169 tcp_info_append_uint(const char *abbrev, guint32 val) {
170   int add_len = 0;
171   
172   if (info_len > 0)
173   if(info_len > 0)
174     add_len = snprintf(&info_str[info_len], COL_MAX_LEN - info_len, " %s=%u",
175       abbrev, val);
176   if (add_len > 0)
177     info_len += add_len;
178 }
179
180 static void
181 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, const u_char *opd,
182     int offset, guint optlen, proto_tree *opt_tree)
183 {
184   proto_tree_add_text(opt_tree, offset,      optlen,
185                         "%s: %u bytes", optp->name, pntohs(opd));
186   tcp_info_append_uint("MSS", pntohs(opd));
187 }
188
189 static void
190 dissect_tcpopt_wscale(const ip_tcp_opt *optp, const u_char *opd,
191     int offset, guint optlen, proto_tree *opt_tree)
192 {
193   proto_tree_add_text(opt_tree, offset,      optlen,
194                         "%s: %u bytes", optp->name, *opd);
195   tcp_info_append_uint("WS", *opd);
196 }
197
198 static void
199 dissect_tcpopt_sack(const ip_tcp_opt *optp, const u_char *opd,
200     int offset, guint optlen, proto_tree *opt_tree)
201 {
202   proto_tree *field_tree = NULL;
203   proto_item *tf;
204   guint leftedge, rightedge;
205
206   tf = proto_tree_add_text(opt_tree, offset,      optlen, "%s:", optp->name);
207   offset += 2;  /* skip past type and length */
208   optlen -= 2;  /* subtract size of type and length */
209   while (optlen > 0) {
210     if (field_tree == NULL) {
211       /* Haven't yet made a subtree out of this option.  Do so. */
212       field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
213     }
214     if (optlen < 4) {
215       proto_tree_add_text(field_tree, offset,      optlen,
216         "(suboption would go past end of option)");
217       break;
218     }
219     /* XXX - check whether it goes past end of packet */
220     leftedge = pntohl(opd);
221     opd += 4;
222     optlen -= 4;
223     if (optlen < 4) {
224       proto_tree_add_text(field_tree, offset,      optlen,
225         "(suboption would go past end of option)");
226       break;
227     }
228     /* XXX - check whether it goes past end of packet */
229     rightedge = pntohl(opd);
230     opd += 4;
231     optlen -= 4;
232     proto_tree_add_text(field_tree, offset,      8,
233         "left edge = %u, right edge = %u", leftedge, rightedge);
234     tcp_info_append_uint("SLE", leftedge);
235     tcp_info_append_uint("SRE", rightedge);
236     offset += 8;
237   }
238 }
239
240 static void
241 dissect_tcpopt_echo(const ip_tcp_opt *optp, const u_char *opd,
242     int offset, guint optlen, proto_tree *opt_tree)
243 {
244   proto_tree_add_text(opt_tree, offset,      optlen,
245                         "%s: %u", optp->name, pntohl(opd));
246   tcp_info_append_uint("ECHO", pntohl(opd));
247 }
248
249 static void
250 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, const u_char *opd,
251     int offset, guint optlen, proto_tree *opt_tree)
252 {
253   proto_tree_add_text(opt_tree, offset,      optlen,
254     "%s: tsval %u, tsecr %u", optp->name, pntohl(opd), pntohl(opd + 4));
255   tcp_info_append_uint("TSV", pntohl(opd));
256   tcp_info_append_uint("TSER", pntohl(opd + 4));
257 }
258
259 static void
260 dissect_tcpopt_cc(const ip_tcp_opt *optp, const u_char *opd,
261     int offset, guint optlen, proto_tree *opt_tree)
262 {
263   proto_tree_add_text(opt_tree, offset,      optlen,
264                         "%s: %u", optp->name, pntohl(opd));
265   tcp_info_append_uint("CC", pntohl(opd));
266 }
267
268 static const ip_tcp_opt tcpopts[] = {
269   {
270     TCPOPT_EOL,
271     "EOL",
272     NULL,
273     NO_LENGTH,
274     0,
275     NULL,
276   },
277   {
278     TCPOPT_NOP,
279     "NOP",
280     NULL,
281     NO_LENGTH,
282     0,
283     NULL,
284   },
285   {
286     TCPOPT_MSS,
287     "Maximum segment size",
288     NULL,
289     FIXED_LENGTH,
290     TCPOLEN_MSS,
291     dissect_tcpopt_maxseg
292   },
293   {
294     TCPOPT_WINDOW,
295     "Window scale",
296     NULL,
297     FIXED_LENGTH,
298     TCPOLEN_WINDOW,
299     dissect_tcpopt_wscale
300   },
301   {
302     TCPOPT_SACK_PERM,
303     "SACK permitted",
304     NULL,
305     FIXED_LENGTH,
306     TCPOLEN_SACK_PERM,
307     NULL,
308   },
309   {
310     TCPOPT_SACK,
311     "SACK",
312     &ett_tcp_option_sack,
313     VARIABLE_LENGTH,
314     TCPOLEN_SACK_MIN,
315     dissect_tcpopt_sack
316   },
317   {
318     TCPOPT_ECHO,
319     "Echo",
320     NULL,
321     FIXED_LENGTH,
322     TCPOLEN_ECHO,
323     dissect_tcpopt_echo
324   },
325   {
326     TCPOPT_ECHOREPLY,
327     "Echo reply",
328     NULL,
329     FIXED_LENGTH,
330     TCPOLEN_ECHOREPLY,
331     dissect_tcpopt_echo
332   },
333   {
334     TCPOPT_TIMESTAMP,
335     "Time stamp",
336     NULL,
337     FIXED_LENGTH,
338     TCPOLEN_TIMESTAMP,
339     dissect_tcpopt_timestamp
340   },
341   {
342     TCPOPT_CC,
343     "CC",
344     NULL,
345     FIXED_LENGTH,
346     TCPOLEN_CC,
347     dissect_tcpopt_cc
348   },
349   {
350     TCPOPT_CCNEW,
351     "CC.NEW",
352     NULL,
353     FIXED_LENGTH,
354     TCPOPT_CCNEW,
355     dissect_tcpopt_cc
356   },
357   {
358     TCPOPT_CCECHO,
359     "CC.ECHO",
360     NULL,
361     FIXED_LENGTH,
362     TCPOLEN_CCECHO,
363     dissect_tcpopt_cc
364   }
365 };
366
367 #define N_TCP_OPTS      (sizeof tcpopts / sizeof tcpopts[0])
368
369 /* TCP flags flag */
370 static const true_false_string flags_set_truth = {
371   "Set",
372   "Not set"
373 };
374
375 void
376 dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
377   e_tcphdr   th;
378   proto_tree *tcp_tree = NULL, *field_tree = NULL;
379   proto_item *ti, *tf;
380   gchar      flags[64] = "<None>";
381   gchar     *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
382   gint       fpos = 0, i;
383   guint      bpos;
384   guint      hlen;
385   guint      optlen;
386   guint      packet_max = pi.len;
387
388   /* To do: Check for {cap len,pkt len} < struct len */
389   /* Avoids alignment problems on many architectures. */
390   memcpy(&th, &pd[offset], sizeof(e_tcphdr));
391   th.th_sport = ntohs(th.th_sport);
392   th.th_dport = ntohs(th.th_dport);
393   th.th_win   = ntohs(th.th_win);
394   th.th_sum   = ntohs(th.th_sum);
395   th.th_urp   = ntohs(th.th_urp);
396   th.th_seq   = ntohl(th.th_seq);
397   th.th_ack   = ntohl(th.th_ack);
398   
399   info_len = 0;
400
401   if (check_col(fd, COL_PROTOCOL) || tree) {  
402     for (i = 0; i < 6; i++) {
403       bpos = 1 << i;
404       if (th.th_flags & bpos) {
405         if (fpos) {
406           strcpy(&flags[fpos], ", ");
407           fpos += 2;
408         }
409         strcpy(&flags[fpos], fstr[i]);
410         fpos += 3;
411       }
412     }
413     flags[fpos] = '\0';
414   }
415   
416   hlen = hi_nibble(th.th_off_x2) * 4;  /* TCP header length, in bytes */
417
418   if (check_col(fd, COL_PROTOCOL))
419     col_add_str(fd, COL_PROTOCOL, "TCP");
420   if (check_col(fd, COL_INFO)) {
421     /* Copy the data into info_str in case one of the option handling
422        routines needs to append to it. */
423     if (th.th_flags & TH_URG)
424       info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u",
425         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
426         th.th_seq, th.th_ack, th.th_win, th.th_urp);
427     else
428       info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u",
429         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
430         th.th_seq, th.th_ack, th.th_win);
431     /* The info column is actually written after the options are decoded */
432   }
433   
434   if (tree) {
435     ti = proto_tree_add_item_format(tree, proto_tcp, offset, hlen, NULL, "Transmission Control Protocol, Src Port: %s (%u), Dst Port: %s (%u), Seq: %u, Ack: %u", get_tcp_port(th.th_sport), th.th_sport, get_tcp_port(th.th_dport), th.th_dport, th.th_seq, th.th_ack);
436     tcp_tree = proto_item_add_subtree(ti, ett_tcp);
437     proto_tree_add_item_format(tcp_tree, hf_tcp_srcport, offset, 2, th.th_sport,
438         "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
439     proto_tree_add_item_format(tcp_tree, hf_tcp_dstport, offset + 2, 2, th.th_dport,
440         "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
441     proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset, 2, th.th_sport);
442     proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset + 2, 2, th.th_dport);
443     proto_tree_add_item(tcp_tree, hf_tcp_seq, offset + 4, 4, th.th_seq);
444     if (th.th_flags & TH_ACK)
445       proto_tree_add_item(tcp_tree, hf_tcp_ack, offset + 8, 4, th.th_ack);
446     proto_tree_add_item_format(tcp_tree, hf_tcp_hdr_len, offset + 12, 1, hlen,
447         "Header length: %u bytes", hlen);
448     tf = proto_tree_add_item_format(tcp_tree, hf_tcp_flags, offset + 13, 1,
449         th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
450     field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
451     proto_tree_add_item(field_tree, hf_tcp_flags_urg, offset + 13, 1, th.th_flags);
452     proto_tree_add_item(field_tree, hf_tcp_flags_ack, offset + 13, 1, th.th_flags);
453     proto_tree_add_item(field_tree, hf_tcp_flags_push, offset + 13, 1, th.th_flags);
454     proto_tree_add_item(field_tree, hf_tcp_flags_reset, offset + 13, 1, th.th_flags);
455     proto_tree_add_item(field_tree, hf_tcp_flags_syn, offset + 13, 1, th.th_flags);
456     proto_tree_add_item(field_tree, hf_tcp_flags_fin, offset + 13, 1, th.th_flags);
457     proto_tree_add_item(tcp_tree, hf_tcp_window_size, offset + 14, 2, th.th_win);
458     proto_tree_add_item(tcp_tree, hf_tcp_checksum, offset + 16, 2, th.th_sum);
459     if (th.th_flags & TH_URG)
460       proto_tree_add_item(tcp_tree, hf_tcp_urgent_pointer, offset + 18, 2, th.th_urp);
461   }
462
463   /* Decode TCP options, if any. */
464   if (tree  && hlen > sizeof (e_tcphdr)) {
465     /* There's more than just the fixed-length header.  Decode the
466        options. */
467     optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
468     tf = proto_tree_add_text(tcp_tree, offset +  20, optlen,
469       "Options: (%d bytes)", optlen);
470     field_tree = proto_item_add_subtree(tf, ett_tcp_options);
471     dissect_ip_tcp_options(&pd[offset + 20], offset + 20, optlen,
472       tcpopts, N_TCP_OPTS, TCPOPT_EOL, field_tree);
473   }
474
475   if (check_col(fd, COL_INFO))
476     col_add_str(fd, COL_INFO, info_str);
477
478   /* Skip over header + options */
479   offset += hlen;
480
481   pi.ptype = PT_TCP;
482   pi.srcport = th.th_sport;
483   pi.destport = th.th_dport;
484   
485   /* Check the packet length to see if there's more data
486      (it could be an ACK-only packet) */
487   if (packet_max > offset) {
488
489     /* try to apply the plugins */
490 #ifdef HAVE_PLUGINS
491     plugin *pt_plug = plugin_list;
492
493     if (pt_plug) {
494       while (pt_plug) {
495         if (pt_plug->enabled && !strcmp(pt_plug->protocol, "tcp") &&
496             tree && dfilter_apply(pt_plug->filter, tree, pd)) {
497           pt_plug->dissector(pd, offset, fd, tree);
498           goto reas;
499         }
500         pt_plug = pt_plug->next;
501       }
502     }
503 #endif
504
505     /* ONC RPC.  We can't base this on anything in the TCP header; we have
506        to look at the payload.  If "dissect_rpc()" returns TRUE, it was
507        an RPC packet, otherwise it's some other type of packet. */
508     if (dissect_rpc(pd, offset, fd, tree))
509       goto reas;
510
511     /* XXX - this should be handled the way UDP handles this, with a table
512        of port numbers to which stuff can be added */
513 #define PORT_IS(port)   (th.th_sport == port || th.th_dport == port)
514     if (PORT_IS(TCP_PORT_PRINTER))
515       dissect_lpd(pd, offset, fd, tree);
516     else if (PORT_IS(TCP_PORT_TELNET)) {
517       pi.match_port = TCP_PORT_TELNET;
518       dissect_telnet(pd, offset, fd, tree);
519     } else if (PORT_IS(TCP_PORT_FTPDATA)) {
520       pi.match_port = TCP_PORT_FTPDATA;
521       dissect_ftpdata(pd, offset, fd, tree);
522     } else if (PORT_IS(TCP_PORT_FTP)) {
523       pi.match_port = TCP_PORT_FTP;
524       dissect_ftp(pd, offset, fd, tree);
525     } else if (PORT_IS(TCP_PORT_POP)) {
526       pi.match_port = TCP_PORT_POP;
527       dissect_pop(pd, offset, fd, tree);
528     } else if (PORT_IS(TCP_PORT_IMAP)) {
529       pi.match_port = TCP_PORT_IMAP;
530       dissect_imap(pd, offset, fd, tree);
531     } else if (PORT_IS(TCP_PORT_NNTP)) {
532       pi.match_port = TCP_PORT_NNTP;
533       dissect_nntp(pd, offset, fd, tree);
534     } else if (PORT_IS(TCP_PORT_NTP)) {
535       pi.match_port = TCP_PORT_NTP;
536       dissect_ntp(pd, offset, fd, tree);
537     } else if (PORT_IS(TCP_PORT_PPTP)) {
538       pi.match_port = TCP_PORT_PPTP;
539       dissect_pptp(pd, offset, fd, tree);
540     } else if (PORT_IS(TCP_PORT_HTTP) || PORT_IS(TCP_ALT_PORT_HTTP)
541             || PORT_IS(631) || PORT_IS(TCP_PORT_PROXY_HTTP)
542             || PORT_IS(TCP_PORT_PROXY_ADMIN_HTTP))
543       dissect_http(pd, offset, fd, tree);
544     else if (PORT_IS(TCP_PORT_NBSS)) {
545       pi.match_port = TCP_PORT_NBSS;
546       dissect_nbss(pd, offset, fd, tree);
547     } else if (PORT_IS(TCP_PORT_RTSP))
548       dissect_rtsp(pd, offset, fd, tree);
549     else if (PORT_IS(TCP_PORT_BGP)) {
550       pi.match_port = TCP_PORT_BGP;
551       dissect_bgp(pd, offset, fd, tree);
552     } else if (PORT_IS(TCP_PORT_TACACS)) {
553       pi.match_port = TCP_PORT_TACACS;
554       dissect_tacplus(pd, offset, fd, tree);
555     } else if (PORT_IS(TCP_PORT_MAPI)) {
556       pi.match_port = TCP_PORT_MAPI;
557       dissect_mapi(pd, offset, fd, tree);
558     } else if (PORT_IS(TCP_PORT_TNS)) {
559       pi.match_port = TCP_PORT_TNS;
560       dissect_tns(pd, offset, fd, tree);
561     } else if (PORT_IS(TCP_PORT_IRC)) {
562       pi.match_port = TCP_PORT_IRC;
563       dissect_irc(pd, offset, fd, tree);
564     } else if (PORT_IS(TCP_PORT_LDAP)) {
565       pi.match_port = TCP_PORT_LDAP;
566       dissect_ldap(pd, offset, fd, tree);
567     } else if (PORT_IS(TCP_PORT_SRVLOC)) {
568       pi.match_port = TCP_PORT_SRVLOC;
569       dissect_srvloc(pd, offset, fd, tree);
570     } else if (PORT_IS(TCP_PORT_NCP)) {
571       pi.match_port = TCP_PORT_NCP;
572       dissect_ncp(pd, offset, fd, tree);
573     } else {
574         /* check existence of high level protocols */
575
576         if (memcmp(&pd[offset], "GIOP",  4) == 0) {
577           dissect_giop(pd, offset, fd, tree);
578         }
579         else if ( PORT_IS(TCP_PORT_YHOO) && 
580                 (memcmp(&pd[offset], "YPNS",  4) == 0 ||
581                         memcmp(&pd[offset], "YHOO",  4) == 0 )) {
582           dissect_yhoo(pd, offset, fd, tree);
583         }
584         else {
585           dissect_data(pd, offset, fd, tree);
586         }
587     }
588   }
589
590 reas:
591  
592   if( data_out_file ) {
593     reassemble_tcp( th.th_seq,          /* sequence number */
594         ( pi.len - offset ),            /* data length */
595         ( pd+offset ),                  /* data */
596         ( pi.captured_len - offset ),   /* captured data length */
597         ( th.th_flags & TH_SYN ),       /* is syn set? */
598         &pi.net_src,
599         &pi.net_dst,
600         pi.srcport,
601         pi.destport,
602         fd->rel_secs,
603         fd->rel_usecs);
604   }
605 }
606
607 void
608 proto_register_tcp(void)
609 {
610         static hf_register_info hf[] = {
611
612                 { &hf_tcp_srcport,
613                 { "Source Port",                "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
614                         "" }},
615
616                 { &hf_tcp_dstport,
617                 { "Destination Port",           "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
618                         "" }},
619
620                 { &hf_tcp_port,
621                 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
622                         "" }},
623
624                 { &hf_tcp_seq,
625                 { "Sequence number",            "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
626                         "" }},
627
628                 { &hf_tcp_ack,
629                 { "Acknowledgement number",     "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
630                         "" }},
631
632                 { &hf_tcp_hdr_len,
633                 { "Header Length",              "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
634                         "" }},
635
636                 { &hf_tcp_flags,
637                 { "Flags",                      "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
638                         "" }},
639
640                 { &hf_tcp_flags_urg,
641                 { "Urgent",                     "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
642                         "" }},
643
644                 { &hf_tcp_flags_ack,
645                 { "Acknowledgment",             "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
646                         "" }},
647
648                 { &hf_tcp_flags_push,
649                 { "Push",                       "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
650                         "" }},
651
652                 { &hf_tcp_flags_reset,
653                 { "Reset",                      "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
654                         "" }},
655
656                 { &hf_tcp_flags_syn,
657                 { "Syn",                        "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
658                         "" }},
659
660                 { &hf_tcp_flags_fin,
661                 { "Fin",                        "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
662                         "" }},
663
664                 { &hf_tcp_window_size,
665                 { "Window size",                "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
666                         "" }},
667
668                 { &hf_tcp_checksum,
669                 { "Checksum",                   "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
670                         "" }},
671
672                 { &hf_tcp_urgent_pointer,
673                 { "Urgent pointer",             "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
674                         "" }},
675         };
676         static gint *ett[] = {
677                 &ett_tcp,
678                 &ett_tcp_flags,
679                 &ett_tcp_options,
680                 &ett_tcp_option_sack,
681         };
682
683         proto_tcp = proto_register_protocol ("Transmission Control Protocol", "tcp");
684         proto_register_field_array(proto_tcp, hf, array_length(hf));
685         proto_register_subtree_array(ett, array_length(ett));
686 }