2 * ===================================
3 * HARP | Host ATM Research Platform
4 * ===================================
7 * This Host ATM Research Platform ("HARP") file (the "Software") is
8 * made available by Network Computing Services, Inc. ("NetworkCS")
9 * "AS IS". NetworkCS does not provide maintenance, improvements or
10 * support of any kind.
12 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
13 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
14 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
15 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
16 * In no event shall NetworkCS be responsible for any damages, including
17 * but not limited to consequential damages, arising from or relating to
18 * any use of the Software or related support.
20 * Copyright 1994-1998 Network Computing Services, Inc.
22 * Copies of this Software may be made, however, the above copyright
23 * notice must be reproduced on all copies.
27 * SPANS Signalling Manager
28 * ---------------------------
30 * Kernel XDR (External Data Representation) routines
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
37 #include <netatm/port.h>
40 * This file contains code that has been copied and/or modified from
41 * the following FreeBSD files:
43 * /usr/src/lib/libc/xdr/xdr.c
44 * /usr/src/lib/libc/xdr/xdr_mem.c
46 * which are covered by the copyright notice below.
50 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
51 * unrestricted use provided that this legend is included on all tape
52 * media and as a part of the software program in whole or part. Users
53 * may copy or modify Sun RPC without charge, but are not authorized
54 * to license or distribute it to anyone else except as part of a product or
55 * program developed by the user.
57 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
58 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
61 * Sun RPC is provided with no support and without any obligation on the
62 * part of Sun Microsystems, Inc. to assist in its use, correction,
63 * modification or enhancement.
65 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
66 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
67 * OR ANY PART THEREOF.
69 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
70 * or profits or other special, indirect and consequential damages, even if
71 * Sun has been advised of the possibility of such damages.
73 * Sun Microsystems, Inc.
75 * Mountain View, California 94043
78 #if defined(LIBC_SCCS) && !defined(lint)
79 /*static char *sccsid = "from: @(#)xdr.c 1.35 87/08/12";*/
80 /*static char *sccsid = "from: @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";*/
82 #include <sys/cdefs.h>
83 __FBSDID("$FreeBSD$");
86 * xdr.c, Generic XDR routines implementation.
88 * Copyright (C) 1986, Sun Microsystems, Inc.
90 * These are the "generic" xdr routines used to serialize and de-serialize
91 * most common data items. See xdr.h for more info on the interface to
95 #include <netatm/spans/spans_kxdr.h>
98 * constants specific to the xdr "protocol"
100 #define XDR_FALSE ((long) 0)
101 #define XDR_TRUE ((long) 1)
102 #define LASTUNSIGNED ((u_int) 0-1)
107 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
119 (void) (xdr_short(xdrs, (short *)ip));
120 return (xdr_long(xdrs, (long *)ip));
122 if (sizeof (int) == sizeof (long)) {
123 return (xdr_long(xdrs, (long *)ip));
125 return (xdr_short(xdrs, (short *)ip));
131 * XDR unsigned integers
140 (void) (xdr_short(xdrs, (short *)up));
141 return (xdr_u_long(xdrs, (u_long *)up));
143 if (sizeof (u_int) == sizeof (u_long)) {
144 return (xdr_u_long(xdrs, (u_long *)up));
146 return (xdr_short(xdrs, (short *)up));
153 * same as xdr_u_long - open coded to save a proc call!
161 if (xdrs->x_op == XDR_ENCODE)
162 return (XDR_PUTLONG(xdrs, lp));
164 if (xdrs->x_op == XDR_DECODE)
165 return (XDR_GETLONG(xdrs, lp));
167 if (xdrs->x_op == XDR_FREE)
174 * XDR unsigned long integers
175 * same as xdr_long - open coded to save a proc call!
178 xdr_u_long(xdrs, ulp)
183 if (xdrs->x_op == XDR_DECODE)
184 return (XDR_GETLONG(xdrs, (long *)ulp));
185 if (xdrs->x_op == XDR_ENCODE)
186 return (XDR_PUTLONG(xdrs, (long *)ulp));
187 if (xdrs->x_op == XDR_FREE)
202 switch (xdrs->x_op) {
206 return (XDR_PUTLONG(xdrs, &l));
209 if (!XDR_GETLONG(xdrs, &l)) {
222 * XDR unsigned short integers
225 xdr_u_short(xdrs, usp)
231 switch (xdrs->x_op) {
235 return (XDR_PUTLONG(xdrs, &l));
238 if (!XDR_GETLONG(xdrs, &l)) {
262 if (!xdr_int(xdrs, &i)) {
270 * XDR an unsigned char
280 if (!xdr_u_int(xdrs, &u)) {
297 switch (xdrs->x_op) {
300 lb = *bp ? XDR_TRUE : XDR_FALSE;
301 return (XDR_PUTLONG(xdrs, &lb));
304 if (!XDR_GETLONG(xdrs, &lb)) {
307 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
325 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
328 * enums are treated as ints
330 if (sizeof (enum sizecheck) == sizeof (long)) {
331 return (xdr_long(xdrs, (long *)ep));
332 } else if (sizeof (enum sizecheck) == sizeof (short)) {
333 return (xdr_short(xdrs, (short *)ep));
338 (void) (xdr_short(xdrs, (short *)ep));
339 return (xdr_long(xdrs, (long *)ep));
345 * Allows the specification of a fixed size sequence of opaque bytes.
346 * cp points to the opaque object and cnt gives the byte length.
349 xdr_opaque(xdrs, cp, cnt)
354 register u_int rndup;
355 static char crud[BYTES_PER_XDR_UNIT];
358 * if no data we are done
364 * round byte count to full xdr units
366 rndup = cnt % BYTES_PER_XDR_UNIT;
368 rndup = BYTES_PER_XDR_UNIT - rndup;
370 if (xdrs->x_op == XDR_DECODE) {
371 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
376 return (XDR_GETBYTES(xdrs, crud, rndup));
379 if (xdrs->x_op == XDR_ENCODE) {
380 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
385 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
388 if (xdrs->x_op == XDR_FREE) {
397 * XDR implementation using kernel buffers
400 #if defined(LIBC_SCCS) && !defined(lint)
401 /*static char *sccsid = "from: @(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";*/
402 /*static char *sccsid = "from: @(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";*/
406 * xdr_mem.h, XDR implementation using memory buffers.
408 * Copyright (C) 1984, Sun Microsystems, Inc.
410 * If you have some data to be interpreted as external data representation
411 * or to be converted to external data representation in a memory buffer,
412 * then this is the package for you.
417 void xdrmbuf_init(XDR *, KBuffer *, enum xdr_op);
418 static bool_t xdrmbuf_getlong(XDR *, long *);
419 static bool_t xdrmbuf_putlong(XDR *, const long *);
420 static bool_t xdrmbuf_getbytes(XDR *, char *, u_int);
421 static bool_t xdrmbuf_putbytes(XDR *, const char *, u_int);
422 static u_int xdrmbuf_getpos(XDR *);
424 static struct xdr_ops xdrmbuf_ops = {
436 * The procedure xdrmbuf_init initializes a stream descriptor for a
440 xdrmbuf_init(xdrs, m, op)
447 xdrs->x_ops = &xdrmbuf_ops;
448 xdrs->x_base = (caddr_t)m;
449 KB_DATASTART(m, xdrs->x_private, caddr_t);
450 xdrs->x_handy = KB_LEN(m);
454 xdrmbuf_getlong(xdrs, lp)
460 * See if long is contained in this buffer
462 if ((xdrs->x_handy -= sizeof(long)) < 0) {
466 * We (currently) don't allow a long to span a buffer
468 if (xdrs->x_handy != -sizeof(long)) {
469 printf("xdrmbuf_getlong: data spans buffers\n");
474 * Try to move to a chained buffer
476 if ((m = (KBuffer *)(xdrs->x_base)) != NULL) {
478 xdrs->x_base = (caddr_t)m;
482 * Setup new buffer's info
484 KB_DATASTART(m, xdrs->x_private, caddr_t);
485 if ((xdrs->x_handy = KB_LEN(m) - sizeof(long)) < 0) {
486 printf("xdrmbuf_getlong: short buffer\n");
498 * Return the long value
500 *lp = (long)ntohl((u_long)(*((long *)(xdrs->x_private))));
503 * Advance the data stream
505 xdrs->x_private = (long *)xdrs->x_private + 1;
510 xdrmbuf_putlong(xdrs, lp)
516 * See if long will fit in this buffer
518 if ((xdrs->x_handy -= sizeof(long)) < 0) {
522 * We (currently) don't allow a long to span a buffer
524 if (xdrs->x_handy != -sizeof(long)) {
525 printf("xdrmbuf_putlong: data spans buffers\n");
530 * Try to move to a chained buffer
532 if ((m = (KBuffer *)(xdrs->x_base)) != NULL) {
534 xdrs->x_base = (caddr_t)m;
538 * Setup new buffer's info
540 KB_DATASTART(m, xdrs->x_private, caddr_t);
541 if ((xdrs->x_handy = KB_LEN(m) - sizeof(long)) < 0) {
542 printf("xdrmbuf_putlong: short buffer\n");
554 * Store the long value into our buffer
556 *(long *)xdrs->x_private = (long)htonl((u_long)(*lp));
559 * Advance the data stream
561 xdrs->x_private = (long *)xdrs->x_private + 1;
566 xdrmbuf_getbytes(xdrs, addr, len)
575 if (xdrs->x_handy <= 0) {
579 * No data in current buffer, move to a chained buffer
581 if ((m = (KBuffer *)(xdrs->x_base)) != NULL) {
583 xdrs->x_base = (caddr_t)m;
587 * Setup new buffer's info
589 KB_DATASTART(m, xdrs->x_private, caddr_t);
590 xdrs->x_handy = KB_LEN(m);
600 * Copy from buffer to user's space
602 copy = MIN(len, xdrs->x_handy);
603 bcopy(xdrs->x_private, addr, copy);
606 * Update data stream controls
608 xdrs->x_private = (char *)xdrs->x_private + copy;
609 xdrs->x_handy -= copy;
617 xdrmbuf_putbytes(xdrs, addr, len)
626 if (xdrs->x_handy <= 0) {
630 * No data in current buffer, move to a chained buffer
632 if ((m = (KBuffer *)(xdrs->x_base)) != NULL) {
634 xdrs->x_base = (caddr_t)m;
638 * Setup new buffer's info
640 KB_DATASTART(m, xdrs->x_private, caddr_t);
641 xdrs->x_handy = KB_LEN(m);
651 * Copy from user's space into buffer
653 copy = MIN(len, xdrs->x_handy);
654 bcopy(addr, xdrs->x_private, copy);
657 * Update data stream controls
659 xdrs->x_private = (char *)xdrs->x_private + 1; /*XXXKAN:copy? */
660 xdrs->x_handy -= copy;
672 return ((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base);