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