Have the IPv6 dissector use the same dissector table as the IPv4
[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.69 2000/04/20 07:05:57 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
43 #include <glib.h>
44 #include "globals.h"
45 #include "resolv.h"
46
47 #include "plugins.h"
48 #include "packet-udp.h"
49
50 #include "packet-ip.h"
51 #include "packet-ncp.h"
52 #include "packet-rpc.h"
53 #include "packet-tftp.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_TFTP    69
77 #define UDP_PORT_VINES  573
78
79 static dissector_table_t udp_dissector_table;
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( const u_char *pd, int offset, frame_data *fd,
87         proto_tree *tree, int uh_sport, int uh_dport){
88   dissector_t sub_dissector;
89
90 /* determine if this packet is part of a conversation and call dissector */
91 /* for the conversation if available */
92
93   sub_dissector = find_conversation_dissector( &pi.src, &pi.dst, PT_UDP,
94                 uh_sport, uh_dport);
95   if (sub_dissector){
96         (sub_dissector)(pd, offset, fd, tree);
97         return;
98   }
99
100   /* ONC RPC.  We can't base this on anything in the UDP header; we have
101      to look at the payload.  If "dissect_rpc()" returns TRUE, it was
102      an RPC packet, otherwise it's some other type of packet. */
103   if (dissect_rpc(pd, offset, fd, tree))
104     return;
105
106   /* try to apply the plugins */
107 #ifdef HAVE_PLUGINS
108   {
109       plugin *pt_plug = plugin_list;
110
111       if (enabled_plugins_number > 0) {
112           while (pt_plug) {
113               if (pt_plug->enabled && !strcmp(pt_plug->protocol, "udp") &&
114                   tree && dfilter_apply(pt_plug->filter, tree, pd, fd->cap_len)) {
115                   pt_plug->dissector(pd, offset, fd, tree);
116                   return;
117               }
118               pt_plug = pt_plug->next;
119           }
120       }
121   }
122 #endif
123
124   /* do lookup with the subdissector table */
125   if (dissector_try_port(udp_dissector_table, uh_sport, pd, offset, fd, tree) ||
126       dissector_try_port(udp_dissector_table, uh_dport, pd, offset, fd, tree))
127     return;
128
129   /* XXX - we should do these with the subdissector table as well. */
130 #define PORT_IS(port)   (uh_sport == port || uh_dport == port)
131   if (PORT_IS(UDP_PORT_VINES)) {
132     /* FIXME: AFAIK, src and dst port must be the same */
133     dissect_vines_frp(pd, offset, fd, tree);
134   } else if (PORT_IS(UDP_PORT_TFTP)) {
135     /* This is the first point of call, but it adds a dynamic call */
136     dissector_add("udp.port", MAX(uh_sport, uh_dport), dissect_tftp);  /* Add to table */
137     dissect_tftp(pd, offset, fd, tree);
138   } else
139     dissect_data(pd, offset, fd, tree);
140 }
141
142
143 static void
144 dissect_udp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
145   e_udphdr  uh;
146   guint16    uh_sport, uh_dport, uh_ulen, uh_sum;
147   proto_tree *udp_tree;
148   proto_item *ti;
149
150   if (!BYTES_ARE_IN_FRAME(offset, sizeof(e_udphdr))) {
151     dissect_data(pd, offset, fd, tree);
152     return;
153   }
154
155   /* Avoids alignment problems on many architectures. */
156   memcpy(&uh, &pd[offset], sizeof(e_udphdr));
157   uh_sport = ntohs(uh.uh_sport);
158   uh_dport = ntohs(uh.uh_dport);
159   uh_ulen  = ntohs(uh.uh_ulen);
160   uh_sum   = ntohs(uh.uh_sum);
161   
162   if (check_col(fd, COL_PROTOCOL))
163     col_add_str(fd, COL_PROTOCOL, "UDP");
164   if (check_col(fd, COL_INFO))
165     col_add_fstr(fd, COL_INFO, "Source port: %s  Destination port: %s",
166             get_udp_port(uh_sport), get_udp_port(uh_dport));
167     
168   if (tree) {
169     ti = proto_tree_add_item(tree, proto_udp, offset, 8);
170     udp_tree = proto_item_add_subtree(ti, ett_udp);
171
172     proto_tree_add_uint_format(udp_tree, hf_udp_srcport, offset, 2, uh_sport,
173         "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
174     proto_tree_add_uint_format(udp_tree, hf_udp_dstport, offset + 2, 2, uh_dport,
175         "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
176
177     proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset, 2, uh_sport);
178     proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset+2, 2, uh_dport);
179
180     proto_tree_add_item(udp_tree, hf_udp_length, offset + 4, 2,  uh_ulen);
181     proto_tree_add_uint_format(udp_tree, hf_udp_checksum, offset + 6, 2, uh_sum,
182         "Checksum: 0x%04x", uh_sum);
183   }
184
185   /* Skip over header */
186   offset += 8;
187
188   pi.ptype = PT_UDP;
189   pi.srcport = uh_sport;
190   pi.destport = uh_dport;
191
192 /* call sub-dissectors */
193   decode_udp_ports( pd, offset, fd, tree, uh_sport, uh_dport);
194
195 }
196
197 void
198 proto_register_udp(void)
199 {
200         static hf_register_info hf[] = {
201                 { &hf_udp_srcport,
202                 { "Source Port",        "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
203                         "" }},
204
205                 { &hf_udp_dstport,
206                 { "Destination Port",   "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
207                         "" }},
208
209                 { &hf_udp_port,
210                 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC,  NULL, 0x0,
211                         "" }},
212
213                 { &hf_udp_length,
214                 { "Length",             "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
215                         "" }},
216
217                 { &hf_udp_checksum,
218                 { "Checksum",           "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
219                         "" }},
220         };
221         static gint *ett[] = {
222                 &ett_udp,
223         };
224
225         proto_udp = proto_register_protocol("User Datagram Protocol", "udp");
226         proto_register_field_array(proto_udp, hf, array_length(hf));
227         proto_register_subtree_array(ett, array_length(ett));
228
229 /* subdissector code */
230         udp_dissector_table = register_dissector_table("udp.port");
231 }
232
233 void
234 proto_reg_handoff_udp(void)
235 {
236         dissector_add("ip.proto", IP_PROTO_UDP, dissect_udp);
237 }