From Didier Gautheron:
[obnox/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  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
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.
16  *
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.
21  *
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  *
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
32  */
33
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <glib.h>
43
44 #include <epan/packet.h>
45 #include <packet-tcp.h>
46
47 /* heuristic subdissectors */
48 static heur_dissector_list_t heur_subdissector_list;
49
50 /* data dissector handle */
51 static dissector_handle_t data_handle;
52
53 /* Initialize the protocol and registered fields */
54 static int proto_turnchannel = -1;
55
56 static int hf_turnchannel_id = -1;
57 static int hf_turnchannel_len = -1;
58
59 #define TURNCHANNEL_HDR_LEN     ((guint)4)      
60
61
62 /* Initialize the subtree pointers */
63 static gint ett_turnchannel = -1;
64
65 static int
66 dissect_turnchannel_message(tvbuff_t *tvb, packet_info *pinfo, 
67                             proto_tree *tree)
68 {
69         guint   len;
70         guint16 channel_id;
71         guint16 data_len;
72         proto_item *ti;
73         proto_tree *turnchannel_tree;
74
75         len = tvb_length(tvb);
76         /* First, make sure we have enough data to do the check. */
77         if (len < TURNCHANNEL_HDR_LEN) {
78                   return 0;
79         }
80
81         channel_id = tvb_get_ntohs(tvb, 0);
82         data_len = tvb_get_ntohs(tvb, 2);
83
84         if ((channel_id < 0x4000) || (channel_id > 0xFFFE)) {
85           return 0;
86         }
87
88         if (len != TURNCHANNEL_HDR_LEN + data_len) {
89           return 0;
90         }
91
92         /* Seems to be a decent TURN channel message */
93         col_set_str(pinfo->cinfo, COL_PROTOCOL, "TURN CHANNEL");
94
95         if (check_col(pinfo->cinfo, COL_INFO))
96           col_add_fstr(pinfo->cinfo, COL_INFO, "Channel Id 0x%x", channel_id);
97
98         ti = proto_tree_add_item(tree, proto_turnchannel, tvb, 0, -1, FALSE);
99
100         turnchannel_tree = proto_item_add_subtree(ti, ett_turnchannel);
101
102         proto_tree_add_uint(turnchannel_tree, hf_turnchannel_id, tvb, 0, 2, channel_id);
103         proto_tree_add_uint(turnchannel_tree, hf_turnchannel_len, tvb, 2, 2, data_len);
104
105         
106         if (len > TURNCHANNEL_HDR_LEN) {
107           tvbuff_t *next_tvb;
108           guint reported_len, new_len;
109
110           new_len = tvb_length_remaining(tvb, TURNCHANNEL_HDR_LEN);
111           reported_len = tvb_reported_length_remaining(tvb, 
112                                                        TURNCHANNEL_HDR_LEN);
113           if (data_len < reported_len) {
114             reported_len = data_len;
115           }
116           next_tvb = tvb_new_subset(tvb, TURNCHANNEL_HDR_LEN, new_len, 
117                                     reported_len);
118
119
120           if (!dissector_try_heuristic(heur_subdissector_list, 
121                                        next_tvb, pinfo, tree)) {
122             call_dissector(data_handle,next_tvb, pinfo, tree);
123           }
124         }
125
126         return tvb_length(tvb);
127 }
128
129
130 static void
131 dissect_turnchannel_message_no_return(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
132 {
133         dissect_turnchannel_message(tvb, pinfo, tree);
134 }
135
136
137 static guint
138 get_turnchannel_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
139 {
140         return (guint)tvb_get_ntohs(tvb, offset+2) + TURNCHANNEL_HDR_LEN;
141 }
142
143 static void
144 dissect_turnchannel_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
145 {
146         tcp_dissect_pdus(tvb, pinfo, tree, TRUE, TURNCHANNEL_HDR_LEN,
147                         get_turnchannel_message_len, dissect_turnchannel_message_no_return);
148 }
149
150
151 static gboolean
152 dissect_turnchannel_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
153 {
154         guint   len;
155         guint16 channel_id;
156         guint16 data_len;
157
158         len = tvb_length(tvb);
159         /* First, make sure we have enough data to do the check. */
160         if (len < TURNCHANNEL_HDR_LEN) {
161                   return FALSE;
162         }
163
164         channel_id = tvb_get_ntohs(tvb, 0);
165         data_len = tvb_get_ntohs(tvb, 2);
166
167         if ((channel_id < 0x4000) || (channel_id > 0xFFFE)) {
168           return FALSE;
169         }
170
171         if (len != TURNCHANNEL_HDR_LEN + data_len) {
172           return FALSE;
173         }
174
175         return dissect_turnchannel_message(tvb, pinfo, tree);
176 }
177
178 void
179 proto_register_turnchannel(void)
180 {
181         static hf_register_info hf[] = {
182                 { &hf_turnchannel_id,
183                         { "TURN Channel ID",    "turnchannel.id",       FT_UINT16,
184                         BASE_HEX,       NULL,   0x0,    NULL,   HFILL }
185                 },
186                 { &hf_turnchannel_len,
187                         { "Data Length",  "turnchannel.length", FT_UINT16,
188                         BASE_DEC,       NULL,   0x0,    NULL,   HFILL }
189                 },
190         };
191
192 /* Setup protocol subtree array */
193         static gint *ett[] = {
194                 &ett_turnchannel,
195         };
196
197 /* Register the protocol name and description */
198         proto_turnchannel = proto_register_protocol("TURN Channel",
199             "TURNCHANNEL", "turnchannel");
200
201         new_register_dissector("turnchannel", dissect_turnchannel_message,
202                            proto_turnchannel);
203
204 /* subdissectors */
205         register_heur_dissector_list("turnchannel", &heur_subdissector_list);
206
207 /* Required function calls to register the header fields and subtrees used */
208         proto_register_field_array(proto_turnchannel, hf, array_length(hf));
209         proto_register_subtree_array(ett, array_length(ett));
210
211 }
212
213
214 void
215 proto_reg_handoff_turnchannel(void)
216 {
217         dissector_handle_t turnchannel_tcp_handle;
218         dissector_handle_t turnchannel_udp_handle;
219
220         turnchannel_tcp_handle = create_dissector_handle(dissect_turnchannel_tcp, proto_turnchannel);
221         turnchannel_udp_handle = find_dissector("turnchannel");
222
223         dissector_add_handle("tcp.port", turnchannel_tcp_handle);   /* for decode-as */
224         dissector_add_handle("udp.port", turnchannel_udp_handle);   /* ...           */
225
226         heur_dissector_add("udp", dissect_turnchannel_heur, proto_turnchannel);
227         heur_dissector_add("tcp", dissect_turnchannel_heur, proto_turnchannel);
228
229         data_handle = find_dissector("data");
230 }