home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / UNZIPPED / MSWATTCP / SRC / PCPING.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-24  |  1.8 KB  |  86 lines

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