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_add (struct timeval *tv1, struct timeval *tv2,
98 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
99 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
101 while (res->tv_usec > 1000000)
103 res->tv_usec -= 1000000;
110 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
114 if ((tv1->tv_sec < tv2->tv_sec)
115 || ((tv1->tv_sec == tv2->tv_sec)
116 && (tv1->tv_usec < tv2->tv_usec)))
119 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
120 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
122 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
124 while (res->tv_usec < 0)
126 res->tv_usec += 1000000;
133 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
140 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
145 *(char *) &ret = *(char *) ptr;
149 /* Do this twice to get all possible carries.. */
150 sum = (sum >> 16) + (sum & 0xFFFF);
151 sum = (sum >> 16) + (sum & 0xFFFF);
158 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
161 struct icmp *icmp_hdr;
165 uint16_t recv_checksum;
166 uint16_t calc_checksum;
173 if (buffer_len < sizeof (struct ip))
176 ip_hdr = (struct ip *) buffer;
177 ip_hdr_len = ip_hdr->ip_hl << 2;
179 if (buffer_len < ip_hdr_len)
182 buffer += ip_hdr_len;
183 buffer_len -= ip_hdr_len;
185 if (buffer_len < sizeof (struct icmp))
188 icmp_hdr = (struct icmp *) buffer;
189 buffer += sizeof (struct icmp);
190 buffer_len -= sizeof (struct icmp);
192 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
194 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
198 recv_checksum = icmp_hdr->icmp_cksum;
199 icmp_hdr->icmp_cksum = 0;
200 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
201 sizeof (struct icmp) + buffer_len);
203 if (recv_checksum != calc_checksum)
205 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
206 recv_checksum, calc_checksum);
210 ident = ntohs (icmp_hdr->icmp_id);
211 seq = ntohs (icmp_hdr->icmp_seq);
213 for (ptr = ph; ptr != NULL; ptr = ptr->next)
215 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
216 ptr->hostname, ptr->ident, ptr->sequence - 1);
218 if (ptr->addrfamily != AF_INET)
221 if (!timerisset (ptr->timer))
224 if (ptr->ident != ident)
227 if ((ptr->sequence - 1) != seq)
230 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
231 ptr->hostname, ident, seq);
238 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
245 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
247 struct icmp6_hdr *icmp_hdr;
254 if (buffer_len < sizeof (struct icmp6_hdr))
257 icmp_hdr = (struct icmp6_hdr *) buffer;
258 buffer += sizeof (struct icmp);
259 buffer_len -= sizeof (struct icmp);
261 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
263 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
267 if (icmp_hdr->icmp6_code != 0)
269 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
273 ident = ntohs (icmp_hdr->icmp6_id);
274 seq = ntohs (icmp_hdr->icmp6_seq);
276 for (ptr = ph; ptr != NULL; ptr = ptr->next)
278 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
279 ptr->hostname, ptr->ident, ptr->sequence - 1);
281 if (ptr->addrfamily != AF_INET6)
284 if (!timerisset (ptr->timer))
287 if (ptr->ident != ident)
290 if ((ptr->sequence - 1) != seq)
293 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
294 ptr->hostname, ident, seq);
301 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
308 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
315 pinghost_t *host = NULL;
317 struct sockaddr_storage sa;
320 sa_len = sizeof (sa);
322 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
323 (struct sockaddr *) &sa, &sa_len);
324 if (buffer_len == -1)
326 dprintf ("recvfrom: %s\n", strerror (errno));
330 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
332 if (sa.ss_family == AF_INET)
334 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
337 else if (sa.ss_family == AF_INET6)
339 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
343 dprintf ("rcvd: %12i.%06i\n",
346 dprintf ("sent: %12i.%06i\n",
347 (int) host->timer->tv_sec,
348 (int) host->timer->tv_usec);
350 if (ping_timeval_sub (now, host->timer, &diff) < 0)
352 timerclear (host->timer);
356 dprintf ("diff: %12i.%06i\n",
360 host->latency = ((double) diff.tv_usec) / 1000.0;
361 host->latency += ((double) diff.tv_sec) * 1000.0;
363 timerclear (host->timer);
368 static int ping_receive_all (pingobj_t *obj)
377 struct timeval endtime;
378 struct timeval nowtime;
379 struct timeval timeout;
387 for (ptr = ph; ptr != NULL; ptr = ptr->next)
390 if (gettimeofday (&nowtime, NULL) == -1)
394 timeout.tv_sec = (time_t) obj->timeout;
395 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
397 dprintf ("Set timeout to %i.%06i seconds\n",
398 (int) timeout.tv_sec,
399 (int) timeout.tv_usec);
401 ping_timeval_add (&nowtime, &timeout, &endtime);
409 for (ptr = ph; ptr != NULL; ptr = ptr->next)
411 if (!timerisset (ptr->timer))
414 FD_SET (ptr->fd, &readfds);
417 if (max_readfds < ptr->fd)
418 max_readfds = ptr->fd;
421 if (num_readfds == 0)
424 if (gettimeofday (&nowtime, NULL) == -1)
427 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
430 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
431 (int) timeout.tv_sec,
432 (int) timeout.tv_usec);
434 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
436 if (gettimeofday (&nowtime, NULL) == -1)
439 if ((status == -1) && (errno == EINTR))
441 dprintf ("select was interrupted by signal..\n");
446 dprintf ("select: %s\n", strerror (errno));
449 else if (status == 0)
451 dprintf ("select timed out\n");
455 for (ptr = ph; ptr != NULL; ptr = ptr->next)
457 if (FD_ISSET (ptr->fd, &readfds))
458 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
466 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
467 * Sending functions: *
470 * +-> ping_send_one_ipv4 *
471 * `-> ping_send_one_ipv6 *
472 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
473 ssize_t ping_sendto (pinghost_t *ph, const void *buf, size_t buflen)
477 if (gettimeofday (ph->timer, NULL) == -1)
479 timerclear (ph->timer);
483 ret = sendto (ph->fd, buf, buflen, 0,
484 (struct sockaddr *) ph->addr, ph->addrlen);
489 static int ping_send_one_ipv4 (pinghost_t *ph)
500 dprintf ("ph->hostname = %s\n", ph->hostname);
502 memset (buf, '\0', sizeof (buf));
503 icmp4 = (struct icmp *) buf;
504 data = (char *) (icmp4 + 1);
506 icmp4->icmp_type = ICMP_ECHO;
507 icmp4->icmp_code = 0;
508 icmp4->icmp_cksum = 0;
509 icmp4->icmp_id = htons (ph->ident);
510 icmp4->icmp_seq = htons (ph->sequence);
512 strcpy (data, PING_DATA);
513 datalen = strlen (data);
515 buflen = datalen + sizeof (struct icmp);
517 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
519 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
521 status = ping_sendto (ph, buf, buflen);
524 perror ("ping_sendto");
528 dprintf ("sendto: status = %i\n", status);
533 static int ping_send_one_ipv6 (pinghost_t *ph)
535 struct icmp6_hdr *icmp6;
544 dprintf ("ph->hostname = %s\n", ph->hostname);
546 memset (buf, '\0', sizeof (buf));
547 icmp6 = (struct icmp6_hdr *) buf;
548 data = (char *) (icmp6 + 1);
550 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
551 icmp6->icmp6_code = 0;
552 /* The checksum will be calculated by the TCP/IP stack. */
553 icmp6->icmp6_cksum = 0;
554 icmp6->icmp6_id = htons (ph->ident);
555 icmp6->icmp6_seq = htons (ph->sequence);
557 strcpy (data, PING_DATA);
558 datalen = strlen (data);
560 buflen = datalen + sizeof (struct icmp6_hdr);
562 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
564 status = ping_sendto (ph, buf, buflen);
567 perror ("ping_sendto");
571 dprintf ("sendto: status = %i\n", status);
576 static int ping_send_all (pinghost_t *ph)
580 for (ptr = ph; ptr != NULL; ptr = ptr->next)
582 /* start timer.. The GNU `ping6' starts the timer before
583 * sending the packet, so I will do that too */
584 if (gettimeofday (ptr->timer, NULL) == -1)
586 dprintf ("gettimeofday: %s\n", strerror (errno));
587 timerclear (ptr->timer);
592 dprintf ("timer set for hostname = %s\n", ptr->hostname);
595 if (ptr->addrfamily == AF_INET6)
597 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
598 if (ping_send_one_ipv6 (ptr) != 0)
600 timerclear (ptr->timer);
604 else if (ptr->addrfamily == AF_INET)
606 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
607 if (ping_send_one_ipv4 (ptr) != 0)
609 timerclear (ptr->timer);
613 else /* this should not happen */
615 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
616 timerclear (ptr->timer);
628 * Set the TTL of a socket protocol independently.
630 static int ping_set_ttl (pinghost_t *ph, int ttl)
634 if (ph->addrfamily == AF_INET)
636 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
638 else if (ph->addrfamily == AF_INET6)
640 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
646 static int ping_get_ident (void)
649 static int did_seed = 0;
655 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
659 if (read (fd, &seed, sizeof (seed)) != -1)
662 dprintf ("Random seed: %i\n", seed);
670 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
674 retval = (int) random ();
676 dprintf ("Random number: %i\n", retval);
681 static pinghost_t *ping_alloc (void)
686 ph_size = sizeof (pinghost_t)
687 + sizeof (struct sockaddr_storage)
688 + sizeof (struct timeval);
690 ph = (pinghost_t *) malloc (ph_size);
694 memset (ph, '\0', ph_size);
696 ph->timer = (struct timeval *) (ph + 1);
697 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
699 ph->addrlen = sizeof (struct sockaddr_storage);
701 ph->ident = ping_get_ident () & 0xFFFF;
706 static void ping_free (pinghost_t *ph)
708 if (ph->hostname != NULL)
717 pingobj_t *ping_construct (void)
721 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
729 void ping_destroy (pingobj_t *obj)
737 while (current != NULL)
739 next = current->next;
749 int ping_setopt (pingobj_t *obj, int option, void *value)
755 case PING_OPT_TIMEOUT:
756 obj->timeout = *((double *) value);
757 if (obj->timeout < 0.0)
759 obj->timeout = PING_DEF_TIMEOUT;
765 obj->ttl = *((int *) value);
766 if ((obj->ttl < 1) || (obj->ttl > 255))
768 obj->ttl = PING_DEF_TTL;
774 obj->addrfamily = *((int *) value);
775 if ((obj->addrfamily != AF_UNSPEC)
776 && (obj->addrfamily != AF_INET)
777 && (obj->addrfamily != AF_INET6))
779 obj->addrfamily = PING_DEF_AF;
786 } /* switch (option) */
789 } /* int ping_setopt */
792 int ping_send (pingobj_t *obj)
796 if (ping_send_all (obj->head) < 0)
799 if ((ret = ping_receive_all (obj)) < 0)
805 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
809 if (strcasecmp (ph->hostname, host) == 0)
818 int ping_host_add (pingobj_t *obj, const char *host)
822 struct sockaddr_storage sockaddr;
823 socklen_t sockaddr_len;
825 struct addrinfo ai_hints;
826 struct addrinfo *ai_list, *ai_ptr;
829 dprintf ("host = %s\n", host);
831 if (ping_host_search (obj->head, host) != NULL)
834 memset (&ai_hints, '\0', sizeof (ai_hints));
835 ai_hints.ai_flags = 0;
837 ai_hints.ai_flags |= AI_ADDRCONFIG;
839 ai_hints.ai_family = obj->addrfamily;
840 ai_hints.ai_socktype = SOCK_RAW;
842 if ((ph = ping_alloc ()) == NULL)
844 dprintf ("Out of memory!\n");
848 if ((ph->hostname = strdup (host)) == NULL)
850 dprintf ("Out of memory!\n");
855 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
857 dprintf ("getaddrinfo failed\n");
862 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
866 sockaddr_len = sizeof (sockaddr);
867 memset (&sockaddr, '\0', sockaddr_len);
869 if (ai_ptr->ai_family == AF_INET)
871 struct sockaddr_in *si;
873 si = (struct sockaddr_in *) &sockaddr;
874 si->sin_family = AF_INET;
875 si->sin_port = htons (ph->ident);
876 si->sin_addr.s_addr = htonl (INADDR_ANY);
878 ai_ptr->ai_protocol = IPPROTO_ICMP;
880 else if (ai_ptr->ai_family == AF_INET6)
882 struct sockaddr_in6 *si;
884 si = (struct sockaddr_in6 *) &sockaddr;
885 si->sin6_family = AF_INET6;
886 si->sin6_port = htons (ph->ident);
887 si->sin6_addr = in6addr_any;
889 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
893 dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
897 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
900 dprintf ("socket: %s\n", strerror (errno));
904 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
906 dprintf ("bind: %s\n", strerror (errno));
912 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
913 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
914 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
915 ph->addrlen = ai_ptr->ai_addrlen;
916 ph->addrfamily = ai_ptr->ai_family;
921 freeaddrinfo (ai_list);
930 ph->next = obj->head;
933 ping_set_ttl (ph, obj->ttl);
938 int ping_host_remove (pingobj_t *obj, const char *host)
940 pinghost_t *pre, *cur;
947 if (strcasecmp (host, cur->hostname))
958 obj->head = cur->next;
960 pre->next = cur->next;
970 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
972 return ((pingobj_iter_t *) obj->head);
975 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
977 return ((pingobj_iter_t *) iter->next);
980 const char *ping_iterator_get_host (pingobj_iter_t *iter)
982 return (iter->hostname);
985 double ping_iterator_get_latency (pingobj_iter_t *iter)
987 return (iter->latency);