Advertisement
Guest User

PicoTCP ping tool on barebox

a guest
Jun 2nd, 2014
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. #include <pico_icmp4.h>
  2. #include <poller.h>
  3.  
  4. #define NUM_PING 10
  5.  
  6. #define PING_STATE_WORK     0
  7. #define PING_STATE_DONE     1
  8.  
  9. static int ping_state;
  10. static int ping_replies;
  11.  
  12. /* callback function for receiving ping reply */
  13. void cb_ping(struct pico_icmp4_stats *s)
  14. {
  15.     char host[30];
  16.     int time_sec = 0;
  17.     int time_msec = 0;
  18.  
  19.     if (ping_state == PING_STATE_DONE) {
  20.         return;
  21.     }
  22.  
  23.     /* convert ip address from icmp4_stats structure to string */
  24.     pico_ipv4_to_string(host, s->dst.addr);
  25.  
  26.     /* get time information from icmp4_stats structure */
  27.     time_sec = s->time / 1000;
  28.     time_msec = s->time % 1000;
  29.  
  30.     if (s->err == PICO_PING_ERR_REPLIED) {
  31.         /* print info if no error reported in icmp4_stats structure */
  32.         printf("%lu bytes from %s: icmp_req=%lu ttl=%lu time=%llu ms\n", \
  33.             s->size, host, s->seq, s->ttl, s->time);
  34.     } else {
  35.         /* else, print error info */
  36.         printf("PING %lu to %s: Error %d\n", s->seq, host, s->err);
  37.     }
  38.     if (++ping_replies >= NUM_PING)
  39.         ping_state = PING_STATE_DONE;
  40. }
  41.  
  42. static int do_picoping(int argc, char *argv[])
  43. {
  44.     int id;
  45.     if (argc < 1) {
  46.         perror("picoping");
  47.         return 1;
  48.     }
  49.  
  50.     ping_state = PING_STATE_WORK;
  51.     ping_replies = 0;
  52.  
  53.     id = pico_icmp4_ping(argv[1], NUM_PING, 1000, 5000, 48, cb_ping);
  54.     if (id < 0) {
  55.         perror("picoping");
  56.         return 1;
  57.     }
  58.  
  59.     while (ping_state != PING_STATE_DONE) {
  60.         if (ctrlc()) {
  61.             pico_icmp4_ping_abort(id);
  62.             ping_state = PING_STATE_DONE;
  63.             break;
  64.         }
  65.         get_time_ns();
  66.         poller_call();
  67.     }
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement