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

        ICMP demonstration, by pinging a remote host.
*******************************************************************************/

#define MY_IP_ADDRESS 	"10.10.6.101"
#define MY_NETMASK		"255.255.255.0"
#define MY_GATEWAY		"10.10.6.19"
#define MY_NAMESERVER	"10.10.6.1"

#define PING_WHO			"10.10.6.19"

#memmap xmem
#use "dcrtcp.lib"

main()
{
	longword seq,ping_who,tmp_seq,time_out;

	seq=0;
	
	sock_init();
	
	/*
	 *		Get the binary ip address for the target of our 
	 *		pinging.
	 */
	
	ping_who=resolve(PING_WHO);
	if(ping_who==0) {
		printf("unable to resolve %s",PING_WHO);
		return 1;
	}
	
	for(;;) {
		/*
		 *		It is important to call tcp_tick here because
		 *		ping packets will not get processed otherwise.
		 *
		 */

		tcp_tick(NULL);
		
		/*
		 *		Send one ping per second.
		 */
		
		costate {
			waitfor(DelaySec(1));
			_ping(ping_who,seq++);
		}
		
		/*
		 *		Has a ping come in?  time_out!=0xfffffff->yes.
		 */

		time_out=_chk_ping(ping_who,&tmp_seq);
		if(time_out!=0xffffffff)
			printf("received ping:  %ld\n", tmp_seq);
	}
}