Note that wiring the current longest string in a column into
[obnox/wireshark/wip.git] / packet-udp.c
1 /* packet-udp.c
2  * Routines for UDP packet disassembly
3  *
4  * $Id: packet-udp.c,v 1.9 1998/11/20 05:58:41 gram 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 #include "resolv.h"
45
46 void
47 dissect_udp(const u_char *pd, int offset, frame_data *fd, GtkTree *tree) {
48   e_udphdr  uh;
49   guint16    uh_sport, uh_dport, uh_ulen, uh_sum;
50   GtkWidget *udp_tree, *ti;
51
52   /* To do: Check for {cap len,pkt len} < struct len */
53   /* Avoids alignment problems on many architectures. */
54   memcpy(&uh, &pd[offset], sizeof(e_udphdr));
55   uh_sport = ntohs(uh.uh_sport);
56   uh_dport = ntohs(uh.uh_dport);
57   uh_ulen  = ntohs(uh.uh_ulen);
58   uh_sum   = ntohs(uh.uh_sum);
59   
60   if (check_col(fd, COL_PROTOCOL))
61     col_add_str(fd, COL_PROTOCOL, "UDP");
62   if (check_col(fd, COL_INFO))
63     col_add_fstr(fd, COL_INFO, "Source port: %s  Destination port: %s",
64             get_udp_port(uh_sport), get_udp_port(uh_dport));
65   if (check_col(fd, COL_RES_SRC_PORT))
66     col_add_str(fd, COL_RES_SRC_PORT, get_udp_port(uh_sport));
67   if (check_col(fd, COL_UNRES_SRC_PORT))
68     col_add_fstr(fd, COL_UNRES_SRC_PORT, "%d", uh_sport);
69   if (check_col(fd, COL_RES_DST_PORT))
70     col_add_str(fd, COL_RES_DST_PORT, get_udp_port(uh_dport));
71   if (check_col(fd, COL_UNRES_DST_PORT))
72     col_add_fstr(fd, COL_UNRES_DST_PORT, "%d", uh_dport);
73     
74   if (tree) {
75     ti = add_item_to_tree(GTK_WIDGET(tree), offset, 8,
76       "User Datagram Protocol");
77     udp_tree = gtk_tree_new();
78     add_subtree(ti, udp_tree, ETT_UDP);
79     add_item_to_tree(udp_tree, offset,     2, "Source port: %s", get_udp_port(uh_sport));
80     add_item_to_tree(udp_tree, offset + 2, 2, "Destination port: %s", get_udp_port(uh_dport));
81     add_item_to_tree(udp_tree, offset + 4, 2, "Length: %d", uh_ulen);
82     add_item_to_tree(udp_tree, offset + 6, 2, "Checksum: 0x%04x", uh_sum);
83   }
84
85   /* Skip over header */
86   offset += 8;
87
88   /* To do: make sure we aren't screwing ourselves with the MIN call. */
89   switch (MIN(uh_sport, uh_dport)) {
90     case UDP_PORT_BOOTPS:
91       dissect_bootp(pd, offset, fd, tree);
92       break;
93     case UDP_PORT_DNS:
94       dissect_dns(pd, offset, fd, tree);
95       break;
96     case UDP_PORT_RIP:
97       /* we should check the source port too (RIP: UDP src and dst port 520) */
98       dissect_rip(pd, offset, fd, tree);
99       break;
100         case UDP_PORT_NBNS:
101           dissect_nbns(pd, offset, fd, tree);
102           break;
103         case UDP_PORT_NBDGM:
104           dissect_nbdgm(pd, offset, fd, tree);
105           break;
106     case UDP_PORT_IPX: /* RFC 1234 */
107       dissect_ipx(pd, offset, fd, tree);
108       break;
109     default:
110       dissect_data(pd, offset, fd, tree);
111   }
112 }