Move the xDLC helper routines into epan.
[obnox/wireshark/wip.git] / epan / dissectors / packet-v120.c
1 /* packet-v120.c
2  * Routines for v120 frame disassembly
3  * Bert Driehuis <driehuis@playbeing.org>
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <glib.h>
32 #include <string.h>
33 #include <epan/packet.h>
34 #include <epan/xdlc.h>
35
36 static int proto_v120 = -1;
37 static int hf_v120_address = -1;
38 static int hf_v120_control = -1;
39 static int hf_v120_n_r = -1;
40 static int hf_v120_n_s = -1;
41 static int hf_v120_p = -1;
42 static int hf_v120_p_ext = -1;
43 static int hf_v120_f = -1;
44 static int hf_v120_f_ext = -1;
45 static int hf_v120_s_ftype = -1;
46 static int hf_v120_u_modifier_cmd = -1;
47 static int hf_v120_u_modifier_resp = -1;
48 static int hf_v120_ftype_i = -1;
49 static int hf_v120_ftype_s_u = -1;
50 static int hf_v120_ftype_s_u_ext = -1;
51 static int hf_v120_header = -1;
52
53 static gint ett_v120 = -1;
54 static gint ett_v120_address = -1;
55 static gint ett_v120_control = -1;
56 static gint ett_v120_header = -1;
57
58 static dissector_handle_t data_handle;
59
60 static int dissect_v120_header(tvbuff_t *tvb, int offset, proto_tree *tree);
61
62 /* Used only for U frames */
63 static const xdlc_cf_items v120_cf_items = {
64         NULL,
65         NULL,
66         &hf_v120_p,
67         &hf_v120_f,
68         NULL,
69         &hf_v120_u_modifier_cmd,
70         &hf_v120_u_modifier_resp,
71         NULL,
72         &hf_v120_ftype_s_u
73 };
74
75 /* Used only for I and S frames */
76 static const xdlc_cf_items v120_cf_items_ext = {
77         &hf_v120_n_r,
78         &hf_v120_n_s,
79         &hf_v120_p_ext,
80         &hf_v120_f_ext,
81         &hf_v120_s_ftype,
82         NULL,
83         NULL,
84         &hf_v120_ftype_i,
85         &hf_v120_ftype_s_u_ext
86 };
87
88 static void
89 dissect_v120(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
90 {
91     proto_tree  *v120_tree, *tc, *address_tree;
92     proto_item  *ti;
93     int         is_response;
94     int         addr;
95     char        info[80];
96     int         v120len;
97     guint8      byte0, byte1;
98     guint16     control;
99     tvbuff_t    *next_tvb;
100
101     if (check_col(pinfo->cinfo, COL_PROTOCOL))
102         col_set_str(pinfo->cinfo, COL_PROTOCOL, "V.120");
103     if (check_col(pinfo->cinfo, COL_INFO))
104         col_clear(pinfo->cinfo, COL_INFO);
105
106     byte0 = tvb_get_guint8(tvb, 0);
107
108     if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
109         col_add_fstr(pinfo->cinfo, COL_RES_DL_SRC, "0x%02X", byte0);
110
111     byte1 = tvb_get_guint8(tvb, 1);
112
113     if ((byte0 & 0x01) != 0x00 && (byte1 && 0x01) != 0x01)
114     {
115         if (check_col(pinfo->cinfo, COL_INFO))
116             col_set_str(pinfo->cinfo, COL_INFO, "Invalid V.120 frame");
117         if (tree)
118             ti = proto_tree_add_protocol_format(tree, proto_v120, tvb, 0, -1,
119                                             "Invalid V.120 frame");
120         return;
121     }
122
123     if (pinfo->p2p_dir == P2P_DIR_SENT) {
124         is_response = (byte0 & 0x02) ? FALSE: TRUE;
125         if(check_col(pinfo->cinfo, COL_RES_DL_DST))
126             col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DCE");
127         if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
128             col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DTE");
129     } else {
130         /* XXX - what if the direction is unknown? */
131         is_response = (byte0 & 0x02) ? TRUE : FALSE;
132         if(check_col(pinfo->cinfo, COL_RES_DL_DST))
133             col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DTE");
134         if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
135             col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DCE");
136     }
137
138     if (tree) {
139         ti = proto_tree_add_protocol_format(tree, proto_v120, tvb, 0, -1, "V.120");
140         v120_tree = proto_item_add_subtree(ti, ett_v120);
141         addr = byte1 << 8 | byte0;
142         sprintf(info, "LLI: %d C/R: %s",
143                         ((byte0 & 0xfc) << 5) | ((byte1 & 0xfe) >> 1),
144                         byte0 & 0x02 ? "R" : "C");
145         tc = proto_tree_add_text(v120_tree, tvb,
146                         0, 2,
147                         "Address field: %s", info);
148         address_tree = proto_item_add_subtree(tc, ett_v120_address);
149         proto_tree_add_text(address_tree, tvb, 0, 2,
150                     decode_boolean_bitfield(addr, 0x0002, 2*8,
151                         "Response", "Command"), NULL);
152         sprintf(info, "LLI: %d", ((byte0 & 0xfc) << 5) | ((byte1 & 0xfe) >> 1));
153         proto_tree_add_text(address_tree, tvb, 0, 2,
154                     decode_numeric_bitfield(addr, 0xfefc, 2*8, info));
155         proto_tree_add_text(address_tree, tvb, 0, 2,
156                     decode_boolean_bitfield(addr, 0x0001, 2*8,
157                         "EA0 = 1 (Error)", "EA0 = 0"), NULL);
158         proto_tree_add_text(address_tree, tvb, 0, 2,
159                     decode_boolean_bitfield(addr, 0x0100, 2*8,
160                         "EA1 = 1", "EA1 = 0 (Error)"), NULL);
161     }
162     else {
163         v120_tree = NULL;
164         ti = NULL;
165     }
166     control = dissect_xdlc_control(tvb, 2, pinfo, v120_tree, hf_v120_control,
167             ett_v120_control, &v120_cf_items, &v120_cf_items_ext,
168             NULL, NULL, is_response, TRUE, FALSE);
169     if (tree) {
170         v120len = 2 + XDLC_CONTROL_LEN(control, TRUE);
171         if (tvb_bytes_exist(tvb, v120len, 1))
172                 v120len += dissect_v120_header(tvb, v120len, v120_tree);
173         proto_item_set_len(ti, v120len);
174         next_tvb = tvb_new_subset(tvb, v120len, -1, -1);
175         call_dissector(data_handle,next_tvb, pinfo, v120_tree);
176     }
177 }
178
179 static int
180 dissect_v120_header(tvbuff_t *tvb, int offset, proto_tree *tree)
181 {
182         char info[80];
183         int header_len, nbits;
184         int header;
185         proto_tree *h_tree, *tc;
186         guint8  byte0;
187
188         byte0 = tvb_get_guint8(tvb, offset);
189
190         if (byte0 & 0x80) {
191                 header_len = 1;
192                 header = byte0;
193         } else {
194                 header_len = 2;
195                 header = byte0 | tvb_get_guint8(tvb, offset + 1) << 8;
196         }
197         nbits = header_len * 8;
198         sprintf(info, "Header: B: %d F: %d", byte0 & 0x02 ? 1:0,
199                         byte0 & 0x01 ? 1:0);
200         tc = proto_tree_add_text(tree, tvb,
201                         offset, header_len,
202                         "Header octet: %s (0x%02X)", info, byte0);
203         h_tree = proto_item_add_subtree(tc, ett_v120_header);
204         proto_tree_add_text(h_tree, tvb, offset, header_len,
205                     decode_boolean_bitfield(header, 0x80, nbits,
206                         "No extension octet", "Extension octet follows"), NULL);
207         proto_tree_add_text(h_tree, tvb, offset, header_len,
208                     decode_boolean_bitfield(header, 0x40, nbits,
209                         "Break condition", "No break condition"), NULL);
210         sprintf(info, "Error control C1/C2: %d", (header & 0x0c) >> 2);
211         proto_tree_add_text(h_tree, tvb, offset, header_len,
212                     decode_numeric_bitfield(header, 0x0c, nbits, info));
213         proto_tree_add_text(h_tree, tvb, offset, header_len,
214                     decode_boolean_bitfield(header, 0x02, nbits,
215                         "Segmentation bit B", "No segmentation bit B"), NULL);
216         proto_tree_add_text(h_tree, tvb, offset, header_len,
217                     decode_boolean_bitfield(header, 0x01, nbits,
218                         "Segmentation bit F", "No segmentation bit F"), NULL);
219         if (header_len == 2) {
220                 proto_tree_add_text(h_tree, tvb, offset, header_len,
221                     decode_boolean_bitfield(header, 0x8000, nbits,
222                         "E", "E bit not set (Error)"), NULL);
223                 proto_tree_add_text(h_tree, tvb, offset, header_len,
224                     decode_boolean_bitfield(header, 0x4000, nbits,
225                         "DR", "No DR"), NULL);
226                 proto_tree_add_text(h_tree, tvb, offset, header_len,
227                     decode_boolean_bitfield(header, 0x2000, nbits,
228                         "SR", "No SR"), NULL);
229                 proto_tree_add_text(h_tree, tvb, offset, header_len,
230                     decode_boolean_bitfield(header, 0x1000, nbits,
231                         "RR", "No RR"), NULL);
232         }
233         return header_len;
234 }
235
236 void
237 proto_register_v120(void)
238 {
239     static hf_register_info hf[] = {
240         { &hf_v120_address,
241           { "Link Address", "v120.address", FT_UINT16, BASE_HEX, NULL,
242                   0x0, "", HFILL }},
243         { &hf_v120_control,
244           { "Control Field", "v120.control", FT_UINT16, BASE_HEX, NULL, 0x0,
245                 "", HFILL }},
246         { &hf_v120_n_r,
247             { "N(R)", "v120.control.n_r", FT_UINT16, BASE_DEC,
248                 NULL, XDLC_N_R_EXT_MASK, "", HFILL }},
249         { &hf_v120_n_s,
250             { "N(S)", "v120.control.n_s", FT_UINT16, BASE_DEC,
251                 NULL, XDLC_N_S_EXT_MASK, "", HFILL }},
252         { &hf_v120_p,
253             { "Poll", "v120.control.p", FT_BOOLEAN, 8,
254                 TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
255         { &hf_v120_p_ext,
256             { "Poll", "v120.control.p", FT_BOOLEAN, 16,
257                 TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
258         { &hf_v120_f,
259             { "Final", "v120.control.f", FT_BOOLEAN, 8,
260                 TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
261         { &hf_v120_f_ext,
262             { "Final", "v120.control.f", FT_BOOLEAN, 16,
263                 TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
264         { &hf_v120_s_ftype,
265             { "Supervisory frame type", "v120.control.s_ftype", FT_UINT16, BASE_HEX,
266                 VALS(stype_vals), XDLC_S_FTYPE_MASK, "", HFILL }},
267         { &hf_v120_u_modifier_cmd,
268             { "Command", "v120.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
269                 VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, "", HFILL }},
270         { &hf_v120_u_modifier_resp,
271             { "Response", "v120.control.u_modifier_resp", FT_UINT8, BASE_HEX,
272                 VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, "", HFILL }},
273         { &hf_v120_ftype_i,
274             { "Frame type", "v120.control.ftype", FT_UINT16, BASE_HEX,
275                 VALS(ftype_vals), XDLC_I_MASK, "", HFILL }},
276         { &hf_v120_ftype_s_u,
277             { "Frame type", "v120.control.ftype", FT_UINT8, BASE_HEX,
278                 VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
279         { &hf_v120_ftype_s_u_ext,
280             { "Frame type", "v120.control.ftype", FT_UINT16, BASE_HEX,
281                 VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
282         { &hf_v120_header,
283           { "Header Field", "v120.header", FT_STRING, BASE_NONE, NULL, 0x0,
284                 "", HFILL }},
285     };
286     static gint *ett[] = {
287         &ett_v120,
288         &ett_v120_address,
289         &ett_v120_control,
290         &ett_v120_header,
291     };
292
293     proto_v120 = proto_register_protocol("Async data over ISDN (V.120)",
294                                          "V.120", "v120");
295     proto_register_field_array (proto_v120, hf, array_length(hf));
296     proto_register_subtree_array(ett, array_length(ett));
297
298     register_dissector("v120", dissect_v120, proto_v120);
299 }
300
301 void
302 proto_reg_handoff_v120(void)
303 {
304     data_handle = find_dissector("data");
305 }