pmap_clnt.c 5.0 KB

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