#include "mt.h"
#include <stdlib.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 <stdio.h>
#include <errno.h>
#include <ucred.h>
#include <signal.h>
#include "tx.h"
static int _alloc_buf(struct netbuf *buf, t_scalar_t n, int fields,
int api_semantics, boolean_t option);
char *
_tx_alloc(int fd, int struct_type, int fields, int api_semantics)
{
struct strioctl strioc;
struct T_info_ack info;
union structptrs {
char *caddr;
struct t_bind *bind;
struct t_call *call;
struct t_discon *dis;
struct t_optmgmt *opt;
struct t_unitdata *udata;
struct t_uderr *uderr;
struct t_info *info;
} p;
unsigned int dsize;
struct _ti_user *tiptr;
int retval, sv_errno;
t_scalar_t optsize;
if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
return (NULL);
sig_mutex_lock(&tiptr->ti_lock);
info.PRIM_type = T_INFO_REQ;
strioc.ic_cmd = TI_GETINFO;
strioc.ic_timout = -1;
strioc.ic_len = (int)sizeof (struct T_info_req);
strioc.ic_dp = (char *)&info;
do {
retval = ioctl(fd, I_STR, &strioc);
} while (retval < 0 && errno == EINTR);
if (retval < 0) {
sv_errno = errno;
sig_mutex_unlock(&tiptr->ti_lock);
t_errno = TSYSERR;
errno = sv_errno;
return (NULL);
}
if (strioc.ic_len != (int)sizeof (struct T_info_ack)) {
t_errno = TSYSERR;
sig_mutex_unlock(&tiptr->ti_lock);
errno = EIO;
return (NULL);
}
switch (struct_type) {
case T_BIND:
if ((p.bind = calloc(1, sizeof (struct t_bind))) == NULL)
goto errout;
if (fields & T_ADDR) {
if (_alloc_buf(&p.bind->addr,
info.ADDR_size,
fields, api_semantics, B_FALSE) < 0)
goto errout;
}
sig_mutex_unlock(&tiptr->ti_lock);
return ((char *)p.bind);
case T_CALL:
if ((p.call = calloc(1, sizeof (struct t_call))) == NULL)
goto errout;
if (fields & T_ADDR) {
if (_alloc_buf(&p.call->addr,
info.ADDR_size,
fields, api_semantics, B_FALSE) < 0)
goto errout;
}
if (fields & T_OPT) {
if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
optsize = info.OPT_size +
TX_XTI_LEVEL_MAX_OPTBUF;
else
optsize = info.OPT_size;
if (_alloc_buf(&p.call->opt, optsize,
fields, api_semantics, B_TRUE) < 0)
goto errout;
}
if (fields & T_UDATA) {
dsize = _T_MAX((int)info.CDATA_size,
(int)info.DDATA_size);
if (_alloc_buf(&p.call->udata, (t_scalar_t)dsize,
fields, api_semantics, B_FALSE) < 0)
goto errout;
}
sig_mutex_unlock(&tiptr->ti_lock);
return ((char *)p.call);
case T_OPTMGMT:
if ((p.opt = calloc(1, sizeof (struct t_optmgmt))) == NULL)
goto errout;
if (fields & T_OPT) {
if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
optsize = info.OPT_size +
TX_XTI_LEVEL_MAX_OPTBUF;
else
optsize = info.OPT_size;
if (_alloc_buf(&p.opt->opt, optsize,
fields, api_semantics, B_TRUE) < 0)
goto errout;
}
sig_mutex_unlock(&tiptr->ti_lock);
return ((char *)p.opt);
case T_DIS:
if ((p.dis = calloc(1, sizeof (struct t_discon))) == NULL)
goto errout;
if (fields & T_UDATA) {
if (_alloc_buf(&p.dis->udata, info.DDATA_size,
fields, api_semantics, B_FALSE) < 0)
goto errout;
}
sig_mutex_unlock(&tiptr->ti_lock);
return ((char *)p.dis);
case T_UNITDATA:
if ((p.udata = calloc(1, sizeof (struct t_unitdata))) == NULL)
goto errout;
if (fields & T_ADDR) {
if (_alloc_buf(&p.udata->addr, info.ADDR_size,
fields, api_semantics, B_FALSE) < 0)
goto errout;
}
if (fields & T_OPT) {
if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
optsize = info.OPT_size +
TX_XTI_LEVEL_MAX_OPTBUF;
else
optsize = info.OPT_size;
if (_alloc_buf(&p.udata->opt, optsize,
fields, api_semantics, B_TRUE) < 0)
goto errout;
}
if (fields & T_UDATA) {
if (_alloc_buf(&p.udata->udata, info.TSDU_size,
fields, api_semantics, B_FALSE) < 0)
goto errout;
}
sig_mutex_unlock(&tiptr->ti_lock);
return ((char *)p.udata);
case T_UDERROR:
if ((p.uderr = calloc(1, sizeof (struct t_uderr))) == NULL)
goto errout;
if (fields & T_ADDR) {
if (_alloc_buf(&p.uderr->addr, info.ADDR_size,
fields, api_semantics, B_FALSE) < 0)
goto errout;
}
if (fields & T_OPT) {
if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
optsize = info.OPT_size +
TX_XTI_LEVEL_MAX_OPTBUF;
else
optsize = info.OPT_size;
if (_alloc_buf(&p.uderr->opt, optsize,
fields, api_semantics, B_FALSE) < 0)
goto errout;
}
sig_mutex_unlock(&tiptr->ti_lock);
return ((char *)p.uderr);
case T_INFO:
if ((p.info = calloc(1, sizeof (struct t_info))) == NULL)
goto errout;
sig_mutex_unlock(&tiptr->ti_lock);
return ((char *)p.info);
default:
if (_T_IS_XTI(api_semantics)) {
t_errno = TNOSTRUCTYPE;
sig_mutex_unlock(&tiptr->ti_lock);
} else {
t_errno = TSYSERR;
sig_mutex_unlock(&tiptr->ti_lock);
errno = EINVAL;
}
return (NULL);
}
errout:
if (p.caddr)
(void) t_free(p.caddr, struct_type);
t_errno = TSYSERR;
sig_mutex_unlock(&tiptr->ti_lock);
return (NULL);
}
static int
_alloc_buf(struct netbuf *buf, t_scalar_t n, int fields, int api_semantics,
boolean_t option)
{
switch (n) {
case T_INFINITE :
if (_T_IS_XTI(api_semantics)) {
buf->buf = NULL;
buf->maxlen = 0;
if (fields != T_ALL) {
errno = EINVAL;
return (-1);
}
} else if (option) {
static size_t infalloc;
if (infalloc == 0) {
size_t uc = ucred_size();
if (uc < 1024/2)
infalloc = 1024;
else
infalloc = uc + 1024/2;
}
if ((buf->buf = calloc(1, infalloc)) == NULL) {
errno = ENOMEM;
return (-1);
} else
buf->maxlen = infalloc;
} else {
if ((buf->buf = calloc(1, 1024)) == NULL) {
errno = ENOMEM;
return (-1);
} else
buf->maxlen = 1024;
}
break;
case 0:
buf->buf = NULL;
buf->maxlen = 0;
break;
case T_INVALID :
if (_T_IS_XTI(api_semantics)) {
buf->buf = NULL;
buf->maxlen = 0;
if (fields != T_ALL) {
errno = EINVAL;
return (-1);
}
} else {
buf->buf = NULL;
buf->maxlen = 0;
}
break;
default:
if ((buf->buf = calloc(1, (size_t)n)) == NULL) {
errno = ENOMEM;
return (-1);
} else
buf->maxlen = n;
break;
}
return (0);
}