xdr_rec.c revision 1.7 1 1.7 christos /* $NetBSD: xdr_rec.c,v 1.7 1997/07/13 20:13:31 christos Exp $ */
2 1.5 cgd
3 1.1 cgd /*
4 1.1 cgd * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.1 cgd * unrestricted use provided that this legend is included on all tape
6 1.1 cgd * media and as a part of the software program in whole or part. Users
7 1.1 cgd * may copy or modify Sun RPC without charge, but are not authorized
8 1.1 cgd * to license or distribute it to anyone else except as part of a product or
9 1.1 cgd * program developed by the user.
10 1.1 cgd *
11 1.1 cgd * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 cgd * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 cgd * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.1 cgd *
15 1.1 cgd * Sun RPC is provided with no support and without any obligation on the
16 1.1 cgd * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 cgd * modification or enhancement.
18 1.1 cgd *
19 1.1 cgd * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 cgd * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 cgd * OR ANY PART THEREOF.
22 1.1 cgd *
23 1.1 cgd * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 cgd * or profits or other special, indirect and consequential damages, even if
25 1.1 cgd * Sun has been advised of the possibility of such damages.
26 1.1 cgd *
27 1.1 cgd * Sun Microsystems, Inc.
28 1.1 cgd * 2550 Garcia Avenue
29 1.1 cgd * Mountain View, California 94043
30 1.1 cgd */
31 1.7 christos
32 1.7 christos #include <sys/cdefs.h>
33 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
34 1.7 christos #if 0
35 1.7 christos static char *sccsid = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
36 1.7 christos static char *sccsid = "@(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC";
37 1.7 christos #else
38 1.7 christos __RCSID("$NetBSD: xdr_rec.c,v 1.7 1997/07/13 20:13:31 christos Exp $");
39 1.7 christos #endif
40 1.1 cgd #endif
41 1.1 cgd
42 1.1 cgd /*
43 1.1 cgd * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
44 1.1 cgd * layer above tcp (for rpc's use).
45 1.1 cgd *
46 1.1 cgd * Copyright (C) 1984, Sun Microsystems, Inc.
47 1.1 cgd *
48 1.1 cgd * These routines interface XDRSTREAMS to a tcp/ip connection.
49 1.1 cgd * There is a record marking layer between the xdr stream
50 1.1 cgd * and the tcp transport level. A record is composed on one or more
51 1.1 cgd * record fragments. A record fragment is a thirty-two bit header followed
52 1.1 cgd * by n bytes of data, where n is contained in the header. The header
53 1.1 cgd * is represented as a htonl(u_long). Thegh order bit encodes
54 1.1 cgd * whether or not the fragment is the last fragment of the record
55 1.1 cgd * (1 => fragment is last, 0 => more fragments to follow.
56 1.1 cgd * The other 31 bits encode the byte length of the fragment.
57 1.1 cgd */
58 1.1 cgd
59 1.1 cgd #include <stdio.h>
60 1.1 cgd #include <stdlib.h>
61 1.6 cgd #include <string.h>
62 1.1 cgd #include <rpc/types.h>
63 1.1 cgd #include <rpc/xdr.h>
64 1.1 cgd #include <netinet/in.h>
65 1.1 cgd
66 1.6 cgd static bool_t xdrrec_getlong __P((XDR *, long *));
67 1.6 cgd static bool_t xdrrec_putlong __P((XDR *, long *));
68 1.6 cgd static bool_t xdrrec_getbytes __P((XDR *, caddr_t, u_int));
69 1.6 cgd
70 1.6 cgd static bool_t xdrrec_putbytes __P((XDR *, caddr_t, u_int));
71 1.6 cgd static u_int xdrrec_getpos __P((XDR *));
72 1.6 cgd static bool_t xdrrec_setpos __P((XDR *, u_int));
73 1.6 cgd static int32_t *xdrrec_inline __P((XDR *, u_int));
74 1.6 cgd static void xdrrec_destroy __P((XDR *));
75 1.1 cgd
76 1.1 cgd static struct xdr_ops xdrrec_ops = {
77 1.1 cgd xdrrec_getlong,
78 1.1 cgd xdrrec_putlong,
79 1.1 cgd xdrrec_getbytes,
80 1.1 cgd xdrrec_putbytes,
81 1.1 cgd xdrrec_getpos,
82 1.1 cgd xdrrec_setpos,
83 1.1 cgd xdrrec_inline,
84 1.1 cgd xdrrec_destroy
85 1.1 cgd };
86 1.1 cgd
87 1.1 cgd /*
88 1.1 cgd * A record is composed of one or more record fragments.
89 1.1 cgd * A record fragment is a two-byte header followed by zero to
90 1.1 cgd * 2**32-1 bytes. The header is treated as a long unsigned and is
91 1.1 cgd * encode/decoded to the network via htonl/ntohl. The low order 31 bits
92 1.1 cgd * are a byte count of the fragment. The highest order bit is a boolean:
93 1.1 cgd * 1 => this fragment is the last fragment of the record,
94 1.1 cgd * 0 => this fragment is followed by more fragment(s).
95 1.1 cgd *
96 1.1 cgd * The fragment/record machinery is not general; it is constructed to
97 1.1 cgd * meet the needs of xdr and rpc based on tcp.
98 1.1 cgd */
99 1.1 cgd
100 1.4 cgd #define LAST_FRAG ((u_int32_t)(1 << 31))
101 1.1 cgd
102 1.1 cgd typedef struct rec_strm {
103 1.1 cgd caddr_t tcp_handle;
104 1.1 cgd caddr_t the_buffer;
105 1.1 cgd /*
106 1.1 cgd * out-goung bits
107 1.1 cgd */
108 1.4 cgd int (*writeit) __P((caddr_t, caddr_t, int));
109 1.1 cgd caddr_t out_base; /* output buffer (points to frag header) */
110 1.1 cgd caddr_t out_finger; /* next output position */
111 1.1 cgd caddr_t out_boundry; /* data cannot up to this address */
112 1.4 cgd u_int32_t *frag_header; /* beginning of curren fragment */
113 1.1 cgd bool_t frag_sent; /* true if buffer sent in middle of record */
114 1.1 cgd /*
115 1.1 cgd * in-coming bits
116 1.1 cgd */
117 1.4 cgd int (*readit) __P((caddr_t, caddr_t, int));
118 1.1 cgd u_long in_size; /* fixed size of the input buffer */
119 1.1 cgd caddr_t in_base;
120 1.1 cgd caddr_t in_finger; /* location of next byte to be had */
121 1.1 cgd caddr_t in_boundry; /* can read up to this location */
122 1.1 cgd long fbtbc; /* fragment bytes to be consumed */
123 1.1 cgd bool_t last_frag;
124 1.1 cgd u_int sendsize;
125 1.1 cgd u_int recvsize;
126 1.1 cgd } RECSTREAM;
127 1.1 cgd
128 1.6 cgd static u_int fix_buf_size __P((u_int));
129 1.6 cgd static bool_t flush_out __P((RECSTREAM *, bool_t));
130 1.7 christos static bool_t fill_input_buf __P((RECSTREAM *));
131 1.6 cgd static bool_t get_input_bytes __P((RECSTREAM *, caddr_t, int));
132 1.6 cgd static bool_t set_input_fragment __P((RECSTREAM *));
133 1.6 cgd static bool_t skip_input_bytes __P((RECSTREAM *, long));
134 1.6 cgd
135 1.1 cgd
136 1.1 cgd /*
137 1.1 cgd * Create an xdr handle for xdrrec
138 1.1 cgd * xdrrec_create fills in xdrs. Sendsize and recvsize are
139 1.1 cgd * send and recv buffer sizes (0 => use default).
140 1.1 cgd * tcp_handle is an opaque handle that is passed as the first parameter to
141 1.1 cgd * the procedures readit and writeit. Readit and writeit are read and
142 1.1 cgd * write respectively. They are like the system
143 1.1 cgd * calls expect that they take an opaque handle rather than an fd.
144 1.1 cgd */
145 1.1 cgd void
146 1.1 cgd xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
147 1.1 cgd register XDR *xdrs;
148 1.1 cgd register u_int sendsize;
149 1.1 cgd register u_int recvsize;
150 1.1 cgd caddr_t tcp_handle;
151 1.7 christos /* like read, but pass it a tcp_handle, not sock */
152 1.7 christos int (*readit) __P((caddr_t, caddr_t, int));
153 1.7 christos /* like write, but pass it a tcp_handle, not sock */
154 1.7 christos int (*writeit) __P((caddr_t, caddr_t, int));
155 1.1 cgd {
156 1.1 cgd register RECSTREAM *rstrm =
157 1.1 cgd (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
158 1.1 cgd
159 1.1 cgd if (rstrm == NULL) {
160 1.1 cgd (void)fprintf(stderr, "xdrrec_create: out of memory\n");
161 1.1 cgd /*
162 1.1 cgd * This is bad. Should rework xdrrec_create to
163 1.1 cgd * return a handle, and in this case return NULL
164 1.1 cgd */
165 1.1 cgd return;
166 1.1 cgd }
167 1.1 cgd /*
168 1.1 cgd * adjust sizes and allocate buffer quad byte aligned
169 1.1 cgd */
170 1.1 cgd rstrm->sendsize = sendsize = fix_buf_size(sendsize);
171 1.1 cgd rstrm->recvsize = recvsize = fix_buf_size(recvsize);
172 1.1 cgd rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
173 1.1 cgd if (rstrm->the_buffer == NULL) {
174 1.1 cgd (void)fprintf(stderr, "xdrrec_create: out of memory\n");
175 1.1 cgd return;
176 1.1 cgd }
177 1.1 cgd for (rstrm->out_base = rstrm->the_buffer;
178 1.4 cgd (u_long)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
179 1.1 cgd rstrm->out_base++);
180 1.1 cgd rstrm->in_base = rstrm->out_base + sendsize;
181 1.1 cgd /*
182 1.1 cgd * now the rest ...
183 1.1 cgd */
184 1.1 cgd xdrs->x_ops = &xdrrec_ops;
185 1.1 cgd xdrs->x_private = (caddr_t)rstrm;
186 1.1 cgd rstrm->tcp_handle = tcp_handle;
187 1.1 cgd rstrm->readit = readit;
188 1.1 cgd rstrm->writeit = writeit;
189 1.1 cgd rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
190 1.4 cgd rstrm->frag_header = (u_int32_t *)rstrm->out_base;
191 1.4 cgd rstrm->out_finger += sizeof(u_int32_t);
192 1.1 cgd rstrm->out_boundry += sendsize;
193 1.1 cgd rstrm->frag_sent = FALSE;
194 1.1 cgd rstrm->in_size = recvsize;
195 1.1 cgd rstrm->in_boundry = rstrm->in_base;
196 1.1 cgd rstrm->in_finger = (rstrm->in_boundry += recvsize);
197 1.1 cgd rstrm->fbtbc = 0;
198 1.1 cgd rstrm->last_frag = TRUE;
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd
202 1.1 cgd /*
203 1.1 cgd * The reoutines defined below are the xdr ops which will go into the
204 1.1 cgd * xdr handle filled in by xdrrec_create.
205 1.1 cgd */
206 1.1 cgd
207 1.1 cgd static bool_t
208 1.1 cgd xdrrec_getlong(xdrs, lp)
209 1.1 cgd XDR *xdrs;
210 1.1 cgd long *lp;
211 1.1 cgd {
212 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
213 1.4 cgd register int32_t *buflp = (int32_t *)(rstrm->in_finger);
214 1.4 cgd int32_t mylong;
215 1.1 cgd
216 1.1 cgd /* first try the inline, fast case */
217 1.4 cgd if ((rstrm->fbtbc >= sizeof(int32_t)) &&
218 1.4 cgd (((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
219 1.4 cgd *lp = (long)ntohl((u_int32_t)(*buflp));
220 1.4 cgd rstrm->fbtbc -= sizeof(int32_t);
221 1.4 cgd rstrm->in_finger += sizeof(int32_t);
222 1.1 cgd } else {
223 1.4 cgd if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(int32_t)))
224 1.1 cgd return (FALSE);
225 1.4 cgd *lp = (long)ntohl((u_int32_t)mylong);
226 1.1 cgd }
227 1.1 cgd return (TRUE);
228 1.1 cgd }
229 1.1 cgd
230 1.1 cgd static bool_t
231 1.1 cgd xdrrec_putlong(xdrs, lp)
232 1.1 cgd XDR *xdrs;
233 1.1 cgd long *lp;
234 1.1 cgd {
235 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
236 1.4 cgd register int32_t *dest_lp = ((int32_t *)(rstrm->out_finger));
237 1.1 cgd
238 1.4 cgd if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
239 1.1 cgd /*
240 1.1 cgd * this case should almost never happen so the code is
241 1.1 cgd * inefficient
242 1.1 cgd */
243 1.4 cgd rstrm->out_finger -= sizeof(int32_t);
244 1.1 cgd rstrm->frag_sent = TRUE;
245 1.1 cgd if (! flush_out(rstrm, FALSE))
246 1.1 cgd return (FALSE);
247 1.4 cgd dest_lp = ((int32_t *)(rstrm->out_finger));
248 1.4 cgd rstrm->out_finger += sizeof(int32_t);
249 1.1 cgd }
250 1.4 cgd *dest_lp = (int32_t)htonl((u_int32_t)(*lp));
251 1.1 cgd return (TRUE);
252 1.1 cgd }
253 1.1 cgd
254 1.1 cgd static bool_t /* must manage buffers, fragments, and records */
255 1.1 cgd xdrrec_getbytes(xdrs, addr, len)
256 1.1 cgd XDR *xdrs;
257 1.1 cgd register caddr_t addr;
258 1.1 cgd register u_int len;
259 1.1 cgd {
260 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
261 1.1 cgd register int current;
262 1.1 cgd
263 1.1 cgd while (len > 0) {
264 1.1 cgd current = rstrm->fbtbc;
265 1.1 cgd if (current == 0) {
266 1.1 cgd if (rstrm->last_frag)
267 1.1 cgd return (FALSE);
268 1.1 cgd if (! set_input_fragment(rstrm))
269 1.1 cgd return (FALSE);
270 1.1 cgd continue;
271 1.1 cgd }
272 1.1 cgd current = (len < current) ? len : current;
273 1.1 cgd if (! get_input_bytes(rstrm, addr, current))
274 1.1 cgd return (FALSE);
275 1.1 cgd addr += current;
276 1.1 cgd rstrm->fbtbc -= current;
277 1.1 cgd len -= current;
278 1.1 cgd }
279 1.1 cgd return (TRUE);
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd static bool_t
283 1.1 cgd xdrrec_putbytes(xdrs, addr, len)
284 1.1 cgd XDR *xdrs;
285 1.1 cgd register caddr_t addr;
286 1.1 cgd register u_int len;
287 1.1 cgd {
288 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
289 1.4 cgd register long current;
290 1.1 cgd
291 1.1 cgd while (len > 0) {
292 1.4 cgd current = (u_long)rstrm->out_boundry -
293 1.4 cgd (u_long)rstrm->out_finger;
294 1.1 cgd current = (len < current) ? len : current;
295 1.1 cgd bcopy(addr, rstrm->out_finger, current);
296 1.1 cgd rstrm->out_finger += current;
297 1.1 cgd addr += current;
298 1.1 cgd len -= current;
299 1.1 cgd if (rstrm->out_finger == rstrm->out_boundry) {
300 1.1 cgd rstrm->frag_sent = TRUE;
301 1.1 cgd if (! flush_out(rstrm, FALSE))
302 1.1 cgd return (FALSE);
303 1.1 cgd }
304 1.1 cgd }
305 1.1 cgd return (TRUE);
306 1.1 cgd }
307 1.1 cgd
308 1.1 cgd static u_int
309 1.1 cgd xdrrec_getpos(xdrs)
310 1.1 cgd register XDR *xdrs;
311 1.1 cgd {
312 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
313 1.1 cgd register long pos;
314 1.1 cgd
315 1.4 cgd pos = lseek((off_t)(long)rstrm->tcp_handle, 0, 1);
316 1.1 cgd if (pos != -1)
317 1.1 cgd switch (xdrs->x_op) {
318 1.1 cgd
319 1.1 cgd case XDR_ENCODE:
320 1.1 cgd pos += rstrm->out_finger - rstrm->out_base;
321 1.1 cgd break;
322 1.1 cgd
323 1.1 cgd case XDR_DECODE:
324 1.1 cgd pos -= rstrm->in_boundry - rstrm->in_finger;
325 1.1 cgd break;
326 1.1 cgd
327 1.1 cgd default:
328 1.1 cgd pos = (u_int) -1;
329 1.1 cgd break;
330 1.1 cgd }
331 1.1 cgd return ((u_int) pos);
332 1.1 cgd }
333 1.1 cgd
334 1.1 cgd static bool_t
335 1.1 cgd xdrrec_setpos(xdrs, pos)
336 1.1 cgd register XDR *xdrs;
337 1.1 cgd u_int pos;
338 1.1 cgd {
339 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
340 1.1 cgd u_int currpos = xdrrec_getpos(xdrs);
341 1.1 cgd int delta = currpos - pos;
342 1.1 cgd caddr_t newpos;
343 1.1 cgd
344 1.1 cgd if ((int)currpos != -1)
345 1.1 cgd switch (xdrs->x_op) {
346 1.1 cgd
347 1.1 cgd case XDR_ENCODE:
348 1.1 cgd newpos = rstrm->out_finger - delta;
349 1.1 cgd if ((newpos > (caddr_t)(rstrm->frag_header)) &&
350 1.1 cgd (newpos < rstrm->out_boundry)) {
351 1.1 cgd rstrm->out_finger = newpos;
352 1.1 cgd return (TRUE);
353 1.1 cgd }
354 1.1 cgd break;
355 1.1 cgd
356 1.1 cgd case XDR_DECODE:
357 1.1 cgd newpos = rstrm->in_finger - delta;
358 1.1 cgd if ((delta < (int)(rstrm->fbtbc)) &&
359 1.1 cgd (newpos <= rstrm->in_boundry) &&
360 1.1 cgd (newpos >= rstrm->in_base)) {
361 1.1 cgd rstrm->in_finger = newpos;
362 1.1 cgd rstrm->fbtbc -= delta;
363 1.1 cgd return (TRUE);
364 1.1 cgd }
365 1.1 cgd break;
366 1.7 christos
367 1.7 christos case XDR_FREE:
368 1.7 christos break;
369 1.1 cgd }
370 1.1 cgd return (FALSE);
371 1.1 cgd }
372 1.1 cgd
373 1.4 cgd static int32_t *
374 1.1 cgd xdrrec_inline(xdrs, len)
375 1.1 cgd register XDR *xdrs;
376 1.6 cgd u_int len;
377 1.1 cgd {
378 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
379 1.4 cgd int32_t *buf = NULL;
380 1.1 cgd
381 1.1 cgd switch (xdrs->x_op) {
382 1.1 cgd
383 1.1 cgd case XDR_ENCODE:
384 1.1 cgd if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
385 1.4 cgd buf = (int32_t *) rstrm->out_finger;
386 1.1 cgd rstrm->out_finger += len;
387 1.1 cgd }
388 1.1 cgd break;
389 1.1 cgd
390 1.1 cgd case XDR_DECODE:
391 1.1 cgd if ((len <= rstrm->fbtbc) &&
392 1.1 cgd ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
393 1.4 cgd buf = (int32_t *) rstrm->in_finger;
394 1.1 cgd rstrm->fbtbc -= len;
395 1.1 cgd rstrm->in_finger += len;
396 1.1 cgd }
397 1.7 christos break;
398 1.7 christos
399 1.7 christos case XDR_FREE:
400 1.1 cgd break;
401 1.1 cgd }
402 1.1 cgd return (buf);
403 1.1 cgd }
404 1.1 cgd
405 1.1 cgd static void
406 1.1 cgd xdrrec_destroy(xdrs)
407 1.1 cgd register XDR *xdrs;
408 1.1 cgd {
409 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
410 1.1 cgd
411 1.1 cgd mem_free(rstrm->the_buffer,
412 1.1 cgd rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
413 1.1 cgd mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
414 1.1 cgd }
415 1.1 cgd
416 1.1 cgd
417 1.1 cgd /*
418 1.1 cgd * Exported routines to manage xdr records
419 1.1 cgd */
420 1.1 cgd
421 1.1 cgd /*
422 1.1 cgd * Before reading (deserializing from the stream, one should always call
423 1.1 cgd * this procedure to guarantee proper record alignment.
424 1.1 cgd */
425 1.1 cgd bool_t
426 1.1 cgd xdrrec_skiprecord(xdrs)
427 1.1 cgd XDR *xdrs;
428 1.1 cgd {
429 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
430 1.1 cgd
431 1.1 cgd while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
432 1.1 cgd if (! skip_input_bytes(rstrm, rstrm->fbtbc))
433 1.1 cgd return (FALSE);
434 1.1 cgd rstrm->fbtbc = 0;
435 1.1 cgd if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
436 1.1 cgd return (FALSE);
437 1.1 cgd }
438 1.1 cgd rstrm->last_frag = FALSE;
439 1.1 cgd return (TRUE);
440 1.1 cgd }
441 1.1 cgd
442 1.1 cgd /*
443 1.1 cgd * Look ahead fuction.
444 1.1 cgd * Returns TRUE iff there is no more input in the buffer
445 1.1 cgd * after consuming the rest of the current record.
446 1.1 cgd */
447 1.1 cgd bool_t
448 1.1 cgd xdrrec_eof(xdrs)
449 1.1 cgd XDR *xdrs;
450 1.1 cgd {
451 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
452 1.1 cgd
453 1.1 cgd while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
454 1.1 cgd if (! skip_input_bytes(rstrm, rstrm->fbtbc))
455 1.1 cgd return (TRUE);
456 1.1 cgd rstrm->fbtbc = 0;
457 1.1 cgd if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
458 1.1 cgd return (TRUE);
459 1.1 cgd }
460 1.1 cgd if (rstrm->in_finger == rstrm->in_boundry)
461 1.1 cgd return (TRUE);
462 1.1 cgd return (FALSE);
463 1.1 cgd }
464 1.1 cgd
465 1.1 cgd /*
466 1.1 cgd * The client must tell the package when an end-of-record has occurred.
467 1.1 cgd * The second paraemters tells whether the record should be flushed to the
468 1.1 cgd * (output) tcp stream. (This let's the package support batched or
469 1.1 cgd * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
470 1.1 cgd */
471 1.1 cgd bool_t
472 1.1 cgd xdrrec_endofrecord(xdrs, sendnow)
473 1.1 cgd XDR *xdrs;
474 1.1 cgd bool_t sendnow;
475 1.1 cgd {
476 1.1 cgd register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
477 1.1 cgd register u_long len; /* fragment length */
478 1.1 cgd
479 1.1 cgd if (sendnow || rstrm->frag_sent ||
480 1.4 cgd ((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
481 1.1 cgd (u_long)rstrm->out_boundry)) {
482 1.1 cgd rstrm->frag_sent = FALSE;
483 1.1 cgd return (flush_out(rstrm, TRUE));
484 1.1 cgd }
485 1.1 cgd len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
486 1.4 cgd sizeof(u_int32_t);
487 1.1 cgd *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
488 1.4 cgd rstrm->frag_header = (u_int32_t *)rstrm->out_finger;
489 1.4 cgd rstrm->out_finger += sizeof(u_int32_t);
490 1.1 cgd return (TRUE);
491 1.1 cgd }
492 1.1 cgd
493 1.1 cgd
494 1.1 cgd /*
495 1.1 cgd * Internal useful routines
496 1.1 cgd */
497 1.1 cgd static bool_t
498 1.1 cgd flush_out(rstrm, eor)
499 1.1 cgd register RECSTREAM *rstrm;
500 1.1 cgd bool_t eor;
501 1.1 cgd {
502 1.1 cgd register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
503 1.4 cgd register u_int32_t len = (u_long)(rstrm->out_finger) -
504 1.4 cgd (u_long)(rstrm->frag_header) - sizeof(u_int32_t);
505 1.1 cgd
506 1.1 cgd *(rstrm->frag_header) = htonl(len | eormask);
507 1.1 cgd len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
508 1.1 cgd if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
509 1.1 cgd != (int)len)
510 1.1 cgd return (FALSE);
511 1.4 cgd rstrm->frag_header = (u_int32_t *)rstrm->out_base;
512 1.4 cgd rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_int32_t);
513 1.1 cgd return (TRUE);
514 1.1 cgd }
515 1.1 cgd
516 1.1 cgd static bool_t /* knows nothing about records! Only about input buffers */
517 1.1 cgd fill_input_buf(rstrm)
518 1.1 cgd register RECSTREAM *rstrm;
519 1.1 cgd {
520 1.1 cgd register caddr_t where;
521 1.4 cgd u_long i;
522 1.4 cgd register long len;
523 1.1 cgd
524 1.1 cgd where = rstrm->in_base;
525 1.4 cgd i = (u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
526 1.1 cgd where += i;
527 1.1 cgd len = rstrm->in_size - i;
528 1.1 cgd if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
529 1.1 cgd return (FALSE);
530 1.1 cgd rstrm->in_finger = where;
531 1.1 cgd where += len;
532 1.1 cgd rstrm->in_boundry = where;
533 1.1 cgd return (TRUE);
534 1.1 cgd }
535 1.1 cgd
536 1.1 cgd static bool_t /* knows nothing about records! Only about input buffers */
537 1.1 cgd get_input_bytes(rstrm, addr, len)
538 1.1 cgd register RECSTREAM *rstrm;
539 1.1 cgd register caddr_t addr;
540 1.1 cgd register int len;
541 1.1 cgd {
542 1.4 cgd register long current;
543 1.1 cgd
544 1.1 cgd while (len > 0) {
545 1.4 cgd current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
546 1.1 cgd if (current == 0) {
547 1.1 cgd if (! fill_input_buf(rstrm))
548 1.1 cgd return (FALSE);
549 1.1 cgd continue;
550 1.1 cgd }
551 1.1 cgd current = (len < current) ? len : current;
552 1.1 cgd bcopy(rstrm->in_finger, addr, current);
553 1.1 cgd rstrm->in_finger += current;
554 1.1 cgd addr += current;
555 1.1 cgd len -= current;
556 1.1 cgd }
557 1.1 cgd return (TRUE);
558 1.1 cgd }
559 1.1 cgd
560 1.1 cgd static bool_t /* next two bytes of the input stream are treated as a header */
561 1.1 cgd set_input_fragment(rstrm)
562 1.1 cgd register RECSTREAM *rstrm;
563 1.1 cgd {
564 1.4 cgd u_int32_t header;
565 1.1 cgd
566 1.1 cgd if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
567 1.1 cgd return (FALSE);
568 1.1 cgd header = (long)ntohl(header);
569 1.1 cgd rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
570 1.1 cgd rstrm->fbtbc = header & (~LAST_FRAG);
571 1.1 cgd return (TRUE);
572 1.1 cgd }
573 1.1 cgd
574 1.1 cgd static bool_t /* consumes input bytes; knows nothing about records! */
575 1.1 cgd skip_input_bytes(rstrm, cnt)
576 1.1 cgd register RECSTREAM *rstrm;
577 1.1 cgd long cnt;
578 1.1 cgd {
579 1.4 cgd register long current;
580 1.1 cgd
581 1.1 cgd while (cnt > 0) {
582 1.4 cgd current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
583 1.1 cgd if (current == 0) {
584 1.1 cgd if (! fill_input_buf(rstrm))
585 1.1 cgd return (FALSE);
586 1.1 cgd continue;
587 1.1 cgd }
588 1.1 cgd current = (cnt < current) ? cnt : current;
589 1.1 cgd rstrm->in_finger += current;
590 1.1 cgd cnt -= current;
591 1.1 cgd }
592 1.1 cgd return (TRUE);
593 1.1 cgd }
594 1.1 cgd
595 1.1 cgd static u_int
596 1.1 cgd fix_buf_size(s)
597 1.1 cgd register u_int s;
598 1.1 cgd {
599 1.1 cgd
600 1.1 cgd if (s < 100)
601 1.1 cgd s = 4000;
602 1.1 cgd return (RNDUP(s));
603 1.1 cgd }
604