1 /* $NetBSD: clnt_generic.c,v 1.27 2008/04/25 17:44:44 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 /* #ident "@(#)clnt_generic.c 1.20 94/05/03 SMI" */
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid
[] = "@(#)clnt_generic.c 1.32 89/03/16 Copyr 1988 Sun Micro";
42 __RCSID("$NetBSD: clnt_generic.c,v 1.27 2008/04/25 17:44:44 christos Exp $");
46 #include "namespace.h"
47 #include "reentrant.h"
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
55 #include <rpc/nettype.h>
59 #include "rpc_internal.h"
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
)
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.
75 clnt_create_vers(hostname
, prog
, vers_out
, vers_low
, vers_high
, nettype
)
85 enum clnt_stat rpc_stat
;
86 struct rpc_err rpcerr
;
88 _DIAGASSERT(hostname
!= NULL
);
89 _DIAGASSERT(vers_out
!= NULL
);
90 /* XXX: nettype appears to support being NULL */
92 clnt
= clnt_create(hostname
, prog
, vers_high
, nettype
);
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
;
104 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
105 unsigned long minvers
, maxvers
;
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
) {
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
;
125 clnt_geterr(clnt
, &rpcerr
);
128 rpc_createerr
.cf_stat
= rpc_stat
;
129 rpc_createerr
.cf_error
= rpcerr
;
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.
140 * It tries for all the netids in that particular class of netid until
142 * XXX The error message in the case of failure will be the one
143 * pertaining to the last create error.
145 * It calls clnt_tp_create();
148 clnt_create(hostname
, prog
, vers
, nettype
)
149 const char *hostname
; /* server name */
150 rpcprog_t prog
; /* program number */
151 rpcvers_t vers
; /* version number */
152 const char *nettype
; /* net type */
154 struct netconfig
*nconf
;
157 enum clnt_stat save_cf_stat
= RPC_SUCCESS
;
158 struct rpc_err save_cf_error
;
160 _DIAGASSERT(hostname
!= NULL
);
161 /* XXX: nettype appears to support being NULL */
163 if ((handle
= __rpc_setconf(nettype
)) == NULL
) {
164 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
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
;
175 printf("trying netid %s\n", nconf
->nc_netid
);
177 clnt
= clnt_tp_create(hostname
, prog
, vers
, nconf
);
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
195 if (rpc_createerr
.cf_stat
!= RPC_N2AXLATEFAILURE
) {
196 save_cf_stat
= rpc_createerr
.cf_stat
;
197 save_cf_error
= rpc_createerr
.cf_error
;
202 * Attempt to return an error more specific than ``Name to address
203 * translation failed''
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
;
210 __rpc_endconf(handle
);
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()
221 clnt_tp_create(hostname
, prog
, vers
, nconf
)
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 */
227 struct netbuf
*svcaddr
; /* servers address */
228 CLIENT
*cl
= NULL
; /* client handle */
230 _DIAGASSERT(hostname
!= NULL
);
231 /* nconf is handled below */
234 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
239 * Get the address of the server
241 if ((svcaddr
= __rpcb_findaddr(prog
, vers
, nconf
, hostname
,
243 /* appropriate error number is set by rpcbind libraries */
247 cl
= clnt_tli_create(RPC_ANYFD
, nconf
, svcaddr
,
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
)
257 if (cl
->cl_tp
== NULL
) {
258 cl
->cl_tp
= strdup(nconf
->nc_device
);
259 if (cl
->cl_tp
== NULL
)
262 (void) CLNT_CONTROL(cl
, CLSET_PROG
, (void *)&prog
);
263 (void) CLNT_CONTROL(cl
, CLSET_VERS
, (void *)&vers
);
266 cl
= clnt_tli_create(RPC_ANYFD
, nconf
, svcaddr
,
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.
287 clnt_tli_create(fd
, nconf
, svcaddr
, prog
, vers
, sendsz
, recvsz
)
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 */
296 CLIENT
*cl
; /* client handle */
297 bool_t madefd
= FALSE
; /* whether fd opened here */
299 struct __rpc_sockinfo si
;
301 /* nconf is handled below */
302 _DIAGASSERT(svcaddr
!= NULL
);
304 if (fd
== RPC_ANYFD
) {
306 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
310 fd
= __rpc_nconf2fd(nconf
);
316 servtype
= nconf
->nc_semantics
;
317 if (!__rpc_fd2sockinfo(fd
, &si
))
320 bindresvport(fd
, NULL
);
322 if (!__rpc_fd2sockinfo(fd
, &si
))
324 servtype
= __rpc_socktype2seman(si
.si_socktype
);
325 if (servtype
== -1) {
326 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
332 if (si
.si_af
!= ((struct sockaddr
*)svcaddr
->buf
)->sa_family
) {
333 rpc_createerr
.cf_stat
= RPC_UNKNOWNHOST
; /* XXX */
338 case NC_TPI_COTS_ORD
:
339 cl
= clnt_vc_create(fd
, svcaddr
, prog
, vers
, sendsz
, recvsz
);
342 __rpc_setnodelay(fd
, &si
);
345 cl
= clnt_dg_create(fd
, svcaddr
, prog
, vers
, sendsz
, recvsz
);
352 goto err1
; /* borrow errors from clnt_dg/vc creates */
354 cl
->cl_netid
= strdup(nconf
->nc_netid
);
355 if (cl
->cl_netid
== NULL
)
357 cl
->cl_tp
= strdup(nconf
->nc_device
);
358 if (cl
->cl_tp
== NULL
)
361 cl
->cl_netid
= __UNCONST("");
362 cl
->cl_tp
= __UNCONST("");
365 (void) CLNT_CONTROL(cl
, CLSET_FD_CLOSE
, NULL
);
366 /* (void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL); */
374 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
375 rpc_createerr
.cf_error
.re_errno
= errno
;