home *** CD-ROM | disk | FTP | other *** search
/ Supremacy 1 / Supremacy-1.iso / UTILS / S-T / STING.ZIP / STING / TOOLS / PING.C next >
C/C++ Source or Header  |  1997-05-03  |  6KB  |  229 lines

  1.  
  2. /*********************************************************************/
  3. /*                                                                   */
  4. /*     STinG : Ping Network Tool                                     */
  5. /*                                                                   */
  6. /*                                                                   */
  7. /*      Version 1.0                        from 16. Januar 1997      */
  8. /*                                                                   */
  9. /*********************************************************************/
  10.  
  11.  
  12. #include <aes.h>
  13. #include <tos.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. #include "transprt.h"
  19. #include "ping.h"
  20.  
  21.  
  22. #define  ICMP_ECHO_REPLY      0
  23. #define  ICMP_ECHO            8
  24.  
  25.  
  26. void          gem_program (void);
  27. long          get_sting_cookie (void);
  28. void          do_some_work (void);
  29. void          fetch_parameters (void);
  30. void          send_echo (void);
  31. int16  cdecl  receive_echo (IP_DGRAM *datagram);
  32. long          fetch_clock (void);
  33.  
  34.  
  35. typedef int  BOOLEAN;
  36.  
  37. DRV_LIST  *sting_drivers;
  38. TPL       *tpl;
  39. uint16    sent, received, min, ave, max, num_packets;
  40. uint32    host;
  41. char      alert[200];
  42. char  not_there[] = "[1][ |  STinG is not loaded or enabled !   ][ Hmmm ]";
  43. char  corrupted[] = "[1][ |  STinG structures corrupted !   ][ Oooops ]";
  44. char  found_it[]  = "[3][ |  Driver \'%s\',|  by %s, found,   |  version %s.][ Okay ]";
  45. char  no_module[] = "[1][ |  STinG Transport Driver not found !   ][ Grmbl ]";
  46. char  takes[]     = "[3][ |  This will take a little more   | |    than %d seconds.][ Okay ]";
  47. char  first[]     = "[3][  Ping Actions :| |    %u packets sent, %u received;   |    %ld %% lost.][ Okay ]";
  48. char  second[]    = "[3][  Ping Time Statistics :   | |    Minimum %5ld ms|    Average %5ld ms|    Maximum %5ld ms][ Okay ]";
  49.  
  50.  
  51.  
  52. void  main()
  53.  
  54. {
  55.    appl_init();
  56.  
  57.    if (rsrc_load ("PING.RSC") == 0)
  58.         form_alert (1, "[1][ No RSC File !  ][ Hmpf ]");
  59.      else
  60.         gem_program();
  61.  
  62.    appl_exit();
  63.  }
  64.  
  65.  
  66. void  gem_program()
  67.  
  68. {
  69.    sting_drivers = (DRV_LIST *) Supexec (get_sting_cookie);
  70.  
  71.    if (sting_drivers == 0L) {
  72.         form_alert (1, not_there);
  73.         return;
  74.       }
  75.    if (strcmp (sting_drivers->magic, MAGIC) != 0) {
  76.         form_alert (1, corrupted);
  77.         return;
  78.       }
  79.  
  80.    tpl = (TPL *) (*sting_drivers->get_dftab) (TRANSPORT_DRIVER);
  81.  
  82.    if (tpl != (TPL *) NULL) {
  83.         sprintf (alert, found_it, tpl->module, tpl->author, tpl->version);
  84.         form_alert (1, alert);
  85.         do_some_work();
  86.       }
  87.      else
  88.         form_alert (1, no_module);
  89.  }
  90.  
  91.  
  92. long  get_sting_cookie()
  93.  
  94. {
  95.    long  *work;
  96.  
  97.    for (work = * (long **) 0x5a0L; *work != 0L; work += 2)
  98.         if (*work == 'STiK')
  99.              return (*++work);
  100.  
  101.    return (0L);
  102.  }
  103.  
  104.  
  105. void  do_some_work()
  106.  
  107. {
  108.    int  count;
  109.  
  110.    fetch_parameters();
  111.  
  112.    if (host == 0 || num_packets == 0)
  113.         return;
  114.  
  115.    sprintf (alert, takes, (num_packets + 1) / 10);
  116.    form_alert (1, alert);
  117.  
  118.    sent = received = 0;
  119.    min = 50000u;   ave = max = 0;
  120.  
  121.    if (! ICMP_handler (receive_echo, HNDLR_SET))
  122.         return;
  123.  
  124.    for (count = 0; count < num_packets; count++) {
  125.         send_echo();
  126.         evnt_timer (100, 0);
  127.       }
  128.  
  129.    for (count = 0; count < 50 && received != sent; count++)
  130.         evnt_timer (200, 0);
  131.  
  132.    ICMP_handler (receive_echo, HNDLR_REMOVE);
  133.  
  134.    sprintf (alert, first, sent, received, (sent - received) * 100L / sent);
  135.    form_alert (1, alert);
  136.  
  137.    if (received) {
  138.         sprintf (alert, second, min * 5L, ave * 5L / received, max * 5L);
  139.         form_alert (1, alert);
  140.       }
  141.  }
  142.  
  143.  
  144. void  fetch_parameters()
  145.  
  146. {
  147.    OBJECT  *tree;
  148.    char    *txt;
  149.    int     x, y, w, h, c_x, c_y;
  150.  
  151.    rsrc_gaddr (R_TREE, PING, &tree);
  152.  
  153.    wind_update (BEG_UPDATE);
  154.  
  155.    form_center (tree, &x, &y, &w, &h);
  156.    c_x = x + w / 2;   c_y = y + h / 2;
  157.    form_dial (FMD_START, c_x, c_y, 0, 0, x, y, w, h);
  158.    form_dial (FMD_GROW,  c_x, c_y, 0, 0, x, y, w, h);
  159.  
  160.    strcpy (tree[HOST].ob_spec.tedinfo->te_ptext, "127  0  0  1");
  161.    strcpy (tree[NUM ].ob_spec.tedinfo->te_ptext, "50");
  162.  
  163.    objc_draw (tree, ROOT, MAX_DEPTH, x, y, w, h);
  164.    form_do (tree, HOST);
  165.  
  166.    form_dial (FMD_SHRINK, c_x, c_y, 0, 0, x, y, w, h);
  167.    form_dial (FMD_FINISH, c_x, c_y, 0, 0, x, y, w, h);
  168.  
  169.    wind_update (END_UPDATE);
  170.  
  171.    num_packets = atoi (tree[NUM].ob_spec.tedinfo->te_ptext);
  172.    txt = tree[HOST].ob_spec.tedinfo->te_ptext;
  173.    txt[12] = '\0';   h = atoi (&txt[9]);
  174.    txt[ 9] = '\0';   w = atoi (&txt[6]);
  175.    txt[ 6] = '\0';   y = atoi (&txt[3]);
  176.    txt[ 3] = '\0';   x = atoi (&txt[0]);
  177.    host = ((uint32) x << 24) | ((uint32) y << 16) | ((uint32) w << 8) | (uint32) h;
  178.  }
  179.  
  180.  
  181. void  send_echo()
  182.  
  183. {
  184.            uint16  buffer[16];
  185.    static  uint16  sequence = 0;
  186.  
  187.    buffer[0] = 0xaffeu;   buffer[1] = sequence++;
  188.    * (long *) &buffer[2] = Supexec (fetch_clock);
  189.  
  190.    buffer[4] = 0xa5a5u;   buffer[5] = 0x5a5au;
  191.    buffer[6] = 0x0f0fu;   buffer[7] = 0xf0f0u;
  192.  
  193.    ICMP_send (host, ICMP_ECHO, 0, buffer, 32);
  194.  
  195.    sent++;
  196.  }
  197.  
  198.  
  199. int16  cdecl  receive_echo (datagram)
  200.  
  201. IP_DGRAM  *datagram;
  202.  
  203. {
  204.    uint16  *data, delay;
  205.  
  206.    data = datagram->pkt_data;
  207.  
  208.    if (data[0] != (ICMP_ECHO_REPLY << 8) || data[2] != 0xaffeu)
  209.         return (FALSE);
  210.  
  211.    received++;
  212.    delay = (uint16) (fetch_clock() - * (long *) &data[4]);
  213.  
  214.    if (min > delay)   min = delay;
  215.    if (max < delay)   max = delay;
  216.    ave += delay;
  217.  
  218.    ICMP_discard (datagram);
  219.  
  220.    return (TRUE);
  221.  }
  222.  
  223.  
  224. long  fetch_clock()
  225.  
  226. {
  227.    return (* (long *) 0x4baL);
  228.  }
  229.