Tvbuffify the Vines dissector, and add protocols for the Vines
[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.82 2001/01/06 08:44:03 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 "plugins.h"
50 #include "packet-udp.h"
51
52 #include "packet-ip.h"
53 #include "conversation.h"
54 #include "packet-vines.h"
55
56 static int proto_udp = -1;              
57 static int hf_udp_srcport = -1;
58 static int hf_udp_dstport = -1;
59 static int hf_udp_port = -1;
60 static int hf_udp_length = -1;
61 static int hf_udp_checksum = -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
77 /* Determine if there is a sub-dissector and call it.  This has been */
78 /* separated into a stand alone routine to other protocol dissectors */
79 /* can call to it, ie. socks    */
80
81 void
82 decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
83         proto_tree *tree, int uh_sport, int uh_dport)
84 {
85   tvbuff_t *next_tvb;
86   const u_char *next_pd;
87   int next_offset;
88
89   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
90
91 /* determine if this packet is part of a conversation and call dissector */
92 /* for the conversation if available */
93
94   if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_UDP,
95                 uh_sport, uh_dport, next_tvb, pinfo, tree))
96         return;
97
98   /* try to apply the plugins */
99 #ifdef HAVE_PLUGINS
100   {
101       plugin *pt_plug = plugin_list;
102
103       if (enabled_plugins_number > 0) {
104           tvb_compat(next_tvb, &next_pd, &next_offset);
105           while (pt_plug) {
106               if (pt_plug->enabled && strstr(pt_plug->protocol, "udp") &&
107                   tree && dfilter_apply(pt_plug->filter, tree, next_pd, pinfo->fd->cap_len)) {
108                   pt_plug->dissector(next_pd, next_offset, pinfo->fd, tree);
109                   return;
110               }
111               pt_plug = pt_plug->next;
112           }
113       }
114   }
115 #endif
116
117   /* do lookup with the subdissector table */
118   if (dissector_try_port(udp_dissector_table, uh_sport, next_tvb, pinfo, tree) ||
119       dissector_try_port(udp_dissector_table, uh_dport, next_tvb, pinfo, tree))
120     return;
121
122   /* do lookup with the heuristic subdissector table */
123   if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
124     return;
125
126   dissect_data(next_tvb, 0, pinfo, tree);
127 }
128
129
130 static void
131 dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
132 {
133   e_udphdr  uh;
134   guint16    uh_sport, uh_dport, uh_ulen, uh_sum;
135   proto_tree *udp_tree;
136   proto_item *ti;
137   guint      len;
138   guint      reported_len;
139   vec_t      cksum_vec[4];
140   guint32    phdr[2];
141   guint16    computed_cksum;
142   int        offset = 0;
143
144   CHECK_DISPLAY_AS_DATA(proto_udp, tvb, pinfo, tree);
145
146   pinfo->current_proto = "UDP";
147
148   /* Avoids alignment problems on many architectures. */
149   tvb_memcpy(tvb, (guint8 *)&uh, offset, sizeof(e_udphdr));
150   uh_sport = ntohs(uh.uh_sport);
151   uh_dport = ntohs(uh.uh_dport);
152   uh_ulen  = ntohs(uh.uh_ulen);
153   uh_sum   = ntohs(uh.uh_sum);
154   
155   if (check_col(pinfo->fd, COL_PROTOCOL))
156     col_set_str(pinfo->fd, COL_PROTOCOL, "UDP");
157   if (check_col(pinfo->fd, COL_INFO))
158     col_add_fstr(pinfo->fd, COL_INFO, "Source port: %s  Destination port: %s",
159             get_udp_port(uh_sport), get_udp_port(uh_dport));
160     
161   if (tree) {
162     ti = proto_tree_add_item(tree, proto_udp, tvb, offset, 8, FALSE);
163     udp_tree = proto_item_add_subtree(ti, ett_udp);
164
165     proto_tree_add_uint_format(udp_tree, hf_udp_srcport, tvb, offset, 2, uh_sport,
166         "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
167     proto_tree_add_uint_format(udp_tree, hf_udp_dstport, tvb, offset + 2, 2, uh_dport,
168         "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
169
170     proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset, 2, uh_sport);
171     proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset+2, 2, uh_dport);
172
173     proto_tree_add_uint(udp_tree, hf_udp_length, tvb, offset + 4, 2,  uh_ulen);
174     reported_len = tvb_reported_length(tvb);
175     len = tvb_length(tvb);
176     if (uh_sum == 0) {
177       /* No checksum supplied in the packet. */
178       proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
179         offset + 6, 2, uh_sum, "Checksum: 0x%04x (none)", uh_sum);
180     } else if (!pinfo->fragmented && len >= reported_len) {
181       /* The packet isn't part of a fragmented datagram and isn't
182          truncated, so we can checksum it.
183          XXX - make a bigger scatter-gather list once we do fragment
184          reassembly? */
185
186       /* Set up the fields of the pseudo-header. */
187       cksum_vec[0].ptr = pinfo->src.data;
188       cksum_vec[0].len = pinfo->src.len;
189       cksum_vec[1].ptr = pinfo->dst.data;
190       cksum_vec[1].len = pinfo->dst.len;
191       cksum_vec[2].ptr = (const guint8 *)&phdr;
192       switch (pinfo->src.type) {
193
194       case AT_IPv4:
195         phdr[0] = htonl((IP_PROTO_UDP<<16) + reported_len);
196         cksum_vec[2].len = 4;
197         break;
198
199       case AT_IPv6:
200         phdr[0] = htonl(reported_len);
201         phdr[1] = htonl(IP_PROTO_UDP);
202         cksum_vec[2].len = 8;
203         break;
204
205       default:
206         /* TCP runs only atop IPv4 and IPv6.... */
207         g_assert_not_reached();
208         break;
209       }
210       cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
211       cksum_vec[3].len = reported_len;
212       computed_cksum = in_cksum(&cksum_vec[0], 4);
213       if (computed_cksum == 0) {
214         proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
215           offset + 6, 2, uh_sum, "Checksum: 0x%04x (correct)", uh_sum);
216       } else {
217         proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
218           offset + 6, 2, uh_sum,
219           "Checksum: 0x%04x (incorrect, should be 0x%04x)", uh_sum,
220            in_cksum_shouldbe(uh_sum, computed_cksum));
221       }
222     } else {
223       proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
224         offset + 6, 2, uh_sum, "Checksum: 0x%04x", uh_sum);
225     }
226   }
227
228   /* Skip over header */
229   offset += 8;
230
231   pinfo->ptype = PT_UDP;
232   pinfo->srcport = uh_sport;
233   pinfo->destport = uh_dport;
234
235 /* call sub-dissectors */
236   decode_udp_ports( tvb, offset, pinfo, tree, uh_sport, uh_dport);
237
238 }
239
240 void
241 proto_register_udp(void)
242 {
243         static hf_register_info hf[] = {
244                 { &hf_udp_srcport,
245                 { "Source Port",        "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
246                         "" }},
247
248                 { &hf_udp_dstport,
249                 { "Destination Port",   "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
250                         "" }},
251
252                 { &hf_udp_port,
253                 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC,  NULL, 0x0,
254                         "" }},
255
256                 { &hf_udp_length,
257                 { "Length",             "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
258                         "" }},
259
260                 { &hf_udp_checksum,
261                 { "Checksum",           "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
262                         "" }},
263         };
264         static gint *ett[] = {
265                 &ett_udp,
266         };
267
268         proto_udp = proto_register_protocol("User Datagram Protocol",
269             "UDP", "udp");
270         proto_register_field_array(proto_udp, hf, array_length(hf));
271         proto_register_subtree_array(ett, array_length(ett));
272
273 /* subdissector code */
274         udp_dissector_table = register_dissector_table("udp.port");
275         register_heur_dissector_list("udp", &heur_subdissector_list);
276 }
277
278 void
279 proto_reg_handoff_udp(void)
280 {
281         dissector_add("ip.proto", IP_PROTO_UDP, dissect_udp);
282 }