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