* Remove (void) casts for discarded return values.
[dragonfly.git] / sys / netproto / atm / spans / spans_util.c
1 /*
2  *
3  * ===================================
4  * HARP  |  Host ATM Research Platform
5  * ===================================
6  *
7  *
8  * This Host ATM Research Platform ("HARP") file (the "Software") is
9  * made available by Network Computing Services, Inc. ("NetworkCS")
10  * "AS IS".  NetworkCS does not provide maintenance, improvements or
11  * support of any kind.
12  *
13  * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14  * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15  * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16  * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17  * In no event shall NetworkCS be responsible for any damages, including
18  * but not limited to consequential damages, arising from or relating to
19  * any use of the Software or related support.
20  *
21  * Copyright 1994-1998 Network Computing Services, Inc.
22  *
23  * Copies of this Software may be made, however, the above copyright
24  * notice must be reproduced on all copies.
25  *
26  *      @(#) $FreeBSD: src/sys/netatm/spans/spans_util.c,v 1.5 1999/08/29 10:28:10 bde Exp $
27  *      @(#) $DragonFly: src/sys/netproto/atm/spans/spans_util.c,v 1.5 2006/01/14 13:36:39 swildner Exp $
28  */
29
30 /*
31  * SPANS Signalling Manager
32  * ---------------------------
33  *
34  * SPANS-related utility routines.
35  *
36  */
37
38 #include <netproto/atm/kern_include.h>
39
40 #include "spans_xdr.h"
41 #include "spans_var.h"
42
43 #ifdef NOTDEF
44 /* XXX -- Remove all SAP checks? */
45 #define MAX_SAP_ENT     1
46 static struct {
47         spans_sap       spans_sap;
48         Sap_t   local_sap;
49 } sap_table[MAX_SAP_ENT] = {
50         {SPANS_SAP_IP, SAP_IP},
51 };
52
53
54 /*
55  * Translate an internal SAP to a SPANS SAP
56  *
57  * Search the SAP table for the given SAP.  Put the corresponding SPANS
58  * SAP into the indicated variable.
59  *
60  * Arguments:
61  *      lsap    the value of the internal SAP
62  *      ssap    a pointer to the variable to receive the SPANS SAP value
63  *
64  * Returns:
65  *      TRUE    the SAP was found; *ssap is valid
66  *      FALSE   the SAP was not found; *ssap is not valid
67  *
68  */
69 int
70 spans_get_spans_sap(Sap_t lsap, spans_sap *ssap)
71 {
72         int i;
73
74         /*
75          * Search the SAP table for the given local SAP
76          */
77         for (i=0; i< MAX_SAP_ENT; i++) {
78                 if (sap_table[i].local_sap == lsap) {
79                         *ssap = sap_table[i].spans_sap;
80                         return(TRUE);
81                 }
82         }
83         return(FALSE);
84 }
85
86
87 /*
88  * Translate a SPANS SAP to internal format
89  *
90  * Search the SAP table for the given SAP.  Put the corresponding
91  * internal SAP into the indicated variable.
92  *
93  * Arguments:
94  *      ssap    the value of the SPANS SAP
95  *      lsap    a pointer to the variable to receive the internal
96  *              SAP value
97  *
98  * Returns:
99  *      TRUE    the SAP was found; *lsap is valid
100  *      FALSE   the SAP was not found; *lsap is not valid
101  *
102  */
103 int
104 spans_get_local_sap(spans_sap ssap, Sap_t *lsap)
105 {
106         int i;
107
108         /*
109          * Search the SAP table for the given SPANS SAP
110          */
111         for (i=0; i< MAX_SAP_ENT; i++) {
112                 if (sap_table[i].spans_sap == ssap) {
113                         *lsap = sap_table[i].local_sap;
114                         return(TRUE);
115                 }
116         }
117         return(FALSE);
118 }
119 #endif
120
121
122 /*
123  * Allocate an ephemeral SPANS SAP
124  *
125  * Arguments:
126  *      spp     pointer to SPANS protocol instance
127  *
128  * Returns:
129  *      a SPANS ephemeral SAP number
130  *
131  */
132 int
133 spans_ephemeral_sap(struct spans *spp)
134 {
135         return(SPANS_SAP_EPHEMERAL);
136 }
137
138
139 /*
140  * Translate an internal AAL designator to a SPANS AAL type
141  *
142  * Arguments:
143  *      laal    internal AAL designation
144  *      saal    a pointer to the variable to receive the SPANS AAL type
145  *
146  * Returns:
147  *      TRUE    the AAL was found; *saal is valid
148  *      FALSE   the AAL was not found; *saal is not valid
149  *
150  */
151 int
152 spans_get_spans_aal(Aal_t laal, spans_aal *saal)
153 {
154         /*
155          *
156          */
157         switch (laal) {
158         case ATM_AAL0:
159                 *saal = SPANS_AAL0;
160                 return(TRUE);
161         case ATM_AAL1:
162                 *saal = SPANS_AAL1;
163                 return(TRUE);
164         case ATM_AAL2:
165                 *saal = SPANS_AAL2;
166                 return(TRUE);
167         case ATM_AAL3_4:
168                 *saal = SPANS_AAL4;
169                 return(TRUE);
170         case ATM_AAL5:
171                 *saal = SPANS_AAL5;
172                 return(TRUE);
173         default:
174                 return(FALSE);
175         }
176 }
177
178
179 /*
180  * Translate a SPANS AAL type to an internal AAL designator
181  *
182  * Arguments:
183  *      saal    the SPANS AAL type
184  *      laal    a pointer to the variable to receive the internal
185  *              AAL designation
186  *
187  * Returns:
188  *      TRUE    the AAL was found; *laal is valid
189  *      FALSE   the AAL was not found; *laal is not valid
190  *
191  */
192 int
193 spans_get_local_aal(spans_aal saal, Aal_t *laal)
194 {
195         /*
196          *
197          */
198         switch (saal) {
199         case SPANS_AAL0:
200                 *laal = ATM_AAL0;
201                 return(TRUE);
202         case SPANS_AAL1:
203                 *laal = ATM_AAL1;
204                 return(TRUE);
205         case SPANS_AAL2:
206                 *laal = ATM_AAL2;
207                 return(TRUE);
208         case SPANS_AAL3:
209         case SPANS_AAL4:
210                 *laal = ATM_AAL3_4;
211                 return(TRUE);
212         case SPANS_AAL5:
213                 *laal = ATM_AAL5;
214                 return(TRUE);
215         default:
216                 return(FALSE);
217         }
218 }
219
220
221 /*
222  * Verify a VCCB
223  *
224  * Search SPANS's VCCB queue to verify that a VCCB belongs to SPANS.
225  *
226  * Arguments:
227  *      spp     pointer to SPANS protocol instance
228  *      svp     pointer to a VCCB
229  *
230  * Returns:
231  *      TRUE    the VCCB belongs to SPANS
232  *      FALSE   the VCCB doesn't belong to SPANS
233  *
234  */
235 int
236 spans_verify_vccb(struct spans *spp, struct spans_vccb *svp)
237 {
238         struct spans_vccb       *vcp, *vcnext;
239
240         for (vcp = Q_HEAD(spp->sp_vccq, struct spans_vccb);
241                         vcp; vcp = vcnext){
242                 vcnext = Q_NEXT(vcp, struct spans_vccb, sv_sigelem);
243                 if (svp == vcp) {
244                         return(TRUE);
245                 }
246         }
247         return(FALSE);
248 }
249
250
251 /*
252  * Find a VCCB
253  *
254  * Find a VCCB given the VPI and VCI.
255  *
256  * Arguments:
257  *      spp     pointer to SPANS protocol instance
258  *      vpi     the VPI to search for
259  *      vci     the VCI to search for
260  *      dir     the direction of the VCC (VCC_IN, VCC_OUT, or both).
261  *              If dir is set to zero, return the address of any VCCB
262  *              with the given VPI/VCI, regardless of direction.
263  *
264  * Returns:
265  *      0       there is no such VCCB
266  *      address the address of the VCCB
267  *
268  */
269 struct spans_vccb *
270 spans_find_vpvc(struct spans *spp, int vpi, int vci, u_char dir)
271 {
272         struct spans_vccb       *svp, *svnext;
273
274         for (svp = Q_HEAD(spp->sp_vccq, struct spans_vccb); svp;
275                         svp = svnext){
276                 svnext = Q_NEXT(svp, struct spans_vccb, sv_sigelem);
277                 if (svp->sv_vpi == vpi &&
278                                 svp->sv_vci == vci &&
279                                 (svp->sv_type & dir) == dir)
280                         break;
281         }
282         return(svp);
283 }
284
285
286 /*
287  * Find a connection
288  *
289  * Find a VCCB given the connection structure.
290  *
291  * Arguments:
292  *      spp     pointer to SPANS protocol instance
293  *      p       pointer to an spans_atm_conn structure
294  *
295  * Returns:
296  *      0       there is no such VCCB
297  *      address the address of the VCCB
298  *
299  */
300 struct spans_vccb *
301 spans_find_conn(struct spans *spp, struct spans_atm_conn *p)
302 {
303         struct spans_vccb       *svp, *svnext;
304
305         for (svp = Q_HEAD(spp->sp_vccq, struct spans_vccb); svp; svp = svnext){
306                 svnext = Q_NEXT(svp, struct spans_vccb, sv_sigelem);
307                 if (!bcmp(p, &svp->sv_conn, sizeof (spans_atm_conn)))
308                         break;
309         }
310         return(svp);
311 }
312
313
314 /*
315  * Allocate a VPI/VCI pair
316  *
317  * When we get an open request or indication from the network, we have
318  * allocate a VPI and VCI for the conection.  This routine will allocate
319  * a VPI/VCI based on the next available VCI in the SPANS protocol block.
320  * The VPI/VCI chose must be within the range allowed by the interface and
321  * must not already be in use.
322  *
323  * Currently the Fore ATM interface only supports VPI 0, so this code only
324  * allocates a VCI.
325  *
326  * There's probably a more elegant way to do this.
327  *
328  * Arguments:
329  *      spp     pointer to connection's SPANS protocol instance
330  *
331  * Returns:
332  *      0       no VPI/VCI available
333  *      vpvc    the VPI/VCI for the connection
334  *
335  */
336 spans_vpvc
337 spans_alloc_vpvc(struct spans *spp)
338 {
339         int     vpi, vci;
340
341         /*
342          * Loop through the allowable VCIs, starting with the curent one,
343          * to find one that's not in use.
344          */
345         while (spp->sp_alloc_vci <= spp->sp_max_vci) {
346                 vpi = spp->sp_alloc_vpi;
347                 vci = spp->sp_alloc_vci++;
348                 if (!spans_find_vpvc(spp, vpi, vci, 0)) {
349                         return(SPANS_PACK_VPIVCI(vpi, vci));
350                 }
351         }
352
353         /*
354          * Reset the VCI to the minimum
355          */
356         spp->sp_alloc_vci = spp->sp_min_vci;
357
358         /*
359          * Try looping through again
360          */
361         while (spp->sp_alloc_vci <= spp->sp_max_vci) {
362                 vpi = spp->sp_alloc_vpi;
363                 vci = spp->sp_alloc_vci++;
364                 if (!spans_find_vpvc(spp, vpi, vci, 0)) {
365                         return(SPANS_PACK_VPIVCI(vpi, vci));
366                 }
367         }
368
369         /*
370          * All allowable VCIs are in use
371          */
372         return(0);
373 }
374
375
376 /*
377  * Print a SPANS address
378  *
379  * Convert a SPANS address into an ASCII string suitable for printing.
380  *
381  * Arguments:
382  *      p       pointer to a struct spans_addr
383  *
384  * Returns:
385  *      the address of a string with the ASCII representation of the
386  *      address.
387  *
388  */
389 char *
390 spans_addr_print(struct spans_addr *p)
391 {
392         static char     strbuff[80];
393         union {
394                 int     w;
395                 char    c[4];
396         } u1, u2;
397
398
399         /*
400          * Clear the returned string
401          */
402         KM_ZERO(strbuff, sizeof(strbuff));
403
404         /*
405          * Get address into integers
406          */
407         u1.c[0] =p->addr[0];
408         u1.c[1] =p->addr[1];
409         u1.c[2] =p->addr[2];
410         u1.c[3] =p->addr[3];
411         u2.c[0] =p->addr[4];
412         u2.c[1] =p->addr[5];
413         u2.c[2] =p->addr[6];
414         u2.c[3] =p->addr[7];
415
416         /*
417          * Print and return the string
418          */
419         sprintf(strbuff, "%lx.%lx", (u_long)ntohl(u1.w), (u_long)ntohl(u2.w));
420         return(strbuff);
421 }
422
423
424 /*
425  * Print a buffer chain
426  *
427  * Arguments:
428  *      m       pointer to a buffer chain
429  *
430  * Returns:
431  *      none
432  *
433  */
434 void
435 spans_dump_buffer(KBuffer *m)
436 {
437         int             i;
438         caddr_t         cp;
439
440         printf("spans_dump_buffer:\n");
441         while (m) {
442                 KB_DATASTART(m, cp, caddr_t);
443                 for (i = 0; i < KB_LEN(m); i++) {
444                         if (i == 0)
445                                 printf("   bfr=%p: ", m);
446                         printf("%x ", (u_char)*cp++);
447                 }
448                 printf("<end_bfr>\n");
449                 m = KB_NEXT(m);
450         }
451 }