2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
49 * Sun Microsystems, Inc.
51 * Mountain View, California 94043
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";*/
55 /*static char *sccsid = "from: @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC";*/
56 static char *rcsid
= "$Id: xdr_rec.c,v 1.4 2003/06/23 17:24:59 majka Exp $";
60 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
61 * layer above tcp (for rpc's use).
63 * Copyright (C) 1984, Sun Microsystems, Inc.
65 * These routines interface XDRSTREAMS to a tcp/ip connection.
66 * There is a record marking layer between the xdr stream
67 * and the tcp transport level. A record is composed on one or more
68 * record fragments. A record fragment is a thirty-two bit header followed
69 * by n bytes of data, where n is contained in the header. The header
70 * is represented as a htonl(u_long). Thegh order bit encodes
71 * whether or not the fragment is the last fragment of the record
72 * (1 => fragment is last, 0 => more fragments to follow.
73 * The other 31 bits encode the byte length of the fragment.
80 #include <rpc/types.h>
82 #include <netinet/in.h>
84 static u_int
fix_buf_size();
85 static bool_t
flush_out();
86 static bool_t
get_input_bytes();
87 static bool_t
set_input_fragment();
88 static bool_t
skip_input_bytes();
90 static bool_t
xdrrec_getlong();
91 static bool_t
xdrrec_putlong();
92 static bool_t
xdrrec_getbytes();
93 static bool_t
xdrrec_putbytes();
94 static u_int
xdrrec_getpos();
95 static bool_t
xdrrec_setpos();
96 static long * xdrrec_inline();
97 static void xdrrec_destroy();
99 static struct xdr_ops xdrrec_ops
= {
111 * A record is composed of one or more record fragments.
112 * A record fragment is a two-byte header followed by zero to
113 * 2**32-1 bytes. The header is treated as a long unsigned and is
114 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
115 * are a byte count of the fragment. The highest order bit is a boolean:
116 * 1 => this fragment is the last fragment of the record,
117 * 0 => this fragment is followed by more fragment(s).
119 * The fragment/record machinery is not general; it is constructed to
120 * meet the needs of xdr and rpc based on tcp.
123 #define LAST_FRAG ((u_long)(1 << 31))
125 typedef struct rec_strm
{
132 caddr_t out_base
; /* output buffer (points to frag header) */
133 caddr_t out_finger
; /* next output position */
134 caddr_t out_boundry
; /* data cannot up to this address */
135 u_long
*frag_header
; /* beginning of curren fragment */
136 bool_t frag_sent
; /* true if buffer sent in middle of record */
141 u_long in_size
; /* fixed size of the input buffer */
143 caddr_t in_finger
; /* location of next byte to be had */
144 caddr_t in_boundry
; /* can read up to this location */
145 long fbtbc
; /* fragment bytes to be consumed */
153 * Create an xdr handle for xdrrec
154 * xdrrec_create fills in xdrs. Sendsize and recvsize are
155 * send and recv buffer sizes (0 => use default).
156 * tcp_handle is an opaque handle that is passed as the first parameter to
157 * the procedures readit and writeit. Readit and writeit are read and
158 * write respectively. They are like the system
159 * calls expect that they take an opaque handle rather than an fd.
162 xdrrec_create(xdrs
, sendsize
, recvsize
, tcp_handle
, readit
, writeit
)
164 register u_int sendsize
;
165 register u_int recvsize
;
167 int (*readit
)(); /* like read, but pass it a tcp_handle, not sock */
168 int (*writeit
)(); /* like write, but pass it a tcp_handle, not sock */
170 register RECSTREAM
*rstrm
=
171 (RECSTREAM
*)mem_alloc(sizeof(RECSTREAM
));
174 (void)fprintf(stderr
, "xdrrec_create: out of memory\n");
176 * This is bad. Should rework xdrrec_create to
177 * return a handle, and in this case return NULL
182 * adjust sizes and allocate buffer quad byte aligned
184 rstrm
->sendsize
= sendsize
= fix_buf_size(sendsize
);
185 rstrm
->recvsize
= recvsize
= fix_buf_size(recvsize
);
186 rstrm
->the_buffer
= mem_alloc(sendsize
+ recvsize
+ BYTES_PER_XDR_UNIT
);
187 if (rstrm
->the_buffer
== NULL
) {
188 (void)fprintf(stderr
, "xdrrec_create: out of memory\n");
191 for (rstrm
->out_base
= rstrm
->the_buffer
;
192 (u_int
)rstrm
->out_base
% BYTES_PER_XDR_UNIT
!= 0;
194 rstrm
->in_base
= rstrm
->out_base
+ sendsize
;
198 xdrs
->x_ops
= &xdrrec_ops
;
199 xdrs
->x_private
= (caddr_t
)rstrm
;
200 rstrm
->tcp_handle
= tcp_handle
;
201 rstrm
->readit
= readit
;
202 rstrm
->writeit
= writeit
;
203 rstrm
->out_finger
= rstrm
->out_boundry
= rstrm
->out_base
;
204 rstrm
->frag_header
= (u_long
*)rstrm
->out_base
;
205 rstrm
->out_finger
+= sizeof(u_long
);
206 rstrm
->out_boundry
+= sendsize
;
207 rstrm
->frag_sent
= FALSE
;
208 rstrm
->in_size
= recvsize
;
209 rstrm
->in_boundry
= rstrm
->in_base
;
210 rstrm
->in_finger
= (rstrm
->in_boundry
+= recvsize
);
212 rstrm
->last_frag
= TRUE
;
217 * The reoutines defined below are the xdr ops which will go into the
218 * xdr handle filled in by xdrrec_create.
222 xdrrec_getlong(xdrs
, lp
)
226 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
227 register long *buflp
= (long *)(rstrm
->in_finger
);
230 /* first try the inline, fast case */
231 if ((rstrm
->fbtbc
>= sizeof(long)) &&
232 (((int)rstrm
->in_boundry
- (int)buflp
) >= sizeof(long))) {
233 *lp
= (long)ntohl((u_long
)(*buflp
));
234 rstrm
->fbtbc
-= sizeof(long);
235 rstrm
->in_finger
+= sizeof(long);
237 if (! xdrrec_getbytes(xdrs
, (caddr_t
)&mylong
, sizeof(long)))
239 *lp
= (long)ntohl((u_long
)mylong
);
245 xdrrec_putlong(xdrs
, lp
)
249 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
250 register long *dest_lp
= ((long *)(rstrm
->out_finger
));
252 if ((rstrm
->out_finger
+= sizeof(long)) > rstrm
->out_boundry
) {
254 * this case should almost never happen so the code is
257 rstrm
->out_finger
-= sizeof(long);
258 rstrm
->frag_sent
= TRUE
;
259 if (! flush_out(rstrm
, FALSE
))
261 dest_lp
= ((long *)(rstrm
->out_finger
));
262 rstrm
->out_finger
+= sizeof(long);
264 *dest_lp
= (long)htonl((u_long
)(*lp
));
268 static bool_t
/* must manage buffers, fragments, and records */
269 xdrrec_getbytes(xdrs
, addr
, len
)
271 register caddr_t addr
;
274 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
275 register int current
;
278 current
= rstrm
->fbtbc
;
280 if (rstrm
->last_frag
)
282 if (! set_input_fragment(rstrm
))
286 current
= (len
< current
) ? len
: current
;
287 if (! get_input_bytes(rstrm
, addr
, current
))
290 rstrm
->fbtbc
-= current
;
297 xdrrec_putbytes(xdrs
, addr
, len
)
299 register caddr_t addr
;
302 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
303 register int current
;
306 current
= (u_int
)rstrm
->out_boundry
- (u_int
)rstrm
->out_finger
;
307 current
= (len
< current
) ? len
: current
;
308 bcopy(addr
, rstrm
->out_finger
, current
);
309 rstrm
->out_finger
+= current
;
312 if (rstrm
->out_finger
== rstrm
->out_boundry
) {
313 rstrm
->frag_sent
= TRUE
;
314 if (! flush_out(rstrm
, FALSE
))
325 register RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
328 pos
= lseek((int)rstrm
->tcp_handle
, 0, 1);
330 switch (xdrs
->x_op
) {
333 pos
+= rstrm
->out_finger
- rstrm
->out_base
;
337 pos
-= rstrm
->in_boundry
- rstrm
->in_finger
;
344 return ((u_int
) pos
);
348 xdrrec_setpos(xdrs
, pos
)
352 register RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
353 u_int currpos
= xdrrec_getpos(xdrs
);
354 int delta
= currpos
- pos
;
357 if ((int)currpos
!= -1)
358 switch (xdrs
->x_op
) {
361 newpos
= rstrm
->out_finger
- delta
;
362 if ((newpos
> (caddr_t
)(rstrm
->frag_header
)) &&
363 (newpos
< rstrm
->out_boundry
)) {
364 rstrm
->out_finger
= newpos
;
370 newpos
= rstrm
->in_finger
- delta
;
371 if ((delta
< (int)(rstrm
->fbtbc
)) &&
372 (newpos
<= rstrm
->in_boundry
) &&
373 (newpos
>= rstrm
->in_base
)) {
374 rstrm
->in_finger
= newpos
;
375 rstrm
->fbtbc
-= delta
;
385 xdrrec_inline(xdrs
, len
)
389 register RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
392 switch (xdrs
->x_op
) {
395 if ((rstrm
->out_finger
+ len
) <= rstrm
->out_boundry
) {
396 buf
= (long *) rstrm
->out_finger
;
397 rstrm
->out_finger
+= len
;
402 if ((len
<= rstrm
->fbtbc
) &&
403 ((rstrm
->in_finger
+ len
) <= rstrm
->in_boundry
)) {
404 buf
= (long *) rstrm
->in_finger
;
406 rstrm
->in_finger
+= len
;
418 register RECSTREAM
*rstrm
= (RECSTREAM
*)xdrs
->x_private
;
420 mem_free(rstrm
->the_buffer
,
421 rstrm
->sendsize
+ rstrm
->recvsize
+ BYTES_PER_XDR_UNIT
);
422 mem_free((caddr_t
)rstrm
, sizeof(RECSTREAM
));
427 * Exported routines to manage xdr records
431 * Before reading (deserializing from the stream, one should always call
432 * this procedure to guarantee proper record alignment.
435 xdrrec_skiprecord(xdrs
)
438 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
440 while (rstrm
->fbtbc
> 0 || (! rstrm
->last_frag
)) {
441 if (! skip_input_bytes(rstrm
, rstrm
->fbtbc
))
444 if ((! rstrm
->last_frag
) && (! set_input_fragment(rstrm
)))
447 rstrm
->last_frag
= FALSE
;
452 * Look ahead fuction.
453 * Returns TRUE iff there is no more input in the buffer
454 * after consuming the rest of the current record.
460 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
462 while (rstrm
->fbtbc
> 0 || (! rstrm
->last_frag
)) {
463 if (! skip_input_bytes(rstrm
, rstrm
->fbtbc
))
466 if ((! rstrm
->last_frag
) && (! set_input_fragment(rstrm
)))
469 if (rstrm
->in_finger
== rstrm
->in_boundry
)
475 * The client must tell the package when an end-of-record has occurred.
476 * The second paraemters tells whether the record should be flushed to the
477 * (output) tcp stream. (This let's the package support batched or
478 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
481 xdrrec_endofrecord(xdrs
, sendnow
)
485 register RECSTREAM
*rstrm
= (RECSTREAM
*)(xdrs
->x_private
);
486 register u_long len
; /* fragment length */
488 if (sendnow
|| rstrm
->frag_sent
||
489 ((u_long
)rstrm
->out_finger
+ sizeof(u_long
) >=
490 (u_long
)rstrm
->out_boundry
)) {
491 rstrm
->frag_sent
= FALSE
;
492 return (flush_out(rstrm
, TRUE
));
494 len
= (u_long
)(rstrm
->out_finger
) - (u_long
)(rstrm
->frag_header
) -
496 *(rstrm
->frag_header
) = htonl((u_long
)len
| LAST_FRAG
);
497 rstrm
->frag_header
= (u_long
*)rstrm
->out_finger
;
498 rstrm
->out_finger
+= sizeof(u_long
);
504 * Internal useful routines
507 flush_out(rstrm
, eor
)
508 register RECSTREAM
*rstrm
;
511 register u_long eormask
= (eor
== TRUE
) ? LAST_FRAG
: 0;
512 register u_long len
= (u_long
)(rstrm
->out_finger
) -
513 (u_long
)(rstrm
->frag_header
) - sizeof(u_long
);
515 *(rstrm
->frag_header
) = htonl(len
| eormask
);
516 len
= (u_long
)(rstrm
->out_finger
) - (u_long
)(rstrm
->out_base
);
517 if ((*(rstrm
->writeit
))(rstrm
->tcp_handle
, rstrm
->out_base
, (int)len
)
520 rstrm
->frag_header
= (u_long
*)rstrm
->out_base
;
521 rstrm
->out_finger
= (caddr_t
)rstrm
->out_base
+ sizeof(u_long
);
525 static bool_t
/* knows nothing about records! Only about input buffers */
526 fill_input_buf(rstrm
)
527 register RECSTREAM
*rstrm
;
529 register caddr_t where
;
533 where
= rstrm
->in_base
;
534 i
= (u_int
)rstrm
->in_boundry
% BYTES_PER_XDR_UNIT
;
536 len
= rstrm
->in_size
- i
;
537 if ((len
= (*(rstrm
->readit
))(rstrm
->tcp_handle
, where
, len
)) == -1)
539 rstrm
->in_finger
= where
;
541 rstrm
->in_boundry
= where
;
545 static bool_t
/* knows nothing about records! Only about input buffers */
546 get_input_bytes(rstrm
, addr
, len
)
547 register RECSTREAM
*rstrm
;
548 register caddr_t addr
;
551 register int current
;
554 current
= (int)rstrm
->in_boundry
- (int)rstrm
->in_finger
;
556 if (! fill_input_buf(rstrm
))
560 current
= (len
< current
) ? len
: current
;
561 bcopy(rstrm
->in_finger
, addr
, current
);
562 rstrm
->in_finger
+= current
;
569 static bool_t
/* next two bytes of the input stream are treated as a header */
570 set_input_fragment(rstrm
)
571 register RECSTREAM
*rstrm
;
575 if (! get_input_bytes(rstrm
, (caddr_t
)&header
, sizeof(header
)))
577 header
= (long)ntohl(header
);
578 rstrm
->last_frag
= ((header
& LAST_FRAG
) == 0) ? FALSE
: TRUE
;
579 rstrm
->fbtbc
= header
& (~LAST_FRAG
);
583 static bool_t
/* consumes input bytes; knows nothing about records! */
584 skip_input_bytes(rstrm
, cnt
)
585 register RECSTREAM
*rstrm
;
588 register int current
;
591 current
= (int)rstrm
->in_boundry
- (int)rstrm
->in_finger
;
593 if (! fill_input_buf(rstrm
))
597 current
= (cnt
< current
) ? cnt
: current
;
598 rstrm
->in_finger
+= current
;