1 /*********************************************************************
2 * RPC for the Windows NT Operating System
3 * 1993 by Martin F. Gergeleit
4 * Users may use, copy or modify Sun RPC for the Windows NT Operating
5 * System according to the Sun copyright below.
7 * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO
8 * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE
9 * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10 *********************************************************************/
12 /* @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC */
14 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15 * unrestricted use provided that this legend is included on all tape
16 * media and as a part of the software program in whole or part. Users
17 * may copy or modify Sun RPC without charge, but are not authorized
18 * to license or distribute it to anyone else except as part of a product or
19 * program developed by the user.
21 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
25 * Sun RPC is provided with no support and without any obligation on the
26 * part of Sun Microsystems, Inc. to assist in its use, correction,
27 * modification or enhancement.
29 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31 * OR ANY PART THEREOF.
33 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34 * or profits or other special, indirect and consequential damages, even if
35 * Sun has been advised of the possibility of such damages.
37 * Sun Microsystems, Inc.
39 * Mountain View, California 94043
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
46 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
47 * layer above tcp (for rpc's use).
49 * Copyright (C) 1984, Sun Microsystems, Inc.
51 * These routines interface XDRSTREAMS to a tcp/ip connection.
52 * There is a record marking layer between the xdr stream
53 * and the tcp transport level. A record is composed on one or more
54 * record fragments. A record fragment is a thirty-two bit header followed
55 * by n bytes of data, where n is contained in the header. The header
56 * is represented as a htonl(u_long). Thegh order bit encodes
57 * whether or not the fragment is the last fragment of the record
58 * (1 => fragment is last, 0 => more fragments to follow.
59 * The other 31 bits encode the byte length of the fragment.
68 #include <rpc/types.h>
70 #include <netinet/in.h>
72 extern off_t lseek(int, off_t, int);
75 static u_int fix_buf_size();
77 static bool_t xdrrec_getlong();
78 static bool_t xdrrec_putlong();
79 static bool_t xdrrec_getbytes();
80 static bool_t xdrrec_putbytes();
81 static u_int xdrrec_getpos();
82 static bool_t xdrrec_setpos();
83 static long * xdrrec_inline();
84 static void xdrrec_destroy();
86 static bool_t flush_out();
87 static bool_t get_input_bytes();
88 static bool_t set_input_fragment();
89 static bool_t skip_input_bytes();
91 static struct xdr_ops xdrrec_ops = {
103 * A record is composed of one or more record fragments.
104 * A record fragment is a two-byte header followed by zero to
105 * 2**32-1 bytes. The header is treated as a long unsigned and is
106 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
107 * are a byte count of the fragment. The highest order bit is a boolean:
108 * 1 => this fragment is the last fragment of the record,
109 * 0 => this fragment is followed by more fragment(s).
111 * The fragment/record machinery is not general; it is constructed to
112 * meet the needs of xdr and rpc based on tcp.
115 #define LAST_FRAG ((u_long)(1 << 31))
117 typedef struct rec_strm {
124 caddr_t out_base; /* output buffer (points to frag header) */
125 caddr_t out_finger; /* next output position */
126 caddr_t out_boundry; /* data cannot up to this address */
127 u_long *frag_header; /* beginning of curren fragment */
128 bool_t frag_sent; /* true if buffer sent in middle of record */
133 u_long in_size; /* fixed size of the input buffer */
135 caddr_t in_finger; /* location of next byte to be had */
136 caddr_t in_boundry; /* can read up to this location */
137 long fbtbc; /* fragment bytes to be consumed */
145 * Create an xdr handle for xdrrec
146 * xdrrec_create fills in xdrs. Sendsize and recvsize are
147 * send and recv buffer sizes (0 => use default).
148 * tcp_handle is an opaque handle that is passed as the first parameter to
149 * the procedures readit and writeit. Readit and writeit are read and
150 * write respectively. They are like the system
151 * calls expect that they take an opaque handle rather than an fd.
154 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
156 register u_int sendsize;
157 register u_int recvsize;
159 int (*readit)(); /* like read, but pass it a tcp_handle, not sock */
160 int (*writeit)(); /* like write, but pass it a tcp_handle, not sock */
162 register RECSTREAM *rstrm =
163 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
167 nt_rpc_report("xdrrec_create: out of memory\n");
169 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
172 * This is bad. Should rework xdrrec_create to
173 * return a handle, and in this case return NULL
178 * adjust sizes and allocate buffer quad byte aligned
180 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
181 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
182 rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
183 if (rstrm->the_buffer == NULL) {
185 nt_rpc_report("xdrrec_create: out of memory\n");
187 (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, (long) 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;
384 xdrrec_inline(xdrs, len)
388 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
391 switch (xdrs->x_op) {
394 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
395 buf = (long *) rstrm->out_finger;
396 rstrm->out_finger += len;
401 if ((len <= rstrm->fbtbc) &&
402 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
403 buf = (long *) rstrm->in_finger;
405 rstrm->in_finger += len;
416 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
418 mem_free(rstrm->the_buffer,
419 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
420 mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
425 * Exported routines to manage xdr records
429 * Before reading (deserializing from the stream, one should always call
430 * this procedure to guarantee proper record alignment.
433 xdrrec_skiprecord(xdrs)
436 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
438 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
439 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
442 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
445 rstrm->last_frag = FALSE;
450 * Look ahead fuction.
451 * Returns TRUE iff there is no more input in the buffer
452 * after consuming the rest of the current record.
458 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
460 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
461 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
464 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
467 if (rstrm->in_finger == rstrm->in_boundry)
473 * The client must tell the package when an end-of-record has occurred.
474 * The second paraemters tells whether the record should be flushed to the
475 * (output) tcp stream. (This let's the package support batched or
476 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
479 xdrrec_endofrecord(xdrs, sendnow)
483 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
484 register u_long len; /* fragment length */
486 if (sendnow || rstrm->frag_sent ||
487 ((u_long)rstrm->out_finger + sizeof(u_long) >=
488 (u_long)rstrm->out_boundry)) {
489 rstrm->frag_sent = FALSE;
490 return (flush_out(rstrm, TRUE));
492 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
494 *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
495 rstrm->frag_header = (u_long *)rstrm->out_finger;
496 rstrm->out_finger += sizeof(u_long);
502 * Internal useful routines
505 flush_out(rstrm, eor)
506 register RECSTREAM *rstrm;
509 register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
510 register u_long len = (u_long)(rstrm->out_finger) -
511 (u_long)(rstrm->frag_header) - sizeof(u_long);
513 *(rstrm->frag_header) = htonl(len | eormask);
514 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
515 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
518 rstrm->frag_header = (u_long *)rstrm->out_base;
519 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_long);
523 static bool_t /* knows nothing about records! Only about input buffers */
524 fill_input_buf(rstrm)
525 register RECSTREAM *rstrm;
527 register caddr_t where;
531 where = rstrm->in_base;
532 i = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
534 len = rstrm->in_size - i;
535 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
537 rstrm->in_finger = where;
539 rstrm->in_boundry = where;
543 static bool_t /* knows nothing about records! Only about input buffers */
544 get_input_bytes(rstrm, addr, len)
545 register RECSTREAM *rstrm;
546 register caddr_t addr;
549 register int current;
552 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
554 if (! fill_input_buf(rstrm))
558 current = (len < current) ? len : current;
559 bcopy(rstrm->in_finger, addr, current);
560 rstrm->in_finger += current;
567 static bool_t /* next two bytes of the input stream are treated as a header */
568 set_input_fragment(rstrm)
569 register RECSTREAM *rstrm;
573 if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
575 header = (long)ntohl(header);
576 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
577 rstrm->fbtbc = header & (~LAST_FRAG);
581 static bool_t /* consumes input bytes; knows nothing about records! */
582 skip_input_bytes(rstrm, cnt)
583 register RECSTREAM *rstrm;
586 register int current;
589 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
591 if (! fill_input_buf(rstrm))
595 current = (cnt < current) ? cnt : current;
596 rstrm->in_finger += current;