include std.h
include gpsprint.h

define ERC_LPD_TIMEOUT		0xaaaa
define ERC_LPD_PROTOCOL	0xaaab
define ERC_LPD_FILE		0xaaac

typedef struct log_type {
	unsigned			type;
	unsigned			cbmessage;
	char				message[124];
} LOG;

define LOG_USER_TYPE	0xFFF7

typedef struct pbcb_type {
	char				* pb;
	unsigned int		cb;
} PBCB;

typedef struct timerrequestblocktype{
	unsigned			counter;
	unsigned			counterreload;
	unsigned			cevents;
	unsigned			exchresp;
	unsigned			ercret;
	unsigned			rqcode;
} TRB;

define OPENRTCLOCK		0x0031

typedef struct requesttype{
	unsigned char		scntinfo;
	unsigned char		rtcode;
	unsigned char		nreqpbcb;
	unsigned char		nresppbcb;
	unsigned			usernum;
	unsigned			exchresp;
	unsigned			ercret;
	unsigned			rqcode;
} REQUEST;

define NULLREQUEST		(REQUEST *)0

typedef struct processdescriptorblocktype {
	unsigned			* pentry;
	unsigned			sadata;
	unsigned			saextra;
	unsigned			sastack;
	unsigned			ostackinit;
	unsigned char		priority;
	unsigned char		fsys;
	unsigned			defaultrespexch;
	unsigned char		fdebug;
} PDB;

typedef struct systemconfigblocktype {
	unsigned char		systembuildtype;
	unsigned char		ostype;
	unsigned			saminll;
	unsigned			sacurrll;
	unsigned			sacurrsl;
	unsigned			samaxsl;
	unsigned			samemmax;
	unsigned			cpcb;
	unsigned			cexch;
	unsigned			clinkblk;
	unsigned			clinkblkres;
	unsigned			ctrb;
	unsigned			ciob;
	unsigned			fcb;
	unsigned			cvhb;
	unsigned			cucb;
	unsigned			cufb;
	unsigned char		hardwaretype;
	unsigned char		clusterconfiguration;
	unsigned char		fnofilesystem;
	unsigned char		fcommiop;
	unsigned char		fmultipartition;
} SCB;

define SCB_CODE		0x02c8

define MODE_MODIFY		0x6D6D

define PRIORITY_AGENT	144
define PRIORITY_SERVER	PRIORITY_AGENT + 1


define ALLOC_DEFAULT	20
define TICKS			5
define NTICKS			15

define AGENT			0
define SERVER			1

extern unsigned			session_num[];
extern unsigned			session_out[];

extern unsigned			exchanges[];

extern unsigned			srp;
extern unsigned			protected;
extern unsigned			sp_os;
extern unsigned			clstr;

extern unsigned long	tcp_ipaddr;

extern unsigned			lpd_port;
extern unsigned			lpd_nticks;
extern unsigned			lpd_usernumber;

extern TRB				timer_rq;

define SET_RQ_OPEN_TCP_CONNECTION	1
define SET_RQ_CLOSE_TCP_CONNECTION	1
define SET_RQ_ABORT_TCP_CONNECTION	1
define SET_RQ_SEND_TCP_MESSAGE		1
define SET_RQ_RECEIVE_TCP_MESSAGE	1
define SET_RQ_GET_TCB				1
define SET_TCB_RETURNED			1

include internetct.h

extern unsigned			tcp_error( );

extern void				tcp_open_connection_request( );
extern void				tcp_open_connection_response( );
extern void				tcp_get_tcb_request( );
extern void				tcp_get_tcb_response( );
extern void				tcp_send_message_request( );
extern void				tcp_send_message_response( );
extern void				tcp_receive_message_request( );
extern void				tcp_receive_message_response( );
extern void				tcp_close_connection_request( );
extern void				tcp_close_connection_response( );
extern void				tcp_abort_connection_request( );
extern void				tcp_abort_connection_response( );

extern void				agent_sent( );
extern void				agent_receive( );
extern void				agent_process( );
extern void				agent_transport_failure( );
extern void				agent_abort_all( );

extern void				server_sent( );
extern void				server_receive( );
extern void				server_process( );
extern void				server_transport_failure( );
extern void				server_abort_all( );

