home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC20A01.ZIP / PING.CPP < prev    next >
C/C++ Source or Header  |  1999-06-20  |  5KB  |  176 lines

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