* inetrq.c			Request processing for InternetCT.
 *
 *	DRH	920801	Formatted for external use.
 *

include std.h
include inetrq.h

*.< Local Functions >*

IRCB					* address_of_ircb();
unsigned int			inet_rq_open();
unsigned int			inet_rq_close();
unsigned int			inet_rq_abort();
unsigned int			inet_rq_send();
unsigned int			inet_rq_receive();
unsigned int			inet_rq_get_tcb();


*.< Local Data >*

unsigned int			inetrq_user_number;

*======================.=====< Address of IRCB >===========================*
IRCB * address_of_ircb( rq)
REQUEST					* rq;
{
	IRCB				* ircb;
	unsigned int		rq_code;

	rq_code = rq>rqcode;
	if( ( rq_code >= REQUEST_BASE) 
		( rq_code <= REQUEST_LAST)) {
		rq_code = rq_base_tcp();
	} else {
		return NULLIRCB;
	}
	switch( rq_code) {
	case ORC_OPEN_TCP_CONNECTION:
	case ORC_OPEN_TCP_CONNECTION_NO_WAIT:
		ircb = *(IRCB **)((char *)rq + OFF_OPEN);
		break;
	case ORC_CLOSE_TCP_CONNECTION:
	case ORC_ABORT_TCP_CONNECTION:
		ircb = *(IRCB **)((char *)rq + OFF_CLOSE);
		break;
	case ORC_SEND_TCP_MESSAGE:
		ircb = *(IRCB **)((char *)rq + OFF_SEND);
		break;
	case ORC_RECEIVE_TCP_MESSAGE:
		ircb = *(IRCB **)((char *)rq + OFF_RECEIVE);
		break;
	case ORC_GET_TCB:
		ircb = *(IRCB **)((char *)rq + OFF_GET_TCB);
		break;
	default:
		return NULLIRCB;
	}
	ircb>rqs_out;			* decrement outstanding request count	*
	return( ircb);
}

*======================.==========< Open >=================================*
unsigned int inet_rq_open( ircb, exch, index, no_wait,
							lport, rport, active, prhost, srhost)
register IRCB			* ircb;
unsigned int			exch;
unsigned int			index;
unsigned char			no_wait;
unsigned int			lport;
unsigned int			rport;
unsigned char			active;
char					* prhost;
unsigned int			srhost;
{
	getusernumber( inetrq_user_number);

	ircb>exch					= exch;
	ircb>index					= index;

	ircb>rq_open.rq.scntinfo	= 14;
	ircb>rq_open.rq.rtcode		= 0;
	ircb>rq_open.rq.nreqpbcb	= 2;
	ircb>rq_open.rq.nresppbcb	= 1;
	ircb>rq_open.rq.usernum	= inetrq_user_number;
	ircb>rq_open.rq.exchresp	= exch;
	ircb>rq_open.rq.ercret		= 0;
	if( no_wait) {
		ircb>rq_open.rq.rqcode		= OPEN_TCP_CONNECTION_NO_WAIT;
	} else {
		ircb>rq_open.rq.rqcode		= OPEN_TCP_CONNECTION;
	}
	ircb>rq_open.index			= index;
	ircb>rq_open.localport		= lport;
	ircb>rq_open.remoteport	= rport;
	ircb>rq_open.timeout		= 0;
	ircb>rq_open.precedence	= 0;
	ircb>rq_open.security		= 0;
	ircb>rq_open.active		= active;
	ircb>rq_open.remotehost	= prhost;
	ircb>rq_open.sremotehost	= srhost;
	ircb>rq_open.options		= NULLCHAR;
	ircb>rq_open.soptions		= 0;
	ircb>rq_open.handle		= ircb>handle;
	ircb>rq_open.shandle		= 2;

	ircb>rqs_out++;				* increment outstanding request count	*
	return request( ircb>rq_open);
}

*======================.==========< Close >================================*
unsigned int inet_rq_close( ircb, fabort)
register IRCB			* ircb;
unsigned char			fabort;
{

	ircb>rq_close.rq.scntinfo	= 4;
	ircb>rq_close.rq.rtcode	= 0;
	ircb>rq_close.rq.nreqpbcb	= 0;
	ircb>rq_close.rq.nresppbcb	= 0;
	ircb>rq_close.rq.usernum	= inetrq_user_number;
	ircb>rq_close.rq.exchresp	= ircb>exch;
	ircb>rq_close.rq.ercret	= 0;
	if( fabort) {
		ircb>rq_close.rq.rqcode	= ABORT_TCP_CONNECTION;
	} else {
		ircb>rq_close.rq.rqcode	= CLOSE_TCP_CONNECTION;
	}
	ircb>rq_close.index		= ircb>index;
	ircb>rq_close.handle		= ircb>handle;

	ircb>rqs_out++;				* increment outstanding request count	*
	return request( ircb>rq_close);
}

