xref: /openbsd-src/lib/libc/rpc/clnt_simple.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *rcsid = "$OpenBSD: clnt_simple.c,v 1.8 1998/03/19 00:27:20 millert Exp $";
33 #endif /* LIBC_SCCS and not lint */
34 
35 /*
36  * clnt_simple.c
37  * Simplified front end to rpc.
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  */
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <rpc/rpc.h>
46 #include <sys/socket.h>
47 #include <netdb.h>
48 #include <unistd.h>
49 
50 static struct callrpc_private {
51 	CLIENT	*client;
52 	int	socket;
53 	int	oldprognum, oldversnum, valid;
54 	char	*oldhost;
55 } *callrpc_private;
56 
57 int
58 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
59 	char *host;
60 	int prognum, versnum, procnum;
61 	xdrproc_t inproc, outproc;
62 	char *in, *out;
63 {
64 	register struct callrpc_private *crp = callrpc_private;
65 	struct sockaddr_in server_addr;
66 	enum clnt_stat clnt_stat;
67 	struct hostent *hp;
68 	struct timeval timeout, tottimeout;
69 
70 	if (crp == 0) {
71 		crp = (struct callrpc_private *)calloc(1, sizeof (*crp));
72 		if (crp == 0)
73 			return (0);
74 		callrpc_private = crp;
75 	}
76 	if (crp->oldhost == NULL) {
77 		crp->oldhost = malloc(MAXHOSTNAMELEN);
78 		crp->oldhost[0] = 0;
79 		crp->socket = RPC_ANYSOCK;
80 	}
81 	if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
82 		&& strcmp(crp->oldhost, host) == 0) {
83 		/* reuse old client */
84 	} else {
85 		crp->valid = 0;
86 		if (crp->socket != -1)
87 			(void)close(crp->socket);
88 		crp->socket = RPC_ANYSOCK;
89 		if (crp->client) {
90 			clnt_destroy(crp->client);
91 			crp->client = NULL;
92 		}
93 		if ((hp = gethostbyname(host)) == NULL)
94 			return ((int) RPC_UNKNOWNHOST);
95 		timeout.tv_usec = 0;
96 		timeout.tv_sec = 5;
97 		memset(&server_addr, 0, sizeof(server_addr));
98 		memcpy((char *)&server_addr.sin_addr, hp->h_addr, hp->h_length);
99 		server_addr.sin_len = sizeof(struct sockaddr_in);
100 		server_addr.sin_family = AF_INET;
101 		server_addr.sin_port =  0;
102 		if ((crp->client = clntudp_create(&server_addr, (u_long)prognum,
103 		    (u_long)versnum, timeout, &crp->socket)) == NULL)
104 			return ((int) rpc_createerr.cf_stat);
105 		crp->valid = 1;
106 		crp->oldprognum = prognum;
107 		crp->oldversnum = versnum;
108 		(void) strncpy(crp->oldhost, host, MAXHOSTNAMELEN-1);
109 		crp->oldhost[MAXHOSTNAMELEN-1] = '\0';
110 	}
111 	tottimeout.tv_sec = 25;
112 	tottimeout.tv_usec = 0;
113 	clnt_stat = clnt_call(crp->client, procnum, inproc, in,
114 	    outproc, out, tottimeout);
115 	/*
116 	 * if call failed, empty cache
117 	 */
118 	if (clnt_stat != RPC_SUCCESS)
119 		crp->valid = 0;
120 	return ((int) clnt_stat);
121 }
122