Add the relative time to the frame tree, at the request of Manfred Young.
[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.92 2000/12/13 02:24:21 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 <string.h>
40 #include <glib.h>
41 #include "in_cksum.h"
42
43 #ifdef NEED_SNPRINTF_H
44 # include "snprintf.h"
45 #endif
46
47 #include "globals.h"
48 #include "resolv.h"
49 #include "follow.h"
50 #include "prefs.h"
51 #include "plugins.h"
52 #include "packet-tcp.h"
53 #include "packet-ip.h"
54 #include "conversation.h"
55 #include "strutil.h"
56
57 /* Place TCP summary in proto tree */
58 static gboolean tcp_summary_in_tree = TRUE;
59
60 extern FILE* data_out_file;
61
62 guint16 tcp_urgent_pointer;
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_nxtseq = -1;
70 static int hf_tcp_ack = -1;
71 static int hf_tcp_hdr_len = -1;
72 static int hf_tcp_flags = -1;
73 static int hf_tcp_flags_cwr = -1;
74 static int hf_tcp_flags_ecn = -1;
75 static int hf_tcp_flags_urg = -1;
76 static int hf_tcp_flags_ack = -1;
77 static int hf_tcp_flags_push = -1;
78 static int hf_tcp_flags_reset = -1;
79 static int hf_tcp_flags_syn = -1;
80 static int hf_tcp_flags_fin = -1;
81 static int hf_tcp_window_size = -1;
82 static int hf_tcp_checksum = -1;
83 static int hf_tcp_urgent_pointer = -1;
84
85 static gint ett_tcp = -1;
86 static gint ett_tcp_flags = -1;
87 static gint ett_tcp_options = -1;
88 static gint ett_tcp_option_sack = -1;
89
90 static dissector_table_t subdissector_table;
91 static heur_dissector_list_t heur_subdissector_list;
92
93 /* TCP Ports */
94
95 #define TCP_PORT_SMTP                   25
96
97 /* TCP structs and definitions */
98
99 typedef struct _e_tcphdr {
100   guint16 th_sport;
101   guint16 th_dport;
102   guint32 th_seq;
103   guint32 th_ack;
104   guint8  th_off_x2; /* combines th_off and th_x2 */
105   guint8  th_flags;
106 #define TH_FIN  0x01
107 #define TH_SYN  0x02
108 #define TH_RST  0x04
109 #define TH_PUSH 0x08
110 #define TH_ACK  0x10
111 #define TH_URG  0x20
112 #define TH_ECN  0x40
113 #define TH_CWR  0x80
114   guint16 th_win;
115   guint16 th_sum;
116   guint16 th_urp;
117 } e_tcphdr;
118
119 /*
120  *      TCP option
121  */
122  
123 #define TCPOPT_NOP              1       /* Padding */
124 #define TCPOPT_EOL              0       /* End of options */
125 #define TCPOPT_MSS              2       /* Segment size negotiating */
126 #define TCPOPT_WINDOW           3       /* Window scaling */
127 #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
128 #define TCPOPT_SACK             5       /* SACK Block */
129 #define TCPOPT_ECHO             6
130 #define TCPOPT_ECHOREPLY        7
131 #define TCPOPT_TIMESTAMP        8       /* Better RTT estimations/PAWS */
132 #define TCPOPT_CC               11
133 #define TCPOPT_CCNEW            12
134 #define TCPOPT_CCECHO           13
135
136 /*
137  *     TCP option lengths
138  */
139
140 #define TCPOLEN_MSS            4
141 #define TCPOLEN_WINDOW         3
142 #define TCPOLEN_SACK_PERM      2
143 #define TCPOLEN_SACK_MIN       2
144 #define TCPOLEN_ECHO           6
145 #define TCPOLEN_ECHOREPLY      6
146 #define TCPOLEN_TIMESTAMP      10
147 #define TCPOLEN_CC             6
148 #define TCPOLEN_CCNEW          6
149 #define TCPOLEN_CCECHO         6
150
151 static void
152 tcp_info_append_uint(frame_data *fd, const char *abbrev, guint32 val)
153 {
154   if (check_col(fd, COL_INFO))
155     col_append_fstr(fd, COL_INFO, " %s=%u", abbrev, val);
156 }
157
158 static void
159 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, tvbuff_t *tvb,
160     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
161 {
162   guint16 mss;
163
164   mss = tvb_get_ntohs(tvb, offset + 2);
165   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
166                         "%s: %u bytes", optp->name, mss);
167   tcp_info_append_uint(fd, "MSS", mss);
168 }
169
170 static void
171 dissect_tcpopt_wscale(const ip_tcp_opt *optp, tvbuff_t *tvb,
172     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
173 {
174   guint8 ws;
175
176   ws = tvb_get_guint8(tvb, offset + 2);
177   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
178                         "%s: %u bytes", optp->name, ws);
179   tcp_info_append_uint(fd, "WS", ws);
180 }
181
182 static void
183 dissect_tcpopt_sack(const ip_tcp_opt *optp, tvbuff_t *tvb,
184     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
185 {
186   proto_tree *field_tree = NULL;
187   proto_item *tf;
188   guint leftedge, rightedge;
189
190   tf = proto_tree_add_text(opt_tree, tvb, offset,      optlen, "%s:", optp->name);
191   offset += 2;  /* skip past type and length */
192   optlen -= 2;  /* subtract size of type and length */
193   while (optlen > 0) {
194     if (field_tree == NULL) {
195       /* Haven't yet made a subtree out of this option.  Do so. */
196       field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
197     }
198     if (optlen < 4) {
199       proto_tree_add_text(field_tree, tvb, offset,      optlen,
200         "(suboption would go past end of option)");
201       break;
202     }
203     leftedge = tvb_get_ntohl(tvb, offset);
204     optlen -= 4;
205     if (optlen < 4) {
206       proto_tree_add_text(field_tree, tvb, offset,      optlen,
207         "(suboption would go past end of option)");
208       break;
209     }
210     /* XXX - check whether it goes past end of packet */
211     rightedge = tvb_get_ntohl(tvb, offset + 4);
212     optlen -= 4;
213     proto_tree_add_text(field_tree, tvb, offset,      8,
214         "left edge = %u, right edge = %u", leftedge, rightedge);
215     tcp_info_append_uint(fd, "SLE", leftedge);
216     tcp_info_append_uint(fd, "SRE", rightedge);
217     offset += 8;
218   }
219 }
220
221 static void
222 dissect_tcpopt_echo(const ip_tcp_opt *optp, tvbuff_t *tvb,
223     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
224 {
225   guint32 echo;
226
227   echo = tvb_get_ntohl(tvb, offset + 2);
228   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
229                         "%s: %u", optp->name, echo);
230   tcp_info_append_uint(fd, "ECHO", echo);
231 }
232
233 static void
234 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, tvbuff_t *tvb,
235     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
236 {
237   guint32 tsv, tser;
238
239   tsv = tvb_get_ntohl(tvb, offset + 2);
240   tser = tvb_get_ntohl(tvb, offset + 6);
241   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
242     "%s: tsval %u, tsecr %u", optp->name, tsv, tser);
243   tcp_info_append_uint(fd, "TSV", tsv);
244   tcp_info_append_uint(fd, "TSER", tser);
245 }
246
247 static void
248 dissect_tcpopt_cc(const ip_tcp_opt *optp, tvbuff_t *tvb,
249     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
250 {
251   guint32 cc;
252
253   cc = tvb_get_ntohl(tvb, offset + 2);
254   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
255                         "%s: %u", optp->name, cc);
256   tcp_info_append_uint(fd, "CC", cc);
257 }
258
259 static const ip_tcp_opt tcpopts[] = {
260   {
261     TCPOPT_EOL,
262     "EOL",
263     NULL,
264     NO_LENGTH,
265     0,
266     NULL,
267   },
268   {
269     TCPOPT_NOP,
270     "NOP",
271     NULL,
272     NO_LENGTH,
273     0,
274     NULL,
275   },
276   {
277     TCPOPT_MSS,
278     "Maximum segment size",
279     NULL,
280     FIXED_LENGTH,
281     TCPOLEN_MSS,
282     dissect_tcpopt_maxseg
283   },
284   {
285     TCPOPT_WINDOW,
286     "Window scale",
287     NULL,
288     FIXED_LENGTH,
289     TCPOLEN_WINDOW,
290     dissect_tcpopt_wscale
291   },
292   {
293     TCPOPT_SACK_PERM,
294     "SACK permitted",
295     NULL,
296     FIXED_LENGTH,
297     TCPOLEN_SACK_PERM,
298     NULL,
299   },
300   {
301     TCPOPT_SACK,
302     "SACK",
303     &ett_tcp_option_sack,
304     VARIABLE_LENGTH,
305     TCPOLEN_SACK_MIN,
306     dissect_tcpopt_sack
307   },
308   {
309     TCPOPT_ECHO,
310     "Echo",
311     NULL,
312     FIXED_LENGTH,
313     TCPOLEN_ECHO,
314     dissect_tcpopt_echo
315   },
316   {
317     TCPOPT_ECHOREPLY,
318     "Echo reply",
319     NULL,
320     FIXED_LENGTH,
321     TCPOLEN_ECHOREPLY,
322     dissect_tcpopt_echo
323   },
324   {
325     TCPOPT_TIMESTAMP,
326     "Time stamp",
327     NULL,
328     FIXED_LENGTH,
329     TCPOLEN_TIMESTAMP,
330     dissect_tcpopt_timestamp
331   },
332   {
333     TCPOPT_CC,
334     "CC",
335     NULL,
336     FIXED_LENGTH,
337     TCPOLEN_CC,
338     dissect_tcpopt_cc
339   },
340   {
341     TCPOPT_CCNEW,
342     "CC.NEW",
343     NULL,
344     FIXED_LENGTH,
345     TCPOPT_CCNEW,
346     dissect_tcpopt_cc
347   },
348   {
349     TCPOPT_CCECHO,
350     "CC.ECHO",
351     NULL,
352     FIXED_LENGTH,
353     TCPOLEN_CCECHO,
354     dissect_tcpopt_cc
355   }
356 };
357
358 #define N_TCP_OPTS      (sizeof tcpopts / sizeof tcpopts[0])
359
360 /* TCP flags flag */
361 static const true_false_string flags_set_truth = {
362   "Set",
363   "Not set"
364 };
365
366
367 /* Determine if there is a sub-dissector and call it.  This has been */
368 /* separated into a stand alone routine to other protocol dissectors */
369 /* can call to it, ie. socks    */
370
371 void
372 decode_tcp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
373         proto_tree *tree, int src_port, int dst_port)
374 {
375   tvbuff_t *next_tvb;
376 #ifdef HAVE_PLUGINS
377   const u_char *next_pd;
378   int next_offset;
379 #endif
380
381   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
382
383 /* determine if this packet is part of a conversation and call dissector */
384 /* for the conversation if available */
385
386   if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_TCP,
387                 src_port, dst_port, next_tvb, pinfo, tree))
388         return;
389
390   /* try to apply the plugins */
391 #ifdef HAVE_PLUGINS
392   {
393     plugin *pt_plug = plugin_list;
394
395     if (enabled_plugins_number > 0) {
396       tvb_compat(next_tvb, &next_pd, &next_offset);
397       while (pt_plug) {
398         if (pt_plug->enabled && strstr(pt_plug->protocol, "tcp") &&
399             tree && dfilter_apply(pt_plug->filter, tree, next_pd, pinfo->fd->cap_len)) {
400           pt_plug->dissector(next_pd, next_offset, pinfo->fd, tree);
401           return;
402         }
403         pt_plug = pt_plug->next;
404       }
405     }
406   }
407 #endif
408
409   /* do lookup with the subdissector table */
410   if (dissector_try_port(subdissector_table, src_port, next_tvb, pinfo, tree) ||
411       dissector_try_port(subdissector_table, dst_port, next_tvb, pinfo, tree))
412     return;
413
414   /* do lookup with the heuristic subdissector table */
415   if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
416     return;
417
418   /* Oh, well, we don't know this; dissect it as data. */
419   dissect_data(next_tvb, 0, pinfo, tree);
420 }
421
422
423 static void
424 dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
425 {
426   e_tcphdr   th;
427   proto_tree *tcp_tree = NULL, *field_tree = NULL;
428   proto_item *ti, *tf;
429   int        offset = 0;
430   gchar      flags[64] = "<None>";
431   gchar     *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECN", "CWR" };
432   gint       fpos = 0, i;
433   guint      bpos;
434   guint      hlen;
435   guint      optlen;
436   guint32    seglen;
437   guint32    nxtseq;
438   guint      len;
439   guint      reported_len;
440   vec_t      cksum_vec[4];
441   guint32    phdr[2];
442   guint16    computed_cksum;
443   guint      length_remaining;
444
445   CHECK_DISPLAY_AS_DATA(proto_tcp, tvb, pinfo, tree);
446
447   pinfo->current_proto = "TCP";
448
449   if (check_col(pinfo->fd, COL_PROTOCOL))
450     col_set_str(pinfo->fd, COL_PROTOCOL, "TCP");
451
452   /* Clear out the Info column. */
453   if (check_col(pinfo->fd, COL_INFO))
454     col_clear(pinfo->fd, COL_INFO);
455
456   /* Avoids alignment problems on many architectures. */
457   tvb_memcpy(tvb, (guint8 *)&th, offset, sizeof(e_tcphdr));
458   th.th_sport = ntohs(th.th_sport);
459   th.th_dport = ntohs(th.th_dport);
460   th.th_win   = ntohs(th.th_win);
461   th.th_sum   = ntohs(th.th_sum);
462   th.th_urp   = ntohs(th.th_urp);
463   th.th_seq   = ntohl(th.th_seq);
464   th.th_ack   = ntohl(th.th_ack);
465
466   /* Export the urgent pointer, for the benefit of protocols such as
467      rlogin. */
468   tcp_urgent_pointer = th.th_urp;
469  
470   if (check_col(pinfo->fd, COL_INFO) || tree) {  
471     for (i = 0; i < 8; i++) {
472       bpos = 1 << i;
473       if (th.th_flags & bpos) {
474         if (fpos) {
475           strcpy(&flags[fpos], ", ");
476           fpos += 2;
477         }
478         strcpy(&flags[fpos], fstr[i]);
479         fpos += 3;
480       }
481     }
482     flags[fpos] = '\0';
483   }
484   
485   hlen = hi_nibble(th.th_off_x2) * 4;  /* TCP header length, in bytes */
486
487   reported_len = tvb_reported_length(tvb);
488   len = tvb_length(tvb);
489
490   /* Compute the length of data in this segment. */
491   seglen = reported_len - hlen;
492
493   /* Compute the sequence number of next octet after this segment. */
494   nxtseq = th.th_seq + seglen;
495
496   if (check_col(pinfo->fd, COL_INFO)) {
497     if (th.th_flags & TH_URG)
498       col_append_fstr(pinfo->fd, COL_INFO, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u Len=%d",
499         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
500         th.th_seq, th.th_ack, th.th_win, th.th_urp, seglen);
501     else
502       col_append_fstr(pinfo->fd, COL_INFO, "%s > %s [%s] Seq=%u Ack=%u Win=%u Len=%d",
503         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
504         th.th_seq, th.th_ack, th.th_win, seglen);
505   }
506   
507   if (tree) {
508     if (tcp_summary_in_tree) {
509             ti = proto_tree_add_protocol_format(tree, proto_tcp, tvb, offset, hlen, "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);
510     }
511     else {
512             ti = proto_tree_add_item(tree, proto_tcp, tvb, offset, hlen, FALSE);
513     }
514     tcp_tree = proto_item_add_subtree(ti, ett_tcp);
515     proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, tvb, offset, 2, th.th_sport,
516         "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
517     proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, tvb, offset + 2, 2, th.th_dport,
518         "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
519     proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset, 2, th.th_sport);
520     proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset + 2, 2, th.th_dport);
521     proto_tree_add_uint(tcp_tree, hf_tcp_seq, tvb, offset + 4, 4, th.th_seq);
522     if (nxtseq != th.th_seq)
523       proto_tree_add_uint(tcp_tree, hf_tcp_nxtseq, tvb, offset, 0, nxtseq);
524     if (th.th_flags & TH_ACK)
525       proto_tree_add_uint(tcp_tree, hf_tcp_ack, tvb, offset + 8, 4, th.th_ack);
526     proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, tvb, offset + 12, 1, hlen,
527         "Header length: %u bytes", hlen);
528     tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, tvb, offset + 13, 1,
529         th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
530     field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
531     proto_tree_add_boolean(field_tree, hf_tcp_flags_cwr, tvb, offset + 13, 1, th.th_flags);
532     proto_tree_add_boolean(field_tree, hf_tcp_flags_ecn, tvb, offset + 13, 1, th.th_flags);
533     proto_tree_add_boolean(field_tree, hf_tcp_flags_urg, tvb, offset + 13, 1, th.th_flags);
534     proto_tree_add_boolean(field_tree, hf_tcp_flags_ack, tvb, offset + 13, 1, th.th_flags);
535     proto_tree_add_boolean(field_tree, hf_tcp_flags_push, tvb, offset + 13, 1, th.th_flags);
536     proto_tree_add_boolean(field_tree, hf_tcp_flags_reset, tvb, offset + 13, 1, th.th_flags);
537     proto_tree_add_boolean(field_tree, hf_tcp_flags_syn, tvb, offset + 13, 1, th.th_flags);
538     proto_tree_add_boolean(field_tree, hf_tcp_flags_fin, tvb, offset + 13, 1, th.th_flags);
539     proto_tree_add_uint(tcp_tree, hf_tcp_window_size, tvb, offset + 14, 2, th.th_win);
540     if (!pinfo->fragmented && len >= reported_len) {
541       /* The packet isn't part of a fragmented datagram and isn't
542          truncated, so we can checksum it.
543          XXX - make a bigger scatter-gather list once we do fragment
544          reassembly? */
545
546       /* Set up the fields of the pseudo-header. */
547       cksum_vec[0].ptr = pinfo->src.data;
548       cksum_vec[0].len = pinfo->src.len;
549       cksum_vec[1].ptr = pinfo->dst.data;
550       cksum_vec[1].len = pinfo->dst.len;
551       cksum_vec[2].ptr = (const guint8 *)&phdr;
552       switch (pinfo->src.type) {
553
554       case AT_IPv4:
555         phdr[0] = htonl((IP_PROTO_TCP<<16) + reported_len);
556         cksum_vec[2].len = 4;
557         break;
558
559       case AT_IPv6:
560         phdr[0] = htonl(reported_len);
561         phdr[1] = htonl(IP_PROTO_TCP);
562         cksum_vec[2].len = 8;
563         break;
564
565       default:
566         /* TCP runs only atop IPv4 and IPv6.... */
567         g_assert_not_reached();
568         break;
569       }
570       cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
571       cksum_vec[3].len = reported_len;
572       computed_cksum = in_cksum(&cksum_vec[0], 4);
573       if (computed_cksum == 0) {
574         proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
575            offset + 16, 2, th.th_sum, "Checksum: 0x%04x (correct)", th.th_sum);
576       } else {
577         proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
578            offset + 16, 2, th.th_sum,
579            "Checksum: 0x%04x (incorrect, should be 0x%04x)", th.th_sum,
580            in_cksum_shouldbe(th.th_sum, computed_cksum));
581       }
582     } else {
583       proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
584        offset + 16, 2, th.th_sum, "Checksum: 0x%04x", th.th_sum);
585     }
586     if (th.th_flags & TH_URG)
587       proto_tree_add_uint(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th.th_urp);
588   }
589
590   /* Decode TCP options, if any. */
591   if (tree  && hlen > sizeof (e_tcphdr)) {
592     /* There's more than just the fixed-length header.  Decode the
593        options. */
594     optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
595     tf = proto_tree_add_text(tcp_tree, tvb, offset +  20, optlen,
596       "Options: (%d bytes)", optlen);
597     field_tree = proto_item_add_subtree(tf, ett_tcp_options);
598     dissect_ip_tcp_options(tvb, offset + 20, optlen,
599       tcpopts, N_TCP_OPTS, TCPOPT_EOL, pinfo->fd, field_tree);
600   }
601
602   /* Skip over header + options */
603   offset += hlen;
604
605   pinfo->ptype = PT_TCP;
606   pinfo->srcport = th.th_sport;
607   pinfo->destport = th.th_dport;
608   
609   /* Check the packet length to see if there's more data
610      (it could be an ACK-only packet) */
611   length_remaining = tvb_length_remaining(tvb, offset);
612   if (length_remaining != 0) {
613     if (th.th_flags & TH_RST) {
614       /*
615        * RFC1122 says:
616        *
617        *        4.2.2.12  RST Segment: RFC-793 Section 3.4
618        *
619        *          A TCP SHOULD allow a received RST segment to include data.
620        *
621        *          DISCUSSION
622        *               It has been suggested that a RST segment could contain
623        *               ASCII text that encoded and explained the cause of the
624        *               RST.  No standard has yet been established for such
625        *               data.
626        *
627        * so for segments with RST we just display the data as text.
628        */
629       proto_tree_add_text(tcp_tree, tvb, offset, length_remaining,
630                             "Reset cause: %s",
631                             tvb_format_text(tvb, offset, length_remaining));
632     } else
633       decode_tcp_ports( tvb, offset, pinfo, tree, th.th_sport, th.th_dport);
634   }
635  
636   if( data_out_file ) {
637     reassemble_tcp( th.th_seq,          /* sequence number */
638         seglen,                         /* data length */
639         tvb_get_ptr(tvb, offset, length_remaining),     /* data */
640         length_remaining,               /* captured data length */
641         ( th.th_flags & TH_SYN ),       /* is syn set? */
642         &pinfo->net_src,
643         &pinfo->net_dst,
644         pinfo->srcport,
645         pinfo->destport);
646   }
647 }
648
649 void
650 proto_register_tcp(void)
651 {
652         static hf_register_info hf[] = {
653
654                 { &hf_tcp_srcport,
655                 { "Source Port",                "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
656                         "" }},
657
658                 { &hf_tcp_dstport,
659                 { "Destination Port",           "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
660                         "" }},
661
662                 { &hf_tcp_port,
663                 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
664                         "" }},
665
666                 { &hf_tcp_seq,
667                 { "Sequence number",            "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
668                         "" }},
669
670                 { &hf_tcp_nxtseq,
671                 { "Next sequence number",       "tcp.nxtseq", FT_UINT32, BASE_DEC, NULL, 0x0,
672                         "" }},
673
674                 { &hf_tcp_ack,
675                 { "Acknowledgement number",     "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
676                         "" }},
677
678                 { &hf_tcp_hdr_len,
679                 { "Header Length",              "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
680                         "" }},
681
682                 { &hf_tcp_flags,
683                 { "Flags",                      "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
684                         "" }},
685
686                 { &hf_tcp_flags_cwr,
687                 { "Congestion Window Reduced (CWR)",                    "tcp.flags.cwr", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_CWR,
688                         "" }},
689
690                 { &hf_tcp_flags_ecn,
691                 { "ECN-Echo",                   "tcp.flags.ecn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ECN,
692                         "" }},
693
694                 { &hf_tcp_flags_urg,
695                 { "Urgent",                     "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
696                         "" }},
697
698                 { &hf_tcp_flags_ack,
699                 { "Acknowledgment",             "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
700                         "" }},
701
702                 { &hf_tcp_flags_push,
703                 { "Push",                       "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
704                         "" }},
705
706                 { &hf_tcp_flags_reset,
707                 { "Reset",                      "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
708                         "" }},
709
710                 { &hf_tcp_flags_syn,
711                 { "Syn",                        "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
712                         "" }},
713
714                 { &hf_tcp_flags_fin,
715                 { "Fin",                        "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
716                         "" }},
717
718                 { &hf_tcp_window_size,
719                 { "Window size",                "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
720                         "" }},
721
722                 { &hf_tcp_checksum,
723                 { "Checksum",                   "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
724                         "" }},
725
726                 { &hf_tcp_urgent_pointer,
727                 { "Urgent pointer",             "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
728                         "" }},
729         };
730         static gint *ett[] = {
731                 &ett_tcp,
732                 &ett_tcp_flags,
733                 &ett_tcp_options,
734                 &ett_tcp_option_sack,
735         };
736         module_t *tcp_module;
737
738         proto_tcp = proto_register_protocol ("Transmission Control Protocol", "tcp");
739         proto_register_field_array(proto_tcp, hf, array_length(hf));
740         proto_register_subtree_array(ett, array_length(ett));
741
742         /* subdissector code */
743         subdissector_table = register_dissector_table("tcp.port");
744         register_heur_dissector_list("tcp", &heur_subdissector_list);
745
746         /* Register configuration preferences */
747         tcp_module = prefs_register_module("tcp", "TCP", NULL);
748         prefs_register_bool_preference(tcp_module, "tcp_summary_in_tree",
749             "Show TCP summary in protocol tree",
750 "Whether the TCP summary line should be shown in the protocol tree",
751             &tcp_summary_in_tree);
752 }
753
754 void
755 proto_reg_handoff_tcp(void)
756 {
757         dissector_add("ip.proto", IP_PROTO_TCP, dissect_tcp);
758 }