Blame sunrpc/clnt_simp.c

Packit 6c4009
/*
Packit 6c4009
 * clnt_simple.c
Packit 6c4009
 * Simplified front end to rpc.
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 <alloca.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <rpc/rpc.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
struct callrpc_private_s
Packit 6c4009
  {
Packit 6c4009
    CLIENT *client;
Packit 6c4009
    int socket;
Packit 6c4009
    u_long oldprognum, oldversnum, valid;
Packit 6c4009
    char *oldhost;
Packit 6c4009
  };
Packit 6c4009
#define callrpc_private RPC_THREAD_VARIABLE(callrpc_private_s)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
callrpc (const char *host, u_long prognum, u_long versnum, u_long procnum,
Packit 6c4009
	 xdrproc_t inproc, const char *in, xdrproc_t outproc, char *out)
Packit 6c4009
{
Packit 6c4009
  struct callrpc_private_s *crp = callrpc_private;
Packit 6c4009
  struct sockaddr_in server_addr;
Packit 6c4009
  enum clnt_stat clnt_stat;
Packit 6c4009
  struct timeval timeout, tottimeout;
Packit 6c4009
Packit 6c4009
  if (crp == 0)
Packit 6c4009
    {
Packit 6c4009
      crp = (struct callrpc_private_s *) calloc (1, sizeof (*crp));
Packit 6c4009
      if (crp == 0)
Packit 6c4009
	return 0;
Packit 6c4009
      callrpc_private = crp;
Packit 6c4009
    }
Packit 6c4009
  if (crp->oldhost == NULL)
Packit 6c4009
    {
Packit 6c4009
      crp->oldhost = malloc (256);
Packit 6c4009
      crp->oldhost[0] = 0;
Packit 6c4009
      crp->socket = RPC_ANYSOCK;
Packit 6c4009
    }
Packit 6c4009
  if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
Packit 6c4009
      && strcmp (crp->oldhost, host) == 0)
Packit 6c4009
    {
Packit 6c4009
      /* reuse old client */
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      crp->valid = 0;
Packit 6c4009
      if (crp->socket != RPC_ANYSOCK)
Packit 6c4009
	{
Packit 6c4009
	  (void) __close (crp->socket);
Packit 6c4009
	  crp->socket = RPC_ANYSOCK;
Packit 6c4009
	}
Packit 6c4009
      if (crp->client)
Packit 6c4009
	{
Packit 6c4009
	  clnt_destroy (crp->client);
Packit 6c4009
	  crp->client = NULL;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (__libc_rpc_gethostbyname (host, &server_addr) != 0)
Packit 6c4009
	return (int) get_rpc_createerr().cf_stat;
Packit 6c4009
Packit 6c4009
      timeout.tv_usec = 0;
Packit 6c4009
      timeout.tv_sec = 5;
Packit 6c4009
      if ((crp->client = clntudp_create (&server_addr, (u_long) prognum,
Packit 6c4009
			  (u_long) versnum, timeout, &crp->socket)) == NULL)
Packit 6c4009
	return (int) get_rpc_createerr().cf_stat;
Packit 6c4009
      crp->valid = 1;
Packit 6c4009
      crp->oldprognum = prognum;
Packit 6c4009
      crp->oldversnum = versnum;
Packit 6c4009
      (void) strncpy (crp->oldhost, host, 255);
Packit 6c4009
      crp->oldhost[255] = '\0';
Packit 6c4009
    }
Packit 6c4009
  tottimeout.tv_sec = 25;
Packit 6c4009
  tottimeout.tv_usec = 0;
Packit 6c4009
  clnt_stat = clnt_call (crp->client, procnum, inproc, (char *) in,
Packit 6c4009
			 outproc, out, tottimeout);
Packit 6c4009
  /*
Packit 6c4009
   * if call failed, empty cache
Packit 6c4009
   */
Packit 6c4009
  if (clnt_stat != RPC_SUCCESS)
Packit 6c4009
    crp->valid = 0;
Packit 6c4009
  return (int) clnt_stat;
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (callrpc, GLIBC_2_0)
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__rpc_thread_clnt_cleanup (void)
Packit 6c4009
{
Packit 6c4009
	struct callrpc_private_s *rcp = RPC_THREAD_VARIABLE(callrpc_private_s);
Packit 6c4009
Packit 6c4009
	if (rcp) {
Packit 6c4009
		if (rcp->client)
Packit 6c4009
			CLNT_DESTROY (rcp->client);
Packit 6c4009
		free (rcp);
Packit 6c4009
	}
Packit 6c4009
}