xref: /netbsd/lib/libc/rpc/clnt_generic.c (revision c4a72b64)
1 /*	$NetBSD: clnt_generic.c,v 1.20 2002/11/08 00:13:07 fvdl 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  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33  */
34 
35 /* #ident	"@(#)clnt_generic.c	1.20	94/05/03 SMI" */
36 
37 #if 0
38 #if !defined(lint) && defined(SCCSIDS)
39 static char sccsid[] = "@(#)clnt_generic.c 1.32 89/03/16 Copyr 1988 Sun Micro";
40 #endif
41 #endif
42 
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <netinet/tcp.h>
49 #include <assert.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <rpc/rpc.h>
53 #include <rpc/nettype.h>
54 #include <string.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include "rpc_internal.h"
58 
59 #ifdef __weak_alias
60 __weak_alias(clnt_create_vers,_clnt_create_vers)
61 __weak_alias(clnt_create,_clnt_create)
62 __weak_alias(clnt_tp_create,_clnt_tp_create)
63 __weak_alias(clnt_tli_create,_clnt_tli_create)
64 #endif
65 
66 /*
67  * Generic client creation with version checking the value of
68  * vers_out is set to the highest server supported value
69  * vers_low <= vers_out <= vers_high  AND an error results
70  * if this can not be done.
71  */
72 CLIENT *
73 clnt_create_vers(hostname, prog, vers_out, vers_low, vers_high, nettype)
74 	const char *hostname;
75 	rpcprog_t prog;
76 	rpcvers_t *vers_out;
77 	rpcvers_t vers_low;
78 	rpcvers_t vers_high;
79 	const char *nettype;
80 {
81 	CLIENT *clnt;
82 	struct timeval to;
83 	enum clnt_stat rpc_stat;
84 	struct rpc_err rpcerr;
85 
86 	_DIAGASSERT(hostname != NULL);
87 	_DIAGASSERT(vers_out != NULL);
88 	/* XXX: nettype appears to support being NULL */
89 
90 	clnt = clnt_create(hostname, prog, vers_high, nettype);
91 	if (clnt == NULL) {
92 		return (NULL);
93 	}
94 	to.tv_sec = 10;
95 	to.tv_usec = 0;
96 	rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void,
97 			(char *) NULL, (xdrproc_t) xdr_void, (char *) NULL, to);
98 	if (rpc_stat == RPC_SUCCESS) {
99 		*vers_out = vers_high;
100 		return (clnt);
101 	}
102 	if (rpc_stat == RPC_PROGVERSMISMATCH) {
103 		unsigned long minvers, maxvers;
104 
105 		clnt_geterr(clnt, &rpcerr);
106 		minvers = rpcerr.re_vers.low;
107 		maxvers = rpcerr.re_vers.high;
108 		if (maxvers < vers_high)
109 			vers_high = (rpcvers_t)maxvers;
110 		if (minvers > vers_low)
111 			vers_low = (rpcvers_t)minvers;
112 		if (vers_low > vers_high) {
113 			goto error;
114 		}
115 		CLNT_CONTROL(clnt, CLSET_VERS, (char *)(void *)&vers_high);
116 		rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void,
117 				(char *) NULL, (xdrproc_t) xdr_void,
118 				(char *) NULL, to);
119 		if (rpc_stat == RPC_SUCCESS) {
120 			*vers_out = vers_high;
121 			return (clnt);
122 		}
123 	}
124 	clnt_geterr(clnt, &rpcerr);
125 
126 error:
127 	rpc_createerr.cf_stat = rpc_stat;
128 	rpc_createerr.cf_error = rpcerr;
129 	clnt_destroy(clnt);
130 	return (NULL);
131 }
132 
133 /*
134  * Top level client creation routine.
135  * Generic client creation: takes (servers name, program-number, nettype) and
136  * returns client handle. Default options are set, which the user can
137  * change using the rpc equivalent of ioctl()'s.
138  *
139  * It tries for all the netids in that particular class of netid until
140  * it succeeds.
141  * XXX The error message in the case of failure will be the one
142  * pertaining to the last create error.
143  *
144  * It calls clnt_tp_create();
145  */
146 CLIENT *
147 clnt_create(hostname, prog, vers, nettype)
148 	const char *hostname;				/* server name */
149 	rpcprog_t prog;				/* program number */
150 	rpcvers_t vers;				/* version number */
151 	const char *nettype;				/* net type */
152 {
153 	struct netconfig *nconf;
154 	CLIENT *clnt = NULL;
155 	void *handle;
156 	enum clnt_stat	save_cf_stat = RPC_SUCCESS;
157 	struct rpc_err	save_cf_error;
158 
159 	_DIAGASSERT(hostname != NULL);
160 	/* XXX: nettype appears to support being NULL */
161 
162 	if ((handle = __rpc_setconf(nettype)) == NULL) {
163 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
164 		return (NULL);
165 	}
166 	rpc_createerr.cf_stat = RPC_SUCCESS;
167 	while (clnt == NULL) {
168 		if ((nconf = __rpc_getconf(handle)) == NULL) {
169 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
170 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
171 			break;
172 		}
173 #ifdef CLNT_DEBUG
174 		printf("trying netid %s\n", nconf->nc_netid);
175 #endif
176 		clnt = clnt_tp_create(hostname, prog, vers, nconf);
177 		if (clnt)
178 			break;
179 		else
180 			/*
181 			 *	Since we didn't get a name-to-address
182 			 *	translation failure here, we remember
183 			 *	this particular error.  The object of
184 			 *	this is to enable us to return to the
185 			 *	caller a more-specific error than the
186 			 *	unhelpful ``Name to address translation
187 			 *	failed'' which might well occur if we
188 			 *	merely returned the last error (because
189 			 *	the local loopbacks are typically the
190 			 *	last ones in /etc/netconfig and the most
191 			 *	likely to be unable to translate a host
192 			 *	name).
193 			 */
194 			if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE) {
195 				save_cf_stat = rpc_createerr.cf_stat;
196 				save_cf_error = rpc_createerr.cf_error;
197 			}
198 	}
199 
200 	/*
201 	 *	Attempt to return an error more specific than ``Name to address
202 	 *	translation failed''
203 	 */
204 	if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE) &&
205 		(save_cf_stat != RPC_SUCCESS)) {
206 		rpc_createerr.cf_stat = save_cf_stat;
207 		rpc_createerr.cf_error = save_cf_error;
208 	}
209 	__rpc_endconf(handle);
210 	return (clnt);
211 }
212 
213 /*
214  * Generic client creation: takes (servers name, program-number, netconf) and
215  * returns client handle. Default options are set, which the user can
216  * change using the rpc equivalent of ioctl()'s : clnt_control()
217  * It finds out the server address from rpcbind and calls clnt_tli_create()
218  */
219 CLIENT *
220 clnt_tp_create(hostname, prog, vers, nconf)
221 	const char *hostname;			/* server name */
222 	rpcprog_t prog;				/* program number */
223 	rpcvers_t vers;				/* version number */
224 	const struct netconfig *nconf;		/* net config struct */
225 {
226 	struct netbuf *svcaddr;			/* servers address */
227 	CLIENT *cl = NULL;			/* client handle */
228 
229 	_DIAGASSERT(hostname != NULL);
230 	/* nconf is handled below */
231 
232 	if (nconf == NULL) {
233 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
234 		return (NULL);
235 	}
236 
237 	/*
238 	 * Get the address of the server
239 	 */
240 	if ((svcaddr = __rpcb_findaddr(prog, vers, nconf, hostname,
241 		&cl)) == NULL) {
242 		/* appropriate error number is set by rpcbind libraries */
243 		return (NULL);
244 	}
245 	if (cl == NULL) {
246 		cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
247 					prog, vers, 0, 0);
248 	} else {
249 		/* Reuse the CLIENT handle and change the appropriate fields */
250 		if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
251 			if (cl->cl_netid == NULL)
252 				cl->cl_netid = strdup(nconf->nc_netid);
253 			if (cl->cl_tp == NULL)
254 				cl->cl_tp = strdup(nconf->nc_device);
255 			(void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
256 			(void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
257 		} else {
258 			CLNT_DESTROY(cl);
259 			cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
260 					prog, vers, 0, 0);
261 		}
262 	}
263 	free(svcaddr->buf);
264 	free(svcaddr);
265 	return (cl);
266 }
267 
268 /*
269  * Generic client creation:  returns client handle.
270  * Default options are set, which the user can
271  * change using the rpc equivalent of ioctl()'s : clnt_control().
272  * If fd is RPC_ANYFD, it will be opened using nconf.
273  * It will be bound if not so.
274  * If sizes are 0; appropriate defaults will be chosen.
275  */
276 CLIENT *
277 clnt_tli_create(fd, nconf, svcaddr, prog, vers, sendsz, recvsz)
278 	int fd;				/* fd */
279 	const struct netconfig *nconf;	/* netconfig structure */
280 	const struct netbuf *svcaddr;	/* servers address */
281 	rpcprog_t prog;			/* program number */
282 	rpcvers_t vers;			/* version number */
283 	u_int sendsz;			/* send size */
284 	u_int recvsz;			/* recv size */
285 {
286 	CLIENT *cl;			/* client handle */
287 	bool_t madefd = FALSE;		/* whether fd opened here */
288 	long servtype;
289 	int one = 1;
290 	struct __rpc_sockinfo si;
291 
292 	/* nconf is handled below */
293 	_DIAGASSERT(svcaddr != NULL);
294 
295 	if (fd == RPC_ANYFD) {
296 		if (nconf == NULL) {
297 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
298 			return (NULL);
299 		}
300 
301 		fd = __rpc_nconf2fd(nconf);
302 
303 		if (fd == -1)
304 			goto err;
305 
306 		madefd = TRUE;
307 		servtype = nconf->nc_semantics;
308 		if (!__rpc_fd2sockinfo(fd, &si))
309 			goto err;
310 
311 		bindresvport(fd, NULL);
312 	} else {
313 		if (!__rpc_fd2sockinfo(fd, &si))
314 			goto err;
315 		servtype = __rpc_socktype2seman(si.si_socktype);
316 		if (servtype == -1) {
317 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
318 			return NULL;
319 		}
320 
321 	}
322 
323 	if (si.si_af != ((struct sockaddr *)svcaddr->buf)->sa_family) {
324 		rpc_createerr.cf_stat = RPC_UNKNOWNHOST;	/* XXX */
325 		goto err1;
326 	}
327 
328 	switch (servtype) {
329 	case NC_TPI_COTS_ORD:
330 		cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
331 		if (!nconf || !cl)
332 			break;
333 		/* XXX fvdl - is this useful? */
334 		if (strncmp(nconf->nc_protofmly, "inet", 4) == 0)
335 			setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one,
336 			    sizeof (one));
337 		break;
338 	case NC_TPI_CLTS:
339 		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
340 		break;
341 	default:
342 		goto err;
343 	}
344 
345 	if (cl == NULL)
346 		goto err1; /* borrow errors from clnt_dg/vc creates */
347 	if (nconf) {
348 		cl->cl_netid = strdup(nconf->nc_netid);
349 		cl->cl_tp = strdup(nconf->nc_device);
350 	} else {
351 		cl->cl_netid = "";
352 		cl->cl_tp = "";
353 	}
354 	if (madefd) {
355 		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
356 /*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, (char *) NULL);  */
357 	};
358 
359 	return (cl);
360 
361 err:
362 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
363 	rpc_createerr.cf_error.re_errno = errno;
364 err1:	if (madefd)
365 		(void) close(fd);
366 	return (NULL);
367 }
368