#include <conio.h>
#include "pcdefs.h"
#include "netdef.h"
#include "protocol.h"
#include "in.h"
#include <malloc.h>
#include "net.h"

int ipdump(struct ip far *);
struct udpport udpout;
struct tcpport tcpout;

main()
{
	int	i, j;
	int	s;
	int	t;
	u_long	fromaddr;
	u_int	fromport;
	u_char	buf[1024];
	struct conf far *cp;
	struct sockaddr saddr;

	strcpy(buf, "This is a test of what we can send out.\n");
	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);
	}
	saddr.addr = 0x010101c0L;
	saddr.port = htons(1000);
	s = Socket(SOCK_DGRAM, &saddr, 0, (struct port far *)&udpout);
	if (s < 0) {
		printf("send Socket returns %d\n", s);
		Snetshut();
		exit(-1);
	}
	printf("send socket is %d\n", s);
	saddr.port = htons(4000);
	if (SoBind(s, &saddr) < 0) {
		printf("send SoBind returns errord\n");
		Snetshut();
		exit(-1);
	}
	printf("before SoSend\n");
	saddr.port = htons(2000);
	saddr.addr = 0x010101c0L;
	for (i = 0; i < 10; i++) {
		if ((j = SoSend(s, &saddr, (u_char far *)buf, strlen(buf))) < 0)
			printf("Sosend: error returns %d\n", j);
		else
			printf("Sosend returns %d\n", j);
	}
	printf("done send\n");
	(void) memset(buf, 0, sizeof(buf));
	for (;;) {
	   if ((j = SoReceive(s, &saddr, buf, sizeof(buf))) > 0) {
		write(1, buf, j);
		(void) memset(buf, 0, sizeof(buf));
	   }
		if (j == 1)
			break;
	}
	printf("done\n");
	while(!kbhit())
		;
	Snetshut();
}

int
ipdump(p)
struct ip far *p;
{
	u_int iplen,iid;
	u_char	*cp;

	cp = p->d.dest;
	printf("dest = %x:%x:%x:%x:%x:%x\n", *cp, *(cp+1), *(cp+2), *(cp+3), *(cp+4), *(cp+5));
	cp = p->d.me;
	printf("src = %x:%x:%x:%x:%x:%x\n", *cp, *(cp+1), *(cp+2), *(cp+3), *(cp+4), *(cp+5));
	printf("type = %x\n", ntohs(p->d.type));
	iid = ntohs(p->i.ident);
	iplen = ntohs(p->i.tlen);
	printf("Version+hdr: %x     service %d      tlen %u",
			p->i.versionandhdrlen,p->i.service,iplen);
	printf("Ident: %u    frags: %4x    ttl: %d    prot: %d",
			iid, p->i.frags, p->i.ttl, p->i.protocol);
	cp = (u_char far *)&p->i.ipsource;
	printf("addresses: s: %d.%d.%d.%d ", *cp, *(cp+1), *(cp+2), *(cp+3));
	cp = (u_char far *)&p->i.ipdest;
	printf("    t: %d.%d.%d.%d\n", *cp, *(cp+1), *(cp+2), *(cp+3));
	printf("ipcheck = %x\n", ntohs(p->i.check));
}
