]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/xdr_rec.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: xdr_rec.c,v 1.18 2000/07/06 03:10:35 christos Exp $ */
28 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
29 * unrestricted use provided that this legend is included on all tape
30 * media and as a part of the software program in whole or part. Users
31 * may copy or modify Sun RPC without charge, but are not authorized
32 * to license or distribute it to anyone else except as part of a product or
33 * program developed by the user.
35 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
36 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
39 * Sun RPC is provided with no support and without any obligation on the
40 * part of Sun Microsystems, Inc. to assist in its use, correction,
41 * modification or enhancement.
43 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
44 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
45 * OR ANY PART THEREOF.
47 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
48 * or profits or other special, indirect and consequential damages, even if
49 * Sun has been advised of the possibility of such damages.
51 * Sun Microsystems, Inc.
53 * Mountain View, California 94043
56 #include <sys/cdefs.h>
57 #if defined(LIBC_SCCS) && !defined(lint)
58 static char *sccsid
= "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
59 static char *sccsid
= "@(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC";
61 #include <sys/cdefs.h>
64 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
65 * layer above tcp (for rpc's use).
67 * Copyright (C) 1984, Sun Microsystems, Inc.
69 * These routines interface XDRSTREAMS to a tcp/ip connection.
70 * There is a record marking layer between the xdr stream
71 * and the tcp transport level. A record is composed on one or more
72 * record fragments. A record fragment is a thirty-two bit header followed
73 * by n bytes of data, where n is contained in the header. The header
74 * is in network byte order. Thegh order bit encodes
75 * whether or not the fragment is the last fragment of the record
76 * (1 => fragment is last, 0 => more fragments to follow.
77 * The other 31 bits encode the byte length of the fragment.
80 #include <sys/types.h>
82 #include <netinet/in.h>
90 #include <rpc/types.h>
97 static bool_t
xdrrec_getlong(XDR
*, int *);
98 static bool_t
xdrrec_putlong(XDR
*, const int *);
100 static bool_t
xdrrec_getlong(XDR
*, long *);
101 static bool_t
xdrrec_putlong(XDR
*, const long *);
104 static bool_t
xdrrec_getbytes(XDR
*, char *, u_int
);
106 static bool_t
xdrrec_putbytes(XDR
*, const char *, u_int
);
107 static u_int
xdrrec_getpos(XDR
*);
108 static bool_t
xdrrec_setpos(XDR
*, u_int
);
109 static int32_t *xdrrec_inline(XDR
*, u_int
);
110 static void xdrrec_destroy(XDR
*);
112 bool_t
__xdrrec_getrec(XDR
*, enum xprt_stat
*, bool_t
);
114 static const struct xdr_ops xdrrec_ops
= {
126 * A record is composed of one or more record fragments.
127 * A record fragment is a four-byte header followed by zero to
128 * 2**32-1 bytes. The header is treated as an unsigned 32-bit integer and is
129 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
130 * are a byte count of the fragment. The highest order bit is a boolean:
131 * 1 => this fragment is the last fragment of the record,
132 * 0 => this fragment is followed by more fragment(s).
134 * The fragment/record machinery is not general; it is constructed to
135 * meet the needs of xdr and rpc based on tcp.
138 #define LAST_FRAG ((u_int32_t)(1 << 31))
140 typedef struct rec_strm
{
145 int (*writeit
)(void *, void *, int);
146 char *out_base
; /* output buffer (points to frag header) */
147 char *out_finger
; /* next output position */
148 char *out_boundry
; /* data cannot up to this address */
149 u_int32_t
*frag_header
; /* beginning of curren fragment */
150 bool_t frag_sent
; /* true if buffer sent in middle of record */
154 int (*readit
)(void *, void *, int);
155 size_t in_size
; /* fixed size of the input buffer */
157 char *in_finger
; /* location of next byte to be had */
158 char *in_boundry
; /* can read up to this location */
159 int fbtbc
; /* fragment bytes to be consumed */
165 bool_t in_haveheader
;
174 static u_int
fix_buf_size(u_int
);
175 static bool_t
flush_out(RECSTREAM
*, bool_t
);
176 static bool_t
fill_input_buf(RECSTREAM
*);
177 static bool_t
get_input_bytes(RECSTREAM
*, char *, int);
178 static bool_t
set_input_fragment(RECSTREAM
*);
179 static bool_t
skip_input_bytes(RECSTREAM
*, int);
180 static bool_t
realloc_stream(RECSTREAM
*, int);
184 * Create an xdr handle for xdrrec
185 * xdrrec_create fills in xdrs. Sendsize and recvsize are
186 * send and recv buffer sizes (0 => use default).
187 * tcp_handle is an opaque handle that is passed as the first parameter to
188 * the procedures readit and writeit. Readit and writeit are read and
189 * write respectively. They are like the system
190 * calls expect that they take an opaque handle rather than an fd.
193 xdrrec_create(xdrs
, sendsize
, recvsize
, tcp_handle
, readit
, writeit
)
198 /* like read, but pass it a tcp_handle, not sock */
199 int (*readit
)(void *, void *, int);
200 /* like write, but pass it a tcp_handle, not sock */
201 int (*writeit
)(void *, void *, int);
203 RECSTREAM
*rstrm
= mem_alloc(sizeof(RECSTREAM
));
206 warnx("xdrrec_create: out of memory");
208 * This is bad. Should rework xdrrec_create to
209 * return a handle, and in this case return NULL
213 rstrm
->sendsize
= sendsize
= fix_buf_size(sendsize
);
214 rstrm
->out_base
= mem_alloc(rstrm
->sendsize
);
215 if (rstrm
->out_base
== NULL
) {
216 warnx("xdrrec_create: out of memory");
217 mem_free(rstrm
, sizeof(RECSTREAM
));
220 rstrm
->recvsize
= recvsize
= fix_buf_size(recvsize
);
221 rstrm
->in_base
= mem_alloc(recvsize
);
222 if (rstrm
->in_base
== NULL
) {
223 warnx("xdrrec_create: out of memory");
224 mem_free(rstrm
->out_base
, sendsize
);
225 mem_free(rstrm
, sizeof(RECSTREAM
));
231 xdrs
->x_ops
= &xdrrec_ops
;
232 xdrs
->x_private
= rstrm
;
233 rstrm
->tcp_handle
= tcp_handle
;
234 rstrm
->readit
= readit
;
235 rstrm
->writeit
= writeit
;
236 rstrm
->out_finger
= rstrm
->out_boundry
= rstrm
->out_base
;
237 rstrm
->frag_header
= (u_int32_t
*)(void *)rstrm
->out_base
;
238 rstrm
->out_finger
+= sizeof(u_int32_t
);
239 rstrm
->out_boundry
+= sendsize
;
240 rstrm
->frag_sent
= FALSE
;
241 rstrm
->in_size
= recvsize
;
242 rstrm
->in_boundry
= rstrm
->in_base
;
243 rstrm
->in_finger
= (rstrm
->in_boundry
+= recvsize
);
245 rstrm
->last_frag
= TRUE
;
246 rstrm
->in_haveheader
= FALSE
;
247 rstrm
->in_hdrlen
= 0;
248 rstrm
->in_hdrp
= (char *)(void *)&rstrm
->in_header
;
249 rstrm
->nonblock
= FALSE
;
250 rstrm
->in_reclen
= 0;
251 rstrm
->in_received
= 0;
256 * The reoutines defined below are the xdr ops which will go into the
257 * xdr handle filled in by xdrrec_create.
261 xdrrec_getlong(xdrs
, lp
)
269 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
270 int32_t *buflp
= (int32_t *)(void *)(rstrm
->in_finger
);
273 /* first try the inline, fast case */
274 if ((rstrm
->fbtbc
>= sizeof(int32_t)) && ((rstrm
->in_boundry
- (char *)buflp
) >= sizeof(int32_t)))
277 rstrm
->fbtbc
-= sizeof(int32_t);
278 rstrm
->in_finger
+= sizeof(int32_t);
282 if (! xdrrec_getbytes(xdrs
, (char *)(void *)&mylong
, sizeof(int32_t))) return (FALSE
);
290 xdrrec_putlong(xdrs
, lp
)
298 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
299 int32_t *dest_lp
= ((int32_t *)(void *)(rstrm
->out_finger
));
301 if ((rstrm
->out_finger
+= sizeof(int32_t)) > rstrm
->out_boundry
)
304 * this case should almost never happen so the code is
307 rstrm
->out_finger
-= sizeof(int32_t);
308 rstrm
->frag_sent
= TRUE
;
309 if (! flush_out(rstrm
, FALSE
)) return (FALSE
);
310 dest_lp
= ((int32_t *)(void *)(rstrm
->out_finger
));
311 rstrm
->out_finger
+= sizeof(int32_t);
314 *dest_lp
= htonl(*lp
);
318 static bool_t
/* must manage buffers, fragments, and records */
319 xdrrec_getbytes(xdrs
, addr
, len
)
324 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
328 current
= (int)rstrm
->fbtbc
;
330 if (rstrm
->last_frag
)
332 if (! set_input_fragment(rstrm
))
336 current
= (len
< current
) ? len
: current
;
337 if (! get_input_bytes(rstrm
, addr
, current
))
340 rstrm
->fbtbc
-= current
;
347 xdrrec_putbytes(xdrs
, addr
, len
)
352 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
356 current
= (size_t)(rstrm
->out_boundry
- rstrm
->out_finger
);
357 current
= (len
< current
) ? len
: current
;
358 memmove(rstrm
->out_finger
, addr
, current
);
359 rstrm
->out_finger
+= current
;
362 if (rstrm
->out_finger
== rstrm
->out_boundry
) {
363 rstrm
->frag_sent
= TRUE
;
364 if (! flush_out(rstrm
, FALSE
))
375 RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
379 /* tcp_handle is in actual fact just a file descriptor */
381 memcpy(&hfd
, rstrm
->tcp_handle
, sizeof(hfd
));
383 pos
= lseek(hfd
, 0, 1);
385 switch (xdrs
->x_op
) {
388 pos
+= rstrm
->out_finger
- rstrm
->out_base
;
392 pos
-= rstrm
->in_boundry
- rstrm
->in_finger
;
399 return ((u_int
) pos
);
403 xdrrec_setpos(xdrs
, pos
)
407 RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
408 u_int currpos
= xdrrec_getpos(xdrs
);
409 int delta
= currpos
- pos
;
412 if ((int)currpos
!= -1)
413 switch (xdrs
->x_op
) {
416 newpos
= rstrm
->out_finger
- delta
;
417 if ((newpos
> (char *)(void *)(rstrm
->frag_header
)) &&
418 (newpos
< rstrm
->out_boundry
)) {
419 rstrm
->out_finger
= newpos
;
425 newpos
= rstrm
->in_finger
- delta
;
426 if ((delta
< (int)(rstrm
->fbtbc
)) &&
427 (newpos
<= rstrm
->in_boundry
) &&
428 (newpos
>= rstrm
->in_base
)) {
429 rstrm
->in_finger
= newpos
;
430 rstrm
->fbtbc
-= delta
;
442 xdrrec_inline(xdrs
, len
)
446 RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
449 switch (xdrs
->x_op
) {
452 if ((rstrm
->out_finger
+ len
) <= rstrm
->out_boundry
) {
453 buf
= (int32_t *)(void *)rstrm
->out_finger
;
454 rstrm
->out_finger
+= len
;
459 if ((len
<= rstrm
->fbtbc
) &&
460 ((rstrm
->in_finger
+ len
) <= rstrm
->in_boundry
)) {
461 buf
= (int32_t *)(void *)rstrm
->in_finger
;
463 rstrm
->in_finger
+= len
;
477 RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
479 mem_free(rstrm
->out_base
, rstrm
->sendsize
);
480 mem_free(rstrm
->in_base
, rstrm
->recvsize
);
481 mem_free(rstrm
, sizeof(RECSTREAM
));
486 * Exported routines to manage xdr records
490 * Before reading (deserializing from the stream, one should always call
491 * this procedure to guarantee proper record alignment.
494 xdrrec_skiprecord(xdrs
)
497 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
498 enum xprt_stat xstat
;
500 if (rstrm
->nonblock
) {
501 if (__xdrrec_getrec(xdrs
, &xstat
, FALSE
)) {
505 if (rstrm
->in_finger
== rstrm
->in_boundry
&&
506 xstat
== XPRT_MOREREQS
) {
513 while (rstrm
->fbtbc
> 0 || (! rstrm
->last_frag
)) {
514 if (! skip_input_bytes(rstrm
, rstrm
->fbtbc
))
517 if ((! rstrm
->last_frag
) && (! set_input_fragment(rstrm
)))
520 rstrm
->last_frag
= FALSE
;
525 * Look ahead function.
526 * Returns TRUE iff there is no more input in the buffer
527 * after consuming the rest of the current record.
533 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
535 while (rstrm
->fbtbc
> 0 || (! rstrm
->last_frag
)) {
536 if (! skip_input_bytes(rstrm
, rstrm
->fbtbc
))
539 if ((! rstrm
->last_frag
) && (! set_input_fragment(rstrm
)))
542 if (rstrm
->in_finger
== rstrm
->in_boundry
)
548 * The client must tell the package when an end-of-record has occurred.
549 * The second paraemters tells whether the record should be flushed to the
550 * (output) tcp stream. (This let's the package support batched or
551 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
554 xdrrec_endofrecord(xdrs
, sendnow
)
558 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
559 unsigned int len
; /* fragment length */
561 if (sendnow
|| rstrm
->frag_sent
||
562 (rstrm
->out_finger
+ sizeof(u_int32_t
) >= rstrm
->out_boundry
)) {
563 rstrm
->frag_sent
= FALSE
;
564 return (flush_out(rstrm
, TRUE
));
566 len
= rstrm
->out_finger
- (char *)(rstrm
->frag_header
) - sizeof(u_int32_t
);
567 *(rstrm
->frag_header
) = htonl(len
| LAST_FRAG
);
568 rstrm
->frag_header
= (u_int32_t
*)(void *)rstrm
->out_finger
;
569 rstrm
->out_finger
+= sizeof(u_int32_t
);
574 * Fill the stream buffer with a record for a non-blocking connection.
575 * Return true if a record is available in the buffer, false if not.
578 __xdrrec_getrec(xdrs
, statp
, expectdata
)
580 enum xprt_stat
*statp
;
583 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
587 if (!rstrm
->in_haveheader
) {
588 n
= rstrm
->readit(rstrm
->tcp_handle
, rstrm
->in_hdrp
,
589 (int)sizeof (rstrm
->in_header
) - rstrm
->in_hdrlen
);
591 *statp
= expectdata
? XPRT_DIED
: XPRT_IDLE
;
599 rstrm
->in_hdrlen
+= n
;
600 if (rstrm
->in_hdrlen
< sizeof (rstrm
->in_header
)) {
601 *statp
= XPRT_MOREREQS
;
604 rstrm
->in_header
= ntohl(rstrm
->in_header
);
605 fraglen
= (int)(rstrm
->in_header
& ~LAST_FRAG
);
606 if (fraglen
== 0 || fraglen
> rstrm
->in_maxrec
||
607 (rstrm
->in_reclen
+ fraglen
) > rstrm
->in_maxrec
) {
611 rstrm
->in_reclen
+= fraglen
;
612 if (rstrm
->in_reclen
> rstrm
->recvsize
)
613 realloc_stream(rstrm
, rstrm
->in_reclen
);
614 if (rstrm
->in_header
& LAST_FRAG
) {
615 rstrm
->in_header
&= ~LAST_FRAG
;
616 rstrm
->last_frag
= TRUE
;
620 n
= rstrm
->readit(rstrm
->tcp_handle
,
621 rstrm
->in_base
+ rstrm
->in_received
,
622 (rstrm
->in_reclen
- rstrm
->in_received
));
630 *statp
= expectdata
? XPRT_DIED
: XPRT_IDLE
;
634 rstrm
->in_received
+= n
;
636 if (rstrm
->in_received
== rstrm
->in_reclen
) {
637 rstrm
->in_haveheader
= FALSE
;
638 rstrm
->in_hdrp
= (char *)(void *)&rstrm
->in_header
;
639 rstrm
->in_hdrlen
= 0;
640 if (rstrm
->last_frag
) {
641 rstrm
->fbtbc
= rstrm
->in_reclen
;
642 rstrm
->in_boundry
= rstrm
->in_base
+ rstrm
->in_reclen
;
643 rstrm
->in_finger
= rstrm
->in_base
;
644 rstrm
->in_reclen
= rstrm
->in_received
= 0;
645 *statp
= XPRT_MOREREQS
;
650 *statp
= XPRT_MOREREQS
;
655 __xdrrec_setnonblock(xdrs
, maxrec
)
659 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
661 rstrm
->nonblock
= TRUE
;
663 maxrec
= rstrm
->recvsize
;
664 rstrm
->in_maxrec
= maxrec
;
669 * Internal useful routines
672 flush_out(rstrm
, eor
)
676 u_int32_t eormask
= (eor
== TRUE
) ? LAST_FRAG
: 0;
677 u_int32_t len
= rstrm
->out_finger
- (char *)(rstrm
->frag_header
) - sizeof(u_int32_t
);
679 *(rstrm
->frag_header
) = htonl(len
| eormask
);
680 len
= rstrm
->out_finger
- rstrm
->out_base
;
681 if ((*(rstrm
->writeit
))(rstrm
->tcp_handle
, rstrm
->out_base
, len
) != len
)
683 rstrm
->frag_header
= (u_int32_t
*)(void *)rstrm
->out_base
;
684 rstrm
->out_finger
= (char *)rstrm
->out_base
+ sizeof(u_int32_t
);
688 static bool_t
/* knows nothing about records! Only about input buffers */
689 fill_input_buf(rstrm
)
699 where
= rstrm
->in_base
;
700 i
= (size_t)(rstrm
->in_boundry
) % BYTES_PER_XDR_UNIT
;
702 len
= rstrm
->in_size
- i
;
703 if ((len
= (*(rstrm
->readit
))(rstrm
->tcp_handle
, where
, len
)) == -1)
705 rstrm
->in_finger
= where
;
707 rstrm
->in_boundry
= where
;
711 static bool_t
/* knows nothing about records! Only about input buffers */
712 get_input_bytes(rstrm
, addr
, len
)
719 if (rstrm
->nonblock
) {
720 if (len
> (int)(rstrm
->in_boundry
- rstrm
->in_finger
))
722 memcpy(addr
, rstrm
->in_finger
, (size_t)len
);
723 rstrm
->in_finger
+= len
;
728 current
= (size_t)(rstrm
->in_boundry
- rstrm
->in_finger
);
730 if (! fill_input_buf(rstrm
))
734 current
= (len
< current
) ? len
: current
;
735 memmove(addr
, rstrm
->in_finger
, current
);
736 rstrm
->in_finger
+= current
;
743 static bool_t
/* next two bytes of the input stream are treated as a header */
744 set_input_fragment(rstrm
)
751 if (! get_input_bytes(rstrm
, (char *)(void *)&header
, sizeof(header
)))
753 header
= ntohl(header
);
754 rstrm
->last_frag
= ((header
& LAST_FRAG
) == 0) ? FALSE
: TRUE
;
756 * Sanity check. Try not to accept wildly incorrect
757 * record sizes. Unfortunately, the only record size
758 * we can positively identify as being 'wildly incorrect'
759 * is zero. Ridiculously large record sizes may look wrong,
760 * but we don't have any way to be certain that they aren't
761 * what the client actually intended to send us.
765 rstrm
->fbtbc
= header
& (~LAST_FRAG
);
769 static bool_t
/* consumes input bytes; knows nothing about records! */
770 skip_input_bytes(rstrm
, cnt
)
777 current
= (size_t)(rstrm
->in_boundry
- rstrm
->in_finger
);
779 if (! fill_input_buf(rstrm
))
783 current
= (cnt
< current
) ? cnt
: current
;
784 rstrm
->in_finger
+= current
;
801 * Reallocate the input buffer for a non-block stream.
804 realloc_stream(rstrm
, size
)
811 if (size
> rstrm
->recvsize
) {
812 buf
= realloc(rstrm
->in_base
, (size_t)size
);
815 diff
= buf
- rstrm
->in_base
;
816 rstrm
->in_finger
+= diff
;
817 rstrm
->in_base
= buf
;
818 rstrm
->in_boundry
= buf
+ size
;
819 rstrm
->recvsize
= size
;
820 rstrm
->in_size
= size
;