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