From: Chuck Meade <chuckm@verdix.com>
Newsgroups: comp.os.vxworks
Subject: re: ping command available?
Date: Fri, 22 Jan 93 10:03:11 +0000
Organization: Lawrence Berkeley Laboratory, Berkeley CA
NNTP-Posting-Host: 128.3.112.16
Originator: daemon@vxw.ee.lbl.gov


> Does anyone have a ping program, similar to Sun's, available under vxWorks?


I put together a VXworks based ping() routine a while ago.  Supply the host to
ping as the first parameter and the number of packets to send as the second.

********** defs.h ************************

#include "vxWorks.h"
#include "errno.h"
#include "socket.h"
#include "in_systm.h"
#include "in.h"
#include "ip.h"
#include "ip_icmp.h"
#include "netdb.h" 
#include "wdLib.h"
#include "usrLib.h"
#include "sigLib.h"
#include "taskLib.h"

*********** c-support.c **********************

#include "defs.h"

#define OK                 0
#define Bad_Host           1
#define Bad_Protocol       2
#define Cant_Create_Socket 3

#define From_Our_Request     0
#define Not_From_Our_Request 1


struct sockaddr_in  dest;
int                 sockfd;
char                *hostname ;
struct protoent     *proto ;
u_char              recvpack[4096] ;
int                 num_received = 0 ;



char configure_socket(the_hostname)
   char *the_hostname ;
{
   dest.sin_family = AF_INET ;
   dest.sin_addr.s_addr = hostGetByName(the_hostname) ;
   if (dest.sin_addr.s_addr == ERROR) 
   {
      printf("bad host!\n") ;
      return(Bad_Host) ;
   }
   strcpy(hostname,the_hostname) ;

   sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP) ;
   if (sockfd < 0)
   {
      printf("can't create socket\n") ;
      return (Cant_Create_Socket) ;
   }
   else
   {
      return (OK) ;
   }
}



int in_cksm(msg_ptr)
   register u_short  *msg_ptr ;
{
   register long    sum ;
   register u_short answer ;
   int              index ;

   sum = 0 ;

   for (index = 0 ; index < 4 ; index++)
   {
      sum = sum + *msg_ptr ;
      msg_ptr++ ;
   }

   sum = (sum >> 16) + (sum & 0xffff) ;
   sum = sum + (sum >> 16) ;
   answer = ~sum ;
   return (answer) ;
}


int message_number = 1 ;

void construct(msg)
   struct icmp  *msg ;
{
   u_char *byte_ptr ;

   byte_ptr = (u_char *) msg ;

   msg->icmp_type = ICMP_ECHO ;
   msg->icmp_code = 0 ;
   msg->icmp_cksum = 0 ;
   msg->icmp_id = 0x1234 ;
   msg->icmp_seq = message_number++ ;
   msg->icmp_data[0] = 0 ;
   msg->icmp_cksum = in_cksm(msg) ;
}


void xmt_msg(msg)
   struct icmp *msg ;
{
   int i ;
   int packsize ;

   packsize = 8 ;

   i = sendto(sockfd, (caddr_t) msg, packsize, 0, (struct sockaddr *) &dest, sizeof(dest)) ;

   if (i < 0)
   {
      printf("sendto error\n") ;
   }
   else if (i != packsize)
   {
      printf("wrote %s %d bytes, return = %d\n", hostname, packsize, i) ;
   }
}



