home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / wattcp / apps / ping.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  4.1 KB  |  154 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.     sock_init();
  69.  
  70.     if ( argc < 2 )
  71.     help();
  72.  
  73.     name = NULL;
  74.     for ( i = 1; i < argc ; ++i ) {
  75.     if ( !stricmp( argv[i], "-d") || !stricmp( argv[i],"/d")) {
  76.         puts("Debug mode activated");
  77.             debug = 1;
  78.         tcp_set_debug_state( 1 );
  79.     } else if ( !stricmp( argv[i], "-s") || !stricmp( argv[i],"/s"))
  80.         sequence_mode = 1;
  81.     else if ( !name )
  82.         name = argv[i];
  83.     else {
  84.         sequence_mode = 1;
  85.         itts = atol( argv[i] );
  86.     }
  87.     }
  88.     if (!name)
  89.     help();
  90.  
  91.     if (!(host = resolve( name ))) {
  92.     printf("Unable to resolve '%s'\n", name );
  93.     exit( 3 );
  94.     }
  95.     if ( isaddr( name ))
  96.     printf("Pinging [%s]",inet_ntoa(tempbuffer, host));
  97.     else
  98.     printf("Pinging '%s' [%s]",name, inet_ntoa(tempbuffer, host));
  99.  
  100.     if (itts) printf(" %u times", itts);
  101.     else
  102.     itts = sequence_mode ? 0xffffffffL : 1;
  103.  
  104.     if (sequence_mode) printf(" once per_second");
  105.     printf("\n");
  106.  
  107.     tot_timeout = set_timeout( itts + 10 );
  108.  
  109.     _arp_resolve( host , tempbuffer, 0 );   /* resolution it before timer starts */
  110.     if ( debug ) printf("ETH -> %x %x %x %x %x %x\n",
  111.         tempbuffer[0],tempbuffer[1],tempbuffer[2],tempbuffer[3],
  112.         tempbuffer[4],tempbuffer[5]);
  113.  
  114.  
  115.     do {
  116.     /* once per second - do all this */
  117.     if ( chk_timeout( send_timeout ) || !send_timeout ) {
  118.         send_timeout = set_timeout( 1 );
  119.         if ( chk_timeout( tot_timeout ) && tot_timeout )
  120.         stats();
  121.         if ( sent < itts ) {
  122.         sent++;
  123.         if (_ping( host , sent ))
  124.             stats();
  125.         if (!is_new_line) printf("\n");
  126.         printf("sent PING # %lu ", sent );
  127.         is_new_line = 0;
  128.         }
  129.     }
  130.  
  131.     if ( kbhit() ) {
  132.         getch();        /* trash the character */
  133.         stats();
  134.     }
  135.  
  136.     tcp_tick(NULL);
  137.     if ((timer = _chk_ping( host , &new_rcvd)) != 0xffffffffL) {
  138.         tot_delays += timer;
  139.         ++received;
  140.         if ( new_rcvd != last_rcvd + 1 ) {
  141.         if (!is_new_line) printf("\n");
  142.         puts("PING receipt received out of order!");
  143.         is_new_line = 1;
  144.         }
  145.         last_rcvd = new_rcvd;
  146.         if (!is_new_line) printf(", ");
  147.         printf("PING receipt # %lu : response time %lu.%02lu seconds\n", received, timer / 18L, ((timer %18L)*55)/10 );
  148.         is_new_line = 1;
  149.         if ( received == itts )
  150.         stats();
  151.     }
  152.     } while (1);
  153. }
  154.