extern unsigned			file_open( );
extern long				file_size( );
extern unsigned			file_read_line( );
extern unsigned			file_read_buffer( );
extern unsigned			file_write_buffer( );
extern void				file_close( );

define SIZEBUFFER		512

typedef struct session_type {
	struct session_type	* next;
	struct session_type	* prev;
	unsigned			sequence;

	unsigned char		type;
	unsigned char		read_out;
	unsigned char		write_out;
	unsigned char		gettcb_out;
	unsigned			rq_out;
	unsigned			nticks;

	unsigned			in;				* RECEIVING VARIABLES *
	unsigned			sofar;
	unsigned			total;
	unsigned char		in_buffer[SIZEBUFFER];

	unsigned			size_out;		* SENDING VARIABLES *
	unsigned char		out_buffer[SIZEBUFFER];

	unsigned			fh;				* FILE VARIABLES *
	long				file_size;
	unsigned char		file[14+14+14+50+13+1];	

	unsigned			sline;			* COMMAND REASSEMBLY *
	unsigned char		line[81];	

	unsigned char		job[3];	

	unsigned			squeue;			* QUEUE NAME *
	unsigned char		queue[30];	

	unsigned			dfa_offset;

	unsigned char		mode;	

	unsigned char		counting;		* CHECKING FLOW *
	unsigned			count_ok;
	unsigned			count;
	
	unsigned			state;

	unsigned char		aborting;
	unsigned			aborting_erc;

	unsigned char		reset;
	unsigned char		used;
	unsigned char		at;
	unsigned long		address;
	unsigned			port;
	unsigned			index;
	unsigned			handle;
	unsigned			exchange;
	unsigned char		push;
	unsigned char		urgent;
	unsigned			snode;
	unsigned char		node[12];
	TCB_RETURNED		tcb;
	unsigned			stcb;
	RQ_OPEN_TCP_CONNECTION	open;
	RQ_GET_TCB				get;
	RQ_RECEIVE_TCP_MESSAGE	receive;
	RQ_SEND_TCP_MESSAGE		send;
	RQ_CLOSE_TCP_CONNECTION	close;
	RQ_ABORT_TCP_CONNECTION	abort;
	struct session_type	* this;
} SESSION;

define SESSION_OFF_ABORT	sizeof( RQ_ABORT_TCP_CONNECTION)
define SESSION_OFF_CLOSE	SESSION_OFF_ABORT+sizeof( RQ_CLOSE_TCP_CONNECTION)
define SESSION_OFF_SEND	SESSION_OFF_CLOSE + sizeof( RQ_SEND_TCP_MESSAGE)
define SESSION_OFF_RECEIVE	SESSION_OFF_SEND + sizeof( RQ_RECEIVE_TCP_MESSAGE)
define SESSION_OFF_GET		SESSION_OFF_RECEIVE+sizeof(RQ_GET_TCB)
define SESSION_OFF_OPEN	SESSION_OFF_GET+sizeof(RQ_OPEN_TCP_CONNECTION)

define NULLSESSION		(SESSION *)0

extern unsigned			session_index( );
extern unsigned			session_name( );
extern SESSION			* session_lookup( );
extern SESSION			* session_create( );
extern void				session_send( );
extern void				session_arrived( );
extern void				session_sent( );
extern void				session_free( );

extern SESSION			* sessions[];

define SESSION_STATE_COMMAND	0
define SESSION_STATE_RX_CMD	1
define SESSION_STATE_C_DATA	2
define SESSION_STATE_C_ACK		3
define SESSION_STATE_D_DATA	4
define SESSION_STATE_D_ACK		5

extern unsigned			server_stack;
extern unsigned			ds_reg;

extern unsigned char	sbverrun[];

extern unsigned char	* utoa( );

extern unsigned char	* calloc( );
extern void				free( );

extern char				lpd_version[];

d			saminll;
	unsigned			sacurrll;
	unsigned			sacurrsl;
	unsigned			samaxsl;
	unsigned			samemmax;
	unsigned			cpcb;
	unsigned			cexch;
	unsigned			clinkblk;
	unsigned			clinkblkres;
	unsigned			ctrb;
	unsigned			ciob;
	unsigned			fcb;
	unsigned			cvhb;
	unsigned			cucb;
	unsigned			cufb;
	unsigned char		hardwaretype;
	unsigned char		clusterconfiguration;
	unsigned cha