Use "proto_tree_add_boolean_hidden()", not
[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.89 2001/03/28 21:33:31 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * Richard Sharpe, 13-Feb-1999, added dispatch table support and 
11  *                              support for tftp.
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27  
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include <glib.h>
45 #include "globals.h"
46 #include "resolv.h"
47 #include "in_cksum.h"
48
49 #include "packet-udp.h"
50
51 #include "packet-ip.h"
52 #include "conversation.h"
53 #include "packet-vines.h"
54
55 static int proto_udp = -1;              
56 static int hf_udp_srcport = -1;
57 static int hf_udp_dstport = -1;
58 static int hf_udp_port = -1;
59 static int hf_udp_length = -1;
60 static int hf_udp_checksum = -1;
61 static int hf_udp_checksum_bad = -1;
62
63 static gint ett_udp = -1;
64
65 /* UDP structs and definitions */
66
67 typedef struct _e_udphdr {
68   guint16 uh_sport;
69   guint16 uh_dport;
70   guint16 uh_ulen;
71   guint16 uh_sum;
72 } e_udphdr;
73
74 static dissector_table_t udp_dissector_table;
75 static heur_dissector_list_t heur_subdissector_list;
76 static conv_dissector_list_t conv_subdissector_list;
77
78 /* Determine if there is a sub-dissector and call it.  This has been */
79 /* separated into a stand alone routine to other protocol dissectors */
80 /* can call to it, ie. socks    */
81
82 void
83 decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
84         proto_tree *tree, int uh_sport, int uh_dport)
85 {
86   tvbuff_t *next_tvb;
87
88   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
89
90 /* determine if this packet is part of a conversation and call dissector */
91 /* for the conversation if available */
92
93   if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_UDP,
94                 uh_sport, uh_dport, next_tvb, pinfo, tree))
95     return;
96
97   /* do lookup with the subdissector table */
98   if (dissector_try_port(udp_dissector_table, uh_sport, next_tvb, pinfo, tree) ||
99       dissector_try_port(udp_dissector_table, uh_dport, next_tvb, pinfo, tree))
100     return;
101
102   /* do lookup with the heuristic subdissector table */
103   if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
104     return;
105
106   dissect_data(next_tvb, 0, pinfo, tree);
107 }
108
109
110 static void
111 dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
112 {
113   e_udphdr  uh;
114   guint16    uh_sport, uh_dport, uh_ulen, uh_sum;
115   proto_tree *udp_tree;
116   proto_item *ti;
117   guint      len;
118   guint      reported_len;
119   vec_t      cksum_vec[4];
120   guint32    phdr[2];
121   guint16    computed_cksum;
122   int        offset = 0;
123
124   if (check_col(pinfo->fd, COL_PROTOCOL))
125     col_set_str(pinfo->fd, COL_PROTOCOL, "UDP");
126   if (check_col(pinfo->fd, COL_INFO))
127     col_clear(pinfo->fd, COL_INFO);
128
129   /* Avoids alignment problems on many architectures. */
130   tvb_memcpy(tvb, (guint8 *)&uh, offset, sizeof(e_udphdr));
131   uh_sport = ntohs(uh.uh_sport);
132   uh_dport = ntohs(uh.uh_dport);
133   uh_ulen  = ntohs(uh.uh_ulen);
134   uh_sum   = ntohs(uh.uh_sum);
135   
136   if (check_col(pinfo->fd, COL_INFO))
137     col_add_fstr(pinfo->fd, COL_INFO, "Source port: %s  Destination port: %s",
138             get_udp_port(uh_sport), get_udp_port(uh_dport));
139     
140   if (tree) {
141     ti = proto_tree_add_item(tree, proto_udp, tvb, offset, 8, FALSE);
142     udp_tree = proto_item_add_subtree(ti, ett_udp);
143
144     proto_tree_add_uint_format(udp_tree, hf_udp_srcport, tvb, offset, 2, uh_sport,
145         "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
146     proto_tree_add_uint_format(udp_tree, hf_udp_dstport, tvb, offset + 2, 2, uh_dport,
147         "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
148
149     proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset, 2, uh_sport);
150     proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset+2, 2, uh_dport);
151
152     proto_tree_add_uint(udp_tree, hf_udp_length, tvb, offset + 4, 2,  uh_ulen);
153     reported_len = tvb_reported_length(tvb);
154     len = tvb_length(tvb);
155     if (uh_sum == 0) {
156       /* No checksum supplied in the packet. */
157       proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
158         offset + 6, 2, uh_sum, "Checksum: 0x%04x (none)", uh_sum);
159     } else if (!pinfo->fragmented && len >= reported_len) {
160       /* The packet isn't part of a fragmented datagram and isn't
161          truncated, so we can checksum it.
162          XXX - make a bigger scatter-gather list once we do fragment
163          reassembly? */
164
165       /* Set up the fields of the pseudo-header. */
166       cksum_vec[0].ptr = pinfo->src.data;
167       cksum_vec[0].len = pinfo->src.len;
168       cksum_vec[1].ptr = pinfo->dst.data;
169       cksum_vec[1].len = pinfo->dst.len;
170       cksum_vec[2].ptr = (const guint8 *)&phdr;
171       switch (pinfo->src.type) {
172
173       case AT_IPv4:
174         phdr[0] = htonl((IP_PROTO_UDP<<16) + reported_len);
175         cksum_vec[2].len = 4;
176         break;
177
178       case AT_IPv6:
179         phdr[0] = htonl(reported_len);
180         phdr[1] = htonl(IP_PROTO_UDP);
181         cksum_vec[2].len = 8;
182         break;
183
184       default:
185         /* TCP runs only atop IPv4 and IPv6.... */
186         g_assert_not_reached();
187         break;
188       }
189       cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
190       cksum_vec[3].len = reported_len;
191       computed_cksum = in_cksum(&cksum_vec[0], 4);
192       if (computed_cksum == 0) {
193         proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
194           offset + 6, 2, uh_sum, "Checksum: 0x%04x (correct)", uh_sum);
195       } else {
196         proto_tree_add_boolean_hidden(udp_tree, hf_udp_checksum_bad, tvb,
197            offset + 6, 2, TRUE);
198         proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
199           offset + 6, 2, uh_sum,
200           "Checksum: 0x%04x (incorrect, should be 0x%04x)", uh_sum,
201            in_cksum_shouldbe(uh_sum, computed_cksum));
202       }
203     } else {
204       proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
205         offset + 6, 2, uh_sum, "Checksum: 0x%04x", uh_sum);
206     }
207   }
208
209   /* Skip over header */
210   offset += 8;
211
212   pinfo->ptype = PT_UDP;
213   pinfo->srcport = uh_sport;
214   pinfo->destport = uh_dport;
215
216 /* call sub-dissectors */
217   decode_udp_ports( tvb, offset, pinfo, tree, uh_sport, uh_dport);
218
219 }
220
221 void
222 proto_register_udp(void)
223 {
224         static hf_register_info hf[] = {
225                 { &hf_udp_srcport,
226                 { "Source Port",        "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
227                         "" }},
228
229                 { &hf_udp_dstport,
230                 { "Destination Port",   "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
231                         "" }},
232
233                 { &hf_udp_port,
234                 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC,  NULL, 0x0,
235                         "" }},
236
237                 { &hf_udp_length,
238                 { "Length",             "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
239                         "" }},
240
241                 { &hf_udp_checksum_bad,
242                 { "Bad Checksum",       "udp.checksum_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
243                         "" }},
244
245                 { &hf_udp_checksum,
246                 { "Checksum",           "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
247                         "" }},
248         };
249         static gint *ett[] = {
250                 &ett_udp,
251         };
252
253         proto_udp = proto_register_protocol("User Datagram Protocol",
254             "UDP", "udp");
255         proto_register_field_array(proto_udp, hf, array_length(hf));
256         proto_register_subtree_array(ett, array_length(ett));
257
258 /* subdissector code */
259         udp_dissector_table = register_dissector_table("udp.port");
260         register_heur_dissector_list("udp", &heur_subdissector_list);
261         register_conv_dissector_list("udp", &conv_subdissector_list);
262 }
263
264 void
265 proto_reg_handoff_udp(void)
266 {
267         dissector_add("ip.proto", IP_PROTO_UDP, dissect_udp, proto_udp);
268 }