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