f06d9e91b6210a8943036b35621d705fd270a357
[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.25 1999/10/02 16:58:41 deniel 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
43 #include <glib.h>
44 #include "packet.h"
45 #include "resolv.h"
46
47 int proto_udp = -1;             
48 int hf_udp_srcport = -1;
49 int hf_udp_dstport = -1;
50 int hf_udp_port = -1;
51 int hf_udp_length = -1;
52 int hf_udp_checksum = -1;
53
54 /* UDP structs and definitions */
55
56 typedef struct _e_udphdr {
57   guint16 uh_sport;
58   guint16 uh_dport;
59   guint16 uh_ulen;
60   guint16 uh_sum;
61 } e_udphdr;
62
63 /* UDP Ports -> should go in packet-udp.h */
64
65 #define UDP_PORT_DNS     53
66 #define UDP_PORT_BOOTPS  67
67 #define UDP_PORT_TFTP    69
68 #define UDP_PORT_IPX    213
69 #define UDP_PORT_NBNS   137
70 #define UDP_PORT_NBDGM  138
71 #define UDP_PORT_SNMP   161
72 #define UDP_PORT_ISAKMP 500
73 #define UDP_PORT_RIP    520
74 #define UDP_PORT_VINES  573
75 #define UDP_PORT_RADIUS 1645
76 #define UDP_PORT_RADIUS_NEW 1812
77 #define UDP_PORT_RADACCT 1646
78 #define UDP_PORT_RADACCT_NEW 1813
79 #define UDP_PORT_ICP    3130
80
81 struct hash_struct {
82   guint16 proto;
83   void (*dissect)(const u_char *, int, frame_data *, proto_tree *);
84   struct hash_struct *next;
85 };
86
87 static struct hash_struct *hash_table[256];
88
89 /*
90  * These routines are for UDP, will be generalized soon: RJS
91  *
92  * XXX - note that they should probably check the IP address as well as
93  * the port number, so that we don't mistakenly identify packets as, say,
94  * TFTP, merely because they have a source or destination port number
95  * equal to the port being used by a TFTP daemon on some machine other
96  * than the one they're going to or from.
97  */
98
99 struct hash_struct *udp_find_hash_ent(guint16 proto) {
100
101   int idx = proto % 256;
102   struct hash_struct *hash_ent = hash_table[idx];
103
104   while (hash_ent != NULL) {
105
106     if (hash_ent -> proto == proto)
107       return hash_ent;
108   
109     hash_ent = hash_ent -> next;
110
111   }
112
113   return NULL;
114
115 }
116
117 void udp_hash_add(guint16 proto,
118         void (*dissect)(const u_char *, int, frame_data *, proto_tree *)) {
119
120   int idx = proto % 256;   /* Simply take the remainder, hope for no collisions */
121   struct hash_struct *hash_ent = (struct hash_struct *)malloc(sizeof(struct hash_struct));
122   struct hash_struct *hash_ent2;
123   
124   hash_ent -> proto = proto;
125   hash_ent -> dissect = dissect;
126   hash_ent -> next = NULL;
127
128   if (hash_ent == NULL) {
129
130     fprintf(stderr, "Could not allocate space for hash structure in dissect_udp\n");
131     exit(1);
132   }
133
134   if (hash_table[idx]) {  /* Something, add on end */
135
136     hash_ent2 = hash_table[idx];
137
138     while (hash_ent2 -> next != NULL)
139       hash_ent2 = hash_ent2 -> next;
140
141     hash_ent2 -> next = hash_ent;     /* Bad in pathalogical cases */
142
143   }
144   else {
145
146     hash_table[idx] = hash_ent;
147
148   }
149
150 }
151
152 void init_dissect_udp(void) {
153
154   int i;
155
156   for (i = 0; i < 256; i++) {
157
158     hash_table[i] = NULL;
159
160   }
161
162   /* Now add the protocols we know about */
163
164   udp_hash_add(UDP_PORT_BOOTPS, dissect_bootp);
165   udp_hash_add(UDP_PORT_TFTP, dissect_tftp);
166
167 }
168
169 void
170 dissect_udp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
171   e_udphdr  uh;
172   guint16    uh_sport, uh_dport, uh_ulen, uh_sum;
173   struct hash_struct *dissect_routine = NULL;
174   proto_tree *udp_tree;
175   proto_item *ti;
176
177   if (!BYTES_ARE_IN_FRAME(offset, sizeof(e_udphdr))) {
178     dissect_data(pd, offset, fd, tree);
179     return;
180   }
181
182   /* Avoids alignment problems on many architectures. */
183   memcpy(&uh, &pd[offset], sizeof(e_udphdr));
184   uh_sport = ntohs(uh.uh_sport);
185   uh_dport = ntohs(uh.uh_dport);
186   uh_ulen  = ntohs(uh.uh_ulen);
187   uh_sum   = ntohs(uh.uh_sum);
188   
189   if (check_col(fd, COL_PROTOCOL))
190     col_add_str(fd, COL_PROTOCOL, "UDP");
191   if (check_col(fd, COL_INFO))
192     col_add_fstr(fd, COL_INFO, "Source port: %s  Destination port: %s",
193             get_udp_port(uh_sport), get_udp_port(uh_dport));
194   if (check_col(fd, COL_RES_SRC_PORT))
195     col_add_str(fd, COL_RES_SRC_PORT, get_udp_port(uh_sport));
196   if (check_col(fd, COL_UNRES_SRC_PORT))
197     col_add_fstr(fd, COL_UNRES_SRC_PORT, "%u", uh_sport);
198   if (check_col(fd, COL_RES_DST_PORT))
199     col_add_str(fd, COL_RES_DST_PORT, get_udp_port(uh_dport));
200   if (check_col(fd, COL_UNRES_DST_PORT))
201     col_add_fstr(fd, COL_UNRES_DST_PORT, "%u", uh_dport);
202     
203   if (tree) {
204     ti = proto_tree_add_item(tree, proto_udp, offset, 8);
205     udp_tree = proto_item_add_subtree(ti, ETT_UDP);
206
207     proto_tree_add_item_format(udp_tree, hf_udp_srcport, offset, 2, uh_sport,
208         "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
209     proto_tree_add_item_format(udp_tree, hf_udp_dstport, offset + 2, 2, uh_dport,
210         "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
211
212     proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset, 2, uh_sport);
213     proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset+2, 2, uh_dport);
214
215     proto_tree_add_item(udp_tree, hf_udp_length, offset + 4, 2,  uh_ulen);
216     proto_tree_add_item_format(udp_tree, hf_udp_checksum, offset + 6, 2, uh_sum,
217         "Checksum: 0x%04x", uh_sum);
218   }
219
220   /* Skip over header */
221   offset += 8;
222
223   /* XXX - we should do all of this through the table of ports. */
224 #define PORT_IS(port)   (uh_sport == port || uh_dport == port)
225  if (PORT_IS(UDP_PORT_BOOTPS))
226       dissect_bootp(pd, offset, fd, tree);
227  else if (PORT_IS(UDP_PORT_DNS))
228       dissect_dns(pd, offset, fd, tree);
229  else if (PORT_IS(UDP_PORT_ISAKMP))
230       dissect_isakmp(pd, offset, fd, tree);
231  else if (PORT_IS(UDP_PORT_RIP)) {
232       /* we should check the source port too (RIP: UDP src and dst port 520) */
233       dissect_rip(pd, offset, fd, tree);
234  } else if (PORT_IS(UDP_PORT_NBNS))
235       dissect_nbns(pd, offset, fd, tree);
236  else if (PORT_IS(UDP_PORT_NBDGM))
237       dissect_nbdgm(pd, offset, fd, tree);
238  else if (PORT_IS(UDP_PORT_IPX)) /* RFC 1234 */
239       dissect_ipx(pd, offset, fd, tree);
240 #if defined(HAVE_UCD_SNMP_SNMP_H) || defined(HAVE_SNMP_SNMP_H)
241  else if (PORT_IS(UDP_PORT_SNMP))
242       dissect_snmp(pd, offset, fd, tree);
243 #endif
244  else if (PORT_IS(UDP_PORT_VINES)) {
245       /* FIXME: AFAIK, src and dst port must be the same */
246       dissect_vines_frp(pd, offset, fd, tree);
247  } else if (PORT_IS(UDP_PORT_TFTP)) {
248       /* This is the first point of call, but it adds a dynamic call */
249       udp_hash_add(MAX(uh_sport, uh_dport), dissect_tftp);  /* Add to table */
250       dissect_tftp(pd, offset, fd, tree);
251  } else if (PORT_IS(UDP_PORT_RADIUS) ||
252                 PORT_IS(UDP_PORT_RADACCT) ||
253                 PORT_IS(UDP_PORT_RADIUS_NEW) ||
254                 PORT_IS(UDP_PORT_RADACCT_NEW) ) {
255       dissect_radius(pd, offset, fd, tree);
256  } else if ( PORT_IS(UDP_PORT_ICP)) {
257         dissect_icp(pd,offset,fd,tree);
258  } else {
259       /* OK, find a routine in the table, else use the default */
260
261       if ((dissect_routine = udp_find_hash_ent(uh_sport))) {
262
263         struct hash_struct *dr2 = udp_find_hash_ent(uh_dport);
264
265         if (dr2 == NULL) {  /* Not in the table, add */
266
267           udp_hash_add(uh_dport, dissect_tftp);
268
269         }
270
271         dissect_routine -> dissect(pd, offset, fd, tree);
272       }
273       else if ((dissect_routine = udp_find_hash_ent(uh_dport))) {
274
275         dissect_routine -> dissect(pd, offset, fd, tree);
276
277       }
278       else {
279
280         dissect_data(pd, offset, fd, tree);
281       }
282   }
283 }
284
285 void
286 proto_register_udp(void)
287 {
288         static hf_register_info hf[] = {
289                 { &hf_udp_srcport,
290                 { "Source Port",        "udp.srcport", FT_UINT16, NULL }},
291
292                 { &hf_udp_dstport,
293                 { "Destination Port",   "udp.dstport", FT_UINT16, NULL }},
294
295                 { &hf_udp_port,
296                 { "Source or Destination Port", "udp.port", FT_UINT16, NULL }},
297
298                 { &hf_udp_length,
299                 { "Length",             "udp.length", FT_UINT16, NULL }},
300
301                 { &hf_udp_checksum,
302                 { "Checksum",           "udp.checksum", FT_UINT16, NULL }}
303         };
304
305         proto_udp = proto_register_protocol("User Datagram Protocol", "udp");
306         proto_register_field_array(proto_udp, hf, array_length(hf));
307 }
308