#if defined(LIBC_RCS) && !defined(lint)
static char	rcs_id[] = 
"$Header: /mit/sipb/src/src/webster/lib/RCS/clnt_simple.c,v 1.13 1995/03/01 10:06:30 svalente Exp $";
#endif
#ifdef SUNRPC
/*
 * RCS info
 *	$Locker:  $
 */
/* NFSSRC @(#)clnt_simple.c	2.1 86/04/14 */
#if defined(SUN_SCCS) && !defined(lint)
static char	sccsid[] = "@(#)clnt_simple.c 1.1 86/02/03 Copyr 1984 Sun Micro";
#endif

/* 
 * clnt_simple.c
 * Simplified front end to rpc.
 *
 * Copyright (C) 1984, Sun Microsystems, Inc.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>

#ifdef _AIX
#include <sys/select.h>
#endif

#include <rpc/rpc.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h>

static CLIENT *client = NULL;
static int	socket_id;
static int	oldprognum, oldversnum, valid;
static char	*oldhost;

#include <netinet/in.h>

#if defined(POSIX) && !defined(bcopy) && !defined(memcpy)
#define bcopy(a, b, c) memcpy(b, a, c)
#endif

nuke_connection()
{
	if (!client)
		return;
	valid = 0;
	close(socket_id);
	socket_id = 0;
	clnt_destroy(client);
	client = NULL;
}

callrpc_(host, prognum, versnum, procnum, inproc, in, outproc, out)
char	*host;
xdrproc_t inproc, outproc;
char	*in, *out;
{
	struct sockaddr_in server_addr;
	enum clnt_stat clnt_stat;
	struct hostent *hp;
	struct timeval tottimeout;

	if (oldhost == NULL) {
		oldhost = (char *)malloc(256);
		oldhost[0] = 0;
		socket_id = RPC_ANYSOCK;
	}
	if (valid && oldprognum == prognum && oldversnum == versnum &&
	    strcmp(oldhost, host) == 0) {
		/* reuse old client */
	} else {
		valid = 0;
		close(socket_id);
		socket_id = RPC_ANYSOCK;
		if (client) {
			clnt_destroy(client);
			client = NULL;
		}
		if ((hp = gethostbyname(host)) == NULL)
			return ((int) RPC_UNKNOWNHOST);

		bcopy(hp->h_addr, &server_addr.sin_addr, hp->h_length);
		server_addr.sin_family = AF_INET;
		server_addr.sin_port =  0;
		/*
		 * if I interpret sendsz and recvsz correctly,
		 * "sendsz" is what we're putting out -- and the
		 * largest definition I've found so far is a bit over
		 * 11K, before formatting -- and recvsz is what we're
		 * being sent, which is never more than one line.
		 * 
		 * However, let's first see what happens when we feed
		 * it "the system defaults".
		 */
		if ((client = clnttcp_create(&server_addr, prognum,
		     versnum, &socket_id, 0, 0)) == NULL)
			return ((int) rpc_createerr.cf_stat);
		valid = 1;
		oldprognum = prognum;
		oldversnum = versnum;
		strcpy(oldhost, host);
	}
	tottimeout.tv_sec = 15;
	tottimeout.tv_usec = 0;
	clnt_stat = clnt_call(client, procnum, inproc, in, outproc,
	     out, tottimeout);
	/* 
	 * if call failed, empty cache
	 */
	if (clnt_stat != RPC_SUCCESS)
		valid = 0;
	return ((int) clnt_stat);
}

free_answer(proc, answer)
xdrproc_t proc;
char *answer;
{
     clnt_freeres(client, proc, answer);
}
#endif
