pmap_clnt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2010, Oracle America, Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following
  12. * disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. * * Neither the name of the "Oracle America, Inc." nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * pmap_clnt.c
  33. * Client interface to pmap rpc service.
  34. */
  35. #include <stdio.h>
  36. #include <unistd.h>
  37. #include <libintl.h>
  38. #include <net/if.h>
  39. #include <ifaddrs.h>
  40. #include <sys/ioctl.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <rpc/rpc.h>
  45. #include <rpc/pmap_prot.h>
  46. #include <rpc/pmap_clnt.h>
  47. #include <shlib-compat.h>
  48. /*
  49. * Same as get_myaddress, but we try to use the loopback
  50. * interface. portmap caches interfaces, and on DHCP clients,
  51. * it could be that only loopback is started at this time.
  52. */
  53. static bool_t
  54. __get_myaddress (struct sockaddr_in *addr)
  55. {
  56. struct ifaddrs *ifa;
  57. if (getifaddrs (&ifa) != 0)
  58. {
  59. perror ("get_myaddress: getifaddrs");
  60. exit (1);
  61. }
  62. int loopback = 1;
  63. struct ifaddrs *run;
  64. again:
  65. run = ifa;
  66. while (run != NULL)
  67. {
  68. if ((run->ifa_flags & IFF_UP)
  69. && run->ifa_addr != NULL
  70. && run->ifa_addr->sa_family == AF_INET
  71. && ((run->ifa_flags & IFF_LOOPBACK) || loopback == 0))
  72. {
  73. *addr = *((struct sockaddr_in *) run->ifa_addr);
  74. addr->sin_port = htons (PMAPPORT);
  75. goto out;
  76. }
  77. run = run->ifa_next;
  78. }
  79. if (loopback == 1)
  80. {
  81. loopback = 0;
  82. goto again;
  83. }
  84. out:
  85. freeifaddrs (ifa);
  86. return run == NULL ? FALSE : TRUE;
  87. }
  88. static const struct timeval timeout = {5, 0};
  89. static const struct timeval tottimeout = {60, 0};
  90. /*
  91. * Set a mapping between program,version and port.
  92. * Calls the pmap service remotely to do the mapping.
  93. */
  94. bool_t
  95. pmap_set (u_long program, u_long version, int protocol, u_short port)
  96. {
  97. struct sockaddr_in myaddress;
  98. int socket = -1;
  99. CLIENT *client;
  100. struct pmap parms;
  101. bool_t rslt;
  102. if (!__get_myaddress (&myaddress))
  103. return FALSE;
  104. client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS, timeout, &socket,
  105. RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
  106. if (client == (CLIENT *) NULL)
  107. return (FALSE);
  108. parms.pm_prog = program;
  109. parms.pm_vers = version;
  110. parms.pm_prot = protocol;
  111. parms.pm_port = port;
  112. if (CLNT_CALL (client, PMAPPROC_SET, (xdrproc_t)xdr_pmap,
  113. (caddr_t)&parms, (xdrproc_t)xdr_bool, (caddr_t)&rslt,
  114. tottimeout) != RPC_SUCCESS)
  115. {
  116. clnt_perror (client, _("Cannot register service"));
  117. rslt = FALSE;
  118. }
  119. CLNT_DESTROY (client);
  120. /* (void)close(socket); CLNT_DESTROY closes it */
  121. return rslt;
  122. }
  123. libc_hidden_nolink_sunrpc (pmap_set, GLIBC_2_0)
  124. /*
  125. * Remove the mapping between program,version and port.
  126. * Calls the pmap service remotely to do the un-mapping.
  127. */
  128. bool_t
  129. pmap_unset (u_long program, u_long version)
  130. {
  131. struct sockaddr_in myaddress;
  132. int socket = -1;
  133. CLIENT *client;
  134. struct pmap parms;
  135. bool_t rslt;
  136. if (!__get_myaddress (&myaddress))
  137. return FALSE;
  138. client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS, timeout, &socket,
  139. RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
  140. if (client == (CLIENT *) NULL)
  141. return FALSE;
  142. parms.pm_prog = program;
  143. parms.pm_vers = version;
  144. parms.pm_port = parms.pm_prot = 0;
  145. CLNT_CALL (client, PMAPPROC_UNSET, (xdrproc_t)xdr_pmap,
  146. (caddr_t)&parms, (xdrproc_t)xdr_bool, (caddr_t)&rslt,
  147. tottimeout);
  148. CLNT_DESTROY (client);
  149. /* (void)close(socket); CLNT_DESTROY already closed it */
  150. return rslt;
  151. }
  152. libc_hidden_nolink_sunrpc (pmap_unset, GLIBC_2_0)