clnt_generic.c revision 1.28 1 1.28 matt /* $NetBSD: clnt_generic.c,v 1.28 2012/03/20 17:14:50 matt Exp $ */
2 1.4 cgd
3 1.1 cgd /*
4 1.1 cgd * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.1 cgd * unrestricted use provided that this legend is included on all tape
6 1.1 cgd * media and as a part of the software program in whole or part. Users
7 1.1 cgd * may copy or modify Sun RPC without charge, but are not authorized
8 1.1 cgd * to license or distribute it to anyone else except as part of a product or
9 1.1 cgd * program developed by the user.
10 1.1 cgd *
11 1.1 cgd * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 cgd * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 cgd * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.1 cgd *
15 1.1 cgd * Sun RPC is provided with no support and without any obligation on the
16 1.1 cgd * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 cgd * modification or enhancement.
18 1.16 fvdl *
19 1.1 cgd * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 cgd * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 cgd * OR ANY PART THEREOF.
22 1.1 cgd *
23 1.1 cgd * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 cgd * or profits or other special, indirect and consequential damages, even if
25 1.1 cgd * Sun has been advised of the possibility of such damages.
26 1.1 cgd *
27 1.1 cgd * Sun Microsystems, Inc.
28 1.1 cgd * 2550 Garcia Avenue
29 1.1 cgd * Mountain View, California 94043
30 1.1 cgd */
31 1.16 fvdl /*
32 1.16 fvdl * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33 1.16 fvdl */
34 1.16 fvdl
35 1.16 fvdl /* #ident "@(#)clnt_generic.c 1.20 94/05/03 SMI" */
36 1.1 cgd
37 1.22 itojun #include <sys/cdefs.h>
38 1.22 itojun #if defined(LIBC_SCCS) && !defined(lint)
39 1.7 christos #if 0
40 1.16 fvdl static char sccsid[] = "@(#)clnt_generic.c 1.32 89/03/16 Copyr 1988 Sun Micro";
41 1.22 itojun #else
42 1.28 matt __RCSID("$NetBSD: clnt_generic.c,v 1.28 2012/03/20 17:14:50 matt Exp $");
43 1.7 christos #endif
44 1.1 cgd #endif
45 1.1 cgd
46 1.8 jtc #include "namespace.h"
47 1.16 fvdl #include "reentrant.h"
48 1.11 lukem #include <sys/types.h>
49 1.11 lukem #include <sys/socket.h>
50 1.16 fvdl #include <netinet/in.h>
51 1.19 lukem #include <assert.h>
52 1.16 fvdl #include <stdio.h>
53 1.11 lukem #include <errno.h>
54 1.16 fvdl #include <rpc/rpc.h>
55 1.18 christos #include <rpc/nettype.h>
56 1.10 lukem #include <string.h>
57 1.16 fvdl #include <stdlib.h>
58 1.16 fvdl #include <unistd.h>
59 1.20 fvdl #include "rpc_internal.h"
60 1.8 jtc
61 1.8 jtc #ifdef __weak_alias
62 1.16 fvdl __weak_alias(clnt_create_vers,_clnt_create_vers)
63 1.15 mycroft __weak_alias(clnt_create,_clnt_create)
64 1.16 fvdl __weak_alias(clnt_tp_create,_clnt_tp_create)
65 1.16 fvdl __weak_alias(clnt_tli_create,_clnt_tli_create)
66 1.8 jtc #endif
67 1.1 cgd
68 1.1 cgd /*
69 1.16 fvdl * Generic client creation with version checking the value of
70 1.16 fvdl * vers_out is set to the highest server supported value
71 1.16 fvdl * vers_low <= vers_out <= vers_high AND an error results
72 1.16 fvdl * if this can not be done.
73 1.16 fvdl */
74 1.16 fvdl CLIENT *
75 1.28 matt clnt_create_vers(
76 1.28 matt const char * hostname,
77 1.28 matt rpcprog_t prog,
78 1.28 matt rpcvers_t * vers_out,
79 1.28 matt rpcvers_t vers_low,
80 1.28 matt rpcvers_t vers_high,
81 1.28 matt const char * nettype)
82 1.16 fvdl {
83 1.16 fvdl CLIENT *clnt;
84 1.16 fvdl struct timeval to;
85 1.16 fvdl enum clnt_stat rpc_stat;
86 1.16 fvdl struct rpc_err rpcerr;
87 1.16 fvdl
88 1.19 lukem _DIAGASSERT(hostname != NULL);
89 1.19 lukem _DIAGASSERT(vers_out != NULL);
90 1.19 lukem /* XXX: nettype appears to support being NULL */
91 1.19 lukem
92 1.16 fvdl clnt = clnt_create(hostname, prog, vers_high, nettype);
93 1.16 fvdl if (clnt == NULL) {
94 1.16 fvdl return (NULL);
95 1.16 fvdl }
96 1.16 fvdl to.tv_sec = 10;
97 1.16 fvdl to.tv_usec = 0;
98 1.16 fvdl rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void,
99 1.27 christos NULL, (xdrproc_t) xdr_void, NULL, to);
100 1.16 fvdl if (rpc_stat == RPC_SUCCESS) {
101 1.16 fvdl *vers_out = vers_high;
102 1.16 fvdl return (clnt);
103 1.16 fvdl }
104 1.16 fvdl if (rpc_stat == RPC_PROGVERSMISMATCH) {
105 1.16 fvdl unsigned long minvers, maxvers;
106 1.16 fvdl
107 1.16 fvdl clnt_geterr(clnt, &rpcerr);
108 1.16 fvdl minvers = rpcerr.re_vers.low;
109 1.16 fvdl maxvers = rpcerr.re_vers.high;
110 1.16 fvdl if (maxvers < vers_high)
111 1.18 christos vers_high = (rpcvers_t)maxvers;
112 1.16 fvdl if (minvers > vers_low)
113 1.18 christos vers_low = (rpcvers_t)minvers;
114 1.16 fvdl if (vers_low > vers_high) {
115 1.16 fvdl goto error;
116 1.16 fvdl }
117 1.18 christos CLNT_CONTROL(clnt, CLSET_VERS, (char *)(void *)&vers_high);
118 1.16 fvdl rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void,
119 1.27 christos NULL, (xdrproc_t) xdr_void, NULL, to);
120 1.16 fvdl if (rpc_stat == RPC_SUCCESS) {
121 1.16 fvdl *vers_out = vers_high;
122 1.16 fvdl return (clnt);
123 1.16 fvdl }
124 1.16 fvdl }
125 1.16 fvdl clnt_geterr(clnt, &rpcerr);
126 1.16 fvdl
127 1.16 fvdl error:
128 1.16 fvdl rpc_createerr.cf_stat = rpc_stat;
129 1.16 fvdl rpc_createerr.cf_error = rpcerr;
130 1.16 fvdl clnt_destroy(clnt);
131 1.16 fvdl return (NULL);
132 1.16 fvdl }
133 1.16 fvdl
134 1.16 fvdl /*
135 1.16 fvdl * Top level client creation routine.
136 1.16 fvdl * Generic client creation: takes (servers name, program-number, nettype) and
137 1.16 fvdl * returns client handle. Default options are set, which the user can
138 1.1 cgd * change using the rpc equivalent of ioctl()'s.
139 1.16 fvdl *
140 1.16 fvdl * It tries for all the netids in that particular class of netid until
141 1.16 fvdl * it succeeds.
142 1.16 fvdl * XXX The error message in the case of failure will be the one
143 1.16 fvdl * pertaining to the last create error.
144 1.16 fvdl *
145 1.16 fvdl * It calls clnt_tp_create();
146 1.1 cgd */
147 1.1 cgd CLIENT *
148 1.28 matt clnt_create(
149 1.28 matt const char * hostname, /* server name */
150 1.28 matt rpcprog_t prog, /* program number */
151 1.28 matt rpcvers_t vers, /* version number */
152 1.28 matt const char * nettype) /* net type */
153 1.1 cgd {
154 1.16 fvdl struct netconfig *nconf;
155 1.16 fvdl CLIENT *clnt = NULL;
156 1.16 fvdl void *handle;
157 1.16 fvdl enum clnt_stat save_cf_stat = RPC_SUCCESS;
158 1.16 fvdl struct rpc_err save_cf_error;
159 1.16 fvdl
160 1.19 lukem _DIAGASSERT(hostname != NULL);
161 1.19 lukem /* XXX: nettype appears to support being NULL */
162 1.16 fvdl
163 1.18 christos if ((handle = __rpc_setconf(nettype)) == NULL) {
164 1.16 fvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
165 1.18 christos return (NULL);
166 1.16 fvdl }
167 1.16 fvdl rpc_createerr.cf_stat = RPC_SUCCESS;
168 1.18 christos while (clnt == NULL) {
169 1.16 fvdl if ((nconf = __rpc_getconf(handle)) == NULL) {
170 1.16 fvdl if (rpc_createerr.cf_stat == RPC_SUCCESS)
171 1.16 fvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
172 1.16 fvdl break;
173 1.16 fvdl }
174 1.16 fvdl #ifdef CLNT_DEBUG
175 1.16 fvdl printf("trying netid %s\n", nconf->nc_netid);
176 1.16 fvdl #endif
177 1.16 fvdl clnt = clnt_tp_create(hostname, prog, vers, nconf);
178 1.16 fvdl if (clnt)
179 1.16 fvdl break;
180 1.16 fvdl else
181 1.16 fvdl /*
182 1.16 fvdl * Since we didn't get a name-to-address
183 1.16 fvdl * translation failure here, we remember
184 1.16 fvdl * this particular error. The object of
185 1.16 fvdl * this is to enable us to return to the
186 1.16 fvdl * caller a more-specific error than the
187 1.16 fvdl * unhelpful ``Name to address translation
188 1.16 fvdl * failed'' which might well occur if we
189 1.16 fvdl * merely returned the last error (because
190 1.16 fvdl * the local loopbacks are typically the
191 1.16 fvdl * last ones in /etc/netconfig and the most
192 1.16 fvdl * likely to be unable to translate a host
193 1.16 fvdl * name).
194 1.16 fvdl */
195 1.16 fvdl if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE) {
196 1.16 fvdl save_cf_stat = rpc_createerr.cf_stat;
197 1.16 fvdl save_cf_error = rpc_createerr.cf_error;
198 1.16 fvdl }
199 1.1 cgd }
200 1.16 fvdl
201 1.16 fvdl /*
202 1.16 fvdl * Attempt to return an error more specific than ``Name to address
203 1.16 fvdl * translation failed''
204 1.16 fvdl */
205 1.16 fvdl if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE) &&
206 1.16 fvdl (save_cf_stat != RPC_SUCCESS)) {
207 1.16 fvdl rpc_createerr.cf_stat = save_cf_stat;
208 1.16 fvdl rpc_createerr.cf_error = save_cf_error;
209 1.1 cgd }
210 1.16 fvdl __rpc_endconf(handle);
211 1.16 fvdl return (clnt);
212 1.16 fvdl }
213 1.16 fvdl
214 1.16 fvdl /*
215 1.16 fvdl * Generic client creation: takes (servers name, program-number, netconf) and
216 1.16 fvdl * returns client handle. Default options are set, which the user can
217 1.16 fvdl * change using the rpc equivalent of ioctl()'s : clnt_control()
218 1.16 fvdl * It finds out the server address from rpcbind and calls clnt_tli_create()
219 1.16 fvdl */
220 1.16 fvdl CLIENT *
221 1.28 matt clnt_tp_create(
222 1.28 matt const char * hostname, /* server name */
223 1.28 matt rpcprog_t prog, /* program number */
224 1.28 matt rpcvers_t vers, /* version number */
225 1.28 matt const struct netconfig *nconf) /* net config struct */
226 1.16 fvdl {
227 1.16 fvdl struct netbuf *svcaddr; /* servers address */
228 1.16 fvdl CLIENT *cl = NULL; /* client handle */
229 1.16 fvdl
230 1.19 lukem _DIAGASSERT(hostname != NULL);
231 1.19 lukem /* nconf is handled below */
232 1.19 lukem
233 1.18 christos if (nconf == NULL) {
234 1.1 cgd rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
235 1.18 christos return (NULL);
236 1.16 fvdl }
237 1.16 fvdl
238 1.16 fvdl /*
239 1.16 fvdl * Get the address of the server
240 1.16 fvdl */
241 1.16 fvdl if ((svcaddr = __rpcb_findaddr(prog, vers, nconf, hostname,
242 1.16 fvdl &cl)) == NULL) {
243 1.16 fvdl /* appropriate error number is set by rpcbind libraries */
244 1.18 christos return (NULL);
245 1.16 fvdl }
246 1.18 christos if (cl == NULL) {
247 1.16 fvdl cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
248 1.16 fvdl prog, vers, 0, 0);
249 1.16 fvdl } else {
250 1.16 fvdl /* Reuse the CLIENT handle and change the appropriate fields */
251 1.16 fvdl if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
252 1.27 christos if (cl->cl_netid == NULL) {
253 1.16 fvdl cl->cl_netid = strdup(nconf->nc_netid);
254 1.27 christos if (cl->cl_netid == NULL)
255 1.27 christos goto out;
256 1.27 christos }
257 1.27 christos if (cl->cl_tp == NULL) {
258 1.16 fvdl cl->cl_tp = strdup(nconf->nc_device);
259 1.27 christos if (cl->cl_tp == NULL)
260 1.27 christos goto out;
261 1.27 christos }
262 1.16 fvdl (void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
263 1.16 fvdl (void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
264 1.16 fvdl } else {
265 1.16 fvdl CLNT_DESTROY(cl);
266 1.16 fvdl cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
267 1.16 fvdl prog, vers, 0, 0);
268 1.16 fvdl }
269 1.1 cgd }
270 1.16 fvdl free(svcaddr->buf);
271 1.16 fvdl free(svcaddr);
272 1.16 fvdl return (cl);
273 1.27 christos out:
274 1.27 christos clnt_destroy(cl);
275 1.27 christos return NULL;
276 1.16 fvdl }
277 1.16 fvdl
278 1.16 fvdl /*
279 1.16 fvdl * Generic client creation: returns client handle.
280 1.16 fvdl * Default options are set, which the user can
281 1.16 fvdl * change using the rpc equivalent of ioctl()'s : clnt_control().
282 1.16 fvdl * If fd is RPC_ANYFD, it will be opened using nconf.
283 1.16 fvdl * It will be bound if not so.
284 1.16 fvdl * If sizes are 0; appropriate defaults will be chosen.
285 1.16 fvdl */
286 1.16 fvdl CLIENT *
287 1.28 matt clnt_tli_create(
288 1.28 matt int fd, /* fd */
289 1.28 matt const struct netconfig *nconf, /* netconfig structure */
290 1.28 matt const struct netbuf *svcaddr, /* servers address */
291 1.28 matt rpcprog_t prog, /* program number */
292 1.28 matt rpcvers_t vers, /* version number */
293 1.28 matt u_int sendsz, /* send size */
294 1.28 matt u_int recvsz) /* recv size */
295 1.16 fvdl {
296 1.16 fvdl CLIENT *cl; /* client handle */
297 1.16 fvdl bool_t madefd = FALSE; /* whether fd opened here */
298 1.16 fvdl long servtype;
299 1.16 fvdl struct __rpc_sockinfo si;
300 1.19 lukem
301 1.19 lukem /* nconf is handled below */
302 1.19 lukem _DIAGASSERT(svcaddr != NULL);
303 1.16 fvdl
304 1.16 fvdl if (fd == RPC_ANYFD) {
305 1.18 christos if (nconf == NULL) {
306 1.16 fvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
307 1.18 christos return (NULL);
308 1.1 cgd }
309 1.16 fvdl
310 1.16 fvdl fd = __rpc_nconf2fd(nconf);
311 1.16 fvdl
312 1.16 fvdl if (fd == -1)
313 1.16 fvdl goto err;
314 1.16 fvdl
315 1.16 fvdl madefd = TRUE;
316 1.16 fvdl servtype = nconf->nc_semantics;
317 1.16 fvdl if (!__rpc_fd2sockinfo(fd, &si))
318 1.16 fvdl goto err;
319 1.17 fvdl
320 1.17 fvdl bindresvport(fd, NULL);
321 1.16 fvdl } else {
322 1.16 fvdl if (!__rpc_fd2sockinfo(fd, &si))
323 1.16 fvdl goto err;
324 1.16 fvdl servtype = __rpc_socktype2seman(si.si_socktype);
325 1.16 fvdl if (servtype == -1) {
326 1.16 fvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
327 1.16 fvdl return NULL;
328 1.16 fvdl }
329 1.16 fvdl
330 1.16 fvdl }
331 1.16 fvdl
332 1.16 fvdl if (si.si_af != ((struct sockaddr *)svcaddr->buf)->sa_family) {
333 1.16 fvdl rpc_createerr.cf_stat = RPC_UNKNOWNHOST; /* XXX */
334 1.16 fvdl goto err1;
335 1.16 fvdl }
336 1.16 fvdl
337 1.16 fvdl switch (servtype) {
338 1.16 fvdl case NC_TPI_COTS_ORD:
339 1.16 fvdl cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
340 1.16 fvdl if (!nconf || !cl)
341 1.16 fvdl break;
342 1.26 christos __rpc_setnodelay(fd, &si);
343 1.1 cgd break;
344 1.16 fvdl case NC_TPI_CLTS:
345 1.16 fvdl cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
346 1.1 cgd break;
347 1.1 cgd default:
348 1.16 fvdl goto err;
349 1.16 fvdl }
350 1.16 fvdl
351 1.18 christos if (cl == NULL)
352 1.16 fvdl goto err1; /* borrow errors from clnt_dg/vc creates */
353 1.16 fvdl if (nconf) {
354 1.16 fvdl cl->cl_netid = strdup(nconf->nc_netid);
355 1.27 christos if (cl->cl_netid == NULL)
356 1.27 christos goto err0;
357 1.16 fvdl cl->cl_tp = strdup(nconf->nc_device);
358 1.27 christos if (cl->cl_tp == NULL)
359 1.27 christos goto err0;
360 1.16 fvdl } else {
361 1.25 yamt cl->cl_netid = __UNCONST("");
362 1.25 yamt cl->cl_tp = __UNCONST("");
363 1.1 cgd }
364 1.16 fvdl if (madefd) {
365 1.18 christos (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
366 1.27 christos /* (void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL); */
367 1.16 fvdl };
368 1.16 fvdl
369 1.16 fvdl return (cl);
370 1.16 fvdl
371 1.27 christos err0:
372 1.27 christos clnt_destroy(cl);
373 1.16 fvdl err:
374 1.16 fvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
375 1.16 fvdl rpc_createerr.cf_error.re_errno = errno;
376 1.16 fvdl err1: if (madefd)
377 1.16 fvdl (void) close(fd);
378 1.18 christos return (NULL);
379 1.1 cgd }
380