2 * Copyright (c) 1996 Gary Jennejohn. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the author nor the names of any co-contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 *---------------------------------------------------------------------------*
30 * q.921.c - print Q.921 traces
31 * ----------------------------
33 * $Id: q921.c,v 1.4 1999/12/13 21:25:26 hm Exp $
35 * $FreeBSD: src/usr.sbin/i4b/isdntrace/q921.c,v 1.6.2.1 2001/08/01 17:45:08 obrien Exp $
36 * $DragonFly: src/usr.sbin/i4b/isdntrace/q921.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
38 * last edit-date: [Mon Dec 13 21:56:46 1999]
40 *---------------------------------------------------------------------------*/
44 /*---------------------------------------------------------------------------*
45 * decode LAPD (Q.921) protocol
46 *---------------------------------------------------------------------------*/
48 decode_lapd(char *pbuf, int n, unsigned char *buf, int dir, int raw, int printit)
50 int sap, tei, cmd, p_f;
54 char *lbufp = &locbuf[0];
59 sap = (buf[0] >> 2) & 0x3f;
72 /* SAPI control procedures */
76 sprintf((lbufp+strlen(lbufp)), "Q921: SAP=%d (Call Control), %c, TEI=%d, ", sap, cmd?'C':'R', tei);
78 if((buf[2] & 0x01) == 0)
81 sprintf((lbufp+strlen(lbufp)), "I-Frame: ");
86 sprintf((lbufp+strlen(lbufp)), "N(S) %d N(R) %d P %d ", buf [2] >> 1, buf [3] >> 1, p_f);
90 else if((buf[2] & 0x03) == 0x01)
93 sprintf((lbufp+strlen(lbufp)), "S-Frame: ");
101 sprintf((lbufp+strlen(lbufp)), "RR N(R) %d PF %d ", buf [3] >> 1, p_f);
103 sprintf((lbufp+strlen(lbufp)), "RNR N(R) %d PF %d ", buf [3] >> 1, p_f);
105 sprintf((lbufp+strlen(lbufp)), "REJ N(R) %d PF %d ", buf [3] >> 1, p_f);
109 else if((buf[2] & 0x03) == 0x03)
112 sprintf((lbufp+strlen(lbufp)), "U-Frame: ");
114 p_f = (buf [2] & 0x10) >> 4;
115 cmd = buf [2] & 0xec;
120 sprintf((lbufp+strlen(lbufp)), "SABME PF %d ", p_f);
122 sprintf((lbufp+strlen(lbufp)), "DM PF %d ", p_f);
124 sprintf((lbufp+strlen(lbufp)), "UI PF %d ", p_f);
126 sprintf((lbufp+strlen(lbufp)), "DISC PF %d ", p_f);
128 sprintf((lbufp+strlen(lbufp)), "UA PF %d ", p_f);
130 sprintf((lbufp+strlen(lbufp)), "FRMR PF %d ", p_f);
132 sprintf((lbufp+strlen(lbufp)), "XID PF %d ", p_f);
133 /* information field ??? */
144 sprintf((lbufp+strlen(lbufp)), "Q921: SAP=%d (X.25), %c, TEI=%d, ", sap, cmd?'C':'R', tei);
152 sprintf((lbufp+strlen(lbufp)), "Q921: SAP=%d (Loopbacktest), %c, TEI=%d, ", sap, cmd?'C':'R', tei);
156 /* SAPI layer 2 management functions */
161 sprintf((lbufp+strlen(lbufp)), "Q921: SAP=%d (TEI-Management), %c, TEI=%d, ", sap, cmd?'C':'R', tei);
166 sprintf((lbufp+strlen(lbufp)), "ILLEGAL TEI\n");
171 if (buf [2] != 3 && buf [3] != 0xf)
174 sprintf((lbufp+strlen(lbufp)), "invalid format!\n");
178 cnt+= 2; /* UI + MEI */
181 sprintf((lbufp+strlen(lbufp)), "Ri=0x%04hx, ", *(short *)&buf[4]);
188 sprintf((lbufp+strlen(lbufp)), "IdRequest, Ai=%d", (buf [7] >> 1));
193 sprintf((lbufp+strlen(lbufp)), "IdAssign, Ai=%d", (buf [7] >> 1));
198 sprintf((lbufp+strlen(lbufp)), "IdDenied, Ai=%d", (buf [7] >> 1));
203 sprintf((lbufp+strlen(lbufp)), "IdCheckReq, Ai=%d", (buf [7] >> 1));
208 sprintf((lbufp+strlen(lbufp)), "IdCheckResp, Ai=%d", (buf [7] >> 1));
213 sprintf((lbufp+strlen(lbufp)), "IdRemove, Ai=%d", (buf [7] >> 1));
218 sprintf((lbufp+strlen(lbufp)), "IdVerify, Ai=%d", (buf [7] >> 1));
223 sprintf((lbufp+strlen(lbufp)), "Unknown Msg Type\n");
234 sprintf((lbufp+strlen(lbufp)), "Q921: ERROR, SAP=%d (Illegal SAPI), %c, TEI=%d\n", sap, cmd?'C':'R', tei);
241 sprintf((lbufp+strlen(lbufp)), "\n");
246 for (i = 0; i < cnt; i += 16)
248 sprintf((pbuf+strlen(pbuf)),"Dump:%.3d ", i);
249 for (j = 0; j < 16; j++)
251 sprintf((pbuf+strlen(pbuf)),"%02x ", buf[i + j]);
253 sprintf((pbuf+strlen(pbuf))," ");
254 sprintf((pbuf+strlen(pbuf))," ");
255 for (j = 0; j < 16 && i + j < cnt; j++)
256 if (isprint(buf[i + j]))
257 sprintf((pbuf+strlen(pbuf)),"%c", buf[i + j]);
259 sprintf((pbuf+strlen(pbuf)),".");
260 sprintf((pbuf+strlen(pbuf)),"\n");
264 sprintf((pbuf+strlen(pbuf)),"%s", &locbuf[0]);