pmap_clnt.c 5.2 KB

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