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
28 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/in.h>
37 #include <netinet/ip.h>
38 #include <netinet/ip_icmp.h>
39 #ifdef HAVE_NETINET_IP_VAR_H
40 # include <netinet/ip_var.h>
43 #include <netinet/ip6.h>
44 #include <netinet/icmp6.h>
52 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
54 # define dprintf(...) /**/
57 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
60 * private (static) functions
62 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
66 if ((tv1->tv_sec < tv2->tv_sec)
67 || ((tv1->tv_sec == tv2->tv_sec)
68 && (tv1->tv_usec < tv2->tv_usec)))
71 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
72 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
74 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
76 while (res->tv_usec < 0)
78 res->tv_usec += 1000000;
85 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
92 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
97 *(char *) &ret = *(char *) ptr;
101 /* Do this twice to get all possible carries.. */
102 sum = (sum >> 16) + (sum & 0xFFFF);
103 sum = (sum >> 16) + (sum & 0xFFFF);
110 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
113 struct icmp *icmp_hdr;
117 uint16_t recv_checksum;
118 uint16_t calc_checksum;
125 if (buffer_len < sizeof (struct ip))
128 ip_hdr = (struct ip *) buffer;
129 ip_hdr_len = ip_hdr->ip_hl << 2;
131 if (buffer_len < ip_hdr_len)
134 buffer += ip_hdr_len;
135 buffer_len -= ip_hdr_len;
137 if (buffer_len < sizeof (struct icmp))
140 icmp_hdr = (struct icmp *) buffer;
141 buffer += sizeof (struct icmp);
142 buffer_len -= sizeof (struct icmp);
144 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
146 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
150 recv_checksum = icmp_hdr->icmp_cksum;
151 icmp_hdr->icmp_cksum = 0;
152 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
153 sizeof (struct icmp) + buffer_len);
155 if (recv_checksum != calc_checksum)
157 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
158 recv_checksum, calc_checksum);
162 ident = ntohs (icmp_hdr->icmp_id);
163 seq = ntohs (icmp_hdr->icmp_seq);
165 for (ptr = ph; ptr != NULL; ptr = ptr->next)
167 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
168 ptr->hostname, ptr->ident, ptr->sequence - 1);
170 if (ptr->addrfamily != AF_INET)
173 if (!timerisset (ptr->timer))
176 if (ptr->ident != ident)
179 if ((ptr->sequence - 1) != seq)
182 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
183 ptr->hostname, ident, seq);
190 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
197 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
199 struct icmp6_hdr *icmp_hdr;
206 if (buffer_len < sizeof (struct icmp6_hdr))
209 icmp_hdr = (struct icmp6_hdr *) buffer;
210 buffer += sizeof (struct icmp);
211 buffer_len -= sizeof (struct icmp);
213 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
215 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
219 if (icmp_hdr->icmp6_code != 0)
221 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
225 ident = ntohs (icmp_hdr->icmp6_id);
226 seq = ntohs (icmp_hdr->icmp6_seq);
228 for (ptr = ph; ptr != NULL; ptr = ptr->next)
230 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
231 ptr->hostname, ptr->ident, ptr->sequence - 1);
233 if (ptr->addrfamily != AF_INET6)
236 if (!timerisset (ptr->timer))
239 if (ptr->ident != ident)
242 if ((ptr->sequence - 1) != seq)
245 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
246 ptr->hostname, ident, seq);
253 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
260 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
267 pinghost_t *host = NULL;
269 struct sockaddr_storage sa;
272 sa_len = sizeof (sa);
274 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
275 (struct sockaddr *) &sa, &sa_len);
276 if (buffer_len == -1)
278 dprintf ("recvfrom: %s\n", strerror (errno));
282 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
284 if (sa.ss_family == AF_INET)
286 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
289 else if (sa.ss_family == AF_INET6)
291 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
295 dprintf ("rcvd: %12i.%06i\n",
298 dprintf ("sent: %12i.%06i\n",
299 (int) host->timer->tv_sec,
300 (int) host->timer->tv_usec);
302 if (ping_timeval_sub (now, host->timer, &diff) < 0)
304 timerclear (host->timer);
308 dprintf ("diff: %12i.%06i\n",
312 host->latency = ((double) diff.tv_usec) / 1000.0;
313 host->latency += ((double) diff.tv_sec) * 1000.0;
315 timerclear (host->timer);
320 static int ping_receive_all (pinghost_t *ph)
328 struct timeval endtime;
329 struct timeval nowtime;
330 struct timeval timeout;
337 for (ptr = ph; ptr != NULL; ptr = ptr->next)
340 if (gettimeofday (&endtime, NULL) == -1)
350 for (ptr = ph; ptr != NULL; ptr = ptr->next)
352 if (!timerisset (ptr->timer))
355 FD_SET (ptr->fd, &readfds);
358 if (max_readfds < ptr->fd)
359 max_readfds = ptr->fd;
362 if (num_readfds == 0)
365 if (gettimeofday (&nowtime, NULL) == -1)
368 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
371 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
372 (int) timeout.tv_sec,
373 (int) timeout.tv_usec);
375 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
377 if (gettimeofday (&nowtime, NULL) == -1)
380 if ((status == -1) && (errno == EINTR))
382 dprintf ("select was interrupted by signal..\n");
387 dprintf ("select: %s\n", strerror (errno));
390 else if (status == 0)
392 dprintf ("select timed out\n");
396 for (ptr = ph; ptr != NULL; ptr = ptr->next)
398 if (FD_ISSET (ptr->fd, &readfds))
399 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
407 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
408 * Sending functions: *
411 * +-> ping_send_one_ipv4 *
412 * `-> ping_send_one_ipv6 *
413 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
414 ssize_t ping_sendto (pinghost_t *ph, const void *buf, size_t buflen)
418 if (gettimeofday (ph->timer, NULL) == -1)
420 timerclear (ph->timer);
424 ret = sendto (ph->fd, buf, buflen, 0,
425 (struct sockaddr *) ph->addr, ph->addrlen);
430 static int ping_send_one_ipv4 (pinghost_t *ph)
441 dprintf ("ph->hostname = %s\n", ph->hostname);
443 memset (buf, '\0', sizeof (buf));
444 icmp4 = (struct icmp *) buf;
445 data = (char *) (icmp4 + 1);
447 icmp4->icmp_type = ICMP_ECHO;
448 icmp4->icmp_code = 0;
449 icmp4->icmp_cksum = 0;
450 icmp4->icmp_id = htons (ph->ident);
451 icmp4->icmp_seq = htons (ph->sequence);
453 strcpy (data, PING_DATA);
454 datalen = strlen (data);
456 buflen = datalen + sizeof (struct icmp);
458 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
460 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
462 status = ping_sendto (ph, buf, buflen);
465 perror ("ping_sendto");
469 dprintf ("sendto: status = %i\n", status);
474 static int ping_send_one_ipv6 (pinghost_t *ph)
476 struct icmp6_hdr *icmp6;
485 dprintf ("ph->hostname = %s\n", ph->hostname);
487 memset (buf, '\0', sizeof (buf));
488 icmp6 = (struct icmp6_hdr *) buf;
489 data = (char *) (icmp6 + 1);
491 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
492 icmp6->icmp6_code = 0;
493 /* The checksum will be calculated by the TCP/IP stack. */
494 icmp6->icmp6_cksum = 0;
495 icmp6->icmp6_id = htons (ph->ident);
496 icmp6->icmp6_seq = htons (ph->sequence);
498 strcpy (data, PING_DATA);
499 datalen = strlen (data);
501 buflen = datalen + sizeof (struct icmp6_hdr);
503 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
505 status = ping_sendto (ph, buf, buflen);
508 perror ("ping_sendto");
512 dprintf ("sendto: status = %i\n", status);
517 static int ping_send_all (pinghost_t *ph)
521 for (ptr = ph; ptr != NULL; ptr = ptr->next)
523 /* start timer.. The GNU `ping6' starts the timer before
524 * sending the packet, so I will do that too */
525 if (gettimeofday (ptr->timer, NULL) == -1)
527 dprintf ("gettimeofday: %s\n", strerror (errno));
528 timerclear (ptr->timer);
533 dprintf ("timer set for hostname = %s\n", ptr->hostname);
536 if (ptr->addrfamily == AF_INET6)
538 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
539 if (ping_send_one_ipv6 (ptr) != 0)
541 timerclear (ptr->timer);
545 else if (ptr->addrfamily == AF_INET)
547 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
548 if (ping_send_one_ipv4 (ptr) != 0)
550 timerclear (ptr->timer);
554 else /* this should not happen */
556 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
557 timerclear (ptr->timer);
568 static int ping_get_ident (void)
571 static int did_seed = 0;
577 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
581 if (read (fd, &seed, sizeof (seed)) != -1)
584 dprintf ("Random seed: %i\n", seed);
592 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
596 retval = (int) random ();
598 dprintf ("Random number: %i\n", retval);
603 static pinghost_t *ping_alloc (void)
608 ph_size = sizeof (pinghost_t)
609 + sizeof (struct sockaddr_storage)
610 + sizeof (struct timeval);
612 ph = (pinghost_t *) malloc (ph_size);
616 memset (ph, '\0', ph_size);
618 ph->timer = (struct timeval *) (ph + 1);
619 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
621 ph->addrlen = sizeof (struct sockaddr_storage);
623 ph->ident = ping_get_ident () & 0xFFFF;
628 static void ping_free (pinghost_t *ph)
630 if (ph->hostname != NULL)
639 pingobj_t *ping_construct (void)
643 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
651 void ping_destroy (pingobj_t *obj)
659 while (current != NULL)
661 next = current->next;
671 int ping_setopt (pingobj_t *obj, int option, void *value)
677 case PING_OPT_TIMEOUT:
678 obj->timeout = *((double *) value);
679 if (obj->timeout < 0.0)
681 obj->timeout = PING_DEF_TIMEOUT;
687 obj->ttl = *((int *) value);
688 if ((obj->ttl < 1) || (obj->ttl > 255))
690 obj->ttl = PING_DEF_TTL;
696 obj->addrfamily = *((int *) value);
697 if ((obj->addrfamily != AF_UNSPEC)
698 && (obj->addrfamily != AF_INET)
699 && (obj->addrfamily != AF_INET6))
701 obj->addrfamily = PING_DEF_AF;
708 } /* switch (option) */
711 } /* int ping_setopt */
714 int ping_send (pingobj_t *obj)
718 if (ping_send_all (obj->head) < 0)
721 if ((ret = ping_receive_all (obj->head)) < 0)
727 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
731 if (strcasecmp (ph->hostname, host) == 0)
740 int ping_host_add (pingobj_t *obj, const char *host)
744 struct sockaddr_storage sockaddr;
745 socklen_t sockaddr_len;
747 struct addrinfo ai_hints;
748 struct addrinfo *ai_list, *ai_ptr;
751 dprintf ("host = %s\n", host);
753 if (ping_host_search (obj->head, host) != NULL)
756 memset (&ai_hints, '\0', sizeof (ai_hints));
757 ai_hints.ai_flags = 0;
759 ai_hints.ai_flags |= AI_ADDRCONFIG;
761 ai_hints.ai_family = PF_UNSPEC;
762 ai_hints.ai_socktype = SOCK_RAW;
764 if ((ph = ping_alloc ()) == NULL)
766 dprintf ("Out of memory!\n");
770 if ((ph->hostname = strdup (host)) == NULL)
772 dprintf ("Out of memory!\n");
777 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
779 dprintf ("getaddrinfo failed\n");
784 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
788 sockaddr_len = sizeof (sockaddr);
789 memset (&sockaddr, '\0', sockaddr_len);
791 if (ai_ptr->ai_family == AF_INET)
793 struct sockaddr_in *si;
795 si = (struct sockaddr_in *) &sockaddr;
796 si->sin_family = AF_INET;
797 si->sin_port = htons (ph->ident);
798 si->sin_addr.s_addr = htonl (INADDR_ANY);
800 ai_ptr->ai_protocol = IPPROTO_ICMP;
802 else if (ai_ptr->ai_family == AF_INET6)
804 struct sockaddr_in6 *si;
806 si = (struct sockaddr_in6 *) &sockaddr;
807 si->sin6_family = AF_INET6;
808 si->sin6_port = htons (ph->ident);
809 si->sin6_addr = in6addr_any;
811 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
815 dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
819 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
822 dprintf ("socket: %s\n", strerror (errno));
826 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
828 dprintf ("bind: %s\n", strerror (errno));
834 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
835 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
836 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
837 ph->addrlen = ai_ptr->ai_addrlen;
838 ph->addrfamily = ai_ptr->ai_family;
843 freeaddrinfo (ai_list);
852 ph->next = obj->head;
858 int ping_host_remove (pingobj_t *obj, const char *host)
860 pinghost_t *pre, *cur;
867 if (strcasecmp (host, cur->hostname))
878 obj->head = cur->next;
880 pre->next = cur->next;
890 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
892 return ((pingobj_iter_t *) obj->head);
895 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
897 return ((pingobj_iter_t *) iter->next);
900 const char *ping_iterator_get_host (pingobj_iter_t *iter)
902 return (iter->hostname);
905 double ping_iterator_get_latency (pingobj_iter_t *iter)
907 return (iter->latency);