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

  1. /* @(#)pmap_rmt.c    1.4 87/11/30 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[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * pmap_rmt.c
  36.  * Client interface to pmap rpc service.
  37.  * remote call and broadcast service
  38.  *
  39.  * Copyright (C) 1984, Sun Microsystems, Inc.
  40.  */
  41.  
  42. #include <rpc/rpc.h>
  43. #include <rpc/pmap_prot.h>
  44. #include <rpc/pmap_clnt.h>
  45. #include <rpc/pmap_rmt.h>
  46. #include <sys/socket.h>
  47. #include <sys/time.h>
  48. #include <stdio.h>
  49. #include <errno.h>
  50. #include <net/if.h>
  51. #include <sys/ioctl.h>
  52. #include <arpa/inet.h>
  53. #define MAX_BROADCAST_SIZE 1400
  54.  
  55. extern int errno;
  56. static struct timeval timeout = { 3, 0 };
  57.  
  58.  
  59. /*
  60.  * pmapper remote-call-service interface.
  61.  * This routine is used to call the pmapper remote call service
  62.  * which will look up a service program in the port maps, and then
  63.  * remotely call that routine with the given parameters.  This allows
  64.  * programs to do a lookup and call in one step.
  65. */
  66. enum clnt_stat
  67. pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
  68.     struct sockaddr_in *addr;
  69.     u_long prog, vers, proc;
  70.     xdrproc_t xdrargs, xdrres;
  71.     caddr_t argsp, resp;
  72.     struct timeval tout;
  73.     u_long *port_ptr;
  74. {
  75.     int socket = -1;
  76.     register CLIENT *client;
  77.     struct rmtcallargs a;
  78.     struct rmtcallres r;
  79.     enum clnt_stat stat;
  80.  
  81.     addr->sin_port = htons(PMAPPORT);
  82.     client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
  83.     if (client != (CLIENT *)NULL) {
  84.         a.prog = prog;
  85.         a.vers = vers;
  86.         a.proc = proc;
  87.         a.args_ptr = argsp;
  88.         a.xdr_args = xdrargs;
  89.         r.port_ptr = port_ptr;
  90.         r.results_ptr = resp;
  91.         r.xdr_results = xdrres;
  92.         stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
  93.             xdr_rmtcallres, &r, tout);
  94.         CLNT_DESTROY(client);
  95.     } else {
  96.         stat = RPC_FAILED;
  97.     }
  98.     (void)close(socket);
  99.     addr->sin_port = 0;
  100.     return (stat);
  101. }
  102.  
  103.  
  104. /*
  105.  * XDR remote call arguments
  106.  * written for XDR_ENCODE direction only
  107.  */
  108. bool_t
  109. xdr_rmtcall_args(xdrs, cap)
  110.     register XDR *xdrs;
  111.     register struct rmtcallargs *cap;
  112. {
  113.     u_int lenposition, argposition, position;
  114.  
  115.     if (xdr_u_long(xdrs, &(cap->prog)) &&
  116.         xdr_u_long(xdrs, &(cap->vers)) &&
  117.         xdr_u_long(xdrs, &(cap->proc))) {
  118.         lenposition = XDR_GETPOS(xdrs);
  119.         if (! xdr_u_long(xdrs, &(cap->arglen)))
  120.             return (FALSE);
  121.         argposition = XDR_GETPOS(xdrs);
  122.         if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
  123.             return (FALSE);
  124.         position = XDR_GETPOS(xdrs);
  125.         cap->arglen = (u_long)position - (u_long)argposition;
  126.         XDR_SETPOS(xdrs, lenposition);
  127.         if (! xdr_u_long(xdrs, &(cap->arglen)))
  128.             return (FALSE);
  129.         XDR_SETPOS(xdrs, position);
  130.         return (TRUE);
  131.     }
  132.     return (FALSE);
  133. }
  134.  
  135. /*
  136.  * XDR remote call results
  137.  * written for XDR_DECODE direction only
  138.  */
  139. bool_t
  140. xdr_rmtcallres(xdrs, crp)
  141.     register XDR *xdrs;
  142.     register struct rmtcallres *crp;
  143. {
  144.     caddr_t port_ptr;
  145.  
  146.     port_ptr = (caddr_t)crp->port_ptr;
  147.     if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
  148.         xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
  149.         crp->port_ptr = (u_long *)port_ptr;
  150.         return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
  151.     }
  152.     return (FALSE);
  153. }
  154.  
  155.  
  156. /*
  157.  * The following is kludged-up support for simple rpc broadcasts.
  158.  * Someday a large, complicated system will replace these trivial 
  159.  * routines which only support udp/ip .
  160.  */
  161.  
  162. static int
  163. getbroadcastnets(addrs, sock, buf)
  164.     struct in_addr *addrs;
  165.     int sock;  /* any valid socket will do */
  166.     char *buf;  /* why allocxate more when we can use existing... */
  167. {
  168.     struct ifconf ifc;
  169.         struct ifreq ifreq, *ifr;
  170.     struct sockaddr_in *sin;
  171.         int n, i;
  172.  
  173.         ifc.ifc_len = UDPMSGSIZE;
  174.         ifc.ifc_buf = buf;
  175.         if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
  176.                 perror("broadcast: ioctl (get interface configuration)");
  177.                 return (0);
  178.         }
  179.         ifr = ifc.ifc_req;
  180.         for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) {
  181.                 ifreq = *ifr;
  182.                 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  183.                         perror("broadcast: ioctl (get interface flags)");
  184.                         continue;
  185.                 }
  186.                 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
  187.             (ifreq.ifr_flags & IFF_UP) &&
  188.             ifr->ifr_addr.sa_family == AF_INET) {
  189.             sin = (struct sockaddr_in *)&ifr->ifr_addr;
  190. #ifdef SIOCGIFBRDADDR   /* 4.3BSD */
  191.             if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
  192.                 addrs[i++] = inet_makeaddr(inet_netof
  193.                 (sin->sin_addr.s_addr), INADDR_ANY);
  194.             } else {
  195.                 addrs[i++] = ((struct sockaddr_in*)
  196.                   &ifreq.ifr_addr)->sin_addr;
  197.             }
  198. #else /* 4.2 BSD */
  199.             addrs[i++] = inet_makeaddr(inet_netof
  200.               (sin->sin_addr.s_addr), INADDR_ANY);
  201. #endif
  202.         }
  203.     }
  204.     return (i);
  205. }
  206.  
  207. typedef bool_t (*resultproc_t)();
  208.  
  209. enum clnt_stat 
  210. clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
  211.     u_long        prog;        /* program number */
  212.     u_long        vers;        /* version number */
  213.     u_long        proc;        /* procedure number */
  214.     xdrproc_t    xargs;        /* xdr routine for args */
  215.     caddr_t        argsp;        /* pointer to args */
  216.     xdrproc_t    xresults;    /* xdr routine for results */
  217.     caddr_t        resultsp;    /* pointer to results */
  218.     resultproc_t    eachresult;    /* call with each result obtained */
  219. {
  220.     enum clnt_stat stat;
  221.     AUTH *unix_auth = authunix_create_default();
  222.     XDR xdr_stream;
  223.     register XDR *xdrs = &xdr_stream;
  224.     int outlen, inlen, fromlen, nets;
  225.     register int sock;
  226. #ifdef FD_SETSIZE
  227.     fd_set mask;
  228.     fd_set readfds;
  229. #else
  230.     int readfds;
  231.     register int mask;
  232. #endif /* def FD_SETSIZE */
  233.     register int i;
  234.     bool_t done = FALSE;
  235.     register u_long xid;
  236.     u_long port;
  237.     struct in_addr addrs[20];
  238.     struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
  239.     struct rmtcallargs a;
  240.     struct rmtcallres r;
  241.     struct rpc_msg msg;
  242.     struct timeval t; 
  243.     char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
  244.  
  245.     /*
  246.      * initialization: create a socket, a broadcast address, and
  247.      * preserialize the arguments into a send buffer.
  248.      */
  249.     if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  250.         perror("Cannot create socket for broadcast rpc");
  251.         stat = RPC_CANTSEND;
  252.         goto done_broad;
  253.     }
  254. #ifdef FD_SETSIZE
  255.     FD_ZERO(&mask);
  256.     FD_SET(sock, &mask);
  257. #else
  258.     mask = (1 << sock);
  259. #endif /* def FD_SETSIZE */
  260.     nets = getbroadcastnets(addrs, sock, inbuf);
  261.     bzero((char *)&baddr, sizeof (baddr));
  262.     baddr.sin_family = AF_INET;
  263.     baddr.sin_port = htons(PMAPPORT);
  264.     baddr.sin_addr.s_addr = htonl(INADDR_ANY);
  265. /*    baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
  266.     (void)gettimeofday(&t, (struct timezone *)0);
  267.     msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec;
  268.     t.tv_usec = 0;
  269.     msg.rm_direction = CALL;
  270.     msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  271.     msg.rm_call.cb_prog = PMAPPROG;
  272.     msg.rm_call.cb_vers = PMAPVERS;
  273.     msg.rm_call.cb_proc = PMAPPROC_CALLIT;
  274.     msg.rm_call.cb_cred = unix_auth->ah_cred;
  275.     msg.rm_call.cb_verf = unix_auth->ah_verf;
  276.     a.prog = prog;
  277.     a.vers = vers;
  278.     a.proc = proc;
  279.     a.xdr_args = xargs;
  280.     a.args_ptr = argsp;
  281.     r.port_ptr = &port;
  282.     r.xdr_results = xresults;
  283.     r.results_ptr = resultsp;
  284.     xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
  285.     if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
  286.         stat = RPC_CANTENCODEARGS;
  287.         goto done_broad;
  288.     }
  289.     outlen = (int)xdr_getpos(xdrs);
  290.     xdr_destroy(xdrs);
  291.     /*
  292.      * Basic loop: broadcast a packet and wait a while for response(s).
  293.      * The response timeout grows larger per iteration.
  294.      */
  295.     for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
  296.         for (i = 0; i < nets; i++) {
  297.             baddr.sin_addr = addrs[i];
  298.             if (sendto(sock, outbuf, outlen, 0,
  299.                 (struct sockaddr *)&baddr,
  300.                 sizeof (struct sockaddr)) != outlen) {
  301.                 perror("Cannot send broadcast packet");
  302.                 stat = RPC_CANTSEND;
  303.                 goto done_broad;
  304.             }
  305.         }
  306.         if (eachresult == NULL) {
  307.             stat = RPC_SUCCESS;
  308.             goto done_broad;
  309.         }
  310.     recv_again:
  311.         msg.acpted_rply.ar_verf = _null_auth;
  312.         msg.acpted_rply.ar_results.where = (caddr_t)&r;
  313.                 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
  314.         readfds = mask;
  315.         switch (select(_rpc_dtablesize(), &readfds, (int *)NULL, 
  316.                    (int *)NULL, &t)) {
  317.  
  318.         case 0:  /* timed out */
  319.             stat = RPC_TIMEDOUT;
  320.             continue;
  321.  
  322.         case -1:  /* some kind of error */
  323.             if (errno == EINTR)
  324.                 goto recv_again;
  325.             perror("Broadcast select problem");
  326.             stat = RPC_CANTRECV;
  327.             goto done_broad;
  328.  
  329.         }  /* end of select results switch */
  330.     try_again:
  331.         fromlen = sizeof(struct sockaddr);
  332.         inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
  333.             (struct sockaddr *)&raddr, &fromlen);
  334.         if (inlen < 0) {
  335.             if (errno == EINTR)
  336.                 goto try_again;
  337.             perror("Cannot receive reply to broadcast");
  338.             stat = RPC_CANTRECV;
  339.             goto done_broad;
  340.         }
  341.         if (inlen < sizeof(u_long))
  342.             goto recv_again;
  343.         /*
  344.          * see if reply transaction id matches sent id.
  345.          * If so, decode the results.
  346.          */
  347.         xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
  348.         if (xdr_replymsg(xdrs, &msg)) {
  349.             if ((msg.rm_xid == xid) &&
  350.                 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
  351.                 (msg.acpted_rply.ar_stat == SUCCESS)) {
  352.                 raddr.sin_port = htons((u_short)port);
  353.                 done = (*eachresult)(resultsp, &raddr);
  354.             }
  355.             /* otherwise, we just ignore the errors ... */
  356.         } else {
  357. #ifdef notdef
  358.             /* some kind of deserialization problem ... */
  359.             if (msg.rm_xid == xid)
  360.                 fprintf(stderr, "Broadcast deserialization problem");
  361.             /* otherwise, just random garbage */
  362. #endif
  363.         }
  364.         xdrs->x_op = XDR_FREE;
  365.         msg.acpted_rply.ar_results.proc = xdr_void;
  366.         (void)xdr_replymsg(xdrs, &msg);
  367.         (void)(*xresults)(xdrs, resultsp);
  368.         xdr_destroy(xdrs);
  369.         if (done) {
  370.             stat = RPC_SUCCESS;
  371.             goto done_broad;
  372.         } else {
  373.             goto recv_again;
  374.         }
  375.     }
  376. done_broad:
  377.     (void)close(sock);
  378.     AUTH_DESTROY(unix_auth);
  379.     return (stat);
  380. }
  381.  
  382.