Blame sunrpc/clnt_simp.c

Packit Service 82fcde
/*
Packit Service 82fcde
 * clnt_simple.c
Packit Service 82fcde
 * Simplified front end to rpc.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Copyright (c) 2010, Oracle America, Inc.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Redistribution and use in source and binary forms, with or without
Packit Service 82fcde
 * modification, are permitted provided that the following conditions are
Packit Service 82fcde
 * met:
Packit Service 82fcde
 *
Packit Service 82fcde
 *     * Redistributions of source code must retain the above copyright
Packit Service 82fcde
 *       notice, this list of conditions and the following disclaimer.
Packit Service 82fcde
 *     * Redistributions in binary form must reproduce the above
Packit Service 82fcde
 *       copyright notice, this list of conditions and the following
Packit Service 82fcde
 *       disclaimer in the documentation and/or other materials
Packit Service 82fcde
 *       provided with the distribution.
Packit Service 82fcde
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit Service 82fcde
 *       contributors may be used to endorse or promote products derived
Packit Service 82fcde
 *       from this software without specific prior written permission.
Packit Service 82fcde
 *
Packit Service 82fcde
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 82fcde
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 82fcde
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit Service 82fcde
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit Service 82fcde
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit Service 82fcde
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service 82fcde
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit Service 82fcde
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit Service 82fcde
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit Service 82fcde
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit Service 82fcde
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 82fcde
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 82fcde
 */
Packit Service 82fcde
Packit Service 82fcde
#include <alloca.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <rpc/rpc.h>
Packit Service 82fcde
#include <sys/socket.h>
Packit Service 82fcde
#include <netdb.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
#include <shlib-compat.h>
Packit Service 82fcde
Packit Service 82fcde
struct callrpc_private_s
Packit Service 82fcde
  {
Packit Service 82fcde
    CLIENT *client;
Packit Service 82fcde
    int socket;
Packit Service 82fcde
    u_long oldprognum, oldversnum, valid;
Packit Service 82fcde
    char *oldhost;
Packit Service 82fcde
  };
Packit Service 82fcde
#define callrpc_private RPC_THREAD_VARIABLE(callrpc_private_s)
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
callrpc (const char *host, u_long prognum, u_long versnum, u_long procnum,
Packit Service 82fcde
	 xdrproc_t inproc, const char *in, xdrproc_t outproc, char *out)
Packit Service 82fcde
{
Packit Service 82fcde
  struct callrpc_private_s *crp = callrpc_private;
Packit Service 82fcde
  struct sockaddr_in server_addr;
Packit Service 82fcde
  enum clnt_stat clnt_stat;
Packit Service 82fcde
  struct timeval timeout, tottimeout;
Packit Service 82fcde
Packit Service 82fcde
  if (crp == 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      crp = (struct callrpc_private_s *) calloc (1, sizeof (*crp));
Packit Service 82fcde
      if (crp == 0)
Packit Service 82fcde
	return 0;
Packit Service 82fcde
      callrpc_private = crp;
Packit Service 82fcde
    }
Packit Service 82fcde
  if (crp->oldhost == NULL)
Packit Service 82fcde
    {
Packit Service 82fcde
      crp->oldhost = malloc (256);
Packit Service 82fcde
      crp->oldhost[0] = 0;
Packit Service 82fcde
      crp->socket = RPC_ANYSOCK;
Packit Service 82fcde
    }
Packit Service 82fcde
  if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
Packit Service 82fcde
      && strcmp (crp->oldhost, host) == 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      /* reuse old client */
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      crp->valid = 0;
Packit Service 82fcde
      if (crp->socket != RPC_ANYSOCK)
Packit Service 82fcde
	{
Packit Service 82fcde
	  (void) __close (crp->socket);
Packit Service 82fcde
	  crp->socket = RPC_ANYSOCK;
Packit Service 82fcde
	}
Packit Service 82fcde
      if (crp->client)
Packit Service 82fcde
	{
Packit Service 82fcde
	  clnt_destroy (crp->client);
Packit Service 82fcde
	  crp->client = NULL;
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
      if (__libc_rpc_gethostbyname (host, &server_addr) != 0)
Packit Service 82fcde
	return (int) get_rpc_createerr().cf_stat;
Packit Service 82fcde
Packit Service 82fcde
      timeout.tv_usec = 0;
Packit Service 82fcde
      timeout.tv_sec = 5;
Packit Service 82fcde
      if ((crp->client = clntudp_create (&server_addr, (u_long) prognum,
Packit Service 82fcde
			  (u_long) versnum, timeout, &crp->socket)) == NULL)
Packit Service 82fcde
	return (int) get_rpc_createerr().cf_stat;
Packit Service 82fcde
      crp->valid = 1;
Packit Service 82fcde
      crp->oldprognum = prognum;
Packit Service 82fcde
      crp->oldversnum = versnum;
Packit Service 82fcde
      (void) strncpy (crp->oldhost, host, 255);
Packit Service 82fcde
      crp->oldhost[255] = '\0';
Packit Service 82fcde
    }
Packit Service 82fcde
  tottimeout.tv_sec = 25;
Packit Service 82fcde
  tottimeout.tv_usec = 0;
Packit Service 82fcde
  clnt_stat = clnt_call (crp->client, procnum, inproc, (char *) in,
Packit Service 82fcde
			 outproc, out, tottimeout);
Packit Service 82fcde
  /*
Packit Service 82fcde
   * if call failed, empty cache
Packit Service 82fcde
   */
Packit Service 82fcde
  if (clnt_stat != RPC_SUCCESS)
Packit Service 82fcde
    crp->valid = 0;
Packit Service 82fcde
  return (int) clnt_stat;
Packit Service 82fcde
}
Packit Service 82fcde
libc_hidden_nolink_sunrpc (callrpc, GLIBC_2_0)
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
__rpc_thread_clnt_cleanup (void)
Packit Service 82fcde
{
Packit Service 82fcde
	struct callrpc_private_s *rcp = RPC_THREAD_VARIABLE(callrpc_private_s);
Packit Service 82fcde
Packit Service 82fcde
	if (rcp) {
Packit Service 82fcde
		if (rcp->client)
Packit Service 82fcde
			CLNT_DESTROY (rcp->client);
Packit Service 82fcde
		free (rcp);
Packit Service 82fcde
	}
Packit Service 82fcde
}