Have a heur_dissector_list_t be an opaque handle.
[metze/wireshark/wip.git] / epan / dissectors / packet-turnchannel.c
1 /* packet-turnchannel.c
2  * Routines for TURN channel dissection (TURN negociation is handled
3  * in the STUN2 dissector
4  * Copyright 2008, 8x8 Inc. <petithug@8x8.com>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  *
24  * Please refer to the following specs for protocol detail:
25  * - draft-ietf-behave-rfc3489bis-15
26  * - draft-ietf-mmusic-ice-19
27  * - draft-ietf-behave-nat-behavior-discovery-03
28  * - draft-ietf-behave-turn-07
29  * - draft-ietf-behave-turn-ipv6-03
30  */
31
32 #include "config.h"
33
34 #include <glib.h>
35
36 #include <epan/packet.h>
37 #include <packet-tcp.h>
38
39 void proto_register_turnchannel(void);
40 void proto_reg_handoff_turnchannel(void);
41
42 /* heuristic subdissectors */
43 static heur_dissector_list_t heur_subdissector_list;
44
45 /* data dissector handle */
46 static dissector_handle_t data_handle;
47
48 /* Initialize the protocol and registered fields */
49 static int proto_turnchannel = -1;
50
51 static int hf_turnchannel_id = -1;
52 static int hf_turnchannel_len = -1;
53
54 #define TURNCHANNEL_HDR_LEN     ((guint)4)
55
56
57 /* Initialize the subtree pointers */
58 static gint ett_turnchannel = -1;
59
60 static int
61 dissect_turnchannel_message(tvbuff_t *tvb, packet_info *pinfo,
62                             proto_tree *tree, void *data _U_)
63 {
64         guint   len;
65         guint16 channel_id;
66         guint16 data_len;
67         proto_item *ti;
68         proto_tree *turnchannel_tree;
69         heur_dtbl_entry_t *hdtbl_entry;
70
71         len = tvb_length(tvb);
72         /* First, make sure we have enough data to do the check. */
73         if (len < TURNCHANNEL_HDR_LEN) {
74                   return 0;
75         }
76
77         channel_id = tvb_get_ntohs(tvb, 0);
78         data_len = tvb_get_ntohs(tvb, 2);
79
80         if ((channel_id < 0x4000) || (channel_id > 0xFFFE)) {
81           return 0;
82         }
83
84         if (len != TURNCHANNEL_HDR_LEN + data_len) {
85           return 0;
86         }
87
88         /* Seems to be a decent TURN channel message */
89         col_set_str(pinfo->cinfo, COL_PROTOCOL, "TURN CHANNEL");
90
91         col_add_fstr(pinfo->cinfo, COL_INFO, "Channel Id 0x%x", channel_id);
92
93         ti = proto_tree_add_item(tree, proto_turnchannel, tvb, 0, -1, ENC_NA);
94
95         turnchannel_tree = proto_item_add_subtree(ti, ett_turnchannel);
96
97         proto_tree_add_uint(turnchannel_tree, hf_turnchannel_id, tvb, 0, 2, channel_id);
98         proto_tree_add_uint(turnchannel_tree, hf_turnchannel_len, tvb, 2, 2, data_len);
99
100
101         if (len > TURNCHANNEL_HDR_LEN) {
102           tvbuff_t *next_tvb;
103           guint reported_len, new_len;
104
105           new_len = tvb_length_remaining(tvb, TURNCHANNEL_HDR_LEN);
106           reported_len = tvb_reported_length_remaining(tvb,
107                                                        TURNCHANNEL_HDR_LEN);
108           if (data_len < reported_len) {
109             reported_len = data_len;
110           }
111           next_tvb = tvb_new_subset(tvb, TURNCHANNEL_HDR_LEN, new_len,
112                                     reported_len);
113
114
115           if (!dissector_try_heuristic(heur_subdissector_list,
116                                        next_tvb, pinfo, tree, &hdtbl_entry, NULL)) {
117             call_dissector(data_handle,next_tvb, pinfo, tree);
118           }
119         }
120
121         return tvb_length(tvb);
122 }
123
124 static guint
125 get_turnchannel_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
126 {
127         return (guint)tvb_get_ntohs(tvb, offset+2) + TURNCHANNEL_HDR_LEN;
128 }
129
130 static int
131 dissect_turnchannel_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
132 {
133         tcp_dissect_pdus(tvb, pinfo, tree, TRUE, TURNCHANNEL_HDR_LEN,
134                         get_turnchannel_message_len, dissect_turnchannel_message, data);
135         return tvb_length(tvb);
136 }
137
138
139 static gboolean
140 dissect_turnchannel_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
141 {
142         guint   len;
143         guint16 channel_id;
144         guint16 data_len;
145
146         len = tvb_length(tvb);
147         /* First, make sure we have enough data to do the check. */
148         if (len < TURNCHANNEL_HDR_LEN) {
149                   return FALSE;
150         }
151
152         channel_id = tvb_get_ntohs(tvb, 0);
153         data_len = tvb_get_ntohs(tvb, 2);
154
155         if ((channel_id < 0x4000) || (channel_id > 0xFFFE)) {
156           return FALSE;
157         }
158
159         if (len != TURNCHANNEL_HDR_LEN + data_len) {
160           return FALSE;
161         }
162
163         return dissect_turnchannel_message(tvb, pinfo, tree, NULL);
164 }
165
166 void
167 proto_register_turnchannel(void)
168 {
169         static hf_register_info hf[] = {
170                 { &hf_turnchannel_id,
171                         { "TURN Channel ID",    "turnchannel.id",       FT_UINT16,
172                         BASE_HEX,       NULL,   0x0,    NULL,   HFILL }
173                 },
174                 { &hf_turnchannel_len,
175                         { "Data Length",  "turnchannel.length", FT_UINT16,
176                         BASE_DEC,       NULL,   0x0,    NULL,   HFILL }
177                 },
178         };
179
180 /* Setup protocol subtree array */
181         static gint *ett[] = {
182                 &ett_turnchannel,
183         };
184
185 /* Register the protocol name and description */
186         proto_turnchannel = proto_register_protocol("TURN Channel",
187             "TURNCHANNEL", "turnchannel");
188
189         new_register_dissector("turnchannel", dissect_turnchannel_message,
190                            proto_turnchannel);
191
192 /* subdissectors */
193         heur_subdissector_list = register_heur_dissector_list("turnchannel");
194
195 /* Required function calls to register the header fields and subtrees used */
196         proto_register_field_array(proto_turnchannel, hf, array_length(hf));
197         proto_register_subtree_array(ett, array_length(ett));
198
199 }
200
201
202 void
203 proto_reg_handoff_turnchannel(void)
204 {
205         dissector_handle_t turnchannel_tcp_handle;
206         dissector_handle_t turnchannel_udp_handle;
207
208         turnchannel_tcp_handle = new_create_dissector_handle(dissect_turnchannel_tcp, proto_turnchannel);
209         turnchannel_udp_handle = find_dissector("turnchannel");
210
211         /* Register for "Decode As" in case STUN negotiation isn't captured */
212         dissector_add_for_decode_as("tcp.port", turnchannel_tcp_handle);
213         dissector_add_for_decode_as("udp.port", turnchannel_udp_handle);
214
215         /* TURN negotiation is handled through STUN2 dissector (packet-stun.c),
216            so only it should be able to determine if a packet is a TURN packet */
217         heur_dissector_add("stun", dissect_turnchannel_heur, proto_turnchannel);
218
219         data_handle = find_dissector("data");
220 }
221
222 /*
223  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
224  *
225  * Local variables:
226  * c-basic-offset: 8
227  * tab-width: 8
228  * indent-tabs-mode: t
229  * End:
230  *
231  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
232  * :indentSize=8:tabSize=8:noTabs=false:
233  */