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 */
46 # include <sys/types.h>
49 # include <sys/stat.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
64 # include <sys/socket.h>
70 #if HAVE_NETINET_IN_SYSTM_H
71 # include <netinet/in_systm.h>
74 # include <netinet/in.h>
77 # include <netinet/ip.h>
79 #if HAVE_NETINET_IP_ICMP_H
80 # include <netinet/ip_icmp.h>
82 #ifdef HAVE_NETINET_IP_VAR_H
83 # include <netinet/ip_var.h>
85 #if HAVE_NETINET_IP6_H
86 # include <netinet/ip6.h>
88 #if HAVE_NETINET_ICMP6_H
89 # include <netinet/icmp6.h>
95 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
97 # define dprintf(...) /**/
100 #define PING_ERRMSG_LEN 256
102 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
107 struct sockaddr_storage *addr;
113 struct timeval *timer;
118 struct pinghost *next;
127 char errmsg[PING_ERRMSG_LEN];
133 * private (static) functions
135 static void ping_set_error (pingobj_t *obj, const char *function,
138 snprintf (obj->errmsg, PING_ERRMSG_LEN, "%s: %s", function, message);
139 obj->errmsg[PING_ERRMSG_LEN - 1] = '\0';
142 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
145 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
146 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
148 while (res->tv_usec > 1000000)
150 res->tv_usec -= 1000000;
157 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
161 if ((tv1->tv_sec < tv2->tv_sec)
162 || ((tv1->tv_sec == tv2->tv_sec)
163 && (tv1->tv_usec < tv2->tv_usec)))
166 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
167 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
169 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
171 while (res->tv_usec < 0)
173 res->tv_usec += 1000000;
180 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
187 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
192 *(char *) &ret = *(char *) ptr;
196 /* Do this twice to get all possible carries.. */
197 sum = (sum >> 16) + (sum & 0xFFFF);
198 sum = (sum >> 16) + (sum & 0xFFFF);
205 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
208 struct icmp *icmp_hdr;
212 uint16_t recv_checksum;
213 uint16_t calc_checksum;
220 if (buffer_len < sizeof (struct ip))
223 ip_hdr = (struct ip *) buffer;
224 ip_hdr_len = ip_hdr->ip_hl << 2;
226 if (buffer_len < ip_hdr_len)
229 buffer += ip_hdr_len;
230 buffer_len -= ip_hdr_len;
232 if (buffer_len < sizeof (struct icmp))
235 icmp_hdr = (struct icmp *) buffer;
236 buffer += sizeof (struct icmp);
237 buffer_len -= sizeof (struct icmp);
239 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
241 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
245 recv_checksum = icmp_hdr->icmp_cksum;
246 icmp_hdr->icmp_cksum = 0;
247 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
248 sizeof (struct icmp) + buffer_len);
250 if (recv_checksum != calc_checksum)
252 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
253 recv_checksum, calc_checksum);
257 ident = ntohs (icmp_hdr->icmp_id);
258 seq = ntohs (icmp_hdr->icmp_seq);
260 for (ptr = ph; ptr != NULL; ptr = ptr->next)
262 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
263 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
265 if (ptr->addrfamily != AF_INET)
268 if (!timerisset (ptr->timer))
271 if (ptr->ident != ident)
274 if (((ptr->sequence - 1) & 0xFFFF) != seq)
277 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
278 ptr->hostname, ident, seq);
285 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
292 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
294 struct icmp6_hdr *icmp_hdr;
301 if (buffer_len < sizeof (struct icmp6_hdr))
304 icmp_hdr = (struct icmp6_hdr *) buffer;
305 buffer += sizeof (struct icmp);
306 buffer_len -= sizeof (struct icmp);
308 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
310 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
314 if (icmp_hdr->icmp6_code != 0)
316 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
320 ident = ntohs (icmp_hdr->icmp6_id);
321 seq = ntohs (icmp_hdr->icmp6_seq);
323 for (ptr = ph; ptr != NULL; ptr = ptr->next)
325 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
326 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
328 if (ptr->addrfamily != AF_INET6)
331 if (!timerisset (ptr->timer))
334 if (ptr->ident != ident)
337 if (((ptr->sequence - 1) & 0xFFFF) != seq)
340 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
341 ptr->hostname, ident, seq);
348 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
355 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
362 pinghost_t *host = NULL;
364 struct sockaddr_storage sa;
367 sa_len = sizeof (sa);
369 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
370 (struct sockaddr *) &sa, &sa_len);
371 if (buffer_len == -1)
373 dprintf ("recvfrom: %s\n", strerror (errno));
377 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
379 if (sa.ss_family == AF_INET)
381 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
384 else if (sa.ss_family == AF_INET6)
386 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
390 dprintf ("rcvd: %12i.%06i\n",
393 dprintf ("sent: %12i.%06i\n",
394 (int) host->timer->tv_sec,
395 (int) host->timer->tv_usec);
397 if (ping_timeval_sub (now, host->timer, &diff) < 0)
399 timerclear (host->timer);
403 dprintf ("diff: %12i.%06i\n",
407 host->latency = ((double) diff.tv_usec) / 1000.0;
408 host->latency += ((double) diff.tv_sec) * 1000.0;
410 timerclear (host->timer);
415 static int ping_receive_all (pingobj_t *obj)
424 struct timeval endtime;
425 struct timeval nowtime;
426 struct timeval timeout;
434 for (ptr = ph; ptr != NULL; ptr = ptr->next)
437 if (gettimeofday (&nowtime, NULL) == -1)
439 ping_set_error (obj, "gettimeofday", strerror (errno));
444 timeout.tv_sec = (time_t) obj->timeout;
445 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
447 dprintf ("Set timeout to %i.%06i seconds\n",
448 (int) timeout.tv_sec,
449 (int) timeout.tv_usec);
451 ping_timeval_add (&nowtime, &timeout, &endtime);
459 for (ptr = ph; ptr != NULL; ptr = ptr->next)
461 if (!timerisset (ptr->timer))
464 FD_SET (ptr->fd, &readfds);
467 if (max_readfds < ptr->fd)
468 max_readfds = ptr->fd;
471 if (num_readfds == 0)
474 if (gettimeofday (&nowtime, NULL) == -1)
476 ping_set_error (obj, "gettimeofday", strerror (errno));
480 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
483 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
484 (int) timeout.tv_sec,
485 (int) timeout.tv_usec);
487 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
489 if (gettimeofday (&nowtime, NULL) == -1)
491 ping_set_error (obj, "gettimeofday", strerror (errno));
495 if ((status == -1) && (errno == EINTR))
497 dprintf ("select was interrupted by signal..\n");
502 dprintf ("select: %s\n", strerror (errno));
505 else if (status == 0)
507 dprintf ("select timed out\n");
511 for (ptr = ph; ptr != NULL; ptr = ptr->next)
513 if (FD_ISSET (ptr->fd, &readfds))
514 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
522 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
523 * Sending functions: *
526 * +-> ping_send_one_ipv4 *
527 * `-> ping_send_one_ipv6 *
528 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
529 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
530 const void *buf, size_t buflen)
534 if (gettimeofday (ph->timer, NULL) == -1)
536 timerclear (ph->timer);
540 ret = sendto (ph->fd, buf, buflen, 0,
541 (struct sockaddr *) ph->addr, ph->addrlen);
544 ping_set_error (obj, "sendto", strerror (errno));
549 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
560 dprintf ("ph->hostname = %s\n", ph->hostname);
562 memset (buf, '\0', sizeof (buf));
563 icmp4 = (struct icmp *) buf;
564 data = (char *) (icmp4 + 1);
566 icmp4->icmp_type = ICMP_ECHO;
567 icmp4->icmp_code = 0;
568 icmp4->icmp_cksum = 0;
569 icmp4->icmp_id = htons (ph->ident);
570 icmp4->icmp_seq = htons (ph->sequence);
572 strcpy (data, PING_DATA);
573 datalen = strlen (data);
575 buflen = datalen + sizeof (struct icmp);
577 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
579 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
581 status = ping_sendto (obj, ph, buf, buflen);
584 perror ("ping_sendto");
588 dprintf ("sendto: status = %i\n", status);
593 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
595 struct icmp6_hdr *icmp6;
604 dprintf ("ph->hostname = %s\n", ph->hostname);
606 memset (buf, '\0', sizeof (buf));
607 icmp6 = (struct icmp6_hdr *) buf;
608 data = (char *) (icmp6 + 1);
610 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
611 icmp6->icmp6_code = 0;
612 /* The checksum will be calculated by the TCP/IP stack. */
613 icmp6->icmp6_cksum = 0;
614 icmp6->icmp6_id = htons (ph->ident);
615 icmp6->icmp6_seq = htons (ph->sequence);
617 strcpy (data, PING_DATA);
618 datalen = strlen (data);
620 buflen = datalen + sizeof (struct icmp6_hdr);
622 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
624 status = ping_sendto (obj, ph, buf, buflen);
627 perror ("ping_sendto");
631 dprintf ("sendto: status = %i\n", status);
636 static int ping_send_all (pingobj_t *obj)
646 for (ptr = ph; ptr != NULL; ptr = ptr->next)
648 /* start timer.. The GNU `ping6' starts the timer before
649 * sending the packet, so I will do that too */
650 if (gettimeofday (ptr->timer, NULL) == -1)
652 dprintf ("gettimeofday: %s\n", strerror (errno));
653 timerclear (ptr->timer);
659 dprintf ("timer set for hostname = %s\n", ptr->hostname);
662 if (ptr->addrfamily == AF_INET6)
664 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
665 if (ping_send_one_ipv6 (obj, ptr) != 0)
667 timerclear (ptr->timer);
672 else if (ptr->addrfamily == AF_INET)
674 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
675 if (ping_send_one_ipv4 (obj, ptr) != 0)
677 timerclear (ptr->timer);
682 else /* this should not happen */
684 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
685 timerclear (ptr->timer);
697 * Set the TTL of a socket protocol independently.
699 static int ping_set_ttl (pinghost_t *ph, int ttl)
703 if (ph->addrfamily == AF_INET)
705 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
707 else if (ph->addrfamily == AF_INET6)
709 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
715 static int ping_get_ident (void)
718 static int did_seed = 0;
724 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
728 if (read (fd, &seed, sizeof (seed)) != -1)
731 dprintf ("Random seed: %i\n", seed);
739 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
743 retval = (int) random ();
745 dprintf ("Random number: %i\n", retval);
750 static pinghost_t *ping_alloc (void)
755 ph_size = sizeof (pinghost_t)
756 + sizeof (struct sockaddr_storage)
757 + sizeof (struct timeval);
759 ph = (pinghost_t *) malloc (ph_size);
763 memset (ph, '\0', ph_size);
765 ph->timer = (struct timeval *) (ph + 1);
766 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
768 ph->addrlen = sizeof (struct sockaddr_storage);
770 ph->ident = ping_get_ident () & 0xFFFF;
775 static void ping_free (pinghost_t *ph)
777 if (ph->hostname != NULL)
786 const char *ping_get_error (pingobj_t *obj)
788 return (obj->errmsg);
791 pingobj_t *ping_construct (void)
795 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
797 memset (obj, '\0', sizeof (pingobj_t));
799 obj->timeout = PING_DEF_TIMEOUT;
800 obj->ttl = PING_DEF_TTL;
801 obj->addrfamily = PING_DEF_AF;
806 void ping_destroy (pingobj_t *obj)
814 while (current != NULL)
816 next = current->next;
826 int ping_setopt (pingobj_t *obj, int option, void *value)
832 case PING_OPT_TIMEOUT:
833 obj->timeout = *((double *) value);
834 if (obj->timeout < 0.0)
836 obj->timeout = PING_DEF_TIMEOUT;
842 obj->ttl = *((int *) value);
843 if ((obj->ttl < 1) || (obj->ttl > 255))
845 obj->ttl = PING_DEF_TTL;
851 obj->addrfamily = *((int *) value);
852 if ((obj->addrfamily != AF_UNSPEC)
853 && (obj->addrfamily != AF_INET)
854 && (obj->addrfamily != AF_INET6))
856 obj->addrfamily = PING_DEF_AF;
863 } /* switch (option) */
866 } /* int ping_setopt */
869 int ping_send (pingobj_t *obj)
873 if (ping_send_all (obj) < 0)
876 if ((ret = ping_receive_all (obj)) < 0)
882 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
886 if (strcasecmp (ph->hostname, host) == 0)
895 int ping_host_add (pingobj_t *obj, const char *host)
899 struct sockaddr_storage sockaddr;
900 socklen_t sockaddr_len;
902 struct addrinfo ai_hints;
903 struct addrinfo *ai_list, *ai_ptr;
906 dprintf ("host = %s\n", host);
908 if (ping_host_search (obj->head, host) != NULL)
911 memset (&ai_hints, '\0', sizeof (ai_hints));
912 ai_hints.ai_flags = 0;
914 ai_hints.ai_flags |= AI_ADDRCONFIG;
916 ai_hints.ai_family = obj->addrfamily;
917 ai_hints.ai_socktype = SOCK_RAW;
919 if ((ph = ping_alloc ()) == NULL)
921 dprintf ("Out of memory!\n");
925 if ((ph->hostname = strdup (host)) == NULL)
927 dprintf ("Out of memory!\n");
928 ping_set_error (obj, "strdup", strerror (errno));
933 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
935 dprintf ("getaddrinfo failed\n");
936 ping_set_error (obj, "getaddrinfo",
937 (ai_return == EAI_SYSTEM)
939 : gai_strerror (ai_return));
945 ping_set_error (obj, "getaddrinfo", "No hosts returned");
947 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
951 sockaddr_len = sizeof (sockaddr);
952 memset (&sockaddr, '\0', sockaddr_len);
954 if (ai_ptr->ai_family == AF_INET)
956 struct sockaddr_in *si;
958 si = (struct sockaddr_in *) &sockaddr;
959 si->sin_family = AF_INET;
960 si->sin_port = htons (ph->ident);
961 si->sin_addr.s_addr = htonl (INADDR_ANY);
963 ai_ptr->ai_socktype = SOCK_RAW;
964 ai_ptr->ai_protocol = IPPROTO_ICMP;
966 else if (ai_ptr->ai_family == AF_INET6)
968 struct sockaddr_in6 *si;
970 si = (struct sockaddr_in6 *) &sockaddr;
971 si->sin6_family = AF_INET6;
972 si->sin6_port = htons (ph->ident);
973 si->sin6_addr = in6addr_any;
975 ai_ptr->ai_socktype = SOCK_RAW;
976 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
980 char errmsg[PING_ERRMSG_LEN];
982 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
983 errmsg[PING_ERRMSG_LEN - 1] = '\0';
986 ping_set_error (obj, "getaddrinfo", errmsg);
990 /* TODO: Move this to a static function `ping_open_socket' and
991 * call it whenever the socket dies. */
992 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
995 dprintf ("socket: %s\n", strerror (errno));
996 ping_set_error (obj, "socket", strerror (errno));
1000 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
1002 dprintf ("bind: %s\n", strerror (errno));
1003 ping_set_error (obj, "bind", strerror (errno));
1009 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1010 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1011 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1012 ph->addrlen = ai_ptr->ai_addrlen;
1013 ph->addrfamily = ai_ptr->ai_family;
1018 freeaddrinfo (ai_list);
1022 free (ph->hostname);
1027 ph->next = obj->head;
1030 ping_set_ttl (ph, obj->ttl);
1035 int ping_host_remove (pingobj_t *obj, const char *host)
1037 pinghost_t *pre, *cur;
1044 if (strcasecmp (host, cur->hostname))
1053 ping_set_error (obj, "ping_host_remove", "Host not found");
1058 obj->head = cur->next;
1060 pre->next = cur->next;
1070 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1072 return ((pingobj_iter_t *) obj->head);
1075 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1077 return ((pingobj_iter_t *) iter->next);
1080 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1081 void *buffer, size_t *buffer_len)
1085 size_t orig_buffer_len = *buffer_len;
1089 case PING_INFO_HOSTNAME:
1091 *buffer_len = strlen (iter->hostname);
1092 if (orig_buffer_len <= *buffer_len)
1094 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1095 * will copy `*buffer_len' and pad the rest of
1096 * `buffer' with null-bytes */
1097 strncpy (buffer, iter->hostname, orig_buffer_len);
1101 case PING_INFO_ADDRESS:
1102 ret = getnameinfo ((struct sockaddr *) iter->addr,
1110 if ((ret == EAI_MEMORY)
1112 || (ret == EAI_OVERFLOW)
1116 else if (ret == EAI_SYSTEM)
1117 /* XXX: Not thread-safe! */
1124 case PING_INFO_FAMILY:
1126 *buffer_len = sizeof (int);
1127 if (orig_buffer_len < sizeof (int))
1129 *((int *) buffer) = iter->addrfamily;
1133 case PING_INFO_LATENCY:
1135 *buffer_len = sizeof (double);
1136 if (orig_buffer_len < sizeof (double))
1138 *((double *) buffer) = iter->latency;
1142 /* FIXME Return the sequence as an unsigned int */
1143 case PING_INFO_SEQUENCE:
1145 *buffer_len = sizeof (uint16_t);
1146 if (orig_buffer_len < sizeof (uint16_t))
1148 *((uint16_t *) buffer) = (uint16_t) iter->sequence;
1152 case PING_INFO_IDENT:
1154 *buffer_len = sizeof (uint16_t);
1155 if (orig_buffer_len < sizeof (uint16_t))
1157 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1165 void *ping_iterator_get_context (pingobj_iter_t *iter)
1167 return (iter->context);
1170 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1172 iter->context = context;