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_ERRMSG_LEN 256
92 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
97 struct sockaddr_storage *addr;
103 struct timeval *timer;
106 struct pinghost *next;
115 char errmsg[PING_ERRMSG_LEN];
121 * private (static) functions
123 static void ping_set_error (pingobj_t *obj, const char *function,
126 snprintf (obj->errmsg, PING_ERRMSG_LEN, "%s: %s", function, message);
127 obj->errmsg[PING_ERRMSG_LEN - 1] = '\0';
130 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
133 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
134 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
136 while (res->tv_usec > 1000000)
138 res->tv_usec -= 1000000;
145 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
149 if ((tv1->tv_sec < tv2->tv_sec)
150 || ((tv1->tv_sec == tv2->tv_sec)
151 && (tv1->tv_usec < tv2->tv_usec)))
154 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
155 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
157 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
159 while (res->tv_usec < 0)
161 res->tv_usec += 1000000;
168 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
175 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
180 *(char *) &ret = *(char *) ptr;
184 /* Do this twice to get all possible carries.. */
185 sum = (sum >> 16) + (sum & 0xFFFF);
186 sum = (sum >> 16) + (sum & 0xFFFF);
193 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
196 struct icmp *icmp_hdr;
200 uint16_t recv_checksum;
201 uint16_t calc_checksum;
208 if (buffer_len < sizeof (struct ip))
211 ip_hdr = (struct ip *) buffer;
212 ip_hdr_len = ip_hdr->ip_hl << 2;
214 if (buffer_len < ip_hdr_len)
217 buffer += ip_hdr_len;
218 buffer_len -= ip_hdr_len;
220 if (buffer_len < sizeof (struct icmp))
223 icmp_hdr = (struct icmp *) buffer;
224 buffer += sizeof (struct icmp);
225 buffer_len -= sizeof (struct icmp);
227 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
229 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
233 recv_checksum = icmp_hdr->icmp_cksum;
234 icmp_hdr->icmp_cksum = 0;
235 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
236 sizeof (struct icmp) + buffer_len);
238 if (recv_checksum != calc_checksum)
240 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
241 recv_checksum, calc_checksum);
245 ident = ntohs (icmp_hdr->icmp_id);
246 seq = ntohs (icmp_hdr->icmp_seq);
248 for (ptr = ph; ptr != NULL; ptr = ptr->next)
250 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
251 ptr->hostname, ptr->ident, ptr->sequence - 1);
253 if (ptr->addrfamily != AF_INET)
256 if (!timerisset (ptr->timer))
259 if (ptr->ident != ident)
262 if ((ptr->sequence - 1) != seq)
265 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
266 ptr->hostname, ident, seq);
273 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
280 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
282 struct icmp6_hdr *icmp_hdr;
289 if (buffer_len < sizeof (struct icmp6_hdr))
292 icmp_hdr = (struct icmp6_hdr *) buffer;
293 buffer += sizeof (struct icmp);
294 buffer_len -= sizeof (struct icmp);
296 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
298 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
302 if (icmp_hdr->icmp6_code != 0)
304 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
308 ident = ntohs (icmp_hdr->icmp6_id);
309 seq = ntohs (icmp_hdr->icmp6_seq);
311 for (ptr = ph; ptr != NULL; ptr = ptr->next)
313 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
314 ptr->hostname, ptr->ident, ptr->sequence - 1);
316 if (ptr->addrfamily != AF_INET6)
319 if (!timerisset (ptr->timer))
322 if (ptr->ident != ident)
325 if ((ptr->sequence - 1) != seq)
328 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
329 ptr->hostname, ident, seq);
336 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
343 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
350 pinghost_t *host = NULL;
352 struct sockaddr_storage sa;
355 sa_len = sizeof (sa);
357 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
358 (struct sockaddr *) &sa, &sa_len);
359 if (buffer_len == -1)
361 dprintf ("recvfrom: %s\n", strerror (errno));
365 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
367 if (sa.ss_family == AF_INET)
369 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
372 else if (sa.ss_family == AF_INET6)
374 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
378 dprintf ("rcvd: %12i.%06i\n",
381 dprintf ("sent: %12i.%06i\n",
382 (int) host->timer->tv_sec,
383 (int) host->timer->tv_usec);
385 if (ping_timeval_sub (now, host->timer, &diff) < 0)
387 timerclear (host->timer);
391 dprintf ("diff: %12i.%06i\n",
395 host->latency = ((double) diff.tv_usec) / 1000.0;
396 host->latency += ((double) diff.tv_sec) * 1000.0;
398 timerclear (host->timer);
403 static int ping_receive_all (pingobj_t *obj)
412 struct timeval endtime;
413 struct timeval nowtime;
414 struct timeval timeout;
422 for (ptr = ph; ptr != NULL; ptr = ptr->next)
425 if (gettimeofday (&nowtime, NULL) == -1)
427 ping_set_error (obj, "gettimeofday", strerror (errno));
432 timeout.tv_sec = (time_t) obj->timeout;
433 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
435 dprintf ("Set timeout to %i.%06i seconds\n",
436 (int) timeout.tv_sec,
437 (int) timeout.tv_usec);
439 ping_timeval_add (&nowtime, &timeout, &endtime);
447 for (ptr = ph; ptr != NULL; ptr = ptr->next)
449 if (!timerisset (ptr->timer))
452 FD_SET (ptr->fd, &readfds);
455 if (max_readfds < ptr->fd)
456 max_readfds = ptr->fd;
459 if (num_readfds == 0)
462 if (gettimeofday (&nowtime, NULL) == -1)
464 ping_set_error (obj, "gettimeofday", strerror (errno));
468 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
471 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
472 (int) timeout.tv_sec,
473 (int) timeout.tv_usec);
475 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
477 if (gettimeofday (&nowtime, NULL) == -1)
479 ping_set_error (obj, "gettimeofday", strerror (errno));
483 if ((status == -1) && (errno == EINTR))
485 dprintf ("select was interrupted by signal..\n");
490 dprintf ("select: %s\n", strerror (errno));
493 else if (status == 0)
495 dprintf ("select timed out\n");
499 for (ptr = ph; ptr != NULL; ptr = ptr->next)
501 if (FD_ISSET (ptr->fd, &readfds))
502 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
510 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
511 * Sending functions: *
514 * +-> ping_send_one_ipv4 *
515 * `-> ping_send_one_ipv6 *
516 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
517 static ssize_t ping_sendto (pinghost_t *ph, const void *buf, size_t buflen)
521 if (gettimeofday (ph->timer, NULL) == -1)
523 timerclear (ph->timer);
527 ret = sendto (ph->fd, buf, buflen, 0,
528 (struct sockaddr *) ph->addr, ph->addrlen);
533 static int ping_send_one_ipv4 (pinghost_t *ph)
544 dprintf ("ph->hostname = %s\n", ph->hostname);
546 memset (buf, '\0', sizeof (buf));
547 icmp4 = (struct icmp *) buf;
548 data = (char *) (icmp4 + 1);
550 icmp4->icmp_type = ICMP_ECHO;
551 icmp4->icmp_code = 0;
552 icmp4->icmp_cksum = 0;
553 icmp4->icmp_id = htons (ph->ident);
554 icmp4->icmp_seq = htons (ph->sequence);
556 strcpy (data, PING_DATA);
557 datalen = strlen (data);
559 buflen = datalen + sizeof (struct icmp);
561 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
563 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
565 status = ping_sendto (ph, buf, buflen);
568 perror ("ping_sendto");
572 dprintf ("sendto: status = %i\n", status);
577 static int ping_send_one_ipv6 (pinghost_t *ph)
579 struct icmp6_hdr *icmp6;
588 dprintf ("ph->hostname = %s\n", ph->hostname);
590 memset (buf, '\0', sizeof (buf));
591 icmp6 = (struct icmp6_hdr *) buf;
592 data = (char *) (icmp6 + 1);
594 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
595 icmp6->icmp6_code = 0;
596 /* The checksum will be calculated by the TCP/IP stack. */
597 icmp6->icmp6_cksum = 0;
598 icmp6->icmp6_id = htons (ph->ident);
599 icmp6->icmp6_seq = htons (ph->sequence);
601 strcpy (data, PING_DATA);
602 datalen = strlen (data);
604 buflen = datalen + sizeof (struct icmp6_hdr);
606 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
608 status = ping_sendto (ph, buf, buflen);
611 perror ("ping_sendto");
615 dprintf ("sendto: status = %i\n", status);
620 static int ping_send_all (pinghost_t *ph)
624 for (ptr = ph; ptr != NULL; ptr = ptr->next)
626 /* start timer.. The GNU `ping6' starts the timer before
627 * sending the packet, so I will do that too */
628 if (gettimeofday (ptr->timer, NULL) == -1)
630 dprintf ("gettimeofday: %s\n", strerror (errno));
631 timerclear (ptr->timer);
636 dprintf ("timer set for hostname = %s\n", ptr->hostname);
639 if (ptr->addrfamily == AF_INET6)
641 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
642 if (ping_send_one_ipv6 (ptr) != 0)
644 timerclear (ptr->timer);
648 else if (ptr->addrfamily == AF_INET)
650 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
651 if (ping_send_one_ipv4 (ptr) != 0)
653 timerclear (ptr->timer);
657 else /* this should not happen */
659 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
660 timerclear (ptr->timer);
672 * Set the TTL of a socket protocol independently.
674 static int ping_set_ttl (pinghost_t *ph, int ttl)
678 if (ph->addrfamily == AF_INET)
680 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
682 else if (ph->addrfamily == AF_INET6)
684 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
690 static int ping_get_ident (void)
693 static int did_seed = 0;
699 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
703 if (read (fd, &seed, sizeof (seed)) != -1)
706 dprintf ("Random seed: %i\n", seed);
714 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
718 retval = (int) random ();
720 dprintf ("Random number: %i\n", retval);
725 static pinghost_t *ping_alloc (void)
730 ph_size = sizeof (pinghost_t)
731 + sizeof (struct sockaddr_storage)
732 + sizeof (struct timeval);
734 ph = (pinghost_t *) malloc (ph_size);
738 memset (ph, '\0', ph_size);
740 ph->timer = (struct timeval *) (ph + 1);
741 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
743 ph->addrlen = sizeof (struct sockaddr_storage);
745 ph->ident = ping_get_ident () & 0xFFFF;
750 static void ping_free (pinghost_t *ph)
752 if (ph->hostname != NULL)
761 const char *ping_get_error (pingobj_t *obj)
763 return (obj->errmsg);
766 pingobj_t *ping_construct (void)
770 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
772 memset (obj, '\0', sizeof (pingobj_t));
774 obj->timeout = PING_DEF_TIMEOUT;
775 obj->ttl = PING_DEF_TTL;
776 obj->addrfamily = PING_DEF_AF;
781 void ping_destroy (pingobj_t *obj)
789 while (current != NULL)
791 next = current->next;
801 int ping_setopt (pingobj_t *obj, int option, void *value)
807 case PING_OPT_TIMEOUT:
808 obj->timeout = *((double *) value);
809 if (obj->timeout < 0.0)
811 obj->timeout = PING_DEF_TIMEOUT;
817 obj->ttl = *((int *) value);
818 if ((obj->ttl < 1) || (obj->ttl > 255))
820 obj->ttl = PING_DEF_TTL;
826 obj->addrfamily = *((int *) value);
827 if ((obj->addrfamily != AF_UNSPEC)
828 && (obj->addrfamily != AF_INET)
829 && (obj->addrfamily != AF_INET6))
831 obj->addrfamily = PING_DEF_AF;
838 } /* switch (option) */
841 } /* int ping_setopt */
844 int ping_send (pingobj_t *obj)
848 if (ping_send_all (obj->head) < 0)
851 if ((ret = ping_receive_all (obj)) < 0)
857 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
861 if (strcasecmp (ph->hostname, host) == 0)
870 int ping_host_add (pingobj_t *obj, const char *host)
874 struct sockaddr_storage sockaddr;
875 socklen_t sockaddr_len;
877 struct addrinfo ai_hints;
878 struct addrinfo *ai_list, *ai_ptr;
881 dprintf ("host = %s\n", host);
883 if (ping_host_search (obj->head, host) != NULL)
886 memset (&ai_hints, '\0', sizeof (ai_hints));
887 ai_hints.ai_flags = 0;
889 ai_hints.ai_flags |= AI_ADDRCONFIG;
891 ai_hints.ai_family = obj->addrfamily;
892 ai_hints.ai_socktype = SOCK_RAW;
894 if ((ph = ping_alloc ()) == NULL)
896 dprintf ("Out of memory!\n");
900 if ((ph->hostname = strdup (host)) == NULL)
902 dprintf ("Out of memory!\n");
903 ping_set_error (obj, "strdup", strerror (errno));
908 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
910 dprintf ("getaddrinfo failed\n");
911 ping_set_error (obj, "getaddrinfo",
912 (ai_return == EAI_SYSTEM)
914 : gai_strerror (ai_return));
920 ping_set_error (obj, "getaddrinfo", "No hosts returned");
922 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
926 sockaddr_len = sizeof (sockaddr);
927 memset (&sockaddr, '\0', sockaddr_len);
929 if (ai_ptr->ai_family == AF_INET)
931 struct sockaddr_in *si;
933 si = (struct sockaddr_in *) &sockaddr;
934 si->sin_family = AF_INET;
935 si->sin_port = htons (ph->ident);
936 si->sin_addr.s_addr = htonl (INADDR_ANY);
938 ai_ptr->ai_socktype = SOCK_RAW;
939 ai_ptr->ai_protocol = IPPROTO_ICMP;
941 else if (ai_ptr->ai_family == AF_INET6)
943 struct sockaddr_in6 *si;
945 si = (struct sockaddr_in6 *) &sockaddr;
946 si->sin6_family = AF_INET6;
947 si->sin6_port = htons (ph->ident);
948 si->sin6_addr = in6addr_any;
950 ai_ptr->ai_socktype = SOCK_RAW;
951 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
955 char errmsg[PING_ERRMSG_LEN];
957 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
958 errmsg[PING_ERRMSG_LEN - 1] = '\0';
961 ping_set_error (obj, "getaddrinfo", errmsg);
965 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
968 dprintf ("socket: %s\n", strerror (errno));
969 ping_set_error (obj, "socket", strerror (errno));
973 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
975 dprintf ("bind: %s\n", strerror (errno));
976 ping_set_error (obj, "bind", strerror (errno));
982 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
983 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
984 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
985 ph->addrlen = ai_ptr->ai_addrlen;
986 ph->addrfamily = ai_ptr->ai_family;
991 freeaddrinfo (ai_list);
1000 ph->next = obj->head;
1003 ping_set_ttl (ph, obj->ttl);
1008 int ping_host_remove (pingobj_t *obj, const char *host)
1010 pinghost_t *pre, *cur;
1017 if (strcasecmp (host, cur->hostname))
1026 ping_set_error (obj, "ping_host_remove", "Host not found");
1031 obj->head = cur->next;
1033 pre->next = cur->next;
1043 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1045 return ((pingobj_iter_t *) obj->head);
1048 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1050 return ((pingobj_iter_t *) iter->next);
1053 const char *ping_iterator_get_host (pingobj_iter_t *iter)
1055 return (iter->hostname);
1058 double ping_iterator_get_latency (pingobj_iter_t *iter)
1060 return (iter->latency);