]>
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 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
52 #if defined(LIBC_SCCS) && !defined(lint)
53 /*static char *sccsid = "from: @(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";*/
54 /*static char *sccsid = "from: @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC";*/
55 static char *rcsid
= "$Id: xdr_rec.c,v 1.2 1999/10/14 21:56:55 wsanchez Exp $";
59 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
60 * layer above tcp (for rpc's use).
62 * Copyright (C) 1984, Sun Microsystems, Inc.
64 * These routines interface XDRSTREAMS to a tcp/ip connection.
65 * There is a record marking layer between the xdr stream
66 * and the tcp transport level. A record is composed on one or more
67 * record fragments. A record fragment is a thirty-two bit header followed
68 * by n bytes of data, where n is contained in the header. The header
69 * is represented as a htonl(u_long). Thegh order bit encodes
70 * whether or not the fragment is the last fragment of the record
71 * (1 => fragment is last, 0 => more fragments to follow.
72 * The other 31 bits encode the byte length of the fragment.
77 #include <rpc/types.h>
79 #include <netinet/in.h>
81 static u_int
fix_buf_size();
82 static bool_t
flush_out();
83 static bool_t
get_input_bytes();
84 static bool_t
set_input_fragment();
85 static bool_t
skip_input_bytes();
87 static bool_t
xdrrec_getlong();
88 static bool_t
xdrrec_putlong();
89 static bool_t
xdrrec_getbytes();
90 static bool_t
xdrrec_putbytes();
91 static u_int
xdrrec_getpos();
92 static bool_t
xdrrec_setpos();
93 static long * xdrrec_inline();
94 static void xdrrec_destroy();
96 static struct xdr_ops xdrrec_ops
= {
108 * A record is composed of one or more record fragments.
109 * A record fragment is a two-byte header followed by zero to
110 * 2**32-1 bytes. The header is treated as a long unsigned and is
111 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
112 * are a byte count of the fragment. The highest order bit is a boolean:
113 * 1 => this fragment is the last fragment of the record,
114 * 0 => this fragment is followed by more fragment(s).
116 * The fragment/record machinery is not general; it is constructed to
117 * meet the needs of xdr and rpc based on tcp.
120 #define LAST_FRAG ((u_long)(1 << 31))
122 typedef struct rec_strm
{
129 caddr_t out_base
; /* output buffer (points to frag header) */
130 caddr_t out_finger
; /* next output position */
131 caddr_t out_boundry
; /* data cannot up to this address */
132 u_long
*frag_header
; /* beginning of curren fragment */
133 bool_t frag_sent
; /* true if buffer sent in middle of record */
138 u_long in_size
; /* fixed size of the input buffer */
140 caddr_t in_finger
; /* location of next byte to be had */
141 caddr_t in_boundry
; /* can read up to this location */
142 long fbtbc
; /* fragment bytes to be consumed */
150 * Create an xdr handle for xdrrec
151 * xdrrec_create fills in xdrs. Sendsize and recvsize are
152 * send and recv buffer sizes (0 => use default).
153 * tcp_handle is an opaque handle that is passed as the first parameter to
154 * the procedures readit and writeit. Readit and writeit are read and
155 * write respectively. They are like the system
156 * calls expect that they take an opaque handle rather than an fd.
159 xdrrec_create(xdrs
, sendsize
, recvsize
, tcp_handle
, readit
, writeit
)
161 register u_int sendsize
;
162 register u_int recvsize
;
164 int (*readit
)(); /* like read, but pass it a tcp_handle, not sock */
165 int (*writeit
)(); /* like write, but pass it a tcp_handle, not sock */
167 register RECSTREAM
*rstrm
=
168 (RECSTREAM
*)mem_alloc(sizeof(RECSTREAM
));
171 (void)fprintf(stderr
, "xdrrec_create: out of memory\n");
173 * This is bad. Should rework xdrrec_create to
174 * return a handle, and in this case return NULL
179 * adjust sizes and allocate buffer quad byte aligned
181 rstrm
->sendsize
= sendsize
= fix_buf_size(sendsize
);
182 rstrm
->recvsize
= recvsize
= fix_buf_size(recvsize
);
183 rstrm
->the_buffer
= mem_alloc(sendsize
+ recvsize
+ BYTES_PER_XDR_UNIT
);
184 if (rstrm
->the_buffer
== NULL
) {
185 (void)fprintf(stderr
, "xdrrec_create: out of memory\n");
188 for (rstrm
->out_base
= rstrm
->the_buffer
;
189 (u_int
)rstrm
->out_base
% BYTES_PER_XDR_UNIT
!= 0;
191 rstrm
->in_base
= rstrm
->out_base
+ sendsize
;
195 xdrs
->x_ops
= &xdrrec_ops
;
196 xdrs
->x_private
= (caddr_t
)rstrm
;
197 rstrm
->tcp_handle
= tcp_handle
;
198 rstrm
->readit
= readit
;
199 rstrm
->writeit
= writeit
;
200 rstrm
->out_finger
= rstrm
->out_boundry
= rstrm
->out_base
;
201 rstrm
->frag_header
= (u_long
*)rstrm
->out_base
;
202 rstrm
->out_finger
+= sizeof(u_long
);
203 rstrm
->out_boundry
+= sendsize
;
204 rstrm
->frag_sent
= FALSE
;
205 rstrm
->in_size
= recvsize
;
206 rstrm
->in_boundry
= rstrm
->in_base
;
207 rstrm
->in_finger
= (rstrm
->in_boundry
+= recvsize
);
209 rstrm
->last_frag
= TRUE
;
214 * The reoutines defined below are the xdr ops which will go into the
215 * xdr handle filled in by xdrrec_create.
219 xdrrec_getlong(xdrs
, lp
)
223 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
224 register long *buflp
= (long *)(rstrm
->in_finger
);
227 /* first try the inline, fast case */
228 if ((rstrm
->fbtbc
>= sizeof(long)) &&
229 (((int)rstrm
->in_boundry
- (int)buflp
) >= sizeof(long))) {
230 *lp
= (long)ntohl((u_long
)(*buflp
));
231 rstrm
->fbtbc
-= sizeof(long);
232 rstrm
->in_finger
+= sizeof(long);
234 if (! xdrrec_getbytes(xdrs
, (caddr_t
)&mylong
, sizeof(long)))
236 *lp
= (long)ntohl((u_long
)mylong
);
242 xdrrec_putlong(xdrs
, lp
)
246 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
247 register long *dest_lp
= ((long *)(rstrm
->out_finger
));
249 if ((rstrm
->out_finger
+= sizeof(long)) > rstrm
->out_boundry
) {
251 * this case should almost never happen so the code is
254 rstrm
->out_finger
-= sizeof(long);
255 rstrm
->frag_sent
= TRUE
;
256 if (! flush_out(rstrm
, FALSE
))
258 dest_lp
= ((long *)(rstrm
->out_finger
));
259 rstrm
->out_finger
+= sizeof(long);
261 *dest_lp
= (long)htonl((u_long
)(*lp
));
265 static bool_t
/* must manage buffers, fragments, and records */
266 xdrrec_getbytes(xdrs
, addr
, len
)
268 register caddr_t addr
;
271 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
272 register int current
;
275 current
= rstrm
->fbtbc
;
277 if (rstrm
->last_frag
)
279 if (! set_input_fragment(rstrm
))
283 current
= (len
< current
) ? len
: current
;
284 if (! get_input_bytes(rstrm
, addr
, current
))
287 rstrm
->fbtbc
-= current
;
294 xdrrec_putbytes(xdrs
, addr
, len
)
296 register caddr_t addr
;
299 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
300 register int current
;
303 current
= (u_int
)rstrm
->out_boundry
- (u_int
)rstrm
->out_finger
;
304 current
= (len
< current
) ? len
: current
;
305 bcopy(addr
, rstrm
->out_finger
, current
);
306 rstrm
->out_finger
+= current
;
309 if (rstrm
->out_finger
== rstrm
->out_boundry
) {
310 rstrm
->frag_sent
= TRUE
;
311 if (! flush_out(rstrm
, FALSE
))
322 register RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
325 pos
= lseek((int)rstrm
->tcp_handle
, 0, 1);
327 switch (xdrs
->x_op
) {
330 pos
+= rstrm
->out_finger
- rstrm
->out_base
;
334 pos
-= rstrm
->in_boundry
- rstrm
->in_finger
;
341 return ((u_int
) pos
);
345 xdrrec_setpos(xdrs
, pos
)
349 register RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
350 u_int currpos
= xdrrec_getpos(xdrs
);
351 int delta
= currpos
- pos
;
354 if ((int)currpos
!= -1)
355 switch (xdrs
->x_op
) {
358 newpos
= rstrm
->out_finger
- delta
;
359 if ((newpos
> (caddr_t
)(rstrm
->frag_header
)) &&
360 (newpos
< rstrm
->out_boundry
)) {
361 rstrm
->out_finger
= newpos
;
367 newpos
= rstrm
->in_finger
- delta
;
368 if ((delta
< (int)(rstrm
->fbtbc
)) &&
369 (newpos
<= rstrm
->in_boundry
) &&
370 (newpos
>= rstrm
->in_base
)) {
371 rstrm
->in_finger
= newpos
;
372 rstrm
->fbtbc
-= delta
;
381 xdrrec_inline(xdrs
, len
)
385 register RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
388 switch (xdrs
->x_op
) {
391 if ((rstrm
->out_finger
+ len
) <= rstrm
->out_boundry
) {
392 buf
= (long *) rstrm
->out_finger
;
393 rstrm
->out_finger
+= len
;
398 if ((len
<= rstrm
->fbtbc
) &&
399 ((rstrm
->in_finger
+ len
) <= rstrm
->in_boundry
)) {
400 buf
= (long *) rstrm
->in_finger
;
402 rstrm
->in_finger
+= len
;
413 register RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
415 mem_free(rstrm
->the_buffer
,
416 rstrm
->sendsize
+ rstrm
->recvsize
+ BYTES_PER_XDR_UNIT
);
417 mem_free((caddr_t
)rstrm
, sizeof(RECSTREAM
));
422 * Exported routines to manage xdr records
426 * Before reading (deserializing from the stream, one should always call
427 * this procedure to guarantee proper record alignment.
430 xdrrec_skiprecord(xdrs
)
433 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
435 while (rstrm
->fbtbc
> 0 || (! rstrm
->last_frag
)) {
436 if (! skip_input_bytes(rstrm
, rstrm
->fbtbc
))
439 if ((! rstrm
->last_frag
) && (! set_input_fragment(rstrm
)))
442 rstrm
->last_frag
= FALSE
;
447 * Look ahead fuction.
448 * Returns TRUE iff there is no more input in the buffer
449 * after consuming the rest of the current record.
455 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
457 while (rstrm
->fbtbc
> 0 || (! rstrm
->last_frag
)) {
458 if (! skip_input_bytes(rstrm
, rstrm
->fbtbc
))
461 if ((! rstrm
->last_frag
) && (! set_input_fragment(rstrm
)))
464 if (rstrm
->in_finger
== rstrm
->in_boundry
)
470 * The client must tell the package when an end-of-record has occurred.
471 * The second paraemters tells whether the record should be flushed to the
472 * (output) tcp stream. (This let's the package support batched or
473 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
476 xdrrec_endofrecord(xdrs
, sendnow
)
480 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
481 register u_long len
; /* fragment length */
483 if (sendnow
|| rstrm
->frag_sent
||
484 ((u_long
)rstrm
->out_finger
+ sizeof(u_long
) >=
485 (u_long
)rstrm
->out_boundry
)) {
486 rstrm
->frag_sent
= FALSE
;
487 return (flush_out(rstrm
, TRUE
));
489 len
= (u_long
)(rstrm
->out_finger
) - (u_long
)(rstrm
->frag_header
) -
491 *(rstrm
->frag_header
) = htonl((u_long
)len
| LAST_FRAG
);
492 rstrm
->frag_header
= (u_long
*)rstrm
->out_finger
;
493 rstrm
->out_finger
+= sizeof(u_long
);
499 * Internal useful routines
502 flush_out(rstrm
, eor
)
503 register RECSTREAM
*rstrm
;
506 register u_long eormask
= (eor
== TRUE
) ? LAST_FRAG
: 0;
507 register u_long len
= (u_long
)(rstrm
->out_finger
) -
508 (u_long
)(rstrm
->frag_header
) - sizeof(u_long
);
510 *(rstrm
->frag_header
) = htonl(len
| eormask
);
511 len
= (u_long
)(rstrm
->out_finger
) - (u_long
)(rstrm
->out_base
);
512 if ((*(rstrm
->writeit
))(rstrm
->tcp_handle
, rstrm
->out_base
, (int)len
)
515 rstrm
->frag_header
= (u_long
*)rstrm
->out_base
;
516 rstrm
->out_finger
= (caddr_t
)rstrm
->out_base
+ sizeof(u_long
);
520 static bool_t
/* knows nothing about records! Only about input buffers */
521 fill_input_buf(rstrm
)
522 register RECSTREAM
*rstrm
;
524 register caddr_t where
;
528 where
= rstrm
->in_base
;
529 i
= (u_int
)rstrm
->in_boundry
% BYTES_PER_XDR_UNIT
;
531 len
= rstrm
->in_size
- i
;
532 if ((len
= (*(rstrm
->readit
))(rstrm
->tcp_handle
, where
, len
)) == -1)
534 rstrm
->in_finger
= where
;
536 rstrm
->in_boundry
= where
;
540 static bool_t
/* knows nothing about records! Only about input buffers */
541 get_input_bytes(rstrm
, addr
, len
)
542 register RECSTREAM
*rstrm
;
543 register caddr_t addr
;
546 register int current
;
549 current
= (int)rstrm
->in_boundry
- (int)rstrm
->in_finger
;
551 if (! fill_input_buf(rstrm
))
555 current
= (len
< current
) ? len
: current
;
556 bcopy(rstrm
->in_finger
, addr
, current
);
557 rstrm
->in_finger
+= current
;
564 static bool_t
/* next two bytes of the input stream are treated as a header */
565 set_input_fragment(rstrm
)
566 register RECSTREAM
*rstrm
;
570 if (! get_input_bytes(rstrm
, (caddr_t
)&header
, sizeof(header
)))
572 header
= (long)ntohl(header
);
573 rstrm
->last_frag
= ((header
& LAST_FRAG
) == 0) ? FALSE
: TRUE
;
574 rstrm
->fbtbc
= header
& (~LAST_FRAG
);
578 static bool_t
/* consumes input bytes; knows nothing about records! */
579 skip_input_bytes(rstrm
, cnt
)
580 register RECSTREAM
*rstrm
;
583 register int current
;
586 current
= (int)rstrm
->in_boundry
- (int)rstrm
->in_finger
;
588 if (! fill_input_buf(rstrm
))
592 current
= (cnt
< current
) ? cnt
: current
;
593 rstrm
->in_finger
+= current
;