pm_getport.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * pmap_getport.c
  3. * Client interface to pmap rpc service.
  4. *
  5. * Copyright (c) 2010, Oracle America, Inc.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials
  16. * provided with the distribution.
  17. * * Neither the name of the "Oracle America, Inc." nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  28. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdbool.h>
  35. #include <unistd.h>
  36. #include <rpc/rpc.h>
  37. #include <rpc/pmap_prot.h>
  38. #include <rpc/pmap_clnt.h>
  39. #include <sys/socket.h>
  40. #include <shlib-compat.h>
  41. /*
  42. * Create a socket that is locally bound to a non-reserve port. For
  43. * any failures, -1 is returned which will cause the RPC code to
  44. * create the socket.
  45. */
  46. int
  47. __get_socket (struct sockaddr_in *saddr)
  48. {
  49. int so = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  50. if (so < 0)
  51. return -1;
  52. struct sockaddr_in laddr;
  53. socklen_t namelen = sizeof (laddr);
  54. laddr.sin_family = AF_INET;
  55. laddr.sin_port = 0;
  56. laddr.sin_addr.s_addr = htonl (INADDR_ANY);
  57. int cc = __bind (so, (struct sockaddr *) &laddr, namelen);
  58. if (__glibc_unlikely (cc < 0))
  59. {
  60. fail:
  61. __close (so);
  62. return -1;
  63. }
  64. cc = __connect (so, (struct sockaddr *) saddr, namelen);
  65. if (__glibc_unlikely (cc < 0))
  66. goto fail;
  67. return so;
  68. }
  69. /*
  70. * Find the mapped port for program,version.
  71. * Internal version with additional parameters.
  72. * Calls the pmap service remotely to do the lookup.
  73. * Returns 0 if no map exists.
  74. */
  75. u_short
  76. __libc_rpc_getport (struct sockaddr_in *address, u_long program,
  77. u_long version, u_int protocol, time_t timeout_sec,
  78. time_t tottimeout_sec)
  79. {
  80. const struct timeval timeout = {timeout_sec, 0};
  81. const struct timeval tottimeout = {tottimeout_sec, 0};
  82. u_short port = 0;
  83. int socket = -1;
  84. CLIENT *client;
  85. struct pmap parms;
  86. bool closeit = false;
  87. address->sin_port = htons (PMAPPORT);
  88. if (protocol == IPPROTO_TCP)
  89. {
  90. /* Don't need a reserved port to get ports from the portmapper. */
  91. socket = __get_socket(address);
  92. if (socket != -1)
  93. closeit = true;
  94. client = clnttcp_create (address, PMAPPROG, PMAPVERS, &socket,
  95. RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
  96. }
  97. else
  98. client = clntudp_bufcreate (address, PMAPPROG, PMAPVERS, timeout,
  99. &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
  100. if (client != (CLIENT *) NULL)
  101. {
  102. struct rpc_createerr *ce = &get_rpc_createerr ();
  103. parms.pm_prog = program;
  104. parms.pm_vers = version;
  105. parms.pm_prot = protocol;
  106. parms.pm_port = 0; /* not needed or used */
  107. if (CLNT_CALL (client, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
  108. (caddr_t)&parms, (xdrproc_t)xdr_u_short,
  109. (caddr_t)&port, tottimeout) != RPC_SUCCESS)
  110. {
  111. ce->cf_stat = RPC_PMAPFAILURE;
  112. clnt_geterr (client, &ce->cf_error);
  113. }
  114. else if (port == 0)
  115. {
  116. ce->cf_stat = RPC_PROGNOTREGISTERED;
  117. }
  118. CLNT_DESTROY (client);
  119. }
  120. /* We only need to close the socket here if we opened it. */
  121. if (closeit)
  122. (void) __close (socket);
  123. address->sin_port = 0;
  124. return port;
  125. }
  126. #ifdef EXPORT_RPC_SYMBOLS
  127. libc_hidden_def (__libc_rpc_getport)
  128. #else
  129. libc_hidden_nolink_sunrpc (__libc_rpc_getport, GLIBC_PRIVATE)
  130. #endif
  131. /*
  132. * Find the mapped port for program,version.
  133. * Calls the pmap service remotely to do the lookup.
  134. * Returns 0 if no map exists.
  135. */
  136. u_short
  137. pmap_getport (struct sockaddr_in *address, u_long program, u_long version,
  138. u_int protocol)
  139. {
  140. return __libc_rpc_getport (address, program, version, protocol, 5, 60);
  141. }
  142. libc_hidden_nolink_sunrpc (pmap_getport, GLIBC_2_0)