xref: /openbsd/lib/libc/rpc/clnt_simple.c (revision fa713987)
1 /*	$OpenBSD: clnt_simple.c,v 1.18 2015/08/20 21:49:29 deraadt Exp $ */
2 
3 /*
4  * Copyright (c) 2010, Oracle America, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials
15  *       provided with the distribution.
16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * clnt_simple.c
36  * Simplified front end to rpc.
37  */
38 
39 #include <sys/socket.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <limits.h>
46 #include <rpc/rpc.h>
47 
48 static struct callrpc_private {
49 	CLIENT	*client;
50 	int	socket;
51 	int	oldprognum, oldversnum, valid;
52 	char	*oldhost;
53 } *callrpc_private;
54 
55 int
callrpc(char * host,int prognum,int versnum,int procnum,xdrproc_t inproc,char * in,xdrproc_t outproc,char * out)56 callrpc(char *host, int prognum, int versnum, int procnum, xdrproc_t inproc,
57     char *in, xdrproc_t outproc, char *out)
58 {
59 	struct callrpc_private *save_callrpc_private = callrpc_private;
60 	struct callrpc_private *crp = callrpc_private;
61 	struct sockaddr_in server_addr;
62 	enum clnt_stat clnt_stat;
63 	struct hostent *hp;
64 	struct timeval timeout, tottimeout;
65 
66 	if (crp == NULL) {
67 		crp = calloc(1, sizeof (*crp));
68 		if (crp == NULL)
69 			return RPC_SYSTEMERROR;
70 		callrpc_private = crp;
71 	}
72 	if (crp->oldhost == NULL) {
73 		crp->oldhost = malloc(HOST_NAME_MAX+1);
74 		if (crp->oldhost == NULL) {
75 			free(crp);
76 			callrpc_private = save_callrpc_private;
77 			return RPC_SYSTEMERROR;
78 		}
79 		crp->oldhost[0] = 0;
80 		crp->socket = RPC_ANYSOCK;
81 	}
82 	if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
83 		&& strcmp(crp->oldhost, host) == 0) {
84 		/* reuse old client */
85 	} else {
86 		crp->valid = 0;
87 		if (crp->socket != -1) {
88 			(void)close(crp->socket);
89 			crp->socket = -1;
90 		}
91 		if (crp->client) {
92 			CLNT_DESTROY(crp->client);
93 			crp->client = NULL;
94 		}
95 		crp->socket = RPC_ANYSOCK;
96 		if ((hp = gethostbyname(host)) == NULL)
97 			return ((int) RPC_UNKNOWNHOST);
98 		timeout.tv_usec = 0;
99 		timeout.tv_sec = 5;
100 		memset(&server_addr, 0, sizeof(server_addr));
101 		memcpy((char *)&server_addr.sin_addr, hp->h_addr, hp->h_length);
102 		server_addr.sin_len = sizeof(struct sockaddr_in);
103 		server_addr.sin_family = AF_INET;
104 		server_addr.sin_port =  0;
105 		if ((crp->client = clntudp_create(&server_addr, (u_long)prognum,
106 		    (u_long)versnum, timeout, &crp->socket)) == NULL)
107 			return ((int) rpc_createerr.cf_stat);
108 		crp->valid = 1;
109 		crp->oldprognum = prognum;
110 		crp->oldversnum = versnum;
111 		strlcpy(crp->oldhost, host, HOST_NAME_MAX+1);
112 	}
113 	tottimeout.tv_sec = 25;
114 	tottimeout.tv_usec = 0;
115 	clnt_stat = clnt_call(crp->client, procnum, inproc, in,
116 	    outproc, out, tottimeout);
117 	/*
118 	 * if call failed, empty cache
119 	 */
120 	if (clnt_stat != RPC_SUCCESS)
121 		crp->valid = 0;
122 	return ((int) clnt_stat);
123 }
124