home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / GUSI-RPC 4.0 / xdr_rec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-17  |  15.0 KB  |  582 lines  |  [TEXT/MPS ]

  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).  Thegh 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 <GUSI.h>
  53. #include <rpc/types.h>
  54. #include <rpc/xdr.h>
  55. #include <netinet/in.h>
  56.  
  57. extern long    lseek();
  58.  
  59. static u_int    fix_buf_size();
  60.  
  61. static bool_t    xdrrec_getlong();
  62. static bool_t    xdrrec_putlong();
  63. static bool_t    xdrrec_getbytes();
  64. static bool_t    xdrrec_putbytes();
  65. static u_int    xdrrec_getpos();
  66. static bool_t    xdrrec_setpos();
  67. static long *    xdrrec_inline();
  68. static void    xdrrec_destroy();
  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 two-byte header followed by zero to
  84.  * 2**32-1 bytes.  The header is treated as a long unsigned 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 ((u_long)(1 << 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.     u_long *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.     u_long 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.     long fbtbc;        /* fragment bytes to be consumed */
  117.     bool_t last_frag;
  118.     u_int sendsize;
  119.     u_int recvsize;
  120. } RECSTREAM;
  121.  
  122.  
  123. /*
  124.  * Create an xdr handle for xdrrec
  125.  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
  126.  * send and recv buffer sizes (0 => use default).
  127.  * tcp_handle is an opaque handle that is passed as the first parameter to
  128.  * the procedures readit and writeit.  Readit and writeit are read and
  129.  * write respectively.   They are like the system
  130.  * calls expect that they take an opaque handle rather than an fd.
  131.  */
  132. void
  133. xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
  134.     register XDR *xdrs;
  135.     register u_int sendsize;
  136.     register u_int recvsize;
  137.     caddr_t tcp_handle;
  138.     int (*readit)();  /* like read, but pass it a tcp_handle, not sock */
  139.     int (*writeit)();  /* like write, but pass it a tcp_handle, not sock */
  140. {
  141.     register RECSTREAM *rstrm =
  142.         (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
  143.  
  144.     if (rstrm == NULL) {
  145.         (void)fprintf(stderr, "xdrrec_create: out of memory\n");
  146.         /* 
  147.          *  This is bad.  Should rework xdrrec_create to 
  148.          *  return a handle, and in this case return NULL
  149.          */
  150.         return;
  151.     }
  152.     /*
  153.      * adjust sizes and allocate buffer quad byte aligned
  154.      */
  155.     rstrm->sendsize = sendsize = fix_buf_size(sendsize);
  156.     rstrm->recvsize = recvsize = fix_buf_size(recvsize);
  157.     rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
  158.     if (rstrm->the_buffer == NULL) {
  159.         (void)fprintf(stderr, "xdrrec_create: out of memory\n");
  160.         return;
  161.     }
  162.     for (rstrm->out_base = rstrm->the_buffer;
  163.         (u_int)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
  164.         rstrm->out_base++);
  165.     rstrm->in_base = rstrm->out_base + sendsize;
  166.     /*
  167.      * now the rest ...
  168.      */
  169.     xdrs->x_ops = &xdrrec_ops;
  170.     xdrs->x_private = (caddr_t)rstrm;
  171.     rstrm->tcp_handle = tcp_handle;
  172.     rstrm->readit = readit;
  173.     rstrm->writeit = writeit;
  174.     rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
  175.     rstrm->frag_header = (u_long *)rstrm->out_base;
  176.     rstrm->out_finger += sizeof(u_long);
  177.     rstrm->out_boundry += sendsize;
  178.     rstrm->frag_sent = FALSE;
  179.     rstrm->in_size = recvsize;
  180.     rstrm->in_boundry = rstrm->in_base;
  181.     rstrm->in_finger = (rstrm->in_boundry += recvsize);
  182.     rstrm->fbtbc = 0;
  183.     rstrm->last_frag = TRUE;
  184. }
  185.  
  186.  
  187. /*
  188.  * The reoutines defined below are the xdr ops which will go into the
  189.  * xdr handle filled in by xdrrec_create.
  190.  */
  191.  
  192. static bool_t
  193. xdrrec_getlong(xdrs, lp)
  194.     XDR *xdrs;
  195.     long *lp;
  196. {
  197.     register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
  198.     register long *buflp = (long *)(rstrm->in_finger);
  199.     long mylong;
  200.  
  201.     /* first try the inline, fast case */
  202.     if ((rstrm->fbtbc >= sizeof(long)) &&
  203.         (((int)rstrm->in_boundry - (int)buflp) >= sizeof(long))) {
  204.         *lp = (long)ntohl((u_long)(*buflp));
  205.         rstrm->fbtbc -= sizeof(long);
  206.         rstrm->in_finger += sizeof(long);
  207.     } else {
  208.         if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(long)))
  209.             return (FALSE);
  210.         *lp = (long)ntohl((u_long)mylong);
  211.     }
  212.     return (TRUE);
  213. }
  214.  
  215. static bool_t
  216. xdrrec_putlong(xdrs, lp)
  217.     XDR *xdrs;
  218.     long *lp;
  219. {
  220.     register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
  221.     register long *dest_lp = ((long *)(rstrm->out_finger));
  222.  
  223.     if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
  224.         /*
  225.          * this case should almost never happen so the code is
  226.          * inefficient
  227.          */
  228.         rstrm->out_finger -= sizeof(long);
  229.         rstrm->frag_sent = TRUE;
  230.         if (! flush_out(rstrm, FALSE))
  231.             return (FALSE);
  232.         dest_lp = ((long *)(rstrm->out_finger));
  233.         rstrm->out_finger += sizeof(long);
  234.     }
  235.     *dest_lp = (long)htonl((u_long)(*lp));
  236.     return (TRUE);
  237. }
  238.  
  239. static bool_t  /* must manage buffers, fragments, and records */
  240. xdrrec_getbytes(xdrs, addr, len)
  241.     XDR *xdrs;
  242.     register caddr_t addr;
  243.     register u_int len;
  244. {
  245.     register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
  246.     register int current;
  247.  
  248.     while (len > 0) {
  249.         current = rstrm->fbtbc;
  250.         if (current == 0) {
  251.             if (rstrm->last_frag)
  252.                 return (FALSE);
  253.             if (! set_input_fragment(rstrm))
  254.                 return (FALSE);
  255.             continue;
  256.         }
  257.         current = (len < current) ? len : current;
  258.         if (! get_input_bytes(rstrm, addr, current))
  259.             return (FALSE);
  260.         addr += current; 
  261.         rstrm->fbtbc -= current;
  262.         len -= current;
  263.     }
  264.     return (TRUE);
  265. }
  266.  
  267. static bool_t
  268. xdrrec_putbytes(xdrs, addr, len)
  269.     XDR *xdrs;
  270.     register caddr_t addr;
  271.     register u_int len;
  272. {
  273.     register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
  274.     register int current;
  275.  
  276.     while (len > 0) {
  277.         current = (u_int)rstrm->out_boundry - (u_int)rstrm->out_finger;
  278.         current = (len < current) ? len : current;
  279.         bcopy(addr, rstrm->out_finger, current);
  280.         rstrm->out_finger += current;
  281.         addr += current;
  282.         len -= current;
  283.         if (rstrm->out_finger == rstrm->out_boundry) {
  284.             rstrm->frag_sent = TRUE;
  285.             if (! flush_out(rstrm, FALSE))
  286.                 return (FALSE);
  287.         }
  288.     }
  289.     return (TRUE);
  290. }
  291.  
  292. static u_int
  293. xdrrec_getpos(xdrs)
  294.     register XDR *xdrs;
  295. {
  296.     register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
  297.     register long pos;
  298.  
  299.     pos = lseek((int)rstrm->tcp_handle, (long) 0, 1);
  300.     if (pos != -1)
  301.         switch (xdrs->x_op) {
  302.  
  303.         case XDR_ENCODE:
  304.             pos += rstrm->out_finger - rstrm->out_base;
  305.             break;
  306.  
  307.         case XDR_DECODE:
  308.             pos -= rstrm->in_boundry - rstrm->in_finger;
  309.             break;
  310.  
  311.         default:
  312.             pos = (u_int) -1;
  313.             break;
  314.         }
  315.     return ((u_int) pos);
  316. }
  317.  
  318. static bool_t
  319. xdrrec_setpos(xdrs, pos)
  320.     register XDR *xdrs;
  321.     u_int pos;
  322. {
  323.     register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
  324.     u_int currpos = xdrrec_getpos(xdrs);
  325.     int delta = currpos - pos;
  326.     caddr_t newpos;
  327.  
  328.     if ((int)currpos != -1)
  329.         switch (xdrs->x_op) {
  330.  
  331.         case XDR_ENCODE:
  332.             newpos = rstrm->out_finger - delta;
  333.             if ((newpos > (caddr_t)(rstrm->frag_header)) &&
  334.                 (newpos < rstrm->out_boundry)) {
  335.                 rstrm->out_finger = newpos;
  336.                 return (TRUE);
  337.             }
  338.             break;
  339.  
  340.         case XDR_DECODE:
  341.             newpos = rstrm->in_finger - delta;
  342.             if ((delta < (int)(rstrm->fbtbc)) &&
  343.                 (newpos <= rstrm->in_boundry) &&
  344.                 (newpos >= rstrm->in_base)) {
  345.                 rstrm->in_finger = newpos;
  346.                 rstrm->fbtbc -= delta;
  347.                 return (TRUE);
  348.             }
  349.             break;
  350.         }
  351.     return (FALSE);
  352. }
  353.  
  354. static long *
  355. xdrrec_inline(xdrs, len)
  356.     register XDR *xdrs;
  357.     int len;
  358. {
  359.     register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
  360.     long * buf = NULL;
  361.  
  362.     switch (xdrs->x_op) {
  363.  
  364.     case XDR_ENCODE:
  365.         if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
  366.             buf = (long *) rstrm->out_finger;
  367.             rstrm->out_finger += len;
  368.         }
  369.         break;
  370.  
  371.     case XDR_DECODE:
  372.         if ((len <= rstrm->fbtbc) &&
  373.             ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
  374.             buf = (long *) rstrm->in_finger;
  375.             rstrm->fbtbc -= len;
  376.             rstrm->in_finger += len;
  377.         }
  378.         break;
  379.     }
  380.     return (buf);
  381. }
  382.  
  383. static void
  384. xdrrec_destroy(xdrs)
  385.     register XDR *xdrs;
  386. {
  387.     register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
  388.  
  389.     mem_free(rstrm->the_buffer,
  390.         rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
  391.     mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
  392. }
  393.  
  394.  
  395. /*
  396.  * Exported routines to manage xdr records
  397.  */
  398.  
  399. /*
  400.  * Before reading (deserializing from the stream, one should always call
  401.  * this procedure to guarantee proper record alignment.
  402.  */
  403. bool_t
  404. xdrrec_skiprecord(xdrs)
  405.     XDR *xdrs;
  406. {
  407.     register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
  408.  
  409.     while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
  410.         if (! skip_input_bytes(rstrm, rstrm->fbtbc))
  411.             return (FALSE);
  412.         rstrm->fbtbc = 0;
  413.         if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
  414.             return (FALSE);
  415.     }
  416.     rstrm->last_frag = FALSE;
  417.     return (TRUE);
  418. }
  419.  
  420. /*
  421.  * Look ahead fuction.
  422.  * Returns TRUE iff there is no more input in the buffer 
  423.  * after consuming the rest of the current record.
  424.  */
  425. bool_t
  426. xdrrec_eof(xdrs)
  427.     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(xdrs, sendnow)
  451.     XDR *xdrs;
  452.     bool_t sendnow;
  453. {
  454.     register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
  455.     register u_long len;  /* fragment length */
  456.  
  457.     if (sendnow || rstrm->frag_sent ||
  458.         ((u_long)rstrm->out_finger + sizeof(u_long) >=
  459.         (u_long)rstrm->out_boundry)) {
  460.         rstrm->frag_sent = FALSE;
  461.         return (flush_out(rstrm, TRUE));
  462.     }
  463.     len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
  464.        sizeof(u_long);
  465.     *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
  466.     rstrm->frag_header = (u_long *)rstrm->out_finger;
  467.     rstrm->out_finger += sizeof(u_long);
  468.     return (TRUE);
  469. }
  470.  
  471.  
  472. /*
  473.  * Internal useful routines
  474.  */
  475. static bool_t
  476. flush_out(rstrm, eor)
  477.     register RECSTREAM *rstrm;
  478.     bool_t eor;
  479. {
  480.     register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
  481.     register u_long len = (u_long)(rstrm->out_finger) - 
  482.         (u_long)(rstrm->frag_header) - sizeof(u_long);
  483.  
  484.     *(rstrm->frag_header) = htonl(len | eormask);
  485.     len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
  486.     if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
  487.         != (int)len)
  488.         return (FALSE);
  489.     rstrm->frag_header = (u_long *)rstrm->out_base;
  490.     rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_long);
  491.     return (TRUE);
  492. }
  493.  
  494. static bool_t  /* knows nothing about records!  Only about input buffers */
  495. fill_input_buf(rstrm)
  496.     register RECSTREAM *rstrm;
  497. {
  498.     register caddr_t where;
  499.     u_int i;
  500.     register int len;
  501.  
  502.     where = rstrm->in_base;
  503.     i = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
  504.     where += i;
  505.     len = rstrm->in_size - i;
  506.     if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
  507.         return (FALSE);
  508.     rstrm->in_finger = where;
  509.     where += len;
  510.     rstrm->in_boundry = where;
  511.     return (TRUE);
  512. }
  513.  
  514. static bool_t  /* knows nothing about records!  Only about input buffers */
  515. get_input_bytes(rstrm, addr, len)
  516.     register RECSTREAM *rstrm;
  517.     register caddr_t addr;
  518.     register int len;
  519. {
  520.     register int current;
  521.  
  522.     while (len > 0) {
  523.         current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
  524.         if (current == 0) {
  525.             if (! fill_input_buf(rstrm))
  526.                 return (FALSE);
  527.             continue;
  528.         }
  529.         current = (len < current) ? len : current;
  530.         bcopy(rstrm->in_finger, addr, current);
  531.         rstrm->in_finger += current;
  532.         addr += current;
  533.         len -= current;
  534.     }
  535.     return (TRUE);
  536. }
  537.  
  538. static bool_t  /* next two bytes of the input stream are treated as a header */
  539. set_input_fragment(rstrm)
  540.     register RECSTREAM *rstrm;
  541. {
  542.     u_long header;
  543.  
  544.     if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
  545.         return (FALSE);
  546.     header = (long)ntohl(header);
  547.     rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
  548.     rstrm->fbtbc = header & (~LAST_FRAG);
  549.     return (TRUE);
  550. }
  551.  
  552. static bool_t  /* consumes input bytes; knows nothing about records! */
  553. skip_input_bytes(rstrm, cnt)
  554.     register RECSTREAM *rstrm;
  555.     long cnt;
  556. {
  557.     register int current;
  558.  
  559.     while (cnt > 0) {
  560.         current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
  561.         if (current == 0) {
  562.             if (! fill_input_buf(rstrm))
  563.                 return (FALSE);
  564.             continue;
  565.         }
  566.         current = (cnt < current) ? cnt : current;
  567.         rstrm->in_finger += current;
  568.         cnt -= current;
  569.     }
  570.     return (TRUE);
  571. }
  572.  
  573. static u_int
  574. fix_buf_size(s)
  575.     register u_int s;
  576. {
  577.  
  578.     if (s < 100)
  579.         s = 4000;
  580.     return (RNDUP(s));
  581. }
  582.