Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / i4b / isdntrace / q921.c
1 /*
2  * Copyright (c) 1996 Gary Jennejohn.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  *---------------------------------------------------------------------------*
29  *
30  *      q.921.c - print Q.921 traces
31  *      ----------------------------
32  *
33  *      $Id: q921.c,v 1.4 1999/12/13 21:25:26 hm Exp $ 
34  *
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 $
37  *
38  *      last edit-date: [Mon Dec 13 21:56:46 1999]
39  *
40  *---------------------------------------------------------------------------*/
41
42 #include "trace.h"
43
44 /*---------------------------------------------------------------------------*
45  *      decode LAPD (Q.921) protocol
46  *---------------------------------------------------------------------------*/
47 int
48 decode_lapd(char *pbuf, int n, unsigned char *buf, int dir, int raw, int printit)
49 {
50         int sap, tei, cmd, p_f;
51         int cnt = 0;
52         int i;
53         char locbuf[32000];
54         char *lbufp = &locbuf[0];
55
56         *lbufp = '\0';
57         *pbuf = '\0';
58         
59         sap = (buf[0] >> 2) & 0x3f;
60         cnt++;
61
62         tei = buf[1] >> 1;
63         cnt++;
64
65         if(dir == FROM_TE)
66                 cmd = !(buf[0] & 2);
67         else
68                 cmd = buf[0] & 2;
69
70         switch (sap)
71         {
72                 /* SAPI control procedures */
73                 case 0:
74                 {
75                         if(printit)
76                                 sprintf((lbufp+strlen(lbufp)), "Q921: SAP=%d (Call Control), %c, TEI=%d, ", sap, cmd?'C':'R', tei);
77
78                         if((buf[2] & 0x01) == 0)
79                         {
80                                 if(printit)
81                                         sprintf((lbufp+strlen(lbufp)), "I-Frame: ");
82
83                                 p_f = buf [3] & 1;
84
85                                 if(printit)
86                                         sprintf((lbufp+strlen(lbufp)), "N(S) %d N(R) %d P %d ", buf [2] >> 1, buf [3] >> 1, p_f);
87
88                                 cnt += 2;
89                         }
90                         else if((buf[2] & 0x03) == 0x01)
91                         {
92                                 if(printit)
93                                         sprintf((lbufp+strlen(lbufp)), "S-Frame: ");
94
95                                 p_f = buf [3] & 1;
96                                 cmd = buf [2] & 0x0c;
97
98                                 if(printit)
99                                 {
100                                         if (cmd == 0)
101                                                 sprintf((lbufp+strlen(lbufp)), "RR N(R) %d PF %d ", buf [3] >> 1, p_f);
102                                         if (cmd == 4)
103                                                 sprintf((lbufp+strlen(lbufp)), "RNR N(R) %d PF %d ", buf [3] >> 1, p_f);
104                                         if (cmd == 8)
105                                                 sprintf((lbufp+strlen(lbufp)), "REJ N(R) %d PF %d ", buf [3] >> 1, p_f);
106                                 }
107                                 cnt += 2;
108                         }
109                         else if((buf[2] & 0x03) == 0x03)
110                         {
111                                 if(printit)
112                                         sprintf((lbufp+strlen(lbufp)), "U-Frame: ");
113
114                                 p_f = (buf [2] & 0x10) >> 4;
115                                 cmd = buf [2] & 0xec;
116
117                                 if(printit)
118                                 {
119                                         if (cmd == 0x6c)
120                                                 sprintf((lbufp+strlen(lbufp)), "SABME PF %d ", p_f);
121                                         if (cmd == 0x0c)
122                                                 sprintf((lbufp+strlen(lbufp)), "DM PF %d ", p_f);
123                                         if (cmd == 0)
124                                                 sprintf((lbufp+strlen(lbufp)), "UI PF %d ", p_f);
125                                         if (cmd == 0x40)
126                                                 sprintf((lbufp+strlen(lbufp)), "DISC PF %d ", p_f);
127                                         if (cmd == 0x60)
128                                                 sprintf((lbufp+strlen(lbufp)), "UA PF %d ", p_f);
129                                         if (cmd == 0x84)
130                                                 sprintf((lbufp+strlen(lbufp)), "FRMR PF %d ", p_f);
131                                         if (cmd == 0xac)
132                                                 sprintf((lbufp+strlen(lbufp)), "XID PF %d ", p_f);
133                                         /* information field ??? */
134                                 }
135                                 cnt++;
136                         }
137                         break;
138                 }
139
140                 /* D channel X.25 */
141                 
142                 case 16:
143                         if(printit)
144                                 sprintf((lbufp+strlen(lbufp)), "Q921: SAP=%d (X.25), %c, TEI=%d, ", sap, cmd?'C':'R', tei);
145                         cnt = n;
146                         goto dump;                              
147
148                 /* Loopback test */
149                 
150                 case 32:
151                         if(printit)
152                                 sprintf((lbufp+strlen(lbufp)), "Q921: SAP=%d (Loopbacktest), %c, TEI=%d, ", sap, cmd?'C':'R', tei);
153                         cnt = n;
154                         goto dump;                              
155
156                 /* SAPI layer 2 management functions */
157
158                 case 63:
159                 {
160                         if(printit)
161                                 sprintf((lbufp+strlen(lbufp)), "Q921: SAP=%d (TEI-Management), %c, TEI=%d, ", sap, cmd?'C':'R', tei);
162
163                         if (tei != 127)
164                         {
165                                 if(printit)
166                                         sprintf((lbufp+strlen(lbufp)), "ILLEGAL TEI\n");
167                                 cnt = n;
168                                 goto dump;                              
169                         }
170
171                         if (buf [2] != 3 && buf [3] != 0xf)
172                         {
173                                 if(printit)
174                                         sprintf((lbufp+strlen(lbufp)), "invalid format!\n");
175                                 cnt = n;
176                                 goto dump;                              
177                         }
178                         cnt+= 2; /* UI + MEI */
179
180                         if(printit)
181                                 sprintf((lbufp+strlen(lbufp)), "Ri=0x%04hx, ", *(short *)&buf[4]);
182                         cnt += 2; /* Ri */
183                         
184                         switch (buf[6])
185                         {
186                                 case 1: 
187                                         if(printit)
188                                                 sprintf((lbufp+strlen(lbufp)), "IdRequest, Ai=%d", (buf [7] >> 1));
189                                         cnt += 2;
190                                         break;
191                                 case 2:
192                                         if(printit)
193                                                 sprintf((lbufp+strlen(lbufp)), "IdAssign, Ai=%d", (buf [7] >> 1));
194                                         cnt += 2;
195                                         break;
196                                 case 3:
197                                         if(printit)
198                                                 sprintf((lbufp+strlen(lbufp)), "IdDenied, Ai=%d", (buf [7] >> 1));
199                                         cnt += 2;
200                                         break;
201                                 case 4:
202                                         if(printit)
203                                                 sprintf((lbufp+strlen(lbufp)), "IdCheckReq, Ai=%d", (buf [7] >> 1));
204                                         cnt += 2;
205                                         break;
206                                 case 5:
207                                         if(printit)
208                                                 sprintf((lbufp+strlen(lbufp)), "IdCheckResp, Ai=%d", (buf [7] >> 1));
209                                         cnt += 2;
210                                         break;
211                                 case 6:
212                                         if(printit)
213                                                 sprintf((lbufp+strlen(lbufp)), "IdRemove, Ai=%d", (buf [7] >> 1));
214                                         cnt += 2;
215                                         break;
216                                 case 7:
217                                         if(printit)
218                                                 sprintf((lbufp+strlen(lbufp)), "IdVerify, Ai=%d", (buf [7] >> 1));
219                                         cnt += 2;
220                                         break;
221                                 default:
222                                         if(printit)
223                                                 sprintf((lbufp+strlen(lbufp)), "Unknown Msg Type\n");
224                                         cnt = n;
225                                         goto dump;                              
226                         }
227                         break;
228                 }
229
230                 /* Illegal SAPI */
231                 
232                 default:
233                         if(printit)
234                                 sprintf((lbufp+strlen(lbufp)), "Q921: ERROR, SAP=%d (Illegal SAPI), %c, TEI=%d\n", sap, cmd?'C':'R', tei);
235                         cnt = n;
236                         goto dump;                              
237         }
238
239 dump:   
240         if(printit)
241                 sprintf((lbufp+strlen(lbufp)), "\n");
242
243         if(raw && printit)
244         {
245                 int j;
246                 for (i = 0; i < cnt; i += 16)
247                 {
248                         sprintf((pbuf+strlen(pbuf)),"Dump:%.3d  ", i);
249                         for (j = 0; j < 16; j++)
250                                 if (i + j < cnt)
251                                         sprintf((pbuf+strlen(pbuf)),"%02x ", buf[i + j]);
252                                 else
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]);
258                                 else
259                                         sprintf((pbuf+strlen(pbuf)),".");
260                         sprintf((pbuf+strlen(pbuf)),"\n");
261                 }
262         }
263
264         sprintf((pbuf+strlen(pbuf)),"%s", &locbuf[0]);
265         
266         return (cnt);
267 }
268
269 /* EOF */