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

        A sample program that will send out a series of 'pings' to another
        computer on the network.
*******************************************************************************/

/* Portions used with permission of Erick Engelke. */

/***********************************
 * 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, netmask, gateway, and nameserver
 * are. Contact your network administrator
 * for these numbers.
 */
#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.19"

/*
 * PING configuration
 *
 * This is the address of the computer
 * that we should send echo-requests to.
 */
#define PINGWHO	"10.10.6.19"

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


#memmap xmem
#use "dcrtcp.lib"

longword sent;
longword received;
longword tot_delays;
longword last_rcvd;
char *name;

void stats(void)
{
   longword temp;

   puts("\nPing Statistics");
   printf("Sent        : %lu \n", sent );
   printf("Received    : %lu \n", received );
   if (sent)
      printf("Success     : %lu%%\n", (100L*received)/sent);
   if (!received)
      printf("There was no response from %s\n", name );
   else {
      temp =  (tot_delays*100)/received;
      printf("Average RTT : %lu.%02lu msec\n", temp / 100L, temp % 100L);
   }
   exit( received ? 0 : 1 );
}

debug
int main()
{
   longword host, timer, new_rcvd;
   longword tot_timeout, itts, send_timeout;
   word i;
   word sequence_mode, is_new_line;
   word debug_on;
   static unsigned char tempbuffer[255];

   sent=received=tot_delays=last_rcvd=0L;
   tot_timeout=itts=send_timeout=0L;
   sequence_mode=debug_on=0;
   is_new_line=1;

   sock_init();

   name=PINGWHO;
   itts=10;

   if (!(host = resolve( name ))) {
      printf("Unable to resolve '%s'\n", name );
      exit( 3 );
   }
   if ( isaddr( name ))
      printf("Pinging [%s]",inet_ntoa(tempbuffer, host));
   else
      printf("Pinging '%s' [%s]",name, inet_ntoa(tempbuffer, host));

   if (itts) printf(" %u times", itts);
   else
      itts = sequence_mode ? 0xffffffffL : 1;

   if (sequence_mode) printf(" once per_second");
   printf("\n");

   tot_timeout = set_timeout( (int)(itts + 10) );

   _arp_resolve( host, (eth_address *)tempbuffer, 0 );   /* resolution it before timer starts */
   if ( debug_on ) printf("ETH -> %x %x %x %x %x %x\n",
                       tempbuffer[0],tempbuffer[1],tempbuffer[2],tempbuffer[3],
                       tempbuffer[4],tempbuffer[5]);


   do {
      /* once per second - do all this */
      if ( chk_timeout( send_timeout ) || !send_timeout ) {
         send_timeout = set_timeout( 1 );
         if ( chk_timeout( tot_timeout ) && tot_timeout )
            stats();
         if ( sent < itts ) {
            sent++;
            if (_ping( host , sent ))
               stats();
            if (!is_new_line) printf("\n");
            printf("sent PING # %lu ", sent );
            is_new_line = 0;
         }
      }

      if ( kbhit() ) {
         getchar();    /* trash the character */
         stats();
      }

      tcp_tick(NULL);
      if ((timer = _chk_ping( host , &new_rcvd)) != 0xffffffffL) {
         tot_delays += timer;
         ++received;
         if ( new_rcvd != last_rcvd + 1 ) {
            if (!is_new_line) printf("\n");
            puts("PING receipt received out of order!");
            is_new_line = 1;
         }
         last_rcvd = new_rcvd;
         if (!is_new_line) printf(", ");
         printf("PING receipt # %lu : response time %lu ms\n", received, timer);
         is_new_line = 1;
         if ( received == itts )
            stats();
      }
   } while (1);
}