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