From Kovarththanan Rajaratnam via bug 3548:
[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         if (check_col(pinfo->cinfo, COL_PROTOCOL))
94                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TURN CHANNEL");
95
96         if (check_col(pinfo->cinfo, COL_INFO))
97           col_add_fstr(pinfo->cinfo, COL_INFO, "Channel Id 0x%x", channel_id);
98
99         if (!tree)
100           return tvb_length(tvb);
101
102         ti = proto_tree_add_item(tree, proto_turnchannel, tvb, 0, -1, FALSE);
103
104         turnchannel_tree = proto_item_add_subtree(ti, ett_turnchannel);
105
106         proto_tree_add_uint(turnchannel_tree, hf_turnchannel_id, tvb, 0, 2, channel_id);
107         proto_tree_add_uint(turnchannel_tree, hf_turnchannel_len, tvb, 2, 2, data_len);
108
109         
110         if (len > TURNCHANNEL_HDR_LEN) {
111           tvbuff_t *next_tvb;
112           guint reported_len, new_len;
113
114           new_len = tvb_length_remaining(tvb, TURNCHANNEL_HDR_LEN);
115           reported_len = tvb_reported_length_remaining(tvb, 
116                                                        TURNCHANNEL_HDR_LEN);
117           if (data_len < reported_len) {
118             reported_len = data_len;
119           }
120           next_tvb = tvb_new_subset(tvb, TURNCHANNEL_HDR_LEN, new_len, 
121                                     reported_len);
122
123
124           if (!dissector_try_heuristic(heur_subdissector_list, 
125                                        next_tvb, pinfo, tree)) {
126             call_dissector(data_handle,next_tvb, pinfo, tree);
127           }
128         }
129
130         return tvb_length(tvb);
131 }
132
133
134 static void
135 dissect_turnchannel_message_no_return(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
136 {
137         dissect_turnchannel_message(tvb, pinfo, tree);
138 }
139
140
141 static guint
142 get_turnchannel_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
143 {
144         return (guint)tvb_get_ntohs(tvb, offset+2) + TURNCHANNEL_HDR_LEN;
145 }
146
147 static void
148 dissect_turnchannel_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
149 {
150         tcp_dissect_pdus(tvb, pinfo, tree, TRUE, TURNCHANNEL_HDR_LEN,
151                         get_turnchannel_message_len, dissect_turnchannel_message_no_return);
152 }
153
154
155 static gboolean
156 dissect_turnchannel_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
157 {
158         guint   len;
159         guint16 channel_id;
160         guint16 data_len;
161
162         len = tvb_length(tvb);
163         /* First, make sure we have enough data to do the check. */
164         if (len < TURNCHANNEL_HDR_LEN) {
165                   return FALSE;
166         }
167
168         channel_id = tvb_get_ntohs(tvb, 0);
169         data_len = tvb_get_ntohs(tvb, 2);
170
171         if ((channel_id < 0x4000) || (channel_id > 0xFFFE)) {
172           return FALSE;
173         }
174
175         if (len != TURNCHANNEL_HDR_LEN + data_len) {
176           return FALSE;
177         }
178
179         return dissect_turnchannel_message(tvb, pinfo, tree);
180 }
181
182 void
183 proto_register_turnchannel(void)
184 {
185         static hf_register_info hf[] = {
186                 { &hf_turnchannel_id,
187                         { "TURN Channel ID",    "turnchannel.id",       FT_UINT16,
188                         BASE_HEX,       NULL,   0x0,    NULL,   HFILL }
189                 },
190                 { &hf_turnchannel_len,
191                         { "Data Length",  "turnchannel.length", FT_UINT16,
192                         BASE_DEC,       NULL,   0x0,    NULL,   HFILL }
193                 },
194         };
195
196 /* Setup protocol subtree array */
197         static gint *ett[] = {
198                 &ett_turnchannel,
199         };
200
201 /* Register the protocol name and description */
202         proto_turnchannel = proto_register_protocol("TURN Channel",
203             "TURNCHANNEL", "turnchannel");
204
205         new_register_dissector("turnchannel", dissect_turnchannel_message,
206                            proto_turnchannel);
207
208 /* subdissectors */
209         register_heur_dissector_list("turnchannel", &heur_subdissector_list);
210
211 /* Required function calls to register the header fields and subtrees used */
212         proto_register_field_array(proto_turnchannel, hf, array_length(hf));
213         proto_register_subtree_array(ett, array_length(ett));
214
215 }
216
217
218 void
219 proto_reg_handoff_turnchannel(void)
220 {
221         dissector_handle_t turnchannel_tcp_handle;
222         dissector_handle_t turnchannel_udp_handle;
223
224         turnchannel_tcp_handle = create_dissector_handle(dissect_turnchannel_tcp, proto_turnchannel);
225         turnchannel_udp_handle = find_dissector("turnchannel");
226
227         dissector_add_handle("tcp.port", turnchannel_tcp_handle);   /* for decode-as */
228         dissector_add_handle("udp.port", turnchannel_udp_handle);   /* ...           */
229
230         heur_dissector_add("udp", dissect_turnchannel_heur, proto_turnchannel);
231         heur_dissector_add("tcp", dissect_turnchannel_heur, proto_turnchannel);
232
233         data_handle = find_dissector("data");
234 }