#include "mt.h"
#include <unistd.h>
#include <stdlib.h>
#include <errno.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 <syslog.h>
#include "tx.h"
int
_tx_snd(int fd, char *buf, unsigned nbytes, int flags, int api_semantics)
{
struct T_data_req datareq;
struct strbuf ctlbuf, databuf;
unsigned int bytes_sent, bytes_remaining, bytes_to_send;
char *curptr;
struct _ti_user *tiptr;
int band;
int retval, lookevent;
int sv_errno;
int doputmsg = 0;
int32_t tsdu_limit;
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_INREL)) {
t_errno = TOUTSTATE;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
if ((flags & ~(TX_ALL_VALID_FLAGS)) != 0) {
t_errno = TBADFLAG;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
if (flags & T_EXPEDITED)
tsdu_limit = tiptr->ti_etsdusize;
else {
tsdu_limit = tiptr->ti_tsdusize;
}
if ((tsdu_limit > 0) &&
(nbytes > (uint32_t)tsdu_limit)) {
t_errno = TBADDATA;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
lookevent = _t_look_locked(fd, tiptr, 0, api_semantics);
if (lookevent < 0) {
sv_errno = errno;
sig_mutex_unlock(&tiptr->ti_lock);
errno = sv_errno;
return (-1);
}
if (lookevent == T_DISCONNECT ||
(api_semantics == TX_XTI_XNS4_API &&
lookevent == T_ORDREL)) {
t_errno = TLOOK;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
}
if (nbytes == 0 && !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
t_errno = TBADDATA;
sig_mutex_unlock(&tiptr->ti_lock);
return (-1);
}
doputmsg = (tiptr->ti_tsdusize != 0) || (flags & T_EXPEDITED);
if (doputmsg) {
ctlbuf.maxlen = (int)sizeof (struct T_data_req);
ctlbuf.len = (int)sizeof (struct T_data_req);
ctlbuf.buf = (char *)&datareq;
band = TI_NORMAL;
if (flags & T_EXPEDITED) {
datareq.PRIM_type = T_EXDATA_REQ;
if (! (tiptr->ti_prov_flag & EXPINLINE))
band = TI_EXPEDITED;
} else
datareq.PRIM_type = T_DATA_REQ;
}
bytes_remaining = nbytes;
curptr = buf;
sig_mutex_unlock(&tiptr->ti_lock);
do {
bytes_to_send = bytes_remaining;
if (doputmsg) {
if (bytes_to_send > (unsigned int)tiptr->ti_maxpsz) {
datareq.MORE_flag = 1;
bytes_to_send = (unsigned int)tiptr->ti_maxpsz;
} else {
if (flags&T_MORE)
datareq.MORE_flag = 1;
else
datareq.MORE_flag = 0;
}
databuf.maxlen = bytes_to_send;
databuf.len = bytes_to_send;
databuf.buf = curptr;
retval = putpmsg(fd, &ctlbuf, &databuf, band, MSG_BAND);
if (retval == 0)
bytes_sent = bytes_to_send;
} else {
retval = write(fd, curptr, bytes_to_send);
if (retval >= 0) {
bytes_sent = retval;
}
}
if (retval < 0) {
if (nbytes == bytes_remaining) {
if (errno == EAGAIN)
t_errno = TFLOW;
else
t_errno = TSYSERR;
return (-1);
}
break;
}
bytes_remaining = bytes_remaining - bytes_sent;
curptr = curptr + bytes_sent;
} while (bytes_remaining != 0);
_T_TX_NEXTSTATE(T_SND, tiptr, "t_snd: invalid state event T_SND");
return (nbytes - bytes_remaining);
}