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
29 # include <inttypes.h>
33 # error "You don't have the standard C99 header files installed"
34 #endif /* STDC_HEADERS */
44 # include <sys/types.h>
47 # include <sys/stat.h>
50 #if TIME_WITH_SYS_TIME
51 # include <sys/time.h>
55 # include <sys/time.h>
62 # include <sys/socket.h>
68 #if HAVE_NETINET_IN_SYSTM_H
69 # include <netinet/in_systm.h>
72 # include <netinet/in.h>
75 # include <netinet/ip.h>
77 #if HAVE_NETINET_IP_ICMP_H
78 # include <netinet/ip_icmp.h>
80 #ifdef HAVE_NETINET_IP_VAR_H
81 # include <netinet/ip_var.h>
83 #if HAVE_NETINET_IP6_H
84 # include <netinet/ip6.h>
86 #if HAVE_NETINET_ICMP6_H
87 # include <netinet/icmp6.h>
93 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
95 # define dprintf(...) /**/
98 #define PING_ERRMSG_LEN 256
102 /* username: name passed in by the user */
104 /* hostname: name returned by the reverse lookup */
106 struct sockaddr_storage *addr;
112 struct timeval *timer;
120 struct pinghost *next;
130 struct sockaddr *srcaddr;
131 socklen_t srcaddrlen;
133 char errmsg[PING_ERRMSG_LEN];
139 * private (static) functions
141 /* Even though Posix requires "strerror_r" to return an "int",
142 * some systems (e.g. the GNU libc) return a "char *" _and_
143 * ignore the second argument ... -tokkee */
144 char *sstrerror (int errnum, char *buf, size_t buflen)
150 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
152 /* #endif !HAVE_STRERROR_R */
154 #elif STRERROR_R_CHAR_P
157 temp = strerror_r (errnum, buf, buflen);
160 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
161 strncpy (buf, temp, buflen);
163 strncpy (buf, "strerror_r did not return "
164 "an error message", buflen);
167 /* #endif STRERROR_R_CHAR_P */
170 if (strerror_r (errnum, buf, buflen) != 0)
172 snprintf (buf, buflen, "Error %i (%#x); "
173 "Additionally, strerror_r failed.",
176 #endif /* STRERROR_R_CHAR_P */
181 } /* char *sstrerror */
183 static void ping_set_error (pingobj_t *obj, const char *function,
186 snprintf (obj->errmsg, sizeof (obj->errmsg),
187 "%s: %s", function, message);
188 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
191 static void ping_set_errno (pingobj_t *obj, int error_number)
193 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
196 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
199 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
200 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
202 while (res->tv_usec > 1000000)
204 res->tv_usec -= 1000000;
211 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
214 if ((tv1->tv_sec < tv2->tv_sec)
215 || ((tv1->tv_sec == tv2->tv_sec)
216 && (tv1->tv_usec < tv2->tv_usec)))
219 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
220 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
222 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
224 while (res->tv_usec < 0)
226 res->tv_usec += 1000000;
233 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
240 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
245 *(char *) &ret = *(char *) ptr;
249 /* Do this twice to get all possible carries.. */
250 sum = (sum >> 16) + (sum & 0xFFFF);
251 sum = (sum >> 16) + (sum & 0xFFFF);
258 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
261 struct icmp *icmp_hdr;
265 uint16_t recv_checksum;
266 uint16_t calc_checksum;
273 if (buffer_len < sizeof (struct ip))
276 ip_hdr = (struct ip *) buffer;
277 ip_hdr_len = ip_hdr->ip_hl << 2;
279 if (buffer_len < ip_hdr_len)
282 buffer += ip_hdr_len;
283 buffer_len -= ip_hdr_len;
285 if (buffer_len < sizeof (struct icmp))
288 icmp_hdr = (struct icmp *) buffer;
289 buffer += sizeof (struct icmp);
290 buffer_len -= sizeof (struct icmp);
292 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
294 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
298 recv_checksum = icmp_hdr->icmp_cksum;
299 icmp_hdr->icmp_cksum = 0;
300 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
301 sizeof (struct icmp) + buffer_len);
303 if (recv_checksum != calc_checksum)
305 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
306 recv_checksum, calc_checksum);
310 ident = ntohs (icmp_hdr->icmp_id);
311 seq = ntohs (icmp_hdr->icmp_seq);
313 for (ptr = ph; ptr != NULL; ptr = ptr->next)
315 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
316 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
318 if (ptr->addrfamily != AF_INET)
321 if (!timerisset (ptr->timer))
324 if (ptr->ident != ident)
327 if (((ptr->sequence - 1) & 0xFFFF) != seq)
330 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
331 ptr->hostname, ident, seq);
338 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
345 #ifndef ICMP6_ECHO_REQUEST
346 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
347 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
349 # define ICMP6_ECHO_REQUEST 128
353 #ifndef ICMP6_ECHO_REPLY
354 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
355 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
357 # define ICMP6_ECHO_REPLY 129
361 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
363 struct icmp6_hdr *icmp_hdr;
370 if (buffer_len < sizeof (struct icmp6_hdr))
373 icmp_hdr = (struct icmp6_hdr *) buffer;
374 buffer += sizeof (struct icmp);
375 buffer_len -= sizeof (struct icmp);
377 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
379 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
383 if (icmp_hdr->icmp6_code != 0)
385 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
389 ident = ntohs (icmp_hdr->icmp6_id);
390 seq = ntohs (icmp_hdr->icmp6_seq);
392 for (ptr = ph; ptr != NULL; ptr = ptr->next)
394 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
395 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
397 if (ptr->addrfamily != AF_INET6)
400 if (!timerisset (ptr->timer))
403 if (ptr->ident != ident)
406 if (((ptr->sequence - 1) & 0xFFFF) != seq)
409 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
410 ptr->hostname, ident, seq);
417 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
424 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
427 pinghost_t *host = NULL;
432 * Set up the receive buffer..
434 struct msghdr msghdr;
435 struct cmsghdr *cmsg;
436 char payload_buffer[4096];
437 ssize_t payload_buffer_len;
438 char control_buffer[4096];
439 struct iovec payload_iovec;
441 memset (&payload_iovec, 0, sizeof (payload_iovec));
442 payload_iovec.iov_base = payload_buffer;
443 payload_iovec.iov_len = sizeof (payload_buffer);
445 memset (&msghdr, 0, sizeof (msghdr));
446 /* unspecified source address */
447 msghdr.msg_name = NULL;
448 msghdr.msg_namelen = 0;
449 /* output buffer vector, see readv(2) */
450 msghdr.msg_iov = &payload_iovec;
451 msghdr.msg_iovlen = 1;
452 /* output buffer for control messages */
453 msghdr.msg_control = control_buffer;
454 msghdr.msg_controllen = sizeof (control_buffer);
455 /* flags; this is an output only field.. */
456 msghdr.msg_flags = 0;
458 msghdr.msg_flags |= MSG_XPG4_2;
461 payload_buffer_len = recvmsg (fd, &msghdr, /* flags = */ 0);
462 if (payload_buffer_len < 0)
465 char errbuf[PING_ERRMSG_LEN];
466 dprintf ("recvfrom: %s\n",
467 sstrerror (errno, errbuf, sizeof (errbuf)));
471 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, fd);
473 /* Iterate over all auxiliary data in msghdr */
476 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
478 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
480 if (cmsg->cmsg_level == IPPROTO_IP) /* {{{ */
483 if (cmsg->cmsg_type == IP_TTL)
485 memcpy (&recv_ttl, CMSG_DATA (cmsg),
487 dprintf ("TTLv4 = %i;\n", recv_ttl);
491 dprintf ("Not handling option %i.\n",
495 else if (cmsg->cmsg_level == IPPROTO_IPV6) /* {{{ */
498 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
500 memcpy (&recv_ttl, CMSG_DATA (cmsg),
502 dprintf ("TTLv6 = %i;\n", recv_ttl);
506 dprintf ("Not handling option %i.\n",
512 dprintf ("Don't know how to handle "
513 "unknown protocol %i.\n",
516 } /* }}} for (cmsg) */
518 if (family == AF_INET)
520 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
524 else if (family == AF_INET6)
526 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
532 dprintf ("ping_receive_one: Unknown address family %i.\n",
537 dprintf ("rcvd: %12i.%06i\n",
540 dprintf ("sent: %12i.%06i\n",
541 (int) host->timer->tv_sec,
542 (int) host->timer->tv_usec);
544 if (ping_timeval_sub (now, host->timer, &diff) < 0)
546 timerclear (host->timer);
550 dprintf ("diff: %12i.%06i\n",
554 host->latency = ((double) diff.tv_usec) / 1000.0;
555 host->latency += ((double) diff.tv_sec) * 1000.0;
557 host->recv_ttl = recv_ttl;
559 timerclear (host->timer);
564 static int ping_receive_all (pingobj_t *obj)
573 struct timeval endtime;
574 struct timeval nowtime;
575 struct timeval timeout;
583 for (ptr = ph; ptr != NULL; ptr = ptr->next)
586 if (gettimeofday (&nowtime, NULL) == -1)
588 ping_set_errno (obj, errno);
593 timeout.tv_sec = (time_t) obj->timeout;
594 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
596 dprintf ("Set timeout to %i.%06i seconds\n",
597 (int) timeout.tv_sec,
598 (int) timeout.tv_usec);
600 ping_timeval_add (&nowtime, &timeout, &endtime);
608 for (ptr = ph; ptr != NULL; ptr = ptr->next)
610 if (!timerisset (ptr->timer))
613 FD_SET (ptr->fd, &readfds);
616 if (max_readfds < ptr->fd)
617 max_readfds = ptr->fd;
620 if (num_readfds == 0)
623 if (gettimeofday (&nowtime, NULL) == -1)
625 ping_set_errno (obj, errno);
629 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
632 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
633 (int) timeout.tv_sec,
634 (int) timeout.tv_usec);
636 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
638 if (gettimeofday (&nowtime, NULL) == -1)
640 ping_set_errno (obj, errno);
644 if ((status == -1) && (errno == EINTR))
646 dprintf ("select was interrupted by signal..\n");
652 char errbuf[PING_ERRMSG_LEN];
653 dprintf ("select: %s\n",
654 sstrerror (errno, errbuf, sizeof (errbuf)));
658 else if (status == 0)
660 dprintf ("select timed out\n");
661 for (ptr = ph; ptr != NULL; ptr = ptr->next)
662 if (ptr->latency < 0.0)
667 for (ptr = ph; ptr != NULL; ptr = ptr->next)
669 if (FD_ISSET (ptr->fd, &readfds))
670 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
678 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
679 * Sending functions: *
682 * +-> ping_send_one_ipv4 *
683 * `-> ping_send_one_ipv6 *
684 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
685 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
686 const void *buf, size_t buflen)
690 if (gettimeofday (ph->timer, NULL) == -1)
692 timerclear (ph->timer);
696 ret = sendto (ph->fd, buf, buflen, 0,
697 (struct sockaddr *) ph->addr, ph->addrlen);
701 #if defined(EHOSTUNREACH)
702 if (errno == EHOSTUNREACH)
705 #if defined(ENETUNREACH)
706 if (errno == ENETUNREACH)
709 ping_set_errno (obj, errno);
715 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
726 dprintf ("ph->hostname = %s\n", ph->hostname);
728 memset (buf, '\0', sizeof (buf));
729 icmp4 = (struct icmp *) buf;
730 data = (char *) (icmp4 + 1);
732 icmp4->icmp_type = ICMP_ECHO;
733 icmp4->icmp_code = 0;
734 icmp4->icmp_cksum = 0;
735 icmp4->icmp_id = htons (ph->ident);
736 icmp4->icmp_seq = htons (ph->sequence);
738 buflen = 4096 - sizeof (struct icmp);
739 strncpy (data, ph->data, buflen);
740 datalen = strlen (data);
742 buflen = datalen + sizeof (struct icmp);
744 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
746 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
748 status = ping_sendto (obj, ph, buf, buflen);
751 perror ("ping_sendto");
755 dprintf ("sendto: status = %i\n", status);
760 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
762 struct icmp6_hdr *icmp6;
771 dprintf ("ph->hostname = %s\n", ph->hostname);
773 memset (buf, '\0', sizeof (buf));
774 icmp6 = (struct icmp6_hdr *) buf;
775 data = (char *) (icmp6 + 1);
777 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
778 icmp6->icmp6_code = 0;
779 /* The checksum will be calculated by the TCP/IP stack. */
781 icmp6->icmp6_cksum = 0;
782 icmp6->icmp6_id = htons (ph->ident);
783 icmp6->icmp6_seq = htons (ph->sequence);
785 buflen = 4096 - sizeof (struct icmp6_hdr);
786 strncpy (data, ph->data, buflen);
787 datalen = strlen (data);
789 buflen = datalen + sizeof (struct icmp6_hdr);
791 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
793 status = ping_sendto (obj, ph, buf, buflen);
796 perror ("ping_sendto");
800 dprintf ("sendto: status = %i\n", status);
805 static int ping_send_all (pingobj_t *obj)
815 for (ptr = ph; ptr != NULL; ptr = ptr->next)
817 /* start timer.. The GNU `ping6' starts the timer before
818 * sending the packet, so I will do that too */
819 if (gettimeofday (ptr->timer, NULL) == -1)
822 char errbuf[PING_ERRMSG_LEN];
823 dprintf ("gettimeofday: %s\n",
824 sstrerror (errno, errbuf, sizeof (errbuf)));
826 timerclear (ptr->timer);
832 dprintf ("timer set for hostname = %s\n", ptr->hostname);
835 if (ptr->addrfamily == AF_INET6)
837 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
838 if (ping_send_one_ipv6 (obj, ptr) != 0)
840 timerclear (ptr->timer);
845 else if (ptr->addrfamily == AF_INET)
847 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
848 if (ping_send_one_ipv4 (obj, ptr) != 0)
850 timerclear (ptr->timer);
855 else /* this should not happen */
857 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
858 timerclear (ptr->timer);
870 * Set the TTL of a socket protocol independently.
872 static int ping_set_ttl (pinghost_t *ph, int ttl)
876 if (ph->addrfamily == AF_INET)
878 dprintf ("Setting TTLv4 to %i\n", ttl);
879 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
882 else if (ph->addrfamily == AF_INET6)
884 dprintf ("Setting TTLv6 to %i\n", ttl);
885 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
892 static int ping_get_ident (void)
895 static int did_seed = 0;
901 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
905 if (read (fd, &seed, sizeof (seed)) != -1)
908 dprintf ("Random seed: %#x\n", seed);
917 char errbuf[PING_ERRMSG_LEN];
918 dprintf ("open (/dev/urandom): %s\n",
919 sstrerror (errno, errbuf, sizeof (errbuf)));
924 retval = (int) random ();
926 dprintf ("Random number: %#x\n", retval);
931 static pinghost_t *ping_alloc (void)
936 ph_size = sizeof (pinghost_t)
937 + sizeof (struct sockaddr_storage)
938 + sizeof (struct timeval);
940 ph = (pinghost_t *) malloc (ph_size);
944 memset (ph, '\0', ph_size);
946 ph->timer = (struct timeval *) (ph + 1);
947 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
949 ph->addrlen = sizeof (struct sockaddr_storage);
953 ph->ident = ping_get_ident () & 0xFFFF;
958 static void ping_free (pinghost_t *ph)
963 if (ph->username != NULL)
966 if (ph->hostname != NULL)
969 if (ph->data != NULL)
978 const char *ping_get_error (pingobj_t *obj)
980 return (obj->errmsg);
983 pingobj_t *ping_construct (void)
987 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
989 memset (obj, '\0', sizeof (pingobj_t));
991 obj->timeout = PING_DEF_TIMEOUT;
992 obj->ttl = PING_DEF_TTL;
993 obj->addrfamily = PING_DEF_AF;
994 obj->data = strdup (PING_DEF_DATA);
999 void ping_destroy (pingobj_t *obj)
1001 pinghost_t *current;
1004 current = obj->head;
1007 while (current != NULL)
1009 next = current->next;
1010 ping_free (current);
1014 if (obj->data != NULL)
1017 if (obj->srcaddr != NULL)
1018 free (obj->srcaddr);
1025 int ping_setopt (pingobj_t *obj, int option, void *value)
1031 case PING_OPT_TIMEOUT:
1032 obj->timeout = *((double *) value);
1033 if (obj->timeout < 0.0)
1035 obj->timeout = PING_DEF_TIMEOUT;
1041 obj->ttl = *((int *) value);
1042 if ((obj->ttl < 1) || (obj->ttl > 255))
1044 obj->ttl = PING_DEF_TTL;
1051 for (ph = obj->head; ph != NULL; ph = ph->next)
1052 ping_set_ttl (ph, obj->ttl);
1057 obj->addrfamily = *((int *) value);
1058 if ((obj->addrfamily != AF_UNSPEC)
1059 && (obj->addrfamily != AF_INET)
1060 && (obj->addrfamily != AF_INET6))
1062 obj->addrfamily = PING_DEF_AF;
1065 if (obj->srcaddr != NULL)
1067 free (obj->srcaddr);
1068 obj->srcaddr = NULL;
1073 if (obj->data != NULL)
1078 obj->data = strdup ((const char *) value);
1081 case PING_OPT_SOURCE:
1083 char *hostname = (char *) value;
1084 struct addrinfo ai_hints;
1085 struct addrinfo *ai_list;
1088 if (obj->addrfamily != AF_UNSPEC)
1090 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1093 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1094 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1095 #if defined(AI_ADDRCONFIG)
1096 ai_hints.ai_flags = AI_ADDRCONFIG;
1098 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1101 #if defined(EAI_SYSTEM)
1102 char errbuf[PING_ERRMSG_LEN];
1104 ping_set_error (obj, "getaddrinfo",
1105 #if defined(EAI_SYSTEM)
1106 (status == EAI_SYSTEM)
1107 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1109 gai_strerror (status));
1114 if (ai_list->ai_next != NULL)
1116 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1119 if (obj->srcaddr == NULL)
1121 obj->srcaddrlen = 0;
1122 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1123 if (obj->srcaddr == NULL)
1125 ping_set_errno (obj, errno);
1127 freeaddrinfo (ai_list);
1131 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1132 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1133 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1134 ai_list->ai_addrlen);
1135 obj->srcaddrlen = ai_list->ai_addrlen;
1136 obj->addrfamily = ai_list->ai_family;
1138 freeaddrinfo (ai_list);
1139 } /* case PING_OPT_SOURCE */
1144 } /* switch (option) */
1147 } /* int ping_setopt */
1150 int ping_send (pingobj_t *obj)
1154 if (ping_send_all (obj) < 0)
1157 if ((ret = ping_receive_all (obj)) < 0)
1163 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1167 if (strcasecmp (ph->username, host) == 0)
1176 int ping_host_add (pingobj_t *obj, const char *host)
1180 struct addrinfo ai_hints;
1181 struct addrinfo *ai_list, *ai_ptr;
1184 dprintf ("host = %s\n", host);
1186 if (ping_host_search (obj->head, host) != NULL)
1189 memset (&ai_hints, '\0', sizeof (ai_hints));
1190 ai_hints.ai_flags = 0;
1191 #ifdef AI_ADDRCONFIG
1192 ai_hints.ai_flags |= AI_ADDRCONFIG;
1195 ai_hints.ai_flags |= AI_CANONNAME;
1197 ai_hints.ai_family = obj->addrfamily;
1198 ai_hints.ai_socktype = SOCK_RAW;
1200 if ((ph = ping_alloc ()) == NULL)
1202 dprintf ("Out of memory!\n");
1206 if ((ph->username = strdup (host)) == NULL)
1208 dprintf ("Out of memory!\n");
1209 ping_set_errno (obj, errno);
1214 if ((ph->hostname = strdup (host)) == NULL)
1216 dprintf ("Out of memory!\n");
1217 ping_set_errno (obj, errno);
1222 /* obj->data is not garuanteed to be != NULL */
1223 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1225 dprintf ("Out of memory!\n");
1226 ping_set_errno (obj, errno);
1231 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1233 #if defined(EAI_SYSTEM)
1234 char errbuf[PING_ERRMSG_LEN];
1236 dprintf ("getaddrinfo failed\n");
1237 ping_set_error (obj, "getaddrinfo",
1238 #if defined(EAI_SYSTEM)
1239 (ai_return == EAI_SYSTEM)
1240 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1242 gai_strerror (ai_return));
1247 if (ai_list == NULL)
1248 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1250 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1254 if (ai_ptr->ai_family == AF_INET)
1256 ai_ptr->ai_socktype = SOCK_RAW;
1257 ai_ptr->ai_protocol = IPPROTO_ICMP;
1259 else if (ai_ptr->ai_family == AF_INET6)
1261 ai_ptr->ai_socktype = SOCK_RAW;
1262 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1266 char errmsg[PING_ERRMSG_LEN];
1268 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1269 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1272 ping_set_error (obj, "getaddrinfo", errmsg);
1276 /* TODO: Move this to a static function `ping_open_socket' and
1277 * call it whenever the socket dies. */
1278 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1282 char errbuf[PING_ERRMSG_LEN];
1283 dprintf ("socket: %s\n",
1284 sstrerror (errno, errbuf, sizeof (errbuf)));
1286 ping_set_errno (obj, errno);
1290 if (obj->srcaddr != NULL)
1292 assert (obj->srcaddrlen > 0);
1293 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1295 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1298 char errbuf[PING_ERRMSG_LEN];
1299 dprintf ("bind: %s\n",
1300 sstrerror (errno, errbuf, sizeof (errbuf)));
1302 ping_set_errno (obj, errno);
1309 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1310 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1311 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1312 ph->addrlen = ai_ptr->ai_addrlen;
1313 ph->addrfamily = ai_ptr->ai_family;
1316 if ((ai_ptr->ai_canonname != NULL)
1317 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1321 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1322 ph->hostname, ai_ptr->ai_canonname);
1324 old_hostname = ph->hostname;
1325 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1327 /* strdup failed, falling back to old hostname */
1328 ph->hostname = old_hostname;
1330 else if (old_hostname != NULL)
1332 free (old_hostname);
1335 #endif /* AI_CANONNAME */
1337 if (ph->addrfamily == AF_INET)
1341 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1342 &opt, sizeof (opt));
1344 else if (ph->addrfamily == AF_INET6)
1348 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1349 &opt, sizeof (opt));
1355 freeaddrinfo (ai_list);
1364 * Adding in the front is much easier, but then the iterator will
1365 * return the host that was added last as first host. That's just not
1368 if (obj->head == NULL)
1377 while (hptr->next != NULL)
1380 assert ((hptr != NULL) && (hptr->next == NULL));
1384 ping_set_ttl (ph, obj->ttl);
1387 } /* int ping_host_add */
1389 int ping_host_remove (pingobj_t *obj, const char *host)
1391 pinghost_t *pre, *cur;
1398 if (strcasecmp (host, cur->username) == 0)
1407 ping_set_error (obj, "ping_host_remove", "Host not found");
1412 obj->head = cur->next;
1414 pre->next = cur->next;
1421 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1423 return ((pingobj_iter_t *) obj->head);
1426 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1428 return ((pingobj_iter_t *) iter->next);
1431 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1432 void *buffer, size_t *buffer_len)
1436 size_t orig_buffer_len = *buffer_len;
1440 case PING_INFO_USERNAME:
1442 *buffer_len = strlen (iter->username) + 1;
1443 if (orig_buffer_len <= *buffer_len)
1445 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1446 * will copy `*buffer_len' and pad the rest of
1447 * `buffer' with null-bytes */
1448 strncpy (buffer, iter->username, orig_buffer_len);
1452 case PING_INFO_HOSTNAME:
1454 *buffer_len = strlen (iter->hostname) + 1;
1455 if (orig_buffer_len < *buffer_len)
1457 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1458 * will copy `*buffer_len' and pad the rest of
1459 * `buffer' with null-bytes */
1460 strncpy (buffer, iter->hostname, orig_buffer_len);
1464 case PING_INFO_ADDRESS:
1465 ret = getnameinfo ((struct sockaddr *) iter->addr,
1473 if ((ret == EAI_MEMORY)
1475 || (ret == EAI_OVERFLOW)
1479 #if defined(EAI_SYSTEM)
1480 else if (ret == EAI_SYSTEM)
1488 case PING_INFO_FAMILY:
1490 *buffer_len = sizeof (int);
1491 if (orig_buffer_len < sizeof (int))
1493 *((int *) buffer) = iter->addrfamily;
1497 case PING_INFO_LATENCY:
1499 *buffer_len = sizeof (double);
1500 if (orig_buffer_len < sizeof (double))
1502 *((double *) buffer) = iter->latency;
1506 case PING_INFO_DROPPED:
1508 *buffer_len = sizeof (uint32_t);
1509 if (orig_buffer_len < sizeof (uint32_t))
1511 *((uint32_t *) buffer) = iter->dropped;
1515 case PING_INFO_SEQUENCE:
1517 *buffer_len = sizeof (unsigned int);
1518 if (orig_buffer_len < sizeof (unsigned int))
1520 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1524 case PING_INFO_IDENT:
1526 *buffer_len = sizeof (uint16_t);
1527 if (orig_buffer_len < sizeof (uint16_t))
1529 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1533 case PING_INFO_DATA:
1535 *buffer_len = strlen (iter->data);
1536 if (orig_buffer_len < *buffer_len)
1538 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1542 case PING_INFO_RECV_TTL:
1544 *buffer_len = sizeof (int);
1545 if (orig_buffer_len < sizeof (int))
1547 *((int *) buffer) = iter->recv_ttl;
1553 } /* ping_iterator_get_info */
1555 void *ping_iterator_get_context (pingobj_iter_t *iter)
1557 return (iter->context);
1560 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1562 iter->context = context;