I added the use of the END_OF_FRAME macro in dissect_dns() so that the entire
[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.3 1998/09/17 03:12:28 gerald 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 #include <gtk/gtk.h>
31
32 #include <stdio.h>
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include "ethereal.h"
43 #include "packet.h"
44
45 extern FILE* data_out_file;
46 extern packet_info pi;
47
48 void
49 dissect_tcp(const u_char *pd, int offset, frame_data *fd, GtkTree *tree) {
50   e_tcphdr   th;
51   GtkWidget *tcp_tree, *ti;
52   gchar      flags[64] = "<None>";
53   gchar     *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
54   gint       fpos = 0, i;
55   guint      bpos;
56
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);
67   
68   for (i = 0; i < 6; i++) {
69     bpos = 1 << i;
70     if (th.th_flags & bpos) {
71       if (fpos) {
72         strcpy(&flags[fpos], ", ");
73         fpos += 2;
74       }
75       strcpy(&flags[fpos], fstr[i]);
76       fpos += 3;
77     }
78   }
79   flags[fpos] = '\0';
80   
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);
85   }
86   
87   if (tree) {
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",
95       th.th_seq);
96     add_item_to_tree(tcp_tree, offset +  8, 4, "Acknowledgement number: 0x%08x",
97       th.th_ack);
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",
103       th.th_urp);
104     /* To do: TCP options */
105
106   }
107     /* Skip over header + options */
108         offset += 4 * th.th_off;
109
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);
116                                 break;
117                         default:
118                                 dissect_data(pd, offset, fd, tree);
119                 }
120         }
121  
122         pi.srcport = th.th_sport;
123         pi.destport = th.th_dport;
124         
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 */
131         }
132
133
134 }