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