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