new_create_dissector_handle -> create_dissector_handle for dissector directory.
[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 <epan/packet.h>
35 #include "packet-tcp.h"
36
37 void proto_register_turnchannel(void);
38 void proto_reg_handoff_turnchannel(void);
39
40 /* heuristic subdissectors */
41 static heur_dissector_list_t heur_subdissector_list;
42
43 /* data dissector handle */
44 static dissector_handle_t data_handle;
45
46 /* Initialize the protocol and registered fields */
47 static int proto_turnchannel = -1;
48
49 static int hf_turnchannel_id = -1;
50 static int hf_turnchannel_len = -1;
51
52 #define TURNCHANNEL_HDR_LEN     ((guint)4)
53
54
55 /* Initialize the subtree pointers */
56 static gint ett_turnchannel = -1;
57
58 static int
59 dissect_turnchannel_message(tvbuff_t *tvb, packet_info *pinfo,
60                             proto_tree *tree, void *data _U_)
61 {
62         guint   len;
63         guint16 channel_id;
64         guint16 data_len;
65         proto_item *ti;
66         proto_tree *turnchannel_tree;
67         heur_dtbl_entry_t *hdtbl_entry;
68
69         len = tvb_captured_length(tvb);
70         /* First, make sure we have enough data to do the check. */
71         if (len < TURNCHANNEL_HDR_LEN) {
72                   return 0;
73         }
74
75         channel_id = tvb_get_ntohs(tvb, 0);
76         data_len = tvb_get_ntohs(tvb, 2);
77
78         if ((channel_id < 0x4000) || (channel_id > 0xFFFE)) {
79           return 0;
80         }
81
82         if (len != TURNCHANNEL_HDR_LEN + data_len) {
83           return 0;
84         }
85
86         /* Seems to be a decent TURN channel message */
87         col_set_str(pinfo->cinfo, COL_PROTOCOL, "TURN CHANNEL");
88
89         col_add_fstr(pinfo->cinfo, COL_INFO, "Channel Id 0x%x", channel_id);
90
91         ti = proto_tree_add_item(tree, proto_turnchannel, tvb, 0, -1, ENC_NA);
92
93         turnchannel_tree = proto_item_add_subtree(ti, ett_turnchannel);
94
95         proto_tree_add_uint(turnchannel_tree, hf_turnchannel_id, tvb, 0, 2, channel_id);
96         proto_tree_add_uint(turnchannel_tree, hf_turnchannel_len, tvb, 2, 2, data_len);
97
98
99         if (len > TURNCHANNEL_HDR_LEN) {
100           tvbuff_t *next_tvb;
101           guint reported_len, new_len;
102
103           new_len = tvb_captured_length_remaining(tvb, TURNCHANNEL_HDR_LEN);
104           reported_len = tvb_reported_length_remaining(tvb,
105                                                        TURNCHANNEL_HDR_LEN);
106           if (data_len < reported_len) {
107             reported_len = data_len;
108           }
109           next_tvb = tvb_new_subset(tvb, TURNCHANNEL_HDR_LEN, new_len,
110                                     reported_len);
111
112
113           if (!dissector_try_heuristic(heur_subdissector_list,
114                                        next_tvb, pinfo, tree, &hdtbl_entry, NULL)) {
115             call_dissector(data_handle,next_tvb, pinfo, tree);
116           }
117         }
118
119         return tvb_captured_length(tvb);
120 }
121
122 static guint
123 get_turnchannel_message_len(packet_info *pinfo _U_, tvbuff_t *tvb,
124                             int offset, void *data _U_)
125 {
126         return (guint)tvb_get_ntohs(tvb, offset+2) + TURNCHANNEL_HDR_LEN;
127 }
128
129 static int
130 dissect_turnchannel_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
131 {
132         tcp_dissect_pdus(tvb, pinfo, tree, TRUE, TURNCHANNEL_HDR_LEN,
133                         get_turnchannel_message_len, dissect_turnchannel_message, data);
134         return tvb_captured_length(tvb);
135 }
136
137
138 static gboolean
139 dissect_turnchannel_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
140 {
141         guint   len;
142         guint16 channel_id;
143         guint16 data_len;
144
145         len = tvb_captured_length(tvb);
146         /* First, make sure we have enough data to do the check. */
147         if (len < TURNCHANNEL_HDR_LEN) {
148                   return FALSE;
149         }
150
151         channel_id = tvb_get_ntohs(tvb, 0);
152         data_len = tvb_get_ntohs(tvb, 2);
153
154         if ((channel_id < 0x4000) || (channel_id > 0xFFFE)) {
155           return FALSE;
156         }
157
158         if (len != TURNCHANNEL_HDR_LEN + data_len) {
159           return FALSE;
160         }
161
162         return dissect_turnchannel_message(tvb, pinfo, tree, NULL);
163 }
164
165 void
166 proto_register_turnchannel(void)
167 {
168         static hf_register_info hf[] = {
169                 { &hf_turnchannel_id,
170                         { "TURN Channel ID",    "turnchannel.id",       FT_UINT16,
171                         BASE_HEX,       NULL,   0x0,    NULL,   HFILL }
172                 },
173                 { &hf_turnchannel_len,
174                         { "Data Length",  "turnchannel.length", FT_UINT16,
175                         BASE_DEC,       NULL,   0x0,    NULL,   HFILL }
176                 },
177         };
178
179 /* Setup protocol subtree array */
180         static gint *ett[] = {
181                 &ett_turnchannel,
182         };
183
184 /* Register the protocol name and description */
185         proto_turnchannel = proto_register_protocol("TURN Channel",
186             "TURNCHANNEL", "turnchannel");
187
188         new_register_dissector("turnchannel", dissect_turnchannel_message,
189                            proto_turnchannel);
190
191 /* subdissectors */
192         heur_subdissector_list = register_heur_dissector_list("turnchannel");
193
194 /* Required function calls to register the header fields and subtrees used */
195         proto_register_field_array(proto_turnchannel, hf, array_length(hf));
196         proto_register_subtree_array(ett, array_length(ett));
197
198 }
199
200
201 void
202 proto_reg_handoff_turnchannel(void)
203 {
204         dissector_handle_t turnchannel_tcp_handle;
205         dissector_handle_t turnchannel_udp_handle;
206
207         turnchannel_tcp_handle = create_dissector_handle(dissect_turnchannel_tcp, proto_turnchannel);
208         turnchannel_udp_handle = find_dissector("turnchannel");
209
210         /* Register for "Decode As" in case STUN negotiation isn't captured */
211         dissector_add_for_decode_as("tcp.port", turnchannel_tcp_handle);
212         dissector_add_for_decode_as("udp.port", turnchannel_udp_handle);
213
214         /* TURN negotiation is handled through STUN2 dissector (packet-stun.c),
215            so only it should be able to determine if a packet is a TURN packet */
216         heur_dissector_add("stun", dissect_turnchannel_heur, "TURN Channel over STUN", "turnchannel_stun", proto_turnchannel, HEURISTIC_ENABLE);
217
218         data_handle = find_dissector("data");
219 }
220
221 /*
222  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
223  *
224  * Local variables:
225  * c-basic-offset: 8
226  * tab-width: 8
227  * indent-tabs-mode: t
228  * End:
229  *
230  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
231  * :indentSize=8:tabSize=8:noTabs=false:
232  */