Move 3 ASN1 dissectors to 'clean' group; move 1 PIDL dissector to 'dirty' group.
[metze/wireshark/wip.git] / epan / dissectors / packet-x224.c
1 /* packet-x224.c
2  *
3  * Routine to dissect X.224
4  * Copyright 2007, Ronnie Sahlberg
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #include <glib.h>
30 #include <epan/packet.h>
31
32 #include "packet-tpkt.h"
33 #include "packet-frame.h"
34 #include <epan/conversation.h>
35 #include <epan/emem.h>
36
37 /* X.224 header fields             */
38 static int proto_x224                   = -1;
39 static int hf_x224_length               = -1;
40 static int hf_x224_code                 = -1;
41 static int hf_x224_src_ref              = -1;
42 static int hf_x224_dst_ref              = -1;
43 static int hf_x224_class                = -1;
44 static int hf_x224_rdp_rt               = -1;
45 static int hf_x224_nr                   = -1;
46 static int hf_x224_eot                  = -1;
47
48
49
50 /* X.224 fields defining a sub tree */
51 static gint ett_x224           = -1;
52
53
54 /* find the dissector for T.125 */
55 static dissector_handle_t t125_handle;
56
57
58 typedef struct _x224_conv_info_t {
59         guint8  class;
60 } x224_conv_info_t;
61
62
63 #define X224_CODE_CR            0xE
64 #define X224_CODE_CC            0xD
65 #define X224_CODE_DR            0x8
66 #define X224_CODE_DC            0xC
67 #define X224_CODE_DT            0xF
68 #define X224_CODE_ED            0x1
69 #define X224_CODE_AK            0x6
70 #define X224_CODE_EA            0x2
71 #define X224_CODE_RJ            0x5
72 #define X224_CODE_ER            0x7
73
74 static const value_string code_vals[] = {
75         {X224_CODE_CR,          "Connection Request"},
76         {X224_CODE_CC,          "Connection Confirm"},
77         {X224_CODE_DR,          "Disconnect Request"},
78         {X224_CODE_DC,          "Disconnect Confirm"},
79         {X224_CODE_DT,          "Data"},
80         {X224_CODE_ED,          "Expedited Data"},
81         {X224_CODE_AK,          "Data Ack"},
82         {X224_CODE_EA,          "Expedited Data Ack"},
83         {X224_CODE_RJ,          "Reject"},
84         {X224_CODE_ER,          "Error"},
85         {0,NULL}
86 };
87
88 static const value_string class_option_vals[] = {
89         {0,     "Class 0"},
90         {1,     "Class 1"},
91         {2,     "Class 2"},
92         {3,     "Class 3"},
93         {4,     "Class 4"},
94         {0,NULL}
95 };
96
97 static int
98 dissect_x224_cr(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, x224_conv_info_t *x224_info _U_)
99 {
100         /*guint8 class;*/
101         gint len, next_offset;
102
103         /*DST-REF is always 0 */
104         offset+=2;
105
106         /*SRC-REF*/
107         proto_tree_add_item(tree, hf_x224_src_ref, tvb, offset, 2, ENC_BIG_ENDIAN);
108         offset+=2;
109
110         /* class options */
111         /*class = tvb_get_guint8(tvb, offset);*/
112         proto_tree_add_item(tree, hf_x224_class, tvb, offset, 1, ENC_BIG_ENDIAN);
113         offset+=1;
114
115         if(tvb_length_remaining(tvb, offset) > 0) {
116                 len = tvb_find_line_end(tvb, offset, -1, &next_offset, TRUE);
117                 proto_tree_add_item(tree, hf_x224_rdp_rt, tvb, offset, len,
118                                     ENC_ASCII|ENC_NA);
119                 offset = next_offset;
120         }
121
122         return offset;
123 }
124
125 static int
126 dissect_x224_cc(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, x224_conv_info_t *x224_info)
127 {
128         guint8 class;
129
130         /*DST-REF */
131         proto_tree_add_item(tree, hf_x224_dst_ref, tvb, offset, 2, ENC_BIG_ENDIAN);
132         offset+=2;
133
134         /*SRC-REF*/
135         proto_tree_add_item(tree, hf_x224_src_ref, tvb, offset, 2, ENC_BIG_ENDIAN);
136         offset+=2;
137
138         /* class options */
139         class = tvb_get_guint8(tvb, offset);
140         proto_tree_add_item(tree, hf_x224_class, tvb, offset, 1, ENC_BIG_ENDIAN);
141         x224_info->class = class;
142         offset+=1;
143
144         return offset;
145 }
146
147 static int
148 dissect_x224_dt(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, x224_conv_info_t *x224_info, proto_tree *parent_tree)
149 {
150         proto_item *item = NULL;
151         tvbuff_t *next_tvb;
152
153         switch (x224_info->class >>4) {
154         case 2:
155         case 3:
156         case 4:
157                 /*DST-REF */
158                 proto_tree_add_item(tree, hf_x224_dst_ref, tvb, offset, 2, ENC_BIG_ENDIAN);
159                 offset+=2;
160                 break;
161         }
162
163         item = proto_tree_add_uint(tree, hf_x224_class, tvb, 0, 0, x224_info->class);
164         PROTO_ITEM_SET_GENERATED(item);
165
166
167         /* EOT / NR */
168         proto_tree_add_item(tree, hf_x224_eot, tvb, offset, 1, ENC_BIG_ENDIAN);
169         proto_tree_add_item(tree, hf_x224_nr, tvb, offset, 1, ENC_BIG_ENDIAN);
170         offset+=1;
171
172
173         next_tvb = tvb_new_subset_remaining(tvb, offset);
174         call_dissector(t125_handle, next_tvb, pinfo, parent_tree);
175
176         return offset;
177 }
178
179 static int
180 dissect_x224(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void *data _U_)
181 {
182         proto_tree *tree = NULL;
183         proto_item *item = NULL;
184         int offset = 0 ;
185         guint8 code, length;
186         conversation_t *conversation;
187         x224_conv_info_t *x224_info;
188
189         col_set_str(pinfo->cinfo, COL_PROTOCOL, "X.224");
190         col_clear(pinfo->cinfo, COL_INFO);
191
192         length = tvb_get_guint8(tvb, offset);
193         if (parent_tree) {
194                 item = proto_tree_add_item(parent_tree, proto_x224, tvb, offset, length+1, ENC_NA);
195                 tree = proto_item_add_subtree(item, ett_x224);
196         }
197
198
199         /* length indicator */
200         proto_tree_add_item(tree, hf_x224_length, tvb, offset, 1, ENC_BIG_ENDIAN);
201         offset+=1;
202
203         /* code */
204         code = tvb_get_guint8(tvb, offset);
205         proto_tree_add_item(tree, hf_x224_code, tvb, offset, 1, ENC_BIG_ENDIAN);
206         offset+=1;
207
208         if (check_col(pinfo->cinfo, COL_INFO)) {
209                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s (0x%02x)",
210                         val_to_str(code>>4, code_vals, "Unknown code :%x"),
211                         code);
212         }
213
214
215
216         /*
217          * We need to track some state for this protocol on a per conversation
218          * basis so we can do neat things like request/response tracking
219          */
220         conversation = find_or_create_conversation(pinfo);
221
222         /*
223          * Do we already have a state structure for this conv
224          */
225         x224_info = conversation_get_proto_data(conversation, proto_x224);
226         if (!x224_info) {
227                 /* No.  Attach that information to the conversation, and add
228                  * it to the list of information structures.
229                  */
230                 x224_info = se_alloc(sizeof(x224_conv_info_t));
231                 x224_info->class=0;
232
233                 conversation_add_proto_data(conversation, proto_x224, x224_info);
234        }
235
236         switch (code>>4) {
237         case X224_CODE_CR:
238                 offset = dissect_x224_cr(pinfo, tree, tvb, offset, x224_info);
239                 break;
240         case X224_CODE_CC:
241                 offset = dissect_x224_cc(pinfo, tree, tvb, offset, x224_info);
242                 break;
243         case X224_CODE_DR:
244                 /* XXX not implemented yet */
245                 break;
246         case X224_CODE_DC:
247                 /* XXX not implemented yet */
248                 break;
249         case X224_CODE_DT:
250                 offset = dissect_x224_dt(pinfo, tree, tvb, offset, x224_info, parent_tree);
251                 break;
252         case X224_CODE_ED:
253                 /* XXX not implemented yet */
254                 break;
255         case X224_CODE_AK:
256                 /* XXX not implemented yet */
257                 break;
258         case X224_CODE_EA:
259                 /* XXX not implemented yet */
260                 break;
261         case X224_CODE_RJ:
262                 /* XXX not implemented yet */
263                 break;
264         case X224_CODE_ER:
265                 /* XXX not implemented yet */
266                 break;
267         }
268 return offset;
269 }
270
271 void
272 proto_register_x224(void)
273 {
274         static hf_register_info hf[] =
275         {
276         { &hf_x224_length, {
277         "Length", "x224.length", FT_UINT8, BASE_DEC,
278         NULL, 0, NULL, HFILL }},
279
280         { &hf_x224_code, {
281         "Code", "x224.code", FT_UINT8, BASE_HEX,
282         VALS(code_vals), 0xf0, NULL, HFILL }},
283
284         { &hf_x224_src_ref, {
285         "SRC-REF", "x224.src_ref", FT_UINT16, BASE_HEX,
286         NULL, 0, NULL, HFILL }},
287
288         { &hf_x224_dst_ref, {
289         "DST-REF", "x224.dst_ref", FT_UINT16, BASE_HEX,
290         NULL, 0, NULL, HFILL }},
291
292         { &hf_x224_class, {
293         "Class", "x224.class", FT_UINT8, BASE_HEX,
294         VALS(class_option_vals), 0xf0, NULL, HFILL }},
295
296         { &hf_x224_rdp_rt, {
297         "RDP Routing Token", "x224.rdp_rt", FT_STRING, BASE_NONE, NULL, 0,
298         "Used for Remote Desktop Protocol (RDP) load balancing", HFILL }},
299
300         { &hf_x224_nr, {
301         "NR", "x224.nr", FT_UINT8, BASE_HEX,
302         NULL, 0x7f, NULL, HFILL }},
303
304         { &hf_x224_eot, {
305         "EOT", "x224.eot", FT_BOOLEAN, 8,
306         NULL, 0x80, NULL, HFILL }},
307
308         };
309
310         static gint *ett[] =
311         {
312                 &ett_x224,
313         };
314
315         proto_x224 = proto_register_protocol("ITU-T Rec X.224", "X.224", "x224");
316         proto_register_field_array(proto_x224, hf, array_length(hf));
317         proto_register_subtree_array(ett, array_length(ett));
318         new_register_dissector("x224", dissect_x224, proto_x224);
319
320 }
321
322 void
323 proto_reg_handoff_x224(void)
324 {
325         t125_handle = find_dissector("t125");
326 }