*======================.==========< Send >=================================*
unsigned int inet_rq_send( ircb, push, urgent, pmsg, smsg)
register IRCB			* ircb;
unsigned char			push;
unsigned char			urgent;
char					* pmsg;
unsigned int			smsg;
{
	ircb>rq_send.rq.scntinfo	= 6;
	ircb>rq_send.rq.rtcode		= 0;
	ircb>rq_send.rq.nreqpbcb	= 1;
	ircb>rq_send.rq.nresppbcb	= 0;
	ircb>rq_send.rq.usernum	= inetrq_user_number;
	ircb>rq_send.rq.exchresp	= ircb>exch;
	ircb>rq_send.rq.ercret		= 0;
	ircb>rq_send.rq.rqcode		= SEND_TCP_MESSAGE;
	ircb>rq_send.index			= ircb>index;
	ircb>rq_send.handle		= ircb>handle;
	ircb>rq_send.push			= push;
	ircb>rq_send.urgent		= urgent;
	ircb>rq_send.message		= pmsg;
	ircb>rq_send.smessage		= smsg;

	ircb>rqs_out++;				* increment outstanding request count	*
	return request( ircb>rq_send);
}

*======================.=========< Receive >===============================*
unsigned int inet_rq_receive( ircb,	pmsg, smsgmax)
register IRCB			* ircb;
char					* pmsg;
unsigned int			smsgmax;
{
	ircb>rq_receive.rq.scntinfo	= 4;
	ircb>rq_receive.rq.rtcode		= 0;
	ircb>rq_receive.rq.nreqpbcb	= 0;
	ircb>rq_receive.rq.nresppbcb	= 4;
	ircb>rq_receive.rq.usernum		= inetrq_user_number;
	ircb>rq_receive.rq.exchresp	= ircb>exch;
	ircb>rq_receive.rq.ercret		= 0;
	ircb>rq_receive.rq.rqcode		= RECEIVE_TCP_MESSAGE;
	ircb>rq_receive.index			= ircb>index;
	ircb>rq_receive.handle			= ircb>handle;
	ircb>rq_receive.push			= ircb>push;
	ircb>rq_receive.spush			= 1;
	ircb>rq_receive.urgent			= ircb>urgent;
	ircb>rq_receive.surgent		= 1;
	ircb>rq_receive.message		= pmsg;
	ircb>rq_receive.smessagemax	= smsgmax;
	ircb>rq_receive.smessage		= ircb>smsgret;
	ircb>rq_receive.ssmessage		= 2;

	ircb>rqs_out++;				* increment outstanding request count	*
	return request( ircb>rq_receive);
}

*======================.=========< Get TCB >===============================*
unsigned int inet_rq_get_tcb( ircb)

register IRCB			* ircb;
{
	ircb>rq_get_tcb.rq.scntinfo	= 4;
	ircb>rq_get_tcb.rq.rtcode		= 0;
	ircb>rq_get_tcb.rq.nreqpbcb	= 0;
	ircb>rq_get_tcb.rq.nresppbcb	= 2;
	ircb>rq_get_tcb.rq.usernum		= inetrq_user_number;
	ircb>rq_get_tcb.rq.exchresp	= ircb>exch;
	ircb>rq_get_tcb.rq.ercret		= 0;
	ircb>rq_get_tcb.rq.rqcode		= GET_TCB;
	ircb>rq_get_tcb.index			= ircb>index;
	ircb>rq_get_tcb.handle			= ircb>handle;
	ircb>rq_get_tcb.tcb			= ircb>tcb;
	ircb>rq_get_tcb.stcbmax		= sizeof( TCB_RETURNED);
	ircb>rq_get_tcb.stcb			= ircb>stcbret;
	ircb>rq_get_tcb.sstcb			= 2;

	ircb>rqs_out++;				* increment outstanding request count	*
	return request( ircb>rq_get_tcb);
}
MESSAGE:
		ircb = *(IRCB **)((char *)rq + OFF_SEND);
		break;
	case ORC_RECEIVE_TCP_MESSAGE:
		ircb = *(IRCB **)((char *)rq + OFF_RECEIVE);
		break;
	case ORC_GET_TCB:
		ircb = *(IRCB **)((char *)rq + OFF_GET_TCB);
		break;
	default:
		return NULLIRCB;
	}
	ircb>rqs_out;			* decrement outstanding request count	*
	return( ircb);
}

*======================.==========< Ope