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