#if 0 out the stuff to set the reported length, as it'd throw an
[obnox/wireshark/wip.git] / packet-x29.c
1 /* packet-x29.c
2  * Routines for X.29 packet dissection
3  *
4  * $Id: packet-x29.c,v 1.1 2003/01/06 02:24:57 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #include <string.h>
32 #include <glib.h>
33 #include <epan/packet.h>
34 #include <epan/strutil.h>
35 #include "nlpid.h"
36
37 static int proto_x29 = -1;
38 static int hf_msg_code = -1;
39 static int hf_error_type = -1;
40 static int hf_inv_msg_code = -1;
41
42 static gint ett_x29 = -1;
43
44 /*
45  * PAD messages.
46  */
47 #define SET_MSG                 0x02
48 #define READ_MSG                0x04
49 #define SET_AND_READ_MSG        0x06
50 #define PARAMETER_IND_MSG       0x00
51 #define INV_TO_CLEAR_MSG        0x01
52 #define BREAK_IND_MSG           0x03
53 #define RESELECTION_MSG         0x07
54 #define ERROR_MSG               0x05
55 #define RESEL_WITH_TOA_NPI_MSG  0x08
56
57 static const value_string message_code_vals[] = {
58         { SET_MSG,                      "Set" },
59         { READ_MSG,                     "Read" },
60         { SET_AND_READ_MSG,             "Set and read" },
61         { PARAMETER_IND_MSG,            "Parameter indication" },
62         { INV_TO_CLEAR_MSG,             "Invitation to clear" },
63         { BREAK_IND_MSG,                "Indication of break" },
64         { RESELECTION_MSG,              "Reselection" },
65         { ERROR_MSG,                    "Error" },
66         { RESEL_WITH_TOA_NPI_MSG,       "Reselection with TOA/NPI" },
67         { 0,                            NULL }
68 };
69
70 static const value_string error_type_vals[] = {
71         { 0x00, "Received PAD message contained less than eight bits" },
72         { 0x02, "Unrecognized message code in received PAD message" },
73         { 0x04, "Parameter field format was incorrect or incompatible with message code" },
74         { 0x06, "Received PAD message did not contain an integral number of octets" },
75         { 0x08, "Received Parameter Indication PAD message was unsolicited" },
76         { 0x0A, "Received PAD message was too long" },
77         { 0x0C, "Unauthorized reselection PAD message" },
78         { 0,    NULL },
79 };
80
81 static void
82 dissect_x29(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
83 {
84         int offset = 0;
85         proto_tree *x29_tree = NULL;
86         proto_item *ti;
87         gboolean *q_bit_set = pinfo->private_data;
88         guint8 msg_code;
89         guint8 error_type;
90         guint8 type_ref;
91         gint next_offset;
92         int linelen;
93
94         if (check_col(pinfo->cinfo, COL_PROTOCOL))
95                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "X.29");
96         if (check_col(pinfo->cinfo, COL_INFO))
97                 col_clear(pinfo->cinfo, COL_INFO);
98
99         if (tree) {
100                 ti = proto_tree_add_item(tree, proto_x29, tvb, offset, -1,
101                     FALSE);
102                 x29_tree = proto_item_add_subtree(ti, ett_x29);
103         }
104
105         if (*q_bit_set) {
106                 /*
107                  * Q bit set - this is a PAD message.
108                  */
109                 msg_code = tvb_get_guint8(tvb, offset);
110                 if (check_col(pinfo->cinfo, COL_INFO)) {
111                         col_add_fstr(pinfo->cinfo, COL_INFO, "%s PAD message",
112                             val_to_str(msg_code, message_code_vals,
113                                 "Unknown (0x%02x)"));
114                 }
115                 proto_tree_add_uint(x29_tree, hf_msg_code, tvb,
116                     offset, 1, msg_code);
117                 offset++;
118
119                 switch (msg_code) {
120
121                 case SET_MSG:
122                 case READ_MSG:
123                 case SET_AND_READ_MSG:
124                 case PARAMETER_IND_MSG:
125                         /*
126                          * XXX - dissect the references as per X.3.
127                          */
128                         while (tvb_reported_length_remaining(tvb, offset) > 0) {
129                                 proto_tree_add_text(x29_tree, tvb, offset, 2,
130                                     "Parameter %u, value %u",
131                                     tvb_get_guint8(tvb, offset),
132                                     tvb_get_guint8(tvb, offset + 1));
133                                 offset += 2;
134                         }
135                         break;
136
137                 case INV_TO_CLEAR_MSG:
138                         /*
139                          * No data for this message.
140                          */
141                         break;
142
143                 case ERROR_MSG:
144                         error_type = tvb_get_guint8(tvb, offset);
145                         proto_tree_add_uint(x29_tree, hf_error_type, tvb,
146                             offset, 1, error_type);
147                         offset++;
148                         if (error_type != 0) {
149                                 proto_tree_add_item(x29_tree, hf_inv_msg_code,
150                                     tvb, offset, 1, FALSE);
151                         }
152                         break;
153
154                 case BREAK_IND_MSG:
155                         if (tvb_reported_length_remaining(tvb, offset) > 0) {
156                                 type_ref = tvb_get_guint8(tvb, offset);
157                                 switch (type_ref) {
158
159                                 case 0x01:      /* change in PAD Aspect */
160                                         /*
161                                          * XXX - dissect as per X.28.
162                                          */
163                                         proto_tree_add_text(x29_tree, tvb,
164                                             offset, 1, "Type reference: Change in PAD Aspect");
165                                         offset++;
166                                         proto_tree_add_text(x29_tree, tvb,
167                                             offset, 1, "Type of aspect: 0x%02x",
168                                             type_ref);
169                                         offset++;
170                                         break;
171
172                                 case 0x08:      /* break */
173                                         proto_tree_add_text(x29_tree, tvb,
174                                             offset, 1, "Type reference: Break");
175                                         offset++;
176                                         proto_tree_add_text(x29_tree, tvb,
177                                             offset, 1, "Break value: 0x%02x",
178                                             type_ref);
179                                         offset++;
180                                         break;
181
182                                 default:
183                                         proto_tree_add_text(x29_tree, tvb,
184                                             offset, 1, "Unknown type reference (0x%02x)",
185                                             type_ref);
186                                         offset++;
187                                         proto_tree_add_text(x29_tree, tvb,
188                                             offset, 1, "Type value: 0x%02x",
189                                             type_ref);
190                                         offset++;
191                                         break;
192                                 }
193                         }
194                         break;
195
196                 case RESELECTION_MSG:
197                         /*
198                          * XXX - dissect me.
199                          */
200                         proto_tree_add_text(x29_tree, tvb, offset, -1,
201                             "Reselection message data");
202                         break;
203
204                 case RESEL_WITH_TOA_NPI_MSG:
205                         /*
206                          * XXX - dissect me.
207                          */
208                         proto_tree_add_text(x29_tree, tvb, offset, -1,
209                             "Reselection message data");
210                         break;
211
212                 default:
213                         proto_tree_add_text(x29_tree, tvb, offset, -1,
214                             "PAD message data");
215                         break;
216                 }
217         } else {
218                 /*
219                  * Q bit not set - this is data.
220                  */
221                 if (check_col(pinfo->cinfo, COL_INFO))
222                         col_add_fstr(pinfo->cinfo, COL_INFO, "Data ...");
223
224                 if (tree) {
225                         while (tvb_offset_exists(tvb, offset)) {
226                                 /*
227                                  * Find the end of the line.
228                                  */
229                                 linelen = tvb_find_line_end(tvb, offset, -1,
230                                     &next_offset, FALSE);
231
232                                 /*
233                                  * Now compute the length of the line
234                                  * *including* the end-of-line indication,
235                                  * if any; we display it all.
236                                  */
237                                 linelen = next_offset - offset;
238
239                                 proto_tree_add_text(x29_tree, tvb, offset,
240                                     linelen, "Data: %s",
241                                     tvb_format_text(tvb, offset, linelen));
242                                 offset = next_offset;
243                         }
244                 }
245         }
246 }
247
248 void
249 proto_register_x29(void)
250 {
251         static hf_register_info hf[] = {
252             { &hf_msg_code,
253                 { "Message code", "x29.msg_code", FT_UINT8, BASE_HEX,
254                   VALS(message_code_vals), 0x0, "X.29 PAD message code",
255                   HFILL }},
256             { &hf_error_type,
257                 { "Error type", "x29.error_type", FT_UINT8, BASE_HEX,
258                   VALS(error_type_vals), 0x0, "X.29 error PAD message error type",
259                   HFILL }},
260             { &hf_inv_msg_code,
261                 { "Invalid message code", "x29.inv_msg_code", FT_UINT8, BASE_HEX,
262                   VALS(message_code_vals), 0x0, "X.29 Error PAD message invalid message code",
263                   HFILL }},
264         };
265         static gint *ett[] = {
266                 &ett_x29,
267         };
268
269         proto_x29 = proto_register_protocol("X.29", "X.29", "x.29");
270         proto_register_field_array(proto_x29, hf, array_length(hf));
271         proto_register_subtree_array(ett, array_length(ett));
272 }
273
274 void
275 proto_reg_handoff_x29(void)
276 {
277         dissector_handle_t x29_handle;
278
279         x29_handle = create_dissector_handle(dissect_x29, proto_x29);
280         dissector_add("x.25.spi", NLPID_SPI_X_29, x29_handle);
281 }