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