1 /* $NetBSD: clnt_generic.c,v 1.18 2000/07/06 03:10:34 christos Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of the "Oracle America, Inc." nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 /* #ident "@(#)clnt_generic.c 1.40 99/04/21 SMI" */
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "from: @(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI";
35 static char *sccsid = "from: @(#)clnt_generic.c 2.2 88/08/01 4.0 RPCSRC";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * Copyright (c) 1986-1996,1998 by Sun Microsystems, Inc.
42 * All rights reserved.
44 #include "namespace.h"
45 #include "reentrant.h"
46 #include <sys/types.h>
47 #include <sys/fcntl.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <netinet/tcp.h>
56 #include <rpc/nettype.h>
60 #include "un-namespace.h"
63 extern bool_t __rpc_is_local_host(const char *);
64 int __rpc_raise_fd(int);
72 * Generic client creation with version checking the value of
73 * vers_out is set to the highest server supported value
74 * vers_low <= vers_out <= vers_high AND an error results
75 * if this can not be done.
77 * It calls clnt_create_vers_timed() with a NULL value for the timeout
78 * pointer, which indicates that the default timeout should be used.
81 clnt_create_vers(const char *hostname, rpcprog_t prog, rpcvers_t *vers_out,
82 rpcvers_t vers_low, rpcvers_t vers_high, const char *nettype)
85 return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low,
86 vers_high, nettype, NULL));
90 * This the routine has the same definition as clnt_create_vers(),
91 * except it takes an additional timeout parameter - a pointer to
92 * a timeval structure. A NULL value for the pointer indicates
93 * that the default timeout value should be used.
96 clnt_create_vers_timed(const char *hostname, rpcprog_t prog,
97 rpcvers_t *vers_out, rpcvers_t vers_low, rpcvers_t vers_high,
98 const char *nettype, const struct timeval *tp)
102 enum clnt_stat rpc_stat;
103 struct rpc_err rpcerr;
105 clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp);
111 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
112 (char *)NULL, (xdrproc_t)xdr_void, (char *)NULL, to);
113 if (rpc_stat == RPC_SUCCESS) {
114 *vers_out = vers_high;
117 while (rpc_stat == RPC_PROGVERSMISMATCH && vers_high > vers_low) {
118 unsigned int minvers, maxvers;
120 clnt_geterr(clnt, &rpcerr);
121 minvers = rpcerr.re_vers.low;
122 maxvers = rpcerr.re_vers.high;
123 if (maxvers < vers_high)
127 if (minvers > vers_low)
129 if (vers_low > vers_high) {
132 CLNT_CONTROL(clnt, CLSET_VERS, (char *)&vers_high);
133 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
134 (char *)NULL, (xdrproc_t)xdr_void,
136 if (rpc_stat == RPC_SUCCESS) {
137 *vers_out = vers_high;
141 clnt_geterr(clnt, &rpcerr);
144 rpc_createerr.cf_stat = rpc_stat;
145 rpc_createerr.cf_error = rpcerr;
151 * Top level client creation routine.
152 * Generic client creation: takes (servers name, program-number, nettype) and
153 * returns client handle. Default options are set, which the user can
154 * change using the rpc equivalent of _ioctl()'s.
156 * It tries for all the netids in that particular class of netid until
158 * XXX The error message in the case of failure will be the one
159 * pertaining to the last create error.
161 * It calls clnt_create_timed() with the default timeout.
164 clnt_create(const char *hostname, rpcprog_t prog, rpcvers_t vers,
168 return (clnt_create_timed(hostname, prog, vers, nettype, NULL));
172 * This the routine has the same definition as clnt_create(),
173 * except it takes an additional timeout parameter - a pointer to
174 * a timeval structure. A NULL value for the pointer indicates
175 * that the default timeout value should be used.
177 * This function calls clnt_tp_create_timed().
180 clnt_create_timed(const char *hostname, rpcprog_t prog, rpcvers_t vers,
181 const char *netclass, const struct timeval *tp)
183 struct netconfig *nconf;
186 enum clnt_stat save_cf_stat = RPC_SUCCESS;
187 struct rpc_err save_cf_error;
188 char nettype_array[NETIDLEN];
189 char *nettype = &nettype_array[0];
191 if (netclass == NULL)
194 size_t len = strlen(netclass);
195 if (len >= sizeof (nettype_array)) {
196 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
199 strcpy(nettype, netclass);
202 if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
203 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
206 rpc_createerr.cf_stat = RPC_SUCCESS;
207 while (clnt == NULL) {
208 if ((nconf = __rpc_getconf(handle)) == NULL) {
209 if (rpc_createerr.cf_stat == RPC_SUCCESS)
210 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
214 printf("trying netid %s\n", nconf->nc_netid);
216 clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp);
221 * Since we didn't get a name-to-address
222 * translation failure here, we remember
223 * this particular error. The object of
224 * this is to enable us to return to the
225 * caller a more-specific error than the
226 * unhelpful ``Name to address translation
227 * failed'' which might well occur if we
228 * merely returned the last error (because
229 * the local loopbacks are typically the
230 * last ones in /etc/netconfig and the most
231 * likely to be unable to translate a host
232 * name). We also check for a more
233 * meaningful error than ``unknown host
234 * name'' for the same reasons.
236 if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE &&
237 rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
238 save_cf_stat = rpc_createerr.cf_stat;
239 save_cf_error = rpc_createerr.cf_error;
244 * Attempt to return an error more specific than ``Name to address
245 * translation failed'' or ``unknown host name''
247 if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE ||
248 rpc_createerr.cf_stat == RPC_UNKNOWNHOST) &&
249 (save_cf_stat != RPC_SUCCESS)) {
250 rpc_createerr.cf_stat = save_cf_stat;
251 rpc_createerr.cf_error = save_cf_error;
253 __rpc_endconf(handle);
258 * Generic client creation: takes (servers name, program-number, netconf) and
259 * returns client handle. Default options are set, which the user can
260 * change using the rpc equivalent of _ioctl()'s : clnt_control()
261 * It finds out the server address from rpcbind and calls clnt_tli_create().
263 * It calls clnt_tp_create_timed() with the default timeout.
266 clnt_tp_create(const char *hostname, rpcprog_t prog, rpcvers_t vers,
267 const struct netconfig *nconf)
270 return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL));
274 * This has the same definition as clnt_tp_create(), except it
275 * takes an additional parameter - a pointer to a timeval structure.
276 * A NULL value for the timeout pointer indicates that the default
277 * value for the timeout should be used.
280 clnt_tp_create_timed(const char *hostname, rpcprog_t prog, rpcvers_t vers,
281 const struct netconfig *nconf, const struct timeval *tp)
283 struct netbuf *svcaddr; /* servers address */
284 CLIENT *cl = NULL; /* client handle */
287 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
292 * Get the address of the server
294 if ((svcaddr = __rpcb_findaddr_timed(prog, vers,
295 (struct netconfig *)nconf, (char *)hostname,
296 &cl, (struct timeval *)tp)) == NULL) {
297 /* appropriate error number is set by rpcbind libraries */
301 cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
304 /* Reuse the CLIENT handle and change the appropriate fields */
305 if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
306 if (cl->cl_netid == NULL)
307 cl->cl_netid = strdup(nconf->nc_netid);
308 if (cl->cl_tp == NULL)
309 cl->cl_tp = strdup(nconf->nc_device);
310 (void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
311 (void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
314 cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
324 * Generic client creation: returns client handle.
325 * Default options are set, which the user can
326 * change using the rpc equivalent of _ioctl()'s : clnt_control().
327 * If fd is RPC_ANYFD, it will be opened using nconf.
328 * It will be bound if not so.
329 * If sizes are 0; appropriate defaults will be chosen.
332 clnt_tli_create(int fd, const struct netconfig *nconf,
333 struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers,
334 uint sendsz, uint recvsz)
336 CLIENT *cl; /* client handle */
337 bool_t madefd = FALSE; /* whether fd opened here */
340 struct __rpc_sockinfo si;
341 extern int __rpc_minfd;
343 if (fd == RPC_ANYFD) {
345 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
349 fd = __rpc_nconf2fd(nconf);
353 if (fd < __rpc_minfd)
354 fd = __rpc_raise_fd(fd);
356 servtype = nconf->nc_semantics;
357 if (!__rpc_fd2sockinfo(fd, &si))
359 bindresvport(fd, NULL);
361 if (!__rpc_fd2sockinfo(fd, &si))
363 servtype = __rpc_socktype2seman(si.si_socktype);
364 if (servtype == -1) {
365 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
370 if (si.si_af != ((struct sockaddr *)svcaddr->buf)->sa_family) {
371 rpc_createerr.cf_stat = RPC_UNKNOWNHOST; /* XXX */
377 cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
379 case NC_TPI_COTS_ORD:
380 if (nconf && ((strcmp(nconf->nc_protofmly, "inet") == 0))) {
381 _setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one,
384 cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
387 cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
394 goto err1; /* borrow errors from clnt_dg/vc creates */
396 cl->cl_netid = strdup(nconf->nc_netid);
397 cl->cl_tp = strdup(nconf->nc_device);
403 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
404 /* (void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL); */
410 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
411 rpc_createerr.cf_error.re_errno = errno;
418 * To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
419 * we try to not use them. The __rpc_raise_fd() routine will dup
420 * a descriptor to a higher value. If we fail to do it, we continue
421 * to use the old one (and hope for the best).
426 __rpc_raise_fd(int fd)
430 if (fd >= __rpc_minfd)
433 if ((nfd = _fcntl(fd, F_DUPFD, __rpc_minfd)) == -1)
436 if (_fsync(nfd) == -1) {
441 if (_close(fd) == -1) {
442 /* this is okay, we will syslog an error, then use the new fd */
443 (void) syslog(LOG_ERR,
444 "could not close() fd %d; mem & fd leak", fd);