pmap_clnt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /*
  30. * Copyright (C) 1984, Sun Microsystems, Inc.
  31. */
  32. /*
  33. * pmap_clnt.c
  34. * Client interface to pmap rpc service.
  35. */
  36. #include <stdio.h>
  37. #include <unistd.h>
  38. #include <net/if.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/socket.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <rpc/rpc.h>
  44. #include <rpc/pmap_prot.h>
  45. #include <rpc/pmap_clnt.h>
  46. /*
  47. * Same as get_myaddress, but we try to use the loopback
  48. * interface. portmap caches interfaces, and on DHCP clients,
  49. * it could be that only loopback is started at this time.
  50. */
  51. static bool_t
  52. __get_myaddress (struct sockaddr_in *addr)
  53. {
  54. int s;
  55. char buf[BUFSIZ];
  56. struct ifconf ifc;
  57. struct ifreq ifreq, *ifr;
  58. int len, loopback = 1;
  59. if ((s = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
  60. {
  61. perror ("__get_myaddress: socket");
  62. exit (1);
  63. }
  64. ifc.ifc_len = sizeof (buf);
  65. ifc.ifc_buf = buf;
  66. if (ioctl (s, SIOCGIFCONF, (char *) &ifc) < 0)
  67. {
  68. perror (_("__get_myaddress: ioctl (get interface configuration)"));
  69. exit (1);
  70. }
  71. again:
  72. ifr = ifc.ifc_req;
  73. for (len = ifc.ifc_len; len; len -= sizeof ifreq)
  74. {
  75. ifreq = *ifr;
  76. if (ioctl (s, SIOCGIFFLAGS, (char *) &ifreq) < 0)
  77. {
  78. perror ("__get_myaddress: ioctl");
  79. exit (1);
  80. }
  81. if ((ifreq.ifr_flags & IFF_UP) && (ifr->ifr_addr.sa_family == AF_INET)
  82. && ((ifreq.ifr_flags & IFF_LOOPBACK) || (loopback == 0)))
  83. {
  84. *addr = *((struct sockaddr_in *) &ifr->ifr_addr);
  85. addr->sin_port = htons (PMAPPORT);
  86. close (s);
  87. return TRUE;
  88. }
  89. ifr++;
  90. }
  91. if (loopback == 1)
  92. {
  93. loopback = 0;
  94. goto again;
  95. }
  96. close (s);
  97. return FALSE;
  98. }
  99. static const struct timeval timeout = {5, 0};
  100. static const struct timeval tottimeout = {60, 0};
  101. /*
  102. * Set a mapping between program,version and port.
  103. * Calls the pmap service remotely to do the mapping.
  104. */
  105. bool_t
  106. pmap_set (u_long program, u_long version, int protocol, u_short port)
  107. {
  108. struct sockaddr_in myaddress;
  109. int _socket = -1;
  110. CLIENT *client;
  111. struct pmap parms;
  112. bool_t rslt;
  113. if (!__get_myaddress (&myaddress))
  114. return FALSE;
  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_prot = protocol;
  122. parms.pm_port = port;
  123. if (CLNT_CALL (client, PMAPPROC_SET, (xdrproc_t)xdr_pmap, (caddr_t)&parms,
  124. (xdrproc_t)xdr_bool, (caddr_t)&rslt,
  125. tottimeout) != RPC_SUCCESS)
  126. {
  127. clnt_perror (client, _("Cannot register service"));
  128. rslt = FALSE;
  129. }
  130. CLNT_DESTROY (client);
  131. /* (void)close(_socket); CLNT_DESTROY closes it */
  132. return rslt;
  133. }
  134. libc_hidden_def (pmap_set)
  135. /*
  136. * Remove the mapping between program,version and port.
  137. * Calls the pmap service remotely to do the un-mapping.
  138. */
  139. bool_t
  140. pmap_unset (u_long program, u_long version)
  141. {
  142. struct sockaddr_in myaddress;
  143. int _socket = -1;
  144. CLIENT *client;
  145. struct pmap parms;
  146. bool_t rslt;
  147. if (!__get_myaddress (&myaddress))
  148. return FALSE;
  149. client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS,
  150. timeout, &_socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
  151. if (client == (CLIENT *) NULL)
  152. return FALSE;
  153. parms.pm_prog = program;
  154. parms.pm_vers = version;
  155. parms.pm_port = parms.pm_prot = 0;
  156. CLNT_CALL (client, PMAPPROC_UNSET, (xdrproc_t)xdr_pmap, (caddr_t)&parms,
  157. (xdrproc_t)xdr_bool, (caddr_t)&rslt, tottimeout);
  158. CLNT_DESTROY (client);
  159. /* (void)close(_socket); CLNT_DESTROY already closed it */
  160. return rslt;
  161. }
  162. libc_hidden_def (pmap_unset)