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