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