xref: /freebsd/lib/libc/rpc/clnt_simple.c (revision 0ab74c6fd650480153c23438930a136122f0961e)
199064799SGarrett Wollman /*
299064799SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
399064799SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
499064799SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
599064799SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
699064799SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
799064799SGarrett Wollman  * program developed by the user.
899064799SGarrett Wollman  *
999064799SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1099064799SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1199064799SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1299064799SGarrett Wollman  *
1399064799SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
1499064799SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
1599064799SGarrett Wollman  * modification or enhancement.
1699064799SGarrett Wollman  *
1799064799SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1899064799SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
1999064799SGarrett Wollman  * OR ANY PART THEREOF.
2099064799SGarrett Wollman  *
2199064799SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2299064799SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
2399064799SGarrett Wollman  * Sun has been advised of the possibility of such damages.
2499064799SGarrett Wollman  *
2599064799SGarrett Wollman  * Sun Microsystems, Inc.
2699064799SGarrett Wollman  * 2550 Garcia Avenue
2799064799SGarrett Wollman  * Mountain View, California  94043
2899064799SGarrett Wollman  */
2999064799SGarrett Wollman 
3099064799SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
3199064799SGarrett Wollman /*static char *sccsid = "from: @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";*/
3299064799SGarrett Wollman /*static char *sccsid = "from: @(#)clnt_simple.c	2.2 88/08/01 4.0 RPCSRC";*/
330ab74c6fSPeter Wemm static char *rcsid = "$Id: clnt_simple.c,v 1.5 1996/08/12 14:00:20 peter Exp $";
3499064799SGarrett Wollman #endif
3599064799SGarrett Wollman 
3699064799SGarrett Wollman /*
3799064799SGarrett Wollman  * clnt_simple.c
3899064799SGarrett Wollman  * Simplified front end to rpc.
3999064799SGarrett Wollman  *
4099064799SGarrett Wollman  * Copyright (C) 1984, Sun Microsystems, Inc.
4199064799SGarrett Wollman  */
4299064799SGarrett Wollman 
430ab74c6fSPeter Wemm #include <sys/param.h>
4499064799SGarrett Wollman #include <stdio.h>
4599064799SGarrett Wollman #include <stdlib.h>
464c3af266SPoul-Henning Kamp #include <unistd.h>
4799064799SGarrett Wollman #include <string.h>
4899064799SGarrett Wollman #include <rpc/rpc.h>
4999064799SGarrett Wollman #include <sys/socket.h>
5099064799SGarrett Wollman #include <netdb.h>
5199064799SGarrett Wollman 
5299064799SGarrett Wollman static struct callrpc_private {
5399064799SGarrett Wollman 	CLIENT	*client;
5499064799SGarrett Wollman 	int	socket;
5599064799SGarrett Wollman 	int	oldprognum, oldversnum, valid;
5699064799SGarrett Wollman 	char	*oldhost;
5799064799SGarrett Wollman } *callrpc_private;
5899064799SGarrett Wollman 
590ab74c6fSPeter Wemm int
600ab74c6fSPeter Wemm callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
6199064799SGarrett Wollman 	char *host;
62f12d1a5dSJames Raynard 	int prognum, versnum, procnum;
6399064799SGarrett Wollman 	xdrproc_t inproc, outproc;
6499064799SGarrett Wollman 	char *in, *out;
6599064799SGarrett Wollman {
6699064799SGarrett Wollman 	register struct callrpc_private *crp = callrpc_private;
6799064799SGarrett Wollman 	struct sockaddr_in server_addr;
6899064799SGarrett Wollman 	enum clnt_stat clnt_stat;
6999064799SGarrett Wollman 	struct hostent *hp;
7099064799SGarrett Wollman 	struct timeval timeout, tottimeout;
7199064799SGarrett Wollman 
7299064799SGarrett Wollman 	if (crp == 0) {
7399064799SGarrett Wollman 		crp = (struct callrpc_private *)calloc(1, sizeof (*crp));
7499064799SGarrett Wollman 		if (crp == 0)
7599064799SGarrett Wollman 			return (0);
7699064799SGarrett Wollman 		callrpc_private = crp;
7799064799SGarrett Wollman 	}
7899064799SGarrett Wollman 	if (crp->oldhost == NULL) {
790ab74c6fSPeter Wemm 		crp->oldhost = malloc(MAXHOSTNAMELEN);
8099064799SGarrett Wollman 		crp->oldhost[0] = 0;
8199064799SGarrett Wollman 		crp->socket = RPC_ANYSOCK;
8299064799SGarrett Wollman 	}
8399064799SGarrett Wollman 	if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
8499064799SGarrett Wollman 		&& strcmp(crp->oldhost, host) == 0) {
8599064799SGarrett Wollman 		/* reuse old client */
8699064799SGarrett Wollman 	} else {
8799064799SGarrett Wollman 		crp->valid = 0;
880ab74c6fSPeter Wemm 		if (crp->socket != -1)
8999064799SGarrett Wollman 			(void)close(crp->socket);
9099064799SGarrett Wollman 		crp->socket = RPC_ANYSOCK;
9199064799SGarrett Wollman 		if (crp->client) {
9299064799SGarrett Wollman 			clnt_destroy(crp->client);
9399064799SGarrett Wollman 			crp->client = NULL;
9499064799SGarrett Wollman 		}
9599064799SGarrett Wollman 		if ((hp = gethostbyname(host)) == NULL)
9699064799SGarrett Wollman 			return ((int) RPC_UNKNOWNHOST);
9799064799SGarrett Wollman 		timeout.tv_usec = 0;
9899064799SGarrett Wollman 		timeout.tv_sec = 5;
9954edc0bbSPeter Wemm 		memset(&server_addr, 0, sizeof(server_addr));
1000ab74c6fSPeter Wemm 		memcpy((char *)&server_addr.sin_addr, hp->h_addr, hp->h_length);
10154edc0bbSPeter Wemm 		server_addr.sin_len = sizeof(struct sockaddr_in);
10299064799SGarrett Wollman 		server_addr.sin_family = AF_INET;
10399064799SGarrett Wollman 		server_addr.sin_port =  0;
10499064799SGarrett Wollman 		if ((crp->client = clntudp_create(&server_addr, (u_long)prognum,
10599064799SGarrett Wollman 		    (u_long)versnum, timeout, &crp->socket)) == NULL)
10699064799SGarrett Wollman 			return ((int) rpc_createerr.cf_stat);
10799064799SGarrett Wollman 		crp->valid = 1;
10899064799SGarrett Wollman 		crp->oldprognum = prognum;
10999064799SGarrett Wollman 		crp->oldversnum = versnum;
11099064799SGarrett Wollman 		(void) strcpy(crp->oldhost, host);
11199064799SGarrett Wollman 	}
11299064799SGarrett Wollman 	tottimeout.tv_sec = 25;
11399064799SGarrett Wollman 	tottimeout.tv_usec = 0;
11499064799SGarrett Wollman 	clnt_stat = clnt_call(crp->client, procnum, inproc, in,
11599064799SGarrett Wollman 	    outproc, out, tottimeout);
11699064799SGarrett Wollman 	/*
11799064799SGarrett Wollman 	 * if call failed, empty cache
11899064799SGarrett Wollman 	 */
11999064799SGarrett Wollman 	if (clnt_stat != RPC_SUCCESS)
12099064799SGarrett Wollman 		crp->valid = 0;
12199064799SGarrett Wollman 	return ((int) clnt_stat);
12299064799SGarrett Wollman }
123