clnt_generic.c revision 92990
1/*	$NetBSD: clnt_generic.c,v 1.18 2000/07/06 03:10:34 christos Exp $	*/
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part.  Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California  94043
30 */
31
32/* #ident	"@(#)clnt_generic.c	1.20	94/05/03 SMI" */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI";*/
36/*static char *sccsid = "from: @(#)clnt_generic.c	2.2 88/08/01 4.0 RPCSRC";*/
37#endif
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/lib/libc/rpc/clnt_generic.c 92990 2002-03-22 23:18:37Z obrien $");
40
41/*
42 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
43 */
44#include "namespace.h"
45#include "reentrant.h"
46#include <sys/types.h>
47#include <sys/socket.h>
48#include <netinet/in.h>
49#include <netinet/tcp.h>
50#include <stdio.h>
51#include <errno.h>
52#include <netdb.h>
53#include <rpc/rpc.h>
54#include <rpc/nettype.h>
55#include <string.h>
56#include <stdlib.h>
57#include <unistd.h>
58#include "un-namespace.h"
59#include "rpc_com.h"
60
61/*
62 * Generic client creation with version checking the value of
63 * vers_out is set to the highest server supported value
64 * vers_low <= vers_out <= vers_high  AND an error results
65 * if this can not be done.
66 */
67CLIENT *
68clnt_create_vers(hostname, prog, vers_out, vers_low, vers_high, nettype)
69	const char *hostname;
70	rpcprog_t prog;
71	rpcvers_t *vers_out;
72	rpcvers_t vers_low;
73	rpcvers_t vers_high;
74	const char *nettype;
75{
76	CLIENT *clnt;
77	struct timeval to;
78	enum clnt_stat rpc_stat;
79	struct rpc_err rpcerr;
80
81	clnt = clnt_create(hostname, prog, vers_high, nettype);
82	if (clnt == NULL) {
83		return (NULL);
84	}
85	to.tv_sec = 10;
86	to.tv_usec = 0;
87	rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void,
88			(char *) NULL, (xdrproc_t) xdr_void, (char *) NULL, to);
89	if (rpc_stat == RPC_SUCCESS) {
90		*vers_out = vers_high;
91		return (clnt);
92	}
93	if (rpc_stat == RPC_PROGVERSMISMATCH) {
94		unsigned long minvers, maxvers;
95
96		clnt_geterr(clnt, &rpcerr);
97		minvers = rpcerr.re_vers.low;
98		maxvers = rpcerr.re_vers.high;
99		if (maxvers < vers_high)
100			vers_high = (rpcvers_t)maxvers;
101		if (minvers > vers_low)
102			vers_low = (rpcvers_t)minvers;
103		if (vers_low > vers_high) {
104			goto error;
105		}
106		CLNT_CONTROL(clnt, CLSET_VERS, (char *)(void *)&vers_high);
107		rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void,
108				(char *) NULL, (xdrproc_t) xdr_void,
109				(char *) NULL, to);
110		if (rpc_stat == RPC_SUCCESS) {
111			*vers_out = vers_high;
112			return (clnt);
113		}
114	}
115	clnt_geterr(clnt, &rpcerr);
116
117error:
118	rpc_createerr.cf_stat = rpc_stat;
119	rpc_createerr.cf_error = rpcerr;
120	clnt_destroy(clnt);
121	return (NULL);
122}
123
124/*
125 * Top level client creation routine.
126 * Generic client creation: takes (servers name, program-number, nettype) and
127 * returns client handle. Default options are set, which the user can
128 * change using the rpc equivalent of _ioctl()'s.
129 *
130 * It tries for all the netids in that particular class of netid until
131 * it succeeds.
132 * XXX The error message in the case of failure will be the one
133 * pertaining to the last create error.
134 *
135 * It calls clnt_tp_create();
136 */
137CLIENT *
138clnt_create(hostname, prog, vers, nettype)
139	const char *hostname;				/* server name */
140	rpcprog_t prog;				/* program number */
141	rpcvers_t vers;				/* version number */
142	const char *nettype;				/* net type */
143{
144	struct netconfig *nconf;
145	CLIENT *clnt = NULL;
146	void *handle;
147	enum clnt_stat	save_cf_stat = RPC_SUCCESS;
148	struct rpc_err	save_cf_error;
149
150
151	if ((handle = __rpc_setconf(nettype)) == NULL) {
152		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
153		return (NULL);
154	}
155	rpc_createerr.cf_stat = RPC_SUCCESS;
156	while (clnt == NULL) {
157		if ((nconf = __rpc_getconf(handle)) == NULL) {
158			if (rpc_createerr.cf_stat == RPC_SUCCESS)
159				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
160			break;
161		}
162#ifdef CLNT_DEBUG
163		printf("trying netid %s\n", nconf->nc_netid);
164#endif
165		clnt = clnt_tp_create(hostname, prog, vers, nconf);
166		if (clnt)
167			break;
168		else
169			/*
170			 *	Since we didn't get a name-to-address
171			 *	translation failure here, we remember
172			 *	this particular error.  The object of
173			 *	this is to enable us to return to the
174			 *	caller a more-specific error than the
175			 *	unhelpful ``Name to address translation
176			 *	failed'' which might well occur if we
177			 *	merely returned the last error (because
178			 *	the local loopbacks are typically the
179			 *	last ones in /etc/netconfig and the most
180			 *	likely to be unable to translate a host
181			 *	name).
182			 */
183			if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE) {
184				save_cf_stat = rpc_createerr.cf_stat;
185				save_cf_error = rpc_createerr.cf_error;
186			}
187	}
188
189	/*
190	 *	Attempt to return an error more specific than ``Name to address
191	 *	translation failed''
192	 */
193	if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE) &&
194		(save_cf_stat != RPC_SUCCESS)) {
195		rpc_createerr.cf_stat = save_cf_stat;
196		rpc_createerr.cf_error = save_cf_error;
197	}
198	__rpc_endconf(handle);
199	return (clnt);
200}
201
202/*
203 * Generic client creation: takes (servers name, program-number, netconf) and
204 * returns client handle. Default options are set, which the user can
205 * change using the rpc equivalent of _ioctl()'s : clnt_control()
206 * It finds out the server address from rpcbind and calls clnt_tli_create()
207 */
208CLIENT *
209clnt_tp_create(hostname, prog, vers, nconf)
210	const char *hostname;			/* server name */
211	rpcprog_t prog;				/* program number */
212	rpcvers_t vers;				/* version number */
213	const struct netconfig *nconf;		/* net config struct */
214{
215	struct netbuf *svcaddr;			/* servers address */
216	CLIENT *cl = NULL;			/* client handle */
217
218	if (nconf == NULL) {
219		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
220		return (NULL);
221	}
222
223	/*
224	 * Get the address of the server
225	 */
226	if ((svcaddr = __rpcb_findaddr(prog, vers, nconf, hostname,
227		&cl)) == NULL) {
228		/* appropriate error number is set by rpcbind libraries */
229		return (NULL);
230	}
231	if (cl == NULL) {
232		cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
233					prog, vers, 0, 0);
234	} else {
235		/* Reuse the CLIENT handle and change the appropriate fields */
236		if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
237			if (cl->cl_netid == NULL)
238				cl->cl_netid = strdup(nconf->nc_netid);
239			if (cl->cl_tp == NULL)
240				cl->cl_tp = strdup(nconf->nc_device);
241			(void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
242			(void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
243		} else {
244			CLNT_DESTROY(cl);
245			cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
246					prog, vers, 0, 0);
247		}
248	}
249	free(svcaddr->buf);
250	free(svcaddr);
251	return (cl);
252}
253
254/*
255 * Generic client creation:  returns client handle.
256 * Default options are set, which the user can
257 * change using the rpc equivalent of _ioctl()'s : clnt_control().
258 * If fd is RPC_ANYFD, it will be opened using nconf.
259 * It will be bound if not so.
260 * If sizes are 0; appropriate defaults will be chosen.
261 */
262CLIENT *
263clnt_tli_create(fd, nconf, svcaddr, prog, vers, sendsz, recvsz)
264	int fd;				/* fd */
265	const struct netconfig *nconf;	/* netconfig structure */
266	const struct netbuf *svcaddr;	/* servers address */
267	rpcprog_t prog;			/* program number */
268	rpcvers_t vers;			/* version number */
269	u_int sendsz;			/* send size */
270	u_int recvsz;			/* recv size */
271{
272	CLIENT *cl;			/* client handle */
273	bool_t madefd = FALSE;		/* whether fd opened here */
274	long servtype;
275	int one = 1;
276	struct __rpc_sockinfo si;
277
278	if (fd == RPC_ANYFD) {
279		if (nconf == NULL) {
280			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
281			return (NULL);
282		}
283
284		fd = __rpc_nconf2fd(nconf);
285
286		if (fd == -1)
287			goto err;
288
289		madefd = TRUE;
290		servtype = nconf->nc_semantics;
291		if (!__rpc_fd2sockinfo(fd, &si))
292			goto err;
293
294		bindresvport(fd, NULL);
295	} else {
296		if (!__rpc_fd2sockinfo(fd, &si))
297			goto err;
298		servtype = __rpc_socktype2seman(si.si_socktype);
299		if (servtype == -1) {
300			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
301			return NULL;
302		}
303
304	}
305
306	if (si.si_af != ((struct sockaddr *)svcaddr->buf)->sa_family) {
307		rpc_createerr.cf_stat = RPC_UNKNOWNHOST;	/* XXX */
308		goto err1;
309	}
310
311	switch (servtype) {
312	case NC_TPI_COTS_ORD:
313		cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
314		if (!nconf || !cl)
315			break;
316		/* XXX fvdl - is this useful? */
317		if (strncmp(nconf->nc_protofmly, "inet", 4) == 0)
318			_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one,
319			    sizeof (one));
320		break;
321	case NC_TPI_CLTS:
322		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
323		break;
324	default:
325		goto err;
326	}
327
328	if (cl == NULL)
329		goto err1; /* borrow errors from clnt_dg/vc creates */
330	if (nconf) {
331		cl->cl_netid = strdup(nconf->nc_netid);
332		cl->cl_tp = strdup(nconf->nc_device);
333	} else {
334		cl->cl_netid = "";
335		cl->cl_tp = "";
336	}
337	if (madefd) {
338		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
339/*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, (char *) NULL);  */
340	};
341
342	return (cl);
343
344err:
345	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
346	rpc_createerr.cf_error.re_errno = errno;
347err1:	if (madefd)
348		(void)_close(fd);
349	return (NULL);
350}
351