cc2e1b37d61aa14989ae2e8cb00ddd429c344732
[shibboleth/cpp-sp.git] / oncrpc / xdr_rec.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11
12 /* @(#)xdr_rec.c        2.2 88/08/01 4.0 RPCSRC */
13 /*
14  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15  * unrestricted use provided that this legend is included on all tape
16  * media and as a part of the software program in whole or part.  Users
17  * may copy or modify Sun RPC without charge, but are not authorized
18  * to license or distribute it to anyone else except as part of a product or
19  * program developed by the user.
20  *
21  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
24  *
25  * Sun RPC is provided with no support and without any obligation on the
26  * part of Sun Microsystems, Inc. to assist in its use, correction,
27  * modification or enhancement.
28  *
29  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31  * OR ANY PART THEREOF.
32  *
33  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34  * or profits or other special, indirect and consequential damages, even if
35  * Sun has been advised of the possibility of such damages.
36  *
37  * Sun Microsystems, Inc.
38  * 2550 Garcia Avenue
39  * Mountain View, California  94043
40  */
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
43 #endif
44
45 /*
46  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
47  * layer above tcp (for rpc's use).
48  *
49  * Copyright (C) 1984, Sun Microsystems, Inc.
50  *
51  * These routines interface XDRSTREAMS to a tcp/ip connection.
52  * There is a record marking layer between the xdr stream
53  * and the tcp transport level.  A record is composed on one or more
54  * record fragments.  A record fragment is a thirty-two bit header followed
55  * by n bytes of data, where n is contained in the header.  The header
56  * is represented as a htonl(u_long).  Thegh order bit encodes
57  * whether or not the fragment is the last fragment of the record
58  * (1 => fragment is last, 0 => more fragments to follow.
59  * The other 31 bits encode the byte length of the fragment.
60  */
61
62 #include <stdio.h>
63 #ifdef WIN32
64 #include <io.h>
65 #include <rpc/rpc.h>
66 #include <rpc/xdr.h>
67 #else
68 #include <rpc/types.h>
69 #include <rpc/xdr.h>
70 #include <netinet/in.h>
71
72 extern off_t lseek(int, off_t, int);
73 #endif
74
75 static u_int    fix_buf_size();
76
77 static bool_t   xdrrec_getlong();
78 static bool_t   xdrrec_putlong();
79 static bool_t   xdrrec_getbytes();
80 static bool_t   xdrrec_putbytes();
81 static u_int    xdrrec_getpos();
82 static bool_t   xdrrec_setpos();
83 static long *   xdrrec_inline();
84 static void     xdrrec_destroy();
85
86 static bool_t   flush_out();
87 static bool_t   get_input_bytes();
88 static bool_t   set_input_fragment();
89 static bool_t   skip_input_bytes();
90
91 static struct  xdr_ops xdrrec_ops = {
92         xdrrec_getlong,
93         xdrrec_putlong,
94         xdrrec_getbytes,
95         xdrrec_putbytes,
96         xdrrec_getpos,
97         xdrrec_setpos,
98         xdrrec_inline,
99         xdrrec_destroy
100 };
101
102 /*
103  * A record is composed of one or more record fragments.
104  * A record fragment is a two-byte header followed by zero to
105  * 2**32-1 bytes.  The header is treated as a long unsigned and is
106  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
107  * are a byte count of the fragment.  The highest order bit is a boolean:
108  * 1 => this fragment is the last fragment of the record,
109  * 0 => this fragment is followed by more fragment(s).
110  *
111  * The fragment/record machinery is not general;  it is constructed to
112  * meet the needs of xdr and rpc based on tcp.
113  */
114
115 #define LAST_FRAG ((u_long)(1 << 31))
116
117 typedef struct rec_strm {
118         caddr_t tcp_handle;
119         caddr_t the_buffer;
120         /*
121          * out-goung bits
122          */
123         int (*writeit)();
124         caddr_t out_base;       /* output buffer (points to frag header) */
125         caddr_t out_finger;     /* next output position */
126         caddr_t out_boundry;    /* data cannot up to this address */
127         u_long *frag_header;    /* beginning of curren fragment */
128         bool_t frag_sent;       /* true if buffer sent in middle of record */
129         /*
130          * in-coming bits
131          */
132         int (*readit)();
133         u_long in_size; /* fixed size of the input buffer */
134         caddr_t in_base;
135         caddr_t in_finger;      /* location of next byte to be had */
136         caddr_t in_boundry;     /* can read up to this location */
137         long fbtbc;             /* fragment bytes to be consumed */
138         bool_t last_frag;
139         u_int sendsize;
140         u_int recvsize;
141 } RECSTREAM;
142
143
144 /*
145  * Create an xdr handle for xdrrec
146  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
147  * send and recv buffer sizes (0 => use default).
148  * tcp_handle is an opaque handle that is passed as the first parameter to
149  * the procedures readit and writeit.  Readit and writeit are read and
150  * write respectively.   They are like the system
151  * calls expect that they take an opaque handle rather than an fd.
152  */
153 void
154 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
155         register XDR *xdrs;
156         register u_int sendsize;
157         register u_int recvsize;
158         caddr_t tcp_handle;
159         int (*readit)();  /* like read, but pass it a tcp_handle, not sock */
160         int (*writeit)();  /* like write, but pass it a tcp_handle, not sock */
161 {
162         register RECSTREAM *rstrm =
163                 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
164
165         if (rstrm == NULL) {
166 #ifdef WIN32
167                 nt_rpc_report("xdrrec_create: out of memory\n");
168 #else
169                 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
170 #endif
171                 /*
172                  *  This is bad.  Should rework xdrrec_create to
173                  *  return a handle, and in this case return NULL
174                  */
175                 return;
176         }
177         /*
178          * adjust sizes and allocate buffer quad byte aligned
179          */
180         rstrm->sendsize = sendsize = fix_buf_size(sendsize);
181         rstrm->recvsize = recvsize = fix_buf_size(recvsize);
182         rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
183         if (rstrm->the_buffer == NULL) {
184 #ifdef WIN32
185                 nt_rpc_report("xdrrec_create: out of memory\n");
186 #else
187                 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
188 #endif
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, (long) 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                 }
380         return (FALSE);
381 }
382
383 static long *
384 xdrrec_inline(xdrs, len)
385         register XDR *xdrs;
386         int len;
387 {
388         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
389         long * buf = NULL;
390
391         switch (xdrs->x_op) {
392
393         case XDR_ENCODE:
394                 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
395                         buf = (long *) rstrm->out_finger;
396                         rstrm->out_finger += len;
397                 }
398                 break;
399
400         case XDR_DECODE:
401                 if ((len <= rstrm->fbtbc) &&
402                         ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
403                         buf = (long *) rstrm->in_finger;
404                         rstrm->fbtbc -= len;
405                         rstrm->in_finger += len;
406                 }
407                 break;
408         }
409         return (buf);
410 }
411
412 static void
413 xdrrec_destroy(xdrs)
414         register XDR *xdrs;
415 {
416         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
417
418         mem_free(rstrm->the_buffer,
419                 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
420         mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
421 }
422
423
424 /*
425  * Exported routines to manage xdr records
426  */
427
428 /*
429  * Before reading (deserializing from the stream, one should always call
430  * this procedure to guarantee proper record alignment.
431  */
432 bool_t
433 xdrrec_skiprecord(xdrs)
434         XDR *xdrs;
435 {
436         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
437
438         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
439                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
440                         return (FALSE);
441                 rstrm->fbtbc = 0;
442                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
443                         return (FALSE);
444         }
445         rstrm->last_frag = FALSE;
446         return (TRUE);
447 }
448
449 /*
450  * Look ahead fuction.
451  * Returns TRUE iff there is no more input in the buffer
452  * after consuming the rest of the current record.
453  */
454 bool_t
455 xdrrec_eof(xdrs)
456         XDR *xdrs;
457 {
458         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
459
460         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
461                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
462                         return (TRUE);
463                 rstrm->fbtbc = 0;
464                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
465                         return (TRUE);
466         }
467         if (rstrm->in_finger == rstrm->in_boundry)
468                 return (TRUE);
469         return (FALSE);
470 }
471
472 /*
473  * The client must tell the package when an end-of-record has occurred.
474  * The second paraemters tells whether the record should be flushed to the
475  * (output) tcp stream.  (This let's the package support batched or
476  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
477  */
478 bool_t
479 xdrrec_endofrecord(xdrs, sendnow)
480         XDR *xdrs;
481         bool_t sendnow;
482 {
483         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
484         register u_long len;  /* fragment length */
485
486         if (sendnow || rstrm->frag_sent ||
487                 ((u_long)rstrm->out_finger + sizeof(u_long) >=
488                 (u_long)rstrm->out_boundry)) {
489                 rstrm->frag_sent = FALSE;
490                 return (flush_out(rstrm, TRUE));
491         }
492         len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
493            sizeof(u_long);
494         *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
495         rstrm->frag_header = (u_long *)rstrm->out_finger;
496         rstrm->out_finger += sizeof(u_long);
497         return (TRUE);
498 }
499
500
501 /*
502  * Internal useful routines
503  */
504 static bool_t
505 flush_out(rstrm, eor)
506         register RECSTREAM *rstrm;
507         bool_t eor;
508 {
509         register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
510         register u_long len = (u_long)(rstrm->out_finger) -
511                 (u_long)(rstrm->frag_header) - sizeof(u_long);
512
513         *(rstrm->frag_header) = htonl(len | eormask);
514         len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
515         if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
516                 != (int)len)
517                 return (FALSE);
518         rstrm->frag_header = (u_long *)rstrm->out_base;
519         rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_long);
520         return (TRUE);
521 }
522
523 static bool_t  /* knows nothing about records!  Only about input buffers */
524 fill_input_buf(rstrm)
525         register RECSTREAM *rstrm;
526 {
527         register caddr_t where;
528         u_int i;
529         register int len;
530
531         where = rstrm->in_base;
532         i = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
533         where += i;
534         len = rstrm->in_size - i;
535         if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
536                 return (FALSE);
537         rstrm->in_finger = where;
538         where += len;
539         rstrm->in_boundry = where;
540         return (TRUE);
541 }
542
543 static bool_t  /* knows nothing about records!  Only about input buffers */
544 get_input_bytes(rstrm, addr, len)
545         register RECSTREAM *rstrm;
546         register caddr_t addr;
547         register int len;
548 {
549         register int current;
550
551         while (len > 0) {
552                 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
553                 if (current == 0) {
554                         if (! fill_input_buf(rstrm))
555                                 return (FALSE);
556                         continue;
557                 }
558                 current = (len < current) ? len : current;
559                 bcopy(rstrm->in_finger, addr, current);
560                 rstrm->in_finger += current;
561                 addr += current;
562                 len -= current;
563         }
564         return (TRUE);
565 }
566
567 static bool_t  /* next two bytes of the input stream are treated as a header */
568 set_input_fragment(rstrm)
569         register RECSTREAM *rstrm;
570 {
571         u_long header;
572
573         if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
574                 return (FALSE);
575         header = (long)ntohl(header);
576         rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
577         rstrm->fbtbc = header & (~LAST_FRAG);
578         return (TRUE);
579 }
580
581 static bool_t  /* consumes input bytes; knows nothing about records! */
582 skip_input_bytes(rstrm, cnt)
583         register RECSTREAM *rstrm;
584         long cnt;
585 {
586         register int current;
587
588         while (cnt > 0) {
589                 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
590                 if (current == 0) {
591                         if (! fill_input_buf(rstrm))
592                                 return (FALSE);
593                         continue;
594                 }
595                 current = (cnt < current) ? cnt : current;
596                 rstrm->in_finger += current;
597                 cnt -= current;
598         }
599         return (TRUE);
600 }
601
602 static u_int
603 fix_buf_size(s)
604         register u_int s;
605 {
606
607         if (s < 100)
608                 s = 4000;
609         return (RNDUP(s));
610 }