Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / i4b / isdndecode / layer2.c
1 /*
2  * Copyright (c) 1997, 1999 Hellmuth Michaelis. 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  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      layer2.c - decode and print layer 2 (Q.921) information
28  *      -------------------------------------------------------
29  *
30  *      $Id: layer2.c,v 1.5 1999/12/13 21:25:25 hm Exp $
31  *
32  * $FreeBSD: src/usr.sbin/i4b/isdndecode/layer2.c,v 1.6.2.1 2001/08/01 17:45:05 obrien Exp $
33  * $DragonFly: src/usr.sbin/i4b/isdndecode/layer2.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
34  *
35  *      last edit-date: [Mon Dec 13 21:50:41 1999]
36  *
37  *---------------------------------------------------------------------------*/
38
39 #include "decode.h"
40                 
41 /*---------------------------------------------------------------------------*
42  *      decode poll bit
43  *---------------------------------------------------------------------------*/
44 static void
45 poll(int layer, char *buffer, int cnt, unsigned char value, unsigned char mask)
46 {
47         sprintline(layer, buffer, cnt, value, mask, "P/F, Poll = %s", (value & mask) ? "Immediate Response Required" : "No Immediate Response Required");
48 }       
49
50 /*---------------------------------------------------------------------------*
51  *      decode final bit
52  *---------------------------------------------------------------------------*/
53 static void
54 final(int layer, char *buffer, int cnt, unsigned char value, unsigned char mask)
55 {
56         sprintline(layer, buffer, cnt, value, mask, "P/F, Final = %s", (value & mask) ? "Result of Poll" : "No Result of Poll");
57 }       
58
59 /*---------------------------------------------------------------------------*
60  *      decode protocol specified in Q.921
61  *---------------------------------------------------------------------------*/
62 int
63 layer2(char *pbuf, unsigned char *buf, int dir, int printit)
64 {
65         int sap, tei, cmd;
66         int cnt = 0;
67         char locbuf[32000];
68         char *lbufp = &locbuf[0];
69         char buffer[80];
70         
71         *lbufp = '\0';
72         *pbuf = '\0';
73         
74         /* address high */
75
76         sap = (buf[0] >> 2) & 0x3f;
77
78         if(sap == 0)
79                 strcpy(buffer, "Call Control");
80         else if((sap >= 1) && (sap <= 15))
81                 strcpy(buffer, "Reserved");
82         else if(sap == 16)
83                 strcpy(buffer, "X.25");
84         else if((sap >= 17) && (sap <= 31))
85                 strcpy(buffer, "Reserved");
86         else if(sap == 63)
87                 strcpy(buffer, "Layer 2 Management");
88         else
89                 strcpy(buffer, "Not available for Q.921");
90         sprintline(2, lbufp+strlen(lbufp), cnt, buf[0], 0xfc, "SAPI = %d (%s)", sap, buffer);
91
92         if(dir == FROM_TE)
93                 cmd = !(buf[0] & 0x02);
94         else
95                 cmd = buf[0] & 0x02;
96
97         sprintline(2, lbufp+strlen(lbufp), cnt, buf[0], 0x02, "C/R = %s", cmd ? "Command" : "Response");
98         extension(2, lbufp+strlen(lbufp), cnt, buf[0], 0x01);
99         cnt++;
100
101         /* address low */
102
103         tei = buf[1] >> 1;
104
105         if((tei >= 0) && (tei <= 63))
106                 strcpy(buffer, "Non-automatic TEI");
107         else if((tei >= 64) && (tei <= 126))
108                 strcpy(buffer, "Automatic TEI");
109         if(tei == 127)
110                 strcpy(buffer, "Group TEI");
111                 
112         sprintline(2, lbufp+strlen(lbufp), cnt, buf[1], 0xfe, "TEI = %d (%s)", tei, buffer);
113         extension(2, lbufp+strlen(lbufp), cnt, buf[1], 0x01);
114         cnt++;
115
116         /* control 1 */
117         
118         if((buf[2] & 0x03) == 0x03)
119         {
120                 /* U-frame */
121
122                 if((buf[2] & 0xef) == 0x6f)
123                 {
124                         /* SABME */
125                         
126                         sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0xef, "U-Frame: SABME (Set Asynchonous Balanced Mode)");
127                         poll(2, lbufp+strlen(lbufp), cnt, buf[2], 0x10);
128                         cnt++;
129                 }
130                 else if((buf[2] & 0xef) == 0x0f)
131                 {
132                         /* DM */
133
134                         sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0xef, "U-Frame: DM (Disconnected Mode)");
135                         final(2, lbufp+strlen(lbufp), cnt, buf[2], 0x10);
136                         cnt++;
137                 }
138                 else if((buf[2] & 0xef) == 0x03)
139                 {
140                         /* UI */
141
142                         sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0xef, "U-Frame: UI (Unnumbered Information)");
143                         poll(2, lbufp+strlen(lbufp), cnt, buf[2], 0x10);
144                         cnt++;
145         
146                         if(sap == 63 && (buf[3] == 0x0f))       /* TEI management */
147                         {
148                                 sprintline(2, lbufp+strlen(lbufp), cnt, buf[3], 0xff, "MEI (Management Entity Identifier)");
149                                 cnt++;
150                                 sprintline(2, lbufp+strlen(lbufp), cnt, buf[4], 0xff, "Ri = 0x%04x (Reference number high)", (buf[4] << 8) | buf[5]);
151                                 cnt++;
152                                 sprintline(2, lbufp+strlen(lbufp), cnt, buf[5], 0xff, "Ri (Reference Number low)");
153                                 cnt++;
154         
155                                 switch(buf[6])
156                                 {
157                                         case 0x01:
158                                                 strcpy(buffer, "Identity Request");
159                                                 break;
160                                         case 0x02:
161                                                 strcpy(buffer, "Identity Assigned");
162                                                 break;
163                                         case 0x03:
164                                                 strcpy(buffer, "Identity denied");
165                                                 break;
166                                         case 0x04:
167                                                 strcpy(buffer, "Identity Check Request");
168                                                 break;
169                                         case 0x05:
170                                                 strcpy(buffer, "Identity Check Response");
171                                                 break;
172                                         case 0x06:
173                                                 strcpy(buffer, "Identity Remove");
174                                                 break;
175                                         case 0x07:
176                                                 strcpy(buffer, "Identity Verify");
177                                                 break;
178                                         default:
179                                                 strcpy(buffer, "undefined");
180                                                 break;
181                                 }
182                                 
183                                 sprintline(2, lbufp+strlen(lbufp), cnt, buf[6], 0xff, "TEI %s (Message Type %d)", buffer, buf[6]);
184                                 cnt++;
185         
186                                 switch(buf[6])
187                                 {
188                                         case 0x01:
189                                                 strcpy(buffer, "Any TEI value acceptable");
190                                                 break;
191                                         case 0x02:
192                                                 strcpy(buffer, "");
193                                                 break;
194                                         case 0x03:
195                                                 strcpy(buffer, "No TEI Value available");
196                                                 break;
197                                         case 0x04:
198                                                 strcpy(buffer, "Check all TEI values");
199                                                 break;
200                                         case 0x05:
201                                                 strcpy(buffer, "");
202                                                 break;
203                                         case 0x06:
204                                                 strcpy(buffer, "Request for removal of all TEI values");
205                                                 break;
206                                         case 0x07:
207                                                 strcpy(buffer, "");
208                                                 break;
209                                         default:
210                                                 strcpy(buffer, "");
211                                                 break;
212                                 }
213                                 if(((buf[7] >> 1) & 0x7f) == 127)
214                                         sprintline(2, lbufp+strlen(lbufp), cnt, buf[7], 0xfe, "Ai = %d (Action Indicator = %s)", (buf[7] >> 1) & 0x7f, buffer);
215                                 else
216                                         sprintline(2, lbufp+strlen(lbufp), cnt, buf[7], 0xfe, "Ai = %d (Action Indicator)", (buf[7] >> 1) & 0x7f);
217                                 extension(2, lbufp+strlen(lbufp), cnt, buf[7], 0x01);
218                                 cnt++;
219                         }
220                 }
221                 else if((buf[2] & 0xef) == 0x43)
222                 {
223                         /* DISC */
224
225                         sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0xef, "U-Frame: DISC (Disconnect)");
226                         poll(2, lbufp+strlen(lbufp), cnt, buf[2], 0x10);
227                         cnt++;
228                 }
229                 else if((buf[2] & 0xef) == 0x63)
230                 {
231                         /* UA */
232
233                         sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0xef, "U-Frame: UA (Unnumbered Acknowledge)");
234                         final(2, lbufp+strlen(lbufp), cnt, buf[2], 0x10);
235                         cnt++;
236                 }
237                 else if((buf[2] & 0xef) == 0x87)
238                 {
239                         /* FRMR */
240
241                         sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0xef, "U-Frame: FRMR (Frame Reject)");
242                         final(2, lbufp+strlen(lbufp), cnt, buf[2], 0x10);
243                         cnt++;
244                 }
245                 else if((buf[2] & 0xef) == 0x9f)
246                 {
247                         /* XID */
248
249                         sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0xef, "U-Frame: XID (Exchange Identification)");
250                         if(cmd)                 
251                                 poll(2, lbufp+strlen(lbufp), cnt, buf[2], 0x10);
252                         else
253                                 final(2, lbufp+strlen(lbufp), cnt, buf[2], 0x10);
254                         cnt++;
255                 }
256                 
257         }
258         else if((buf[2] & 0x03) == 0x01)
259         {
260                 /* S-frame */
261
262                 if(buf[2] == 0x01)
263                         strcpy(buffer, "RR (Receiver Ready)");
264                 else if(buf[2] == 0x05)
265                         strcpy(buffer, "RNR (Receiver Not Ready)");
266                 else if(buf[2] == 0x09)
267                         strcpy(buffer, "REJ (Reject)");
268                 else
269                         strcpy(buffer, "Unknown");
270                         
271                 sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0xff, "S-Frame: %s", buffer);
272                 cnt++;
273                 
274                 sprintline(2, lbufp+strlen(lbufp), cnt, buf[3], 0xfe, "N(R) = %d (receive sequence number)", (buf[3] >> 1) & 0x7f);
275                 if(cmd)         
276                         poll(2, lbufp+strlen(lbufp), cnt, buf[3], 0x01);
277                 else
278                         final(2, lbufp+strlen(lbufp), cnt, buf[3], 0x01);
279                 cnt++;
280                 
281         }
282         else if((buf[2] & 0x01) == 0x00)
283         {
284                 /* I-frame */
285
286                 sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0xfe, "N(S) = %d (send sequence number)", (buf[2] >> 1) & 0x7f);
287                 sprintline(2, lbufp+strlen(lbufp), cnt, buf[2], 0x01, "I-Frame: Information transfer");
288                 cnt++;
289                 
290                 sprintf(buffer, "N(R) = %d", (buf[3] >> 1) & 0x7f);
291                 sprintline(2, lbufp+strlen(lbufp), cnt, buf[3], 0xfe, "N(R) = %d (receive sequence number)", (buf[3] >> 1) & 0x7f);
292                 poll(2, lbufp+strlen(lbufp), cnt, buf[3], 0x01);
293                 cnt++;
294
295         }
296
297         sprintf((pbuf+strlen(pbuf)),"%s", &locbuf[0]);
298         return (cnt);
299 }
300
301 /* EOF */