Change from Ed Meaney - write capture files in binary, rather than ASCII
[obnox/wireshark/wip.git] / packet-sscop.c
1 /* packet-sscop.c
2  * Routines for SSCOP (Q.2110, Q.SAAL) frame disassembly
3  * Guy Harris <guy@alum.mit.edu>
4  *
5  * $Id: packet-sscop.c,v 1.4 1999/11/23 07:19:14 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
40 static int proto_sscop = -1;
41
42 static gint ett_sscop = -1;
43
44 /*
45  * See
46  *
47  *      http://www.protocols.com/pbook/atmsig.htm
48  *
49  * for some information on SSCOP, although, alas, not the actual PDU
50  * type values - those I got from the FreeBSD 3.2 ATM code.
51  */
52
53 /*
54  * SSCOP PDU types.
55  */
56 #define SSCOP_TYPE_MASK 0x0f
57
58 #define SSCOP_BGN       0x01    /* Begin */
59 #define SSCOP_BGAK      0x02    /* Begin Acknowledge */
60 #define SSCOP_BGREJ     0x07    /* Begin Reject */
61 #define SSCOP_END       0x03    /* End */
62 #define SSCOP_ENDAK     0x04    /* End Acknowledge */
63 #define SSCOP_RS        0x05    /* Resynchronization */
64 #define SSCOP_RSAK      0x06    /* Resynchronization Acknowledge */
65 #define SSCOP_SD        0x08    /* Sequenced Data */
66 #define SSCOP_SDP       0x09    /* Sequenced Data with Poll */
67 #define SSCOP_POLL      0x0a    /* Status Request */
68 #define SSCOP_STAT      0x0b    /* Solicited Status Response */
69 #define SSCOP_USTAT     0x0c    /* Unsolicited Status Response */
70 #define SSCOP_UD        0x0d    /* Unnumbered Data */
71 #define SSCOP_MD        0x0e    /* Management Data */
72 #define SSCOP_ER        0x09    /* Error Recovery */
73 #define SSCOP_ERAK      0x0f    /* Error Acknowledge */
74
75 #define SSCOP_S         0x10    /* Source bit in End PDU */
76
77 /*
78  * XXX - how to distinguish SDP from ER?
79  */
80 static const value_string sscop_type_vals[] = {
81         { SSCOP_BGN,   "Begin" },
82         { SSCOP_BGAK,  "Begin Acknowledge" },
83         { SSCOP_BGREJ, "Begin Reject" },
84         { SSCOP_END,   "End" },
85         { SSCOP_ENDAK, "End Acknowledge" },
86         { SSCOP_RS,    "Resynchronization" },
87         { SSCOP_RSAK,  "Resynchronization Acknowledge" },
88         { SSCOP_SD,    "Sequenced Data" },
89 #if 0
90         { SSCOP_SDP,   "Sequenced Data with Poll" },
91 #endif
92         { SSCOP_POLL,  "Status Request" },
93         { SSCOP_STAT,  "Solicited Status Response" },
94         { SSCOP_USTAT, "Unsolicited Status Response" },
95         { SSCOP_UD,    "Unnumbered Data" },
96         { SSCOP_MD,    "Management Data" },
97         { SSCOP_ER,    "Error Recovery" },
98         { SSCOP_ERAK,  "Error Acknowledge" },
99         { 0,            NULL }
100 };
101
102 /*
103  * The SSCOP "header" is a trailer, so the "offsets" are computed based
104  * on the length of the packet.
105  */
106
107 /*
108  * PDU type.
109  */
110 #define SSCOP_PDU_TYPE  (pi.len - 4)    /* single byte */
111
112 /*
113  * Begin PDU, Begin Acknowledge PDU (no N(SQ) in it), Resynchronization
114  * PDU, Resynchronization Acknowledge PDU (no N(SQ) in it in Q.SAAL),
115  * Error Recovery PDU, Error Recovery Acknoledge PDU (no N(SQ) in it).
116  */
117 #define SSCOP_N_SQ      (pi.len - 5)    /* One byte */
118 #define SSCOP_N_MR      (pi.len - 4)    /* lower 3 bytes thereof */
119
120 /*
121  * Sequenced Data PDU (no N(PS) in it), Sequenced Data with Poll PDU,
122  * Poll PDU.
123  */
124 #define SSCOP_N_PS      (pi.len - 8)    /* lower 3 bytes thereof */
125 #define SSCOP_N_S       (pi.len - 4)    /* lower 3 bytes thereof */
126
127 /*
128  * Solicited Status PDU, Unsolicited Status PDU (no N(PS) in it).
129  */
130 #define SSCOP_SS_N_PS   (pi.len - 12)   /* lower 3 bytes thereof */
131 #define SSCOP_SS_N_MR   (pi.len - 8)    /* lower 3 bytes thereof */
132 #define SSCOP_SS_N_R    (pi.len - 4)    /* lower 3 bytes thereof */
133
134 void
135 dissect_sscop(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) 
136 {
137   proto_item *ti;
138   proto_tree *sscop_tree = NULL;
139   guint8 pdu_type;
140   int pdu_len;
141   int pad_len;
142
143   /*
144    * The SSCOP "header" is a trailer, and the PDU type is in the
145    * last-minus-3 byte of the frame; if the captured length is less
146    * than the actual length by 3 or more bytes, give up, as we don't
147    * have the PDU type.
148    */
149   if ((pi.len - pi.captured_len) >= 3) {
150     dissect_data(pd, offset, fd, tree);
151     return;
152   }
153   pdu_type = pd[SSCOP_PDU_TYPE] & SSCOP_TYPE_MASK;
154   if (check_col(fd, COL_PROTOCOL))
155     col_add_str(fd, COL_PROTOCOL, "SSCOP");
156   if (check_col(fd, COL_INFO))
157     col_add_str(fd, COL_INFO, val_to_str(pdu_type, sscop_type_vals,
158                                         "Unknown PDU type (0x%02x)"));
159
160   /*
161    * Find the length of the PDU and, if there's any payload and
162    * padding, the length of the padding.
163    */
164   switch (pdu_type) {
165
166   case SSCOP_SD:
167     pad_len = (pd[SSCOP_PDU_TYPE] >> 6) & 0x03;
168     pdu_len = 4;
169     break;
170
171   case SSCOP_BGN:
172   case SSCOP_BGAK:
173   case SSCOP_BGREJ:
174   case SSCOP_END:
175   case SSCOP_RS:
176 #if 0
177   case SSCOP_SDP:
178 #endif
179     pad_len = (pd[SSCOP_PDU_TYPE] >> 6) & 0x03;
180     pdu_len = 8;
181     break;
182
183   case SSCOP_UD:
184     pad_len = (pd[SSCOP_PDU_TYPE] >> 6) & 0x03;
185     pdu_len = 4;
186     break;
187
188   default:
189     pad_len = 0;
190     pdu_len = pi.len;   /* No payload, just SSCOP */
191     break;
192   }
193   if (tree) {
194     ti = proto_tree_add_item_format(tree, proto_sscop, pi.len - pdu_len,
195                                         pdu_len, NULL, "SSCOP");
196     sscop_tree = proto_item_add_subtree(ti, ett_sscop);
197
198     proto_tree_add_text(sscop_tree, SSCOP_PDU_TYPE, 1,
199                         "PDU Type: %s",
200                         val_to_str(pdu_type, sscop_type_vals,
201                                 "Unknown (0x%02x)"));
202
203     switch (pdu_type) {
204
205     case SSCOP_BGN:
206     case SSCOP_RS:
207     case SSCOP_ER:
208       proto_tree_add_text(sscop_tree, SSCOP_N_SQ, 1,
209           "N(SQ): %u", pd[SSCOP_N_SQ]);
210       proto_tree_add_text(sscop_tree, SSCOP_N_MR + 1, 3,
211           "N(MR): %u", pntohl(&pd[SSCOP_N_MR]) & 0xFFFFFF);
212       break;
213
214     case SSCOP_END:
215       proto_tree_add_text(sscop_tree, SSCOP_PDU_TYPE, 1,
216           "Source: %s", (pd[SSCOP_PDU_TYPE] & SSCOP_S) ? "SSCOP" : "User");
217       break;
218
219     case SSCOP_BGAK:
220     case SSCOP_RSAK:
221       proto_tree_add_text(sscop_tree, SSCOP_N_MR + 1, 3,
222           "N(MR): %u", pntohl(&pd[SSCOP_N_MR]) & 0xFFFFFF);
223       break;
224
225     case SSCOP_ERAK:
226       proto_tree_add_text(sscop_tree, SSCOP_N_MR + 3, 3,
227           "N(MR): %u", pntohl(&pd[SSCOP_N_MR]) & 0xFFFFFF);
228       break;
229
230     case SSCOP_SD:
231       proto_tree_add_text(sscop_tree, SSCOP_N_S + 1, 3,
232           "N(S): %u", pntohl(&pd[SSCOP_N_S]) & 0xFFFFFF);
233       break;
234
235 #if 0
236     case SSCOP_SDP:
237 #endif
238     case SSCOP_POLL:
239       proto_tree_add_text(sscop_tree, SSCOP_N_PS + 1, 3,
240           "N(PS): %u", pntohl(&pd[SSCOP_N_PS]) & 0xFFFFFF);
241       proto_tree_add_text(sscop_tree, SSCOP_N_S + 1, 3,
242           "N(S): %u", pntohl(&pd[SSCOP_N_S]) & 0xFFFFFF);
243       break;
244
245     case SSCOP_STAT:
246       /*
247        * XXX - dissect the list elements....
248        */
249       proto_tree_add_text(sscop_tree, SSCOP_SS_N_PS + 1, 3,
250           "N(PS): %u", pntohl(&pd[SSCOP_SS_N_PS]) & 0xFFFFFF);
251       proto_tree_add_text(sscop_tree, SSCOP_SS_N_MR + 1, 3,
252           "N(MR): %u", pntohl(&pd[SSCOP_SS_N_MR]) & 0xFFFFFF);
253       proto_tree_add_text(sscop_tree, SSCOP_SS_N_R + 1, 3,
254           "N(R): %u", pntohl(&pd[SSCOP_SS_N_R]) & 0xFFFFFF);
255       break;
256
257     case SSCOP_USTAT:
258       /*
259        * XXX - dissect the list elements....
260        */
261       proto_tree_add_text(sscop_tree, SSCOP_SS_N_MR + 1, 3,
262           "N(MR): %u", pntohl(&pd[SSCOP_SS_N_MR]) & 0xFFFFFF);
263       proto_tree_add_text(sscop_tree, SSCOP_SS_N_R + 1, 3,
264           "N(R): %u", pntohl(&pd[SSCOP_SS_N_R]) & 0xFFFFFF);
265       break;
266     }
267   }
268
269   /*
270    * Dissect the payload, if any.
271    *
272    * XXX - what about a Management Data PDU?
273    */
274   switch (pdu_type) {
275
276   case SSCOP_SD:
277   case SSCOP_UD:
278   case SSCOP_BGN:
279   case SSCOP_BGAK:
280   case SSCOP_BGREJ:
281   case SSCOP_END:
282   case SSCOP_RS:
283 #if 0
284   case SSCOP_SDP:
285 #endif
286     if (tree) {
287       proto_tree_add_text(sscop_tree, SSCOP_PDU_TYPE, 1,
288                         "Pad length: %u", pad_len);
289     }
290
291     /*
292      * Compute length of data in PDU - subtract the trailer length
293      * and the pad length.
294      */
295     pi.len -= (pdu_len + pad_len);
296     if (pi.len < pi.captured_len)
297       pi.captured_len = pi.len;
298
299     /*
300      * XXX - if more than just Q.2931 uses SSCOP, we need to tell
301      * SSCOP what dissector to use here.
302      */
303     if (pi.len != 0) {
304       if (pdu_type == SSCOP_SD)
305         dissect_q2931(pd, offset, fd, tree);
306       else
307         dissect_data(pd, offset, fd, tree);
308     }
309     break;
310   }
311 }
312
313 void
314 proto_register_sscop(void)
315 {
316         static gint *ett[] = {
317                 &ett_sscop,
318         };
319         proto_sscop = proto_register_protocol("SSCOP", "sscop");
320         proto_register_subtree_array(ett, array_length(ett));
321 }