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