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>
34 #include <netinet/ip_icmp.h>
35 #include <netinet/icmp6.h>
43 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
45 # define dprintf(...) /**/
48 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
51 * private (static) functions
53 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
57 if ((tv1->tv_sec < tv2->tv_sec)
58 || ((tv1->tv_sec == tv2->tv_sec)
59 && (tv1->tv_usec < tv2->tv_usec)))
62 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
63 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
65 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
67 while (res->tv_usec < 0)
69 res->tv_usec += 1000000;
76 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
83 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
88 *(char *) &ret = *(char *) ptr;
92 /* Do this twice to get all possible carries.. */
93 sum = (sum >> 16) + (sum & 0xFFFF);
94 sum = (sum >> 16) + (sum & 0xFFFF);
101 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
104 struct icmphdr *icmp_hdr;
108 uint16_t recv_checksum;
109 uint16_t calc_checksum;
116 if (buffer_len < sizeof (struct ip))
119 ip_hdr = (struct ip *) buffer;
120 ip_hdr_len = ip_hdr->ip_hl << 2;
122 if (buffer_len < ip_hdr_len)
125 buffer += ip_hdr_len;
126 buffer_len -= ip_hdr_len;
128 if (buffer_len < sizeof (struct icmphdr))
131 icmp_hdr = (struct icmphdr *) buffer;
132 buffer += sizeof (struct icmphdr);
133 buffer_len -= sizeof (struct icmphdr);
135 if (icmp_hdr->type != ICMP_ECHOREPLY)
137 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->type);
141 recv_checksum = icmp_hdr->checksum;
142 icmp_hdr->checksum = 0;
143 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
144 sizeof (struct icmphdr) + buffer_len);
146 if (recv_checksum != calc_checksum)
148 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
149 recv_checksum, calc_checksum);
153 ident = ntohs (icmp_hdr->un.echo.id);
154 seq = ntohs (icmp_hdr->un.echo.sequence);
156 for (ptr = ph; ptr != NULL; ptr = ptr->next)
158 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
159 ptr->hostname, ptr->ident, ptr->sequence - 1);
161 if (ptr->addrfamily != AF_INET)
164 if (!timerisset (ptr->timer))
167 if (ptr->ident != ident)
170 if ((ptr->sequence - 1) != seq)
173 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
174 ptr->hostname, ident, seq);
181 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
188 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
190 struct icmp6_hdr *icmp_hdr;
197 if (buffer_len < sizeof (struct icmp6_hdr))
200 icmp_hdr = (struct icmp6_hdr *) buffer;
201 buffer += sizeof (struct icmphdr);
202 buffer_len -= sizeof (struct icmphdr);
204 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
206 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
210 if (icmp_hdr->icmp6_code != 0)
212 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
216 ident = ntohs (icmp_hdr->icmp6_id);
217 seq = ntohs (icmp_hdr->icmp6_seq);
219 for (ptr = ph; ptr != NULL; ptr = ptr->next)
221 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
222 ptr->hostname, ptr->ident, ptr->sequence - 1);
224 if (ptr->addrfamily != AF_INET6)
227 if (!timerisset (ptr->timer))
230 if (ptr->ident != ident)
233 if ((ptr->sequence - 1) != seq)
236 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
237 ptr->hostname, ident, seq);
244 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
251 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
258 pinghost_t *host = NULL;
260 struct sockaddr_storage sa;
263 sa_len = sizeof (sa);
265 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
266 (struct sockaddr *) &sa, &sa_len);
267 if (buffer_len == -1)
269 dprintf ("recvfrom: %s\n", strerror (errno));
273 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
275 if (sa.ss_family == AF_INET)
277 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
280 else if (sa.ss_family == AF_INET6)
282 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
286 dprintf ("rcvd: %12i.%06i\n",
289 dprintf ("sent: %12i.%06i\n",
290 (int) host->timer->tv_sec,
291 (int) host->timer->tv_usec);
293 if (ping_timeval_sub (now, host->timer, &diff) < 0)
295 timerclear (host->timer);
299 dprintf ("diff: %12i.%06i\n",
303 host->latency = ((double) diff.tv_usec) / 1000.0;
304 host->latency += ((double) diff.tv_sec) * 1000.0;
306 timerclear (host->timer);
311 static int ping_receive_all (pinghost_t *ph)
319 struct timeval endtime;
320 struct timeval nowtime;
321 struct timeval timeout;
328 for (ptr = ph; ptr != NULL; ptr = ptr->next)
331 if (gettimeofday (&endtime, NULL) == -1)
341 for (ptr = ph; ptr != NULL; ptr = ptr->next)
343 if (!timerisset (ptr->timer))
346 FD_SET (ptr->fd, &readfds);
349 if (max_readfds < ptr->fd)
350 max_readfds = ptr->fd;
353 if (num_readfds == 0)
356 if (gettimeofday (&nowtime, NULL) == -1)
359 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
362 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
363 (int) timeout.tv_sec,
364 (int) timeout.tv_usec);
366 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
368 if (gettimeofday (&nowtime, NULL) == -1)
371 if ((status == -1) && (errno == EINTR))
373 dprintf ("select was interrupted by signal..\n");
378 dprintf ("select: %s\n", strerror (errno));
381 else if (status == 0)
383 dprintf ("select timed out\n");
387 for (ptr = ph; ptr != NULL; ptr = ptr->next)
389 if (FD_ISSET (ptr->fd, &readfds))
390 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
398 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
399 * Sending functions: *
402 * +-> ping_send_one_ipv4 *
403 * `-> ping_send_one_ipv6 *
404 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
405 ssize_t ping_sendto (pinghost_t *ph, const void *buf, size_t buflen)
409 if (gettimeofday (ph->timer, NULL) == -1)
411 timerclear (ph->timer);
415 ret = sendto (ph->fd, buf, buflen, 0,
416 (struct sockaddr *) ph->addr, ph->addrlen);
421 static int ping_send_one_ipv4 (pinghost_t *ph)
423 struct icmphdr *icmp4;
432 dprintf ("ph->hostname = %s\n", ph->hostname);
434 memset (buf, '\0', sizeof (buf));
435 icmp4 = (struct icmphdr *) buf;
436 data = (char *) (icmp4 + 1);
438 icmp4->type = ICMP_ECHO;
440 /* The checksum will be calculated by the TCP/IP stack. */
442 icmp4->un.echo.id = htons (ph->ident);
443 icmp4->un.echo.sequence = htons (ph->sequence);
445 strcpy (data, PING_DATA);
446 datalen = strlen (data);
448 buflen = datalen + sizeof (struct icmphdr);
450 icmp4->checksum = ping_icmp4_checksum (buf, buflen);
452 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
454 status = ping_sendto (ph, buf, buflen);
457 perror ("ping_sendto");
461 dprintf ("sendto: status = %i\n", status);
466 static int ping_send_one_ipv6 (pinghost_t *ph)
468 struct icmp6_hdr *icmp6;
477 dprintf ("ph->hostname = %s\n", ph->hostname);
479 memset (buf, '\0', sizeof (buf));
480 icmp6 = (struct icmp6_hdr *) buf;
481 data = (char *) (icmp6 + 1);
483 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
484 icmp6->icmp6_code = 0;
485 /* The checksum will be calculated by the TCP/IP stack. */
486 icmp6->icmp6_cksum = 0;
487 icmp6->icmp6_id = htons (ph->ident);
488 icmp6->icmp6_seq = htons (ph->sequence);
490 strcpy (data, PING_DATA);
491 datalen = strlen (data);
493 buflen = datalen + sizeof (struct icmp6_hdr);
495 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
497 status = ping_sendto (ph, buf, buflen);
500 perror ("ping_sendto");
504 dprintf ("sendto: status = %i\n", status);
509 static int ping_send_all (pinghost_t *ph)
513 for (ptr = ph; ptr != NULL; ptr = ptr->next)
515 /* start timer.. The GNU `ping6' starts the timer before
516 * sending the packet, so I will do that too */
517 if (gettimeofday (ptr->timer, NULL) == -1)
519 dprintf ("gettimeofday: %s\n", strerror (errno));
520 timerclear (ptr->timer);
525 dprintf ("timer set for hostname = %s\n", ptr->hostname);
528 if (ptr->addrfamily == AF_INET6)
530 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
531 if (ping_send_one_ipv6 (ptr) != 0)
533 timerclear (ptr->timer);
537 else if (ptr->addrfamily == AF_INET)
539 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
540 if (ping_send_one_ipv4 (ptr) != 0)
542 timerclear (ptr->timer);
546 else /* this should not happen */
548 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
549 timerclear (ptr->timer);
560 static int ping_get_ident (void)
563 static int did_seed = 0;
569 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
573 if (read (fd, &seed, sizeof (seed)) != -1)
576 dprintf ("Random seed: %i\n", seed);
584 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
588 retval = (int) random ();
590 dprintf ("Random number: %i\n", retval);
595 static pinghost_t *ping_alloc (void)
600 ph_size = sizeof (pinghost_t)
601 + sizeof (struct sockaddr_storage)
602 + sizeof (struct timeval);
604 ph = (pinghost_t *) malloc (ph_size);
608 memset (ph, '\0', ph_size);
610 ph->timer = (struct timeval *) (ph + 1);
611 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
613 ph->addrlen = sizeof (struct sockaddr_storage);
615 ph->ident = ping_get_ident () & 0xFFFF;
620 static void ping_free (pinghost_t *ph)
622 if (ph->hostname != NULL)
631 pingobj_t *ping_construct (void)
635 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
643 void ping_destroy (pingobj_t *obj)
651 while (current != NULL)
653 next = current->next;
663 int ping_setopt (pingobj_t *obj, int option, void *value)
669 case PING_OPT_TIMEOUT:
670 obj->timeout = *((double *) value);
671 if (obj->timeout < 0.0)
673 obj->timeout = PING_DEF_TIMEOUT;
679 obj->ttl = *((int *) value);
680 if ((obj->ttl < 1) || (obj->ttl > 255))
682 obj->ttl = PING_DEF_TTL;
688 obj->addrfamily = *((int *) value);
689 if ((obj->addrfamily != AF_UNSPEC)
690 && (obj->addrfamily != AF_INET)
691 && (obj->addrfamily != AF_INET6))
693 obj->addrfamily = PING_DEF_AF;
700 } /* switch (option) */
703 } /* int ping_setopt */
706 int ping_send (pingobj_t *obj)
710 if (ping_send_all (obj->head) < 0)
713 if ((ret = ping_receive_all (obj->head)) < 0)
719 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
723 if (strcasecmp (ph->hostname, host) == 0)
732 int ping_host_add (pingobj_t *obj, const char *host)
736 struct sockaddr_storage sockaddr;
737 socklen_t sockaddr_len;
739 struct addrinfo ai_hints;
740 struct addrinfo *ai_list, *ai_ptr;
743 dprintf ("host = %s\n", host);
745 if (ping_host_search (obj->head, host) != NULL)
748 memset (&ai_hints, '\0', sizeof (ai_hints));
749 ai_hints.ai_flags = 0;
751 ai_hints.ai_flags |= AI_ADDRCONFIG;
753 ai_hints.ai_family = PF_UNSPEC;
754 ai_hints.ai_socktype = SOCK_RAW;
756 if ((ph = ping_alloc ()) == NULL)
758 dprintf ("Out of memory!\n");
762 if ((ph->hostname = strdup (host)) == NULL)
764 dprintf ("Out of memory!\n");
769 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
771 dprintf ("getaddrinfo failed\n");
776 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
780 sockaddr_len = sizeof (sockaddr);
781 memset (&sockaddr, '\0', sockaddr_len);
783 if (ai_ptr->ai_family == AF_INET)
785 struct sockaddr_in *si;
787 si = (struct sockaddr_in *) &sockaddr;
788 si->sin_family = AF_INET;
789 si->sin_port = htons (ph->ident);
790 si->sin_addr.s_addr = htonl (INADDR_ANY);
792 ai_ptr->ai_protocol = IPPROTO_ICMP;
794 else if (ai_ptr->ai_family == AF_INET6)
796 struct sockaddr_in6 *si;
798 si = (struct sockaddr_in6 *) &sockaddr;
799 si->sin6_family = AF_INET6;
800 si->sin6_port = htons (ph->ident);
801 si->sin6_addr = in6addr_any;
803 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
807 dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
811 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
814 dprintf ("socket: %s\n", strerror (errno));
818 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
820 dprintf ("bind: %s\n", strerror (errno));
826 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
827 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
828 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
829 ph->addrlen = ai_ptr->ai_addrlen;
830 ph->addrfamily = ai_ptr->ai_family;
835 freeaddrinfo (ai_list);
844 ph->next = obj->head;
850 int ping_host_remove (pingobj_t *obj, const char *host)
852 pinghost_t *pre, *cur;
859 if (strcasecmp (host, cur->hostname))
870 obj->head = cur->next;
872 pre->next = cur->next;
882 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
884 return ((pingobj_iter_t *) obj->head);
887 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
889 return ((pingobj_iter_t *) iter->next);
892 const char *ping_iterator_get_host (pingobj_iter_t *iter)
894 return (iter->hostname);
897 double ping_iterator_get_latency (pingobj_iter_t *iter)
899 return (iter->latency);