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/"
93 * private (static) functions
95 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
99 if ((tv1->tv_sec < tv2->tv_sec)
100 || ((tv1->tv_sec == tv2->tv_sec)
101 && (tv1->tv_usec < tv2->tv_usec)))
104 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
105 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
107 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
109 while (res->tv_usec < 0)
111 res->tv_usec += 1000000;
118 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
125 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
130 *(char *) &ret = *(char *) ptr;
134 /* Do this twice to get all possible carries.. */
135 sum = (sum >> 16) + (sum & 0xFFFF);
136 sum = (sum >> 16) + (sum & 0xFFFF);
143 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
146 struct icmp *icmp_hdr;
150 uint16_t recv_checksum;
151 uint16_t calc_checksum;
158 if (buffer_len < sizeof (struct ip))
161 ip_hdr = (struct ip *) buffer;
162 ip_hdr_len = ip_hdr->ip_hl << 2;
164 if (buffer_len < ip_hdr_len)
167 buffer += ip_hdr_len;
168 buffer_len -= ip_hdr_len;
170 if (buffer_len < sizeof (struct icmp))
173 icmp_hdr = (struct icmp *) buffer;
174 buffer += sizeof (struct icmp);
175 buffer_len -= sizeof (struct icmp);
177 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
179 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
183 recv_checksum = icmp_hdr->icmp_cksum;
184 icmp_hdr->icmp_cksum = 0;
185 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
186 sizeof (struct icmp) + buffer_len);
188 if (recv_checksum != calc_checksum)
190 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
191 recv_checksum, calc_checksum);
195 ident = ntohs (icmp_hdr->icmp_id);
196 seq = ntohs (icmp_hdr->icmp_seq);
198 for (ptr = ph; ptr != NULL; ptr = ptr->next)
200 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
201 ptr->hostname, ptr->ident, ptr->sequence - 1);
203 if (ptr->addrfamily != AF_INET)
206 if (!timerisset (ptr->timer))
209 if (ptr->ident != ident)
212 if ((ptr->sequence - 1) != seq)
215 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
216 ptr->hostname, ident, seq);
223 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
230 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
232 struct icmp6_hdr *icmp_hdr;
239 if (buffer_len < sizeof (struct icmp6_hdr))
242 icmp_hdr = (struct icmp6_hdr *) buffer;
243 buffer += sizeof (struct icmp);
244 buffer_len -= sizeof (struct icmp);
246 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
248 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
252 if (icmp_hdr->icmp6_code != 0)
254 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
258 ident = ntohs (icmp_hdr->icmp6_id);
259 seq = ntohs (icmp_hdr->icmp6_seq);
261 for (ptr = ph; ptr != NULL; ptr = ptr->next)
263 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
264 ptr->hostname, ptr->ident, ptr->sequence - 1);
266 if (ptr->addrfamily != AF_INET6)
269 if (!timerisset (ptr->timer))
272 if (ptr->ident != ident)
275 if ((ptr->sequence - 1) != seq)
278 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
279 ptr->hostname, ident, seq);
286 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
293 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
300 pinghost_t *host = NULL;
302 struct sockaddr_storage sa;
305 sa_len = sizeof (sa);
307 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
308 (struct sockaddr *) &sa, &sa_len);
309 if (buffer_len == -1)
311 dprintf ("recvfrom: %s\n", strerror (errno));
315 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
317 if (sa.ss_family == AF_INET)
319 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
322 else if (sa.ss_family == AF_INET6)
324 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
328 dprintf ("rcvd: %12i.%06i\n",
331 dprintf ("sent: %12i.%06i\n",
332 (int) host->timer->tv_sec,
333 (int) host->timer->tv_usec);
335 if (ping_timeval_sub (now, host->timer, &diff) < 0)
337 timerclear (host->timer);
341 dprintf ("diff: %12i.%06i\n",
345 host->latency = ((double) diff.tv_usec) / 1000.0;
346 host->latency += ((double) diff.tv_sec) * 1000.0;
348 timerclear (host->timer);
353 static int ping_receive_all (pinghost_t *ph)
361 struct timeval endtime;
362 struct timeval nowtime;
363 struct timeval timeout;
370 for (ptr = ph; ptr != NULL; ptr = ptr->next)
373 if (gettimeofday (&endtime, NULL) == -1)
383 for (ptr = ph; ptr != NULL; ptr = ptr->next)
385 if (!timerisset (ptr->timer))
388 FD_SET (ptr->fd, &readfds);
391 if (max_readfds < ptr->fd)
392 max_readfds = ptr->fd;
395 if (num_readfds == 0)
398 if (gettimeofday (&nowtime, NULL) == -1)
401 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
404 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
405 (int) timeout.tv_sec,
406 (int) timeout.tv_usec);
408 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
410 if (gettimeofday (&nowtime, NULL) == -1)
413 if ((status == -1) && (errno == EINTR))
415 dprintf ("select was interrupted by signal..\n");
420 dprintf ("select: %s\n", strerror (errno));
423 else if (status == 0)
425 dprintf ("select timed out\n");
429 for (ptr = ph; ptr != NULL; ptr = ptr->next)
431 if (FD_ISSET (ptr->fd, &readfds))
432 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
440 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
441 * Sending functions: *
444 * +-> ping_send_one_ipv4 *
445 * `-> ping_send_one_ipv6 *
446 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
447 ssize_t ping_sendto (pinghost_t *ph, const void *buf, size_t buflen)
451 if (gettimeofday (ph->timer, NULL) == -1)
453 timerclear (ph->timer);
457 ret = sendto (ph->fd, buf, buflen, 0,
458 (struct sockaddr *) ph->addr, ph->addrlen);
463 static int ping_send_one_ipv4 (pinghost_t *ph)
474 dprintf ("ph->hostname = %s\n", ph->hostname);
476 memset (buf, '\0', sizeof (buf));
477 icmp4 = (struct icmp *) buf;
478 data = (char *) (icmp4 + 1);
480 icmp4->icmp_type = ICMP_ECHO;
481 icmp4->icmp_code = 0;
482 icmp4->icmp_cksum = 0;
483 icmp4->icmp_id = htons (ph->ident);
484 icmp4->icmp_seq = htons (ph->sequence);
486 strcpy (data, PING_DATA);
487 datalen = strlen (data);
489 buflen = datalen + sizeof (struct icmp);
491 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
493 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
495 status = ping_sendto (ph, buf, buflen);
498 perror ("ping_sendto");
502 dprintf ("sendto: status = %i\n", status);
507 static int ping_send_one_ipv6 (pinghost_t *ph)
509 struct icmp6_hdr *icmp6;
518 dprintf ("ph->hostname = %s\n", ph->hostname);
520 memset (buf, '\0', sizeof (buf));
521 icmp6 = (struct icmp6_hdr *) buf;
522 data = (char *) (icmp6 + 1);
524 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
525 icmp6->icmp6_code = 0;
526 /* The checksum will be calculated by the TCP/IP stack. */
527 icmp6->icmp6_cksum = 0;
528 icmp6->icmp6_id = htons (ph->ident);
529 icmp6->icmp6_seq = htons (ph->sequence);
531 strcpy (data, PING_DATA);
532 datalen = strlen (data);
534 buflen = datalen + sizeof (struct icmp6_hdr);
536 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
538 status = ping_sendto (ph, buf, buflen);
541 perror ("ping_sendto");
545 dprintf ("sendto: status = %i\n", status);
550 static int ping_send_all (pinghost_t *ph)
554 for (ptr = ph; ptr != NULL; ptr = ptr->next)
556 /* start timer.. The GNU `ping6' starts the timer before
557 * sending the packet, so I will do that too */
558 if (gettimeofday (ptr->timer, NULL) == -1)
560 dprintf ("gettimeofday: %s\n", strerror (errno));
561 timerclear (ptr->timer);
566 dprintf ("timer set for hostname = %s\n", ptr->hostname);
569 if (ptr->addrfamily == AF_INET6)
571 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
572 if (ping_send_one_ipv6 (ptr) != 0)
574 timerclear (ptr->timer);
578 else if (ptr->addrfamily == AF_INET)
580 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
581 if (ping_send_one_ipv4 (ptr) != 0)
583 timerclear (ptr->timer);
587 else /* this should not happen */
589 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
590 timerclear (ptr->timer);
601 static int ping_get_ident (void)
604 static int did_seed = 0;
610 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
614 if (read (fd, &seed, sizeof (seed)) != -1)
617 dprintf ("Random seed: %i\n", seed);
625 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
629 retval = (int) random ();
631 dprintf ("Random number: %i\n", retval);
636 static pinghost_t *ping_alloc (void)
641 ph_size = sizeof (pinghost_t)
642 + sizeof (struct sockaddr_storage)
643 + sizeof (struct timeval);
645 ph = (pinghost_t *) malloc (ph_size);
649 memset (ph, '\0', ph_size);
651 ph->timer = (struct timeval *) (ph + 1);
652 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
654 ph->addrlen = sizeof (struct sockaddr_storage);
656 ph->ident = ping_get_ident () & 0xFFFF;
661 static void ping_free (pinghost_t *ph)
663 if (ph->hostname != NULL)
672 pingobj_t *ping_construct (void)
676 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
684 void ping_destroy (pingobj_t *obj)
692 while (current != NULL)
694 next = current->next;
704 int ping_setopt (pingobj_t *obj, int option, void *value)
710 case PING_OPT_TIMEOUT:
711 obj->timeout = *((double *) value);
712 if (obj->timeout < 0.0)
714 obj->timeout = PING_DEF_TIMEOUT;
720 obj->ttl = *((int *) value);
721 if ((obj->ttl < 1) || (obj->ttl > 255))
723 obj->ttl = PING_DEF_TTL;
729 obj->addrfamily = *((int *) value);
730 if ((obj->addrfamily != AF_UNSPEC)
731 && (obj->addrfamily != AF_INET)
732 && (obj->addrfamily != AF_INET6))
734 obj->addrfamily = PING_DEF_AF;
741 } /* switch (option) */
744 } /* int ping_setopt */
747 int ping_send (pingobj_t *obj)
751 if (ping_send_all (obj->head) < 0)
754 if ((ret = ping_receive_all (obj->head)) < 0)
760 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
764 if (strcasecmp (ph->hostname, host) == 0)
773 int ping_host_add (pingobj_t *obj, const char *host)
777 struct sockaddr_storage sockaddr;
778 socklen_t sockaddr_len;
780 struct addrinfo ai_hints;
781 struct addrinfo *ai_list, *ai_ptr;
784 dprintf ("host = %s\n", host);
786 if (ping_host_search (obj->head, host) != NULL)
789 memset (&ai_hints, '\0', sizeof (ai_hints));
790 ai_hints.ai_flags = 0;
792 ai_hints.ai_flags |= AI_ADDRCONFIG;
794 ai_hints.ai_family = PF_UNSPEC;
795 ai_hints.ai_socktype = SOCK_RAW;
797 if ((ph = ping_alloc ()) == NULL)
799 dprintf ("Out of memory!\n");
803 if ((ph->hostname = strdup (host)) == NULL)
805 dprintf ("Out of memory!\n");
810 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
812 dprintf ("getaddrinfo failed\n");
817 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
821 sockaddr_len = sizeof (sockaddr);
822 memset (&sockaddr, '\0', sockaddr_len);
824 if (ai_ptr->ai_family == AF_INET)
826 struct sockaddr_in *si;
828 si = (struct sockaddr_in *) &sockaddr;
829 si->sin_family = AF_INET;
830 si->sin_port = htons (ph->ident);
831 si->sin_addr.s_addr = htonl (INADDR_ANY);
833 ai_ptr->ai_protocol = IPPROTO_ICMP;
835 else if (ai_ptr->ai_family == AF_INET6)
837 struct sockaddr_in6 *si;
839 si = (struct sockaddr_in6 *) &sockaddr;
840 si->sin6_family = AF_INET6;
841 si->sin6_port = htons (ph->ident);
842 si->sin6_addr = in6addr_any;
844 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
848 dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
852 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
855 dprintf ("socket: %s\n", strerror (errno));
859 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
861 dprintf ("bind: %s\n", strerror (errno));
867 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
868 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
869 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
870 ph->addrlen = ai_ptr->ai_addrlen;
871 ph->addrfamily = ai_ptr->ai_family;
876 freeaddrinfo (ai_list);
885 ph->next = obj->head;
891 int ping_host_remove (pingobj_t *obj, const char *host)
893 pinghost_t *pre, *cur;
900 if (strcasecmp (host, cur->hostname))
911 obj->head = cur->next;
913 pre->next = cur->next;
923 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
925 return ((pingobj_iter_t *) obj->head);
928 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
930 return ((pingobj_iter_t *) iter->next);
933 const char *ping_iterator_get_host (pingobj_iter_t *iter)
935 return (iter->hostname);
938 double ping_iterator_get_latency (pingobj_iter_t *iter)
940 return (iter->latency);