home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / os / vxworks / 1233 < prev    next >
Encoding:
Text File  |  1993-01-22  |  6.8 KB  |  379 lines

  1. Path: sparky!uunet!spool.mu.edu!agate!dog.ee.lbl.gov!lbl.gov!vxwexplo
  2. From: Chuck Meade <chuckm@verdix.com>
  3. Newsgroups: comp.os.vxworks
  4. Subject: re: ping command available?
  5. Date: Fri, 22 Jan 93 09:44:49 +0000
  6. Organization: Lawrence Berkeley Laboratory, Berkeley CA
  7. Lines: 366
  8. Sender: vxwexplo@lbl.gov
  9. Message-ID: <9301221444.AA04113@armpit.hq.Verdix.COM.verdix.com>
  10. NNTP-Posting-Host: 128.3.112.16
  11. Originator: daemon@vxw.ee.lbl.gov
  12.  
  13.  
  14.  
  15. > Does anyone have a ping program, similar to Sun's, available under vxWorks?
  16.  
  17.  
  18. I put together a VXworks based ping() routine a while ago.  Supply the host to
  19. ping as the first parameter and the number of packets to send as the second.
  20.  
  21. ********** defs.h ************************
  22.  
  23. #include "vxWorks.h"
  24. #include "errno.h"
  25. #include "socket.h"
  26. #include "in_systm.h"
  27. #include "in.h"
  28. #include "ip.h"
  29. #include "ip_icmp.h"
  30. #include "netdb.h" 
  31. #include "wdLib.h"
  32. #include "usrLib.h"
  33. #include "sigLib.h"
  34. #include "taskLib.h"
  35.  
  36. *********** c-support.c **********************
  37.  
  38. #include "defs.h"
  39.  
  40. #define OK                 0
  41. #define Bad_Host           1
  42. #define Bad_Protocol       2
  43. #define Cant_Create_Socket 3
  44.  
  45. #define From_Our_Request     0
  46. #define Not_From_Our_Request 1
  47.  
  48.  
  49. struct sockaddr_in  dest;
  50. int                 sockfd;
  51. char                *hostname ;
  52. struct protoent     *proto ;
  53. u_char              recvpack[4096] ;
  54. int                 num_received = 0 ;
  55.  
  56.  
  57.  
  58. char configure_socket(the_hostname)
  59.    char *the_hostname ;
  60. {
  61.    dest.sin_family = AF_INET ;
  62.    dest.sin_addr.s_addr = hostGetByName(the_hostname) ;
  63.    if (dest.sin_addr.s_addr == ERROR) 
  64.    {
  65.       printf("bad host!\n") ;
  66.       return(Bad_Host) ;
  67.    }
  68.    strcpy(hostname,the_hostname) ;
  69.  
  70.    sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP) ;
  71.    if (sockfd < 0)
  72.    {
  73.       printf("can't create socket\n") ;
  74.       return (Cant_Create_Socket) ;
  75.    }
  76.    else
  77.    {
  78.       return (OK) ;
  79.    }
  80. }
  81.  
  82.  
  83.  
  84. int in_cksm(msg_ptr)
  85.    register u_short  *msg_ptr ;
  86. {
  87.    register long    sum ;
  88.    register u_short answer ;
  89.    int              index ;
  90.  
  91.    sum = 0 ;
  92.  
  93.    for (index = 0 ; index < 4 ; index++)
  94.    {
  95.       sum = sum + *msg_ptr ;
  96.       msg_ptr++ ;
  97.    }
  98.  
  99.    sum = (sum >> 16) + (sum & 0xffff) ;
  100.    sum = sum + (sum >> 16) ;
  101.    answer = ~sum ;
  102.    return (answer) ;
  103. }
  104.  
  105.  
  106. int message_number = 1 ;
  107.  
  108. void construct(msg)
  109.    struct icmp  *msg ;
  110. {
  111.    u_char *byte_ptr ;
  112.  
  113.    byte_ptr = (u_char *) msg ;
  114.  
  115.    msg->icmp_type = ICMP_ECHO ;
  116.    msg->icmp_code = 0 ;
  117.    msg->icmp_cksum = 0 ;
  118.    msg->icmp_id = 0x1234 ;
  119.    msg->icmp_seq = message_number++ ;
  120.    msg->icmp_data[0] = 0 ;
  121.    msg->icmp_cksum = in_cksm(msg) ;
  122. }
  123.  
  124.  
  125. void xmt_msg(msg)
  126.    struct icmp *msg ;
  127. {
  128.    int i ;
  129.    int packsize ;
  130.  
  131.    packsize = 8 ;
  132.  
  133.    i = sendto(sockfd, (caddr_t) msg, packsize, 0, (struct sockaddr *) &dest, sizeof(dest)) ;
  134.  
  135.    if (i < 0)
  136.    {
  137.       printf("sendto error\n") ;
  138.    }
  139.    else if (i != packsize)
  140.    {
  141.       printf("wrote %s %d bytes, return = %d\n", hostname, packsize, i) ;
  142.    }
  143. }
  144.  
  145.  
  146.  
  147. void receive_pings()
  148. {
  149.    int                 n ;
  150.    int                 fromlen ;
  151.    struct sockaddr_in  from ;
  152.    int                 iphdrlen ;
  153.    struct ip           *ip ;
  154.    struct icmp         *icp ;
  155.    int                 index ;
  156.    extern int          original_num_packets ;
  157.  
  158.  
  159.    num_received = 0 ;
  160.  
  161.    while (1)  /* loop until we break out */
  162.    {
  163.       fromlen = sizeof(from) ;
  164.  
  165.       n = recvfrom(sockfd, recvpack, sizeof(recvpack), 0, (struct sockaddr *) &from, &fromlen) ;
  166.  
  167.       if (n < 0)
  168.       {
  169.          if (errno != EINTR)
  170.          {
  171.             printf("recvfrom error\n") ;
  172.          }
  173.          else
  174.          {
  175.             printf("recvfrom interrupted (this is OK)\n") ;
  176.             ;
  177.          }
  178.       }
  179.       else
  180.       {
  181.          ip = (struct ip *) recvpack ;
  182.          iphdrlen = ip->ip_hl << 2 ;
  183.          icp = (struct icmp *) (recvpack + iphdrlen) ;
  184.  
  185.          if ((icp->icmp_type == ICMP_ECHOREPLY) && (icp->icmp_id == 0x1234))
  186.          {
  187.             num_received++ ;
  188.  
  189.             if (num_received == original_num_packets)
  190.             {
  191.                exit() ;
  192.             }
  193.          }
  194.       }
  195.    }
  196. }
  197.  
  198.  
  199.  
  200. ************** echo-transmitter.c *************************
  201.  
  202. #include "defs.h"
  203.  
  204.  
  205. void sig_alarm() ;
  206. void clean_up() ;
  207. void watchdog_routine() ;
  208.  
  209.  
  210. static WDOG_ID wid ;
  211. int  the_tid ;
  212. struct icmp  the_message ;
  213.  
  214.  
  215. void transmit_echo_message()
  216. {
  217.    construct(&the_message) ;
  218.    xmt_msg(&the_message) ;
  219.    return ;
  220.  
  221.  
  222.  
  223. void dummy_alarm_handler()
  224. {
  225.    ;
  226. }
  227.  
  228.  
  229. SIGVEC handler_info = {dummy_alarm_handler,0,0} ;
  230. SIGVEC save_handler_info ;
  231.  
  232.  
  233. void start_pinging()
  234. {
  235.    extern int get_num_packets() ;
  236.    extern void set_num_packets() ;
  237.  
  238.    sigInit() ;
  239.    sigvec(SIGALRM, &handler_info, &save_handler_info) ;
  240.  
  241.    the_tid = taskIdSelf() ;
  242.    wid = wdCreate() ;  /* watchdog */
  243.    wdStart(wid, sysClkRateGet(), watchdog_routine, 0) ;;
  244.  
  245.    while (1)
  246.    {
  247.       if (get_num_packets() > 0)
  248.       {
  249.          set_num_packets(get_num_packets() - 1) ;
  250.          transmit_echo_message() ;
  251.       }
  252.       else
  253.       {
  254.          break ;
  255.       }
  256.  
  257.       pause() ;
  258.    }
  259.  
  260.    pause() ;
  261.    wdDelete(wid) ;
  262.    clean_up() ;
  263.    exit() ;
  264. }
  265.  
  266.  
  267.  
  268. void watchdog_routine()
  269. {
  270.    kill(the_tid,SIGALRM) ;
  271.    wdStart(wid, sysClkRateGet(), watchdog_routine, 0) ;
  272. }
  273.  
  274.  
  275. void clean_up()
  276. {
  277.    extern int original_num_packets ;
  278.    extern int num_received ;
  279.    extern char *hostname ;
  280.  
  281.    if (num_received == original_num_packets)
  282.    {
  283.       printf("host %s is alive\n", hostname) ;
  284.    }
  285.    else
  286.    {
  287.       printf("response trouble with host %s:  sent %d packet%c, received %d packet%c\n",
  288.              hostname, original_num_packets, ((original_num_packets != 1) ? 's' : ' '),
  289.              num_received, ((num_received != 1) ? 's' : ' ') ) ;
  290.    }
  291.  
  292.    return ;
  293. }
  294.  
  295.  
  296. *********** ping-stats.c *******************************
  297.  
  298. #include "defs.h"
  299.  
  300.  
  301. static int num_packets ;
  302.  
  303. void set_num_packets(to)
  304.    int to ;
  305. {
  306.    num_packets = to ;
  307. }
  308.  
  309. int get_num_packets()
  310. {
  311.    return (num_packets) ;
  312. }
  313.  
  314.  
  315. *********** ping.c ****************************************
  316.  
  317. #include "defs.h"
  318.  
  319.  
  320. int original_num_packets ;
  321.  
  322. int ping(arg1, arg2)
  323.    char *arg1 ;
  324.    int arg2 ;
  325. {
  326.    extern int configure_socket() ;
  327.    extern void start_pinging() ;
  328.    extern void receive_pings() ;
  329.    int num_packs ;
  330.    int status ;
  331.  
  332.    /* get user's parameters */
  333.  
  334.    if (arg1 != NULL) 
  335.    {
  336.       if (arg2 > 1) 
  337.       {
  338.          num_packs = arg2 ;
  339.       }
  340.       else
  341.       {
  342.          num_packs = 1 ;
  343.       }
  344.    }
  345.    else
  346.    {
  347.       printf("Must enter a host name to Ping\n") ;
  348.       return ;
  349.    }
  350.  
  351.    set_num_packets(num_packs) ;
  352.    original_num_packets = num_packs ;
  353.  
  354.    /* configure for network communications */
  355.  
  356.    status = configure_socket(arg1) ;
  357.    
  358.    if (status == OK)
  359.    {
  360.       sp(start_pinging,0,0,0,0,0,0,0,0,0) ;
  361.       sp(receive_pings,0,0,0,0,0,0,0,0,0) ;
  362.    }
  363.    else
  364.    {
  365.       printf("error while trying to configure socket\n") ;
  366.    }
  367. }
  368.  
  369.  
  370.  
  371. ************************************************************
  372.  
  373.  
  374. Hope this is what you were looking for.
  375.  
  376. Chuck Meade
  377. Verdix Product Support
  378.