home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / PCPING.C < prev    next >
C/C++ Source or Header  |  1991-08-13  |  2KB  |  77 lines

  1. #include <copyright.h>
  2. #include <wattcp.h>
  3.  
  4. typedef struct icmp_echo {
  5.     byte    type;
  6.     byte    code;
  7.     word    checksum;
  8.     word    identifier;
  9.     word    sequence;
  10.     longword    index;
  11. };
  12.  
  13. typedef struct _pkt {
  14.     in_Header     in;
  15.     struct icmp_echo icmp;
  16.     in_Header    data;
  17. };
  18.  
  19.  
  20.  
  21. int _ping( longword host, longword countnum )
  22. {
  23.     eth_address dest;
  24.     struct _pkt *p;
  25.     in_Header *ip;
  26.     struct icmp_echo *icmp;
  27.     static word icmp_id = 0;
  28.  
  29.     if ((host & 0xff) == 0xff ) {
  30.     outs( "Cannot ping a network!\n\r");
  31.     return( -1 );
  32.     }
  33.     if ( ! _arp_resolve( host, &dest )) {
  34.     outs( "Cannot resolve host's hardware address\n\r");
  35.     return( -1 );
  36.     }
  37.  
  38.     if (debug_on) {
  39.     outs("\n\rDEBUG: destination hardware :");
  40.     outhexes( &dest, 6 );
  41.     outs("\n\r");
  42.     }
  43.     p = (struct _pkt*)_eth_formatpacket( &dest, 8 );
  44.  
  45.     ip = &p->in;
  46.     icmp = &p->icmp;
  47.  
  48.     icmp->type = 8;
  49.     icmp->code = 0;
  50.     icmp->index = countnum;
  51.     *(longword *)(&icmp->identifier) = set_timeout( 1 );
  52. /*
  53.     icmp->identifier = ++icmp_id;
  54.     icmp->sequence = icmp_id;
  55. */
  56.     /* finish the icmp checksum portion */
  57.     icmp->checksum = 0;
  58.     icmp->checksum = ~checksum( icmp, sizeof( struct icmp_echo));
  59.  
  60.     /* encapsulate into a nice ip packet */
  61.     ip->ver = 4;
  62.     ip->hdrlen = 5;
  63.     ip->length = intel16( sizeof( in_Header ) + sizeof( struct icmp_echo));
  64.     ip->tos = 0;
  65.     ip->identification = intel16( icmp_id ++);    /* not using ip id */
  66.     ip->frag = 0;
  67.     ip->ttl = 250;
  68.     ip->proto = ICMP_PROTO;
  69.     ip->checksum = 0;
  70.     ip->source = intel( my_ip_addr );
  71.     ip->destination = intel( host );
  72.     ip->checksum = ~ checksum( ip, sizeof( in_Header ));
  73.  
  74.     return( _eth_send( intel16( ip->length )));
  75. }
  76.  
  77.