41485ecde4dbed400228c2d63836b7e54bf49a70
[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.13 2000/11/16 07:35:38 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 "packet-v120.h"
40 #include "xdlc.h"
41
42 #define FROM_DCE        0x80
43
44 static int proto_v120 = -1;
45 static int hf_v120_address = -1;
46 static int hf_v120_control = -1;
47 static int hf_v120_header = -1;
48
49 static gint ett_v120 = -1;
50 static gint ett_v120_address = -1;
51 static gint ett_v120_control = -1;
52 static gint ett_v120_header = -1;
53
54 static int dissect_v120_header(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
55
56 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     CHECK_DISPLAY_AS_DATA(proto_v120, tvb, pinfo, tree);
70
71     pinfo->current_proto = "V.120";
72
73     if (check_col(pinfo->fd, COL_PROTOCOL))
74         col_add_str(pinfo->fd, COL_PROTOCOL, "V.120");
75
76     byte0 = tvb_get_guint8(tvb, 0);
77
78     if(check_col(pinfo->fd, COL_RES_DL_SRC))
79         col_add_fstr(pinfo->fd, COL_RES_DL_SRC, "0x%02X", byte0);
80
81     byte1 = tvb_get_guint8(tvb, 1);
82
83     if ((byte0 & 0x01) != 0x00 && (byte1 && 0x01) != 0x01)
84     {
85         if (check_col(pinfo->fd, COL_INFO))
86             col_add_str(pinfo->fd, COL_INFO, "Invalid V.120 frame");
87         if (tree)
88             ti = proto_tree_add_protocol_format(tree, proto_v120, tvb, 0, tvb_length(tvb),
89                                             "Invalid V.120 frame");
90         return;
91     }
92
93     if (pinfo->pseudo_header->x25.flags & FROM_DCE) {
94         if(check_col(pinfo->fd, COL_RES_DL_DST))
95             col_add_str(pinfo->fd, COL_RES_DL_DST, "DTE");
96         if(check_col(pinfo->fd, COL_RES_DL_SRC))
97             col_add_str(pinfo->fd, COL_RES_DL_SRC, "DCE");
98     }
99     else {
100         if(check_col(pinfo->fd, COL_RES_DL_DST))
101             col_add_str(pinfo->fd, COL_RES_DL_DST, "DCE");
102         if(check_col(pinfo->fd, COL_RES_DL_SRC))
103             col_add_str(pinfo->fd, COL_RES_DL_SRC, "DTE");
104     }
105
106     if (((pinfo->pseudo_header->x25.flags & FROM_DCE) && byte0 & 0x02) ||
107        (!(pinfo->pseudo_header->x25.flags & FROM_DCE) && !(byte0 & 0x02)))
108         is_response = TRUE;
109     else
110         is_response = FALSE;
111
112     if (tree) {
113         ti = proto_tree_add_protocol_format(tree, proto_v120, tvb, 0, 0, "V.120");
114         v120_tree = proto_item_add_subtree(ti, ett_v120);
115         addr = byte1 << 8 | byte0;
116         sprintf(info, "LLI: %d C/R: %s",
117                         ((byte0 & 0xfc) << 5) | ((byte1 & 0xfe) >> 1),
118                         byte0 & 0x02 ? "R" : "C");
119         tc = proto_tree_add_text(v120_tree, tvb,
120                         0, 2,
121                         "Address field: %s", info);
122         address_tree = proto_item_add_subtree(tc, ett_v120_address);
123         proto_tree_add_text(address_tree, tvb, 0, 2,
124                     decode_boolean_bitfield(addr, 0x0002, 2*8,
125                         "Response", "Command"), NULL);
126         sprintf(info, "LLI: %d", ((byte0 & 0xfc) << 5) | ((byte1 & 0xfe) >> 1));
127         proto_tree_add_text(address_tree, tvb, 0, 2,
128                     decode_numeric_bitfield(addr, 0xfefc, 2*8, info));
129         proto_tree_add_text(address_tree, tvb, 0, 2,
130                     decode_boolean_bitfield(addr, 0x0001, 2*8,
131                         "EA0 = 1 (Error)", "EA0 = 0"), NULL);
132         proto_tree_add_text(address_tree, tvb, 0, 2,
133                     decode_boolean_bitfield(addr, 0x0100, 2*8,
134                         "EA1 = 1", "EA1 = 0 (Error)"), NULL);
135     }
136     else {
137         v120_tree = NULL;
138         ti = NULL;
139     }
140     control = dissect_xdlc_control(tvb, 2, pinfo, v120_tree, hf_v120_control,
141             ett_v120_control, is_response, TRUE);
142     if (tree) {
143         v120len = 2 + XDLC_CONTROL_LEN(control, TRUE);
144         if (tvb_bytes_exist(tvb, v120len, 1))
145                 v120len += dissect_v120_header(tvb, v120len, pinfo, v120_tree);
146         proto_item_set_len(ti, v120len);
147         next_tvb = tvb_new_subset(tvb, v120len, -1, -1);
148         dissect_data(next_tvb, 0, pinfo, v120_tree);
149     }
150 }
151
152 static int
153 dissect_v120_header(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
154 {
155         char info[80];
156         int header_len, nbits;
157         int header;
158         proto_tree *h_tree, *tc;
159         guint8  byte0;
160
161         byte0 = tvb_get_guint8(tvb, offset);
162
163         if (byte0 & 0x80) {
164                 header_len = 1;
165                 header = byte0;
166         } else {
167                 header_len = 2;
168                 header = byte0 | tvb_get_guint8(tvb, offset + 1) << 8;
169         }
170         nbits = header_len * 8;
171         sprintf(info, "Header: B: %d F: %d", byte0 & 0x02 ? 1:0,
172                         byte0 & 0x01 ? 1:0);
173         tc = proto_tree_add_text(tree, tvb,
174                         offset, header_len,
175                         "Header octet: %s (0x%02X)", info, byte0);
176         h_tree = proto_item_add_subtree(tc, ett_v120_header);
177         proto_tree_add_text(h_tree, tvb, offset, header_len,
178                     decode_boolean_bitfield(header, 0x80, nbits,
179                         "No extension octet", "Extension octet follows"), NULL);
180         proto_tree_add_text(h_tree, tvb, offset, header_len,
181                     decode_boolean_bitfield(header, 0x40, nbits,
182                         "Break condition", "No break condition"), NULL);
183         sprintf(info, "Error control C1/C2: %d", (header & 0x0c) >> 2);
184         proto_tree_add_text(h_tree, tvb, offset, header_len,
185                     decode_numeric_bitfield(header, 0x0c, nbits, info));
186         proto_tree_add_text(h_tree, tvb, offset, header_len,
187                     decode_boolean_bitfield(header, 0x02, nbits,
188                         "Segmentation bit B", "No segmentation bit B"), NULL);
189         proto_tree_add_text(h_tree, tvb, offset, header_len,
190                     decode_boolean_bitfield(header, 0x01, nbits,
191                         "Segmentation bit F", "No segmentation bit F"), NULL);
192         if (header_len == 2) {
193                 proto_tree_add_text(h_tree, tvb, offset, header_len,
194                     decode_boolean_bitfield(header, 0x8000, nbits,
195                         "E", "E bit not set (Error)"), NULL);
196                 proto_tree_add_text(h_tree, tvb, offset, header_len,
197                     decode_boolean_bitfield(header, 0x4000, nbits,
198                         "DR", "No DR"), NULL);
199                 proto_tree_add_text(h_tree, tvb, offset, header_len,
200                     decode_boolean_bitfield(header, 0x2000, nbits,
201                         "SR", "No SR"), NULL);
202                 proto_tree_add_text(h_tree, tvb, offset, header_len,
203                     decode_boolean_bitfield(header, 0x1000, nbits,
204                         "RR", "No RR"), NULL);
205         }
206         return header_len;
207 }
208
209 void
210 proto_register_v120(void)
211 {
212     static hf_register_info hf[] = {
213         { &hf_v120_address,
214           { "Link Address", "v120.address", FT_UINT16, BASE_HEX, NULL,
215                   0x0, "" }},
216         { &hf_v120_control,
217           { "Control Field", "v120.control", FT_UINT16, BASE_HEX, NULL, 0x0,
218                 "" }},
219         { &hf_v120_header,
220           { "Header Field", "v120.header", FT_STRING, BASE_NONE, NULL, 0x0,
221                 "" }},
222     };
223     static gint *ett[] = {
224         &ett_v120,
225         &ett_v120_address,
226         &ett_v120_control,
227         &ett_v120_header,
228     };
229
230     proto_v120 = proto_register_protocol ("Async data over ISDN (V.120)", "v120");
231     proto_register_field_array (proto_v120, hf, array_length(hf));
232     proto_register_subtree_array(ett, array_length(ett));
233 }