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(format, ...) /**/
48 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
50 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
54 if ((tv1->tv_sec < tv2->tv_sec)
55 || ((tv1->tv_sec == tv2->tv_sec)
56 && (tv1->tv_usec < tv2->tv_usec)))
59 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
60 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
62 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
64 while (res->tv_usec < 0)
66 res->tv_usec += 1000000;
73 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
80 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
85 *(char *) &ret = *(char *) ptr;
89 /* Do this twice to get all possible carries.. */
90 sum = (sum >> 16) + (sum & 0xFFFF);
91 sum = (sum >> 16) + (sum & 0xFFFF);
98 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
101 struct icmphdr *icmp_hdr;
105 uint16_t recv_checksum;
106 uint16_t calc_checksum;
113 if (buffer_len < sizeof (struct ip))
116 ip_hdr = (struct ip *) buffer;
117 ip_hdr_len = ip_hdr->ip_hl << 2;
119 if (buffer_len < ip_hdr_len)
122 buffer += ip_hdr_len;
123 buffer_len -= ip_hdr_len;
125 if (buffer_len < sizeof (struct icmphdr))
128 icmp_hdr = (struct icmphdr *) buffer;
129 buffer += sizeof (struct icmphdr);
130 buffer_len -= sizeof (struct icmphdr);
132 if (icmp_hdr->type != ICMP_ECHOREPLY)
134 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->type);
138 recv_checksum = icmp_hdr->checksum;
139 icmp_hdr->checksum = 0;
140 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
141 sizeof (struct icmphdr) + buffer_len);
143 if (recv_checksum != calc_checksum)
145 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
146 recv_checksum, calc_checksum);
150 ident = ntohs (icmp_hdr->un.echo.id);
151 seq = ntohs (icmp_hdr->un.echo.sequence);
153 for (ptr = ph; ptr != NULL; ptr = ptr->next)
155 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
156 ptr->hostname, ptr->ident, ptr->sequence - 1);
158 if (ptr->addrfamily != AF_INET)
161 if (!timerisset (ptr->timer))
164 if (ptr->ident != ident)
167 if ((ptr->sequence - 1) != seq)
170 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
171 ptr->hostname, ident, seq);
178 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
185 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
187 struct icmp6_hdr *icmp_hdr;
194 if (buffer_len < sizeof (struct icmp6_hdr))
197 icmp_hdr = (struct icmp6_hdr *) buffer;
198 buffer += sizeof (struct icmphdr);
199 buffer_len -= sizeof (struct icmphdr);
201 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
203 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
207 if (icmp_hdr->icmp6_code != 0)
209 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
213 ident = ntohs (icmp_hdr->icmp6_id);
214 seq = ntohs (icmp_hdr->icmp6_seq);
216 for (ptr = ph; ptr != NULL; ptr = ptr->next)
218 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
219 ptr->hostname, ptr->ident, ptr->sequence - 1);
221 if (ptr->addrfamily != AF_INET6)
224 if (!timerisset (ptr->timer))
227 if (ptr->ident != ident)
230 if ((ptr->sequence - 1) != seq)
233 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
234 ptr->hostname, ident, seq);
241 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
248 static void ping_receive_one (int fd, pinghost_t *ph)
256 pinghost_t *host = NULL;
258 struct sockaddr_storage sa;
261 sa_len = sizeof (sa);
263 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
264 (struct sockaddr *) &sa, &sa_len);
265 if (buffer_len == -1)
267 dprintf ("recvfrom: %s\n", strerror (errno));
271 dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
273 if (sa.ss_family == AF_INET)
275 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
278 else if (sa.ss_family == AF_INET6)
280 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
284 if (gettimeofday (&now, NULL) == -1)
286 dprintf ("gettimeofday: %s\n", strerror (errno));
287 timerclear (host->timer);
291 dprintf ("sent: %12i.%06i\n",
292 (int) host->timer->tv_sec,
293 (int) host->timer->tv_usec);
294 dprintf ("rcvd: %12i.%06i\n",
298 if (ping_timeval_sub (&now, host->timer, &diff) < 0)
300 timerclear (host->timer);
304 dprintf ("diff: %12i.%06i\n",
308 host->latency = ((double) diff.tv_usec) / 1000.0;
309 host->latency += ((double) diff.tv_sec) * 1000.0;
311 timerclear (host->timer);
314 static int ping_receive_all (pinghost_t *ph)
322 struct timeval endtime;
323 struct timeval nowtime;
324 struct timeval timeout;
327 if (gettimeofday (&endtime, NULL) == -1)
331 for (ptr = ph; ptr != NULL; ptr = ptr->next)
340 if (gettimeofday (&nowtime, NULL) == -1)
343 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
346 for (ptr = ph; ptr != NULL; ptr = ptr->next)
348 if (!timerisset (ptr->timer))
351 FD_SET (ptr->fd, &readfds);
354 if (max_readfds < ptr->fd)
355 max_readfds = ptr->fd;
358 if (num_readfds == 0)
361 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
362 (int) timeout.tv_sec,
363 (int) timeout.tv_usec);
365 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
367 if ((status == -1) && (errno == EINTR))
369 dprintf ("select was interrupted by signal..\n");
374 dprintf ("select: %s\n", strerror (errno));
377 else if (status == 0)
379 dprintf ("select timed out\n");
383 for (ptr = ph; ptr != NULL; ptr = ptr->next)
385 if (FD_ISSET (ptr->fd, &readfds))
386 ping_receive_one (ptr->fd, ph);
390 /* FIXME - return correct status */
394 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
395 * Sending functions: *
398 * +-> ping_send_one_ipv4 *
399 * `-> ping_send_one_ipv6 *
400 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
401 static int ping_send_one_ipv4 (pinghost_t *ph)
403 struct icmphdr *icmp4;
412 dprintf ("ph->hostname = %s\n", ph->hostname);
414 memset (buf, '\0', sizeof (buf));
415 icmp4 = (struct icmphdr *) buf;
416 data = (char *) (icmp4 + 1);
418 icmp4->type = ICMP_ECHO;
420 /* The checksum will be calculated by the TCP/IP stack. */
422 icmp4->un.echo.id = htons (ph->ident);
423 icmp4->un.echo.sequence = htons (ph->sequence);
425 strcpy (data, PING_DATA);
426 datalen = strlen (data);
428 buflen = datalen + sizeof (struct icmphdr);
430 icmp4->checksum = ping_icmp4_checksum (buf, buflen);
432 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
434 status = sendto (ph->fd, buf, buflen, 0,
435 (struct sockaddr *) ph->addr, ph->addrlen);
442 dprintf ("sendto: status = %i\n", status);
447 static int ping_send_one_ipv6 (pinghost_t *ph)
449 struct icmp6_hdr *icmp6;
458 dprintf ("ph->hostname = %s\n", ph->hostname);
460 memset (buf, '\0', sizeof (buf));
461 icmp6 = (struct icmp6_hdr *) buf;
462 data = (char *) (icmp6 + 1);
464 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
465 icmp6->icmp6_code = 0;
466 /* The checksum will be calculated by the TCP/IP stack. */
467 icmp6->icmp6_cksum = 0;
468 icmp6->icmp6_id = htons (ph->ident);
469 icmp6->icmp6_seq = htons (ph->sequence);
471 strcpy (data, PING_DATA);
472 datalen = strlen (data);
474 buflen = datalen + sizeof (struct icmp6_hdr);
476 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
478 status = sendto (ph->fd, buf, buflen, 0,
479 (struct sockaddr *) ph->addr, ph->addrlen);
486 dprintf ("sendto: status = %i\n", status);
491 static int ping_send_all (pinghost_t *ph)
495 for (ptr = ph; ptr != NULL; ptr = ptr->next)
497 /* start timer.. The GNU `ping6' starts the timer before
498 * sending the packet, so I will do that too */
499 if (gettimeofday (ptr->timer, NULL) == -1)
501 dprintf ("gettimeofday: %s\n", strerror (errno));
502 timerclear (ptr->timer);
507 dprintf ("timer set for hostname = %s\n", ptr->hostname);
510 if (ptr->addrfamily == AF_INET6)
512 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
513 if (ping_send_one_ipv6 (ptr) != 0)
515 timerclear (ptr->timer);
519 else if (ptr->addrfamily == AF_INET)
521 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
522 if (ping_send_one_ipv4 (ptr) != 0)
524 timerclear (ptr->timer);
528 else /* this should not happen */
530 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
531 timerclear (ptr->timer);
542 static int ping_get_ident (void)
545 static int did_seed = 0;
551 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
555 if (read (fd, &seed, sizeof (seed)) != -1)
558 dprintf ("Random seed: %i\n", seed);
566 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
570 retval = (int) random ();
572 dprintf ("Random number: %i\n", retval);
577 static pinghost_t *ping_alloc (void)
582 ph_size = sizeof (pinghost_t)
583 + sizeof (struct sockaddr_storage)
584 + sizeof (struct timeval);
586 ph = (pinghost_t *) malloc (ph_size);
590 memset (ph, '\0', ph_size);
592 ph->timer = (struct timeval *) (ph + 1);
593 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
595 ph->addrlen = sizeof (struct sockaddr_storage);
597 ph->ident = ping_get_ident () & 0xFFFF;
602 static void ping_free (pinghost_t *ph)
604 if (ph->hostname != NULL)
613 pingobj_t *ping_construct (int flags)
617 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
626 void ping_destroy (pingobj_t *obj)
634 while (current != NULL)
636 next = current->next;
646 int ping_send (pingobj_t *obj)
648 if (ping_send_all (obj->head) < 0)
651 if (ping_receive_all (obj->head) < 0)
657 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
661 if (strcasecmp (ph->hostname, host) == 0)
670 int ping_host_add (pingobj_t *obj, const char *host)
674 struct sockaddr_storage sockaddr;
675 socklen_t sockaddr_len;
677 struct addrinfo ai_hints;
678 struct addrinfo *ai_list, *ai_ptr;
681 dprintf ("host = %s\n", host);
683 if (ping_host_search (obj->head, host) != NULL)
686 memset (&ai_hints, '\0', sizeof (ai_hints));
687 ai_hints.ai_flags = AI_ADDRCONFIG;
688 ai_hints.ai_family = PF_UNSPEC;
689 ai_hints.ai_socktype = SOCK_RAW;
691 if ((ph = ping_alloc ()) == NULL)
693 dprintf ("Out of memory!\n");
697 if ((ph->hostname = strdup (host)) == NULL)
699 dprintf ("Out of memory!\n");
704 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
706 dprintf ("getaddrinfo failed\n");
711 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
715 sockaddr_len = sizeof (sockaddr);
716 memset (&sockaddr, '\0', sockaddr_len);
718 if (ai_ptr->ai_family == AF_INET)
720 struct sockaddr_in *si;
722 si = (struct sockaddr_in *) &sockaddr;
723 si->sin_family = AF_INET;
724 si->sin_port = htons (ph->ident);
725 si->sin_addr.s_addr = htonl (INADDR_ANY);
727 ai_ptr->ai_protocol = IPPROTO_ICMP;
729 else if (ai_ptr->ai_family == AF_INET6)
731 struct sockaddr_in6 *si;
733 si = (struct sockaddr_in6 *) &sockaddr;
734 si->sin6_family = AF_INET6;
735 si->sin6_port = htons (ph->ident);
736 si->sin6_addr = in6addr_any;
738 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
742 dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
746 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
749 dprintf ("socket: %s\n", strerror (errno));
753 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
755 dprintf ("bind: %s\n", strerror (errno));
761 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
762 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
763 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
764 ph->addrlen = ai_ptr->ai_addrlen;
765 ph->addrfamily = ai_ptr->ai_family;
770 freeaddrinfo (ai_list);
779 ph->next = obj->head;
785 int ping_host_remove (pingobj_t *obj, const char *host)
787 pinghost_t *pre, *cur;
794 if (strcasecmp (host, cur->hostname))
805 obj->head = cur->next;
807 pre->next = cur->next;
817 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
819 return ((pingobj_iter_t *) obj->head);
822 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
824 return ((pingobj_iter_t *) iter->next);
827 const char *ping_iterator_get_host (pingobj_iter_t *iter)
829 return (iter->hostname);
832 double ping_iterator_get_latency (pingobj_iter_t *iter)
834 return (iter->latency);