Deleted Added
full compact
1/* $NetBSD: xdr_rec.c,v 1.18 2000/07/06 03:10:35 christos Exp $ */
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or

--- 21 unchanged lines hidden (view full) ---

30 */
31
32#include <sys/cdefs.h>
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *sccsid = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
35static char *sccsid = "@(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC";
36#endif
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libc/xdr/xdr_rec.c 95658 2002-04-28 15:18:50Z des $");
39
40/*
41 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
42 * layer above tcp (for rpc's use).
43 *
44 * Copyright (C) 1984, Sun Microsystems, Inc.
45 *
46 * These routines interface XDRSTREAMS to a tcp/ip connection.

--- 14 unchanged lines hidden (view full) ---

61
62#include <err.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66
67#include <rpc/types.h>
68#include <rpc/xdr.h>
69#include "un-namespace.h"
70
71static bool_t xdrrec_getlong(XDR *, long *);
72static bool_t xdrrec_putlong(XDR *, const long *);
73static bool_t xdrrec_getbytes(XDR *, char *, u_int);
74
75static bool_t xdrrec_putbytes(XDR *, const char *, u_int);
76static u_int xdrrec_getpos(XDR *);

--- 9 unchanged lines hidden (view full) ---

86 xdrrec_getpos,
87 xdrrec_setpos,
88 xdrrec_inline,
89 xdrrec_destroy
90};
91
92/*
93 * A record is composed of one or more record fragments.
94 * A record fragment is a two-byte header followed by zero to
95 * 2**32-1 bytes. The header is treated as a long unsigned and is
96 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
97 * are a byte count of the fragment. The highest order bit is a boolean:
98 * 1 => this fragment is the last fragment of the record,
99 * 0 => this fragment is followed by more fragment(s).
100 *
101 * The fragment/record machinery is not general; it is constructed to
102 * meet the needs of xdr and rpc based on tcp.
103 */
104
105#define LAST_FRAG ((u_int32_t)(1 << 31))
106
107typedef struct rec_strm {
108 char *tcp_handle;
109 char *the_buffer;
110 /*
111 * out-goung bits
112 */
113 int (*writeit)(void *, void *, int);
114 char *out_base; /* output buffer (points to frag header) */
115 char *out_finger; /* next output position */
116 char *out_boundry; /* data cannot up to this address */
117 u_int32_t *frag_header; /* beginning of curren fragment */

--- 5 unchanged lines hidden (view full) ---

123 u_long in_size; /* fixed size of the input buffer */
124 char *in_base;
125 char *in_finger; /* location of next byte to be had */
126 char *in_boundry; /* can read up to this location */
127 long fbtbc; /* fragment bytes to be consumed */
128 bool_t last_frag;
129 u_int sendsize;
130 u_int recvsize;
131} RECSTREAM;
132
133static u_int fix_buf_size(u_int);
134static bool_t flush_out(RECSTREAM *, bool_t);
135static bool_t fill_input_buf(RECSTREAM *);
136static bool_t get_input_bytes(RECSTREAM *, char *, int);
137static bool_t set_input_fragment(RECSTREAM *);
138static bool_t skip_input_bytes(RECSTREAM *, long);
139
140
141/*
142 * Create an xdr handle for xdrrec
143 * xdrrec_create fills in xdrs. Sendsize and recvsize are
144 * send and recv buffer sizes (0 => use default).
145 * tcp_handle is an opaque handle that is passed as the first parameter to
146 * the procedures readit and writeit. Readit and writeit are read and

--- 16 unchanged lines hidden (view full) ---

163 if (rstrm == NULL) {
164 warnx("xdrrec_create: out of memory");
165 /*
166 * This is bad. Should rework xdrrec_create to
167 * return a handle, and in this case return NULL
168 */
169 return;
170 }
171 /*
172 * adjust sizes and allocate buffer quad byte aligned
173 */
174 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
175 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
176 rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
177 if (rstrm->the_buffer == NULL) {
178 warnx("xdrrec_create: out of memory");
179 return;
180 }
181 for (rstrm->out_base = rstrm->the_buffer;
182 (u_long)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
183 rstrm->out_base++);
184 rstrm->in_base = rstrm->out_base + sendsize;
185 /*
186 * now the rest ...
187 */
188 xdrs->x_ops = &xdrrec_ops;
189 xdrs->x_private = rstrm;
190 rstrm->tcp_handle = tcp_handle;
191 rstrm->readit = readit;
192 rstrm->writeit = writeit;
193 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
194 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
195 rstrm->out_finger += sizeof(u_int32_t);
196 rstrm->out_boundry += sendsize;
197 rstrm->frag_sent = FALSE;
198 rstrm->in_size = recvsize;
199 rstrm->in_boundry = rstrm->in_base;
200 rstrm->in_finger = (rstrm->in_boundry += recvsize);
201 rstrm->fbtbc = 0;
202 rstrm->last_frag = TRUE;
203}
204
205
206/*
207 * The reoutines defined below are the xdr ops which will go into the
208 * xdr handle filled in by xdrrec_create.
209 */
210

--- 197 unchanged lines hidden (view full) ---

408}
409
410static void
411xdrrec_destroy(xdrs)
412 XDR *xdrs;
413{
414 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
415
416 mem_free(rstrm->the_buffer,
417 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
418 mem_free(rstrm, sizeof(RECSTREAM));
419}
420
421
422/*
423 * Exported routines to manage xdr records
424 */
425
426/*
427 * Before reading (deserializing from the stream, one should always call
428 * this procedure to guarantee proper record alignment.
429 */
430bool_t
431xdrrec_skiprecord(xdrs)
432 XDR *xdrs;
433{
434 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
435
436 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
437 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
438 return (FALSE);
439 rstrm->fbtbc = 0;
440 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
441 return (FALSE);
442 }
443 rstrm->last_frag = FALSE;

--- 5 unchanged lines hidden (view full) ---

449 * Returns TRUE iff there is no more input in the buffer
450 * after consuming the rest of the current record.
451 */
452bool_t
453xdrrec_eof(xdrs)
454 XDR *xdrs;
455{
456 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
457
458 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
459 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
460 return (TRUE);
461 rstrm->fbtbc = 0;
462 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
463 return (TRUE);
464 }
465 if (rstrm->in_finger == rstrm->in_boundry)

--- 24 unchanged lines hidden (view full) ---

490 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
491 sizeof(u_int32_t);
492 *(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
493 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
494 rstrm->out_finger += sizeof(u_int32_t);
495 return (TRUE);
496}
497
498
499/*
500 * Internal useful routines
501 */
502static bool_t
503flush_out(rstrm, eor)
504 RECSTREAM *rstrm;
505 bool_t eor;
506{

--- 15 unchanged lines hidden (view full) ---

522static bool_t /* knows nothing about records! Only about input buffers */
523fill_input_buf(rstrm)
524 RECSTREAM *rstrm;
525{
526 char *where;
527 u_int32_t i;
528 int len;
529
530 where = rstrm->in_base;
531 i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
532 where += i;
533 len = (u_int32_t)(rstrm->in_size - i);
534 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
535 return (FALSE);
536 rstrm->in_finger = where;
537 where += len;

--- 76 unchanged lines hidden (view full) ---

614fix_buf_size(s)
615 u_int s;
616{
617
618 if (s < 100)
619 s = 4000;
620 return (RNDUP(s));
621}