void receive_pings()
{
   int                 n ;
   int                 fromlen ;
   struct sockaddr_in  from ;
   int                 iphdrlen ;
   struct ip           *ip ;
   struct icmp         *icp ;
   int                 index ;
   extern int          original_num_packets ;


   num_received = 0 ;

   while (1)  /* loop until we break out */
   {
      fromlen = sizeof(from) ;

      n = recvfrom(sockfd, recvpack, sizeof(recvpack), 0, (struct sockaddr *) &from, &fromlen) ;

      if (n < 0)
      {
         if (errno != EINTR)
         {
            printf("recvfrom error\n") ;
         }
         else
         {
            printf("recvfrom interrupted (this is OK)\n") ;
            ;
         }
      }
      else
      {
         ip = (struct ip *) recvpack ;
         iphdrlen = ip->ip_hl << 2 ;
         icp = (struct icmp *) (recvpack + iphdrlen) ;

         if ((icp->icmp_type == ICMP_ECHOREPLY) && (icp->icmp_id == 0x1234))
         {
            num_received++ ;

            if (num_received == original_num_packets)
            {
               exit() ;
            }
         }
      }
   }
}



************** echo-transmitter.c *************************

#include "defs.h"


void sig_alarm() ;
void clean_up() ;
void watchdog_routine() ;


static WDOG_ID wid ;
int  the_tid ;
struct icmp  the_message ;


void transmit_echo_message()
{
   construct(&the_message) ;
   xmt_msg(&the_message) ;
   return ;
} 



void dummy_alarm_handler()
{
   ;
}


SIGVEC handler_info = {dummy_alarm_handler,0,0} ;
SIGVEC save_handler_info ;


void start_pinging()
{
   extern int get_num_packets() ;
   extern void set_num_packets() ;

   sigInit() ;
   sigvec(SIGALRM, &handler_info, &save_handler_info) ;

   the_tid = taskIdSelf() ;
   wid = wdCreate() ;  /* watchdog */
   wdStart(wid, sysClkRateGet(), watchdog_routine, 0) ;;

   while (1)
   {
      if (get_num_packets() > 0)
      {
         set_num_packets(get_num_packets() - 1) ;
         transmit_echo_message() ;
      }
      else
      {
         break ;
      }

      pause() ;
   }

   pause() ;
   wdDelete(wid) ;
   clean_up() ;
   exit() ;
}



void watchdog_routine()
{
   kill(the_tid,SIGALRM) ;
   wdStart(wid, sysClkRateGet(), watchdog_routine, 0) ;
}


void clean_up()
{
   extern int original_num_packets ;
   extern int num_received ;
   extern char *hostname ;

   if (num_received == original_num_packets)
   {
      printf("host %s is alive\n", hostname) ;
   }
   else
   {
      printf("response trouble with host %s:  sent %d packet%c, received %d packet%c\n",
             hostname, original_num_packets, ((original_num_packets != 1) ? 's' : ' '),
             num_received, ((num_received != 1) ? 's' : ' ') ) ;
   }

   return ;
}


*********** ping-stats.c *******************************

#include "defs.h"


static int num_packets ;

void set_num_packets(to)
   int to ;
{
   num_packets = to ;
}

int get_num_packets()
{
   return (num_packets) ;
}


*********** ping.c ****************************************

#include "defs.h"


int original_num_packets ;

int ping(arg1, arg2)
   char *arg1 ;
   int arg2 ;
{
   extern int configure_socket() ;
   extern void start_pinging() ;
   extern void receive_pings() ;
   int num_packs ;
   int status ;

   /* get user's parameters */

   if (arg1 != NULL) 
   {
      if (arg2 > 1) 
      {
         num_packs = arg2 ;
      }
      else
      {
         num_packs = 1 ;
      }
   }
   else
   {
      printf("Must enter a host name to Ping\n") ;
      return ;
   }

   set_num_packets(num_packs) ;
   original_num_packets = num_packs ;

   /* configure for network communications */

   status = configure_socket(arg1) ;
   
   if (status == OK)
   {
      sp(start_pinging,0,0,0,0,0,0,0,0,0) ;
      sp(receive_pings,0,0,0,0,0,0,0,0,0) ;
   }
   else
   {
      printf("error while trying to configure socket\n") ;
   }
}



************************************************************


Hope this is what you were looking for.

Chuck Meade
Verdix Product Support