2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2009 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; only version 2 of the License is
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 <inttypes.h>
32 # error "You don't have the standard C99 header files installed"
33 #endif /* STDC_HEADERS */
47 # include <sys/types.h>
50 # include <sys/stat.h>
53 #if TIME_WITH_SYS_TIME
54 # include <sys/time.h>
58 # include <sys/time.h>
65 # include <sys/socket.h>
72 #if HAVE_NETINET_IN_SYSTM_H
73 # include <netinet/in_systm.h>
76 # include <netinet/in.h>
79 # include <netinet/ip.h>
81 #if HAVE_NETINET_IP_ICMP_H
82 # include <netinet/ip_icmp.h>
84 #ifdef HAVE_NETINET_IP_VAR_H
85 # include <netinet/ip_var.h>
87 #if HAVE_NETINET_IP6_H
88 # include <netinet/ip6.h>
90 #if HAVE_NETINET_ICMP6_H
91 # include <netinet/icmp6.h>
97 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
99 # define dprintf(...) /**/
102 #define PING_ERRMSG_LEN 256
106 /* username: name passed in by the user */
108 /* hostname: name returned by the reverse lookup */
110 struct sockaddr_storage *addr;
116 struct timeval *timer;
124 struct pinghost *next;
134 struct sockaddr *srcaddr;
135 socklen_t srcaddrlen;
137 char errmsg[PING_ERRMSG_LEN];
143 * private (static) functions
145 /* Even though Posix requires "strerror_r" to return an "int",
146 * some systems (e.g. the GNU libc) return a "char *" _and_
147 * ignore the second argument ... -tokkee */
148 char *sstrerror (int errnum, char *buf, size_t buflen)
154 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
156 /* #endif !HAVE_STRERROR_R */
158 #elif STRERROR_R_CHAR_P
161 temp = strerror_r (errnum, buf, buflen);
164 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
165 strncpy (buf, temp, buflen);
167 strncpy (buf, "strerror_r did not return "
168 "an error message", buflen);
171 /* #endif STRERROR_R_CHAR_P */
174 if (strerror_r (errnum, buf, buflen) != 0)
176 snprintf (buf, buflen, "Error %i (%#x); "
177 "Additionally, strerror_r failed.",
180 #endif /* STRERROR_R_CHAR_P */
185 } /* char *sstrerror */
187 static void ping_set_error (pingobj_t *obj, const char *function,
190 snprintf (obj->errmsg, sizeof (obj->errmsg),
191 "%s: %s", function, message);
192 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
195 static void ping_set_errno (pingobj_t *obj, int error_number)
197 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
200 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
203 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
204 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
206 while (res->tv_usec > 1000000)
208 res->tv_usec -= 1000000;
215 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
218 if ((tv1->tv_sec < tv2->tv_sec)
219 || ((tv1->tv_sec == tv2->tv_sec)
220 && (tv1->tv_usec < tv2->tv_usec)))
223 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
224 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
226 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
228 while (res->tv_usec < 0)
230 res->tv_usec += 1000000;
237 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
244 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
249 *(char *) &ret = *(char *) ptr;
253 /* Do this twice to get all possible carries.. */
254 sum = (sum >> 16) + (sum & 0xFFFF);
255 sum = (sum >> 16) + (sum & 0xFFFF);
262 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
265 struct icmp *icmp_hdr;
269 uint16_t recv_checksum;
270 uint16_t calc_checksum;
277 if (buffer_len < sizeof (struct ip))
280 ip_hdr = (struct ip *) buffer;
281 ip_hdr_len = ip_hdr->ip_hl << 2;
283 if (buffer_len < ip_hdr_len)
286 buffer += ip_hdr_len;
287 buffer_len -= ip_hdr_len;
289 if (buffer_len < sizeof (struct icmp))
292 icmp_hdr = (struct icmp *) buffer;
293 buffer += sizeof (struct icmp);
294 buffer_len -= sizeof (struct icmp);
296 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
298 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
302 recv_checksum = icmp_hdr->icmp_cksum;
303 icmp_hdr->icmp_cksum = 0;
304 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
305 sizeof (struct icmp) + buffer_len);
307 if (recv_checksum != calc_checksum)
309 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
310 recv_checksum, calc_checksum);
314 ident = ntohs (icmp_hdr->icmp_id);
315 seq = ntohs (icmp_hdr->icmp_seq);
317 for (ptr = ph; ptr != NULL; ptr = ptr->next)
319 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
320 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
322 if (ptr->addrfamily != AF_INET)
325 if (!timerisset (ptr->timer))
328 if (ptr->ident != ident)
331 if (((ptr->sequence - 1) & 0xFFFF) != seq)
334 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
335 ptr->hostname, ident, seq);
342 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
349 #ifndef ICMP6_ECHO_REQUEST
350 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
351 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
353 # define ICMP6_ECHO_REQUEST 128
357 #ifndef ICMP6_ECHO_REPLY
358 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
359 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
361 # define ICMP6_ECHO_REPLY 129
365 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
367 struct icmp6_hdr *icmp_hdr;
374 if (buffer_len < sizeof (struct icmp6_hdr))
377 icmp_hdr = (struct icmp6_hdr *) buffer;
378 buffer += sizeof (struct icmp);
379 buffer_len -= sizeof (struct icmp);
381 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
383 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
387 if (icmp_hdr->icmp6_code != 0)
389 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
393 ident = ntohs (icmp_hdr->icmp6_id);
394 seq = ntohs (icmp_hdr->icmp6_seq);
396 for (ptr = ph; ptr != NULL; ptr = ptr->next)
398 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
399 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
401 if (ptr->addrfamily != AF_INET6)
404 if (!timerisset (ptr->timer))
407 if (ptr->ident != ident)
410 if (((ptr->sequence - 1) & 0xFFFF) != seq)
413 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
414 ptr->hostname, ident, seq);
421 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
428 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
431 pinghost_t *host = NULL;
436 * Set up the receive buffer..
438 struct msghdr msghdr;
439 struct cmsghdr *cmsg;
440 char payload_buffer[4096];
441 ssize_t payload_buffer_len;
442 char control_buffer[4096];
443 struct iovec payload_iovec;
445 memset (&payload_iovec, 0, sizeof (payload_iovec));
446 payload_iovec.iov_base = payload_buffer;
447 payload_iovec.iov_len = sizeof (payload_buffer);
449 memset (&msghdr, 0, sizeof (msghdr));
450 /* unspecified source address */
451 msghdr.msg_name = NULL;
452 msghdr.msg_namelen = 0;
453 /* output buffer vector, see readv(2) */
454 msghdr.msg_iov = &payload_iovec;
455 msghdr.msg_iovlen = 1;
456 /* output buffer for control messages */
457 msghdr.msg_control = control_buffer;
458 msghdr.msg_controllen = sizeof (control_buffer);
459 /* flags; this is an output only field.. */
460 msghdr.msg_flags = 0;
462 msghdr.msg_flags |= MSG_XPG4_2;
465 payload_buffer_len = recvmsg (fd, &msghdr, /* flags = */ 0);
466 if (payload_buffer_len < 0)
469 char errbuf[PING_ERRMSG_LEN];
470 dprintf ("recvfrom: %s\n",
471 sstrerror (errno, errbuf, sizeof (errbuf)));
475 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, fd);
477 /* Iterate over all auxiliary data in msghdr */
480 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
482 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
484 if (cmsg->cmsg_level == IPPROTO_IP) /* {{{ */
487 if (cmsg->cmsg_type == IP_TTL)
489 memcpy (&recv_ttl, CMSG_DATA (cmsg),
491 dprintf ("TTLv4 = %i;\n", recv_ttl);
495 dprintf ("Not handling option %i.\n",
499 else if (cmsg->cmsg_level == IPPROTO_IPV6) /* {{{ */
502 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
504 memcpy (&recv_ttl, CMSG_DATA (cmsg),
506 dprintf ("TTLv6 = %i;\n", recv_ttl);
510 dprintf ("Not handling option %i.\n",
516 dprintf ("Don't know how to handle "
517 "unknown protocol %i.\n",
520 } /* }}} for (cmsg) */
522 if (family == AF_INET)
524 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
528 else if (family == AF_INET6)
530 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
536 dprintf ("ping_receive_one: Unknown address family %i.\n",
541 dprintf ("rcvd: %12i.%06i\n",
544 dprintf ("sent: %12i.%06i\n",
545 (int) host->timer->tv_sec,
546 (int) host->timer->tv_usec);
548 if (ping_timeval_sub (now, host->timer, &diff) < 0)
550 timerclear (host->timer);
554 dprintf ("diff: %12i.%06i\n",
558 host->latency = ((double) diff.tv_usec) / 1000.0;
559 host->latency += ((double) diff.tv_sec) * 1000.0;
561 host->recv_ttl = recv_ttl;
563 timerclear (host->timer);
568 static int ping_receive_all (pingobj_t *obj)
577 struct timeval endtime;
578 struct timeval nowtime;
579 struct timeval timeout;
587 for (ptr = ph; ptr != NULL; ptr = ptr->next)
590 if (gettimeofday (&nowtime, NULL) == -1)
592 ping_set_errno (obj, errno);
597 timeout.tv_sec = (time_t) obj->timeout;
598 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
600 dprintf ("Set timeout to %i.%06i seconds\n",
601 (int) timeout.tv_sec,
602 (int) timeout.tv_usec);
604 ping_timeval_add (&nowtime, &timeout, &endtime);
612 for (ptr = ph; ptr != NULL; ptr = ptr->next)
614 if (!timerisset (ptr->timer))
617 FD_SET (ptr->fd, &readfds);
620 if (max_readfds < ptr->fd)
621 max_readfds = ptr->fd;
624 if (num_readfds == 0)
627 if (gettimeofday (&nowtime, NULL) == -1)
629 ping_set_errno (obj, errno);
633 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
636 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
637 (int) timeout.tv_sec,
638 (int) timeout.tv_usec);
640 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
642 if (gettimeofday (&nowtime, NULL) == -1)
644 ping_set_errno (obj, errno);
648 if ((status == -1) && (errno == EINTR))
650 dprintf ("select was interrupted by signal..\n");
656 char errbuf[PING_ERRMSG_LEN];
657 dprintf ("select: %s\n",
658 sstrerror (errno, errbuf, sizeof (errbuf)));
662 else if (status == 0)
664 dprintf ("select timed out\n");
665 for (ptr = ph; ptr != NULL; ptr = ptr->next)
666 if (ptr->latency < 0.0)
671 for (ptr = ph; ptr != NULL; ptr = ptr->next)
673 if (FD_ISSET (ptr->fd, &readfds))
674 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
682 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
683 * Sending functions: *
686 * +-> ping_send_one_ipv4 *
687 * `-> ping_send_one_ipv6 *
688 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
689 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
690 const void *buf, size_t buflen)
694 if (gettimeofday (ph->timer, NULL) == -1)
696 timerclear (ph->timer);
700 ret = sendto (ph->fd, buf, buflen, 0,
701 (struct sockaddr *) ph->addr, ph->addrlen);
705 #if defined(EHOSTUNREACH)
706 if (errno == EHOSTUNREACH)
709 #if defined(ENETUNREACH)
710 if (errno == ENETUNREACH)
713 ping_set_errno (obj, errno);
719 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
730 dprintf ("ph->hostname = %s\n", ph->hostname);
732 memset (buf, '\0', sizeof (buf));
733 icmp4 = (struct icmp *) buf;
734 data = (char *) (icmp4 + 1);
736 icmp4->icmp_type = ICMP_ECHO;
737 icmp4->icmp_code = 0;
738 icmp4->icmp_cksum = 0;
739 icmp4->icmp_id = htons (ph->ident);
740 icmp4->icmp_seq = htons (ph->sequence);
742 buflen = 4096 - sizeof (struct icmp);
743 strncpy (data, ph->data, buflen);
744 datalen = strlen (data);
746 buflen = datalen + sizeof (struct icmp);
748 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
750 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
752 status = ping_sendto (obj, ph, buf, buflen);
755 perror ("ping_sendto");
759 dprintf ("sendto: status = %i\n", status);
764 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
766 struct icmp6_hdr *icmp6;
775 dprintf ("ph->hostname = %s\n", ph->hostname);
777 memset (buf, '\0', sizeof (buf));
778 icmp6 = (struct icmp6_hdr *) buf;
779 data = (char *) (icmp6 + 1);
781 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
782 icmp6->icmp6_code = 0;
783 /* The checksum will be calculated by the TCP/IP stack. */
785 icmp6->icmp6_cksum = 0;
786 icmp6->icmp6_id = htons (ph->ident);
787 icmp6->icmp6_seq = htons (ph->sequence);
789 buflen = 4096 - sizeof (struct icmp6_hdr);
790 strncpy (data, ph->data, buflen);
791 datalen = strlen (data);
793 buflen = datalen + sizeof (struct icmp6_hdr);
795 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
797 status = ping_sendto (obj, ph, buf, buflen);
800 perror ("ping_sendto");
804 dprintf ("sendto: status = %i\n", status);
809 static int ping_send_all (pingobj_t *obj)
819 for (ptr = ph; ptr != NULL; ptr = ptr->next)
821 /* start timer.. The GNU `ping6' starts the timer before
822 * sending the packet, so I will do that too */
823 if (gettimeofday (ptr->timer, NULL) == -1)
826 char errbuf[PING_ERRMSG_LEN];
827 dprintf ("gettimeofday: %s\n",
828 sstrerror (errno, errbuf, sizeof (errbuf)));
830 timerclear (ptr->timer);
836 dprintf ("timer set for hostname = %s\n", ptr->hostname);
839 if (ptr->addrfamily == AF_INET6)
841 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
842 if (ping_send_one_ipv6 (obj, ptr) != 0)
844 timerclear (ptr->timer);
849 else if (ptr->addrfamily == AF_INET)
851 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
852 if (ping_send_one_ipv4 (obj, ptr) != 0)
854 timerclear (ptr->timer);
859 else /* this should not happen */
861 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
862 timerclear (ptr->timer);
874 * Set the TTL of a socket protocol independently.
876 static int ping_set_ttl (pinghost_t *ph, int ttl)
880 if (ph->addrfamily == AF_INET)
882 dprintf ("Setting TTLv4 to %i\n", ttl);
883 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
886 else if (ph->addrfamily == AF_INET6)
888 dprintf ("Setting TTLv6 to %i\n", ttl);
889 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
896 static int ping_get_ident (void)
899 static int did_seed = 0;
905 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
909 if (read (fd, &seed, sizeof (seed)) != -1)
912 dprintf ("Random seed: %#x\n", seed);
921 char errbuf[PING_ERRMSG_LEN];
922 dprintf ("open (/dev/urandom): %s\n",
923 sstrerror (errno, errbuf, sizeof (errbuf)));
928 retval = (int) random ();
930 dprintf ("Random number: %#x\n", retval);
935 static pinghost_t *ping_alloc (void)
940 ph_size = sizeof (pinghost_t)
941 + sizeof (struct sockaddr_storage)
942 + sizeof (struct timeval);
944 ph = (pinghost_t *) malloc (ph_size);
948 memset (ph, '\0', ph_size);
950 ph->timer = (struct timeval *) (ph + 1);
951 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
953 ph->addrlen = sizeof (struct sockaddr_storage);
957 ph->ident = ping_get_ident () & 0xFFFF;
962 static void ping_free (pinghost_t *ph)
967 if (ph->username != NULL)
970 if (ph->hostname != NULL)
973 if (ph->data != NULL)
982 const char *ping_get_error (pingobj_t *obj)
984 return (obj->errmsg);
987 pingobj_t *ping_construct (void)
991 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
993 memset (obj, '\0', sizeof (pingobj_t));
995 obj->timeout = PING_DEF_TIMEOUT;
996 obj->ttl = PING_DEF_TTL;
997 obj->addrfamily = PING_DEF_AF;
998 obj->data = strdup (PING_DEF_DATA);
1003 void ping_destroy (pingobj_t *obj)
1005 pinghost_t *current;
1008 current = obj->head;
1011 while (current != NULL)
1013 next = current->next;
1014 ping_free (current);
1018 if (obj->data != NULL)
1021 if (obj->srcaddr != NULL)
1022 free (obj->srcaddr);
1029 int ping_setopt (pingobj_t *obj, int option, void *value)
1035 case PING_OPT_TIMEOUT:
1036 obj->timeout = *((double *) value);
1037 if (obj->timeout < 0.0)
1039 obj->timeout = PING_DEF_TIMEOUT;
1045 obj->ttl = *((int *) value);
1046 if ((obj->ttl < 1) || (obj->ttl > 255))
1048 obj->ttl = PING_DEF_TTL;
1055 for (ph = obj->head; ph != NULL; ph = ph->next)
1056 ping_set_ttl (ph, obj->ttl);
1061 obj->addrfamily = *((int *) value);
1062 if ((obj->addrfamily != AF_UNSPEC)
1063 && (obj->addrfamily != AF_INET)
1064 && (obj->addrfamily != AF_INET6))
1066 obj->addrfamily = PING_DEF_AF;
1069 if (obj->srcaddr != NULL)
1071 free (obj->srcaddr);
1072 obj->srcaddr = NULL;
1077 if (obj->data != NULL)
1082 obj->data = strdup ((const char *) value);
1085 case PING_OPT_SOURCE:
1087 char *hostname = (char *) value;
1088 struct addrinfo ai_hints;
1089 struct addrinfo *ai_list;
1092 if (obj->addrfamily != AF_UNSPEC)
1094 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1097 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1098 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1099 #if defined(AI_ADDRCONFIG)
1100 ai_hints.ai_flags = AI_ADDRCONFIG;
1102 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1105 #if defined(EAI_SYSTEM)
1106 char errbuf[PING_ERRMSG_LEN];
1108 ping_set_error (obj, "getaddrinfo",
1109 #if defined(EAI_SYSTEM)
1110 (status == EAI_SYSTEM)
1111 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1113 gai_strerror (status));
1118 if (ai_list->ai_next != NULL)
1120 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1123 if (obj->srcaddr == NULL)
1125 obj->srcaddrlen = 0;
1126 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1127 if (obj->srcaddr == NULL)
1129 ping_set_errno (obj, errno);
1131 freeaddrinfo (ai_list);
1135 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1136 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1137 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1138 ai_list->ai_addrlen);
1139 obj->srcaddrlen = ai_list->ai_addrlen;
1140 obj->addrfamily = ai_list->ai_family;
1142 freeaddrinfo (ai_list);
1143 } /* case PING_OPT_SOURCE */
1148 } /* switch (option) */
1151 } /* int ping_setopt */
1154 int ping_send (pingobj_t *obj)
1158 if (ping_send_all (obj) < 0)
1161 if ((ret = ping_receive_all (obj)) < 0)
1167 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1171 if (strcasecmp (ph->username, host) == 0)
1180 int ping_host_add (pingobj_t *obj, const char *host)
1184 struct addrinfo ai_hints;
1185 struct addrinfo *ai_list, *ai_ptr;
1188 dprintf ("host = %s\n", host);
1190 if (ping_host_search (obj->head, host) != NULL)
1193 memset (&ai_hints, '\0', sizeof (ai_hints));
1194 ai_hints.ai_flags = 0;
1195 #ifdef AI_ADDRCONFIG
1196 ai_hints.ai_flags |= AI_ADDRCONFIG;
1199 ai_hints.ai_flags |= AI_CANONNAME;
1201 ai_hints.ai_family = obj->addrfamily;
1202 ai_hints.ai_socktype = SOCK_RAW;
1204 if ((ph = ping_alloc ()) == NULL)
1206 dprintf ("Out of memory!\n");
1210 if ((ph->username = strdup (host)) == NULL)
1212 dprintf ("Out of memory!\n");
1213 ping_set_errno (obj, errno);
1218 if ((ph->hostname = strdup (host)) == NULL)
1220 dprintf ("Out of memory!\n");
1221 ping_set_errno (obj, errno);
1226 /* obj->data is not garuanteed to be != NULL */
1227 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1229 dprintf ("Out of memory!\n");
1230 ping_set_errno (obj, errno);
1235 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1237 #if defined(EAI_SYSTEM)
1238 char errbuf[PING_ERRMSG_LEN];
1240 dprintf ("getaddrinfo failed\n");
1241 ping_set_error (obj, "getaddrinfo",
1242 #if defined(EAI_SYSTEM)
1243 (ai_return == EAI_SYSTEM)
1244 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1246 gai_strerror (ai_return));
1251 if (ai_list == NULL)
1252 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1254 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1258 if (ai_ptr->ai_family == AF_INET)
1260 ai_ptr->ai_socktype = SOCK_RAW;
1261 ai_ptr->ai_protocol = IPPROTO_ICMP;
1263 else if (ai_ptr->ai_family == AF_INET6)
1265 ai_ptr->ai_socktype = SOCK_RAW;
1266 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1270 char errmsg[PING_ERRMSG_LEN];
1272 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1273 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1276 ping_set_error (obj, "getaddrinfo", errmsg);
1280 /* TODO: Move this to a static function `ping_open_socket' and
1281 * call it whenever the socket dies. */
1282 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1286 char errbuf[PING_ERRMSG_LEN];
1287 dprintf ("socket: %s\n",
1288 sstrerror (errno, errbuf, sizeof (errbuf)));
1290 ping_set_errno (obj, errno);
1294 if (obj->srcaddr != NULL)
1296 assert (obj->srcaddrlen > 0);
1297 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1299 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1302 char errbuf[PING_ERRMSG_LEN];
1303 dprintf ("bind: %s\n",
1304 sstrerror (errno, errbuf, sizeof (errbuf)));
1306 ping_set_errno (obj, errno);
1313 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1314 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1315 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1316 ph->addrlen = ai_ptr->ai_addrlen;
1317 ph->addrfamily = ai_ptr->ai_family;
1320 if ((ai_ptr->ai_canonname != NULL)
1321 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1325 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1326 ph->hostname, ai_ptr->ai_canonname);
1328 old_hostname = ph->hostname;
1329 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1331 /* strdup failed, falling back to old hostname */
1332 ph->hostname = old_hostname;
1334 else if (old_hostname != NULL)
1336 free (old_hostname);
1339 #endif /* AI_CANONNAME */
1341 if (ph->addrfamily == AF_INET)
1345 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1346 &opt, sizeof (opt));
1348 else if (ph->addrfamily == AF_INET6)
1352 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1353 &opt, sizeof (opt));
1359 freeaddrinfo (ai_list);
1368 * Adding in the front is much easier, but then the iterator will
1369 * return the host that was added last as first host. That's just not
1372 if (obj->head == NULL)
1381 while (hptr->next != NULL)
1384 assert ((hptr != NULL) && (hptr->next == NULL));
1388 ping_set_ttl (ph, obj->ttl);
1391 } /* int ping_host_add */
1393 int ping_host_remove (pingobj_t *obj, const char *host)
1395 pinghost_t *pre, *cur;
1402 if (strcasecmp (host, cur->username) == 0)
1411 ping_set_error (obj, "ping_host_remove", "Host not found");
1416 obj->head = cur->next;
1418 pre->next = cur->next;
1425 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1427 return ((pingobj_iter_t *) obj->head);
1430 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1432 return ((pingobj_iter_t *) iter->next);
1435 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1436 void *buffer, size_t *buffer_len)
1440 size_t orig_buffer_len = *buffer_len;
1444 case PING_INFO_USERNAME:
1446 *buffer_len = strlen (iter->username) + 1;
1447 if (orig_buffer_len <= *buffer_len)
1449 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1450 * will copy `*buffer_len' and pad the rest of
1451 * `buffer' with null-bytes */
1452 strncpy (buffer, iter->username, orig_buffer_len);
1456 case PING_INFO_HOSTNAME:
1458 *buffer_len = strlen (iter->hostname) + 1;
1459 if (orig_buffer_len < *buffer_len)
1461 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1462 * will copy `*buffer_len' and pad the rest of
1463 * `buffer' with null-bytes */
1464 strncpy (buffer, iter->hostname, orig_buffer_len);
1468 case PING_INFO_ADDRESS:
1469 ret = getnameinfo ((struct sockaddr *) iter->addr,
1477 if ((ret == EAI_MEMORY)
1479 || (ret == EAI_OVERFLOW)
1483 #if defined(EAI_SYSTEM)
1484 else if (ret == EAI_SYSTEM)
1492 case PING_INFO_FAMILY:
1494 *buffer_len = sizeof (int);
1495 if (orig_buffer_len < sizeof (int))
1497 *((int *) buffer) = iter->addrfamily;
1501 case PING_INFO_LATENCY:
1503 *buffer_len = sizeof (double);
1504 if (orig_buffer_len < sizeof (double))
1506 *((double *) buffer) = iter->latency;
1510 case PING_INFO_DROPPED:
1512 *buffer_len = sizeof (uint32_t);
1513 if (orig_buffer_len < sizeof (uint32_t))
1515 *((uint32_t *) buffer) = iter->dropped;
1519 case PING_INFO_SEQUENCE:
1521 *buffer_len = sizeof (unsigned int);
1522 if (orig_buffer_len < sizeof (unsigned int))
1524 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1528 case PING_INFO_IDENT:
1530 *buffer_len = sizeof (uint16_t);
1531 if (orig_buffer_len < sizeof (uint16_t))
1533 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1537 case PING_INFO_DATA:
1539 *buffer_len = strlen (iter->data);
1540 if (orig_buffer_len < *buffer_len)
1542 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1546 case PING_INFO_RECV_TTL:
1548 *buffer_len = sizeof (int);
1549 if (orig_buffer_len < sizeof (int))
1551 *((int *) buffer) = iter->recv_ttl;
1557 } /* ping_iterator_get_info */
1559 void *ping_iterator_get_context (pingobj_iter_t *iter)
1561 return (iter->context);
1564 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1566 iter->context = context;