Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

/projects/cubeos/src_current/net/rpc/xdr_rec.c

Go to the documentation of this file.
00001 /* @(#)xdr_rec.c        2.2 88/08/01 4.0 RPCSRC */
00002 /*
00003  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00004  * unrestricted use provided that this legend is included on all tape
00005  * media and as a part of the software program in whole or part.  Users
00006  * may copy or modify Sun RPC without charge, but are not authorized
00007  * to license or distribute it to anyone else except as part of a product or
00008  * program developed by the user.
00009  * 
00010  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00011  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00012  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00013  * 
00014  * Sun RPC is provided with no support and without any obligation on the
00015  * part of Sun Microsystems, Inc. to assist in its use, correction,
00016  * modification or enhancement.
00017  * 
00018  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00019  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00020  * OR ANY PART THEREOF.
00021  * 
00022  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00023  * or profits or other special, indirect and consequential damages, even if
00024  * Sun has been advised of the possibility of such damages.
00025  * 
00026  * Sun Microsystems, Inc.
00027  * 2550 Garcia Avenue
00028  * Mountain View, California  94043
00029  */
00030 #if !defined(lint) && defined(SCCSIDS)
00031 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
00032 #endif
00033 
00038 /*
00039  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
00040  * layer above tcp (for rpc's use).
00041  *
00042  * Copyright (C) 1984, Sun Microsystems, Inc.
00043  *
00044  * These routines interface XDRSTREAMS to a tcp/ip connection.
00045  * There is a record marking layer between the xdr stream
00046  * and the tcp transport level.  A record is composed on one or more
00047  * record fragments.  A record fragment is a thirty-two bit header followed
00048  * by n bytes of data, where n is contained in the header.  The header
00049  * is represented as a htonl(u_long).  Thegh order bit encodes
00050  * whether or not the fragment is the last fragment of the record
00051  * (1 => fragment is last, 0 => more fragments to follow. 
00052  * The other 31 bits encode the byte length of the fragment.
00053  */
00054 
00055 #include <stdio.h>
00056 #include <rpc/types.h>
00057 #include <rpc/xdr.h>
00058 #include <netinet/in.h>
00059 #include <string.h>             /* CUBEOS: for bcopy */
00060 
00061 extern long lseek ();
00062 
00063 static u_int fix_buf_size ();
00064 
00065 static bool_t xdrrec_getlong ();
00066 static bool_t xdrrec_putlong ();
00067 static bool_t xdrrec_getbytes ();
00068 static bool_t xdrrec_putbytes ();
00069 static u_int xdrrec_getpos ();
00070 static bool_t xdrrec_setpos ();
00071 static long *xdrrec_inline ();
00072 static void xdrrec_destroy ();
00073 
00074 static struct xdr_ops xdrrec_ops =
00075 {
00076         xdrrec_getlong,
00077         xdrrec_putlong,
00078         xdrrec_getbytes,
00079         xdrrec_putbytes,
00080         xdrrec_getpos,
00081         xdrrec_setpos,
00082         xdrrec_inline,
00083         xdrrec_destroy
00084 };
00085 
00086 /*
00087  * A record is composed of one or more record fragments.
00088  * A record fragment is a two-byte header followed by zero to
00089  * 2**32-1 bytes.  The header is treated as a long unsigned and is
00090  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
00091  * are a byte count of the fragment.  The highest order bit is a boolean:
00092  * 1 => this fragment is the last fragment of the record,
00093  * 0 => this fragment is followed by more fragment(s).
00094  *
00095  * The fragment/record machinery is not general;  it is constructed to
00096  * meet the needs of xdr and rpc based on tcp.
00097  */
00098 
00099 #define LAST_FRAG ((u_long)(1 << 31))
00100 
00101 typedef struct rec_strm {
00102         caddr_t tcp_handle;
00103         caddr_t the_buffer;
00104         /*
00105          * out-goung bits
00106          */
00107         int (*writeit) ();
00108         caddr_t out_base;       /* output buffer (points to frag header) */
00109         caddr_t out_finger;     /* next output position */
00110         caddr_t out_boundry;    /* data cannot up to this address */
00111         u_long *frag_header;    /* beginning of curren fragment */
00112         bool_t frag_sent;       /* true if buffer sent in middle of record */
00113         /*
00114          * in-coming bits
00115          */
00116         int (*readit) ();
00117         u_long in_size;         /* fixed size of the input buffer */
00118         caddr_t in_base;
00119         caddr_t in_finger;      /* location of next byte to be had */
00120         caddr_t in_boundry;     /* can read up to this location */
00121         long fbtbc;             /* fragment bytes to be consumed */
00122         bool_t last_frag;
00123         u_int sendsize;
00124         u_int recvsize;
00125 } RECSTREAM;
00126 
00127 
00128 /*
00129  * Create an xdr handle for xdrrec
00130  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
00131  * send and recv buffer sizes (0 => use default).
00132  * tcp_handle is an opaque handle that is passed as the first parameter to
00133  * the procedures readit and writeit.  Readit and writeit are read and
00134  * write respectively.   They are like the system
00135  * calls expect that they take an opaque handle rather than an fd.
00136  */
00137 void xdrrec_create (xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
00138      register XDR *xdrs;
00139      register u_int sendsize;
00140      register u_int recvsize;
00141      caddr_t tcp_handle;
00142      int (*readit) ();          /* like read, but pass it a tcp_handle, not sock */
00143      int (*writeit) ();         /* like write, but pass it a tcp_handle, not sock */
00144 {
00145         register RECSTREAM *rstrm =
00146         (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
00147 
00148         if (rstrm == NULL) {
00149                 (void) fprintf (stderr, "xdrrec_create: out of memory\n");
00150                 /* 
00151                  *  This is bad.  Should rework xdrrec_create to 
00152                  *  return a handle, and in this case return NULL
00153                  */
00154                 return;
00155         }
00156         /*
00157          * adjust sizes and allocate buffer quad byte aligned
00158          */
00159         rstrm->sendsize = sendsize = fix_buf_size (sendsize);
00160         rstrm->recvsize = recvsize = fix_buf_size (recvsize);
00161         rstrm->the_buffer = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
00162         if (rstrm->the_buffer == NULL) {
00163                 (void) fprintf (stderr, "xdrrec_create: out of memory\n");
00164                 return;
00165         }
00166         for (rstrm->out_base = rstrm->the_buffer;
00167              (u_int) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
00168              rstrm->out_base++);
00169         rstrm->in_base = rstrm->out_base + sendsize;
00170         /*
00171          * now the rest ...
00172          */
00173         xdrs->x_ops = &xdrrec_ops;
00174         xdrs->x_private = (caddr_t) rstrm;
00175         rstrm->tcp_handle = tcp_handle;
00176         rstrm->readit = readit;
00177         rstrm->writeit = writeit;
00178         rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
00179         rstrm->frag_header = (u_long *) rstrm->out_base;
00180         rstrm->out_finger += sizeof (u_long);
00181         rstrm->out_boundry += sendsize;
00182         rstrm->frag_sent = FALSE;
00183         rstrm->in_size = recvsize;
00184         rstrm->in_boundry = rstrm->in_base;
00185         rstrm->in_finger = (rstrm->in_boundry += recvsize);
00186         rstrm->fbtbc = 0;
00187         rstrm->last_frag = TRUE;
00188 }
00189 
00190 
00191 /*
00192  * The reoutines defined below are the xdr ops which will go into the
00193  * xdr handle filled in by xdrrec_create.
00194  */
00195 
00196 static bool_t
00197   xdrrec_getlong (xdrs, lp)
00198      XDR *xdrs;
00199      long *lp;
00200 {
00201         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
00202         register long *buflp = (long *) (rstrm->in_finger);
00203         long mylong;
00204 
00205         /* first try the inline, fast case */
00206         if ((rstrm->fbtbc >= sizeof (long)) &&
00207               (((int) rstrm->in_boundry - (int) buflp) >= sizeof (long))) {
00208                 *lp = (long) ntohl ((u_long) (*buflp));
00209                 rstrm->fbtbc -= sizeof (long);
00210                 rstrm->in_finger += sizeof (long);
00211         } else {
00212                 if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong, sizeof (long)))
00213                           return (FALSE);
00214                 *lp = (long) ntohl ((u_long) mylong);
00215         }
00216         return (TRUE);
00217 }
00218 
00219 static bool_t
00220   xdrrec_putlong (xdrs, lp)
00221      XDR *xdrs;
00222      long *lp;
00223 {
00224         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
00225         register long *dest_lp = ((long *) (rstrm->out_finger));
00226 
00227         if ((rstrm->out_finger += sizeof (long)) > rstrm->out_boundry) {
00228                 /*
00229                  * this case should almost never happen so the code is
00230                  * inefficient
00231                  */
00232                 rstrm->out_finger -= sizeof (long);
00233                 rstrm->frag_sent = TRUE;
00234                 if (!flush_out (rstrm, FALSE))
00235                         return (FALSE);
00236                 dest_lp = ((long *) (rstrm->out_finger));
00237                 rstrm->out_finger += sizeof (long);
00238         }
00239         *dest_lp = (long) htonl ((u_long) (*lp));
00240         return (TRUE);
00241 }
00242 
00243 static bool_t                   /* must manage buffers, fragments, and records */
00244   xdrrec_getbytes (xdrs, addr, len)
00245      XDR *xdrs;
00246      register caddr_t addr;
00247      register u_int len;
00248 {
00249         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
00250         register int current;
00251 
00252         while (len > 0) {
00253                 current = rstrm->fbtbc;
00254                 if (current == 0) {
00255                         if (rstrm->last_frag)
00256                                 return (FALSE);
00257                         if (!set_input_fragment (rstrm))
00258                                 return (FALSE);
00259                         continue;
00260                 }
00261                 current = (len < current) ? len : current;
00262                 if (!get_input_bytes (rstrm, addr, current))
00263                         return (FALSE);
00264                 addr += current;
00265                 rstrm->fbtbc -= current;
00266                 len -= current;
00267         }
00268         return (TRUE);
00269 }
00270 
00271 static bool_t
00272   xdrrec_putbytes (xdrs, addr, len)
00273      XDR *xdrs;
00274      register caddr_t addr;
00275      register u_int len;
00276 {
00277         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
00278         register int current;
00279 
00280         while (len > 0) {
00281                 current = (u_int) rstrm->out_boundry - (u_int) rstrm->out_finger;
00282                 current = (len < current) ? len : current;
00283                 bcopy (addr, rstrm->out_finger, current);
00284                 rstrm->out_finger += current;
00285                 addr += current;
00286                 len -= current;
00287                 if (rstrm->out_finger == rstrm->out_boundry) {
00288                         rstrm->frag_sent = TRUE;
00289                         if (!flush_out (rstrm, FALSE))
00290                                 return (FALSE);
00291                 }
00292         }
00293         return (TRUE);
00294 }
00295 
00296 static u_int
00297   xdrrec_getpos (xdrs)
00298      register XDR *xdrs;
00299 {
00300         register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
00301         register long pos;
00302 
00303         pos = lseek ((int) rstrm->tcp_handle, (long) 0, 1);
00304         if (pos != -1)
00305                 switch (xdrs->x_op) {
00306 
00307                 case XDR_ENCODE:
00308                         pos += rstrm->out_finger - rstrm->out_base;
00309                         break;
00310 
00311                 case XDR_DECODE:
00312                         pos -= rstrm->in_boundry - rstrm->in_finger;
00313                         break;
00314 
00315                 default:
00316                         pos = (u_int) - 1;
00317                         break;
00318                 }
00319         return ((u_int) pos);
00320 }
00321 
00322 static bool_t
00323   xdrrec_setpos (xdrs, pos)
00324      register XDR *xdrs;
00325      u_int pos;
00326 {
00327         register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
00328         u_int currpos = xdrrec_getpos (xdrs);
00329         int delta = currpos - pos;
00330         caddr_t newpos;
00331 
00332         if ((int) currpos != -1)
00333                 switch (xdrs->x_op) {
00334 
00335                 case XDR_ENCODE:
00336                         newpos = rstrm->out_finger - delta;
00337                         if ((newpos > (caddr_t) (rstrm->frag_header)) &&
00338                             (newpos < rstrm->out_boundry)) {
00339                                 rstrm->out_finger = newpos;
00340                                 return (TRUE);
00341                         }
00342                         break;
00343 
00344                 case XDR_DECODE:
00345                         newpos = rstrm->in_finger - delta;
00346                         if ((delta < (int) (rstrm->fbtbc)) &&
00347                             (newpos <= rstrm->in_boundry) &&
00348                             (newpos >= rstrm->in_base)) {
00349                                 rstrm->in_finger = newpos;
00350                                 rstrm->fbtbc -= delta;
00351                                 return (TRUE);
00352                         }
00353                         break;
00354                 }
00355         return (FALSE);
00356 }
00357 
00358 static long *
00359   xdrrec_inline (xdrs, len)
00360      register XDR *xdrs;
00361      int len;
00362 {
00363         register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
00364         long *buf = NULL;
00365 
00366         switch (xdrs->x_op) {
00367 
00368         case XDR_ENCODE:
00369                 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
00370                         buf = (long *) rstrm->out_finger;
00371                         rstrm->out_finger += len;
00372                 }
00373                 break;
00374 
00375         case XDR_DECODE:
00376                 if ((len <= rstrm->fbtbc) &&
00377                     ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
00378                         buf = (long *) rstrm->in_finger;
00379                         rstrm->fbtbc -= len;
00380                         rstrm->in_finger += len;
00381                 }
00382                 break;
00383         }
00384         return (buf);
00385 }
00386 
00387 static void xdrrec_destroy (xdrs)
00388      register XDR *xdrs;
00389 {
00390         register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
00391 
00392         mem_free (rstrm->the_buffer,
00393                   rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
00394         mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
00395 }
00396 
00397 
00398 /*
00399  * Exported routines to manage xdr records
00400  */
00401 
00402 /*
00403  * Before reading (deserializing from the stream, one should always call
00404  * this procedure to guarantee proper record alignment.
00405  */
00406 bool_t
00407 xdrrec_skiprecord (xdrs)
00408      XDR *xdrs;
00409 {
00410         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
00411 
00412         while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
00413                 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
00414                         return (FALSE);
00415                 rstrm->fbtbc = 0;
00416                 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
00417                         return (FALSE);
00418         }
00419         rstrm->last_frag = FALSE;
00420         return (TRUE);
00421 }
00422 
00423 /*
00424  * Look ahead fuction.
00425  * Returns TRUE iff there is no more input in the buffer 
00426  * after consuming the rest of the current record.
00427  */
00428 bool_t
00429 xdrrec_eof (xdrs)
00430      XDR *xdrs;
00431 {
00432         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
00433 
00434         while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
00435                 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
00436                         return (TRUE);
00437                 rstrm->fbtbc = 0;
00438                 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
00439                         return (TRUE);
00440         }
00441         if (rstrm->in_finger == rstrm->in_boundry)
00442                 return (TRUE);
00443         return (FALSE);
00444 }
00445 
00446 /*
00447  * The client must tell the package when an end-of-record has occurred.
00448  * The second paraemters tells whether the record should be flushed to the
00449  * (output) tcp stream.  (This let's the package support batched or
00450  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
00451  */
00452 bool_t
00453 xdrrec_endofrecord (xdrs, sendnow)
00454      XDR *xdrs;
00455      bool_t sendnow;
00456 {
00457         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
00458         register u_long len;    /* fragment length */
00459 
00460         if (sendnow || rstrm->frag_sent ||
00461             ((u_long) rstrm->out_finger + sizeof (u_long) >=
00462              (u_long) rstrm->out_boundry)) {
00463                 rstrm->frag_sent = FALSE;
00464                 return (flush_out (rstrm, TRUE));
00465         }
00466         len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->frag_header) -
00467                 sizeof (u_long);
00468         *(rstrm->frag_header) = htonl ((u_long) len | LAST_FRAG);
00469         rstrm->frag_header = (u_long *) rstrm->out_finger;
00470         rstrm->out_finger += sizeof (u_long);
00471         return (TRUE);
00472 }
00473 
00474 
00475 /*
00476  * Internal useful routines
00477  */
00478 static bool_t
00479   flush_out (rstrm, eor)
00480      register RECSTREAM *rstrm;
00481      bool_t eor;
00482 {
00483         register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
00484         register u_long len = (u_long) (rstrm->out_finger) -
00485         (u_long) (rstrm->frag_header) - sizeof (u_long);
00486 
00487         *(rstrm->frag_header) = htonl (len | eormask);
00488         len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->out_base);
00489         if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
00490             != (int) len)
00491                 return (FALSE);
00492         rstrm->frag_header = (u_long *) rstrm->out_base;
00493         rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof (u_long);
00494         return (TRUE);
00495 }
00496 
00497 static bool_t                   /* knows nothing about records!  Only about input buffers */
00498   fill_input_buf (rstrm)
00499      register RECSTREAM *rstrm;
00500 {
00501         register caddr_t where;
00502         u_int i;
00503         register int len;
00504 
00505         where = rstrm->in_base;
00506         i = (u_int) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
00507         where += i;
00508         len = rstrm->in_size - i;
00509         if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
00510                 return (FALSE);
00511         rstrm->in_finger = where;
00512         where += len;
00513         rstrm->in_boundry = where;
00514         return (TRUE);
00515 }
00516 
00517 static bool_t                   /* knows nothing about records!  Only about input buffers */
00518   get_input_bytes (rstrm, addr, len)
00519      register RECSTREAM *rstrm;
00520      register caddr_t addr;
00521      register int len;
00522 {
00523         register int current;
00524 
00525         while (len > 0) {
00526                 current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
00527                 if (current == 0) {
00528                         if (!fill_input_buf (rstrm))
00529                                 return (FALSE);
00530                         continue;
00531                 }
00532                 current = (len < current) ? len : current;
00533                 bcopy (rstrm->in_finger, addr, current);
00534                 rstrm->in_finger += current;
00535                 addr += current;
00536                 len -= current;
00537         }
00538         return (TRUE);
00539 }
00540 
00541 static bool_t                   /* next two bytes of the input stream are treated as a header */
00542   set_input_fragment (rstrm)
00543      register RECSTREAM *rstrm;
00544 {
00545         u_long header;
00546 
00547         if (!get_input_bytes (rstrm, (caddr_t) & header, sizeof (header)))
00548                 return (FALSE);
00549         header = (long) ntohl (header);
00550         rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
00551         rstrm->fbtbc = header & (~LAST_FRAG);
00552         return (TRUE);
00553 }
00554 
00555 static bool_t                   /* consumes input bytes; knows nothing about records! */
00556   skip_input_bytes (rstrm, cnt)
00557      register RECSTREAM *rstrm;
00558      long cnt;
00559 {
00560         register int current;
00561 
00562         while (cnt > 0) {
00563                 current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
00564                 if (current == 0) {
00565                         if (!fill_input_buf (rstrm))
00566                                 return (FALSE);
00567                         continue;
00568                 }
00569                 current = (cnt < current) ? cnt : current;
00570                 rstrm->in_finger += current;
00571                 cnt -= current;
00572         }
00573         return (TRUE);
00574 }
00575 
00576 static u_int
00577   fix_buf_size (s)
00578      register u_int s;
00579 {
00580 
00581         if (s < 100)
00582                 s = 4000;
00583         return (RNDUP (s));
00584 }

Generated on Thu Feb 20 15:38:46 2003 for cubeOS by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002