home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / PING.C < prev    next >
C/C++ Source or Header  |  1992-04-28  |  4KB  |  155 lines

  1.  
  2. /******************************************************************************
  3.     PING - internet diagnostic tool
  4.     Copyright (C) 1991, University of Waterloo
  5.     portions Copyright (C) 1990, National Center for Supercomputer Applications
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it, but you may not sell it.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but without any warranty; without even the implied warranty of
  12.     merchantability or fitness for a particular purpose.
  13.  
  14.         Erick Engelke                   or via E-Mail
  15.         Faculty of Engineering
  16.         University of Waterloo          Erick@development.watstar.uwaterloo.ca
  17.         200 University Ave.,
  18.         Waterloo, Ont., Canada
  19.         N2L 3G1
  20.  
  21. ******************************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include <tcp.h>
  25.  
  26. extern longword set_timeout();
  27.  
  28. longword sent = 0L;
  29. longword received = 0L;
  30. longword tot_delays = 0L;
  31. longword last_rcvd = 0L;
  32. char *name;
  33.  
  34. stats()
  35. {
  36.     longword temp;
  37.  
  38.     puts("\nPing Statistics");
  39.     printf("Sent        : %lu \n", sent );
  40.     printf("Received    : %lu \n", received );
  41.     if (sent)
  42.     printf("Success     : %lu \%\n", (100L*received)/sent);
  43.     if (!received)
  44.     printf("There was no response from %s\n", name );
  45.     else {
  46.         temp = ( tot_delays * 2813L)/(512L*received + 1);
  47.     printf("Average RTT : %lu.%02lu seconds\n", temp / 100L, temp % 100L);
  48.     }
  49.     exit( received ? 0 : 1 );
  50. }
  51.  
  52. help()
  53. {
  54.     puts("PING [-s|/s] [-d|/d] hostname [number]");
  55.     exit( 3 );
  56. }
  57.  
  58.  
  59. main(int argc, char **argv)
  60. {
  61.     longword host, timer, new_rcvd;
  62.     longword tot_timeout = 0L, itts = 0L, send_timeout = 0L;
  63.     word i;
  64.     word sequence_mode = 0, is_new_line = 1;
  65.     word debug = 0;
  66.     unsigned char tempbuffer[255];
  67.  
  68.     dbuginit();
  69.     sock_init();
  70.  
  71.     if ( argc < 2 )
  72.     help();
  73.  
  74.     name = NULL;
  75.     for ( i = 1; i < argc ; ++i ) {
  76.     if ( !stricmp( argv[i], "-d") || !stricmp( argv[i],"/d")) {
  77.         puts("Debug mode activated");
  78.             debug = 1;
  79.         tcp_set_debug_state( 1 );
  80.     } else if ( !stricmp( argv[i], "-s") || !stricmp( argv[i],"/s"))
  81.         sequence_mode = 1;
  82.     else if ( !name )
  83.         name = argv[i];
  84.     else {
  85.         sequence_mode = 1;
  86.         itts = atol( argv[i] );
  87.     }
  88.     }
  89.     if (!name)
  90.     help();
  91.  
  92.     if (!(host = resolve( name ))) {
  93.     printf("Unable to resolve '%s'\n", name );
  94.     exit( 3 );
  95.     }
  96.     if ( isaddr( name ))
  97.     printf("Pinging [%s]",inet_ntoa(tempbuffer, host));
  98.     else
  99.     printf("Pinging '%s' [%s]",name, inet_ntoa(tempbuffer, host));
  100.  
  101.     if (itts) printf(" %u times", itts);
  102.     else
  103.     itts = sequence_mode ? 0xffffffffL : 1;
  104.  
  105.     if (sequence_mode) printf(" once per_second");
  106.     printf("\n");
  107.  
  108.     if (!sequence_mode) tot_timeout = set_timeout( itts + 10 );
  109.  
  110.     _arp_resolve( host , tempbuffer );   /* resolution it before timer starts */
  111.     if ( debug ) printf("ETH -> %x %x %x %x %x %x\n",
  112.         tempbuffer[0],tempbuffer[1],tempbuffer[2],tempbuffer[3],
  113.         tempbuffer[4],tempbuffer[5]);
  114.  
  115.  
  116.     do {
  117.     /* once per second - do all this */
  118.     if ( chk_timeout( send_timeout ) || !send_timeout ) {
  119.         send_timeout = set_timeout( 1 );
  120.         if ( chk_timeout( tot_timeout ) && tot_timeout )
  121.         stats();
  122.         if ( sent < itts ) {
  123.         sent++;
  124.         if (_ping( host , sent ))
  125.             stats();
  126.         if (!is_new_line) printf("\n");
  127.         printf("sent PING # %lu ", sent );
  128.         is_new_line = 0;
  129.         }
  130.     }
  131.  
  132.     if ( kbhit() ) {
  133.         getch();        /* trash the character */
  134.         stats();
  135.     }
  136.  
  137.     tcp_tick(NULL);
  138.     if ((timer = _chk_ping( host , &new_rcvd)) != 0xffffffffL) {
  139.         tot_delays += timer;
  140.         ++received;
  141.         if ( new_rcvd != last_rcvd + 1 ) {
  142.         if (!is_new_line) printf("\n");
  143.         puts("PING receipt received out of order!");
  144.         is_new_line = 1;
  145.         }
  146.         last_rcvd = new_rcvd;
  147.         if (!is_new_line) printf(", ");
  148.         printf("PING receipt # %lu : response time %lu.%02lu seconds\n", received, timer / 18L, ((timer %18L)*55)/10 );
  149.         is_new_line = 1;
  150.         if ( received == itts )
  151.         stats();
  152.     }
  153.     } while (1);
  154. }
  155.