2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2011 Florian octo Forster <ff at octo.it>
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by the
7 * Free Software Foundation; either version 2.1 of the License, or (at your
8 * option) any later version.
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #define __APPLE_USE_RFC_3542
32 # include <inttypes.h>
36 # error "You don't have the standard C99 header files installed"
37 #endif /* STDC_HEADERS */
51 # include <sys/types.h>
54 # include <sys/stat.h>
57 #if TIME_WITH_SYS_TIME
58 # include <sys/time.h>
62 # include <sys/time.h>
69 # include <sys/socket.h>
76 #if HAVE_NETINET_IN_SYSTM_H
77 # include <netinet/in_systm.h>
80 # include <netinet/in.h>
83 # include <netinet/ip.h>
85 #if HAVE_NETINET_IP_ICMP_H
86 # include <netinet/ip_icmp.h>
88 #ifdef HAVE_NETINET_IP_VAR_H
89 # include <netinet/ip_var.h>
91 #if HAVE_NETINET_IP6_H
92 # include <netinet/ip6.h>
94 #if HAVE_NETINET_ICMP6_H
95 # include <netinet/icmp6.h>
101 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
103 # define dprintf(...) /**/
106 #define PING_ERRMSG_LEN 256
110 /* username: name passed in by the user */
112 /* hostname: name returned by the reverse lookup */
114 struct sockaddr_storage *addr;
120 struct timeval *timer;
129 struct pinghost *next;
140 struct sockaddr *srcaddr;
141 socklen_t srcaddrlen;
145 char errmsg[PING_ERRMSG_LEN];
151 * private (static) functions
153 /* Even though Posix requires "strerror_r" to return an "int",
154 * some systems (e.g. the GNU libc) return a "char *" _and_
155 * ignore the second argument ... -tokkee */
156 static char *sstrerror (int errnum, char *buf, size_t buflen)
162 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
164 /* #endif !HAVE_STRERROR_R */
166 #elif STRERROR_R_CHAR_P
169 temp = strerror_r (errnum, buf, buflen);
172 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
173 strncpy (buf, temp, buflen);
175 strncpy (buf, "strerror_r did not return "
176 "an error message", buflen);
179 /* #endif STRERROR_R_CHAR_P */
182 if (strerror_r (errnum, buf, buflen) != 0)
184 snprintf (buf, buflen, "Error %i (%#x); "
185 "Additionally, strerror_r failed.",
188 #endif /* STRERROR_R_CHAR_P */
193 } /* char *sstrerror */
195 static void ping_set_error (pingobj_t *obj, const char *function,
198 snprintf (obj->errmsg, sizeof (obj->errmsg),
199 "%s: %s", function, message);
200 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
203 static void ping_set_errno (pingobj_t *obj, int error_number)
205 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
208 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
211 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
212 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
214 while (res->tv_usec > 1000000)
216 res->tv_usec -= 1000000;
223 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
226 if ((tv1->tv_sec < tv2->tv_sec)
227 || ((tv1->tv_sec == tv2->tv_sec)
228 && (tv1->tv_usec < tv2->tv_usec)))
231 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
232 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
234 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
236 while (res->tv_usec < 0)
238 res->tv_usec += 1000000;
245 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
252 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
257 *(char *) &ret = *(char *) ptr;
261 /* Do this twice to get all possible carries.. */
262 sum = (sum >> 16) + (sum & 0xFFFF);
263 sum = (sum >> 16) + (sum & 0xFFFF);
270 static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
274 struct icmp *icmp_hdr;
278 uint16_t recv_checksum;
279 uint16_t calc_checksum;
286 if (buffer_len < sizeof (struct ip))
289 ip_hdr = (struct ip *) buffer;
290 ip_hdr_len = ip_hdr->ip_hl << 2;
292 if (buffer_len < ip_hdr_len)
295 buffer += ip_hdr_len;
296 buffer_len -= ip_hdr_len;
298 if (buffer_len < sizeof (struct icmp))
301 icmp_hdr = (struct icmp *) buffer;
302 buffer += sizeof (struct icmp);
303 buffer_len -= sizeof (struct icmp);
305 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
307 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
311 recv_checksum = icmp_hdr->icmp_cksum;
312 icmp_hdr->icmp_cksum = 0;
313 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
314 sizeof (struct icmp) + buffer_len);
316 if (recv_checksum != calc_checksum)
318 dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
319 "calculated 0x%04"PRIx16"\n",
320 recv_checksum, calc_checksum);
324 ident = ntohs (icmp_hdr->icmp_id);
325 seq = ntohs (icmp_hdr->icmp_seq);
327 /* We have to iterate over all hosts, since ICMPv4 packets may
328 * be received on any raw v4 socket. */
329 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
331 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
332 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
334 if (ptr->addrfamily != AF_INET)
337 if (!timerisset (ptr->timer))
340 if (ptr->ident != ident)
343 if (((ptr->sequence - 1) & 0xFFFF) != seq)
346 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
348 ptr->hostname, ident, seq);
355 dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
360 ptr->recv_ttl = (int) ip_hdr->ip_ttl;
361 ptr->recv_qos = (uint8_t) ip_hdr->ip_tos;
366 #ifndef ICMP6_ECHO_REQUEST
367 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
368 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
370 # define ICMP6_ECHO_REQUEST 128
374 #ifndef ICMP6_ECHO_REPLY
375 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
376 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
378 # define ICMP6_ECHO_REPLY 129
382 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
385 struct icmp6_hdr *icmp_hdr;
392 if (buffer_len < sizeof (struct icmp6_hdr))
395 icmp_hdr = (struct icmp6_hdr *) buffer;
396 buffer += sizeof (struct icmp);
397 buffer_len -= sizeof (struct icmp);
399 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
401 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
405 if (icmp_hdr->icmp6_code != 0)
407 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
411 ident = ntohs (icmp_hdr->icmp6_id);
412 seq = ntohs (icmp_hdr->icmp6_seq);
414 /* We have to iterate over all hosts, since ICMPv6 packets may
415 * be received on any raw v6 socket. */
416 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
418 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
419 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
421 if (ptr->addrfamily != AF_INET6)
424 if (!timerisset (ptr->timer))
427 if (ptr->ident != ident)
430 if (((ptr->sequence - 1) & 0xFFFF) != seq)
433 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
435 ptr->hostname, ident, seq);
442 dprintf ("No match found for ident = 0x%04"PRIx16", "
450 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
453 /* Note: 'ph' is not necessarily the host object for which we receive a
454 * reply. The right object will be returned by ping_receive_ipv*(). For
455 * now, we can only rely on ph->fd and ph->addrfamily. */
457 struct timeval diff, pkt_now = *now;
458 pinghost_t *host = NULL;
463 * Set up the receive buffer..
465 struct msghdr msghdr;
466 struct cmsghdr *cmsg;
467 char payload_buffer[4096];
468 ssize_t payload_buffer_len;
469 char control_buffer[4096];
470 struct iovec payload_iovec;
472 memset (&payload_iovec, 0, sizeof (payload_iovec));
473 payload_iovec.iov_base = payload_buffer;
474 payload_iovec.iov_len = sizeof (payload_buffer);
476 memset (&msghdr, 0, sizeof (msghdr));
477 /* unspecified source address */
478 msghdr.msg_name = NULL;
479 msghdr.msg_namelen = 0;
480 /* output buffer vector, see readv(2) */
481 msghdr.msg_iov = &payload_iovec;
482 msghdr.msg_iovlen = 1;
483 /* output buffer for control messages */
484 msghdr.msg_control = control_buffer;
485 msghdr.msg_controllen = sizeof (control_buffer);
486 /* flags; this is an output only field.. */
487 msghdr.msg_flags = 0;
489 msghdr.msg_flags |= MSG_XPG4_2;
492 payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
493 if (payload_buffer_len < 0)
496 char errbuf[PING_ERRMSG_LEN];
497 dprintf ("recvfrom: %s\n",
498 sstrerror (errno, errbuf, sizeof (errbuf)));
502 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
504 /* Iterate over all auxiliary data in msghdr */
507 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
509 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
511 if (cmsg->cmsg_level == SOL_SOCKET)
514 if (cmsg->cmsg_type == SO_TIMESTAMP)
515 memcpy (&pkt_now, CMSG_DATA (cmsg), sizeof (pkt_now));
516 #endif /* SO_TIMESTAMP */
518 else if (ph->addrfamily == AF_INET) /* {{{ */
520 if (cmsg->cmsg_level != IPPROTO_IP)
523 if (cmsg->cmsg_type == IP_TOS)
525 memcpy (&recv_qos, CMSG_DATA (cmsg),
527 dprintf ("TOSv4 = 0x%02"PRIx8";\n", recv_qos);
529 if (cmsg->cmsg_type == IP_TTL)
531 memcpy (&recv_ttl, CMSG_DATA (cmsg),
533 dprintf ("TTLv4 = %i;\n", recv_ttl);
537 dprintf ("Not handling option %i.\n",
541 else if (ph->addrfamily == AF_INET6) /* {{{ */
543 if (cmsg->cmsg_level != IPPROTO_IPV6)
546 if (cmsg->cmsg_type == IPV6_TCLASS)
548 memcpy (&recv_qos, CMSG_DATA (cmsg),
550 dprintf ("TOSv6 = 0x%02"PRIx8";\n", recv_qos);
553 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
555 memcpy (&recv_ttl, CMSG_DATA (cmsg),
557 dprintf ("TTLv6 = %i;\n", recv_ttl);
561 #ifdef IPV6_UNICAST_HOPS
562 if (cmsg->cmsg_type == IPV6_UNICAST_HOPS)
564 memcpy (&recv_ttl, CMSG_DATA (cmsg),
566 dprintf ("TTLv6 = %i;\n", recv_ttl);
570 #ifdef IPV6_MULTICAST_HOPS
571 if (cmsg->cmsg_type == IPV6_MULTICAST_HOPS)
573 memcpy (&recv_ttl, CMSG_DATA (cmsg),
575 dprintf ("TTLv6 = %i;\n", recv_ttl);
580 dprintf ("Not handling option %i.\n",
586 dprintf ("Don't know how to handle "
587 "unknown protocol %i.\n",
590 } /* }}} for (cmsg) */
592 if (ph->addrfamily == AF_INET)
594 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
598 else if (ph->addrfamily == AF_INET6)
600 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
606 dprintf ("ping_receive_one: Unknown address family %i.\n",
611 dprintf ("rcvd: %12i.%06i\n",
612 (int) pkt_now.tv_sec,
613 (int) pkt_now.tv_usec);
614 dprintf ("sent: %12i.%06i\n",
615 (int) host->timer->tv_sec,
616 (int) host->timer->tv_usec);
618 if (ping_timeval_sub (&pkt_now, host->timer, &diff) < 0)
620 timerclear (host->timer);
624 dprintf ("diff: %12i.%06i\n",
629 host->recv_ttl = recv_ttl;
630 host->recv_qos = recv_qos;
632 host->latency = ((double) diff.tv_usec) / 1000.0;
633 host->latency += ((double) diff.tv_sec) * 1000.0;
635 timerclear (host->timer);
640 static int ping_receive_all (pingobj_t *obj)
650 struct timeval endtime;
651 struct timeval nowtime;
652 struct timeval timeout;
660 for (ptr = ph; ptr != NULL; ptr = ptr->next)
666 if (gettimeofday (&nowtime, NULL) == -1)
668 ping_set_errno (obj, errno);
673 timeout.tv_sec = (time_t) obj->timeout;
674 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
676 dprintf ("Set timeout to %i.%06i seconds\n",
677 (int) timeout.tv_sec,
678 (int) timeout.tv_usec);
680 ping_timeval_add (&nowtime, &timeout, &endtime);
689 for (ptr = ph; ptr != NULL; ptr = ptr->next)
691 if (!timerisset (ptr->timer))
694 FD_SET (ptr->fd, &read_fds);
695 FD_SET (ptr->fd, &err_fds);
698 if (max_fd < ptr->fd)
705 if (gettimeofday (&nowtime, NULL) == -1)
707 ping_set_errno (obj, errno);
711 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
714 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_fds,
715 (int) timeout.tv_sec,
716 (int) timeout.tv_usec);
718 status = select (max_fd + 1, &read_fds, NULL, &err_fds, &timeout);
720 if (gettimeofday (&nowtime, NULL) == -1)
722 ping_set_errno (obj, errno);
726 if ((status == -1) && (errno == EINTR))
728 dprintf ("select was interrupted by signal..\n");
734 char errbuf[PING_ERRMSG_LEN];
735 dprintf ("select: %s\n",
736 sstrerror (errno, errbuf, sizeof (errbuf)));
740 else if (status == 0)
742 dprintf ("select timed out\n");
743 for (ptr = ph; ptr != NULL; ptr = ptr->next)
744 if (ptr->latency < 0.0)
749 for (ptr = ph; ptr != NULL; ptr = ptr->next)
751 if (FD_ISSET (ptr->fd, &read_fds))
753 if (ping_receive_one (obj, ptr, &nowtime) == 0)
756 else if (FD_ISSET (ptr->fd, &err_fds))
758 /* clear the timer in this case so that we
759 * don't run into an endless loop. */
760 /* TODO: Set an error flag in this case. */
761 timerclear (ptr->timer);
769 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
770 * Sending functions: *
773 * +-> ping_send_one_ipv4 *
774 * `-> ping_send_one_ipv6 *
775 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
776 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
777 const void *buf, size_t buflen)
781 if (gettimeofday (ph->timer, NULL) == -1)
783 timerclear (ph->timer);
787 ret = sendto (ph->fd, buf, buflen, 0,
788 (struct sockaddr *) ph->addr, ph->addrlen);
792 #if defined(EHOSTUNREACH)
793 if (errno == EHOSTUNREACH)
796 #if defined(ENETUNREACH)
797 if (errno == ENETUNREACH)
800 ping_set_errno (obj, errno);
806 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
817 dprintf ("ph->hostname = %s\n", ph->hostname);
819 memset (buf, '\0', sizeof (buf));
820 icmp4 = (struct icmp *) buf;
821 data = (char *) (icmp4 + 1);
823 icmp4->icmp_type = ICMP_ECHO;
824 icmp4->icmp_code = 0;
825 icmp4->icmp_cksum = 0;
826 icmp4->icmp_id = htons (ph->ident);
827 icmp4->icmp_seq = htons (ph->sequence);
829 buflen = 4096 - sizeof (struct icmp);
830 strncpy (data, ph->data, buflen);
831 datalen = strlen (data);
833 buflen = datalen + sizeof (struct icmp);
835 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
837 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
839 status = ping_sendto (obj, ph, buf, buflen);
842 perror ("ping_sendto");
846 dprintf ("sendto: status = %i\n", status);
851 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
853 struct icmp6_hdr *icmp6;
862 dprintf ("ph->hostname = %s\n", ph->hostname);
864 memset (buf, '\0', sizeof (buf));
865 icmp6 = (struct icmp6_hdr *) buf;
866 data = (char *) (icmp6 + 1);
868 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
869 icmp6->icmp6_code = 0;
870 /* The checksum will be calculated by the TCP/IP stack. */
872 icmp6->icmp6_cksum = 0;
873 icmp6->icmp6_id = htons (ph->ident);
874 icmp6->icmp6_seq = htons (ph->sequence);
876 buflen = 4096 - sizeof (struct icmp6_hdr);
877 strncpy (data, ph->data, buflen);
878 datalen = strlen (data);
880 buflen = datalen + sizeof (struct icmp6_hdr);
882 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
884 status = ping_sendto (obj, ph, buf, buflen);
887 perror ("ping_sendto");
891 dprintf ("sendto: status = %i\n", status);
896 static int ping_send_all (pingobj_t *obj)
906 for (ptr = ph; ptr != NULL; ptr = ptr->next)
908 /* start timer.. The GNU `ping6' starts the timer before
909 * sending the packet, so I will do that too */
910 if (gettimeofday (ptr->timer, NULL) == -1)
913 char errbuf[PING_ERRMSG_LEN];
914 dprintf ("gettimeofday: %s\n",
915 sstrerror (errno, errbuf, sizeof (errbuf)));
917 timerclear (ptr->timer);
923 dprintf ("timer set for hostname = %s\n", ptr->hostname);
926 if (ptr->addrfamily == AF_INET6)
928 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
929 if (ping_send_one_ipv6 (obj, ptr) != 0)
931 timerclear (ptr->timer);
936 else if (ptr->addrfamily == AF_INET)
938 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
939 if (ping_send_one_ipv4 (obj, ptr) != 0)
941 timerclear (ptr->timer);
946 else /* this should not happen */
948 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
949 timerclear (ptr->timer);
961 * Set the TTL of a socket protocol independently.
963 static int ping_set_ttl (pinghost_t *ph, int ttl)
967 if (ph->addrfamily == AF_INET)
969 dprintf ("Setting TTLv4 to %i\n", ttl);
970 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
973 else if (ph->addrfamily == AF_INET6)
975 dprintf ("Setting TTLv6 to %i\n", ttl);
976 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
984 * Set the TOS of a socket protocol independently.
986 * Using SOL_SOCKET / SO_PRIORITY might be a protocol independent way to
987 * set this. See socket(7) for details.
989 static int ping_set_qos (pingobj_t *obj, pinghost_t *ph, uint8_t qos)
992 char errbuf[PING_ERRMSG_LEN];
994 if (ph->addrfamily == AF_INET)
996 dprintf ("Setting TP_TOS to %#04"PRIx8"\n", qos);
997 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TOS,
1002 ping_set_error (obj, "ping_set_qos",
1003 sstrerror (ret, errbuf, sizeof (errbuf)));
1004 dprintf ("Setting TP_TOS failed: %s\n", errbuf);
1007 else if (ph->addrfamily == AF_INET6)
1009 /* IPV6_TCLASS requires an "int". */
1010 int tmp = (int) qos;
1012 dprintf ("Setting IPV6_TCLASS to %#04"PRIx8" (%i)\n", qos, tmp);
1013 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_TCLASS,
1014 &tmp, sizeof (tmp));
1018 ping_set_error (obj, "ping_set_qos",
1019 sstrerror (ret, errbuf, sizeof (errbuf)));
1020 dprintf ("Setting IPV6_TCLASS failed: %s\n", errbuf);
1027 static int ping_get_ident (void)
1030 static int did_seed = 0;
1036 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
1040 if (read (fd, &seed, sizeof (seed)) != -1)
1043 dprintf ("Random seed: %#x\n", seed);
1052 char errbuf[PING_ERRMSG_LEN];
1053 dprintf ("open (/dev/urandom): %s\n",
1054 sstrerror (errno, errbuf, sizeof (errbuf)));
1059 retval = (int) random ();
1061 dprintf ("Random number: %#x\n", retval);
1066 static pinghost_t *ping_alloc (void)
1071 ph_size = sizeof (pinghost_t)
1072 + sizeof (struct sockaddr_storage)
1073 + sizeof (struct timeval);
1075 ph = (pinghost_t *) malloc (ph_size);
1079 memset (ph, '\0', ph_size);
1081 ph->timer = (struct timeval *) (ph + 1);
1082 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
1084 ph->addrlen = sizeof (struct sockaddr_storage);
1088 ph->ident = ping_get_ident () & 0xFFFF;
1093 static void ping_free (pinghost_t *ph)
1098 if (ph->username != NULL)
1099 free (ph->username);
1101 if (ph->hostname != NULL)
1102 free (ph->hostname);
1104 if (ph->data != NULL)
1113 const char *ping_get_error (pingobj_t *obj)
1117 return (obj->errmsg);
1120 pingobj_t *ping_construct (void)
1124 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1126 memset (obj, 0, sizeof (pingobj_t));
1128 obj->timeout = PING_DEF_TIMEOUT;
1129 obj->ttl = PING_DEF_TTL;
1130 obj->addrfamily = PING_DEF_AF;
1131 obj->data = strdup (PING_DEF_DATA);
1137 void ping_destroy (pingobj_t *obj)
1139 pinghost_t *current;
1145 current = obj->head;
1148 while (current != NULL)
1150 next = current->next;
1151 ping_free (current);
1155 if (obj->data != NULL)
1158 if (obj->srcaddr != NULL)
1159 free (obj->srcaddr);
1161 if (obj->device != NULL)
1169 int ping_setopt (pingobj_t *obj, int option, void *value)
1173 if ((obj == NULL) || (value == NULL))
1182 obj->qos = *((uint8_t *) value);
1183 for (ph = obj->head; ph != NULL; ph = ph->next)
1184 ping_set_qos (obj, ph, obj->qos);
1188 case PING_OPT_TIMEOUT:
1189 obj->timeout = *((double *) value);
1190 if (obj->timeout < 0.0)
1192 obj->timeout = PING_DEF_TIMEOUT;
1198 obj->ttl = *((int *) value);
1199 if ((obj->ttl < 1) || (obj->ttl > 255))
1201 obj->ttl = PING_DEF_TTL;
1208 for (ph = obj->head; ph != NULL; ph = ph->next)
1209 ping_set_ttl (ph, obj->ttl);
1214 obj->addrfamily = *((int *) value);
1215 if ((obj->addrfamily != AF_UNSPEC)
1216 && (obj->addrfamily != AF_INET)
1217 && (obj->addrfamily != AF_INET6))
1219 obj->addrfamily = PING_DEF_AF;
1222 if (obj->srcaddr != NULL)
1224 free (obj->srcaddr);
1225 obj->srcaddr = NULL;
1230 if (obj->data != NULL)
1235 obj->data = strdup ((const char *) value);
1238 case PING_OPT_SOURCE:
1240 char *hostname = (char *) value;
1241 struct addrinfo ai_hints;
1242 struct addrinfo *ai_list;
1245 if (obj->addrfamily != AF_UNSPEC)
1247 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1250 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1251 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1252 #if defined(AI_ADDRCONFIG)
1253 ai_hints.ai_flags = AI_ADDRCONFIG;
1255 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1258 #if defined(EAI_SYSTEM)
1259 char errbuf[PING_ERRMSG_LEN];
1261 ping_set_error (obj, "getaddrinfo",
1262 #if defined(EAI_SYSTEM)
1263 (status == EAI_SYSTEM)
1264 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1266 gai_strerror (status));
1271 if (ai_list->ai_next != NULL)
1273 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1276 if (obj->srcaddr == NULL)
1278 obj->srcaddrlen = 0;
1279 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1280 if (obj->srcaddr == NULL)
1282 ping_set_errno (obj, errno);
1284 freeaddrinfo (ai_list);
1288 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1289 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1290 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1291 ai_list->ai_addrlen);
1292 obj->srcaddrlen = ai_list->ai_addrlen;
1293 obj->addrfamily = ai_list->ai_family;
1295 freeaddrinfo (ai_list);
1296 } /* case PING_OPT_SOURCE */
1299 case PING_OPT_DEVICE:
1301 #ifdef SO_BINDTODEVICE
1302 char *device = strdup ((char *) value);
1306 ping_set_errno (obj, errno);
1311 if (obj->device != NULL)
1313 obj->device = device;
1314 #else /* ! SO_BINDTODEVICE */
1315 ping_set_errno (obj, ENOTSUP);
1317 #endif /* ! SO_BINDTODEVICE */
1318 } /* case PING_OPT_DEVICE */
1323 } /* switch (option) */
1326 } /* int ping_setopt */
1329 int ping_send (pingobj_t *obj)
1336 if (ping_send_all (obj) < 0)
1339 if ((ret = ping_receive_all (obj)) < 0)
1345 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1349 if (strcasecmp (ph->username, host) == 0)
1358 int ping_host_add (pingobj_t *obj, const char *host)
1362 struct addrinfo ai_hints;
1363 struct addrinfo *ai_list, *ai_ptr;
1366 if ((obj == NULL) || (host == NULL))
1369 dprintf ("host = %s\n", host);
1371 if (ping_host_search (obj->head, host) != NULL)
1374 memset (&ai_hints, '\0', sizeof (ai_hints));
1375 ai_hints.ai_flags = 0;
1376 #ifdef AI_ADDRCONFIG
1377 ai_hints.ai_flags |= AI_ADDRCONFIG;
1380 ai_hints.ai_flags |= AI_CANONNAME;
1382 ai_hints.ai_family = obj->addrfamily;
1383 ai_hints.ai_socktype = SOCK_RAW;
1385 if ((ph = ping_alloc ()) == NULL)
1387 dprintf ("Out of memory!\n");
1391 if ((ph->username = strdup (host)) == NULL)
1393 dprintf ("Out of memory!\n");
1394 ping_set_errno (obj, errno);
1399 if ((ph->hostname = strdup (host)) == NULL)
1401 dprintf ("Out of memory!\n");
1402 ping_set_errno (obj, errno);
1407 /* obj->data is not garuanteed to be != NULL */
1408 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1410 dprintf ("Out of memory!\n");
1411 ping_set_errno (obj, errno);
1416 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1418 #if defined(EAI_SYSTEM)
1419 char errbuf[PING_ERRMSG_LEN];
1421 dprintf ("getaddrinfo failed\n");
1422 ping_set_error (obj, "getaddrinfo",
1423 #if defined(EAI_SYSTEM)
1424 (ai_return == EAI_SYSTEM)
1425 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1427 gai_strerror (ai_return));
1432 if (ai_list == NULL)
1433 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1435 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1439 if (ai_ptr->ai_family == AF_INET)
1441 ai_ptr->ai_socktype = SOCK_RAW;
1442 ai_ptr->ai_protocol = IPPROTO_ICMP;
1444 else if (ai_ptr->ai_family == AF_INET6)
1446 ai_ptr->ai_socktype = SOCK_RAW;
1447 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1451 char errmsg[PING_ERRMSG_LEN];
1453 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1454 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1456 dprintf ("%s", errmsg);
1457 ping_set_error (obj, "getaddrinfo", errmsg);
1461 /* TODO: Move this to a static function `ping_open_socket' and
1462 * call it whenever the socket dies. */
1463 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1467 char errbuf[PING_ERRMSG_LEN];
1468 dprintf ("socket: %s\n",
1469 sstrerror (errno, errbuf, sizeof (errbuf)));
1471 ping_set_errno (obj, errno);
1475 if (obj->srcaddr != NULL)
1477 assert (obj->srcaddrlen > 0);
1478 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1480 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1483 char errbuf[PING_ERRMSG_LEN];
1484 dprintf ("bind: %s\n",
1485 sstrerror (errno, errbuf, sizeof (errbuf)));
1487 ping_set_errno (obj, errno);
1494 #ifdef SO_BINDTODEVICE
1495 if (obj->device != NULL)
1497 if (setsockopt (ph->fd, SOL_SOCKET, SO_BINDTODEVICE,
1498 obj->device, strlen (obj->device) + 1) != 0)
1501 char errbuf[PING_ERRMSG_LEN];
1502 dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
1503 sstrerror (errno, errbuf, sizeof (errbuf)));
1505 ping_set_errno (obj, errno);
1511 #endif /* SO_BINDTODEVICE */
1518 status = setsockopt (ph->fd,
1519 SOL_SOCKET, SO_TIMESTAMP,
1520 &opt, sizeof (opt));
1524 char errbuf[PING_ERRMSG_LEN];
1525 dprintf ("setsockopt (SO_TIMESTAMP): %s\n",
1526 sstrerror (errno, errbuf, sizeof (errbuf)));
1528 ping_set_errno (obj, errno);
1534 #endif /* SO_TIMESTAMP */
1535 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1536 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1537 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1538 ph->addrlen = ai_ptr->ai_addrlen;
1539 ph->addrfamily = ai_ptr->ai_family;
1542 if ((ai_ptr->ai_canonname != NULL)
1543 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1547 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1548 ph->hostname, ai_ptr->ai_canonname);
1550 old_hostname = ph->hostname;
1551 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1553 /* strdup failed, falling back to old hostname */
1554 ph->hostname = old_hostname;
1556 else if (old_hostname != NULL)
1558 free (old_hostname);
1561 #endif /* AI_CANONNAME */
1563 if (ph->addrfamily == AF_INET)
1568 /* Enable receiving the TOS field */
1570 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
1571 &opt, sizeof (opt));
1572 #endif /* IP_RECVTOS */
1574 /* Enable receiving the TTL field */
1576 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1577 &opt, sizeof (opt));
1579 #if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
1580 else if (ph->addrfamily == AF_INET6)
1584 # if defined(IPV6_RECVHOPLIMIT)
1585 /* For details see RFC 3542, section 6.3. */
1587 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1588 &opt, sizeof (opt));
1589 # endif /* IPV6_RECVHOPLIMIT */
1591 # if defined(IPV6_RECVTCLASS)
1592 /* For details see RFC 3542, section 6.5. */
1594 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
1595 &opt, sizeof (opt));
1596 # endif /* IPV6_RECVTCLASS */
1598 #endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
1601 } /* for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
1603 freeaddrinfo (ai_list);
1612 * Adding in the front is much easier, but then the iterator will
1613 * return the host that was added last as first host. That's just not
1616 if (obj->head == NULL)
1625 while (hptr->next != NULL)
1628 assert ((hptr != NULL) && (hptr->next == NULL));
1632 ping_set_ttl (ph, obj->ttl);
1633 ping_set_qos (obj, ph, obj->qos);
1636 } /* int ping_host_add */
1638 int ping_host_remove (pingobj_t *obj, const char *host)
1640 pinghost_t *pre, *cur;
1642 if ((obj == NULL) || (host == NULL))
1650 if (strcasecmp (host, cur->username) == 0)
1659 ping_set_error (obj, "ping_host_remove", "Host not found");
1664 obj->head = cur->next;
1666 pre->next = cur->next;
1673 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1677 return ((pingobj_iter_t *) obj->head);
1680 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1684 return ((pingobj_iter_t *) iter->next);
1687 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1688 void *buffer, size_t *buffer_len)
1692 size_t orig_buffer_len = *buffer_len;
1694 if ((iter == NULL) || (buffer_len == NULL))
1697 if ((buffer == NULL) && (*buffer_len != 0 ))
1702 case PING_INFO_USERNAME:
1704 *buffer_len = strlen (iter->username) + 1;
1705 if (orig_buffer_len <= *buffer_len)
1707 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1708 * will copy `*buffer_len' and pad the rest of
1709 * `buffer' with null-bytes */
1710 strncpy (buffer, iter->username, orig_buffer_len);
1714 case PING_INFO_HOSTNAME:
1716 *buffer_len = strlen (iter->hostname) + 1;
1717 if (orig_buffer_len < *buffer_len)
1719 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1720 * will copy `*buffer_len' and pad the rest of
1721 * `buffer' with null-bytes */
1722 strncpy (buffer, iter->hostname, orig_buffer_len);
1726 case PING_INFO_ADDRESS:
1727 ret = getnameinfo ((struct sockaddr *) iter->addr,
1735 if ((ret == EAI_MEMORY)
1737 || (ret == EAI_OVERFLOW)
1741 #if defined(EAI_SYSTEM)
1742 else if (ret == EAI_SYSTEM)
1750 case PING_INFO_FAMILY:
1752 *buffer_len = sizeof (int);
1753 if (orig_buffer_len < sizeof (int))
1755 *((int *) buffer) = iter->addrfamily;
1759 case PING_INFO_LATENCY:
1761 *buffer_len = sizeof (double);
1762 if (orig_buffer_len < sizeof (double))
1764 *((double *) buffer) = iter->latency;
1768 case PING_INFO_DROPPED:
1770 *buffer_len = sizeof (uint32_t);
1771 if (orig_buffer_len < sizeof (uint32_t))
1773 *((uint32_t *) buffer) = iter->dropped;
1777 case PING_INFO_SEQUENCE:
1779 *buffer_len = sizeof (unsigned int);
1780 if (orig_buffer_len < sizeof (unsigned int))
1782 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1786 case PING_INFO_IDENT:
1788 *buffer_len = sizeof (uint16_t);
1789 if (orig_buffer_len < sizeof (uint16_t))
1791 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1795 case PING_INFO_DATA:
1797 *buffer_len = strlen (iter->data);
1798 if (orig_buffer_len < *buffer_len)
1800 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1804 case PING_INFO_RECV_TTL:
1806 *buffer_len = sizeof (int);
1807 if (orig_buffer_len < sizeof (int))
1809 *((int *) buffer) = iter->recv_ttl;
1813 case PING_INFO_RECV_QOS:
1815 if (*buffer_len>sizeof(unsigned)) *buffer_len=sizeof(unsigned);
1816 if (!*buffer_len) *buffer_len=1;
1817 if (orig_buffer_len < *buffer_len)
1819 memcpy(buffer,&iter->recv_qos,*buffer_len);
1825 } /* ping_iterator_get_info */
1827 void *ping_iterator_get_context (pingobj_iter_t *iter)
1831 return (iter->context);
1834 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1838 iter->context = context;