home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / mscwattc / apps / ping.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-13  |  3.9 KB  |  149 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);
  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.     char tempbuffer[255];
  66.  
  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.         tcp_set_debug_state( 1 );
  78.     } else if ( !stricmp( argv[i], "-s") || !stricmp( argv[i],"/s"))
  79.         sequence_mode = 1;
  80.     else if ( !name )
  81.         name = argv[i];
  82.     else {
  83.         sequence_mode = 1;
  84.         itts = atol( argv[i] );
  85.     }
  86.     }
  87.     if (!name)
  88.     help();
  89.  
  90.     if (!(host = resolve( name ))) {
  91.     printf("Unable to resolve '%s'\n", name );
  92.     exit( 3 );
  93.     }
  94.     if ( isaddr( name ))
  95.     printf("Pinging [%s]",inet_ntoa(tempbuffer, host));
  96.     else
  97.     printf("Pinging '%s' [%s]",name, inet_ntoa(tempbuffer, host));
  98.  
  99.     if (itts) printf(" %u times", itts);
  100.     else
  101.     itts = sequence_mode ? 0xffffffffL : 1;
  102.  
  103.     if (sequence_mode) printf(" once per_second");
  104.     printf("\n");
  105.  
  106.     if (!sequence_mode) tot_timeout = set_timeout( (int)itts + 10 );
  107.  
  108.     _arp_resolve( host , NULL );   /* resolution it before timer starts */
  109.  
  110.     do {
  111.     /* once per second - do all this */
  112.     if ( chk_timeout( send_timeout ) || !send_timeout ) {
  113.         send_timeout = set_timeout( 1 );
  114.         if ( chk_timeout( tot_timeout ) && tot_timeout )
  115.         stats();
  116.         if ( sent < itts ) {
  117.         sent++;
  118.         if (_ping( host , sent ))
  119.             stats();
  120.         if (!is_new_line) printf("\n");
  121.         printf("sent PING # %lu ", sent );
  122.         is_new_line = 0;
  123.         }
  124.     }
  125.  
  126.     if ( kbhit() ) {
  127.         getch();        /* trash the character */
  128.         stats();
  129.     }
  130.  
  131.     tcp_tick(NULL);
  132.     if ((timer = _chk_ping( host , &new_rcvd)) != 0xffffffffL) {
  133.         tot_delays += timer;
  134.         ++received;
  135.         if ( new_rcvd != last_rcvd + 1 ) {
  136.         if (!is_new_line) printf("\n");
  137.         puts("PING receipt received out of order!");
  138.         is_new_line = 1;
  139.         }
  140.         last_rcvd = new_rcvd;
  141.         if (!is_new_line) printf(", ");
  142.         printf("PING receipt # %lu : response time %lu.%02lu seconds\n", received, timer / 18L, ((timer %18L)*55)/10 );
  143.         is_new_line = 1;
  144.         if ( received == itts )
  145.         stats();
  146.     }
  147.     } while (1);
  148. }
  149.