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