2 * Routines for TCP packet disassembly
4 * $Id: packet-tcp.c,v 1.9 1998/11/18 03:01:37 gerald Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
47 extern FILE* data_out_file;
48 extern packet_info pi;
51 dissect_tcpopt_maxseg(GtkWidget *opt_tree, const char *name, const u_char *opd,
52 int offset, guint optlen)
54 add_item_to_tree(opt_tree, offset, optlen,
55 "%s: %u bytes", name, pntohs(opd));
59 dissect_tcpopt_wscale(GtkWidget *opt_tree, const char *name, const u_char *opd,
60 int offset, guint optlen)
62 add_item_to_tree(opt_tree, offset, optlen,
63 "%s: %u bytes", name, *opd);
67 dissect_tcpopt_sack(GtkWidget *opt_tree, const char *name, const u_char *opd,
68 int offset, guint optlen)
70 GtkWidget *field_tree = NULL, *tf;
71 guint leftedge, rightedge;
73 tf = add_item_to_tree(opt_tree, offset, optlen, "%s:", name);
74 offset += 2; /* skip past type and length */
75 optlen -= 2; /* subtract size of type and length */
77 if (field_tree == NULL) {
78 /* Haven't yet made a subtree out of this option. Do so. */
79 field_tree = gtk_tree_new();
80 add_subtree(tf, field_tree, ETT_TCP_OPTION_SACK);
83 add_item_to_tree(field_tree, offset, optlen,
84 "(suboption would go past end of option)");
87 /* XXX - check whether it goes past end of packet */
88 leftedge = pntohl(opd);
92 add_item_to_tree(field_tree, offset, optlen,
93 "(suboption would go past end of option)");
96 /* XXX - check whether it goes past end of packet */
97 rightedge = pntohl(opd);
100 add_item_to_tree(field_tree, offset, 8,
101 "left edge = %u, right edge = %u", leftedge, rightedge);
107 dissect_tcpopt_echo(GtkWidget *opt_tree, const char *name, const u_char *opd,
108 int offset, guint optlen)
110 add_item_to_tree(opt_tree, offset, optlen,
111 "%s: %u", name, pntohl(opd));
115 dissect_tcpopt_timestamp(GtkWidget *opt_tree, const char *name,
116 const u_char *opd, int offset, guint optlen)
118 add_item_to_tree(opt_tree, offset, optlen,
119 "%s: tsval %u, tsecr %u", name, pntohl(opd), pntohl(opd + 4));
123 dissect_tcpopt_cc(GtkWidget *opt_tree, const char *name, const u_char *opd,
124 int offset, guint optlen)
126 add_item_to_tree(opt_tree, offset, optlen,
127 "%s: %u", name, pntohl(opd));
130 static ip_tcp_opt tcpopts[] = {
147 "Maximum segment size",
150 dissect_tcpopt_maxseg
157 dissect_tcpopt_wscale
192 dissect_tcpopt_timestamp
217 #define N_TCP_OPTS (sizeof tcpopts / sizeof tcpopts[0])
220 dissect_tcp(const u_char *pd, int offset, frame_data *fd, GtkTree *tree) {
222 GtkWidget *tcp_tree, *ti, *field_tree, *tf;
223 gchar flags[64] = "<None>";
224 gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
230 /* To do: Check for {cap len,pkt len} < struct len */
231 /* Avoids alignment problems on many architectures. */
232 memcpy(&th, &pd[offset], sizeof(e_tcphdr));
233 th.th_sport = ntohs(th.th_sport);
234 th.th_dport = ntohs(th.th_dport);
235 th.th_win = ntohs(th.th_win);
236 th.th_sum = ntohs(th.th_sum);
237 th.th_urp = ntohs(th.th_urp);
238 th.th_seq = ntohl(th.th_seq);
239 th.th_ack = ntohl(th.th_ack);
241 for (i = 0; i < 6; i++) {
243 if (th.th_flags & bpos) {
245 strcpy(&flags[fpos], ", ");
248 strcpy(&flags[fpos], fstr[i]);
254 if (check_col(fd, COL_PROTOCOL))
255 col_add_str(fd, COL_PROTOCOL, "TCP");
256 if (check_col(fd, COL_INFO))
257 col_add_fstr(fd, COL_INFO, "Source port: %s Destination port: %s",
258 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport));
259 if (check_col(fd, COL_RES_SRC_PORT))
260 col_add_str(fd, COL_RES_SRC_PORT, get_tcp_port(th.th_sport));
261 if (check_col(fd, COL_UNRES_SRC_PORT))
262 col_add_fstr(fd, COL_UNRES_SRC_PORT, "%d", th.th_sport);
263 if (check_col(fd, COL_RES_DST_PORT))
264 col_add_str(fd, COL_RES_DST_PORT, get_tcp_port(th.th_dport));
265 if (check_col(fd, COL_UNRES_DST_PORT))
266 col_add_fstr(fd, COL_UNRES_DST_PORT, "%d", th.th_dport);
268 hlen = th.th_off * 4; /* TCP header length, in bytes */
271 ti = add_item_to_tree(GTK_WIDGET(tree), offset, hlen,
272 "Transmission Control Protocol");
273 tcp_tree = gtk_tree_new();
274 add_subtree(ti, tcp_tree, ETT_TCP);
275 add_item_to_tree(tcp_tree, offset, 2, "Source port: %d", th.th_sport);
276 add_item_to_tree(tcp_tree, offset + 2, 2, "Destination port: %d", th.th_dport);
277 add_item_to_tree(tcp_tree, offset + 4, 4, "Sequence number: %u",
279 if (th.th_flags & TH_ACK)
280 add_item_to_tree(tcp_tree, offset + 8, 4, "Acknowledgement number: %u",
282 add_item_to_tree(tcp_tree, offset + 12, 1, "Header length: %d bytes", hlen);
283 add_item_to_tree(tcp_tree, offset + 13, 1, "Flags: %s", flags);
284 add_item_to_tree(tcp_tree, offset + 14, 2, "Window size: %d", th.th_win);
285 add_item_to_tree(tcp_tree, offset + 16, 2, "Checksum: 0x%04x", th.th_sum);
286 if (th.th_flags & TH_URG)
287 add_item_to_tree(tcp_tree, offset + 18, 2, "Urgent pointer: 0x%04x",
290 /* Decode TCP options, if any. */
291 if (hlen > sizeof (e_tcphdr)) {
292 /* There's more than just the fixed-length header. Decode the
294 optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
295 tf = add_item_to_tree(tcp_tree, offset + 20, optlen,
296 "Options: (%d bytes)", optlen);
297 field_tree = gtk_tree_new();
298 add_subtree(tf, field_tree, ETT_TCP_OPTIONS);
299 dissect_ip_tcp_options(field_tree, &pd[offset + 20], offset + 20, optlen,
300 tcpopts, N_TCP_OPTS, TCPOPT_EOL);
304 /* Skip over header + options */
307 /* until we decode those options, I'll check the packet length
308 to see if there's more data. -- gilbert */
309 if (fd->cap_len > offset) {
310 switch(MIN(th.th_sport, th.th_dport)) {
311 case TCP_PORT_PRINTER:
312 dissect_lpd(pd, offset, fd, tree);
315 /* check existence of high level protocols */
317 if (memcmp(&pd[offset], "GIOP", 4) == 0) {
318 dissect_giop(pd, offset, fd, tree);
321 dissect_data(pd, offset, fd, tree);
326 pi.srcport = th.th_sport;
327 pi.destport = th.th_dport;
329 if( data_out_file ) {
330 reassemble_tcp( th.th_seq, /* sequence number */
331 ( pi.iplen -( pi.iphdrlen * 4 )-( th.th_off * 4 ) ), /* length */
332 ( pd+offset ), /* data */
333 ( th.th_flags & 0x02 ), /* is syn set? */
334 pi.ip_src ); /* src ip */