#include <string.h> not needed.
[metze/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  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
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 <glib.h>
31 #include <epan/packet.h>
32 #include <epan/xdlc.h>
33 #include <epan/emem.h>
34
35 static int proto_v120 = -1;
36 static int hf_v120_address = -1;
37 static int hf_v120_control = -1;
38 static int hf_v120_n_r = -1;
39 static int hf_v120_n_s = -1;
40 static int hf_v120_p = -1;
41 static int hf_v120_p_ext = -1;
42 static int hf_v120_f = -1;
43 static int hf_v120_f_ext = -1;
44 static int hf_v120_s_ftype = -1;
45 static int hf_v120_u_modifier_cmd = -1;
46 static int hf_v120_u_modifier_resp = -1;
47 static int hf_v120_ftype_i = -1;
48 static int hf_v120_ftype_s_u = -1;
49 static int hf_v120_ftype_s_u_ext = -1;
50 static int hf_v120_header = -1;
51
52 static gint ett_v120 = -1;
53 static gint ett_v120_address = -1;
54 static gint ett_v120_control = -1;
55 static gint ett_v120_header = -1;
56
57 static dissector_handle_t data_handle;
58
59 static int dissect_v120_header(tvbuff_t *tvb, int offset, proto_tree *tree);
60
61 /* Used only for U frames */
62 static const xdlc_cf_items v120_cf_items = {
63         NULL,
64         NULL,
65         &hf_v120_p,
66         &hf_v120_f,
67         NULL,
68         &hf_v120_u_modifier_cmd,
69         &hf_v120_u_modifier_resp,
70         NULL,
71         &hf_v120_ftype_s_u
72 };
73
74 /* Used only for I and S frames */
75 static const xdlc_cf_items v120_cf_items_ext = {
76         &hf_v120_n_r,
77         &hf_v120_n_s,
78         &hf_v120_p_ext,
79         &hf_v120_f_ext,
80         &hf_v120_s_ftype,
81         NULL,
82         NULL,
83         &hf_v120_ftype_i,
84         &hf_v120_ftype_s_u_ext
85 };
86
87 static void
88 dissect_v120(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
89 {
90     proto_tree  *v120_tree, *tc, *address_tree;
91     proto_item  *ti;
92     int         is_response;
93     int         addr;
94     char        *info;
95     int         v120len;
96     guint8      byte0, byte1;
97     guint16     control;
98     tvbuff_t    *next_tvb;
99
100     info=ep_alloc(80);
101     col_set_str(pinfo->cinfo, COL_PROTOCOL, "V.120");
102     col_clear(pinfo->cinfo, COL_INFO);
103
104     byte0 = tvb_get_guint8(tvb, 0);
105
106     if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
107         col_add_fstr(pinfo->cinfo, COL_RES_DL_SRC, "0x%02X", byte0);
108
109     byte1 = tvb_get_guint8(tvb, 1);
110
111     if ( ((byte0 & 0x01) != 0x00) && ((byte1 & 0x01) != 0x01) )
112     {
113         col_set_str(pinfo->cinfo, COL_INFO, "Invalid V.120 frame");
114         if (tree)
115             proto_tree_add_protocol_format(tree, proto_v120, tvb, 0, -1,
116                                             "Invalid V.120 frame");
117         return;
118     }
119
120     if (pinfo->p2p_dir == P2P_DIR_SENT) {
121         is_response = (byte0 & 0x02) ? FALSE: TRUE;
122         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DCE");
123         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DTE");
124     } else {
125         /* XXX - what if the direction is unknown? */
126         is_response = (byte0 & 0x02) ? TRUE : FALSE;
127         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DTE");
128         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DCE");
129     }
130
131     if (tree) {
132         ti = proto_tree_add_protocol_format(tree, proto_v120, tvb, 0, -1, "V.120");
133         v120_tree = proto_item_add_subtree(ti, ett_v120);
134         addr = byte1 << 8 | byte0;
135         g_snprintf(info, 80, "LLI: %d C/R: %s",
136                         ((byte0 & 0xfc) << 5) | ((byte1 & 0xfe) >> 1),
137                         byte0 & 0x02 ? "R" : "C");
138         tc = proto_tree_add_text(v120_tree, tvb,
139                         0, 2,
140                         "Address field: %s", info);
141         address_tree = proto_item_add_subtree(tc, ett_v120_address);
142         proto_tree_add_text(address_tree, tvb, 0, 2, "%s",
143                     decode_boolean_bitfield(addr, 0x0002, 2*8,
144                         "Response", "Command"));
145         g_snprintf(info, 80, "LLI: %d", ((byte0 & 0xfc) << 5) | ((byte1 & 0xfe) >> 1));
146         proto_tree_add_text(address_tree, tvb, 0, 2, "%s",
147                     decode_numeric_bitfield(addr, 0xfefc, 2*8, info));
148         proto_tree_add_text(address_tree, tvb, 0, 2, "%s",
149                     decode_boolean_bitfield(addr, 0x0001, 2*8,
150                         "EA0 = 1 (Error)", "EA0 = 0"));
151         proto_tree_add_text(address_tree, tvb, 0, 2, "%s",
152                     decode_boolean_bitfield(addr, 0x0100, 2*8,
153                         "EA1 = 1", "EA1 = 0 (Error)"));
154     }
155     else {
156         v120_tree = NULL;
157         ti = NULL;
158     }
159     control = dissect_xdlc_control(tvb, 2, pinfo, v120_tree, hf_v120_control,
160             ett_v120_control, &v120_cf_items, &v120_cf_items_ext,
161             NULL, NULL, is_response, TRUE, FALSE);
162     if (tree) {
163         v120len = 2 + XDLC_CONTROL_LEN(control, TRUE);
164         if (tvb_bytes_exist(tvb, v120len, 1))
165                 v120len += dissect_v120_header(tvb, v120len, v120_tree);
166         proto_item_set_len(ti, v120len);
167         next_tvb = tvb_new_subset_remaining(tvb, v120len);
168         call_dissector(data_handle,next_tvb, pinfo, v120_tree);
169     }
170 }
171
172 static int
173 dissect_v120_header(tvbuff_t *tvb, int offset, proto_tree *tree)
174 {
175         char *info;
176         int header_len, nbits;
177         int header;
178         proto_tree *h_tree, *tc;
179         guint8  byte0;
180
181         info=ep_alloc(80);
182         byte0 = tvb_get_guint8(tvb, offset);
183
184         if (byte0 & 0x80) {
185                 header_len = 1;
186                 header = byte0;
187         } else {
188                 header_len = 2;
189                 header = byte0 | tvb_get_guint8(tvb, offset + 1) << 8;
190         }
191         nbits = header_len * 8;
192         g_snprintf(info, 80, "Header: B: %d F: %d", byte0 & 0x02 ? 1:0,
193                         byte0 & 0x01 ? 1:0);
194         tc = proto_tree_add_text(tree, tvb,
195                         offset, header_len,
196                         "Header octet: %s (0x%02X)", info, byte0);
197         h_tree = proto_item_add_subtree(tc, ett_v120_header);
198         proto_tree_add_text(h_tree, tvb, offset, header_len, "%s",
199                     decode_boolean_bitfield(header, 0x80, nbits,
200                         "No extension octet", "Extension octet follows"));
201         proto_tree_add_text(h_tree, tvb, offset, header_len, "%s",
202                     decode_boolean_bitfield(header, 0x40, nbits,
203                         "Break condition", "No break condition"));
204         g_snprintf(info, 80, "Error control C1/C2: %d", (header & 0x0c) >> 2);
205         proto_tree_add_text(h_tree, tvb, offset, header_len, "%s",
206                     decode_numeric_bitfield(header, 0x0c, nbits, info));
207         proto_tree_add_text(h_tree, tvb, offset, header_len, "%s",
208                     decode_boolean_bitfield(header, 0x02, nbits,
209                         "Segmentation bit B", "No segmentation bit B"));
210         proto_tree_add_text(h_tree, tvb, offset, header_len, "%s",
211                     decode_boolean_bitfield(header, 0x01, nbits,
212                         "Segmentation bit F", "No segmentation bit F"));
213         if (header_len == 2) {
214                 proto_tree_add_text(h_tree, tvb, offset, header_len,
215                     decode_boolean_bitfield(header, 0x8000, nbits,
216                         "E", "E bit not set (Error)"), NULL);
217                 proto_tree_add_text(h_tree, tvb, offset, header_len,
218                     decode_boolean_bitfield(header, 0x4000, nbits,
219                         "DR", "No DR"), NULL);
220                 proto_tree_add_text(h_tree, tvb, offset, header_len,
221                     decode_boolean_bitfield(header, 0x2000, nbits,
222                         "SR", "No SR"), NULL);
223                 proto_tree_add_text(h_tree, tvb, offset, header_len,
224                     decode_boolean_bitfield(header, 0x1000, nbits,
225                         "RR", "No RR"), NULL);
226         }
227         return header_len;
228 }
229
230 void
231 proto_register_v120(void)
232 {
233     static hf_register_info hf[] = {
234         { &hf_v120_address,
235           { "Link Address", "v120.address", FT_UINT16, BASE_HEX, NULL,
236                   0x0, NULL, HFILL }},
237         { &hf_v120_control,
238           { "Control Field", "v120.control", FT_UINT16, BASE_HEX, NULL, 0x0,
239                 NULL, HFILL }},
240         { &hf_v120_n_r,
241             { "N(R)", "v120.control.n_r", FT_UINT16, BASE_DEC,
242                 NULL, XDLC_N_R_EXT_MASK, NULL, HFILL }},
243         { &hf_v120_n_s,
244             { "N(S)", "v120.control.n_s", FT_UINT16, BASE_DEC,
245                 NULL, XDLC_N_S_EXT_MASK, NULL, HFILL }},
246         { &hf_v120_p,
247             { "Poll", "v120.control.p", FT_BOOLEAN, 8,
248                 TFS(&tfs_set_notset), XDLC_P_F, NULL, HFILL }},
249         { &hf_v120_p_ext,
250             { "Poll", "v120.control.p", FT_BOOLEAN, 16,
251                 TFS(&tfs_set_notset), XDLC_P_F_EXT, NULL, HFILL }},
252         { &hf_v120_f,
253             { "Final", "v120.control.f", FT_BOOLEAN, 8,
254                 TFS(&tfs_set_notset), XDLC_P_F, NULL, HFILL }},
255         { &hf_v120_f_ext,
256             { "Final", "v120.control.f", FT_BOOLEAN, 16,
257                 TFS(&tfs_set_notset), XDLC_P_F_EXT, NULL, HFILL }},
258         { &hf_v120_s_ftype,
259             { "Supervisory frame type", "v120.control.s_ftype", FT_UINT16, BASE_HEX,
260                 VALS(stype_vals), XDLC_S_FTYPE_MASK, NULL, HFILL }},
261         { &hf_v120_u_modifier_cmd,
262             { "Command", "v120.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
263                 VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
264         { &hf_v120_u_modifier_resp,
265             { "Response", "v120.control.u_modifier_resp", FT_UINT8, BASE_HEX,
266                 VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
267         { &hf_v120_ftype_i,
268             { "Frame type", "v120.control.ftype", FT_UINT16, BASE_HEX,
269                 VALS(ftype_vals), XDLC_I_MASK, NULL, HFILL }},
270         { &hf_v120_ftype_s_u,
271             { "Frame type", "v120.control.ftype", FT_UINT8, BASE_HEX,
272                 VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},
273         { &hf_v120_ftype_s_u_ext,
274             { "Frame type", "v120.control.ftype", FT_UINT16, BASE_HEX,
275                 VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},
276         { &hf_v120_header,
277           { "Header Field", "v120.header", FT_STRING, BASE_NONE, NULL, 0x0,
278                 NULL, HFILL }},
279     };
280     static gint *ett[] = {
281         &ett_v120,
282         &ett_v120_address,
283         &ett_v120_control,
284         &ett_v120_header,
285     };
286
287     proto_v120 = proto_register_protocol("Async data over ISDN (V.120)",
288                                          "V.120", "v120");
289     proto_register_field_array (proto_v120, hf, array_length(hf));
290     proto_register_subtree_array(ett, array_length(ett));
291
292     register_dissector("v120", dissect_v120, proto_v120);
293 }
294
295 void
296 proto_reg_handoff_v120(void)
297 {
298     data_handle = find_dissector("data");
299 }