2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1989, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A/*
2N/A * Portions of this source code were derived from Berkeley
2N/A * 4.3 BSD under license from the Regents of the University of
2N/A * California.
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include "rpc_mt.h"
2N/A#include <stdio.h>
2N/A#include <errno.h>
2N/A#include <unistd.h>
2N/A#include <stdlib.h>
2N/A#include <rpc/rpc.h>
2N/A#include <rpc/nettype.h>
2N/A#include <netdir.h>
2N/A#include <string.h>
2N/A#include <syslog.h>
2N/A
2N/Aextern int __td_setnodelay(int);
2N/Aextern bool_t __rpc_is_local_host(const char *);
2N/Aextern bool_t __rpc_try_doors(const char *, bool_t *);
2N/Aextern CLIENT *_clnt_vc_create_timed(int, struct netbuf *, rpcprog_t,
2N/A rpcvers_t, uint_t, uint_t, const struct timeval *);
2N/A
2N/ACLIENT *_clnt_tli_create_timed(int, const struct netconfig *, struct netbuf *,
2N/A rpcprog_t, rpcvers_t, uint_t, uint_t, const struct timeval *);
2N/A
2N/A#ifndef NETIDLEN
2N/A#define NETIDLEN 32
2N/A#endif
2N/A
2N/A/*
2N/A * Generic client creation with version checking the value of
2N/A * vers_out is set to the highest server supported value
2N/A * vers_low <= vers_out <= vers_high AND an error results
2N/A * if this can not be done.
2N/A *
2N/A * It calls clnt_create_vers_timed() with a NULL value for the timeout
2N/A * pointer, which indicates that the default timeout should be used.
2N/A */
2N/ACLIENT *
2N/Aclnt_create_vers(const char *hostname, const rpcprog_t prog,
2N/A rpcvers_t *vers_out, const rpcvers_t vers_low,
2N/A const rpcvers_t vers_high, const char *nettype)
2N/A{
2N/A return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low,
2N/A vers_high, nettype, NULL));
2N/A}
2N/A
2N/A/*
2N/A * This routine has the same definition as clnt_create_vers(),
2N/A * except it takes an additional timeout parameter - a pointer to
2N/A * a timeval structure. A NULL value for the pointer indicates
2N/A * that the default timeout value should be used.
2N/A */
2N/ACLIENT *
2N/Aclnt_create_vers_timed(const char *hostname, const rpcprog_t prog,
2N/A rpcvers_t *vers_out, const rpcvers_t vers_low, const rpcvers_t vers_high,
2N/A const char *nettype, const struct timeval *tp)
2N/A{
2N/A CLIENT *clnt;
2N/A struct timeval to;
2N/A enum clnt_stat rpc_stat;
2N/A struct rpc_err rpcerr;
2N/A rpcvers_t v_low, v_high;
2N/A
2N/A clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp);
2N/A if (clnt == NULL)
2N/A return (NULL);
2N/A if (tp == NULL) {
2N/A to.tv_sec = 10;
2N/A to.tv_usec = 0;
2N/A } else
2N/A to = *tp;
2N/A
2N/A rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
2N/A NULL, (xdrproc_t)xdr_void, NULL, to);
2N/A if (rpc_stat == RPC_SUCCESS) {
2N/A *vers_out = vers_high;
2N/A return (clnt);
2N/A }
2N/A v_low = vers_low;
2N/A v_high = vers_high;
2N/A while (rpc_stat == RPC_PROGVERSMISMATCH && v_high > v_low) {
2N/A unsigned int minvers, maxvers;
2N/A
2N/A clnt_geterr(clnt, &rpcerr);
2N/A minvers = rpcerr.re_vers.low;
2N/A maxvers = rpcerr.re_vers.high;
2N/A if (maxvers < v_high)
2N/A v_high = maxvers;
2N/A else
2N/A v_high--;
2N/A if (minvers > v_low)
2N/A v_low = minvers;
2N/A if (v_low > v_high) {
2N/A goto error;
2N/A }
2N/A CLNT_CONTROL(clnt, CLSET_VERS, (char *)&v_high);
2N/A rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
2N/A NULL, (xdrproc_t)xdr_void,
2N/A NULL, to);
2N/A if (rpc_stat == RPC_SUCCESS) {
2N/A *vers_out = v_high;
2N/A return (clnt);
2N/A }
2N/A }
2N/A clnt_geterr(clnt, &rpcerr);
2N/A
2N/Aerror:
2N/A rpc_createerr.cf_stat = rpc_stat;
2N/A rpc_createerr.cf_error = rpcerr;
2N/A clnt_destroy(clnt);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Top level client creation routine.
2N/A * Generic client creation: takes (servers name, program-number, nettype) and
2N/A * returns client handle. Default options are set, which the user can
2N/A * change using the rpc equivalent of ioctl()'s.
2N/A *
2N/A * It tries for all the netids in that particular class of netid until
2N/A * it succeeds.
2N/A * XXX The error message in the case of failure will be the one
2N/A * pertaining to the last create error.
2N/A *
2N/A * It calls clnt_create_timed() with the default timeout.
2N/A */
2N/ACLIENT *
2N/Aclnt_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
2N/A const char *nettype)
2N/A{
2N/A return (clnt_create_timed(hostname, prog, vers, nettype, NULL));
2N/A}
2N/A
2N/A/*
2N/A * This the routine has the same definition as clnt_create(),
2N/A * except it takes an additional timeout parameter - a pointer to
2N/A * a timeval structure. A NULL value for the pointer indicates
2N/A * that the default timeout value should be used.
2N/A *
2N/A * This function calls clnt_tp_create_timed().
2N/A */
2N/ACLIENT *
2N/Aclnt_create_timed(const char *hostname, const rpcprog_t prog,
2N/A const rpcvers_t vers, const char *netclass, const struct timeval *tp)
2N/A{
2N/A struct netconfig *nconf;
2N/A CLIENT *clnt = NULL;
2N/A void *handle;
2N/A enum clnt_stat save_cf_stat = RPC_SUCCESS;
2N/A struct rpc_err save_cf_error;
2N/A char nettype_array[NETIDLEN];
2N/A char *nettype = &nettype_array[0];
2N/A bool_t try_others;
2N/A
2N/A if (netclass == NULL)
2N/A nettype = NULL;
2N/A else {
2N/A size_t len = strlen(netclass);
2N/A if (len >= sizeof (nettype_array)) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A (void) strcpy(nettype, netclass);
2N/A }
2N/A
2N/A /*
2N/A * Check to see if a rendezvous over doors should be attempted.
2N/A */
2N/A if (__rpc_try_doors(nettype, &try_others)) {
2N/A /*
2N/A * Make sure this is the local host.
2N/A */
2N/A if (__rpc_is_local_host(hostname)) {
2N/A if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
2N/A return (clnt);
2N/A else {
2N/A if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
2N/A return (NULL);
2N/A save_cf_stat = rpc_createerr.cf_stat;
2N/A save_cf_error = rpc_createerr.cf_error;
2N/A }
2N/A } else {
2N/A save_cf_stat = rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A }
2N/A }
2N/A if (!try_others)
2N/A return (NULL);
2N/A
2N/A if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A rpc_createerr.cf_stat = RPC_SUCCESS;
2N/A while (clnt == NULL) {
2N/A if ((nconf = __rpc_getconf(handle)) == NULL) {
2N/A if (rpc_createerr.cf_stat == RPC_SUCCESS)
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A break;
2N/A }
2N/A clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp);
2N/A if (clnt)
2N/A break;
2N/A else {
2N/A /*
2N/A * Since we didn't get a name-to-address
2N/A * translation failure here, we remember
2N/A * this particular error. The object of
2N/A * this is to enable us to return to the
2N/A * caller a more-specific error than the
2N/A * unhelpful ``Name to address translation
2N/A * failed'' which might well occur if we
2N/A * merely returned the last error (because
2N/A * the local loopbacks are typically the
2N/A * last ones in /etc/netconfig and the most
2N/A * likely to be unable to translate a host
2N/A * name). We also check for a more
2N/A * meaningful error than ``unknown host
2N/A * name'' for the same reasons.
2N/A */
2N/A if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
2N/A syslog(LOG_ERR, "clnt_create_timed: "
2N/A "RPC_SYSTEMERROR.");
2N/A break;
2N/A }
2N/A
2N/A if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE &&
2N/A rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
2N/A save_cf_stat = rpc_createerr.cf_stat;
2N/A save_cf_error = rpc_createerr.cf_error;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Attempt to return an error more specific than ``Name to address
2N/A * translation failed'' or ``unknown host name''
2N/A */
2N/A if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE ||
2N/A rpc_createerr.cf_stat == RPC_UNKNOWNHOST) &&
2N/A (save_cf_stat != RPC_SUCCESS)) {
2N/A rpc_createerr.cf_stat = save_cf_stat;
2N/A rpc_createerr.cf_error = save_cf_error;
2N/A }
2N/A __rpc_endconf(handle);
2N/A return (clnt);
2N/A}
2N/A
2N/A/*
2N/A * Create a client handle for a well known service or a specific port on
2N/A * host. This routine bypasses rpcbind and can be use to construct a client
2N/A * handle to services that are not registered with rpcbind or where the remote
2N/A * rpcbind is not available, e.g., the remote rpcbind port is blocked by a
2N/A * firewall. We construct a client handle and then ping the service's NULL
2N/A * proc to see that the service is really available. If the caller supplies
2N/A * a non zero port number, the service name is ignored and the port will be
2N/A * used. A non-zero port number limits the protocol family to inet or inet6.
2N/A */
2N/A
2N/ACLIENT *
2N/Aclnt_create_service_timed(const char *host, const char *service,
2N/A const rpcprog_t prog, const rpcvers_t vers,
2N/A const ushort_t port, const char *netclass,
2N/A const struct timeval *tmout)
2N/A{
2N/A int fd;
2N/A void *handle;
2N/A CLIENT *clnt = NULL;
2N/A struct netconfig *nconf;
2N/A struct nd_hostserv hs;
2N/A struct nd_addrlist *raddrs;
2N/A struct t_bind *tbind = NULL;
2N/A struct timeval to;
2N/A char nettype_array[NETIDLEN];
2N/A char *nettype = &nettype_array[0];
2N/A char *hostname, *serv;
2N/A bool_t try_others;
2N/A
2N/A /*
2N/A * handle const of netclass
2N/A */
2N/A if (netclass == NULL)
2N/A nettype = NULL;
2N/A else {
2N/A size_t len = strlen(netclass);
2N/A if (len >= sizeof (nettype_array)) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A (void) strcpy(nettype, netclass);
2N/A }
2N/A
2N/A if (tmout == NULL) {
2N/A to.tv_sec = 10;
2N/A to.tv_usec = 0;
2N/A } else
2N/A to = *tmout;
2N/A
2N/A if ((handle = __rpc_setconf(nettype)) == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * Sinct host, and service are const
2N/A */
2N/A if (host == NULL || (hostname = strdup(host)) == NULL) {
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A rpc_createerr.cf_error.re_errno = (host ? errno : EINVAL);
2N/A rpc_createerr.cf_error.re_terrno = 0;
2N/A return (NULL);
2N/A }
2N/A
2N/A if (service == NULL)
2N/A serv = NULL;
2N/A else if ((serv = strdup(service ? service : "")) == NULL) {
2N/A free(hostname);
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A rpc_createerr.cf_error.re_errno = errno;
2N/A rpc_createerr.cf_error.re_terrno = 0;
2N/A return (NULL);
2N/A }
2N/A
2N/A hs.h_host = hostname;
2N/A hs.h_serv = port ? NULL : serv;
2N/A
2N/A /*
2N/A * Check to see if a rendezvous over doors should be attempted.
2N/A */
2N/A if (__rpc_try_doors(nettype, &try_others)) {
2N/A /*
2N/A * Make sure this is the local host.
2N/A */
2N/A if (__rpc_is_local_host(hostname)) {
2N/A if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
2N/A goto done;
2N/A else if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
2N/A goto done;
2N/A } else {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A }
2N/A }
2N/A if (!try_others)
2N/A goto done;
2N/A
2N/A rpc_createerr.cf_stat = RPC_SUCCESS;
2N/A while (clnt == NULL) {
2N/A tbind = NULL;
2N/A if ((nconf = __rpc_getconf(handle)) == NULL) {
2N/A if (rpc_createerr.cf_stat == RPC_SUCCESS)
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A break;
2N/A }
2N/A
2N/A if (port) {
2N/A if (strcmp(nconf->nc_protofmly, NC_INET) != 0 &&
2N/A strcmp(nconf->nc_protofmly, NC_INET6) != 0)
2N/A continue;
2N/A }
2N/A
2N/A if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
2N/A rpc_createerr.cf_stat = RPC_TLIERROR;
2N/A rpc_createerr.cf_error.re_errno = errno;
2N/A rpc_createerr.cf_error.re_terrno = t_errno;
2N/A continue;
2N/A }
2N/A
2N/A RPC_RAISEFD(fd);
2N/A
2N/A __rpc_set_mac_options(fd, nconf, prog);
2N/A
2N/A if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
2N/A == NULL) {
2N/A (void) t_close(fd);
2N/A rpc_createerr.cf_stat = RPC_TLIERROR;
2N/A rpc_createerr.cf_error.re_errno = errno;
2N/A rpc_createerr.cf_error.re_terrno = t_errno;
2N/A continue;
2N/A }
2N/A
2N/A if (netdir_getbyname(nconf, &hs, &raddrs) != ND_OK) {
2N/A if (rpc_createerr.cf_stat == RPC_SUCCESS)
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
2N/A if (tbind)
2N/A (void) t_free((char *)tbind, T_BIND);
2N/A (void) t_close(fd);
2N/A continue;
2N/A }
2N/A (void) memcpy(tbind->addr.buf, raddrs->n_addrs->buf,
2N/A raddrs->n_addrs->len);
2N/A tbind->addr.len = raddrs->n_addrs->len;
2N/A netdir_free((void *)raddrs, ND_ADDRLIST);
2N/A
2N/A if (port) {
2N/A if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
2N/A ((struct sockaddr_in *)
2N/A tbind->addr.buf)->sin_port = htons(port);
2N/A else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
2N/A ((struct sockaddr_in6 *)
2N/A tbind->addr.buf)->sin6_port = htons(port);
2N/A }
2N/A
2N/A clnt = _clnt_tli_create_timed(fd, nconf, &tbind->addr,
2N/A prog, vers, 0, 0, &to);
2N/A
2N/A if (clnt == NULL) {
2N/A if (tbind)
2N/A (void) t_free((char *)tbind, T_BIND);
2N/A (void) t_close(fd);
2N/A continue;
2N/A }
2N/A
2N/A (void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, NULL);
2N/A
2N/A /*
2N/A * Check if we can reach the server with this clnt handle
2N/A * Other clnt_create calls do a ping by contacting the
2N/A * remote rpcbind, here will just try to execute the service's
2N/A * NULL proc.
2N/A */
2N/A
2N/A rpc_createerr.cf_stat = clnt_call(clnt, NULLPROC,
2N/A xdr_void, 0, xdr_void, 0, to);
2N/A
2N/A rpc_createerr.cf_error.re_errno = rpc_callerr.re_status;
2N/A rpc_createerr.cf_error.re_terrno = 0;
2N/A
2N/A if (rpc_createerr.cf_stat != RPC_SUCCESS) {
2N/A clnt_destroy(clnt);
2N/A clnt = NULL;
2N/A if (tbind)
2N/A (void) t_free((char *)tbind, T_BIND);
2N/A continue;
2N/A } else
2N/A break;
2N/A }
2N/A
2N/A __rpc_endconf(handle);
2N/A if (tbind)
2N/A (void) t_free((char *)tbind, T_BIND);
2N/A
2N/Adone:
2N/A if (hostname)
2N/A free(hostname);
2N/A if (serv)
2N/A free(serv);
2N/A
2N/A return (clnt);
2N/A}
2N/A
2N/A/*
2N/A * Generic client creation: takes (servers name, program-number, netconf) and
2N/A * returns client handle. Default options are set, which the user can
2N/A * change using the rpc equivalent of ioctl()'s : clnt_control()
2N/A * It finds out the server address from rpcbind and calls clnt_tli_create().
2N/A *
2N/A * It calls clnt_tp_create_timed() with the default timeout.
2N/A */
2N/ACLIENT *
2N/Aclnt_tp_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
2N/A const struct netconfig *nconf)
2N/A{
2N/A return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL));
2N/A}
2N/A
2N/A/*
2N/A * This has the same definition as clnt_tp_create(), except it
2N/A * takes an additional parameter - a pointer to a timeval structure.
2N/A * A NULL value for the timeout pointer indicates that the default
2N/A * value for the timeout should be used.
2N/A */
2N/ACLIENT *
2N/Aclnt_tp_create_timed(const char *hostname, const rpcprog_t prog,
2N/A const rpcvers_t vers, const struct netconfig *nconf,
2N/A const struct timeval *tp)
2N/A{
2N/A struct netbuf *svcaddr; /* servers address */
2N/A CLIENT *cl = NULL; /* client handle */
2N/A extern struct netbuf *__rpcb_findaddr_timed(rpcprog_t, rpcvers_t,
2N/A struct netconfig *, char *, CLIENT **, struct timeval *);
2N/A
2N/A if (nconf == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * Get the address of the server
2N/A */
2N/A if ((svcaddr = __rpcb_findaddr_timed(prog, vers,
2N/A (struct netconfig *)nconf, (char *)hostname,
2N/A &cl, (struct timeval *)tp)) == NULL) {
2N/A /* appropriate error number is set by rpcbind libraries */
2N/A return (NULL);
2N/A }
2N/A if (cl == NULL) {
2N/A cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
2N/A prog, vers, 0, 0, tp);
2N/A } else {
2N/A /* Reuse the CLIENT handle and change the appropriate fields */
2N/A if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
2N/A if (cl->cl_netid == NULL) {
2N/A cl->cl_netid = strdup(nconf->nc_netid);
2N/A if (cl->cl_netid == NULL) {
2N/A netdir_free((char *)svcaddr, ND_ADDR);
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A syslog(LOG_ERR,
2N/A "clnt_tp_create_timed: "
2N/A "strdup failed.");
2N/A return (NULL);
2N/A }
2N/A }
2N/A if (cl->cl_tp == NULL) {
2N/A cl->cl_tp = strdup(nconf->nc_device);
2N/A if (cl->cl_tp == NULL) {
2N/A netdir_free((char *)svcaddr, ND_ADDR);
2N/A if (cl->cl_netid)
2N/A free(cl->cl_netid);
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A syslog(LOG_ERR,
2N/A "clnt_tp_create_timed: "
2N/A "strdup failed.");
2N/A return (NULL);
2N/A }
2N/A }
2N/A (void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
2N/A (void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
2N/A } else {
2N/A CLNT_DESTROY(cl);
2N/A cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
2N/A prog, vers, 0, 0, tp);
2N/A }
2N/A }
2N/A netdir_free((char *)svcaddr, ND_ADDR);
2N/A return (cl);
2N/A}
2N/A
2N/A/*
2N/A * Generic client creation: returns client handle.
2N/A * Default options are set, which the user can
2N/A * change using the rpc equivalent of ioctl()'s : clnt_control().
2N/A * If fd is RPC_ANYFD, it will be opened using nconf.
2N/A * It will be bound if not so.
2N/A * If sizes are 0; appropriate defaults will be chosen.
2N/A */
2N/ACLIENT *
2N/Aclnt_tli_create(const int fd, const struct netconfig *nconf,
2N/A struct netbuf *svcaddr, const rpcprog_t prog, const rpcvers_t vers,
2N/A const uint_t sendsz, const uint_t recvsz)
2N/A{
2N/A return (_clnt_tli_create_timed(fd, nconf, svcaddr, prog, vers, sendsz,
2N/A recvsz, NULL));
2N/A}
2N/A
2N/A/*
2N/A * This has the same definition as clnt_tli_create(), except it
2N/A * takes an additional parameter - a pointer to a timeval structure.
2N/A *
2N/A * Not a public interface. This is for clnt_create_timed,
2N/A * clnt_create_vers_times, clnt_tp_create_timed to pass down the
2N/A * timeout value to COTS creation routine.
2N/A * (for bug 4049792: clnt_create_timed does not time out)
2N/A */
2N/ACLIENT *
2N/A_clnt_tli_create_timed(int fd, const struct netconfig *nconf,
2N/A struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers, uint_t sendsz,
2N/A uint_t recvsz, const struct timeval *tp)
2N/A{
2N/A CLIENT *cl; /* client handle */
2N/A struct t_info tinfo; /* transport info */
2N/A bool_t madefd; /* whether fd opened here */
2N/A t_scalar_t servtype;
2N/A int retval;
2N/A
2N/A if (fd == RPC_ANYFD) {
2N/A if (nconf == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A
2N/A fd = t_open(nconf->nc_device, O_RDWR, NULL);
2N/A if (fd == -1)
2N/A goto err;
2N/A RPC_RAISEFD(fd);
2N/A madefd = TRUE;
2N/A __rpc_set_mac_options(fd, nconf, prog);
2N/A if (t_bind(fd, NULL, NULL) == -1)
2N/A goto err;
2N/A switch (nconf->nc_semantics) {
2N/A case NC_TPI_CLTS:
2N/A servtype = T_CLTS;
2N/A break;
2N/A case NC_TPI_COTS:
2N/A servtype = T_COTS;
2N/A break;
2N/A case NC_TPI_COTS_ORD:
2N/A servtype = T_COTS_ORD;
2N/A break;
2N/A default:
2N/A if (t_getinfo(fd, &tinfo) == -1)
2N/A goto err;
2N/A servtype = tinfo.servtype;
2N/A break;
2N/A }
2N/A } else {
2N/A int state; /* Current state of provider */
2N/A
2N/A /*
2N/A * Sync the opened fd.
2N/A * Check whether bound or not, else bind it
2N/A */
2N/A if (((state = t_sync(fd)) == -1) ||
2N/A ((state == T_UNBND) && (t_bind(fd, NULL, NULL) == -1)) ||
2N/A (t_getinfo(fd, &tinfo) == -1))
2N/A goto err;
2N/A servtype = tinfo.servtype;
2N/A madefd = FALSE;
2N/A }
2N/A
2N/A switch (servtype) {
2N/A case T_COTS:
2N/A cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
2N/A recvsz, tp);
2N/A break;
2N/A case T_COTS_ORD:
2N/A if (nconf && ((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
2N/A (strcmp(nconf->nc_protofmly, NC_INET6) == 0))) {
2N/A retval = __td_setnodelay(fd);
2N/A if (retval == -1)
2N/A goto err;
2N/A }
2N/A cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
2N/A recvsz, tp);
2N/A break;
2N/A case T_CLTS:
2N/A cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
2N/A break;
2N/A default:
2N/A goto err;
2N/A }
2N/A
2N/A if (cl == NULL)
2N/A goto err1; /* borrow errors from clnt_dg/vc creates */
2N/A if (nconf) {
2N/A cl->cl_netid = strdup(nconf->nc_netid);
2N/A if (cl->cl_netid == NULL) {
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A rpc_createerr.cf_error.re_errno = errno;
2N/A rpc_createerr.cf_error.re_terrno = 0;
2N/A syslog(LOG_ERR,
2N/A "clnt_tli_create: strdup failed");
2N/A goto err1;
2N/A }
2N/A cl->cl_tp = strdup(nconf->nc_device);
2N/A if (cl->cl_tp == NULL) {
2N/A if (cl->cl_netid)
2N/A free(cl->cl_netid);
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A rpc_createerr.cf_error.re_errno = errno;
2N/A rpc_createerr.cf_error.re_terrno = 0;
2N/A syslog(LOG_ERR,
2N/A "clnt_tli_create: strdup failed");
2N/A goto err1;
2N/A }
2N/A } else {
2N/A struct netconfig *nc;
2N/A
2N/A if ((nc = __rpcfd_to_nconf(fd, servtype)) != NULL) {
2N/A if (nc->nc_netid) {
2N/A cl->cl_netid = strdup(nc->nc_netid);
2N/A if (cl->cl_netid == NULL) {
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A rpc_createerr.cf_error.re_errno = errno;
2N/A rpc_createerr.cf_error.re_terrno = 0;
2N/A syslog(LOG_ERR,
2N/A "clnt_tli_create: "
2N/A "strdup failed");
2N/A goto err1;
2N/A }
2N/A }
2N/A if (nc->nc_device) {
2N/A cl->cl_tp = strdup(nc->nc_device);
2N/A if (cl->cl_tp == NULL) {
2N/A if (cl->cl_netid)
2N/A free(cl->cl_netid);
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A rpc_createerr.cf_error.re_errno = errno;
2N/A rpc_createerr.cf_error.re_terrno = 0;
2N/A syslog(LOG_ERR,
2N/A "clnt_tli_create: "
2N/A "strdup failed");
2N/A goto err1;
2N/A }
2N/A }
2N/A freenetconfigent(nc);
2N/A }
2N/A if (cl->cl_netid == NULL)
2N/A cl->cl_netid = "";
2N/A if (cl->cl_tp == NULL)
2N/A cl->cl_tp = "";
2N/A }
2N/A if (madefd) {
2N/A (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
2N/A/* (void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL); */
2N/A };
2N/A
2N/A return (cl);
2N/A
2N/Aerr:
2N/A rpc_createerr.cf_stat = RPC_TLIERROR;
2N/A rpc_createerr.cf_error.re_errno = errno;
2N/A rpc_createerr.cf_error.re_terrno = t_errno;
2N/Aerr1: if (madefd)
2N/A (void) t_close(fd);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
2N/A * we try to not use them. The __rpc_raise_fd() routine will dup
2N/A * a descriptor to a higher value. If we fail to do it, we continue
2N/A * to use the old one (and hope for the best).
2N/A */
2N/Aint
2N/A__rpc_raise_fd(int fd)
2N/A{
2N/A int nfd;
2N/A
2N/A if ((nfd = fcntl(fd, F_DUPFD, RPC_MINFD)) == -1)
2N/A return (fd);
2N/A
2N/A if (t_sync(nfd) == -1) {
2N/A (void) close(nfd);
2N/A return (fd);
2N/A }
2N/A
2N/A if (t_close(fd) == -1) {
2N/A /* this is okay, we will syslog an error, then use the new fd */
2N/A (void) syslog(LOG_ERR,
2N/A "could not t_close() fd %d; mem & fd leak", fd);
2N/A }
2N/A
2N/A return (nfd);
2N/A}