2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006 Florian octo Forster <octo at verplant.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/ip_icmp.h>
35 #include <netinet/icmp6.h>
43 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
45 # define dprintf(...) /**/
48 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
51 * private (static) functions
53 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
57 if ((tv1->tv_sec < tv2->tv_sec)
58 || ((tv1->tv_sec == tv2->tv_sec)
59 && (tv1->tv_usec < tv2->tv_usec)))
62 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
63 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
65 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
67 while (res->tv_usec < 0)
69 res->tv_usec += 1000000;
76 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
83 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
88 *(char *) &ret = *(char *) ptr;
92 /* Do this twice to get all possible carries.. */
93 sum = (sum >> 16) + (sum & 0xFFFF);
94 sum = (sum >> 16) + (sum & 0xFFFF);
101 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
104 struct icmphdr *icmp_hdr;
108 uint16_t recv_checksum;
109 uint16_t calc_checksum;
116 if (buffer_len < sizeof (struct ip))
119 ip_hdr = (struct ip *) buffer;
120 ip_hdr_len = ip_hdr->ip_hl << 2;
122 if (buffer_len < ip_hdr_len)
125 buffer += ip_hdr_len;
126 buffer_len -= ip_hdr_len;
128 if (buffer_len < sizeof (struct icmphdr))
131 icmp_hdr = (struct icmphdr *) buffer;
132 buffer += sizeof (struct icmphdr);
133 buffer_len -= sizeof (struct icmphdr);
135 if (icmp_hdr->type != ICMP_ECHOREPLY)
137 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->type);
141 recv_checksum = icmp_hdr->checksum;
142 icmp_hdr->checksum = 0;
143 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
144 sizeof (struct icmphdr) + buffer_len);
146 if (recv_checksum != calc_checksum)
148 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
149 recv_checksum, calc_checksum);
153 ident = ntohs (icmp_hdr->un.echo.id);
154 seq = ntohs (icmp_hdr->un.echo.sequence);
156 for (ptr = ph; ptr != NULL; ptr = ptr->next)
158 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
159 ptr->hostname, ptr->ident, ptr->sequence - 1);
161 if (ptr->addrfamily != AF_INET)
164 if (!timerisset (ptr->timer))
167 if (ptr->ident != ident)
170 if ((ptr->sequence - 1) != seq)
173 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
174 ptr->hostname, ident, seq);
181 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
188 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
190 struct icmp6_hdr *icmp_hdr;
197 if (buffer_len < sizeof (struct icmp6_hdr))
200 icmp_hdr = (struct icmp6_hdr *) buffer;
201 buffer += sizeof (struct icmphdr);
202 buffer_len -= sizeof (struct icmphdr);
204 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
206 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
210 if (icmp_hdr->icmp6_code != 0)
212 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
216 ident = ntohs (icmp_hdr->icmp6_id);
217 seq = ntohs (icmp_hdr->icmp6_seq);
219 for (ptr = ph; ptr != NULL; ptr = ptr->next)
221 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
222 ptr->hostname, ptr->ident, ptr->sequence - 1);
224 if (ptr->addrfamily != AF_INET6)
227 if (!timerisset (ptr->timer))
230 if (ptr->ident != ident)
233 if ((ptr->sequence - 1) != seq)
236 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
237 ptr->hostname, ident, seq);
244 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
251 static int ping_receive_one (int fd, pinghost_t *ph)
259 pinghost_t *host = NULL;
261 struct sockaddr_storage sa;
264 sa_len = sizeof (sa);
266 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
267 (struct sockaddr *) &sa, &sa_len);
268 if (buffer_len == -1)
270 dprintf ("recvfrom: %s\n", strerror (errno));
274 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
276 if (sa.ss_family == AF_INET)
278 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
281 else if (sa.ss_family == AF_INET6)
283 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
287 if (gettimeofday (&now, NULL) == -1)
289 dprintf ("gettimeofday: %s\n", strerror (errno));
290 timerclear (host->timer);
294 dprintf ("sent: %12i.%06i\n",
295 (int) host->timer->tv_sec,
296 (int) host->timer->tv_usec);
297 dprintf ("rcvd: %12i.%06i\n",
301 if (ping_timeval_sub (&now, host->timer, &diff) < 0)
303 timerclear (host->timer);
307 dprintf ("diff: %12i.%06i\n",
311 host->latency = ((double) diff.tv_usec) / 1000.0;
312 host->latency += ((double) diff.tv_sec) * 1000.0;
314 timerclear (host->timer);
319 static int ping_receive_all (pinghost_t *ph)
327 struct timeval endtime;
328 struct timeval nowtime;
329 struct timeval timeout;
336 if (gettimeofday (&endtime, NULL) == -1)
340 for (ptr = ph; ptr != NULL; ptr = ptr->next)
349 if (gettimeofday (&nowtime, NULL) == -1)
352 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
355 for (ptr = ph; ptr != NULL; ptr = ptr->next)
357 if (!timerisset (ptr->timer))
360 FD_SET (ptr->fd, &readfds);
363 if (max_readfds < ptr->fd)
364 max_readfds = ptr->fd;
367 if (num_readfds == 0)
370 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
371 (int) timeout.tv_sec,
372 (int) timeout.tv_usec);
374 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
376 if ((status == -1) && (errno == EINTR))
378 dprintf ("select was interrupted by signal..\n");
383 dprintf ("select: %s\n", strerror (errno));
386 else if (status == 0)
388 dprintf ("select timed out\n");
392 for (ptr = ph; ptr != NULL; ptr = ptr->next)
394 if (FD_ISSET (ptr->fd, &readfds))
395 if (ping_receive_one (ptr->fd, ph) == 0)
403 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
404 * Sending functions: *
407 * +-> ping_send_one_ipv4 *
408 * `-> ping_send_one_ipv6 *
409 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
410 static int ping_send_one_ipv4 (pinghost_t *ph)
412 struct icmphdr *icmp4;
421 dprintf ("ph->hostname = %s\n", ph->hostname);
423 memset (buf, '\0', sizeof (buf));
424 icmp4 = (struct icmphdr *) buf;
425 data = (char *) (icmp4 + 1);
427 icmp4->type = ICMP_ECHO;
429 /* The checksum will be calculated by the TCP/IP stack. */
431 icmp4->un.echo.id = htons (ph->ident);
432 icmp4->un.echo.sequence = htons (ph->sequence);
434 strcpy (data, PING_DATA);
435 datalen = strlen (data);
437 buflen = datalen + sizeof (struct icmphdr);
439 icmp4->checksum = ping_icmp4_checksum (buf, buflen);
441 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
443 status = sendto (ph->fd, buf, buflen, 0,
444 (struct sockaddr *) ph->addr, ph->addrlen);
451 dprintf ("sendto: status = %i\n", status);
456 static int ping_send_one_ipv6 (pinghost_t *ph)
458 struct icmp6_hdr *icmp6;
467 dprintf ("ph->hostname = %s\n", ph->hostname);
469 memset (buf, '\0', sizeof (buf));
470 icmp6 = (struct icmp6_hdr *) buf;
471 data = (char *) (icmp6 + 1);
473 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
474 icmp6->icmp6_code = 0;
475 /* The checksum will be calculated by the TCP/IP stack. */
476 icmp6->icmp6_cksum = 0;
477 icmp6->icmp6_id = htons (ph->ident);
478 icmp6->icmp6_seq = htons (ph->sequence);
480 strcpy (data, PING_DATA);
481 datalen = strlen (data);
483 buflen = datalen + sizeof (struct icmp6_hdr);
485 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
487 status = sendto (ph->fd, buf, buflen, 0,
488 (struct sockaddr *) ph->addr, ph->addrlen);
495 dprintf ("sendto: status = %i\n", status);
500 static int ping_send_all (pinghost_t *ph)
504 for (ptr = ph; ptr != NULL; ptr = ptr->next)
506 /* start timer.. The GNU `ping6' starts the timer before
507 * sending the packet, so I will do that too */
508 if (gettimeofday (ptr->timer, NULL) == -1)
510 dprintf ("gettimeofday: %s\n", strerror (errno));
511 timerclear (ptr->timer);
516 dprintf ("timer set for hostname = %s\n", ptr->hostname);
519 if (ptr->addrfamily == AF_INET6)
521 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
522 if (ping_send_one_ipv6 (ptr) != 0)
524 timerclear (ptr->timer);
528 else if (ptr->addrfamily == AF_INET)
530 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
531 if (ping_send_one_ipv4 (ptr) != 0)
533 timerclear (ptr->timer);
537 else /* this should not happen */
539 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
540 timerclear (ptr->timer);
551 static int ping_get_ident (void)
554 static int did_seed = 0;
560 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
564 if (read (fd, &seed, sizeof (seed)) != -1)
567 dprintf ("Random seed: %i\n", seed);
575 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
579 retval = (int) random ();
581 dprintf ("Random number: %i\n", retval);
586 static pinghost_t *ping_alloc (void)
591 ph_size = sizeof (pinghost_t)
592 + sizeof (struct sockaddr_storage)
593 + sizeof (struct timeval);
595 ph = (pinghost_t *) malloc (ph_size);
599 memset (ph, '\0', ph_size);
601 ph->timer = (struct timeval *) (ph + 1);
602 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
604 ph->addrlen = sizeof (struct sockaddr_storage);
606 ph->ident = ping_get_ident () & 0xFFFF;
611 static void ping_free (pinghost_t *ph)
613 if (ph->hostname != NULL)
622 pingobj_t *ping_construct (void)
626 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
634 void ping_destroy (pingobj_t *obj)
642 while (current != NULL)
644 next = current->next;
654 int ping_setopt (pingobj_t *obj, int option, void *value)
660 case PING_OPT_TIMEOUT:
661 obj->timeout = *((double *) value);
662 if (obj->timeout < 0.0)
664 obj->timeout = PING_DEF_TIMEOUT;
670 obj->ttl = *((int *) value);
671 if ((obj->ttl < 1) || (obj->ttl > 255))
673 obj->ttl = PING_DEF_TTL;
679 obj->addrfamily = *((int *) value);
680 if ((obj->addrfamily != AF_UNSPEC)
681 && (obj->addrfamily != AF_INET)
682 && (obj->addrfamily != AF_INET6))
684 obj->addrfamily = PING_DEF_AF;
691 } /* switch (option) */
694 } /* int ping_setopt */
697 int ping_send (pingobj_t *obj)
701 if (ping_send_all (obj->head) < 0)
704 if ((ret = ping_receive_all (obj->head)) < 0)
710 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
714 if (strcasecmp (ph->hostname, host) == 0)
723 int ping_host_add (pingobj_t *obj, const char *host)
727 struct sockaddr_storage sockaddr;
728 socklen_t sockaddr_len;
730 struct addrinfo ai_hints;
731 struct addrinfo *ai_list, *ai_ptr;
734 dprintf ("host = %s\n", host);
736 if (ping_host_search (obj->head, host) != NULL)
739 memset (&ai_hints, '\0', sizeof (ai_hints));
740 ai_hints.ai_flags = 0;
742 ai_hints.ai_flags |= AI_ADDRCONFIG;
744 ai_hints.ai_family = PF_UNSPEC;
745 ai_hints.ai_socktype = SOCK_RAW;
747 if ((ph = ping_alloc ()) == NULL)
749 dprintf ("Out of memory!\n");
753 if ((ph->hostname = strdup (host)) == NULL)
755 dprintf ("Out of memory!\n");
760 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
762 dprintf ("getaddrinfo failed\n");
767 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
771 sockaddr_len = sizeof (sockaddr);
772 memset (&sockaddr, '\0', sockaddr_len);
774 if (ai_ptr->ai_family == AF_INET)
776 struct sockaddr_in *si;
778 si = (struct sockaddr_in *) &sockaddr;
779 si->sin_family = AF_INET;
780 si->sin_port = htons (ph->ident);
781 si->sin_addr.s_addr = htonl (INADDR_ANY);
783 ai_ptr->ai_protocol = IPPROTO_ICMP;
785 else if (ai_ptr->ai_family == AF_INET6)
787 struct sockaddr_in6 *si;
789 si = (struct sockaddr_in6 *) &sockaddr;
790 si->sin6_family = AF_INET6;
791 si->sin6_port = htons (ph->ident);
792 si->sin6_addr = in6addr_any;
794 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
798 dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
802 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
805 dprintf ("socket: %s\n", strerror (errno));
809 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
811 dprintf ("bind: %s\n", strerror (errno));
817 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
818 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
819 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
820 ph->addrlen = ai_ptr->ai_addrlen;
821 ph->addrfamily = ai_ptr->ai_family;
826 freeaddrinfo (ai_list);
835 ph->next = obj->head;
841 int ping_host_remove (pingobj_t *obj, const char *host)
843 pinghost_t *pre, *cur;
850 if (strcasecmp (host, cur->hostname))
861 obj->head = cur->next;
863 pre->next = cur->next;
873 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
875 return ((pingobj_iter_t *) obj->head);
878 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
880 return ((pingobj_iter_t *) iter->next);
883 const char *ping_iterator_get_host (pingobj_iter_t *iter)
885 return (iter->hostname);
888 double ping_iterator_get_latency (pingobj_iter_t *iter)
890 return (iter->latency);