Don't guard col_set_str (COL_PROTOCOL) with col_check
[obnox/wireshark/wip.git] / epan / dissectors / packet-x29.c
1 /* packet-x29.c
2  * Routines for X.29 packet dissection
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 <epan/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         col_set_str(pinfo->cinfo, COL_PROTOCOL, "X.29");
95         if (check_col(pinfo->cinfo, COL_INFO))
96                 col_clear(pinfo->cinfo, COL_INFO);
97
98         if (tree) {
99                 ti = proto_tree_add_item(tree, proto_x29, tvb, offset, -1,
100                     FALSE);
101                 x29_tree = proto_item_add_subtree(ti, ett_x29);
102         }
103
104         if (*q_bit_set) {
105                 /*
106                  * Q bit set - this is a PAD message.
107                  */
108                 msg_code = tvb_get_guint8(tvb, offset);
109                 if (check_col(pinfo->cinfo, COL_INFO)) {
110                         col_add_fstr(pinfo->cinfo, COL_INFO, "%s PAD message",
111                             val_to_str(msg_code, message_code_vals,
112                                 "Unknown (0x%02x)"));
113                 }
114                 proto_tree_add_uint(x29_tree, hf_msg_code, tvb,
115                     offset, 1, msg_code);
116                 offset++;
117
118                 switch (msg_code) {
119
120                 case SET_MSG:
121                 case READ_MSG:
122                 case SET_AND_READ_MSG:
123                 case PARAMETER_IND_MSG:
124                         /*
125                          * XXX - dissect the references as per X.3.
126                          */
127                         while (tvb_reported_length_remaining(tvb, offset) > 0) {
128                                 proto_tree_add_text(x29_tree, tvb, offset, 2,
129                                     "Parameter %u, value %u",
130                                     tvb_get_guint8(tvb, offset),
131                                     tvb_get_guint8(tvb, offset + 1));
132                                 offset += 2;
133                         }
134                         break;
135
136                 case INV_TO_CLEAR_MSG:
137                         /*
138                          * No data for this message.
139                          */
140                         break;
141
142                 case ERROR_MSG:
143                         error_type = tvb_get_guint8(tvb, offset);
144                         proto_tree_add_uint(x29_tree, hf_error_type, tvb,
145                             offset, 1, error_type);
146                         offset++;
147                         if (error_type != 0) {
148                                 proto_tree_add_item(x29_tree, hf_inv_msg_code,
149                                     tvb, offset, 1, FALSE);
150                         }
151                         break;
152
153                 case BREAK_IND_MSG:
154                         if (tvb_reported_length_remaining(tvb, offset) > 0) {
155                                 type_ref = tvb_get_guint8(tvb, offset);
156                                 switch (type_ref) {
157
158                                 case 0x01:      /* change in PAD Aspect */
159                                         /*
160                                          * XXX - dissect as per X.28.
161                                          */
162                                         proto_tree_add_text(x29_tree, tvb,
163                                             offset, 1, "Type reference: Change in PAD Aspect");
164                                         offset++;
165                                         proto_tree_add_text(x29_tree, tvb,
166                                             offset, 1, "Type of aspect: 0x%02x",
167                                             type_ref);
168                                         offset++;
169                                         break;
170
171                                 case 0x08:      /* break */
172                                         proto_tree_add_text(x29_tree, tvb,
173                                             offset, 1, "Type reference: Break");
174                                         offset++;
175                                         proto_tree_add_text(x29_tree, tvb,
176                                             offset, 1, "Break value: 0x%02x",
177                                             type_ref);
178                                         offset++;
179                                         break;
180
181                                 default:
182                                         proto_tree_add_text(x29_tree, tvb,
183                                             offset, 1, "Unknown type reference (0x%02x)",
184                                             type_ref);
185                                         offset++;
186                                         proto_tree_add_text(x29_tree, tvb,
187                                             offset, 1, "Type value: 0x%02x",
188                                             type_ref);
189                                         offset++;
190                                         break;
191                                 }
192                         }
193                         break;
194
195                 case RESELECTION_MSG:
196                         /*
197                          * XXX - dissect me.
198                          */
199                         proto_tree_add_text(x29_tree, tvb, offset, -1,
200                             "Reselection message data");
201                         break;
202
203                 case RESEL_WITH_TOA_NPI_MSG:
204                         /*
205                          * XXX - dissect me.
206                          */
207                         proto_tree_add_text(x29_tree, tvb, offset, -1,
208                             "Reselection message data");
209                         break;
210
211                 default:
212                         proto_tree_add_text(x29_tree, tvb, offset, -1,
213                             "PAD message data");
214                         break;
215                 }
216         } else {
217                 /*
218                  * Q bit not set - this is data.
219                  */
220                 if (check_col(pinfo->cinfo, COL_INFO))
221                         col_set_str(pinfo->cinfo, COL_INFO, "Data ...");
222
223                 if (tree) {
224                         while (tvb_offset_exists(tvb, offset)) {
225                                 /*
226                                  * Find the end of the line.
227                                  */
228                                 linelen = tvb_find_line_end(tvb, offset, -1,
229                                     &next_offset, FALSE);
230
231                                 /*
232                                  * Now compute the length of the line
233                                  * *including* the end-of-line indication,
234                                  * if any; we display it all.
235                                  */
236                                 linelen = next_offset - offset;
237
238                                 proto_tree_add_text(x29_tree, tvb, offset,
239                                     linelen, "Data: %s",
240                                     tvb_format_text(tvb, offset, linelen));
241                                 offset = next_offset;
242                         }
243                 }
244         }
245 }
246
247 void
248 proto_register_x29(void)
249 {
250         static hf_register_info hf[] = {
251             { &hf_msg_code,
252                 { "Message code", "x29.msg_code", FT_UINT8, BASE_HEX,
253                   VALS(message_code_vals), 0x0, "X.29 PAD message code",
254                   HFILL }},
255             { &hf_error_type,
256                 { "Error type", "x29.error_type", FT_UINT8, BASE_HEX,
257                   VALS(error_type_vals), 0x0, "X.29 error PAD message error type",
258                   HFILL }},
259             { &hf_inv_msg_code,
260                 { "Invalid message code", "x29.inv_msg_code", FT_UINT8, BASE_HEX,
261                   VALS(message_code_vals), 0x0, "X.29 Error PAD message invalid message code",
262                   HFILL }},
263         };
264         static gint *ett[] = {
265                 &ett_x29,
266         };
267
268         proto_x29 = proto_register_protocol("X.29", "X.29", "x29");
269         proto_register_field_array(proto_x29, hf, array_length(hf));
270         proto_register_subtree_array(ett, array_length(ett));
271 }
272
273 void
274 proto_reg_handoff_x29(void)
275 {
276         dissector_handle_t x29_handle;
277
278         x29_handle = create_dissector_handle(dissect_x29, proto_x29);
279         dissector_add("x.25.spi", NLPID_SPI_X_29, x29_handle);
280 }