2 * Routines for TCP packet disassembly
4 * $Id: packet-tcp.c,v 1.3 1998/09/17 03:12:28 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>
45 extern FILE* data_out_file;
46 extern packet_info pi;
49 dissect_tcp(const u_char *pd, int offset, frame_data *fd, GtkTree *tree) {
51 GtkWidget *tcp_tree, *ti;
52 gchar flags[64] = "<None>";
53 gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
57 /* To do: Check for {cap len,pkt len} < struct len */
58 /* Avoids alignment problems on many architectures. */
59 memcpy(&th, &pd[offset], sizeof(e_tcphdr));
60 th.th_sport = ntohs(th.th_sport);
61 th.th_dport = ntohs(th.th_dport);
62 th.th_win = ntohs(th.th_win);
63 th.th_sum = ntohs(th.th_sum);
64 th.th_urp = ntohs(th.th_urp);
65 th.th_seq = ntohl(th.th_seq);
66 th.th_ack = ntohl(th.th_ack);
68 for (i = 0; i < 6; i++) {
70 if (th.th_flags & bpos) {
72 strcpy(&flags[fpos], ", ");
75 strcpy(&flags[fpos], fstr[i]);
81 if (fd->win_info[0]) {
82 strcpy(fd->win_info[3], "TCP");
83 sprintf(fd->win_info[4], "Source port: %d Destination port: %d",
84 th.th_sport, th.th_dport);
88 ti = add_item_to_tree(GTK_WIDGET(tree), offset, 20,
89 "Transmission Control Protocol");
90 tcp_tree = gtk_tree_new();
91 add_subtree(ti, tcp_tree, ETT_TCP);
92 add_item_to_tree(tcp_tree, offset, 2, "Source port: %d", th.th_sport);
93 add_item_to_tree(tcp_tree, offset + 2, 2, "Destination port: %d", th.th_dport);
94 add_item_to_tree(tcp_tree, offset + 4, 4, "Sequence number: 0x%08x",
96 add_item_to_tree(tcp_tree, offset + 8, 4, "Acknowledgement number: 0x%08x",
98 add_item_to_tree(tcp_tree, offset + 12, 1, "Header length: %d", th.th_off);
99 add_item_to_tree(tcp_tree, offset + 13, 1, "Flags: %s", flags);
100 add_item_to_tree(tcp_tree, offset + 14, 2, "Window size: %d", th.th_win);
101 add_item_to_tree(tcp_tree, offset + 16, 2, "Checksum: 0x%04x", th.th_sum);
102 add_item_to_tree(tcp_tree, offset + 18, 2, "Urgent pointer: 0x%04x",
104 /* To do: TCP options */
107 /* Skip over header + options */
108 offset += 4 * th.th_off;
110 /* until we decode those options, I'll check the packet length
111 to see if there's more data. -- gilbert */
112 if (fd->cap_len > offset) {
113 switch(MIN(th.th_sport, th.th_dport)) {
114 case TCP_PORT_PRINTER:
115 dissect_lpd(pd, offset, fd, tree);
118 dissect_data(pd, offset, fd, tree);
122 pi.srcport = th.th_sport;
123 pi.destport = th.th_dport;
125 if( data_out_file ) {
126 reassemble_tcp( th.th_seq, /* sequence number */
127 ( pi.iplen -( pi.iphdrlen * 4 )-( th.th_off * 4 ) ), /* length */
128 ( pd+offset ), /* data */
129 ( th.th_flags & 0x02 ), /* is syn set? */
130 pi.ip_src ); /* src ip */