/*******************************************************************************
        bsd.c
        Z-World, 2000

        A demonstration of several advanced TCP/IP functions, that are used
        for namespace (DNS) issues, and finding out information about the
        remote host.
*******************************************************************************/

/*********************************************************
 * Example of the use of various naming functions
 *
 * To use:
 *     Set the IP address / netmask above.
 *     Set the domainname / hostname above.
 *     Run, and when it prints your domain name,
 *       telnet to it from another host
 *     You should see information about the connection
 *       be printed out.
 **********************************************************/

/***********************************
 * Configuration                   *
 * -------------                   *
 * All fields in this section must *
 * be altered to match your local  *
 * network settings.               *
 ***********************************/

/*
 * You must change the following values to whatever
 * your local IP address and netmask are. Contact your
 * network administrator for these numbers.
 */
#define MY_IP_ADDRESS   "10.10.6.101"
#define MY_NETMASK      "255.255.255.0"

/*
 * Domain name configuration
 *
 * This sets your hostname and domainname. If your computer has an
 * internet address like: "test.zweng.com", your hostname
 * is "test", and your domainname is "zweng.com". 
 */
#define MY_HOST 	"test"
#define MY_DOMAIN	"zweng.com"

/********************************
 * End of configuration section *
 ********************************/

#memmap xmem
#use "dcrtcp.lib"

tcp_Socket tcpsock;
sock_type *const s = (sock_type *)&tcpsock;

char textbuf[64];
char *ip_to_text(longword ip)
{
	sprintf(textbuf,"%d.%d.%d.%d",
		(byte)(ip >> 24),
		(byte)(ip >> 16),
		(byte)(ip >> 8),
		(byte)(ip)
		);
	return textbuf;
}

main()
{
	int status;
	struct sockaddr sock_addr;
	
	sock_init();
	
	sethostname(MY_HOST);
	setdomainname(MY_DOMAIN);

	printf("We are %s.%s\n", gethostname(NULL, 0), getdomainname(NULL, 0));

	tcp_listen((tcp_Socket*)s,23,0,0,NULL,0);
	sock_wait_established(s,0,NULL,&status);

	printf("Receiving incomming connection\npsocket(s) -> ");
	psocket(s);

	printf("\n\n");
	if(0 == getpeername(s, &sock_addr, NULL)) {
		printf("getpeername: %s:%d",
			ip_to_text(sock_addr.s_ip),
			sock_addr.s_port
			);
	} else {
		printf("getpeername() retuned no data!\n");
	}

	printf("\n");
	
	if(0 == getsockname(s, &sock_addr, NULL)) {
		printf("getmyname: %s:%d",
			ip_to_text(sock_addr.s_ip),
			sock_addr.s_port
			);	
	} else {
		printf("getsockname() retuned no data!\n");
	}

	
	printf("\n\nClosing socket...\n");
	
	sock_close(s);
	while(tcp_tick(s));
		
sock_err:
	switch(status) {
		case 1: /* foreign host closed */
			printf("User closed session\n");
			break;
			
		case -1: /* timeout */
			printf("\nConnection timed out\n");
			break;
	}

	printf("Socket closed. All done!\n");
}
