/*
 * |-----------------------------------------------------------|
 * | Copyright (c) 1991, 1990 MIPS Computer Systems, Inc.      |
 * | All Rights Reserved                                       |
 * |-----------------------------------------------------------|
 * |          Restricted Rights Legend                         |
 * | Use, duplication, or disclosure by the Government is      |
 * | subject to restrictions as set forth in                   |
 * | subparagraph (c)(1)(ii) of the Rights in Technical        |
 * | Data and Computer Software Clause of DFARS 252.227-7013.  |
 * |         MIPS Computer Systems, Inc.                       |
 * |         950 DeGuigne Avenue                               |
 * |         Sunnyvale, California 94088-3650, USA             |
 * |-----------------------------------------------------------|
 */
#ident	"$Header: t_bind.c,v 1.2.2.1 92/06/24 14:11:36 marker Exp $"
/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#include "sys/param.h"
#include "sys/types.h"
#include "sys/errno.h"
#include "sys/stream.h"
#include "sys/stropts.h"
#include "sys/tihdr.h"
#include "sys/timod.h"
#include "sys/tiuser.h"
#include "sys/signal.h"
#include "_import.h"


extern int t_errno;
extern int errno;
extern struct _ti_user *_t_checkfd();
extern void (*sigset())();
extern char *memcpy();


t_bind(fd, req, ret)
int fd;
register struct t_bind *req;
register struct t_bind *ret;
{
	register char *buf;
	register struct T_bind_req *ti_bind;
	int size;
	register struct _ti_user *tiptr;
	void (*sigsave)();


	if ((tiptr = _t_checkfd(fd)) == NULL)
		return(-1);


	sigsave = sigset(SIGPOLL, SIG_HOLD);
	buf = tiptr->ti_ctlbuf;
	ti_bind = (struct T_bind_req *)buf;
	size = sizeof(struct T_bind_req);

	ti_bind->PRIM_type = T_BIND_REQ;
	ti_bind->ADDR_length = (req == NULL? 0: req->addr.len);
	ti_bind->ADDR_offset = 0;
	ti_bind->CONIND_number = (req == NULL? 0: req->qlen);


	if (ti_bind->ADDR_length) {
		_t_aligned_copy(buf, (int)ti_bind->ADDR_length, size,
			     req->addr.buf, &ti_bind->ADDR_offset);
		size = ti_bind->ADDR_offset + ti_bind->ADDR_length;
	}
			       

	if (!_t_do_ioctl(fd, buf, size, TI_BIND, NULL)) {
		sigset(SIGPOLL, sigsave);
		return(-1);
	}
	sigset(SIGPOLL, sigsave);

	tiptr->ti_ocnt = 0;
	tiptr->ti_state = TLI_NEXTSTATE(T_BIND, tiptr->ti_state);

	if ((ret != NULL) && (ti_bind->ADDR_length > ret->addr.maxlen)) {
		t_errno = TBUFOVFLW;
		return(-1);
	}

	if (ret != NULL) {
		memcpy(ret->addr.buf, (char *)(buf + ti_bind->ADDR_offset),
		       (int)ti_bind->ADDR_length);
		ret->addr.len = ti_bind->ADDR_length;
		ret->qlen = ti_bind->CONIND_number;
	}

	return(0);
}
