Blame sunrpc/pm_getport.c

Packit 6c4009
/*
Packit 6c4009
 * pmap_getport.c
Packit 6c4009
 * Client interface to pmap rpc service.
Packit 6c4009
 *
Packit 6c4009
 * Copyright (c) 2010, Oracle America, Inc.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions are
Packit 6c4009
 * met:
Packit 6c4009
 *
Packit 6c4009
 *     * Redistributions of source code must retain the above copyright
Packit 6c4009
 *       notice, this list of conditions and the following disclaimer.
Packit 6c4009
 *     * Redistributions in binary form must reproduce the above
Packit 6c4009
 *       copyright notice, this list of conditions and the following
Packit 6c4009
 *       disclaimer in the documentation and/or other materials
Packit 6c4009
 *       provided with the distribution.
Packit 6c4009
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit 6c4009
 *       contributors may be used to endorse or promote products derived
Packit 6c4009
 *       from this software without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 6c4009
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 6c4009
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit 6c4009
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit 6c4009
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit 6c4009
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit 6c4009
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 6c4009
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 6c4009
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 6c4009
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 6c4009
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <rpc/rpc.h>
Packit 6c4009
#include <rpc/pmap_prot.h>
Packit 6c4009
#include <rpc/pmap_clnt.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Create a socket that is locally bound to a non-reserve port. For
Packit 6c4009
 * any failures, -1 is returned which will cause the RPC code to
Packit 6c4009
 * create the socket.
Packit 6c4009
 */
Packit 6c4009
int
Packit 6c4009
__get_socket (struct sockaddr_in *saddr)
Packit 6c4009
{
Packit 6c4009
  int so = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
Packit 6c4009
  if (so < 0)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  struct sockaddr_in laddr;
Packit 6c4009
  socklen_t namelen = sizeof (laddr);
Packit 6c4009
  laddr.sin_family = AF_INET;
Packit 6c4009
  laddr.sin_port = 0;
Packit 6c4009
  laddr.sin_addr.s_addr = htonl (INADDR_ANY);
Packit 6c4009
Packit 6c4009
  int cc = __bind (so, (struct sockaddr *) &laddr, namelen);
Packit 6c4009
  if (__glibc_unlikely (cc < 0))
Packit 6c4009
    {
Packit 6c4009
    fail:
Packit 6c4009
      __close (so);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  cc = __connect (so, (struct sockaddr *) saddr, namelen);
Packit 6c4009
  if (__glibc_unlikely (cc < 0))
Packit 6c4009
    goto fail;
Packit 6c4009
Packit 6c4009
  return so;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Find the mapped port for program,version.
Packit 6c4009
 * Internal version with additional parameters.
Packit 6c4009
 * Calls the pmap service remotely to do the lookup.
Packit 6c4009
 * Returns 0 if no map exists.
Packit 6c4009
 */
Packit 6c4009
u_short
Packit 6c4009
__libc_rpc_getport (struct sockaddr_in *address, u_long program,
Packit 6c4009
		    u_long version, u_int protocol, time_t timeout_sec,
Packit 6c4009
		    time_t tottimeout_sec)
Packit 6c4009
{
Packit 6c4009
  const struct timeval timeout = {timeout_sec, 0};
Packit 6c4009
  const struct timeval tottimeout = {tottimeout_sec, 0};
Packit 6c4009
Packit 6c4009
  u_short port = 0;
Packit 6c4009
  int socket = -1;
Packit 6c4009
  CLIENT *client;
Packit 6c4009
  struct pmap parms;
Packit 6c4009
  bool closeit = false;
Packit 6c4009
Packit 6c4009
  address->sin_port = htons (PMAPPORT);
Packit 6c4009
  if (protocol == IPPROTO_TCP)
Packit 6c4009
    {
Packit 6c4009
      /* Don't need a reserved port to get ports from the portmapper.  */
Packit 6c4009
      socket = __get_socket(address);
Packit 6c4009
      if (socket != -1)
Packit 6c4009
	closeit = true;
Packit 6c4009
      client = clnttcp_create (address, PMAPPROG, PMAPVERS, &socket,
Packit 6c4009
			       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    client = clntudp_bufcreate (address, PMAPPROG, PMAPVERS, timeout,
Packit 6c4009
				&socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
Packit 6c4009
  if (client != (CLIENT *) NULL)
Packit 6c4009
    {
Packit 6c4009
      struct rpc_createerr *ce = &get_rpc_createerr ();
Packit 6c4009
      parms.pm_prog = program;
Packit 6c4009
      parms.pm_vers = version;
Packit 6c4009
      parms.pm_prot = protocol;
Packit 6c4009
      parms.pm_port = 0;	/* not needed or used */
Packit 6c4009
      if (CLNT_CALL (client, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
Packit 6c4009
		     (caddr_t)&parms, (xdrproc_t)xdr_u_short,
Packit 6c4009
		     (caddr_t)&port, tottimeout) != RPC_SUCCESS)
Packit 6c4009
	{
Packit 6c4009
	  ce->cf_stat = RPC_PMAPFAILURE;
Packit 6c4009
	  clnt_geterr (client, &ce->cf_error);
Packit 6c4009
	}
Packit 6c4009
      else if (port == 0)
Packit 6c4009
	{
Packit 6c4009
	  ce->cf_stat = RPC_PROGNOTREGISTERED;
Packit 6c4009
	}
Packit 6c4009
      CLNT_DESTROY (client);
Packit 6c4009
    }
Packit 6c4009
  /* We only need to close the socket here if we opened  it.  */
Packit 6c4009
  if (closeit)
Packit 6c4009
    (void) __close (socket);
Packit 6c4009
  address->sin_port = 0;
Packit 6c4009
  return port;
Packit 6c4009
}
Packit 6c4009
#ifdef EXPORT_RPC_SYMBOLS
Packit 6c4009
libc_hidden_def (__libc_rpc_getport)
Packit 6c4009
#else
Packit 6c4009
libc_hidden_nolink_sunrpc (__libc_rpc_getport, GLIBC_PRIVATE)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Find the mapped port for program,version.
Packit 6c4009
 * Calls the pmap service remotely to do the lookup.
Packit 6c4009
 * Returns 0 if no map exists.
Packit 6c4009
 */
Packit 6c4009
u_short
Packit 6c4009
pmap_getport (struct sockaddr_in *address, u_long program, u_long version,
Packit 6c4009
	      u_int protocol)
Packit 6c4009
{
Packit 6c4009
  return __libc_rpc_getport (address, program, version, protocol, 5, 60);
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (pmap_getport, GLIBC_2_0)