Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / lib / rpc / xdr_rec.c
1 /* @(#)xdr_rec.c        2.2 88/08/01 4.0 RPCSRC */
2 /*
3  * Copyright (c) 2010, Oracle America, Inc.
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *
18  *     * Neither the name of the "Oracle America, Inc." nor the names of
19  *       its contributors may be used to endorse or promote products
20  *       derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 #if !defined(lint) && defined(SCCSIDS)
35 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
36 #endif
37
38 /*
39  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
40  * layer above tcp (for rpc's use).
41  *
42  * These routines interface XDRSTREAMS to a tcp/ip connection.
43  * There is a record marking layer between the xdr stream
44  * and the tcp transport level.  A record is composed on one or more
45  * record fragments.  A record fragment is a thirty-two bit header followed
46  * by n bytes of data, where n is contained in the header.  The header
47  * is represented as a htonl(uint32_t).  Thegh order bit encodes
48  * whether or not the fragment is the last fragment of the record
49  * (1 => fragment is last, 0 => more fragments to follow.
50  * The other 31 bits encode the byte length of the fragment.
51  */
52
53 #include <stdio.h>
54 #include <gssrpc/types.h>
55 #include <gssrpc/xdr.h>
56 #include <netinet/in.h>
57
58 #include <unistd.h>
59 #include <string.h>
60
61 static bool_t   xdrrec_getlong(XDR *, long *);
62 static bool_t   xdrrec_putlong(XDR *, long *);
63 static bool_t   xdrrec_getbytes(XDR *, caddr_t, u_int);
64 static bool_t   xdrrec_putbytes(XDR *, caddr_t, u_int);
65 static u_int    xdrrec_getpos(XDR *);
66 static bool_t   xdrrec_setpos(XDR *, u_int);
67 static rpc_inline_t *   xdrrec_inline(XDR *, int);
68 static void     xdrrec_destroy(XDR *);
69
70 static struct  xdr_ops xdrrec_ops = {
71         xdrrec_getlong,
72         xdrrec_putlong,
73         xdrrec_getbytes,
74         xdrrec_putbytes,
75         xdrrec_getpos,
76         xdrrec_setpos,
77         xdrrec_inline,
78         xdrrec_destroy
79 };
80
81 /*
82  * A record is composed of one or more record fragments.
83  * A record fragment is a four-byte header followed by zero to
84  * 2**31-1 bytes.  The header is treated as an unsigned 32 bit integer and is
85  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
86  * are a byte count of the fragment.  The highest order bit is a boolean:
87  * 1 => this fragment is the last fragment of the record,
88  * 0 => this fragment is followed by more fragment(s).
89  *
90  * The fragment/record machinery is not general;  it is constructed to
91  * meet the needs of xdr and rpc based on tcp.
92  */
93
94 #define LAST_FRAG ((uint32_t)(1UL << 31))
95
96 typedef struct rec_strm {
97         caddr_t tcp_handle;
98         caddr_t the_buffer;
99         /*
100          * out-goung bits
101          */
102         int (*writeit)();
103         caddr_t out_base;       /* output buffer (points to frag header) */
104         caddr_t out_finger;     /* next output position */
105         caddr_t out_boundry;    /* data cannot up to this address */
106         uint32_t *frag_header;  /* beginning of curren fragment */
107         bool_t frag_sent;       /* true if buffer sent in middle of record */
108         /*
109          * in-coming bits
110          */
111         int (*readit)();
112         uint32_t in_size;       /* fixed size of the input buffer */
113         caddr_t in_base;
114         caddr_t in_finger;      /* location of next byte to be had */
115         caddr_t in_boundry;     /* can read up to this location */
116         int32_t fbtbc;          /* fragment bytes to be consumed */
117         bool_t last_frag;
118         u_int sendsize;
119         u_int recvsize;
120 } RECSTREAM;
121
122 static u_int    fix_buf_size(u_int);
123 static bool_t   flush_out(RECSTREAM *, bool_t);
124 static bool_t   get_input_bytes(RECSTREAM *, caddr_t, int);
125 static bool_t   set_input_fragment(RECSTREAM *);
126 static bool_t   skip_input_bytes(RECSTREAM *, int32_t);
127
128 /*
129  * Create an xdr handle for xdrrec
130  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
131  * send and recv buffer sizes (0 => use default).
132  * tcp_handle is an opaque handle that is passed as the first parameter to
133  * the procedures readit and writeit.  Readit and writeit are read and
134  * write respectively.   They are like the system
135  * calls expect that they take an opaque handle rather than an fd.
136  */
137 void
138 xdrrec_create(
139         XDR *xdrs,
140         u_int sendsize,
141         u_int recvsize,
142         caddr_t tcp_handle,
143         int (*readit)(), /* like read, but pass it a tcp_handle, not sock */
144         int (*writeit)() /* like write, but pass it a tcp_handle, not sock */
145         )
146 {
147         register RECSTREAM *rstrm =
148                 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
149
150         if (rstrm == NULL) {
151                 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
152                 /*
153                  *  This is bad.  Should rework xdrrec_create to
154                  *  return a handle, and in this case return NULL
155                  */
156                 return;
157         }
158         /*
159          * adjust sizes and allocate buffer quad byte aligned
160          */
161         rstrm->sendsize = sendsize = fix_buf_size(sendsize);
162         rstrm->recvsize = recvsize = fix_buf_size(recvsize);
163         rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
164         if (rstrm->the_buffer == NULL) {
165                 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
166                 return;
167         }
168         for (rstrm->out_base = rstrm->the_buffer;
169              /* Pointer arithmetic - long cast allowed... */
170                 (u_long)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
171                 rstrm->out_base++);
172         rstrm->in_base = rstrm->out_base + sendsize;
173         /*
174          * now the rest ...
175          */
176         xdrs->x_ops = &xdrrec_ops;
177         xdrs->x_private = (caddr_t)rstrm;
178         rstrm->tcp_handle = tcp_handle;
179         rstrm->readit = readit;
180         rstrm->writeit = writeit;
181         rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
182         rstrm->frag_header = (uint32_t *)(void *)rstrm->out_base;
183         rstrm->out_finger += BYTES_PER_XDR_UNIT;
184         rstrm->out_boundry += sendsize;
185         rstrm->frag_sent = FALSE;
186         rstrm->in_size = recvsize;
187         rstrm->in_boundry = rstrm->in_base;
188         rstrm->in_finger = (rstrm->in_boundry += recvsize);
189         rstrm->fbtbc = 0;
190         rstrm->last_frag = TRUE;
191 }
192
193
194 /*
195  * The reoutines defined below are the xdr ops which will go into the
196  * xdr handle filled in by xdrrec_create.
197  */
198
199 static bool_t
200 xdrrec_getlong(XDR *xdrs, long *lp)
201 {
202         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
203         register int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
204         uint32_t mylong;
205
206         /* first try the inline, fast case */
207         if ((rstrm->fbtbc >= BYTES_PER_XDR_UNIT) &&
208                 (((long)rstrm->in_boundry - (long)buflp) >=
209                  BYTES_PER_XDR_UNIT)) {
210                 *lp = (long)ntohl((uint32_t)(*buflp));
211                 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
212                 rstrm->in_finger += BYTES_PER_XDR_UNIT;
213         } else {
214                 if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong,
215                                       BYTES_PER_XDR_UNIT))
216                         return (FALSE);
217                 *lp = (long)(int32_t)ntohl(mylong);
218         }
219         return (TRUE);
220 }
221
222 static bool_t
223 xdrrec_putlong(XDR *xdrs, long *lp)
224 {
225         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
226         register int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
227
228         if (rstrm->out_boundry - rstrm->out_finger < BYTES_PER_XDR_UNIT) {
229                 /*
230                  * this case should almost never happen so the code is
231                  * inefficient
232                  */
233                 rstrm->frag_sent = TRUE;
234                 if (! flush_out(rstrm, FALSE))
235                         return (FALSE);
236                 dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
237         }
238         rstrm->out_finger += BYTES_PER_XDR_UNIT;
239         *dest_lp = (int32_t)htonl((uint32_t)(*lp));
240         return (TRUE);
241 }
242
243 static bool_t  /* must manage buffers, fragments, and records */
244 xdrrec_getbytes(XDR *xdrs, caddr_t addr, u_int len)
245 {
246         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
247         register u_int current;
248
249         while (len > 0) {
250                 current = rstrm->fbtbc;
251                 if (current == 0) {
252                         if (rstrm->last_frag)
253                                 return (FALSE);
254                         if (! set_input_fragment(rstrm))
255                                 return (FALSE);
256                         continue;
257                 }
258                 current = (len < current) ? len : current;
259                 if (! get_input_bytes(rstrm, addr, current))
260                         return (FALSE);
261                 addr += current;
262                 rstrm->fbtbc -= current;
263                 len -= current;
264         }
265         return (TRUE);
266 }
267
268 static bool_t
269 xdrrec_putbytes(XDR *xdrs, caddr_t addr, u_int len)
270 {
271         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
272         register size_t current;
273
274         while (len > 0) {
275                 current = (size_t) ((long)rstrm->out_boundry -
276                                  (long)rstrm->out_finger);
277                 current = (len < current) ? len : current;
278                 memmove(rstrm->out_finger, addr, current);
279                 rstrm->out_finger += current;
280                 addr += current;
281                 len -= current;
282                 if (rstrm->out_finger == rstrm->out_boundry) {
283                         rstrm->frag_sent = TRUE;
284                         if (! flush_out(rstrm, FALSE))
285                                 return (FALSE);
286                 }
287         }
288         return (TRUE);
289 }
290
291 static u_int
292 xdrrec_getpos(XDR *xdrs)
293 {
294         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
295         register int pos;
296
297         switch (xdrs->x_op) {
298
299         case XDR_ENCODE:
300                 pos = rstrm->out_finger - rstrm->out_base
301                         - BYTES_PER_XDR_UNIT;
302                 break;
303
304         case XDR_DECODE:
305                 pos = rstrm->in_boundry - rstrm->in_finger
306                         - BYTES_PER_XDR_UNIT;
307                 break;
308
309         default:
310                 pos = -1;
311                 break;
312         }
313         return ((u_int) pos);
314 }
315
316 static bool_t
317 xdrrec_setpos(XDR *xdrs, u_int pos)
318 {
319         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
320         u_int currpos = xdrrec_getpos(xdrs);
321         int delta = currpos - pos;
322         caddr_t newpos;
323
324         if ((int)currpos != -1)
325                 switch (xdrs->x_op) {
326
327                 case XDR_ENCODE:
328                         newpos = rstrm->out_finger - delta;
329                         if ((newpos > (caddr_t)(rstrm->frag_header)) &&
330                                 (newpos < rstrm->out_boundry)) {
331                                 rstrm->out_finger = newpos;
332                                 return (TRUE);
333                         }
334                         break;
335
336                 case XDR_DECODE:
337                         newpos = rstrm->in_finger - delta;
338                         if ((delta < (int)(rstrm->fbtbc)) &&
339                                 (newpos <= rstrm->in_boundry) &&
340                                 (newpos >= rstrm->in_base)) {
341                                 rstrm->in_finger = newpos;
342                                 rstrm->fbtbc -= delta;
343                                 return (TRUE);
344                         }
345                         break;
346
347                 case XDR_FREE:
348                         break;
349                 }
350         return (FALSE);
351 }
352
353 static rpc_inline_t *
354 xdrrec_inline(XDR *xdrs, int len)
355 {
356         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
357         rpc_inline_t * buf = NULL;
358
359         if (len < 0)
360                 return (FALSE);
361
362         switch (xdrs->x_op) {
363
364         case XDR_ENCODE:
365                 if (len <= (rstrm->out_boundry - rstrm->out_finger)) {
366                         buf = (rpc_inline_t *)(void *) rstrm->out_finger;
367                         rstrm->out_finger += len;
368                 }
369                 break;
370
371         case XDR_DECODE:
372                 if ((len <= rstrm->fbtbc) &&
373                         (len <= (rstrm->in_boundry - rstrm->in_finger))) {
374                         buf = (rpc_inline_t *)(void *) rstrm->in_finger;
375                         rstrm->fbtbc -= len;
376                         rstrm->in_finger += len;
377                 }
378                 break;
379
380         case XDR_FREE:
381                 break;
382         }
383         return (buf);
384 }
385
386 static void
387 xdrrec_destroy(XDR *xdrs)
388 {
389         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
390
391         mem_free(rstrm->the_buffer,
392                 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
393         mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
394 }
395
396
397 /*
398  * Exported routines to manage xdr records
399  */
400
401 /*
402  * Before reading (deserializing from the stream, one should always call
403  * this procedure to guarantee proper record alignment.
404  */
405 bool_t
406 xdrrec_skiprecord(XDR *xdrs)
407 {
408         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
409
410         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
411                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
412                         return (FALSE);
413                 rstrm->fbtbc = 0;
414                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
415                         return (FALSE);
416         }
417         rstrm->last_frag = FALSE;
418         return (TRUE);
419 }
420
421 /*
422  * Look ahead fuction.
423  * Returns TRUE iff there is no more input in the buffer
424  * after consuming the rest of the current record.
425  */
426 bool_t
427 xdrrec_eof(XDR *xdrs)
428 {
429         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
430
431         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
432                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
433                         return (TRUE);
434                 rstrm->fbtbc = 0;
435                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
436                         return (TRUE);
437         }
438         if (rstrm->in_finger == rstrm->in_boundry)
439                 return (TRUE);
440         return (FALSE);
441 }
442
443 /*
444  * The client must tell the package when an end-of-record has occurred.
445  * The second paraemters tells whether the record should be flushed to the
446  * (output) tcp stream.  (This let's the package support batched or
447  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
448  */
449 bool_t
450 xdrrec_endofrecord(XDR *xdrs, bool_t sendnow)
451 {
452         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
453         register uint32_t len;  /* fragment length */
454
455         if (sendnow || rstrm->frag_sent ||
456                 ((long)rstrm->out_finger + BYTES_PER_XDR_UNIT >=
457                 (long)rstrm->out_boundry)) {
458                 rstrm->frag_sent = FALSE;
459                 return (flush_out(rstrm, TRUE));
460         }
461         len = (long)(rstrm->out_finger) - (long)(rstrm->frag_header) -
462            BYTES_PER_XDR_UNIT;
463         *(rstrm->frag_header) = htonl((uint32_t)len | LAST_FRAG);
464         rstrm->frag_header = (uint32_t *)(void *)rstrm->out_finger;
465         rstrm->out_finger += BYTES_PER_XDR_UNIT;
466         return (TRUE);
467 }
468
469
470 /*
471  * Internal useful routines
472  */
473 static bool_t
474 flush_out(RECSTREAM *rstrm, bool_t eor)
475 {
476         register uint32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
477         register uint32_t len = (u_long)(rstrm->out_finger) -
478                 (u_long)(rstrm->frag_header) - BYTES_PER_XDR_UNIT;
479
480         *(rstrm->frag_header) = htonl(len | eormask);
481         len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
482         if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
483                 != (int)len)
484                 return (FALSE);
485         rstrm->frag_header = (uint32_t *)(void *)rstrm->out_base;
486         rstrm->out_finger = (caddr_t)rstrm->out_base + BYTES_PER_XDR_UNIT;
487         return (TRUE);
488 }
489
490 static bool_t  /* knows nothing about records!  Only about input buffers */
491 fill_input_buf(RECSTREAM *rstrm)
492 {
493         register caddr_t where;
494         u_int i;
495         register int len;
496
497         where = rstrm->in_base;
498         i = (u_int)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
499         where += i;
500         len = rstrm->in_size - i;
501         if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
502                 return (FALSE);
503         rstrm->in_finger = where;
504         where += len;
505         rstrm->in_boundry = where;
506         return (TRUE);
507 }
508
509 static bool_t  /* knows nothing about records!  Only about input buffers */
510 get_input_bytes(RECSTREAM *rstrm, caddr_t addr, int len)
511 {
512         register size_t current;
513
514         while (len > 0) {
515                 current = (size_t)((long)rstrm->in_boundry -
516                                 (long)rstrm->in_finger);
517                 if (current == 0) {
518                         if (! fill_input_buf(rstrm))
519                                 return (FALSE);
520                         continue;
521                 }
522                 current = ((size_t)len < current) ? (size_t)len : current;
523                 memmove(addr, rstrm->in_finger, current);
524                 rstrm->in_finger += current;
525                 addr += current;
526                 len -= current;
527         }
528         return (TRUE);
529 }
530
531 static bool_t  /* next four bytes of input stream are treated as a header */
532 set_input_fragment(rstrm)
533         register RECSTREAM *rstrm;
534 {
535         uint32_t header;
536
537         if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
538                 return (FALSE);
539         header = ntohl(header);
540         rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
541         rstrm->fbtbc = header & (~LAST_FRAG);
542         return (TRUE);
543 }
544
545 static bool_t  /* consumes input bytes; knows nothing about records! */
546 skip_input_bytes(RECSTREAM *rstrm, int32_t cnt)
547 {
548         register int current;
549
550         while (cnt > 0) {
551                 current = (int)((long)rstrm->in_boundry -
552                                 (long)rstrm->in_finger);
553                 if (current == 0) {
554                         if (! fill_input_buf(rstrm))
555                                 return (FALSE);
556                         continue;
557                 }
558                 current = (cnt < current) ? cnt : current;
559                 rstrm->in_finger += current;
560                 cnt -= current;
561         }
562         return (TRUE);
563 }
564
565 static u_int
566 fix_buf_size(u_int s)
567 {
568
569         if (s < 100)
570                 s = 4000;
571         return (RNDUP(s));
572 }