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
31 # error "You don't have the standard C99 header files installed"
32 #endif /* STDC_HEADERS */
42 # include <sys/types.h>
45 # include <sys/stat.h>
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
54 # include <sys/socket.h>
60 #if HAVE_NETINET_IN_SYSTM_H
61 # include <netinet/in_systm.h>
64 # include <netinet/in.h>
67 # include <netinet/ip.h>
69 #if HAVE_NETINET_IP_ICMP_H
70 # include <netinet/ip_icmp.h>
72 #ifdef HAVE_NETINET_IP_VAR_H
73 # include <netinet/ip_var.h>
75 #if HAVE_NETINET_IP6_H
76 # include <netinet/ip6.h>
78 #if HAVE_NETINET_ICMP6_H
79 # include <netinet/icmp6.h>
85 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
87 # define dprintf(...) /**/
90 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
95 struct sockaddr_storage *addr;
101 struct timeval *timer;
104 struct pinghost *next;
117 * private (static) functions
119 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
122 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
123 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
125 while (res->tv_usec > 1000000)
127 res->tv_usec -= 1000000;
134 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
138 if ((tv1->tv_sec < tv2->tv_sec)
139 || ((tv1->tv_sec == tv2->tv_sec)
140 && (tv1->tv_usec < tv2->tv_usec)))
143 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
144 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
146 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
148 while (res->tv_usec < 0)
150 res->tv_usec += 1000000;
157 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
164 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
169 *(char *) &ret = *(char *) ptr;
173 /* Do this twice to get all possible carries.. */
174 sum = (sum >> 16) + (sum & 0xFFFF);
175 sum = (sum >> 16) + (sum & 0xFFFF);
182 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
185 struct icmp *icmp_hdr;
189 uint16_t recv_checksum;
190 uint16_t calc_checksum;
197 if (buffer_len < sizeof (struct ip))
200 ip_hdr = (struct ip *) buffer;
201 ip_hdr_len = ip_hdr->ip_hl << 2;
203 if (buffer_len < ip_hdr_len)
206 buffer += ip_hdr_len;
207 buffer_len -= ip_hdr_len;
209 if (buffer_len < sizeof (struct icmp))
212 icmp_hdr = (struct icmp *) buffer;
213 buffer += sizeof (struct icmp);
214 buffer_len -= sizeof (struct icmp);
216 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
218 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
222 recv_checksum = icmp_hdr->icmp_cksum;
223 icmp_hdr->icmp_cksum = 0;
224 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
225 sizeof (struct icmp) + buffer_len);
227 if (recv_checksum != calc_checksum)
229 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
230 recv_checksum, calc_checksum);
234 ident = ntohs (icmp_hdr->icmp_id);
235 seq = ntohs (icmp_hdr->icmp_seq);
237 for (ptr = ph; ptr != NULL; ptr = ptr->next)
239 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
240 ptr->hostname, ptr->ident, ptr->sequence - 1);
242 if (ptr->addrfamily != AF_INET)
245 if (!timerisset (ptr->timer))
248 if (ptr->ident != ident)
251 if ((ptr->sequence - 1) != seq)
254 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
255 ptr->hostname, ident, seq);
262 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
269 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
271 struct icmp6_hdr *icmp_hdr;
278 if (buffer_len < sizeof (struct icmp6_hdr))
281 icmp_hdr = (struct icmp6_hdr *) buffer;
282 buffer += sizeof (struct icmp);
283 buffer_len -= sizeof (struct icmp);
285 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
287 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
291 if (icmp_hdr->icmp6_code != 0)
293 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
297 ident = ntohs (icmp_hdr->icmp6_id);
298 seq = ntohs (icmp_hdr->icmp6_seq);
300 for (ptr = ph; ptr != NULL; ptr = ptr->next)
302 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
303 ptr->hostname, ptr->ident, ptr->sequence - 1);
305 if (ptr->addrfamily != AF_INET6)
308 if (!timerisset (ptr->timer))
311 if (ptr->ident != ident)
314 if ((ptr->sequence - 1) != seq)
317 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
318 ptr->hostname, ident, seq);
325 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
332 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
339 pinghost_t *host = NULL;
341 struct sockaddr_storage sa;
344 sa_len = sizeof (sa);
346 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
347 (struct sockaddr *) &sa, &sa_len);
348 if (buffer_len == -1)
350 dprintf ("recvfrom: %s\n", strerror (errno));
354 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
356 if (sa.ss_family == AF_INET)
358 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
361 else if (sa.ss_family == AF_INET6)
363 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
367 dprintf ("rcvd: %12i.%06i\n",
370 dprintf ("sent: %12i.%06i\n",
371 (int) host->timer->tv_sec,
372 (int) host->timer->tv_usec);
374 if (ping_timeval_sub (now, host->timer, &diff) < 0)
376 timerclear (host->timer);
380 dprintf ("diff: %12i.%06i\n",
384 host->latency = ((double) diff.tv_usec) / 1000.0;
385 host->latency += ((double) diff.tv_sec) * 1000.0;
387 timerclear (host->timer);
392 static int ping_receive_all (pingobj_t *obj)
401 struct timeval endtime;
402 struct timeval nowtime;
403 struct timeval timeout;
411 for (ptr = ph; ptr != NULL; ptr = ptr->next)
414 if (gettimeofday (&nowtime, NULL) == -1)
418 timeout.tv_sec = (time_t) obj->timeout;
419 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
421 dprintf ("Set timeout to %i.%06i seconds\n",
422 (int) timeout.tv_sec,
423 (int) timeout.tv_usec);
425 ping_timeval_add (&nowtime, &timeout, &endtime);
433 for (ptr = ph; ptr != NULL; ptr = ptr->next)
435 if (!timerisset (ptr->timer))
438 FD_SET (ptr->fd, &readfds);
441 if (max_readfds < ptr->fd)
442 max_readfds = ptr->fd;
445 if (num_readfds == 0)
448 if (gettimeofday (&nowtime, NULL) == -1)
451 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
454 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
455 (int) timeout.tv_sec,
456 (int) timeout.tv_usec);
458 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
460 if (gettimeofday (&nowtime, NULL) == -1)
463 if ((status == -1) && (errno == EINTR))
465 dprintf ("select was interrupted by signal..\n");
470 dprintf ("select: %s\n", strerror (errno));
473 else if (status == 0)
475 dprintf ("select timed out\n");
479 for (ptr = ph; ptr != NULL; ptr = ptr->next)
481 if (FD_ISSET (ptr->fd, &readfds))
482 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
490 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
491 * Sending functions: *
494 * +-> ping_send_one_ipv4 *
495 * `-> ping_send_one_ipv6 *
496 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
497 ssize_t ping_sendto (pinghost_t *ph, const void *buf, size_t buflen)
501 if (gettimeofday (ph->timer, NULL) == -1)
503 timerclear (ph->timer);
507 ret = sendto (ph->fd, buf, buflen, 0,
508 (struct sockaddr *) ph->addr, ph->addrlen);
513 static int ping_send_one_ipv4 (pinghost_t *ph)
524 dprintf ("ph->hostname = %s\n", ph->hostname);
526 memset (buf, '\0', sizeof (buf));
527 icmp4 = (struct icmp *) buf;
528 data = (char *) (icmp4 + 1);
530 icmp4->icmp_type = ICMP_ECHO;
531 icmp4->icmp_code = 0;
532 icmp4->icmp_cksum = 0;
533 icmp4->icmp_id = htons (ph->ident);
534 icmp4->icmp_seq = htons (ph->sequence);
536 strcpy (data, PING_DATA);
537 datalen = strlen (data);
539 buflen = datalen + sizeof (struct icmp);
541 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
543 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
545 status = ping_sendto (ph, buf, buflen);
548 perror ("ping_sendto");
552 dprintf ("sendto: status = %i\n", status);
557 static int ping_send_one_ipv6 (pinghost_t *ph)
559 struct icmp6_hdr *icmp6;
568 dprintf ("ph->hostname = %s\n", ph->hostname);
570 memset (buf, '\0', sizeof (buf));
571 icmp6 = (struct icmp6_hdr *) buf;
572 data = (char *) (icmp6 + 1);
574 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
575 icmp6->icmp6_code = 0;
576 /* The checksum will be calculated by the TCP/IP stack. */
577 icmp6->icmp6_cksum = 0;
578 icmp6->icmp6_id = htons (ph->ident);
579 icmp6->icmp6_seq = htons (ph->sequence);
581 strcpy (data, PING_DATA);
582 datalen = strlen (data);
584 buflen = datalen + sizeof (struct icmp6_hdr);
586 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
588 status = ping_sendto (ph, buf, buflen);
591 perror ("ping_sendto");
595 dprintf ("sendto: status = %i\n", status);
600 static int ping_send_all (pinghost_t *ph)
604 for (ptr = ph; ptr != NULL; ptr = ptr->next)
606 /* start timer.. The GNU `ping6' starts the timer before
607 * sending the packet, so I will do that too */
608 if (gettimeofday (ptr->timer, NULL) == -1)
610 dprintf ("gettimeofday: %s\n", strerror (errno));
611 timerclear (ptr->timer);
616 dprintf ("timer set for hostname = %s\n", ptr->hostname);
619 if (ptr->addrfamily == AF_INET6)
621 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
622 if (ping_send_one_ipv6 (ptr) != 0)
624 timerclear (ptr->timer);
628 else if (ptr->addrfamily == AF_INET)
630 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
631 if (ping_send_one_ipv4 (ptr) != 0)
633 timerclear (ptr->timer);
637 else /* this should not happen */
639 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
640 timerclear (ptr->timer);
652 * Set the TTL of a socket protocol independently.
654 static int ping_set_ttl (pinghost_t *ph, int ttl)
658 if (ph->addrfamily == AF_INET)
660 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
662 else if (ph->addrfamily == AF_INET6)
664 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
670 static int ping_get_ident (void)
673 static int did_seed = 0;
679 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
683 if (read (fd, &seed, sizeof (seed)) != -1)
686 dprintf ("Random seed: %i\n", seed);
694 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
698 retval = (int) random ();
700 dprintf ("Random number: %i\n", retval);
705 static pinghost_t *ping_alloc (void)
710 ph_size = sizeof (pinghost_t)
711 + sizeof (struct sockaddr_storage)
712 + sizeof (struct timeval);
714 ph = (pinghost_t *) malloc (ph_size);
718 memset (ph, '\0', ph_size);
720 ph->timer = (struct timeval *) (ph + 1);
721 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
723 ph->addrlen = sizeof (struct sockaddr_storage);
725 ph->ident = ping_get_ident () & 0xFFFF;
730 static void ping_free (pinghost_t *ph)
732 if (ph->hostname != NULL)
741 pingobj_t *ping_construct (void)
745 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
753 void ping_destroy (pingobj_t *obj)
761 while (current != NULL)
763 next = current->next;
773 int ping_setopt (pingobj_t *obj, int option, void *value)
779 case PING_OPT_TIMEOUT:
780 obj->timeout = *((double *) value);
781 if (obj->timeout < 0.0)
783 obj->timeout = PING_DEF_TIMEOUT;
789 obj->ttl = *((int *) value);
790 if ((obj->ttl < 1) || (obj->ttl > 255))
792 obj->ttl = PING_DEF_TTL;
798 obj->addrfamily = *((int *) value);
799 if ((obj->addrfamily != AF_UNSPEC)
800 && (obj->addrfamily != AF_INET)
801 && (obj->addrfamily != AF_INET6))
803 obj->addrfamily = PING_DEF_AF;
810 } /* switch (option) */
813 } /* int ping_setopt */
816 int ping_send (pingobj_t *obj)
820 if (ping_send_all (obj->head) < 0)
823 if ((ret = ping_receive_all (obj)) < 0)
829 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
833 if (strcasecmp (ph->hostname, host) == 0)
842 int ping_host_add (pingobj_t *obj, const char *host)
846 struct sockaddr_storage sockaddr;
847 socklen_t sockaddr_len;
849 struct addrinfo ai_hints;
850 struct addrinfo *ai_list, *ai_ptr;
853 dprintf ("host = %s\n", host);
855 if (ping_host_search (obj->head, host) != NULL)
858 memset (&ai_hints, '\0', sizeof (ai_hints));
859 ai_hints.ai_flags = 0;
861 ai_hints.ai_flags |= AI_ADDRCONFIG;
863 ai_hints.ai_family = obj->addrfamily;
864 ai_hints.ai_socktype = SOCK_RAW;
866 if ((ph = ping_alloc ()) == NULL)
868 dprintf ("Out of memory!\n");
872 if ((ph->hostname = strdup (host)) == NULL)
874 dprintf ("Out of memory!\n");
879 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
881 dprintf ("getaddrinfo failed\n");
886 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
890 sockaddr_len = sizeof (sockaddr);
891 memset (&sockaddr, '\0', sockaddr_len);
893 if (ai_ptr->ai_family == AF_INET)
895 struct sockaddr_in *si;
897 si = (struct sockaddr_in *) &sockaddr;
898 si->sin_family = AF_INET;
899 si->sin_port = htons (ph->ident);
900 si->sin_addr.s_addr = htonl (INADDR_ANY);
902 ai_ptr->ai_protocol = IPPROTO_ICMP;
904 else if (ai_ptr->ai_family == AF_INET6)
906 struct sockaddr_in6 *si;
908 si = (struct sockaddr_in6 *) &sockaddr;
909 si->sin6_family = AF_INET6;
910 si->sin6_port = htons (ph->ident);
911 si->sin6_addr = in6addr_any;
913 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
917 dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
921 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
924 dprintf ("socket: %s\n", strerror (errno));
928 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
930 dprintf ("bind: %s\n", strerror (errno));
936 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
937 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
938 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
939 ph->addrlen = ai_ptr->ai_addrlen;
940 ph->addrfamily = ai_ptr->ai_family;
945 freeaddrinfo (ai_list);
954 ph->next = obj->head;
957 ping_set_ttl (ph, obj->ttl);
962 int ping_host_remove (pingobj_t *obj, const char *host)
964 pinghost_t *pre, *cur;
971 if (strcasecmp (host, cur->hostname))
982 obj->head = cur->next;
984 pre->next = cur->next;
994 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
996 return ((pingobj_iter_t *) obj->head);
999 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1001 return ((pingobj_iter_t *) iter->next);
1004 const char *ping_iterator_get_host (pingobj_iter_t *iter)
1006 return (iter->hostname);
1009 double ping_iterator_get_latency (pingobj_iter_t *iter)
1011 return (iter->latency);