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