#include <io.h>
#include <fcntl.h>
#include <conio.h>
#include <signal.h>
#include "pcdefs.h"
#include "mbuf.h"
#include "netdef.h"
#include "protocol.h"
#include "in.h"
#include <malloc.h>
#include "net.h"
#include "config.h"

struct ebuf _far * _far Sgeteb();
int far configure(char far *, struct conf far *);
unsigned long far Sdomain();
struct tcpport tcpout;
u_char	buf[1024];
u_char	inbuf[1024];
int onint();
extern 

main()
{
	int	i, j;
	int	k, l;
	int	s;
	int	fd, fd1;
	int	wrote;
	struct conf far *cp;
	struct sockaddr saddr;
	struct ebuf _far *ep;
	unsigned long ret;
	int printed=0;

	fd = open("termcap", O_RDONLY|O_TEXT);
	if (fd < 0) {
		printf("error: opening termcap\n");
		exit(-1);
	}
	fd1 = open("got", O_CREAT|O_TRUNC|O_WRONLY|O_TEXT, 0666);
	if (fd1 < 0) {
		printf("error: opening got\n");
		exit(-1);
	}
	cp = SgetScon();
	if (configure((char far *)0, cp) < 0) {
		printf("ERROR: reading config.tel\n");
		exit (-1);
	}
	i = Snetinit();
	if (i < 0) {
		printf("Snetinit returns %d\n", i);
		exit (-1);
	}
	signal(SIGINT, onint);
#ifdef notdef
	printf("Packet info:\n");
	printf("    version %x\n", pk_info.packet_version);
	printf("    class %x\n", pk_info.packet_class);
	printf("    type %x\n", pk_info.packet_type);
	printf("    ifnumber %x\n", pk_info.packet_ifnumber);
	printf("    name %Fs\n", pk_info.packet_name);
	printf("    extended %x\n", pk_info.packet_extended);
	printf("ip handle = %x\n", ip_handle);
	printf("arp handle = %x\n", arp_handle);
#endif
	ep = Sgeteb();
	printf("ebaddr 0x%x, len = %d\n", (int)ep->start_addr, (int)ep->count);
#ifdef DOMAIN
	i = Sdominit();
	printf("Sdominit returns %d\n", i);

	ret = Sdomain("vax", 36);
	printf("Sdomain returns %lx\n", ret);
#endif

	s = Socket(SOCK_STREAM, &saddr, 0, (struct port far *)&tcpout);
	if (s < 0) {
		printf("send Socket returns %d\n", s);
		Snetshut();
		exit(-1);
	}
#ifdef notdef
	saddr.port = htons(L_CLICKS());
	if (SoBind(s, &saddr) < 0) {
		printf("send SoBind returns errord\n");
		Snetshut();
		exit(-1);
	}
	saddr.addr = 0x010101c0L;
	saddr.port = htons(2000);
	i = SoConnect(s, &saddr);
	if (i < 0) {
		printf("SoConnect returns %d\n", i);
		goto err;
	}
#endif
	saddr.port = htons(4000);
	if (SoBind(s, &saddr) < 0) {
		printf("send SoBind returns errord\n");
		Snetshut();
		exit(-1);
	}
	i = SoListen(s);
	printf("SoListen returns %d\n", i);
	if (i < 0) {
		Snetshut();
		exit(-1);
	}
	printf("Hit any key to abort: ");
	do {
		netsleep();
		i = SoAccept(s, &saddr);
		if (!i && kbhit()) {
			printf("\n");
			goto err;
		}
	} while (i == 0);
	printf("SoAccept returns %d\n", i);
	if (i < 0)
		goto err;
	printf("connected from 0x%lx port %d\n", saddr.addr, ntohs(saddr.port));
	while ((i = read(fd, buf, sizeof(buf))) > 0) {
		netsleep();
		k = SoRead(s, inbuf, sizeof(inbuf));
		if (!printed && k < 0) {
			printf("got EOF\n");
			printed++;
		}
		if (k > 0) {
			write(fd1, inbuf, k);
		}
		wrote = 0;
		do {
			netsleep();
			j = SoWrite(s, &buf[wrote], i - wrote);
			if (j < 0)
				goto leave;
			wrote += j;
			k = SoRead(s, inbuf, sizeof(inbuf));
			if (k > 0) {
				write(fd1, inbuf, k);
			}
		} while (wrote < i);
	}
leave:
	printf("test: calling SoClose\n");
	i = SoClose(s);
	printf("test: after leave Soclose returns %d\n", i);
	while ((k = SoRead(s, inbuf, sizeof(inbuf))) >= 0) {
		netsleep();
		if (k > 0) {
			write(fd1, inbuf, k);
		}
	}
	printf("done writing\n");
err:
	printf("hit any key to continue\n");
	while (!kbhit())
		netsleep();
	close(fd);
	close(fd1);
#ifdef DOMAIN
	Sdomshut();
#endif
	Snetshut();
	ep = Sgeteb();
	printf("ebaddr 0x%x, len = %d\n", (int)ep->start_addr, (int)ep->count);
}

onint()
{
	Snetshut();
	exit(1);
}
