xref: /onnv-gate/usr/src/lib/libnsl/nsl/t_getname.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
240Sstevel@tonic-gate /*	  All Rights Reserved  	*/
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
33*1219Sraf #include "mt.h"
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <stropts.h>
390Sstevel@tonic-gate #include <sys/stream.h>
400Sstevel@tonic-gate #include <xti.h>
410Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
420Sstevel@tonic-gate #include <sys/tihdr.h>
430Sstevel@tonic-gate #include <sys/timod.h>
440Sstevel@tonic-gate #include <assert.h>
450Sstevel@tonic-gate #include <signal.h>
460Sstevel@tonic-gate #include "tx.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static int __tx_tlitpi_getprotaddr_locked(struct _ti_user *tiptr,
490Sstevel@tonic-gate 	struct t_bind *boundaddr, struct t_bind *peer);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static int __tx_getname_locked(int fd, struct netbuf *name, int type);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate int
_tx_getname(int fd,struct netbuf * name,int type,int api_semantics)550Sstevel@tonic-gate _tx_getname(int fd, struct netbuf *name, int type, int api_semantics)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	struct _ti_user *tiptr;
580Sstevel@tonic-gate 	int retval, sv_errno;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	assert(_T_IS_TLI(api_semantics)); /* TLI only interface */
610Sstevel@tonic-gate 	if (!name || ((type != LOCALNAME) && (type != REMOTENAME))) {
620Sstevel@tonic-gate 		errno = EINVAL;
630Sstevel@tonic-gate 		return (-1);
640Sstevel@tonic-gate 	}
650Sstevel@tonic-gate 
66132Srobinson 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0)
670Sstevel@tonic-gate 		return (-1);
680Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	retval = __tx_getname_locked(fd, name, type);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (retval < 0) {
730Sstevel@tonic-gate 		sv_errno = errno;
740Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
750Sstevel@tonic-gate 		errno = sv_errno;
760Sstevel@tonic-gate 		return (-1);
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	return (0);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static int
__tx_getname_locked(int fd,struct netbuf * name,int type)860Sstevel@tonic-gate __tx_getname_locked(int fd, struct netbuf *name, int type)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	int retval;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	do {
91*1219Sraf 		retval = ioctl(fd,
920Sstevel@tonic-gate 		    (type == LOCALNAME) ? TI_GETMYNAME : TI_GETPEERNAME, name);
930Sstevel@tonic-gate 	} while (retval < 0 && errno == EINTR);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if (retval < 0) {
960Sstevel@tonic-gate 		t_errno = TSYSERR;
970Sstevel@tonic-gate 		return (-1);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 	return (0);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate int
_tx_getprotaddr(int fd,struct t_bind * boundaddr,struct t_bind * peeraddr,int api_semantics)1050Sstevel@tonic-gate _tx_getprotaddr(
1060Sstevel@tonic-gate 	int fd,
1070Sstevel@tonic-gate 	struct t_bind *boundaddr,
1080Sstevel@tonic-gate 	struct t_bind *peeraddr,
1090Sstevel@tonic-gate 	int api_semantics)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	struct _ti_user *tiptr;
1120Sstevel@tonic-gate 	int retval, sv_errno;
1130Sstevel@tonic-gate 	struct T_addr_req *addreqp;
1140Sstevel@tonic-gate 	struct T_addr_ack *addrackp;
1150Sstevel@tonic-gate 	int didalloc;
1160Sstevel@tonic-gate 	struct strbuf ctlbuf;
1170Sstevel@tonic-gate 	int retlen;
1180Sstevel@tonic-gate 
119132Srobinson 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0)
1200Sstevel@tonic-gate 		return (-1);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if ((tiptr->ti_prov_flag & XPG4_1) == 0) {
1250Sstevel@tonic-gate 		/*
1260Sstevel@tonic-gate 		 * Provider does not support XTI inspired TPI so we
1270Sstevel@tonic-gate 		 * try to do operation assuming TLI inspired TPI
1280Sstevel@tonic-gate 		 */
1290Sstevel@tonic-gate 		retval = __tx_tlitpi_getprotaddr_locked(tiptr, boundaddr,
1300Sstevel@tonic-gate 			peeraddr);
1310Sstevel@tonic-gate 		sv_errno = errno;
1320Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1330Sstevel@tonic-gate 		errno = sv_errno;
1340Sstevel@tonic-gate 		return (retval);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/*
1380Sstevel@tonic-gate 	 * Acquire buffer for use in sending/receiving the message.
1390Sstevel@tonic-gate 	 * Note: assumes (correctly) that ti_ctlsize is large enough
1400Sstevel@tonic-gate 	 * to hold sizeof (struct T_addr_req/ack)
1410Sstevel@tonic-gate 	 */
1420Sstevel@tonic-gate 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
1430Sstevel@tonic-gate 		sv_errno = errno;
1440Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1450Sstevel@tonic-gate 		errno = sv_errno;
1460Sstevel@tonic-gate 		return (-1);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
149132Srobinson 	/* LINTED pointer cast */
1500Sstevel@tonic-gate 	addreqp = (struct T_addr_req *)ctlbuf.buf;
1510Sstevel@tonic-gate 	addreqp->PRIM_type = T_ADDR_REQ;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	do {
1540Sstevel@tonic-gate 		retval = _t_do_ioctl(fd, ctlbuf.buf,
1550Sstevel@tonic-gate 			(int)sizeof (struct T_addr_req), TI_GETADDRS, &retlen);
1560Sstevel@tonic-gate 	} while (retval < 0 && errno == EINTR);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/* retval can now be either 0 or -1 */
1590Sstevel@tonic-gate 	if (retval < 0) {
1600Sstevel@tonic-gate 		sv_errno = errno;
1610Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1620Sstevel@tonic-gate 		errno = sv_errno;
1630Sstevel@tonic-gate 		goto err_out;
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (retlen < (int)sizeof (struct T_addr_ack)) {
1680Sstevel@tonic-gate 		t_errno = TSYSERR;
1690Sstevel@tonic-gate 		errno = EIO;
1700Sstevel@tonic-gate 		retval = -1;
1710Sstevel@tonic-gate 		goto err_out;
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
174132Srobinson 	/* LINTED pointer cast */
1750Sstevel@tonic-gate 	addrackp = (struct T_addr_ack *)ctlbuf.buf;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * We assume null parameters are OK and not errors
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 	if (boundaddr != NULL && boundaddr->addr.maxlen > 0) {
1810Sstevel@tonic-gate 		if (TLEN_GT_NLEN(addrackp->LOCADDR_length,
1820Sstevel@tonic-gate 		    boundaddr->addr.maxlen)) {
1830Sstevel@tonic-gate 			t_errno = TBUFOVFLW;
1840Sstevel@tonic-gate 			retval = -1;
1850Sstevel@tonic-gate 			goto err_out;
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 		boundaddr->addr.len = addrackp->LOCADDR_length;
188132Srobinson 		(void) memcpy(boundaddr->addr.buf,
1890Sstevel@tonic-gate 		    ctlbuf.buf + addrackp->LOCADDR_offset,
1900Sstevel@tonic-gate 		    (size_t)addrackp->LOCADDR_length);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * Note: In states where there is no remote end of the connection
1950Sstevel@tonic-gate 	 * the T_ADDR_REQ primitive does not return a remote address. However,
1960Sstevel@tonic-gate 	 * in protcols such as TCP, the transport connection is established
1970Sstevel@tonic-gate 	 * before the TLI/XTI level association is established. Therefore,
1980Sstevel@tonic-gate 	 * in state T_OUTCON, the transport may return a remote address where
1990Sstevel@tonic-gate 	 * TLI/XTI level thinks there is no remote end and therefore
2000Sstevel@tonic-gate 	 * no remote address should be returned. We therefore do not look at
2010Sstevel@tonic-gate 	 * address returned by transport provider in T_OUTCON state.
2020Sstevel@tonic-gate 	 * Tested by XTI test suite.
2030Sstevel@tonic-gate 	 */
2040Sstevel@tonic-gate 	if (tiptr->ti_state != T_OUTCON &&
2050Sstevel@tonic-gate 	    peeraddr != NULL && peeraddr->addr.maxlen > 0) {
2060Sstevel@tonic-gate 		if (TLEN_GT_NLEN(addrackp->REMADDR_length,
2070Sstevel@tonic-gate 		    peeraddr->addr.maxlen)) {
2080Sstevel@tonic-gate 			t_errno = TBUFOVFLW;
2090Sstevel@tonic-gate 			retval = -1;
2100Sstevel@tonic-gate 			goto err_out;
2110Sstevel@tonic-gate 		}
2120Sstevel@tonic-gate 		peeraddr->addr.len = addrackp->REMADDR_length;
213132Srobinson 		(void) memcpy(peeraddr->addr.buf,
2140Sstevel@tonic-gate 		    ctlbuf.buf + addrackp->REMADDR_offset,
2150Sstevel@tonic-gate 		    (size_t)addrackp->REMADDR_length);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate err_out:
2190Sstevel@tonic-gate 	if (didalloc)
2200Sstevel@tonic-gate 		free(ctlbuf.buf);
2210Sstevel@tonic-gate 	else
2220Sstevel@tonic-gate 		tiptr->ti_ctlbuf = ctlbuf.buf;
2230Sstevel@tonic-gate 	return (retval);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate static int
__tx_tlitpi_getprotaddr_locked(struct _ti_user * tiptr,struct t_bind * boundaddr,struct t_bind * peeraddr)2270Sstevel@tonic-gate __tx_tlitpi_getprotaddr_locked(
2280Sstevel@tonic-gate 	struct _ti_user *tiptr,
2290Sstevel@tonic-gate 	struct t_bind *boundaddr,
2300Sstevel@tonic-gate 	struct t_bind *peeraddr)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	if (boundaddr) {
2330Sstevel@tonic-gate 		boundaddr->addr.len = 0;
2340Sstevel@tonic-gate 		if (tiptr->ti_state >= TS_IDLE) {
2350Sstevel@tonic-gate 			/*
2360Sstevel@tonic-gate 			 * assume bound endpoint .
2370Sstevel@tonic-gate 			 * Note: TI_GETMYNAME can return
2380Sstevel@tonic-gate 			 * a finite length all zeroes address for unbound
2390Sstevel@tonic-gate 			 * endpoint so we avoid relying on it for bound
2400Sstevel@tonic-gate 			 * endpoints for XTI t_getprotaddr() semantics.
2410Sstevel@tonic-gate 			 */
2420Sstevel@tonic-gate 			if (__tx_getname_locked(tiptr->ti_fd, &boundaddr->addr,
2430Sstevel@tonic-gate 			    LOCALNAME) < 0)
2440Sstevel@tonic-gate 				return (-1);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	if (peeraddr) {
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		peeraddr->addr.len = 0;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		if (tiptr->ti_state >= TS_DATA_XFER) {
2530Sstevel@tonic-gate 			/*
2540Sstevel@tonic-gate 			 * assume connected endpoint.
2550Sstevel@tonic-gate 			 * The TI_GETPEERNAME call can fail with error
2560Sstevel@tonic-gate 			 * if endpoint is not connected so we don't call it
2570Sstevel@tonic-gate 			 * for XTI t_getprotaddr() semantics
2580Sstevel@tonic-gate 			 */
2590Sstevel@tonic-gate 			if (__tx_getname_locked(tiptr->ti_fd, &peeraddr->addr,
2600Sstevel@tonic-gate 			    REMOTENAME) < 0)
2610Sstevel@tonic-gate 				return (-1);
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 	return (0);
2650Sstevel@tonic-gate }
266