Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/clnt_generic.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2010, Oracle America, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the "Oracle America, Inc." nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <pthread.h>
30 #include <reentrant.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
33 #include <fcntl.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/tcp.h>
37 #include <stdio.h>
38 #include <errno.h>
39 #include <netdb.h>
40 #include <syslog.h>
41 #include <rpc/rpc.h>
42 #include <rpc/nettype.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include "rpc_com.h"
47
48 extern bool_t __rpc_is_local_host(const char *);
49 int __rpc_raise_fd(int);
50
51 #ifndef NETIDLEN
52 #define NETIDLEN 32
53 #endif
54
55 /*
56 * Generic client creation with version checking the value of
57 * vers_out is set to the highest server supported value
58 * vers_low <= vers_out <= vers_high AND an error results
59 * if this can not be done.
60 *
61 * It calls clnt_create_vers_timed() with a NULL value for the timeout
62 * pointer, which indicates that the default timeout should be used.
63 */
64 CLIENT *
65 clnt_create_vers(const char *hostname, rpcprog_t prog, rpcvers_t *vers_out,
66 rpcvers_t vers_low, rpcvers_t vers_high, const char *nettype)
67 {
68
69 return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low,
70 vers_high, nettype, NULL));
71 }
72
73 /*
74 * This the routine has the same definition as clnt_create_vers(),
75 * except it takes an additional timeout parameter - a pointer to
76 * a timeval structure. A NULL value for the pointer indicates
77 * that the default timeout value should be used.
78 */
79 CLIENT *
80 clnt_create_vers_timed(const char *hostname, rpcprog_t prog,
81 rpcvers_t *vers_out, rpcvers_t vers_low, rpcvers_t vers_high,
82 const char *nettype, const struct timeval *tp)
83 {
84 CLIENT *clnt;
85 struct timeval to;
86 enum clnt_stat rpc_stat;
87 struct rpc_err rpcerr;
88
89 clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp);
90 if (clnt == NULL) {
91 return (NULL);
92 }
93 to.tv_sec = 10;
94 to.tv_usec = 0;
95 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
96 (char *)NULL, (xdrproc_t)xdr_void, (char *)NULL, to);
97 if (rpc_stat == RPC_SUCCESS) {
98 *vers_out = vers_high;
99 return (clnt);
100 }
101 while (rpc_stat == RPC_PROGVERSMISMATCH && vers_high > vers_low) {
102 unsigned int minvers, maxvers;
103
104 clnt_geterr(clnt, &rpcerr);
105 minvers = rpcerr.re_vers.low;
106 maxvers = rpcerr.re_vers.high;
107 if (maxvers < vers_high)
108 vers_high = maxvers;
109 else
110 vers_high--;
111 if (minvers > vers_low)
112 vers_low = minvers;
113 if (vers_low > vers_high) {
114 goto error;
115 }
116 CLNT_CONTROL(clnt, CLSET_VERS, (char *)&vers_high);
117 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
118 (char *)NULL, (xdrproc_t)xdr_void,
119 (char *)NULL, to);
120 if (rpc_stat == RPC_SUCCESS) {
121 *vers_out = vers_high;
122 return (clnt);
123 }
124 }
125 clnt_geterr(clnt, &rpcerr);
126
127 error:
128 rpc_createerr.cf_stat = rpc_stat;
129 rpc_createerr.cf_error = rpcerr;
130 clnt_destroy(clnt);
131 return (NULL);
132 }
133
134 /*
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.
139 *
140 * It tries for all the netids in that particular class of netid until
141 * it succeeds.
142 * XXX The error message in the case of failure will be the one
143 * pertaining to the last create error.
144 *
145 * It calls clnt_create_timed() with the default timeout.
146 */
147 CLIENT *
148 clnt_create(const char *hostname, rpcprog_t prog, rpcvers_t vers,
149 const char *nettype)
150 {
151
152 return (clnt_create_timed(hostname, prog, vers, nettype, NULL));
153 }
154
155 /*
156 * This the routine has the same definition as clnt_create(),
157 * except it takes an additional timeout parameter - a pointer to
158 * a timeval structure. A NULL value for the pointer indicates
159 * that the default timeout value should be used.
160 *
161 * This function calls clnt_tp_create_timed().
162 */
163 CLIENT *
164 clnt_create_timed(const char *hostname, rpcprog_t prog, rpcvers_t vers,
165 const char *netclass, const struct timeval *tp)
166 {
167 struct netconfig *nconf;
168 CLIENT *clnt = NULL;
169 void *handle;
170 enum clnt_stat save_cf_stat = RPC_SUCCESS;
171 struct rpc_err save_cf_error;
172 char nettype_array[NETIDLEN];
173 char *nettype = &nettype_array[0];
174
175 if (netclass == NULL)
176 nettype = NULL;
177 else {
178 size_t len = strlen(netclass);
179 if (len >= sizeof (nettype_array)) {
180 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
181 return (NULL);
182 }
183 strcpy(nettype, netclass);
184 }
185
186 if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
187 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
188 return (NULL);
189 }
190 rpc_createerr.cf_stat = RPC_SUCCESS;
191 while (clnt == NULL) {
192 if ((nconf = __rpc_getconf(handle)) == NULL) {
193 if (rpc_createerr.cf_stat == RPC_SUCCESS)
194 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
195 break;
196 }
197 #ifdef CLNT_DEBUG
198 printf("trying netid %s\n", nconf->nc_netid);
199 #endif
200 clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp);
201 if (clnt)
202 break;
203 else {
204 /*
205 * Since we didn't get a name-to-address
206 * translation failure here, we remember
207 * this particular error. The object of
208 * this is to enable us to return to the
209 * caller a more-specific error than the
210 * unhelpful ``Name to address translation
211 * failed'' which might well occur if we
212 * merely returned the last error (because
213 * the local loopbacks are typically the
214 * last ones in /etc/netconfig and the most
215 * likely to be unable to translate a host
216 * name). We also check for a more
217 * meaningful error than ``unknown host
218 * name'' for the same reasons.
219 */
220 if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE &&
221 rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
222 save_cf_stat = rpc_createerr.cf_stat;
223 save_cf_error = rpc_createerr.cf_error;
224 }
225 }
226 }
227
228 /*
229 * Attempt to return an error more specific than ``Name to address
230 * translation failed'' or ``unknown host name''
231 */
232 if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE ||
233 rpc_createerr.cf_stat == RPC_UNKNOWNHOST) &&
234 (save_cf_stat != RPC_SUCCESS)) {
235 rpc_createerr.cf_stat = save_cf_stat;
236 rpc_createerr.cf_error = save_cf_error;
237 }
238 __rpc_endconf(handle);
239 return (clnt);
240 }
241
242 /*
243 * Generic client creation: takes (servers name, program-number, netconf) and
244 * returns client handle. Default options are set, which the user can
245 * change using the rpc equivalent of _ioctl()'s : clnt_control()
246 * It finds out the server address from rpcbind and calls clnt_tli_create().
247 *
248 * It calls clnt_tp_create_timed() with the default timeout.
249 */
250 CLIENT *
251 clnt_tp_create(const char *hostname, rpcprog_t prog, rpcvers_t vers,
252 const struct netconfig *nconf)
253 {
254 return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL));
255 }
256
257 /*
258 * This has the same definition as clnt_tp_create(), except it
259 * takes an additional parameter - a pointer to a timeval structure.
260 * A NULL value for the timeout pointer indicates that the default
261 * value for the timeout should be used.
262 */
263 CLIENT *
264 clnt_tp_create_timed(const char *hostname, rpcprog_t prog, rpcvers_t vers,
265 const struct netconfig *nconf, const struct timeval *tp)
266 {
267 struct netbuf *svcaddr; /* servers address */
268 CLIENT *cl = NULL; /* client handle */
269
270 if (nconf == NULL) {
271 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
272 return (NULL);
273 }
274
275 /*
276 * Get the address of the server
277 */
278 if ((svcaddr = __rpcb_findaddr_timed(prog, vers,
279 (struct netconfig *)nconf, (char *)hostname,
280 &cl, (struct timeval *)tp)) == NULL) {
281 /* appropriate error number is set by rpcbind libraries */
282 return (NULL);
283 }
284 if (cl == NULL) {
285 cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
286 prog, vers, 0, 0);
287 } else {
288 /* Reuse the CLIENT handle and change the appropriate fields */
289 if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
290 if (cl->cl_netid == NULL)
291 cl->cl_netid = strdup(nconf->nc_netid);
292 if (cl->cl_tp == NULL)
293 cl->cl_tp = strdup(nconf->nc_device);
294 (void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
295 (void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
296 } else {
297 CLNT_DESTROY(cl);
298 cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
299 prog, vers, 0, 0);
300 }
301 }
302 free(svcaddr->buf);
303 free(svcaddr);
304 return (cl);
305 }
306
307 /*
308 * Generic client creation: returns client handle.
309 * Default options are set, which the user can
310 * change using the rpc equivalent of _ioctl()'s : clnt_control().
311 * If fd is RPC_ANYFD, it will be opened using nconf.
312 * It will be bound if not so.
313 * If sizes are 0; appropriate defaults will be chosen.
314 */
315 CLIENT *
316 clnt_tli_create(int fd, const struct netconfig *nconf,
317 struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers,
318 uint sendsz, uint recvsz)
319 {
320 CLIENT *cl; /* client handle */
321 bool_t madefd = FALSE; /* whether fd opened here */
322 long servtype;
323 int one = 1;
324 struct __rpc_sockinfo si;
325 extern int __rpc_minfd;
326
327 if (fd == RPC_ANYFD) {
328 if (nconf == NULL) {
329 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
330 return (NULL);
331 }
332
333 fd = __rpc_nconf2fd(nconf);
334
335 if (fd == -1)
336 goto err;
337 if (fd < __rpc_minfd)
338 fd = __rpc_raise_fd(fd);
339 madefd = TRUE;
340 servtype = nconf->nc_semantics;
341 if (!__rpc_fd2sockinfo(fd, &si))
342 goto err;
343 bindresvport(fd, NULL);
344 } else {
345 if (!__rpc_fd2sockinfo(fd, &si))
346 goto err;
347 servtype = __rpc_socktype2seman(si.si_socktype);
348 if (servtype == -1) {
349 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
350 return (NULL);
351 }
352 }
353
354 if (si.si_af != ((struct sockaddr *)svcaddr->buf)->sa_family) {
355 rpc_createerr.cf_stat = RPC_UNKNOWNHOST; /* XXX */
356 goto err1;
357 }
358
359 switch (servtype) {
360 case NC_TPI_COTS:
361 cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
362 break;
363 case NC_TPI_COTS_ORD:
364 if (nconf &&
365 ((strcmp(nconf->nc_protofmly, "inet") == 0) ||
366 (strcmp(nconf->nc_protofmly, "inet6") == 0))) {
367 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one,
368 sizeof (one));
369 }
370 cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
371 break;
372 case NC_TPI_CLTS:
373 cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
374 break;
375 default:
376 goto err;
377 }
378
379 if (cl == NULL)
380 goto err1; /* borrow errors from clnt_dg/vc creates */
381 if (nconf) {
382 cl->cl_netid = strdup(nconf->nc_netid);
383 cl->cl_tp = strdup(nconf->nc_device);
384 } else {
385 cl->cl_netid = "";
386 cl->cl_tp = "";
387 }
388 if (madefd) {
389 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
390 /* (void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL); */
391 };
392
393 return (cl);
394
395 err:
396 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
397 rpc_createerr.cf_error.re_errno = errno;
398 err1: if (madefd)
399 (void)close(fd);
400 return (NULL);
401 }
402
403 /*
404 * To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
405 * we try to not use them. The __rpc_raise_fd() routine will dup
406 * a descriptor to a higher value. If we fail to do it, we continue
407 * to use the old one (and hope for the best).
408 */
409 int __rpc_minfd = 3;
410
411 int
412 __rpc_raise_fd(int fd)
413 {
414 int nfd;
415
416 if (fd >= __rpc_minfd)
417 return (fd);
418
419 if ((nfd = fcntl(fd, F_DUPFD, __rpc_minfd)) == -1)
420 return (fd);
421
422 if (fsync(nfd) == -1) {
423 close(nfd);
424 return (fd);
425 }
426
427 if (close(fd) == -1) {
428 /* this is okay, we will syslog an error, then use the new fd */
429 (void) syslog(LOG_ERR,
430 "could not close() fd %d; mem & fd leak", fd);
431 }
432
433 return (nfd);
434 }