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

  1. /* @(#)clnt_udp.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_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
  36.  *
  37.  * Copyright (C) 1984, Sun Microsystems, Inc.
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <rpc/rpc.h>
  42. #include <sys/socket.h>
  43. #include <sys/time.h>
  44. #include <sys/ioctl.h>
  45. #include <netdb.h>
  46. #include <errno.h>
  47. #include <rpc/pmap_clnt.h>
  48.  
  49. extern int errno;
  50.  
  51. /*
  52.  * UDP bases client side rpc operations
  53.  */
  54. static enum clnt_stat    clntudp_call();
  55. static void        clntudp_abort();
  56. static void        clntudp_geterr();
  57. static bool_t        clntudp_freeres();
  58. static bool_t           clntudp_control();
  59. static void        clntudp_destroy();
  60.  
  61. static struct clnt_ops udp_ops = {
  62.     clntudp_call,
  63.     clntudp_abort,
  64.     clntudp_geterr,
  65.     clntudp_freeres,
  66.     clntudp_destroy,
  67.     clntudp_control
  68. };
  69.  
  70. /* 
  71.  * Private data kept per client handle
  72.  */
  73. struct cu_data {
  74.     int           cu_sock;
  75.     bool_t           cu_closeit;
  76.     struct sockaddr_in cu_raddr;
  77.     int           cu_rlen;
  78.     struct timeval       cu_wait;
  79.     struct timeval     cu_total;
  80.     struct rpc_err       cu_error;
  81.     XDR           cu_outxdrs;
  82.     u_int           cu_xdrpos;
  83.     u_int           cu_sendsz;
  84.     char           *cu_outbuf;
  85.     u_int           cu_recvsz;
  86.     char           cu_inbuf[1];
  87. };
  88.  
  89. /*
  90.  * Create a UDP based client handle.
  91.  * If *sockp<0, *sockp is set to a newly created UPD socket.
  92.  * If raddr->sin_port is 0 a binder on the remote machine
  93.  * is consulted for the correct port number.
  94.  * NB: It is the clients responsibility to close *sockp.
  95.  * NB: The rpch->cl_auth is initialized to null authentication.
  96.  *     Caller may wish to set this something more useful.
  97.  *
  98.  * wait is the amount of time used between retransmitting a call if
  99.  * no response has been heard;  retransmition occurs until the actual
  100.  * rpc call times out.
  101.  *
  102.  * sendsz and recvsz are the maximum allowable packet sizes that can be
  103.  * sent and received.
  104.  */
  105. CLIENT *
  106. clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
  107.     struct sockaddr_in *raddr;
  108.     u_long program;
  109.     u_long version;
  110.     struct timeval wait;
  111.     register int *sockp;
  112.     u_int sendsz;
  113.     u_int recvsz;
  114. {
  115.     CLIENT *cl;
  116.     register struct cu_data *cu;
  117.     struct timeval now;
  118.     struct rpc_msg call_msg;
  119.  
  120.     cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
  121.     if (cl == NULL) {
  122.         (void) fprintf(stderr, "clntudp_create: out of memory\n");
  123.         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  124.         rpc_createerr.cf_error.re_errno = errno;
  125.         goto fooy;
  126.     }
  127.     sendsz = ((sendsz + 3) / 4) * 4;
  128.     recvsz = ((recvsz + 3) / 4) * 4;
  129.     cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
  130.     if (cu == NULL) {
  131.         (void) fprintf(stderr, "clntudp_create: out of memory\n");
  132.         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  133.         rpc_createerr.cf_error.re_errno = errno;
  134.         goto fooy;
  135.     }
  136.     cu->cu_outbuf = &cu->cu_inbuf[recvsz];
  137.  
  138.     (void)gettimeofday(&now, (struct timezone *)0);
  139.     if (raddr->sin_port == 0) {
  140.         u_short port;
  141.         if ((port =
  142.             pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
  143.             goto fooy;
  144.         }
  145.         raddr->sin_port = htons(port);
  146.     }
  147.     cl->cl_ops = &udp_ops;
  148.     cl->cl_private = (caddr_t)cu;
  149.     cu->cu_raddr = *raddr;
  150.     cu->cu_rlen = sizeof (cu->cu_raddr);
  151.     cu->cu_wait = wait;
  152.     cu->cu_total.tv_sec = -1;
  153.     cu->cu_total.tv_usec = -1;
  154.     cu->cu_sendsz = sendsz;
  155.     cu->cu_recvsz = recvsz;
  156.     call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
  157.     call_msg.rm_direction = CALL;
  158.     call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  159.     call_msg.rm_call.cb_prog = program;
  160.     call_msg.rm_call.cb_vers = version;
  161.     xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
  162.         sendsz, XDR_ENCODE);
  163.     if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
  164.         goto fooy;
  165.     }
  166.     cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
  167.     if (*sockp < 0) {
  168.         int dontblock = 1;
  169.  
  170.         *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  171.         if (*sockp < 0) {
  172.             rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  173.             rpc_createerr.cf_error.re_errno = errno;
  174.             goto fooy;
  175.         }
  176.         /* attempt to bind to prov port */
  177.         (void)bindresvport(*sockp, (struct sockaddr_in *)0);
  178.         /* the sockets rpc controls are non-blocking */
  179.         (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
  180.         cu->cu_closeit = TRUE;
  181.     } else {
  182.         cu->cu_closeit = FALSE;
  183.     }
  184.     cu->cu_sock = *sockp;
  185.     cl->cl_auth = authnone_create();
  186.     return (cl);
  187. fooy:
  188.     if (cu)
  189.         mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
  190.     if (cl)
  191.         mem_free((caddr_t)cl, sizeof(CLIENT));
  192.     return ((CLIENT *)NULL);
  193. }
  194.  
  195. CLIENT *
  196. clntudp_create(raddr, program, version, wait, sockp)
  197.     struct sockaddr_in *raddr;
  198.     u_long program;
  199.     u_long version;
  200.     struct timeval wait;
  201.     register int *sockp;
  202. {
  203.  
  204.     return(clntudp_bufcreate(raddr, program, version, wait, sockp,
  205.         UDPMSGSIZE, UDPMSGSIZE));
  206. }
  207.  
  208. static enum clnt_stat 
  209. clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
  210.     register CLIENT    *cl;        /* client handle */
  211.     u_long        proc;        /* procedure number */
  212.     xdrproc_t    xargs;        /* xdr routine for args */
  213.     caddr_t        argsp;        /* pointer to args */
  214.     xdrproc_t    xresults;    /* xdr routine for results */
  215.     caddr_t        resultsp;    /* pointer to results */
  216.     struct timeval    utimeout;    /* seconds to wait before giving up */
  217. {
  218.     register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  219.     register XDR *xdrs;
  220.     register int outlen;
  221.     register int inlen;
  222.     int fromlen;
  223. #ifdef FD_SETSIZE
  224.     fd_set readfds;
  225.     fd_set mask;
  226. #else
  227.     int readfds;
  228.     register int mask;
  229. #endif /* def FD_SETSIZE */
  230.     struct sockaddr_in from;
  231.     struct rpc_msg reply_msg;
  232.     XDR reply_xdrs;
  233.     struct timeval time_waited;
  234.     bool_t ok;
  235.     int nrefreshes = 2;    /* number of times to refresh cred */
  236.     struct timeval timeout;
  237.  
  238.     if (cu->cu_total.tv_usec == -1) {
  239.         timeout = utimeout;     /* use supplied timeout */
  240.     } else {
  241.         timeout = cu->cu_total; /* use default timeout */
  242.     }
  243.  
  244.     time_waited.tv_sec = 0;
  245.     time_waited.tv_usec = 0;
  246. call_again:
  247.     xdrs = &(cu->cu_outxdrs);
  248.     xdrs->x_op = XDR_ENCODE;
  249.     XDR_SETPOS(xdrs, cu->cu_xdrpos);
  250.     /*
  251.      * the transaction is the first thing in the out buffer
  252.      */
  253.     (*(u_short *)(cu->cu_outbuf))++;
  254.     if ((! XDR_PUTLONG(xdrs, (long *)&proc)) ||
  255.         (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
  256.         (! (*xargs)(xdrs, argsp)))
  257.         return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
  258.     outlen = (int)XDR_GETPOS(xdrs);
  259.  
  260. send_again:
  261.     if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
  262.         (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen)
  263.         != outlen) {
  264.         cu->cu_error.re_errno = errno;
  265.         return (cu->cu_error.re_status = RPC_CANTSEND);
  266.     }
  267.  
  268.     /*
  269.      * Hack to provide rpc-based message passing
  270.      */
  271.     if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
  272.         return (cu->cu_error.re_status = RPC_TIMEDOUT);
  273.     }
  274.     /*
  275.      * sub-optimal code appears here because we have
  276.      * some clock time to spare while the packets are in flight.
  277.      * (We assume that this is actually only executed once.)
  278.      */
  279.     reply_msg.acpted_rply.ar_verf = _null_auth;
  280.     reply_msg.acpted_rply.ar_results.where = resultsp;
  281.     reply_msg.acpted_rply.ar_results.proc = xresults;
  282. #ifdef FD_SETSIZE
  283.     FD_ZERO(&mask);
  284.     FD_SET(cu->cu_sock, &mask);
  285. #else
  286.     mask = 1 << cu->cu_sock;
  287. #endif /* def FD_SETSIZE */
  288.     for (;;) {
  289.         readfds = mask;
  290.         switch (select(_rpc_dtablesize(), &readfds, (int *)NULL, 
  291.                    (int *)NULL, &(cu->cu_wait))) {
  292.  
  293.         case 0:
  294.             time_waited.tv_sec += cu->cu_wait.tv_sec;
  295.             time_waited.tv_usec += cu->cu_wait.tv_usec;
  296.             while (time_waited.tv_usec >= 1000000) {
  297.                 time_waited.tv_sec++;
  298.                 time_waited.tv_usec -= 1000000;
  299.             }
  300.             if ((time_waited.tv_sec < timeout.tv_sec) ||
  301.                 ((time_waited.tv_sec == timeout.tv_sec) &&
  302.                 (time_waited.tv_usec < timeout.tv_usec)))
  303.                 goto send_again;    
  304.             return (cu->cu_error.re_status = RPC_TIMEDOUT);
  305.  
  306.         /*
  307.          * buggy in other cases because time_waited is not being
  308.          * updated.
  309.          */
  310.         case -1:
  311.             if (errno == EINTR)
  312.                 continue;    
  313.             cu->cu_error.re_errno = errno;
  314.             return (cu->cu_error.re_status = RPC_CANTRECV);
  315.         }
  316.         do {
  317.             fromlen = sizeof(struct sockaddr);
  318.             inlen = recvfrom(cu->cu_sock, cu->cu_inbuf, 
  319.                 (int) cu->cu_recvsz, 0,
  320.                 (struct sockaddr *)&from, &fromlen);
  321.         } while (inlen < 0 && errno == EINTR);
  322.         if (inlen < 0) {
  323.             if (errno == EWOULDBLOCK)
  324.                 continue;    
  325.             cu->cu_error.re_errno = errno;
  326.             return (cu->cu_error.re_status = RPC_CANTRECV);
  327.         }
  328.         if (inlen < sizeof(u_long))
  329.             continue;    
  330.         /* see if reply transaction id matches sent id */
  331.         if (*((u_long *)(cu->cu_inbuf)) != *((u_long *)(cu->cu_outbuf)))
  332.             continue;    
  333.         /* we now assume we have the proper reply */
  334.         break;
  335.     }
  336.  
  337.     /*
  338.      * now decode and validate the response
  339.      */
  340.     xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
  341.     ok = xdr_replymsg(&reply_xdrs, &reply_msg);
  342.     /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
  343.     if (ok) {
  344.         _seterr_reply(&reply_msg, &(cu->cu_error));
  345.         if (cu->cu_error.re_status == RPC_SUCCESS) {
  346.             if (! AUTH_VALIDATE(cl->cl_auth,
  347.                 &reply_msg.acpted_rply.ar_verf)) {
  348.                 cu->cu_error.re_status = RPC_AUTHERROR;
  349.                 cu->cu_error.re_why = AUTH_INVALIDRESP;
  350.             }
  351.             if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
  352.                 xdrs->x_op = XDR_FREE;
  353.                 (void)xdr_opaque_auth(xdrs,
  354.                     &(reply_msg.acpted_rply.ar_verf));
  355.             } 
  356.         }  /* end successful completion */
  357.         else {
  358.             /* maybe our credentials need to be refreshed ... */
  359.             if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
  360.                 nrefreshes--;
  361.                 goto call_again;
  362.             }
  363.         }  /* end of unsuccessful completion */
  364.     }  /* end of valid reply message */
  365.     else {
  366.         cu->cu_error.re_status = RPC_CANTDECODERES;
  367.     }
  368.     return (cu->cu_error.re_status);
  369. }
  370.  
  371. static void
  372. clntudp_geterr(cl, errp)
  373.     CLIENT *cl;
  374.     struct rpc_err *errp;
  375. {
  376.     register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  377.  
  378.     *errp = cu->cu_error;
  379. }
  380.  
  381.  
  382. static bool_t
  383. clntudp_freeres(cl, xdr_res, res_ptr)
  384.     CLIENT *cl;
  385.     xdrproc_t xdr_res;
  386.     caddr_t res_ptr;
  387. {
  388.     register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  389.     register XDR *xdrs = &(cu->cu_outxdrs);
  390.  
  391.     xdrs->x_op = XDR_FREE;
  392.     return ((*xdr_res)(xdrs, res_ptr));
  393. }
  394.  
  395. static void 
  396. clntudp_abort(/*h*/)
  397.     /*CLIENT *h;*/
  398. {
  399. }
  400.  
  401. static bool_t
  402. clntudp_control(cl, request, info)
  403.     CLIENT *cl;
  404.     int request;
  405.     char *info;
  406. {
  407.     register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  408.  
  409.     switch (request) {
  410.     case CLSET_TIMEOUT:
  411.         cu->cu_total = *(struct timeval *)info;
  412.         break;
  413.     case CLGET_TIMEOUT:
  414.         *(struct timeval *)info = cu->cu_total;
  415.         break;
  416.     case CLSET_RETRY_TIMEOUT:
  417.         cu->cu_wait = *(struct timeval *)info;
  418.         break;
  419.     case CLGET_RETRY_TIMEOUT:
  420.         *(struct timeval *)info = cu->cu_wait;
  421.         break;
  422.     case CLGET_SERVER_ADDR:
  423.         *(struct sockaddr_in *)info = cu->cu_raddr;
  424.         break;
  425.     default:
  426.         return (FALSE);
  427.     }
  428.     return (TRUE);
  429. }
  430.     
  431. static void
  432. clntudp_destroy(cl)
  433.     CLIENT *cl;
  434. {
  435.     register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  436.  
  437.     if (cu->cu_closeit) {
  438.         (void)close(cu->cu_sock);
  439.     }
  440.     XDR_DESTROY(&(cu->cu_outxdrs));
  441.     mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
  442.     mem_free((caddr_t)cl, sizeof(CLIENT));
  443. }
  444.