home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / rpc3.9 / part02 / clnt_raw.c next >
Encoding:
C/C++ Source or Header  |  1988-02-27  |  5.7 KB  |  240 lines

  1. /* @(#)clnt_raw.c    1.2 87/11/09 3.9 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[] = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * clnt_raw.c
  36.  *
  37.  * Copyright (C) 1984, Sun Microsystems, Inc.
  38.  *
  39.  * Memory based rpc for simple testing and timing.
  40.  * Interface to create an rpc client and server in the same process.
  41.  * This lets us similate rpc and get round trip overhead, without
  42.  * any interference from the kernal.
  43.  */
  44.  
  45. #include <rpc/rpc.h>
  46. #include <sys/time.h>
  47.  
  48. #define MCALL_MSG_SIZE 24
  49.  
  50. /*
  51.  * This is the "network" we will be moving stuff over.
  52.  */
  53. static struct clntraw_private {
  54.     CLIENT    client_object;
  55.     XDR    xdr_stream;
  56.     char    _raw_buf[UDPMSGSIZE];
  57.     char    mashl_callmsg[MCALL_MSG_SIZE];
  58.     u_int    mcnt;
  59. } *clntraw_private;
  60.  
  61. static enum clnt_stat    clntraw_call();
  62. static void        clntraw_abort();
  63. static void        clntraw_geterr();
  64. static bool_t        clntraw_freeres();
  65. static bool_t        clntraw_control();
  66. static void        clntraw_destroy();
  67.  
  68. static struct clnt_ops client_ops = {
  69.     clntraw_call,
  70.     clntraw_abort,
  71.     clntraw_geterr,
  72.     clntraw_freeres,
  73.     clntraw_destroy,
  74.     clntraw_control
  75. };
  76.  
  77. void    svc_getreq();
  78.  
  79. /*
  80.  * Create a client handle for memory based rpc.
  81.  */
  82. CLIENT *
  83. clntraw_create(prog, vers)
  84.     u_long prog;
  85.     u_long vers;
  86. {
  87.     register struct clntraw_private *clp = clntraw_private;
  88.     struct rpc_msg call_msg;
  89.     XDR *xdrs = &clp->xdr_stream;
  90.     CLIENT    *client = &clp->client_object;
  91.  
  92.     if (clp == 0) {
  93.         clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
  94.         if (clp == 0)
  95.             return (0);
  96.         clntraw_private = clp;
  97.     }
  98.     /*
  99.      * pre-serialize the staic part of the call msg and stash it away
  100.      */
  101.     call_msg.rm_direction = CALL;
  102.     call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  103.     call_msg.rm_call.cb_prog = prog;
  104.     call_msg.rm_call.cb_vers = vers;
  105.     xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 
  106.     if (! xdr_callhdr(xdrs, &call_msg)) {
  107.         perror("clnt_raw.c - Fatal header serialization error.");
  108.     }
  109.     clp->mcnt = XDR_GETPOS(xdrs);
  110.     XDR_DESTROY(xdrs);
  111.  
  112.     /*
  113.      * Set xdrmem for client/server shared buffer
  114.      */
  115.     xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
  116.  
  117.     /*
  118.      * create client handle
  119.      */
  120.     client->cl_ops = &client_ops;
  121.     client->cl_auth = authnone_create();
  122.     return (client);
  123. }
  124.  
  125. static enum clnt_stat 
  126. clntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
  127.     CLIENT *h;
  128.     u_long proc;
  129.     xdrproc_t xargs;
  130.     caddr_t argsp;
  131.     xdrproc_t xresults;
  132.     caddr_t resultsp;
  133.     struct timeval timeout;
  134. {
  135.     register struct clntraw_private *clp = clntraw_private;
  136.     register XDR *xdrs = &clp->xdr_stream;
  137.     struct rpc_msg msg;
  138.     enum clnt_stat status;
  139.     struct rpc_err error;
  140.  
  141.     if (clp == 0)
  142.         return (RPC_FAILED);
  143. call_again:
  144.     /*
  145.      * send request
  146.      */
  147.     xdrs->x_op = XDR_ENCODE;
  148.     XDR_SETPOS(xdrs, 0);
  149.     ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ;
  150.     if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
  151.         (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
  152.         (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
  153.         (! (*xargs)(xdrs, argsp))) {
  154.         return (RPC_CANTENCODEARGS);
  155.     }
  156.     (void)XDR_GETPOS(xdrs);  /* called just to cause overhead */
  157.  
  158.     /*
  159.      * We have to call server input routine here because this is
  160.      * all going on in one process. Yuk.
  161.      */
  162.     svc_getreq(1);
  163.  
  164.     /*
  165.      * get results
  166.      */
  167.     xdrs->x_op = XDR_DECODE;
  168.     XDR_SETPOS(xdrs, 0);
  169.     msg.acpted_rply.ar_verf = _null_auth;
  170.     msg.acpted_rply.ar_results.where = resultsp;
  171.     msg.acpted_rply.ar_results.proc = xresults;
  172.     if (! xdr_replymsg(xdrs, &msg))
  173.         return (RPC_CANTDECODERES);
  174.     _seterr_reply(&msg, &error);
  175.     status = error.re_status;
  176.  
  177.     if (status == RPC_SUCCESS) {
  178.         if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
  179.             status = RPC_AUTHERROR;
  180.         }
  181.     }  /* end successful completion */
  182.     else {
  183.         if (AUTH_REFRESH(h->cl_auth))
  184.             goto call_again;
  185.     }  /* end of unsuccessful completion */
  186.  
  187.     if (status == RPC_SUCCESS) {
  188.         if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
  189.             status = RPC_AUTHERROR;
  190.         }
  191.         if (msg.acpted_rply.ar_verf.oa_base != NULL) {
  192.             xdrs->x_op = XDR_FREE;
  193.             (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
  194.         }
  195.     }
  196.  
  197.     return (status);
  198. }
  199.  
  200. static void
  201. clntraw_geterr()
  202. {
  203. }
  204.  
  205.  
  206. static bool_t
  207. clntraw_freeres(cl, xdr_res, res_ptr)
  208.     CLIENT *cl;
  209.     xdrproc_t xdr_res;
  210.     caddr_t res_ptr;
  211. {
  212.     register struct clntraw_private *clp = clntraw_private;
  213.     register XDR *xdrs = &clp->xdr_stream;
  214.     bool_t rval;
  215.  
  216.     if (clp == 0)
  217.     {
  218.         rval = (bool_t) RPC_FAILED;
  219.         return (rval);
  220.     }
  221.     xdrs->x_op = XDR_FREE;
  222.     return ((*xdr_res)(xdrs, res_ptr));
  223. }
  224.  
  225. static void
  226. clntraw_abort()
  227. {
  228. }
  229.  
  230. static bool_t
  231. clntraw_control()
  232. {
  233.     return (FALSE);
  234. }
  235.  
  236. static void
  237. clntraw_destroy()
  238. {
  239. }
  240.