#include "mt.h"
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stropts.h>
#include <sys/stream.h>
#include <xti.h>
#define _SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/timod.h>
#include <assert.h>
#include <signal.h>
#include "tx.h"
static int __tx_tlitpi_getprotaddr_locked(struct _ti_user *tiptr,
struct t_bind *boundaddr, struct t_bind *peer);
static int __tx_getname_locked(int fd, struct netbuf *name, int type);
int
_tx_getname(int fd, struct netbuf *name, int type, int api_semantics)
{
struct _ti_user *tiptr;
int retval, sv_errno;
assert(_T_IS_TLI(api_semantics));
if (!name || ((type != LOCALNAME) && (type != REMOTENAME))) {
errno = EINVAL;
return (-1);
}
if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0)
return (-1);
sig_mutex_lock(&tiptr->ti_lock);
retval = __tx_getname_locked(fd, name, type);
if (retval < 0) {
sv_errno = errno;
sig_mutex_unlock(&tiptr->ti_lock);
errno = sv_errno;
return (-1);
}
sig_mutex_unlock(&tiptr->ti_lock);
return (0);
}
static int
__tx_getname_locked(int fd, struct netbuf *name, int type)
{
int retval;
do {
retval = ioctl(fd,
(type == LOCALNAME) ? TI_GETMYNAME : TI_GETPEERNAME, name);
} while (retval < 0 && errno == EINTR);
if (retval < 0) {
t_errno = TSYSERR;
return (-1);
}
return (0);
}
int
_tx_getprotaddr(
int fd,
struct t_bind *boundaddr,
struct t_bind *peeraddr,
int api_semantics)
{
struct _ti_user *tiptr;
int retval, sv_errno;
struct T_addr_req *addreqp;
struct T_addr_ack *addrackp;
int didalloc;
struct strbuf ctlbuf;
int retlen;
if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0)
return (-1);
sig_mutex_lock(&tiptr->ti_lock);
if ((tiptr->ti_prov_flag & XPG4_1) == 0) {
retval = __tx_tlitpi_getprotaddr_locked(tiptr, boundaddr,
peeraddr);
sv_errno = errno;
sig_mutex_unlock(&tiptr->ti_lock);
errno = sv_errno;
return (retval);
}
if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
sv_errno = errno;
sig_mutex_unlock(&tiptr->ti_lock);
errno = sv_errno;
return (-1);
}
addreqp = (struct T_addr_req *)ctlbuf.buf;
addreqp->PRIM_type = T_ADDR_REQ;
do {
retval = _t_do_ioctl(fd, ctlbuf.buf,
(int)sizeof (struct T_addr_req), TI_GETADDRS, &retlen);
} while (retval < 0 && errno == EINTR);
if (retval < 0) {
sv_errno = errno;
sig_mutex_unlock(&tiptr->ti_lock);
errno = sv_errno;
goto err_out;
}
sig_mutex_unlock(&tiptr->ti_lock);
if (retlen < (int)sizeof (struct T_addr_ack)) {
t_errno = TSYSERR;
errno = EIO;
retval = -1;
goto err_out;
}
addrackp = (struct T_addr_ack *)ctlbuf.buf;
if (boundaddr != NULL && boundaddr->addr.maxlen > 0) {
if (TLEN_GT_NLEN(addrackp->LOCADDR_length,
boundaddr->addr.maxlen)) {
t_errno = TBUFOVFLW;
retval = -1;
goto err_out;
}
boundaddr->addr.len = addrackp->LOCADDR_length;
(void) memcpy(boundaddr->addr.buf,
ctlbuf.buf + addrackp->LOCADDR_offset,
(size_t)addrackp->LOCADDR_length);
}
if (tiptr->ti_state != T_OUTCON &&
peeraddr != NULL && peeraddr->addr.maxlen > 0) {
if (TLEN_GT_NLEN(addrackp->REMADDR_length,
peeraddr->addr.maxlen)) {
t_errno = TBUFOVFLW;
retval = -1;
goto err_out;
}
peeraddr->addr.len = addrackp->REMADDR_length;
(void) memcpy(peeraddr->addr.buf,
ctlbuf.buf + addrackp->REMADDR_offset,
(size_t)addrackp->REMADDR_length);
}
err_out:
if (didalloc)
free(ctlbuf.buf);
else
tiptr->ti_ctlbuf = ctlbuf.buf;
return (retval);
}
static int
__tx_tlitpi_getprotaddr_locked(
struct _ti_user *tiptr,
struct t_bind *boundaddr,
struct t_bind *peeraddr)
{
if (boundaddr) {
boundaddr->addr.len = 0;
if (tiptr->ti_state >= TS_IDLE) {
if (__tx_getname_locked(tiptr->ti_fd, &boundaddr->addr,
LOCALNAME) < 0)
return (-1);
}
}
if (peeraddr) {
peeraddr->addr.len = 0;
if (tiptr->ti_state >= TS_DATA_XFER) {
if (__tx_getname_locked(tiptr->ti_fd, &peeraddr->addr,
REMOTENAME) < 0)
return (-1);
}
}
return (0);
}