Allow a length of -1 to be specified when adding FT_NONE and FT_PROTOCOL
[obnox/wireshark/wip.git] / packet-v120.c
1 /* packet-v120.c
2  * Routines for v120 frame disassembly
3  * Bert Driehuis <driehuis@playbeing.org>
4  *
5  * $Id: packet-v120.c,v 1.23 2002/01/20 22:12:29 guy Exp $
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 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <stdio.h>
35 #include <glib.h>
36 #include <string.h>
37 #include "packet.h"
38 #include "xdlc.h"
39
40 #define FROM_DCE        0x80
41
42 static int proto_v120 = -1;
43 static int hf_v120_address = -1;
44 static int hf_v120_control = -1;
45 static int hf_v120_header = -1;
46
47 static gint ett_v120 = -1;
48 static gint ett_v120_address = -1;
49 static gint ett_v120_control = -1;
50 static gint ett_v120_header = -1;
51
52 static dissector_handle_t data_handle;
53
54 static int dissect_v120_header(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
55
56 static void
57 dissect_v120(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
58 {
59     proto_tree  *v120_tree, *tc, *address_tree;
60     proto_item  *ti;
61     int         is_response;
62     int         addr;
63     char        info[80];
64     int         v120len;
65     guint8      byte0, byte1;
66     guint16     control;
67     tvbuff_t    *next_tvb;
68
69     if (check_col(pinfo->cinfo, COL_PROTOCOL))
70         col_set_str(pinfo->cinfo, COL_PROTOCOL, "V.120");
71     if (check_col(pinfo->cinfo, COL_INFO))
72         col_clear(pinfo->cinfo, COL_INFO);
73
74     byte0 = tvb_get_guint8(tvb, 0);
75
76     if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
77         col_add_fstr(pinfo->cinfo, COL_RES_DL_SRC, "0x%02X", byte0);
78
79     byte1 = tvb_get_guint8(tvb, 1);
80
81     if ((byte0 & 0x01) != 0x00 && (byte1 && 0x01) != 0x01)
82     {
83         if (check_col(pinfo->cinfo, COL_INFO))
84             col_set_str(pinfo->cinfo, COL_INFO, "Invalid V.120 frame");
85         if (tree)
86             ti = proto_tree_add_protocol_format(tree, proto_v120, tvb, 0, -1,
87                                             "Invalid V.120 frame");
88         return;
89     }
90
91     if (pinfo->pseudo_header->x25.flags & FROM_DCE) {
92         if(check_col(pinfo->cinfo, COL_RES_DL_DST))
93             col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DTE");
94         if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
95             col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DCE");
96     }
97     else {
98         if(check_col(pinfo->cinfo, COL_RES_DL_DST))
99             col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DCE");
100         if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
101             col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DTE");
102     }
103
104     if (((pinfo->pseudo_header->x25.flags & FROM_DCE) && byte0 & 0x02) ||
105        (!(pinfo->pseudo_header->x25.flags & FROM_DCE) && !(byte0 & 0x02)))
106         is_response = TRUE;
107     else
108         is_response = FALSE;
109
110     if (tree) {
111         ti = proto_tree_add_protocol_format(tree, proto_v120, tvb, 0, -1, "V.120");
112         v120_tree = proto_item_add_subtree(ti, ett_v120);
113         addr = byte1 << 8 | byte0;
114         sprintf(info, "LLI: %d C/R: %s",
115                         ((byte0 & 0xfc) << 5) | ((byte1 & 0xfe) >> 1),
116                         byte0 & 0x02 ? "R" : "C");
117         tc = proto_tree_add_text(v120_tree, tvb,
118                         0, 2,
119                         "Address field: %s", info);
120         address_tree = proto_item_add_subtree(tc, ett_v120_address);
121         proto_tree_add_text(address_tree, tvb, 0, 2,
122                     decode_boolean_bitfield(addr, 0x0002, 2*8,
123                         "Response", "Command"), NULL);
124         sprintf(info, "LLI: %d", ((byte0 & 0xfc) << 5) | ((byte1 & 0xfe) >> 1));
125         proto_tree_add_text(address_tree, tvb, 0, 2,
126                     decode_numeric_bitfield(addr, 0xfefc, 2*8, info));
127         proto_tree_add_text(address_tree, tvb, 0, 2,
128                     decode_boolean_bitfield(addr, 0x0001, 2*8,
129                         "EA0 = 1 (Error)", "EA0 = 0"), NULL);
130         proto_tree_add_text(address_tree, tvb, 0, 2,
131                     decode_boolean_bitfield(addr, 0x0100, 2*8,
132                         "EA1 = 1", "EA1 = 0 (Error)"), NULL);
133     }
134     else {
135         v120_tree = NULL;
136         ti = NULL;
137     }
138     control = dissect_xdlc_control(tvb, 2, pinfo, v120_tree, hf_v120_control,
139             ett_v120_control, is_response, TRUE);
140     if (tree) {
141         v120len = 2 + XDLC_CONTROL_LEN(control, TRUE);
142         if (tvb_bytes_exist(tvb, v120len, 1))
143                 v120len += dissect_v120_header(tvb, v120len, pinfo, v120_tree);
144         proto_item_set_len(ti, v120len);
145         next_tvb = tvb_new_subset(tvb, v120len, -1, -1);
146         call_dissector(data_handle,next_tvb, pinfo, v120_tree);
147     }
148 }
149
150 static int
151 dissect_v120_header(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
152 {
153         char info[80];
154         int header_len, nbits;
155         int header;
156         proto_tree *h_tree, *tc;
157         guint8  byte0;
158
159         byte0 = tvb_get_guint8(tvb, offset);
160
161         if (byte0 & 0x80) {
162                 header_len = 1;
163                 header = byte0;
164         } else {
165                 header_len = 2;
166                 header = byte0 | tvb_get_guint8(tvb, offset + 1) << 8;
167         }
168         nbits = header_len * 8;
169         sprintf(info, "Header: B: %d F: %d", byte0 & 0x02 ? 1:0,
170                         byte0 & 0x01 ? 1:0);
171         tc = proto_tree_add_text(tree, tvb,
172                         offset, header_len,
173                         "Header octet: %s (0x%02X)", info, byte0);
174         h_tree = proto_item_add_subtree(tc, ett_v120_header);
175         proto_tree_add_text(h_tree, tvb, offset, header_len,
176                     decode_boolean_bitfield(header, 0x80, nbits,
177                         "No extension octet", "Extension octet follows"), NULL);
178         proto_tree_add_text(h_tree, tvb, offset, header_len,
179                     decode_boolean_bitfield(header, 0x40, nbits,
180                         "Break condition", "No break condition"), NULL);
181         sprintf(info, "Error control C1/C2: %d", (header & 0x0c) >> 2);
182         proto_tree_add_text(h_tree, tvb, offset, header_len,
183                     decode_numeric_bitfield(header, 0x0c, nbits, info));
184         proto_tree_add_text(h_tree, tvb, offset, header_len,
185                     decode_boolean_bitfield(header, 0x02, nbits,
186                         "Segmentation bit B", "No segmentation bit B"), NULL);
187         proto_tree_add_text(h_tree, tvb, offset, header_len,
188                     decode_boolean_bitfield(header, 0x01, nbits,
189                         "Segmentation bit F", "No segmentation bit F"), NULL);
190         if (header_len == 2) {
191                 proto_tree_add_text(h_tree, tvb, offset, header_len,
192                     decode_boolean_bitfield(header, 0x8000, nbits,
193                         "E", "E bit not set (Error)"), NULL);
194                 proto_tree_add_text(h_tree, tvb, offset, header_len,
195                     decode_boolean_bitfield(header, 0x4000, nbits,
196                         "DR", "No DR"), NULL);
197                 proto_tree_add_text(h_tree, tvb, offset, header_len,
198                     decode_boolean_bitfield(header, 0x2000, nbits,
199                         "SR", "No SR"), NULL);
200                 proto_tree_add_text(h_tree, tvb, offset, header_len,
201                     decode_boolean_bitfield(header, 0x1000, nbits,
202                         "RR", "No RR"), NULL);
203         }
204         return header_len;
205 }
206
207 void
208 proto_register_v120(void)
209 {
210     static hf_register_info hf[] = {
211         { &hf_v120_address,
212           { "Link Address", "v120.address", FT_UINT16, BASE_HEX, NULL,
213                   0x0, "", HFILL }},
214         { &hf_v120_control,
215           { "Control Field", "v120.control", FT_UINT16, BASE_HEX, NULL, 0x0,
216                 "", HFILL }},
217         { &hf_v120_header,
218           { "Header Field", "v120.header", FT_STRING, BASE_NONE, NULL, 0x0,
219                 "", HFILL }},
220     };
221     static gint *ett[] = {
222         &ett_v120,
223         &ett_v120_address,
224         &ett_v120_control,
225         &ett_v120_header,
226     };
227
228     proto_v120 = proto_register_protocol("Async data over ISDN (V.120)",
229                                          "V.120", "v120");
230     proto_register_field_array (proto_v120, hf, array_length(hf));
231     proto_register_subtree_array(ett, array_length(ett));
232 }
233
234 void
235 proto_reg_handoff_v120(void)
236 {
237     dissector_handle_t v120_handle;
238
239     data_handle = find_dissector("data");
240     v120_handle = create_dissector_handle(dissect_v120, proto_v120);
241     dissector_add("wtap_encap", WTAP_ENCAP_V120, v120_handle);
242 }