3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
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.
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.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
26 * @(#) $FreeBSD: src/sys/netatm/spans/spans_kxdr.c,v 1.3 1999/08/28 00:48:50 peter Exp $
27 * @(#) $DragonFly: src/sys/netproto/atm/spans/spans_kxdr.c,v 1.9 2008/10/28 17:23:46 swildner Exp $
31 * SPANS Signalling Manager
32 * ---------------------------
34 * Kernel XDR (External Data Representation) routines
38 #include <netproto/atm/kern_include.h>
41 * This file contains code that has been copied and/or modified from
42 * the following FreeBSD files:
44 * /usr/src/lib/libc/xdr/xdr.c
45 * /usr/src/lib/libc/xdr/xdr_mem.c
47 * which are covered by the copyright notice below.
51 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
52 * unrestricted use provided that this legend is included on all tape
53 * media and as a part of the software program in whole or part. Users
54 * may copy or modify Sun RPC without charge, but are not authorized
55 * to license or distribute it to anyone else except as part of a product or
56 * program developed by the user.
58 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
59 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
60 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
62 * Sun RPC is provided with no support and without any obligation on the
63 * part of Sun Microsystems, Inc. to assist in its use, correction,
64 * modification or enhancement.
66 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
67 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
68 * OR ANY PART THEREOF.
70 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
71 * or profits or other special, indirect and consequential damages, even if
72 * Sun has been advised of the possibility of such damages.
74 * Sun Microsystems, Inc.
76 * Mountain View, California 94043
80 * xdr.c, Generic XDR routines implementation.
82 * Copyright (C) 1986, Sun Microsystems, Inc.
84 * These are the "generic" xdr routines used to serialize and de-serialize
85 * most common data items. See xdr.h for more info on the interface to
89 #include <netproto/atm/spans/spans_kxdr.h>
92 * constants specific to the xdr "protocol"
94 #define XDR_FALSE ((long) 0)
95 #define XDR_TRUE ((long) 1)
96 #define LASTUNSIGNED ((u_int) 0-1)
101 static char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
107 xdr_int(XDR
*xdrs
, int *ip
)
111 (xdr_short(xdrs
, (short *)ip
));
112 return (xdr_long(xdrs
, (long *)ip
));
114 if (sizeof (int) == sizeof (long)) {
115 return (xdr_long(xdrs
, (long *)ip
));
117 return (xdr_short(xdrs
, (short *)ip
));
123 * XDR unsigned integers
126 xdr_u_int(XDR
*xdrs
, u_int
*up
)
130 (xdr_short(xdrs
, (short *)up
));
131 return (xdr_u_long(xdrs
, (u_long
*)up
));
133 if (sizeof (u_int
) == sizeof (u_long
)) {
134 return (xdr_u_long(xdrs
, (u_long
*)up
));
136 return (xdr_short(xdrs
, (short *)up
));
143 * same as xdr_u_long - open coded to save a proc call!
146 xdr_long(XDR
*xdrs
, long *lp
)
149 if (xdrs
->x_op
== XDR_ENCODE
)
150 return (XDR_PUTLONG(xdrs
, lp
));
152 if (xdrs
->x_op
== XDR_DECODE
)
153 return (XDR_GETLONG(xdrs
, lp
));
155 if (xdrs
->x_op
== XDR_FREE
)
162 * XDR unsigned long integers
163 * same as xdr_long - open coded to save a proc call!
166 xdr_u_long(XDR
*xdrs
, u_long
*ulp
)
169 if (xdrs
->x_op
== XDR_DECODE
)
170 return (XDR_GETLONG(xdrs
, (long *)ulp
));
171 if (xdrs
->x_op
== XDR_ENCODE
)
172 return (XDR_PUTLONG(xdrs
, (long *)ulp
));
173 if (xdrs
->x_op
== XDR_FREE
)
182 xdr_short(XDR
*xdrs
, short *sp
)
186 switch (xdrs
->x_op
) {
190 return (XDR_PUTLONG(xdrs
, &l
));
193 if (!XDR_GETLONG(xdrs
, &l
)) {
206 * XDR unsigned short integers
209 xdr_u_short(XDR
*xdrs
, u_short
*usp
)
213 switch (xdrs
->x_op
) {
217 return (XDR_PUTLONG(xdrs
, &l
));
220 if (!XDR_GETLONG(xdrs
, &l
)) {
237 xdr_char(XDR
*xdrs
, char *cp
)
242 if (!xdr_int(xdrs
, &i
)) {
250 * XDR an unsigned char
253 xdr_u_char(XDR
*xdrs
, u_char
*cp
)
258 if (!xdr_u_int(xdrs
, &u
)) {
269 xdr_bool(XDR
*xdrs
, bool_t
*bp
)
273 switch (xdrs
->x_op
) {
276 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
277 return (XDR_PUTLONG(xdrs
, &lb
));
280 if (!XDR_GETLONG(xdrs
, &lb
)) {
283 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
296 xdr_enum(XDR
*xdrs
, enum_t
*ep
)
299 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
302 * enums are treated as ints
304 if (sizeof (enum sizecheck
) == sizeof (long)) {
305 return (xdr_long(xdrs
, (long *)ep
));
306 } else if (sizeof (enum sizecheck
) == sizeof (short)) {
307 return (xdr_short(xdrs
, (short *)ep
));
312 (xdr_short(xdrs
, (short *)ep
));
313 return (xdr_long(xdrs
, (long *)ep
));
319 * Allows the specification of a fixed size sequence of opaque bytes.
320 * cp points to the opaque object and cnt gives the byte length.
323 xdr_opaque(XDR
*xdrs
, caddr_t cp
, u_int cnt
)
326 static char crud
[BYTES_PER_XDR_UNIT
];
329 * if no data we are done
335 * round byte count to full xdr units
337 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
339 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
341 if (xdrs
->x_op
== XDR_DECODE
) {
342 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
347 return (XDR_GETBYTES(xdrs
, crud
, rndup
));
350 if (xdrs
->x_op
== XDR_ENCODE
) {
351 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
356 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
359 if (xdrs
->x_op
== XDR_FREE
) {
368 * XDR implementation using kernel buffers
372 * xdr_mem.h, XDR implementation using memory buffers.
374 * Copyright (C) 1984, Sun Microsystems, Inc.
376 * If you have some data to be interpreted as external data representation
377 * or to be converted to external data representation in a memory buffer,
378 * then this is the package for you.
383 void xdrmbuf_init (XDR
*, KBuffer
*, enum xdr_op
);
384 static bool_t
xdrmbuf_getlong (XDR
*, long *);
385 static bool_t
xdrmbuf_putlong (XDR
*, long *);
386 static bool_t
xdrmbuf_getbytes (XDR
*, caddr_t
, u_int
);
387 static bool_t
xdrmbuf_putbytes (XDR
*, caddr_t
, u_int
);
388 static u_int
xdrmbuf_getpos (XDR
*);
390 static struct xdr_ops xdrmbuf_ops
= {
402 * The procedure xdrmbuf_init initializes a stream descriptor for a
406 xdrmbuf_init(XDR
*xdrs
, KBuffer
*m
, enum xdr_op op
)
410 xdrs
->x_ops
= &xdrmbuf_ops
;
411 xdrs
->x_base
= (caddr_t
)m
;
412 KB_DATASTART(m
, xdrs
->x_private
, caddr_t
);
413 xdrs
->x_handy
= KB_LEN(m
);
417 xdrmbuf_getlong(XDR
*xdrs
, long *lp
)
421 * See if long is contained in this buffer
423 if ((xdrs
->x_handy
-= sizeof(long)) < 0) {
427 * We (currently) don't allow a long to span a buffer
429 if (xdrs
->x_handy
!= -sizeof(long)) {
430 kprintf("xdrmbuf_getlong: data spans buffers\n");
435 * Try to move to a chained buffer
437 if ((m
= (KBuffer
*)(xdrs
->x_base
)) != NULL
) {
439 xdrs
->x_base
= (caddr_t
)m
;
443 * Setup new buffer's info
445 KB_DATASTART(m
, xdrs
->x_private
, caddr_t
);
446 if ((xdrs
->x_handy
= KB_LEN(m
) - sizeof(long)) < 0) {
447 kprintf("xdrmbuf_getlong: short buffer\n");
459 * Return the long value
461 *lp
= (long)ntohl((u_long
)(*((long *)(xdrs
->x_private
))));
464 * Advance the data stream
466 xdrs
->x_private
+= sizeof(long);
471 xdrmbuf_putlong(XDR
*xdrs
, long *lp
)
475 * See if long will fit in this buffer
477 if ((xdrs
->x_handy
-= sizeof(long)) < 0) {
481 * We (currently) don't allow a long to span a buffer
483 if (xdrs
->x_handy
!= -sizeof(long)) {
484 kprintf("xdrmbuf_putlong: data spans buffers\n");
489 * Try to move to a chained buffer
491 if ((m
= (KBuffer
*)(xdrs
->x_base
)) != NULL
) {
493 xdrs
->x_base
= (caddr_t
)m
;
497 * Setup new buffer's info
499 KB_DATASTART(m
, xdrs
->x_private
, caddr_t
);
500 if ((xdrs
->x_handy
= KB_LEN(m
) - sizeof(long)) < 0) {
501 kprintf("xdrmbuf_putlong: short buffer\n");
513 * Store the long value into our buffer
515 *(long *)xdrs
->x_private
= (long)htonl((u_long
)(*lp
));
518 * Advance the data stream
520 xdrs
->x_private
+= sizeof(long);
525 xdrmbuf_getbytes(XDR
*xdrs
, caddr_t addr
, u_int len
)
531 if (xdrs
->x_handy
<= 0) {
535 * No data in current buffer, move to a chained buffer
537 if ((m
= (KBuffer
*)(xdrs
->x_base
)) != NULL
) {
539 xdrs
->x_base
= (caddr_t
)m
;
543 * Setup new buffer's info
545 KB_DATASTART(m
, xdrs
->x_private
, caddr_t
);
546 xdrs
->x_handy
= KB_LEN(m
);
556 * Copy from buffer to user's space
558 copy
= MIN(len
, xdrs
->x_handy
);
559 KM_COPY(xdrs
->x_private
, addr
, copy
);
562 * Update data stream controls
564 xdrs
->x_private
+= copy
;
565 xdrs
->x_handy
-= copy
;
573 xdrmbuf_putbytes(XDR
*xdrs
, caddr_t addr
, u_int len
)
579 if (xdrs
->x_handy
<= 0) {
583 * No data in current buffer, move to a chained buffer
585 if ((m
= (KBuffer
*)(xdrs
->x_base
)) != NULL
) {
587 xdrs
->x_base
= (caddr_t
)m
;
591 * Setup new buffer's info
593 KB_DATASTART(m
, xdrs
->x_private
, caddr_t
);
594 xdrs
->x_handy
= KB_LEN(m
);
604 * Copy from user's space into buffer
606 copy
= MIN(len
, xdrs
->x_handy
);
607 KM_COPY(addr
, xdrs
->x_private
, copy
);
610 * Update data stream controls
612 xdrs
->x_private
+= copy
;
613 xdrs
->x_handy
-= copy
;
621 xdrmbuf_getpos(XDR
*xdrs
)
624 return ((u_int
)xdrs
->x_private
- (u_int
)xdrs
->x_base
);