1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 /* 29 * Portions of this source code were derived from Berkeley 30 * 4.3 BSD under license from the Regents of the University of 31 * California. 32 */ 33 34 #pragma ident "%Z%%M% %I% %E% SMI" 35 36 #include "rpc_mt.h" 37 #include <stdio.h> 38 #include <errno.h> 39 #include <unistd.h> 40 #include <stdlib.h> 41 #include <rpc/rpc.h> 42 #include <rpc/trace.h> 43 #include <rpc/nettype.h> 44 #include <netdir.h> 45 #include <string.h> 46 #include <syslog.h> 47 48 extern int __td_setnodelay(int); 49 extern bool_t __rpc_is_local_host(const char *); 50 extern bool_t __rpc_try_doors(char *, bool_t *); 51 extern CLIENT *_clnt_vc_create_timed(register int, struct netbuf *, rpcprog_t, 52 rpcvers_t, uint_t, uint_t, const struct timeval *); 53 54 CLIENT *_clnt_tli_create_timed(int, const struct netconfig *, struct netbuf *, 55 rpcprog_t, rpcvers_t, uint_t, uint_t, const struct timeval *); 56 57 #ifndef NETIDLEN 58 #define NETIDLEN 32 59 #endif 60 61 /* 62 * Generic client creation with version checking the value of 63 * vers_out is set to the highest server supported value 64 * vers_low <= vers_out <= vers_high AND an error results 65 * if this can not be done. 66 * 67 * It calls clnt_create_vers_timed() with a NULL value for the timeout 68 * pointer, which indicates that the default timeout should be used. 69 */ 70 CLIENT * 71 clnt_create_vers(const char *hostname, rpcprog_t prog, rpcvers_t *vers_out, 72 rpcvers_t vers_low, rpcvers_t vers_high, const char *nettype) 73 { 74 return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low, 75 vers_high, nettype, NULL)); 76 } 77 78 /* 79 * This routine has the same definition as clnt_create_vers(), 80 * except it takes an additional timeout parameter - a pointer to 81 * a timeval structure. A NULL value for the pointer indicates 82 * that the default timeout value should be used. 83 */ 84 CLIENT * 85 clnt_create_vers_timed(const char *hostname, rpcprog_t prog, 86 rpcvers_t *vers_out, rpcvers_t vers_low, rpcvers_t vers_high, 87 const char *nettype, const struct timeval *tp) 88 { 89 CLIENT *clnt; 90 struct timeval to; 91 enum clnt_stat rpc_stat; 92 struct rpc_err rpcerr; 93 94 trace4(TR_clnt_create_vers_timed, 0, prog, vers_low, vers_high); 95 clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp); 96 if (clnt == NULL) { 97 trace4(TR_clnt_create_vers_timed, 1, prog, vers_low, vers_high); 98 return (NULL); 99 } 100 if (tp == NULL) { 101 to.tv_sec = 10; 102 to.tv_usec = 0; 103 } else 104 to = *tp; 105 106 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void, 107 (char *)NULL, (xdrproc_t)xdr_void, (char *)NULL, to); 108 if (rpc_stat == RPC_SUCCESS) { 109 *vers_out = vers_high; 110 trace4(TR_clnt_create_vers_timed, 1, prog, vers_low, vers_high); 111 return (clnt); 112 } 113 while (rpc_stat == RPC_PROGVERSMISMATCH && vers_high > vers_low) { 114 unsigned int minvers, maxvers; 115 116 clnt_geterr(clnt, &rpcerr); 117 minvers = rpcerr.re_vers.low; 118 maxvers = rpcerr.re_vers.high; 119 if (maxvers < vers_high) 120 vers_high = maxvers; 121 else 122 vers_high--; 123 if (minvers > vers_low) 124 vers_low = minvers; 125 if (vers_low > vers_high) { 126 goto error; 127 } 128 CLNT_CONTROL(clnt, CLSET_VERS, (char *)&vers_high); 129 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void, 130 (char *)NULL, (xdrproc_t)xdr_void, 131 (char *)NULL, to); 132 if (rpc_stat == RPC_SUCCESS) { 133 *vers_out = vers_high; 134 trace4(TR_clnt_create_vers_timed, 1, prog, 135 vers_low, vers_high); 136 return (clnt); 137 } 138 } 139 clnt_geterr(clnt, &rpcerr); 140 141 error: 142 rpc_createerr.cf_stat = rpc_stat; 143 rpc_createerr.cf_error = rpcerr; 144 clnt_destroy(clnt); 145 trace4(TR_clnt_create_vers_timed, 1, prog, vers_low, vers_high); 146 return (NULL); 147 } 148 149 /* 150 * Top level client creation routine. 151 * Generic client creation: takes (servers name, program-number, nettype) and 152 * returns client handle. Default options are set, which the user can 153 * change using the rpc equivalent of ioctl()'s. 154 * 155 * It tries for all the netids in that particular class of netid until 156 * it succeeds. 157 * XXX The error message in the case of failure will be the one 158 * pertaining to the last create error. 159 * 160 * It calls clnt_create_timed() with the default timeout. 161 */ 162 CLIENT * 163 clnt_create(const char *hostname, rpcprog_t prog, rpcvers_t vers, 164 const char *nettype) 165 { 166 return (clnt_create_timed(hostname, prog, vers, nettype, NULL)); 167 } 168 169 /* 170 * This the routine has the same definition as clnt_create(), 171 * except it takes an additional timeout parameter - a pointer to 172 * a timeval structure. A NULL value for the pointer indicates 173 * that the default timeout value should be used. 174 * 175 * This function calls clnt_tp_create_timed(). 176 */ 177 CLIENT * 178 clnt_create_timed(const char *hostname, rpcprog_t prog, rpcvers_t vers, 179 const char *netclass, const struct timeval *tp) 180 { 181 struct netconfig *nconf; 182 CLIENT *clnt = NULL; 183 void *handle; 184 enum clnt_stat save_cf_stat = RPC_SUCCESS; 185 struct rpc_err save_cf_error; 186 char nettype_array[NETIDLEN]; 187 char *nettype = &nettype_array[0]; 188 bool_t try_others; 189 190 trace3(TR_clnt_create, 0, prog, vers); 191 192 if (netclass == NULL) 193 nettype = NULL; 194 else { 195 size_t len = strlen(netclass); 196 if (len >= sizeof (nettype_array)) { 197 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 198 trace3(TR_clnt_create, 1, prog, vers); 199 return ((CLIENT *)NULL); 200 } 201 strcpy(nettype, netclass); 202 } 203 204 /* 205 * Check to see if a rendezvous over doors should be attempted. 206 */ 207 if (__rpc_try_doors(nettype, &try_others)) { 208 /* 209 * Make sure this is the local host. 210 */ 211 if (__rpc_is_local_host(hostname)) { 212 if ((clnt = clnt_door_create(prog, vers, 0)) != NULL) 213 return (clnt); 214 else { 215 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) 216 return ((CLIENT *)NULL); 217 save_cf_stat = rpc_createerr.cf_stat; 218 save_cf_error = rpc_createerr.cf_error; 219 } 220 } else { 221 save_cf_stat = rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 222 } 223 } 224 if (!try_others) 225 return (NULL); 226 227 if ((handle = __rpc_setconf((char *)nettype)) == NULL) { 228 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 229 trace3(TR_clnt_create, 1, prog, vers); 230 return ((CLIENT *)NULL); 231 } 232 rpc_createerr.cf_stat = RPC_SUCCESS; 233 while (clnt == (CLIENT *)NULL) { 234 if ((nconf = __rpc_getconf(handle)) == NULL) { 235 if (rpc_createerr.cf_stat == RPC_SUCCESS) 236 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 237 break; 238 } 239 clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp); 240 if (clnt) 241 break; 242 else { 243 /* 244 * Since we didn't get a name-to-address 245 * translation failure here, we remember 246 * this particular error. The object of 247 * this is to enable us to return to the 248 * caller a more-specific error than the 249 * unhelpful ``Name to address translation 250 * failed'' which might well occur if we 251 * merely returned the last error (because 252 * the local loopbacks are typically the 253 * last ones in /etc/netconfig and the most 254 * likely to be unable to translate a host 255 * name). We also check for a more 256 * meaningful error than ``unknown host 257 * name'' for the same reasons. 258 */ 259 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) { 260 syslog(LOG_ERR, "clnt_create_timed: " 261 "RPC_SYSTEMERROR."); 262 break; 263 } 264 265 if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE && 266 rpc_createerr.cf_stat != RPC_UNKNOWNHOST) { 267 save_cf_stat = rpc_createerr.cf_stat; 268 save_cf_error = rpc_createerr.cf_error; 269 } 270 } 271 } 272 273 /* 274 * Attempt to return an error more specific than ``Name to address 275 * translation failed'' or ``unknown host name'' 276 */ 277 if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE || 278 rpc_createerr.cf_stat == RPC_UNKNOWNHOST) && 279 (save_cf_stat != RPC_SUCCESS)) { 280 rpc_createerr.cf_stat = save_cf_stat; 281 rpc_createerr.cf_error = save_cf_error; 282 } 283 __rpc_endconf(handle); 284 trace3(TR_clnt_create, 1, prog, vers); 285 return (clnt); 286 } 287 288 /* 289 * Create a client handle for a well known service or a specific port on 290 * host. This routine bypasses rpcbind and can be use to construct a client 291 * handle to services that are not registered with rpcbind or where the remote 292 * rpcbind is not available, e.g., the remote rpcbind port is blocked by a 293 * firewall. We construct a client handle and then ping the service's NULL 294 * proc to see that the service is really available. If the caller supplies 295 * a non zero port number, the service name is ignored and the port will be 296 * used. A non-zero port number limits the protocol family to inet or inet6. 297 */ 298 299 CLIENT * 300 clnt_create_service_timed(const char *host, const char *service, 301 const rpcprog_t prog, const rpcvers_t vers, 302 const ushort_t port, const char *netclass, 303 const struct timeval *tmout) 304 { 305 int fd; 306 void *handle; 307 CLIENT *clnt = NULL; 308 struct netconfig *nconf; 309 struct nd_hostserv hs; 310 struct nd_addrlist *raddrs; 311 struct t_bind *tbind = NULL; 312 struct timeval to; 313 char nettype_array[NETIDLEN]; 314 char *nettype = &nettype_array[0]; 315 char *hostname, *serv; 316 bool_t try_others; 317 extern int __rpc_minfd; 318 319 320 /* 321 * handle const of netclass 322 */ 323 if (netclass == NULL) 324 nettype = NULL; 325 else { 326 size_t len = strlen(netclass); 327 if (len >= sizeof (nettype_array)) { 328 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 329 trace3(TR_clnt_create, 1, prog, vers); 330 return ((CLIENT *)NULL); 331 } 332 strcpy(nettype, netclass); 333 } 334 335 if (tmout == NULL) { 336 to.tv_sec = 10; 337 to.tv_usec = 0; 338 } else 339 to = *tmout; 340 341 if ((handle = __rpc_setconf(nettype)) == NULL) { 342 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 343 return ((CLIENT *)NULL); 344 } 345 346 /* 347 * Sinct host, and service are const 348 */ 349 if (host == NULL || (hostname = strdup(host)) == NULL) { 350 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 351 rpc_createerr.cf_error.re_errno = (host ? errno : EINVAL); 352 rpc_createerr.cf_error.re_terrno = 0; 353 return ((CLIENT *)NULL); 354 } 355 356 if (service == NULL) 357 serv = NULL; 358 else if ((serv = strdup(service ? service : "")) == NULL) { 359 free(hostname); 360 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 361 rpc_createerr.cf_error.re_errno = errno; 362 rpc_createerr.cf_error.re_terrno = 0; 363 return ((CLIENT *)NULL); 364 } 365 366 hs.h_host = hostname; 367 hs.h_serv = port ? NULL : serv; 368 369 /* 370 * Check to see if a rendezvous over doors should be attempted. 371 */ 372 if (__rpc_try_doors(nettype, &try_others)) { 373 /* 374 * Make sure this is the local host. 375 */ 376 if (__rpc_is_local_host(hostname)) { 377 if ((clnt = clnt_door_create(prog, vers, 0)) != NULL) 378 goto done; 379 else if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) 380 goto done; 381 } else { 382 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 383 } 384 } 385 if (!try_others) 386 goto done; 387 388 rpc_createerr.cf_stat = RPC_SUCCESS; 389 while (clnt == (CLIENT *)NULL) { 390 tbind = NULL; 391 if ((nconf = __rpc_getconf(handle)) == NULL) { 392 if (rpc_createerr.cf_stat == RPC_SUCCESS) 393 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 394 break; 395 } 396 397 if (port) { 398 if (strcmp(nconf->nc_protofmly, NC_INET) != 0 && 399 strcmp(nconf->nc_protofmly, NC_INET6) != 0) 400 continue; 401 } 402 403 if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) { 404 rpc_createerr.cf_stat = RPC_TLIERROR; 405 rpc_createerr.cf_error.re_errno = errno; 406 rpc_createerr.cf_error.re_terrno = t_errno; 407 continue; 408 } 409 410 if (fd < __rpc_minfd) 411 fd = __rpc_raise_fd(fd); 412 413 if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) 414 == NULL) { 415 t_close(fd); 416 rpc_createerr.cf_stat = RPC_TLIERROR; 417 rpc_createerr.cf_error.re_errno = errno; 418 rpc_createerr.cf_error.re_terrno = t_errno; 419 continue; 420 } 421 422 if (netdir_getbyname(nconf, &hs, &raddrs) != ND_OK) { 423 if (rpc_createerr.cf_stat == RPC_SUCCESS) 424 rpc_createerr.cf_stat = RPC_UNKNOWNHOST; 425 if (tbind) 426 t_free((char *)tbind, T_BIND); 427 t_close(fd); 428 continue; 429 } 430 memcpy(tbind->addr.buf, raddrs->n_addrs->buf, 431 raddrs->n_addrs->len); 432 tbind->addr.len = raddrs->n_addrs->len; 433 netdir_free((void *)raddrs, ND_ADDRLIST); 434 435 if (port) { 436 /* LINTED pointer alignment */ 437 if (strcmp(nconf->nc_protofmly, NC_INET) == NULL) 438 ((struct sockaddr_in *) 439 tbind->addr.buf)->sin_port = htons(port); 440 else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL) 441 ((struct sockaddr_in6 *) 442 tbind->addr.buf)->sin6_port = htons(port); 443 } 444 445 clnt = _clnt_tli_create_timed(fd, nconf, &tbind->addr, 446 prog, vers, 0, 0, &to); 447 448 if (clnt == NULL) { 449 if (tbind) 450 t_free((char *)tbind, T_BIND); 451 t_close(fd); 452 continue; 453 } 454 455 (void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, (char *)NULL); 456 457 /* 458 * Check if we can reach the server with this clnt handle 459 * Other clnt_create calls do a ping by contacting the 460 * remote rpcbind, here will just try to execute the service's 461 * NULL proc. 462 */ 463 464 rpc_createerr.cf_stat = clnt_call(clnt, NULLPROC, 465 xdr_void, 0, xdr_void, 0, to); 466 467 rpc_createerr.cf_error.re_errno = rpc_callerr.re_status; 468 rpc_createerr.cf_error.re_terrno = 0; 469 470 if (rpc_createerr.cf_stat != RPC_SUCCESS) { 471 clnt_destroy(clnt); 472 clnt = NULL; 473 if (tbind) 474 t_free((char *)tbind, T_BIND); 475 continue; 476 } else 477 break; 478 } 479 480 __rpc_endconf(handle); 481 if (tbind) 482 t_free((char *)tbind, T_BIND); 483 484 done: 485 if (hostname) 486 free(hostname); 487 if (serv) 488 free(serv); 489 490 return (clnt); 491 } 492 493 /* 494 * Generic client creation: takes (servers name, program-number, netconf) and 495 * returns client handle. Default options are set, which the user can 496 * change using the rpc equivalent of ioctl()'s : clnt_control() 497 * It finds out the server address from rpcbind and calls clnt_tli_create(). 498 * 499 * It calls clnt_tp_create_timed() with the default timeout. 500 */ 501 CLIENT * 502 clnt_tp_create(const char *hostname, rpcprog_t prog, rpcvers_t vers, 503 const struct netconfig *nconf) 504 { 505 return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL)); 506 } 507 508 /* 509 * This has the same definition as clnt_tp_create(), except it 510 * takes an additional parameter - a pointer to a timeval structure. 511 * A NULL value for the timeout pointer indicates that the default 512 * value for the timeout should be used. 513 */ 514 CLIENT * 515 clnt_tp_create_timed(const char *hostname, rpcprog_t prog, rpcvers_t vers, 516 const struct netconfig *nconf, const struct timeval *tp) 517 { 518 struct netbuf *svcaddr; /* servers address */ 519 CLIENT *cl = NULL; /* client handle */ 520 extern struct netbuf *__rpcb_findaddr_timed(rpcprog_t, rpcvers_t, 521 struct netconfig *, char *, CLIENT **, struct timeval *); 522 523 trace3(TR_clnt_tp_create, 0, prog, vers); 524 if (nconf == (struct netconfig *)NULL) { 525 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 526 trace3(TR_clnt_tp_create, 1, prog, vers); 527 return ((CLIENT *)NULL); 528 } 529 530 /* 531 * Get the address of the server 532 */ 533 if ((svcaddr = __rpcb_findaddr_timed(prog, vers, 534 (struct netconfig *)nconf, (char *)hostname, 535 &cl, (struct timeval *)tp)) == NULL) { 536 /* appropriate error number is set by rpcbind libraries */ 537 trace3(TR_clnt_tp_create, 1, prog, vers); 538 return ((CLIENT *)NULL); 539 } 540 if (cl == (CLIENT *)NULL) { 541 cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr, 542 prog, vers, 0, 0, tp); 543 } else { 544 /* Reuse the CLIENT handle and change the appropriate fields */ 545 if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) { 546 if (cl->cl_netid == NULL) { 547 cl->cl_netid = strdup(nconf->nc_netid); 548 if (cl->cl_netid == NULL) { 549 netdir_free((char *)svcaddr, ND_ADDR); 550 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 551 syslog(LOG_ERR, 552 "clnt_tp_create_timed: " 553 "strdup failed."); 554 return ((CLIENT *)NULL); 555 } 556 } 557 if (cl->cl_tp == NULL) { 558 cl->cl_tp = strdup(nconf->nc_device); 559 if (cl->cl_tp == NULL) { 560 netdir_free((char *)svcaddr, ND_ADDR); 561 if (cl->cl_netid) 562 free(cl->cl_netid); 563 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 564 syslog(LOG_ERR, 565 "clnt_tp_create_timed: " 566 "strdup failed."); 567 return ((CLIENT *)NULL); 568 } 569 } 570 (void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog); 571 (void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers); 572 } else { 573 CLNT_DESTROY(cl); 574 cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr, 575 prog, vers, 0, 0, tp); 576 } 577 } 578 netdir_free((char *)svcaddr, ND_ADDR); 579 trace3(TR_clnt_tp_create, 1, prog, vers); 580 return (cl); 581 } 582 583 /* 584 * Generic client creation: returns client handle. 585 * Default options are set, which the user can 586 * change using the rpc equivalent of ioctl()'s : clnt_control(). 587 * If fd is RPC_ANYFD, it will be opened using nconf. 588 * It will be bound if not so. 589 * If sizes are 0; appropriate defaults will be chosen. 590 */ 591 CLIENT * 592 clnt_tli_create(int fd, const struct netconfig *nconf, struct netbuf *svcaddr, 593 rpcprog_t prog, rpcvers_t vers, uint_t sendsz, uint_t recvsz) 594 { 595 return (_clnt_tli_create_timed(fd, nconf, svcaddr, prog, vers, sendsz, 596 recvsz, NULL)); 597 } 598 599 /* 600 * This has the same definition as clnt_tli_create(), except it 601 * takes an additional parameter - a pointer to a timeval structure. 602 * 603 * Not a public interface. This is for clnt_create_timed, 604 * clnt_create_vers_times, clnt_tp_create_timed to pass down the 605 * timeout value to COTS creation routine. 606 * (for bug 4049792: clnt_create_timed does not time out) 607 */ 608 CLIENT * 609 _clnt_tli_create_timed(int fd, const struct netconfig *nconf, 610 struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers, uint_t sendsz, 611 uint_t recvsz, const struct timeval *tp) 612 { 613 CLIENT *cl; /* client handle */ 614 struct t_info tinfo; /* transport info */ 615 bool_t madefd; /* whether fd opened here */ 616 t_scalar_t servtype; 617 int retval; 618 extern int __rpc_minfd; 619 620 trace5(TR_clnt_tli_create, 0, prog, vers, sendsz, recvsz); 621 if (fd == RPC_ANYFD) { 622 if (nconf == (struct netconfig *)NULL) { 623 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 624 trace3(TR_clnt_tli_create, 1, prog, vers); 625 return ((CLIENT *)NULL); 626 } 627 628 fd = t_open(nconf->nc_device, O_RDWR, NULL); 629 if (fd == -1) 630 goto err; 631 if (fd < __rpc_minfd) 632 fd = __rpc_raise_fd(fd); 633 madefd = TRUE; 634 if (t_bind(fd, (struct t_bind *)NULL, 635 (struct t_bind *)NULL) == -1) 636 goto err; 637 switch (nconf->nc_semantics) { 638 case NC_TPI_CLTS: 639 servtype = T_CLTS; 640 break; 641 case NC_TPI_COTS: 642 servtype = T_COTS; 643 break; 644 case NC_TPI_COTS_ORD: 645 servtype = T_COTS_ORD; 646 break; 647 default: 648 if (t_getinfo(fd, &tinfo) == -1) 649 goto err; 650 servtype = tinfo.servtype; 651 break; 652 } 653 } else { 654 int state; /* Current state of provider */ 655 656 /* 657 * Sync the opened fd. 658 * Check whether bound or not, else bind it 659 */ 660 if (((state = t_sync(fd)) == -1) || 661 ((state == T_UNBND) && (t_bind(fd, (struct t_bind *)NULL, 662 (struct t_bind *)NULL) == -1)) || 663 (t_getinfo(fd, &tinfo) == -1)) 664 goto err; 665 servtype = tinfo.servtype; 666 madefd = FALSE; 667 } 668 669 switch (servtype) { 670 case T_COTS: 671 cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz, 672 recvsz, tp); 673 break; 674 case T_COTS_ORD: 675 if (nconf && ((strcmp(nconf->nc_protofmly, NC_INET) == 0) || 676 (strcmp(nconf->nc_protofmly, NC_INET6) == 0))) { 677 retval = __td_setnodelay(fd); 678 if (retval == -1) 679 goto err; 680 } 681 cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz, 682 recvsz, tp); 683 break; 684 case T_CLTS: 685 cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz); 686 break; 687 default: 688 goto err; 689 } 690 691 if (cl == (CLIENT *)NULL) 692 goto err1; /* borrow errors from clnt_dg/vc creates */ 693 if (nconf) { 694 cl->cl_netid = strdup(nconf->nc_netid); 695 if (cl->cl_netid == NULL) { 696 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 697 rpc_createerr.cf_error.re_errno = errno; 698 rpc_createerr.cf_error.re_terrno = 0; 699 syslog(LOG_ERR, 700 "clnt_tli_create: strdup failed"); 701 goto err1; 702 } 703 cl->cl_tp = strdup(nconf->nc_device); 704 if (cl->cl_tp == NULL) { 705 if (cl->cl_netid) 706 free(cl->cl_netid); 707 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 708 rpc_createerr.cf_error.re_errno = errno; 709 rpc_createerr.cf_error.re_terrno = 0; 710 syslog(LOG_ERR, 711 "clnt_tli_create: strdup failed"); 712 goto err1; 713 } 714 } else { 715 struct netconfig *nc; 716 717 if ((nc = __rpcfd_to_nconf(fd, servtype)) != NULL) { 718 if (nc->nc_netid) { 719 cl->cl_netid = strdup(nc->nc_netid); 720 if (cl->cl_netid == NULL) { 721 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 722 rpc_createerr.cf_error.re_errno = errno; 723 rpc_createerr.cf_error.re_terrno = 0; 724 syslog(LOG_ERR, 725 "clnt_tli_create: " 726 "strdup failed"); 727 goto err1; 728 } 729 } 730 if (nc->nc_device) { 731 cl->cl_tp = strdup(nc->nc_device); 732 if (cl->cl_tp == NULL) { 733 if (cl->cl_netid) 734 free(cl->cl_netid); 735 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 736 rpc_createerr.cf_error.re_errno = errno; 737 rpc_createerr.cf_error.re_terrno = 0; 738 syslog(LOG_ERR, 739 "clnt_tli_create: " 740 "strdup failed"); 741 goto err1; 742 } 743 } 744 freenetconfigent(nc); 745 } 746 if (cl->cl_netid == NULL) 747 cl->cl_netid = ""; 748 if (cl->cl_tp == NULL) 749 cl->cl_tp = ""; 750 } 751 if (madefd) { 752 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, (char *)NULL); 753 /* (void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, (char *)NULL); */ 754 }; 755 756 trace3(TR_clnt_tli_create, 1, prog, vers); 757 return (cl); 758 759 err: 760 rpc_createerr.cf_stat = RPC_TLIERROR; 761 rpc_createerr.cf_error.re_errno = errno; 762 rpc_createerr.cf_error.re_terrno = t_errno; 763 err1: if (madefd) 764 (void) t_close(fd); 765 trace3(TR_clnt_tli_create, 1, prog, vers); 766 return ((CLIENT *)NULL); 767 } 768 769 /* 770 * To avoid conflicts with the "magic" file descriptors (0, 1, and 2), 771 * we try to not use them. The __rpc_raise_fd() routine will dup 772 * a descriptor to a higher value. If we fail to do it, we continue 773 * to use the old one (and hope for the best). 774 */ 775 int __rpc_minfd = 3; 776 777 int 778 __rpc_raise_fd(int fd) 779 { 780 int nfd; 781 782 if (fd >= __rpc_minfd) 783 return (fd); 784 785 if ((nfd = _fcntl(fd, F_DUPFD, __rpc_minfd)) == -1) 786 return (fd); 787 788 if (t_sync(nfd) == -1) { 789 close(nfd); 790 return (fd); 791 } 792 793 if (t_close(fd) == -1) { 794 /* this is okay, we will syslog an error, then use the new fd */ 795 (void) syslog(LOG_ERR, 796 "could not t_close() fd %d; mem & fd leak", fd); 797 } 798 799 return (nfd); 800 } 801