MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-turnchannel.c
blob2fa4c0f19c90057cdc419601355146fdf37d4cb8
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>
6 * $Id$
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * Please refer to the following specs for protocol detail:
27 * - draft-ietf-behave-rfc3489bis-15
28 * - draft-ietf-mmusic-ice-19
29 * - draft-ietf-behave-nat-behavior-discovery-03
30 * - draft-ietf-behave-turn-07
31 * - draft-ietf-behave-turn-ipv6-03
34 #include "config.h"
36 #include <glib.h>
38 #include <epan/packet.h>
39 #include <packet-tcp.h>
41 /* heuristic subdissectors */
42 static heur_dissector_list_t heur_subdissector_list;
44 /* data dissector handle */
45 static dissector_handle_t data_handle;
47 /* Initialize the protocol and registered fields */
48 static int proto_turnchannel = -1;
50 static int hf_turnchannel_id = -1;
51 static int hf_turnchannel_len = -1;
53 #define TURNCHANNEL_HDR_LEN ((guint)4)
56 /* Initialize the subtree pointers */
57 static gint ett_turnchannel = -1;
59 static int
60 dissect_turnchannel_message(tvbuff_t *tvb, packet_info *pinfo,
61 proto_tree *tree, void *data _U_)
63 guint len;
64 guint16 channel_id;
65 guint16 data_len;
66 proto_item *ti;
67 proto_tree *turnchannel_tree;
69 len = tvb_length(tvb);
70 /* First, make sure we have enough data to do the check. */
71 if (len < TURNCHANNEL_HDR_LEN) {
72 return 0;
75 channel_id = tvb_get_ntohs(tvb, 0);
76 data_len = tvb_get_ntohs(tvb, 2);
78 if ((channel_id < 0x4000) || (channel_id > 0xFFFE)) {
79 return 0;
82 if (len != TURNCHANNEL_HDR_LEN + data_len) {
83 return 0;
86 /* Seems to be a decent TURN channel message */
87 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TURN CHANNEL");
89 col_add_fstr(pinfo->cinfo, COL_INFO, "Channel Id 0x%x", channel_id);
91 ti = proto_tree_add_item(tree, proto_turnchannel, tvb, 0, -1, ENC_NA);
93 turnchannel_tree = proto_item_add_subtree(ti, ett_turnchannel);
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);
99 if (len > TURNCHANNEL_HDR_LEN) {
100 tvbuff_t *next_tvb;
101 guint reported_len, new_len;
103 new_len = tvb_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;
109 next_tvb = tvb_new_subset(tvb, TURNCHANNEL_HDR_LEN, new_len,
110 reported_len);
113 if (!dissector_try_heuristic(heur_subdissector_list,
114 next_tvb, pinfo, tree, NULL)) {
115 call_dissector(data_handle,next_tvb, pinfo, tree);
119 return tvb_length(tvb);
122 static guint
123 get_turnchannel_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
125 return (guint)tvb_get_ntohs(tvb, offset+2) + TURNCHANNEL_HDR_LEN;
128 static int
129 dissect_turnchannel_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
131 tcp_dissect_pdus(tvb, pinfo, tree, TRUE, TURNCHANNEL_HDR_LEN,
132 get_turnchannel_message_len, dissect_turnchannel_message, data);
133 return tvb_length(tvb);
137 static gboolean
138 dissect_turnchannel_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
140 guint len;
141 guint16 channel_id;
142 guint16 data_len;
144 len = tvb_length(tvb);
145 /* First, make sure we have enough data to do the check. */
146 if (len < TURNCHANNEL_HDR_LEN) {
147 return FALSE;
150 channel_id = tvb_get_ntohs(tvb, 0);
151 data_len = tvb_get_ntohs(tvb, 2);
153 if ((channel_id < 0x4000) || (channel_id > 0xFFFE)) {
154 return FALSE;
157 if (len != TURNCHANNEL_HDR_LEN + data_len) {
158 return FALSE;
161 return dissect_turnchannel_message(tvb, pinfo, tree, NULL);
164 void
165 proto_register_turnchannel(void)
167 static hf_register_info hf[] = {
168 { &hf_turnchannel_id,
169 { "TURN Channel ID", "turnchannel.id", FT_UINT16,
170 BASE_HEX, NULL, 0x0, NULL, HFILL }
172 { &hf_turnchannel_len,
173 { "Data Length", "turnchannel.length", FT_UINT16,
174 BASE_DEC, NULL, 0x0, NULL, HFILL }
178 /* Setup protocol subtree array */
179 static gint *ett[] = {
180 &ett_turnchannel,
183 /* Register the protocol name and description */
184 proto_turnchannel = proto_register_protocol("TURN Channel",
185 "TURNCHANNEL", "turnchannel");
187 new_register_dissector("turnchannel", dissect_turnchannel_message,
188 proto_turnchannel);
190 /* subdissectors */
191 register_heur_dissector_list("turnchannel", &heur_subdissector_list);
193 /* Required function calls to register the header fields and subtrees used */
194 proto_register_field_array(proto_turnchannel, hf, array_length(hf));
195 proto_register_subtree_array(ett, array_length(ett));
200 void
201 proto_reg_handoff_turnchannel(void)
203 dissector_handle_t turnchannel_tcp_handle;
204 dissector_handle_t turnchannel_udp_handle;
206 turnchannel_tcp_handle = new_create_dissector_handle(dissect_turnchannel_tcp, proto_turnchannel);
207 turnchannel_udp_handle = find_dissector("turnchannel");
209 /* Used for "Decode As" in case STUN negotiation isn't captured */
210 dissector_add_handle("tcp.port", turnchannel_tcp_handle);
211 dissector_add_handle("udp.port", turnchannel_udp_handle);
213 /* TURN negoiation is handled through STUN2 dissector (packet-stun.c),
214 so only it should be able to determine if a packet is a TURN packet */
215 heur_dissector_add("stun", dissect_turnchannel_heur, proto_turnchannel);
217 data_handle = find_dissector("data");