home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp-sdk / src / rpclib / pmap_rmt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  10.7 KB  |  413 lines

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