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