home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / rpc2 / part07 / rpc / rpclib / pmap_clnt.c next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.3 KB  |  168 lines

  1. /*
  2.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3.  * unrestricted use provided that this legend is included on all tape
  4.  * media and as a part of the software program in whole or part.  Users
  5.  * may copy or modify Sun RPC without charge, but are not authorized
  6.  * to license or distribute it to anyone else except as part of a product or
  7.  * program developed by the user.
  8.  * 
  9.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12.  * 
  13.  * Sun RPC is provided with no support and without any obligation on the
  14.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  15.  * modification or enhancement.
  16.  * 
  17.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  19.  * OR ANY PART THEREOF.
  20.  * 
  21.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22.  * or profits or other special, indirect and consequential damages, even if
  23.  * Sun has been advised of the possibility of such damages.
  24.  * 
  25.  * Sun Microsystems, Inc.
  26.  * 2550 Garcia Avenue
  27.  * Mountain View, California  94043
  28.  */
  29. #ifndef lint
  30. static char sccsid[] = "@(#)pmap_clnt.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  31. #endif
  32.  
  33. /*
  34.  * pmap_clnt.c
  35.  * Client interface to pmap rpc service.
  36.  *
  37.  * Copyright (C) 1984, Sun Microsystems, Inc.
  38.  */
  39.  
  40. #include "types.h"
  41. #include <netinet/in.h>
  42. #include "xdr.h"
  43. #include "auth.h"
  44. #include "clnt.h"
  45. #include "rpc_msg.h"
  46. #include "pmap_prot.h" 
  47. #include "pmap_clnt.h"
  48. #include <sys/socket.h>
  49. #include <sys/time.h>
  50. #include <stdio.h>
  51. #include <net/if.h>
  52. #include <sys/ioctl.h>
  53. #include <arpa/inet.h>
  54. #define NAMELEN 255
  55.  
  56. static struct timeval timeout = { 5, 0 };
  57. static struct timeval tottimeout = { 60, 0 };
  58. static struct sockaddr_in myaddress;
  59.  
  60. void clnt_perror();
  61.  
  62.  
  63. /*
  64.  * Set a mapping between program,version and port.
  65.  * Calls the pmap service remotely to do the mapping.
  66.  */
  67. bool_t
  68. pmap_set(program, version, protocol, port)
  69.     u_long program;
  70.     u_long version;
  71.     u_long protocol;
  72.     u_short port;
  73. {
  74.     struct sockaddr_in myaddress;
  75.     int socket = -1;
  76.     register CLIENT *client;
  77.     struct pmap parms;
  78.     bool_t rslt;
  79.  
  80.     get_myaddress(&myaddress);
  81.     client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
  82.         timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
  83.     if (client == (CLIENT *)NULL)
  84.         return (FALSE);
  85.     parms.pm_prog = program;
  86.     parms.pm_vers = version;
  87.     parms.pm_prot = protocol;
  88.     parms.pm_port = port;
  89.     if (CLNT_CALL(client, PMAPPROC_SET, xdr_pmap, &parms, xdr_bool, &rslt,
  90.         tottimeout) != RPC_SUCCESS) {
  91.         clnt_perror(client, "Cannot register service");
  92.         return (FALSE);
  93.     }
  94.     CLNT_DESTROY(client);
  95.     (void)close(socket);
  96.     return (rslt);
  97. }
  98.  
  99. /*
  100.  * Remove the mapping between program,version and port.
  101.  * Calls the pmap service remotely to do the un-mapping.
  102.  */
  103. bool_t
  104. pmap_unset(program, version)
  105.     u_long program;
  106.     u_long version;
  107. {
  108.     struct sockaddr_in myaddress;
  109.     int socket = -1;
  110.     register CLIENT *client;
  111.     struct pmap parms;
  112.     bool_t rslt;
  113.  
  114.     get_myaddress(&myaddress);
  115.     client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
  116.         timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
  117.     if (client == (CLIENT *)NULL)
  118.         return (FALSE);
  119.     parms.pm_prog = program;
  120.     parms.pm_vers = version;
  121.     parms.pm_port = parms.pm_prot = 0;
  122.     CLNT_CALL(client, PMAPPROC_UNSET, xdr_pmap, &parms, xdr_bool, &rslt,
  123.         tottimeout);
  124.     CLNT_DESTROY(client);
  125.     (void)close(socket);
  126.     return (rslt);
  127. }
  128.  
  129. /* 
  130.  * don't use gethostbyname, which would invoke yellow pages
  131.  */
  132. get_myaddress(addr)
  133.     struct sockaddr_in *addr;
  134. {
  135.     int s;
  136.     char buf[BUFSIZ];
  137.     struct ifconf ifc;
  138.     struct ifreq ifreq, *ifr;
  139.     int len;
  140.  
  141.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  142.         perror("get_myaddress: socket");
  143.         exit(1);
  144.     }
  145.     ifc.ifc_len = sizeof (buf);
  146.     ifc.ifc_buf = buf;
  147.     if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
  148.         perror("get_myaddress: ioctl (get interface configuration)");
  149.         exit(1);
  150.     }
  151.     ifr = ifc.ifc_req;
  152.     for (len = ifc.ifc_len; len; len -= sizeof ifreq) {
  153.         ifreq = *ifr;
  154.         if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  155.             perror("get_myaddress: ioctl");
  156.             exit(1);
  157.         }
  158.         if ((ifreq.ifr_flags & IFF_UP) &&
  159.             ifr->ifr_addr.sa_family == AF_INET) {
  160.             *addr = *((struct sockaddr_in *)&ifr->ifr_addr);
  161.             addr->sin_port = htons(PMAPPORT);
  162.             break;
  163.         }
  164.         ifr++;
  165.     }
  166.     close(s);
  167. }
  168.