Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pico_icmp4.h>
- #include <poller.h>
- #define NUM_PING 10
- #define PING_STATE_WORK 0
- #define PING_STATE_DONE 1
- static int ping_state;
- static int ping_replies;
- /* callback function for receiving ping reply */
- void cb_ping(struct pico_icmp4_stats *s)
- {
- char host[30];
- int time_sec = 0;
- int time_msec = 0;
- if (ping_state == PING_STATE_DONE) {
- return;
- }
- /* convert ip address from icmp4_stats structure to string */
- pico_ipv4_to_string(host, s->dst.addr);
- /* get time information from icmp4_stats structure */
- time_sec = s->time / 1000;
- time_msec = s->time % 1000;
- if (s->err == PICO_PING_ERR_REPLIED) {
- /* print info if no error reported in icmp4_stats structure */
- printf("%lu bytes from %s: icmp_req=%lu ttl=%lu time=%llu ms\n", \
- s->size, host, s->seq, s->ttl, s->time);
- } else {
- /* else, print error info */
- printf("PING %lu to %s: Error %d\n", s->seq, host, s->err);
- }
- if (++ping_replies >= NUM_PING)
- ping_state = PING_STATE_DONE;
- }
- static int do_picoping(int argc, char *argv[])
- {
- int id;
- if (argc < 1) {
- perror("picoping");
- return 1;
- }
- ping_state = PING_STATE_WORK;
- ping_replies = 0;
- id = pico_icmp4_ping(argv[1], NUM_PING, 1000, 5000, 48, cb_ping);
- if (id < 0) {
- perror("picoping");
- return 1;
- }
- while (ping_state != PING_STATE_DONE) {
- if (ctrlc()) {
- pico_icmp4_ping_abort(id);
- ping_state = PING_STATE_DONE;
- break;
- }
- get_time_ns();
- poller_call();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement