#include "mt.h"
#include <errno.h>
#include <unistd.h>
#include <stropts.h>
#include <sys/stream.h>
#define _SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/timod.h>
#include <xti.h>
#include <signal.h>
#include <syslog.h>
#include "tx.h"
int
_tx_snddis(int fd, const struct t_call *call, int api_semantics)
{
struct T_discon_req dreq;
struct strbuf ctlbuf;
struct strbuf databuf;
struct _ti_user *tiptr;
int sv_errno;
int retval;
if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
return (-1);
sig_mutex_lock(&tiptr->ti_lock);
if (tiptr->ti_servtype == T_CLTS) {
t_errno = TNOTSUPPORT;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
if (_T_IS_XTI(api_semantics)) {
if (!(tiptr->ti_state == T_DATAXFER ||
tiptr->ti_state == T_OUTCON ||
tiptr->ti_state == T_OUTREL ||
tiptr->ti_state == T_INREL ||
(tiptr->ti_state == T_INCON && tiptr->ti_ocnt > 0))) {
t_errno = TOUTSTATE;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
}
if (call != NULL && call->udata.len) {
if ((tiptr->ti_ddatasize == T_INVALID ) ||
((tiptr->ti_ddatasize != T_INFINITE ) &&
(call->udata.len >
(uint32_t)tiptr->ti_ddatasize))) {
t_errno = TBADDATA;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
}
if ((tiptr->ti_state == T_INCON) &&
(call == NULL)) {
t_errno = TBADSEQ;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
if (_t_look_locked(fd, tiptr, 0, api_semantics) == T_DISCONNECT) {
t_errno = TLOOK;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
if ((tiptr->ti_lookcnt > 0) && (call == 0))
_t_flush_lookevents(tiptr);
do {
retval = ioctl(fd, I_FLUSH, FLUSHW);
} while (retval < 0 && errno == EINTR);
if (retval < 0) {
sv_errno = errno;
t_errno = TSYSERR;
sig_mutex_unlock(&tiptr->ti_lock);
errno = sv_errno;
return (-1);
}
ctlbuf.len = (int)sizeof (struct T_discon_req);
ctlbuf.maxlen = (int)sizeof (struct T_discon_req);
ctlbuf.buf = (char *)&dreq;
dreq.PRIM_type = T_DISCON_REQ;
dreq.SEQ_number = (call? call->sequence: -1);
databuf.maxlen = (call? call->udata.len: 0);
databuf.len = (call? call->udata.len: 0);
databuf.buf = (call? call->udata.buf: NULL);
sig_mutex_unlock(&tiptr->ti_lock);
if (putmsg(fd, &ctlbuf, (databuf.len? &databuf: NULL), 0) < 0) {
t_errno = TSYSERR;
return (-1);
}
sig_mutex_lock(&tiptr->ti_lock);
if (_t_is_ok(fd, tiptr, T_DISCON_REQ) < 0) {
sv_errno = errno;
sig_mutex_unlock(&tiptr->ti_lock);
errno = sv_errno;
return (-1);
}
tiptr->ti_flags &= ~(MORE|EXPEDITED);
if (tiptr->ti_ocnt <= 1) {
if (tiptr->ti_state == T_INCON) {
tiptr->ti_ocnt--;
tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
}
_T_TX_NEXTSTATE(T_SNDDIS1, tiptr,
"t_snddis: invalid state event T_SNDDIS1");
} else {
if (tiptr->ti_state == T_INCON) {
tiptr->ti_ocnt--;
tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
}
_T_TX_NEXTSTATE(T_SNDDIS2, tiptr,
"t_snddis: invalid state event T_SNDDIS2");
}
sig_mutex_unlock(&tiptr->ti_lock);
return (0);
}