Libinfo-173.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr_rec.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
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.
32 *
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.
36 *
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.
40 *
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.
44 *
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.
48 *
49 * Sun Microsystems, Inc.
50 * 2550 Garcia Avenue
51 * Mountain View, California 94043
52 */
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 $";
57 #endif
58
59 /*
60 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
61 * layer above tcp (for rpc's use).
62 *
63 * Copyright (C) 1984, Sun Microsystems, Inc.
64 *
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.
74 */
75
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <unistd.h>
80 #include <rpc/types.h>
81 #include <rpc/xdr.h>
82 #include <netinet/in.h>
83
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();
89
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();
98
99 static struct xdr_ops xdrrec_ops = {
100 xdrrec_getlong,
101 xdrrec_putlong,
102 xdrrec_getbytes,
103 xdrrec_putbytes,
104 xdrrec_getpos,
105 xdrrec_setpos,
106 xdrrec_inline,
107 xdrrec_destroy
108 };
109
110 /*
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).
118 *
119 * The fragment/record machinery is not general; it is constructed to
120 * meet the needs of xdr and rpc based on tcp.
121 */
122
123 #define LAST_FRAG ((u_long)(1 << 31))
124
125 typedef struct rec_strm {
126 caddr_t tcp_handle;
127 caddr_t the_buffer;
128 /*
129 * out-goung bits
130 */
131 int (*writeit)();
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 */
137 /*
138 * in-coming bits
139 */
140 int (*readit)();
141 u_long in_size; /* fixed size of the input buffer */
142 caddr_t in_base;
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 */
146 bool_t last_frag;
147 u_int sendsize;
148 u_int recvsize;
149 } RECSTREAM;
150
151
152 /*
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.
160 */
161 void
162 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
163 register XDR *xdrs;
164 register u_int sendsize;
165 register u_int recvsize;
166 caddr_t tcp_handle;
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 */
169 {
170 register RECSTREAM *rstrm =
171 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
172
173 if (rstrm == NULL) {
174 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
175 /*
176 * This is bad. Should rework xdrrec_create to
177 * return a handle, and in this case return NULL
178 */
179 return;
180 }
181 /*
182 * adjust sizes and allocate buffer quad byte aligned
183 */
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");
189 return;
190 }
191 for (rstrm->out_base = rstrm->the_buffer;
192 (u_int)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
193 rstrm->out_base++);
194 rstrm->in_base = rstrm->out_base + sendsize;
195 /*
196 * now the rest ...
197 */
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);
211 rstrm->fbtbc = 0;
212 rstrm->last_frag = TRUE;
213 }
214
215
216 /*
217 * The reoutines defined below are the xdr ops which will go into the
218 * xdr handle filled in by xdrrec_create.
219 */
220
221 static bool_t
222 xdrrec_getlong(xdrs, lp)
223 XDR *xdrs;
224 long *lp;
225 {
226 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
227 register long *buflp = (long *)(rstrm->in_finger);
228 long mylong;
229
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);
236 } else {
237 if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(long)))
238 return (FALSE);
239 *lp = (long)ntohl((u_long)mylong);
240 }
241 return (TRUE);
242 }
243
244 static bool_t
245 xdrrec_putlong(xdrs, lp)
246 XDR *xdrs;
247 long *lp;
248 {
249 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
250 register long *dest_lp = ((long *)(rstrm->out_finger));
251
252 if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
253 /*
254 * this case should almost never happen so the code is
255 * inefficient
256 */
257 rstrm->out_finger -= sizeof(long);
258 rstrm->frag_sent = TRUE;
259 if (! flush_out(rstrm, FALSE))
260 return (FALSE);
261 dest_lp = ((long *)(rstrm->out_finger));
262 rstrm->out_finger += sizeof(long);
263 }
264 *dest_lp = (long)htonl((u_long)(*lp));
265 return (TRUE);
266 }
267
268 static bool_t /* must manage buffers, fragments, and records */
269 xdrrec_getbytes(xdrs, addr, len)
270 XDR *xdrs;
271 register caddr_t addr;
272 register u_int len;
273 {
274 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
275 register int current;
276
277 while (len > 0) {
278 current = rstrm->fbtbc;
279 if (current == 0) {
280 if (rstrm->last_frag)
281 return (FALSE);
282 if (! set_input_fragment(rstrm))
283 return (FALSE);
284 continue;
285 }
286 current = (len < current) ? len : current;
287 if (! get_input_bytes(rstrm, addr, current))
288 return (FALSE);
289 addr += current;
290 rstrm->fbtbc -= current;
291 len -= current;
292 }
293 return (TRUE);
294 }
295
296 static bool_t
297 xdrrec_putbytes(xdrs, addr, len)
298 XDR *xdrs;
299 register caddr_t addr;
300 register u_int len;
301 {
302 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
303 register int current;
304
305 while (len > 0) {
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;
310 addr += current;
311 len -= current;
312 if (rstrm->out_finger == rstrm->out_boundry) {
313 rstrm->frag_sent = TRUE;
314 if (! flush_out(rstrm, FALSE))
315 return (FALSE);
316 }
317 }
318 return (TRUE);
319 }
320
321 static u_int
322 xdrrec_getpos(xdrs)
323 register XDR *xdrs;
324 {
325 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
326 register long pos;
327
328 pos = lseek((int)rstrm->tcp_handle, 0, 1);
329 if (pos != -1)
330 switch (xdrs->x_op) {
331
332 case XDR_ENCODE:
333 pos += rstrm->out_finger - rstrm->out_base;
334 break;
335
336 case XDR_DECODE:
337 pos -= rstrm->in_boundry - rstrm->in_finger;
338 break;
339
340 default:
341 pos = (u_int) -1;
342 break;
343 }
344 return ((u_int) pos);
345 }
346
347 static bool_t
348 xdrrec_setpos(xdrs, pos)
349 register XDR *xdrs;
350 u_int pos;
351 {
352 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
353 u_int currpos = xdrrec_getpos(xdrs);
354 int delta = currpos - pos;
355 caddr_t newpos;
356
357 if ((int)currpos != -1)
358 switch (xdrs->x_op) {
359
360 case XDR_ENCODE:
361 newpos = rstrm->out_finger - delta;
362 if ((newpos > (caddr_t)(rstrm->frag_header)) &&
363 (newpos < rstrm->out_boundry)) {
364 rstrm->out_finger = newpos;
365 return (TRUE);
366 }
367 break;
368
369 case XDR_DECODE:
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;
376 return (TRUE);
377 }
378 break;
379 default: break;
380 }
381 return (FALSE);
382 }
383
384 static long *
385 xdrrec_inline(xdrs, len)
386 register XDR *xdrs;
387 int len;
388 {
389 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
390 long * buf = NULL;
391
392 switch (xdrs->x_op) {
393
394 case XDR_ENCODE:
395 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
396 buf = (long *) rstrm->out_finger;
397 rstrm->out_finger += len;
398 }
399 break;
400
401 case XDR_DECODE:
402 if ((len <= rstrm->fbtbc) &&
403 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
404 buf = (long *) rstrm->in_finger;
405 rstrm->fbtbc -= len;
406 rstrm->in_finger += len;
407 }
408 break;
409 default: break;
410 }
411 return (buf);
412 }
413
414 static void
415 xdrrec_destroy(xdrs)
416 register XDR *xdrs;
417 {
418 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
419
420 mem_free(rstrm->the_buffer,
421 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
422 mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
423 }
424
425
426 /*
427 * Exported routines to manage xdr records
428 */
429
430 /*
431 * Before reading (deserializing from the stream, one should always call
432 * this procedure to guarantee proper record alignment.
433 */
434 bool_t
435 xdrrec_skiprecord(xdrs)
436 XDR *xdrs;
437 {
438 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
439
440 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
441 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
442 return (FALSE);
443 rstrm->fbtbc = 0;
444 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
445 return (FALSE);
446 }
447 rstrm->last_frag = FALSE;
448 return (TRUE);
449 }
450
451 /*
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.
455 */
456 bool_t
457 xdrrec_eof(xdrs)
458 XDR *xdrs;
459 {
460 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
461
462 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
463 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
464 return (TRUE);
465 rstrm->fbtbc = 0;
466 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
467 return (TRUE);
468 }
469 if (rstrm->in_finger == rstrm->in_boundry)
470 return (TRUE);
471 return (FALSE);
472 }
473
474 /*
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.
479 */
480 bool_t
481 xdrrec_endofrecord(xdrs, sendnow)
482 XDR *xdrs;
483 bool_t sendnow;
484 {
485 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
486 register u_long len; /* fragment length */
487
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));
493 }
494 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
495 sizeof(u_long);
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);
499 return (TRUE);
500 }
501
502
503 /*
504 * Internal useful routines
505 */
506 static bool_t
507 flush_out(rstrm, eor)
508 register RECSTREAM *rstrm;
509 bool_t eor;
510 {
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);
514
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)
518 != (int)len)
519 return (FALSE);
520 rstrm->frag_header = (u_long *)rstrm->out_base;
521 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_long);
522 return (TRUE);
523 }
524
525 static bool_t /* knows nothing about records! Only about input buffers */
526 fill_input_buf(rstrm)
527 register RECSTREAM *rstrm;
528 {
529 register caddr_t where;
530 u_int i;
531 register int len;
532
533 where = rstrm->in_base;
534 i = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
535 where += i;
536 len = rstrm->in_size - i;
537 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
538 return (FALSE);
539 rstrm->in_finger = where;
540 where += len;
541 rstrm->in_boundry = where;
542 return (TRUE);
543 }
544
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;
549 register int len;
550 {
551 register int current;
552
553 while (len > 0) {
554 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
555 if (current == 0) {
556 if (! fill_input_buf(rstrm))
557 return (FALSE);
558 continue;
559 }
560 current = (len < current) ? len : current;
561 bcopy(rstrm->in_finger, addr, current);
562 rstrm->in_finger += current;
563 addr += current;
564 len -= current;
565 }
566 return (TRUE);
567 }
568
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;
572 {
573 u_long header;
574
575 if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
576 return (FALSE);
577 header = (long)ntohl(header);
578 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
579 rstrm->fbtbc = header & (~LAST_FRAG);
580 return (TRUE);
581 }
582
583 static bool_t /* consumes input bytes; knows nothing about records! */
584 skip_input_bytes(rstrm, cnt)
585 register RECSTREAM *rstrm;
586 long cnt;
587 {
588 register int current;
589
590 while (cnt > 0) {
591 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
592 if (current == 0) {
593 if (! fill_input_buf(rstrm))
594 return (FALSE);
595 continue;
596 }
597 current = (cnt < current) ? cnt : current;
598 rstrm->in_finger += current;
599 cnt -= current;
600 }
601 return (TRUE);
602 }
603
604 static u_int
605 fix_buf_size(s)
606 register u_int s;
607 {
608
609 if (s < 100)
610 s = 4000;
611 return (RNDUP(s));
612 }