]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/xdr_rec.c
2 * Copyright (c) 1999-2018 Apple 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 "libinfo_common.h"
58 #include <sys/cdefs.h>
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char *sccsid
= "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
61 static char *sccsid
= "@(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC";
63 #include <sys/cdefs.h>
66 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
67 * layer above tcp (for rpc's use).
69 * Copyright (C) 1984, Sun Microsystems, Inc.
71 * These routines interface XDRSTREAMS to a tcp/ip connection.
72 * There is a record marking layer between the xdr stream
73 * and the tcp transport level. A record is composed on one or more
74 * record fragments. A record fragment is a thirty-two bit header followed
75 * by n bytes of data, where n is contained in the header. The header
76 * is in network byte order. Thegh order bit encodes
77 * whether or not the fragment is the last fragment of the record
78 * (1 => fragment is last, 0 => more fragments to follow.
79 * The other 31 bits encode the byte length of the fragment.
82 #include <sys/types.h>
84 #include <netinet/in.h>
92 #include <rpc/types.h>
99 static bool_t
xdrrec_getlong(XDR
*, int *);
100 static bool_t
xdrrec_putlong(XDR
*, const int *);
102 static bool_t
xdrrec_getlong(XDR
*, long *);
103 static bool_t
xdrrec_putlong(XDR
*, const long *);
106 static bool_t
xdrrec_getbytes(XDR
*, char *, u_int
);
108 static bool_t
xdrrec_putbytes(XDR
*, const char *, u_int
);
109 static u_int
xdrrec_getpos(XDR
*);
110 static bool_t
xdrrec_setpos(XDR
*, u_int
);
111 static int32_t *xdrrec_inline(XDR
*, u_int
);
112 static void xdrrec_destroy(XDR
*);
114 bool_t
__xdrrec_getrec(XDR
*, enum xprt_stat
*, bool_t
);
116 static const struct xdr_ops xdrrec_ops
= {
128 * A record is composed of one or more record fragments.
129 * A record fragment is a four-byte header followed by zero to
130 * 2**32-1 bytes. The header is treated as an unsigned 32-bit integer and is
131 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
132 * are a byte count of the fragment. The highest order bit is a boolean:
133 * 1 => this fragment is the last fragment of the record,
134 * 0 => this fragment is followed by more fragment(s).
136 * The fragment/record machinery is not general; it is constructed to
137 * meet the needs of xdr and rpc based on tcp.
140 #define LAST_FRAG ((u_int32_t)(1 << 31))
142 typedef struct rec_strm
{
147 int (*writeit
)(void *, void *, int);
148 char *out_base
; /* output buffer (points to frag header) */
149 char *out_finger
; /* next output position */
150 char *out_boundry
; /* data cannot up to this address */
151 u_int32_t
*frag_header
; /* beginning of curren fragment */
152 bool_t frag_sent
; /* true if buffer sent in middle of record */
156 int (*readit
)(void *, void *, int);
157 size_t in_size
; /* fixed size of the input buffer */
159 char *in_finger
; /* location of next byte to be had */
160 char *in_boundry
; /* can read up to this location */
161 int fbtbc
; /* fragment bytes to be consumed */
167 bool_t in_haveheader
;
176 static u_int
fix_buf_size(u_int
);
177 static bool_t
flush_out(RECSTREAM
*, bool_t
);
178 static bool_t
fill_input_buf(RECSTREAM
*);
179 static bool_t
get_input_bytes(RECSTREAM
*, char *, int);
180 static bool_t
set_input_fragment(RECSTREAM
*);
181 static bool_t
skip_input_bytes(RECSTREAM
*, int);
182 static bool_t
realloc_stream(RECSTREAM
*, int);
186 * Create an xdr handle for xdrrec
187 * xdrrec_create fills in xdrs. Sendsize and recvsize are
188 * send and recv buffer sizes (0 => use default).
189 * tcp_handle is an opaque handle that is passed as the first parameter to
190 * the procedures readit and writeit. Readit and writeit are read and
191 * write respectively. They are like the system
192 * calls expect that they take an opaque handle rather than an fd.
196 xdrrec_create(xdrs
, sendsize
, recvsize
, tcp_handle
, readit
, writeit
)
201 /* like read, but pass it a tcp_handle, not sock */
202 int (*readit
)(void *, void *, int);
203 /* like write, but pass it a tcp_handle, not sock */
204 int (*writeit
)(void *, void *, int);
206 RECSTREAM
*rstrm
= mem_alloc(sizeof(RECSTREAM
));
209 warnx("xdrrec_create: out of memory");
211 * This is bad. Should rework xdrrec_create to
212 * return a handle, and in this case return NULL
216 rstrm
->sendsize
= sendsize
= fix_buf_size(sendsize
);
217 rstrm
->out_base
= mem_alloc(rstrm
->sendsize
);
218 if (rstrm
->out_base
== NULL
) {
219 warnx("xdrrec_create: out of memory");
220 mem_free(rstrm
, sizeof(RECSTREAM
));
223 rstrm
->recvsize
= recvsize
= fix_buf_size(recvsize
);
224 rstrm
->in_base
= mem_alloc(recvsize
);
225 if (rstrm
->in_base
== NULL
) {
226 warnx("xdrrec_create: out of memory");
227 mem_free(rstrm
->out_base
, sendsize
);
228 mem_free(rstrm
, sizeof(RECSTREAM
));
234 xdrs
->x_ops
= &xdrrec_ops
;
235 xdrs
->x_private
= rstrm
;
236 rstrm
->tcp_handle
= tcp_handle
;
237 rstrm
->readit
= readit
;
238 rstrm
->writeit
= writeit
;
239 rstrm
->out_finger
= rstrm
->out_boundry
= rstrm
->out_base
;
240 rstrm
->frag_header
= (u_int32_t
*)(void *)rstrm
->out_base
;
241 rstrm
->out_finger
+= sizeof(u_int32_t
);
242 rstrm
->out_boundry
+= sendsize
;
243 rstrm
->frag_sent
= FALSE
;
244 rstrm
->in_size
= recvsize
;
245 rstrm
->in_boundry
= rstrm
->in_base
;
246 rstrm
->in_finger
= (rstrm
->in_boundry
+= recvsize
);
248 rstrm
->last_frag
= TRUE
;
249 rstrm
->in_haveheader
= FALSE
;
250 rstrm
->in_hdrlen
= 0;
251 rstrm
->in_hdrp
= (char *)(void *)&rstrm
->in_header
;
252 rstrm
->nonblock
= FALSE
;
253 rstrm
->in_reclen
= 0;
254 rstrm
->in_received
= 0;
259 * The reoutines defined below are the xdr ops which will go into the
260 * xdr handle filled in by xdrrec_create.
264 xdrrec_getlong(xdrs
, lp
)
272 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
273 int32_t *buflp
= (int32_t *)(void *)(rstrm
->in_finger
);
276 /* first try the inline, fast case */
277 if ((rstrm
->fbtbc
>= sizeof(int32_t)) && ((rstrm
->in_boundry
- (char *)buflp
) >= sizeof(int32_t)))
280 rstrm
->fbtbc
-= sizeof(int32_t);
281 rstrm
->in_finger
+= sizeof(int32_t);
285 if (! xdrrec_getbytes(xdrs
, (char *)(void *)&mylong
, sizeof(int32_t))) return (FALSE
);
293 xdrrec_putlong(xdrs
, lp
)
301 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
302 int32_t *dest_lp
= ((int32_t *)(void *)(rstrm
->out_finger
));
304 if ((rstrm
->out_finger
+= sizeof(int32_t)) > rstrm
->out_boundry
)
307 * this case should almost never happen so the code is
310 rstrm
->out_finger
-= sizeof(int32_t);
311 rstrm
->frag_sent
= TRUE
;
312 if (! flush_out(rstrm
, FALSE
)) return (FALSE
);
313 dest_lp
= ((int32_t *)(void *)(rstrm
->out_finger
));
314 rstrm
->out_finger
+= sizeof(int32_t);
317 *dest_lp
= htonl(*lp
);
321 static bool_t
/* must manage buffers, fragments, and records */
322 xdrrec_getbytes(xdrs
, addr
, len
)
327 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
331 current
= (int)rstrm
->fbtbc
;
333 if (rstrm
->last_frag
)
335 if (! set_input_fragment(rstrm
))
339 current
= (len
< current
) ? len
: current
;
340 if (! get_input_bytes(rstrm
, addr
, current
))
343 rstrm
->fbtbc
-= current
;
350 xdrrec_putbytes(xdrs
, addr
, len
)
355 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
359 current
= (size_t)(rstrm
->out_boundry
- rstrm
->out_finger
);
360 current
= (len
< current
) ? len
: current
;
361 memmove(rstrm
->out_finger
, addr
, current
);
362 rstrm
->out_finger
+= current
;
365 if (rstrm
->out_finger
== rstrm
->out_boundry
) {
366 rstrm
->frag_sent
= TRUE
;
367 if (! flush_out(rstrm
, FALSE
))
378 RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
382 /* tcp_handle is in actual fact just a file descriptor */
384 memcpy(&hfd
, rstrm
->tcp_handle
, sizeof(hfd
));
386 pos
= lseek(hfd
, 0, 1);
388 switch (xdrs
->x_op
) {
391 pos
+= rstrm
->out_finger
- rstrm
->out_base
;
395 pos
-= rstrm
->in_boundry
- rstrm
->in_finger
;
402 return ((u_int
) pos
);
406 xdrrec_setpos(xdrs
, pos
)
410 RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
411 u_int currpos
= xdrrec_getpos(xdrs
);
412 int delta
= currpos
- pos
;
415 if ((int)currpos
!= -1)
416 switch (xdrs
->x_op
) {
419 newpos
= rstrm
->out_finger
- delta
;
420 if ((newpos
> (char *)(void *)(rstrm
->frag_header
)) &&
421 (newpos
< rstrm
->out_boundry
)) {
422 rstrm
->out_finger
= newpos
;
428 newpos
= rstrm
->in_finger
- delta
;
429 if ((delta
< (int)(rstrm
->fbtbc
)) &&
430 (newpos
<= rstrm
->in_boundry
) &&
431 (newpos
>= rstrm
->in_base
)) {
432 rstrm
->in_finger
= newpos
;
433 rstrm
->fbtbc
-= delta
;
445 xdrrec_inline(xdrs
, len
)
449 RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
452 switch (xdrs
->x_op
) {
455 if ((rstrm
->out_finger
+ len
) <= rstrm
->out_boundry
) {
456 buf
= (int32_t *)(void *)rstrm
->out_finger
;
457 rstrm
->out_finger
+= len
;
462 if ((len
<= rstrm
->fbtbc
) &&
463 ((rstrm
->in_finger
+ len
) <= rstrm
->in_boundry
)) {
464 buf
= (int32_t *)(void *)rstrm
->in_finger
;
466 rstrm
->in_finger
+= len
;
480 RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
482 mem_free(rstrm
->out_base
, rstrm
->sendsize
);
483 mem_free(rstrm
->in_base
, rstrm
->recvsize
);
484 mem_free(rstrm
, sizeof(RECSTREAM
));
489 * Exported routines to manage xdr records
493 * Before reading (deserializing from the stream, one should always call
494 * this procedure to guarantee proper record alignment.
498 xdrrec_skiprecord(xdrs
)
501 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
502 enum xprt_stat xstat
;
504 if (rstrm
->nonblock
) {
505 if (__xdrrec_getrec(xdrs
, &xstat
, FALSE
)) {
509 if (rstrm
->in_finger
== rstrm
->in_boundry
&&
510 xstat
== XPRT_MOREREQS
) {
517 while (rstrm
->fbtbc
> 0 || (! rstrm
->last_frag
)) {
518 if (! skip_input_bytes(rstrm
, rstrm
->fbtbc
))
521 if ((! rstrm
->last_frag
) && (! set_input_fragment(rstrm
)))
524 rstrm
->last_frag
= FALSE
;
529 * Look ahead function.
530 * Returns TRUE iff there is no more input in the buffer
531 * after consuming the rest of the current record.
538 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
540 while (rstrm
->fbtbc
> 0 || (! rstrm
->last_frag
)) {
541 if (! skip_input_bytes(rstrm
, rstrm
->fbtbc
))
544 if ((! rstrm
->last_frag
) && (! set_input_fragment(rstrm
)))
547 if (rstrm
->in_finger
== rstrm
->in_boundry
)
553 * The client must tell the package when an end-of-record has occurred.
554 * The second paraemters tells whether the record should be flushed to the
555 * (output) tcp stream. (This let's the package support batched or
556 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
560 xdrrec_endofrecord(xdrs
, sendnow
)
564 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
565 unsigned int len
; /* fragment length */
567 if (sendnow
|| rstrm
->frag_sent
||
568 (rstrm
->out_finger
+ sizeof(u_int32_t
) >= rstrm
->out_boundry
)) {
569 rstrm
->frag_sent
= FALSE
;
570 return (flush_out(rstrm
, TRUE
));
572 len
= rstrm
->out_finger
- (char *)(rstrm
->frag_header
) - sizeof(u_int32_t
);
573 *(rstrm
->frag_header
) = htonl(len
| LAST_FRAG
);
574 rstrm
->frag_header
= (u_int32_t
*)(void *)rstrm
->out_finger
;
575 rstrm
->out_finger
+= sizeof(u_int32_t
);
580 * Fill the stream buffer with a record for a non-blocking connection.
581 * Return true if a record is available in the buffer, false if not.
584 __xdrrec_getrec(xdrs
, statp
, expectdata
)
586 enum xprt_stat
*statp
;
589 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
593 if (!rstrm
->in_haveheader
) {
594 n
= rstrm
->readit(rstrm
->tcp_handle
, rstrm
->in_hdrp
,
595 (int)sizeof (rstrm
->in_header
) - rstrm
->in_hdrlen
);
597 *statp
= expectdata
? XPRT_DIED
: XPRT_IDLE
;
605 rstrm
->in_hdrlen
+= n
;
606 if (rstrm
->in_hdrlen
< sizeof (rstrm
->in_header
)) {
607 *statp
= XPRT_MOREREQS
;
610 rstrm
->in_header
= ntohl(rstrm
->in_header
);
611 fraglen
= (int)(rstrm
->in_header
& ~LAST_FRAG
);
612 if (fraglen
== 0 || fraglen
> rstrm
->in_maxrec
||
613 (rstrm
->in_reclen
+ fraglen
) > rstrm
->in_maxrec
) {
617 rstrm
->in_reclen
+= fraglen
;
618 if (rstrm
->in_reclen
> rstrm
->recvsize
)
619 realloc_stream(rstrm
, rstrm
->in_reclen
);
620 if (rstrm
->in_header
& LAST_FRAG
) {
621 rstrm
->in_header
&= ~LAST_FRAG
;
622 rstrm
->last_frag
= TRUE
;
626 n
= rstrm
->readit(rstrm
->tcp_handle
,
627 rstrm
->in_base
+ rstrm
->in_received
,
628 (rstrm
->in_reclen
- rstrm
->in_received
));
636 *statp
= expectdata
? XPRT_DIED
: XPRT_IDLE
;
640 rstrm
->in_received
+= n
;
642 if (rstrm
->in_received
== rstrm
->in_reclen
) {
643 rstrm
->in_haveheader
= FALSE
;
644 rstrm
->in_hdrp
= (char *)(void *)&rstrm
->in_header
;
645 rstrm
->in_hdrlen
= 0;
646 if (rstrm
->last_frag
) {
647 rstrm
->fbtbc
= rstrm
->in_reclen
;
648 rstrm
->in_boundry
= rstrm
->in_base
+ rstrm
->in_reclen
;
649 rstrm
->in_finger
= rstrm
->in_base
;
650 rstrm
->in_reclen
= rstrm
->in_received
= 0;
651 *statp
= XPRT_MOREREQS
;
656 *statp
= XPRT_MOREREQS
;
661 __xdrrec_setnonblock(xdrs
, maxrec
)
665 RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
667 rstrm
->nonblock
= TRUE
;
669 maxrec
= rstrm
->recvsize
;
670 rstrm
->in_maxrec
= maxrec
;
675 * Internal useful routines
678 flush_out(rstrm
, eor
)
682 u_int32_t eormask
= (eor
== TRUE
) ? LAST_FRAG
: 0;
683 u_int32_t len
= rstrm
->out_finger
- (char *)(rstrm
->frag_header
) - sizeof(u_int32_t
);
685 *(rstrm
->frag_header
) = htonl(len
| eormask
);
686 len
= rstrm
->out_finger
- rstrm
->out_base
;
687 if ((*(rstrm
->writeit
))(rstrm
->tcp_handle
, rstrm
->out_base
, len
) != len
)
689 rstrm
->frag_header
= (u_int32_t
*)(void *)rstrm
->out_base
;
690 rstrm
->out_finger
= (char *)rstrm
->out_base
+ sizeof(u_int32_t
);
694 static bool_t
/* knows nothing about records! Only about input buffers */
695 fill_input_buf(rstrm
)
705 where
= rstrm
->in_base
;
706 i
= (size_t)(rstrm
->in_boundry
) % BYTES_PER_XDR_UNIT
;
708 len
= rstrm
->in_size
- i
;
709 if ((len
= (*(rstrm
->readit
))(rstrm
->tcp_handle
, where
, len
)) == -1)
711 rstrm
->in_finger
= where
;
713 rstrm
->in_boundry
= where
;
717 static bool_t
/* knows nothing about records! Only about input buffers */
718 get_input_bytes(rstrm
, addr
, len
)
725 if (rstrm
->nonblock
) {
726 if (len
> (int)(rstrm
->in_boundry
- rstrm
->in_finger
))
728 memcpy(addr
, rstrm
->in_finger
, (size_t)len
);
729 rstrm
->in_finger
+= len
;
734 current
= (size_t)(rstrm
->in_boundry
- rstrm
->in_finger
);
736 if (! fill_input_buf(rstrm
))
740 current
= (len
< current
) ? len
: current
;
741 memmove(addr
, rstrm
->in_finger
, current
);
742 rstrm
->in_finger
+= current
;
749 static bool_t
/* next two bytes of the input stream are treated as a header */
750 set_input_fragment(rstrm
)
757 if (! get_input_bytes(rstrm
, (char *)(void *)&header
, sizeof(header
)))
759 header
= ntohl(header
);
760 rstrm
->last_frag
= ((header
& LAST_FRAG
) == 0) ? FALSE
: TRUE
;
762 * Sanity check. Try not to accept wildly incorrect
763 * record sizes. Unfortunately, the only record size
764 * we can positively identify as being 'wildly incorrect'
765 * is zero. Ridiculously large record sizes may look wrong,
766 * but we don't have any way to be certain that they aren't
767 * what the client actually intended to send us.
771 rstrm
->fbtbc
= header
& (~LAST_FRAG
);
775 static bool_t
/* consumes input bytes; knows nothing about records! */
776 skip_input_bytes(rstrm
, cnt
)
783 current
= (size_t)(rstrm
->in_boundry
- rstrm
->in_finger
);
785 if (! fill_input_buf(rstrm
))
789 current
= (cnt
< current
) ? cnt
: current
;
790 rstrm
->in_finger
+= current
;
807 * Reallocate the input buffer for a non-block stream.
810 realloc_stream(rstrm
, size
)
817 if (size
> rstrm
->recvsize
) {
818 buf
= realloc(rstrm
->in_base
, (size_t)size
);
821 diff
= buf
- rstrm
->in_base
;
822 rstrm
->in_finger
+= diff
;
823 rstrm
->in_base
= buf
;
824 rstrm
->in_boundry
= buf
+ size
;
825 rstrm
->recvsize
= size
;
826 rstrm
->in_size
= size
;