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;
139 char errmsg[PING_ERRMSG_LEN];
145 * private (static) functions
147 /* Even though Posix requires "strerror_r" to return an "int",
148 * some systems (e.g. the GNU libc) return a "char *" _and_
149 * ignore the second argument ... -tokkee */
150 static char *sstrerror (int errnum, char *buf, size_t buflen)
156 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
158 /* #endif !HAVE_STRERROR_R */
160 #elif STRERROR_R_CHAR_P
163 temp = strerror_r (errnum, buf, buflen);
166 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
167 strncpy (buf, temp, buflen);
169 strncpy (buf, "strerror_r did not return "
170 "an error message", buflen);
173 /* #endif STRERROR_R_CHAR_P */
176 if (strerror_r (errnum, buf, buflen) != 0)
178 snprintf (buf, buflen, "Error %i (%#x); "
179 "Additionally, strerror_r failed.",
182 #endif /* STRERROR_R_CHAR_P */
187 } /* char *sstrerror */
189 static void ping_set_error (pingobj_t *obj, const char *function,
192 snprintf (obj->errmsg, sizeof (obj->errmsg),
193 "%s: %s", function, message);
194 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
197 static void ping_set_errno (pingobj_t *obj, int error_number)
199 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
202 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
205 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
206 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
208 while (res->tv_usec > 1000000)
210 res->tv_usec -= 1000000;
217 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
220 if ((tv1->tv_sec < tv2->tv_sec)
221 || ((tv1->tv_sec == tv2->tv_sec)
222 && (tv1->tv_usec < tv2->tv_usec)))
225 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
226 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
228 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
230 while (res->tv_usec < 0)
232 res->tv_usec += 1000000;
239 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
246 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
251 *(char *) &ret = *(char *) ptr;
255 /* Do this twice to get all possible carries.. */
256 sum = (sum >> 16) + (sum & 0xFFFF);
257 sum = (sum >> 16) + (sum & 0xFFFF);
264 static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
268 struct icmp *icmp_hdr;
272 uint16_t recv_checksum;
273 uint16_t calc_checksum;
280 if (buffer_len < sizeof (struct ip))
283 ip_hdr = (struct ip *) buffer;
284 ip_hdr_len = ip_hdr->ip_hl << 2;
286 if (buffer_len < ip_hdr_len)
289 buffer += ip_hdr_len;
290 buffer_len -= ip_hdr_len;
292 if (buffer_len < sizeof (struct icmp))
295 icmp_hdr = (struct icmp *) buffer;
296 buffer += sizeof (struct icmp);
297 buffer_len -= sizeof (struct icmp);
299 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
301 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
305 recv_checksum = icmp_hdr->icmp_cksum;
306 icmp_hdr->icmp_cksum = 0;
307 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
308 sizeof (struct icmp) + buffer_len);
310 if (recv_checksum != calc_checksum)
312 dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
313 "calculated 0x%04"PRIx16"\n",
314 recv_checksum, calc_checksum);
318 ident = ntohs (icmp_hdr->icmp_id);
319 seq = ntohs (icmp_hdr->icmp_seq);
321 /* We have to iterate over all hosts, since ICMPv4 packets may
322 * be received on any raw v4 socket. */
323 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
325 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
326 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
328 if (ptr->addrfamily != AF_INET)
331 if (!timerisset (ptr->timer))
334 if (ptr->ident != ident)
337 if (((ptr->sequence - 1) & 0xFFFF) != seq)
340 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
342 ptr->hostname, ident, seq);
349 dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
354 ptr->recv_ttl = ip_hdr->ip_ttl;
359 #ifndef ICMP6_ECHO_REQUEST
360 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
361 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
363 # define ICMP6_ECHO_REQUEST 128
367 #ifndef ICMP6_ECHO_REPLY
368 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
369 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
371 # define ICMP6_ECHO_REPLY 129
375 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
378 struct icmp6_hdr *icmp_hdr;
385 if (buffer_len < sizeof (struct icmp6_hdr))
388 icmp_hdr = (struct icmp6_hdr *) buffer;
389 buffer += sizeof (struct icmp);
390 buffer_len -= sizeof (struct icmp);
392 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
394 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
398 if (icmp_hdr->icmp6_code != 0)
400 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
404 ident = ntohs (icmp_hdr->icmp6_id);
405 seq = ntohs (icmp_hdr->icmp6_seq);
407 /* We have to iterate over all hosts, since ICMPv6 packets may
408 * be received on any raw v6 socket. */
409 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
411 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
412 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
414 if (ptr->addrfamily != AF_INET6)
417 if (!timerisset (ptr->timer))
420 if (ptr->ident != ident)
423 if (((ptr->sequence - 1) & 0xFFFF) != seq)
426 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
428 ptr->hostname, ident, seq);
435 dprintf ("No match found for ident = 0x%04"PRIx16", "
443 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
446 /* Note: 'ph' is not necessarily the host object for which we receive a
447 * reply. The right object will be returned by ping_receive_ipv*(). For
448 * now, we can only rely on ph->fd and ph->addrfamily. */
451 pinghost_t *host = NULL;
455 * Set up the receive buffer..
457 struct msghdr msghdr;
458 struct cmsghdr *cmsg;
459 char payload_buffer[4096];
460 ssize_t payload_buffer_len;
461 char control_buffer[4096];
462 struct iovec payload_iovec;
464 memset (&payload_iovec, 0, sizeof (payload_iovec));
465 payload_iovec.iov_base = payload_buffer;
466 payload_iovec.iov_len = sizeof (payload_buffer);
468 memset (&msghdr, 0, sizeof (msghdr));
469 /* unspecified source address */
470 msghdr.msg_name = NULL;
471 msghdr.msg_namelen = 0;
472 /* output buffer vector, see readv(2) */
473 msghdr.msg_iov = &payload_iovec;
474 msghdr.msg_iovlen = 1;
475 /* output buffer for control messages */
476 msghdr.msg_control = control_buffer;
477 msghdr.msg_controllen = sizeof (control_buffer);
478 /* flags; this is an output only field.. */
479 msghdr.msg_flags = 0;
481 msghdr.msg_flags |= MSG_XPG4_2;
484 payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
485 if (payload_buffer_len < 0)
488 char errbuf[PING_ERRMSG_LEN];
489 dprintf ("recvfrom: %s\n",
490 sstrerror (errno, errbuf, sizeof (errbuf)));
494 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
496 /* Iterate over all auxiliary data in msghdr */
498 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
500 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
502 if (ph->addrfamily == AF_INET) /* {{{ */
504 if (cmsg->cmsg_level != IPPROTO_IP)
507 if (cmsg->cmsg_type == IP_TTL)
509 memcpy (&recv_ttl, CMSG_DATA (cmsg),
511 dprintf ("TTLv4 = %i;\n", recv_ttl);
515 dprintf ("Not handling option %i.\n",
519 else if (ph->addrfamily == AF_INET6) /* {{{ */
521 if (cmsg->cmsg_level != IPPROTO_IPV6)
524 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
526 memcpy (&recv_ttl, CMSG_DATA (cmsg),
528 dprintf ("TTLv6 = %i;\n", recv_ttl);
532 dprintf ("Not handling option %i.\n",
538 dprintf ("Don't know how to handle "
539 "unknown protocol %i.\n",
542 } /* }}} for (cmsg) */
544 if (ph->addrfamily == AF_INET)
546 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
550 else if (ph->addrfamily == AF_INET6)
552 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
558 dprintf ("ping_receive_one: Unknown address family %i.\n",
563 dprintf ("rcvd: %12i.%06i\n",
566 dprintf ("sent: %12i.%06i\n",
567 (int) host->timer->tv_sec,
568 (int) host->timer->tv_usec);
570 if (ping_timeval_sub (now, host->timer, &diff) < 0)
572 timerclear (host->timer);
576 dprintf ("diff: %12i.%06i\n",
581 host->recv_ttl = recv_ttl;
583 host->latency = ((double) diff.tv_usec) / 1000.0;
584 host->latency += ((double) diff.tv_sec) * 1000.0;
586 timerclear (host->timer);
591 static int ping_receive_all (pingobj_t *obj)
600 struct timeval endtime;
601 struct timeval nowtime;
602 struct timeval timeout;
610 for (ptr = ph; ptr != NULL; ptr = ptr->next)
616 if (gettimeofday (&nowtime, NULL) == -1)
618 ping_set_errno (obj, errno);
623 timeout.tv_sec = (time_t) obj->timeout;
624 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
626 dprintf ("Set timeout to %i.%06i seconds\n",
627 (int) timeout.tv_sec,
628 (int) timeout.tv_usec);
630 ping_timeval_add (&nowtime, &timeout, &endtime);
638 for (ptr = ph; ptr != NULL; ptr = ptr->next)
640 if (!timerisset (ptr->timer))
643 FD_SET (ptr->fd, &readfds);
646 if (max_readfds < ptr->fd)
647 max_readfds = ptr->fd;
650 if (num_readfds == 0)
653 if (gettimeofday (&nowtime, NULL) == -1)
655 ping_set_errno (obj, errno);
659 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
662 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
663 (int) timeout.tv_sec,
664 (int) timeout.tv_usec);
666 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
668 if (gettimeofday (&nowtime, NULL) == -1)
670 ping_set_errno (obj, errno);
674 if ((status == -1) && (errno == EINTR))
676 dprintf ("select was interrupted by signal..\n");
682 char errbuf[PING_ERRMSG_LEN];
683 dprintf ("select: %s\n",
684 sstrerror (errno, errbuf, sizeof (errbuf)));
688 else if (status == 0)
690 dprintf ("select timed out\n");
691 for (ptr = ph; ptr != NULL; ptr = ptr->next)
692 if (ptr->latency < 0.0)
697 for (ptr = ph; ptr != NULL; ptr = ptr->next)
699 if (FD_ISSET (ptr->fd, &readfds))
700 if (ping_receive_one (obj, ptr, &nowtime) == 0)
708 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
709 * Sending functions: *
712 * +-> ping_send_one_ipv4 *
713 * `-> ping_send_one_ipv6 *
714 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
715 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
716 const void *buf, size_t buflen)
720 if (gettimeofday (ph->timer, NULL) == -1)
722 timerclear (ph->timer);
726 ret = sendto (ph->fd, buf, buflen, 0,
727 (struct sockaddr *) ph->addr, ph->addrlen);
731 #if defined(EHOSTUNREACH)
732 if (errno == EHOSTUNREACH)
735 #if defined(ENETUNREACH)
736 if (errno == ENETUNREACH)
739 ping_set_errno (obj, errno);
745 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
756 dprintf ("ph->hostname = %s\n", ph->hostname);
758 memset (buf, '\0', sizeof (buf));
759 icmp4 = (struct icmp *) buf;
760 data = (char *) (icmp4 + 1);
762 icmp4->icmp_type = ICMP_ECHO;
763 icmp4->icmp_code = 0;
764 icmp4->icmp_cksum = 0;
765 icmp4->icmp_id = htons (ph->ident);
766 icmp4->icmp_seq = htons (ph->sequence);
768 buflen = 4096 - sizeof (struct icmp);
769 strncpy (data, ph->data, buflen);
770 datalen = strlen (data);
772 buflen = datalen + sizeof (struct icmp);
774 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
776 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
778 status = ping_sendto (obj, ph, buf, buflen);
781 perror ("ping_sendto");
785 dprintf ("sendto: status = %i\n", status);
790 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
792 struct icmp6_hdr *icmp6;
801 dprintf ("ph->hostname = %s\n", ph->hostname);
803 memset (buf, '\0', sizeof (buf));
804 icmp6 = (struct icmp6_hdr *) buf;
805 data = (char *) (icmp6 + 1);
807 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
808 icmp6->icmp6_code = 0;
809 /* The checksum will be calculated by the TCP/IP stack. */
811 icmp6->icmp6_cksum = 0;
812 icmp6->icmp6_id = htons (ph->ident);
813 icmp6->icmp6_seq = htons (ph->sequence);
815 buflen = 4096 - sizeof (struct icmp6_hdr);
816 strncpy (data, ph->data, buflen);
817 datalen = strlen (data);
819 buflen = datalen + sizeof (struct icmp6_hdr);
821 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
823 status = ping_sendto (obj, ph, buf, buflen);
826 perror ("ping_sendto");
830 dprintf ("sendto: status = %i\n", status);
835 static int ping_send_all (pingobj_t *obj)
845 for (ptr = ph; ptr != NULL; ptr = ptr->next)
847 /* start timer.. The GNU `ping6' starts the timer before
848 * sending the packet, so I will do that too */
849 if (gettimeofday (ptr->timer, NULL) == -1)
852 char errbuf[PING_ERRMSG_LEN];
853 dprintf ("gettimeofday: %s\n",
854 sstrerror (errno, errbuf, sizeof (errbuf)));
856 timerclear (ptr->timer);
862 dprintf ("timer set for hostname = %s\n", ptr->hostname);
865 if (ptr->addrfamily == AF_INET6)
867 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
868 if (ping_send_one_ipv6 (obj, ptr) != 0)
870 timerclear (ptr->timer);
875 else if (ptr->addrfamily == AF_INET)
877 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
878 if (ping_send_one_ipv4 (obj, ptr) != 0)
880 timerclear (ptr->timer);
885 else /* this should not happen */
887 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
888 timerclear (ptr->timer);
900 * Set the TTL of a socket protocol independently.
902 static int ping_set_ttl (pinghost_t *ph, int ttl)
906 if (ph->addrfamily == AF_INET)
908 dprintf ("Setting TTLv4 to %i\n", ttl);
909 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
912 else if (ph->addrfamily == AF_INET6)
914 dprintf ("Setting TTLv6 to %i\n", ttl);
915 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
922 static int ping_get_ident (void)
925 static int did_seed = 0;
931 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
935 if (read (fd, &seed, sizeof (seed)) != -1)
938 dprintf ("Random seed: %#x\n", seed);
947 char errbuf[PING_ERRMSG_LEN];
948 dprintf ("open (/dev/urandom): %s\n",
949 sstrerror (errno, errbuf, sizeof (errbuf)));
954 retval = (int) random ();
956 dprintf ("Random number: %#x\n", retval);
961 static pinghost_t *ping_alloc (void)
966 ph_size = sizeof (pinghost_t)
967 + sizeof (struct sockaddr_storage)
968 + sizeof (struct timeval);
970 ph = (pinghost_t *) malloc (ph_size);
974 memset (ph, '\0', ph_size);
976 ph->timer = (struct timeval *) (ph + 1);
977 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
979 ph->addrlen = sizeof (struct sockaddr_storage);
983 ph->ident = ping_get_ident () & 0xFFFF;
988 static void ping_free (pinghost_t *ph)
993 if (ph->username != NULL)
996 if (ph->hostname != NULL)
999 if (ph->data != NULL)
1008 const char *ping_get_error (pingobj_t *obj)
1012 return (obj->errmsg);
1015 pingobj_t *ping_construct (void)
1019 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1021 memset (obj, '\0', sizeof (pingobj_t));
1023 obj->timeout = PING_DEF_TIMEOUT;
1024 obj->ttl = PING_DEF_TTL;
1025 obj->addrfamily = PING_DEF_AF;
1026 obj->data = strdup (PING_DEF_DATA);
1031 void ping_destroy (pingobj_t *obj)
1033 pinghost_t *current;
1039 current = obj->head;
1042 while (current != NULL)
1044 next = current->next;
1045 ping_free (current);
1049 if (obj->data != NULL)
1052 if (obj->srcaddr != NULL)
1053 free (obj->srcaddr);
1055 if (obj->device != NULL)
1063 int ping_setopt (pingobj_t *obj, int option, void *value)
1067 if ((obj == NULL) || (value == NULL))
1072 case PING_OPT_TIMEOUT:
1073 obj->timeout = *((double *) value);
1074 if (obj->timeout < 0.0)
1076 obj->timeout = PING_DEF_TIMEOUT;
1082 obj->ttl = *((int *) value);
1083 if ((obj->ttl < 1) || (obj->ttl > 255))
1085 obj->ttl = PING_DEF_TTL;
1092 for (ph = obj->head; ph != NULL; ph = ph->next)
1093 ping_set_ttl (ph, obj->ttl);
1098 obj->addrfamily = *((int *) value);
1099 if ((obj->addrfamily != AF_UNSPEC)
1100 && (obj->addrfamily != AF_INET)
1101 && (obj->addrfamily != AF_INET6))
1103 obj->addrfamily = PING_DEF_AF;
1106 if (obj->srcaddr != NULL)
1108 free (obj->srcaddr);
1109 obj->srcaddr = NULL;
1114 if (obj->data != NULL)
1119 obj->data = strdup ((const char *) value);
1122 case PING_OPT_SOURCE:
1124 char *hostname = (char *) value;
1125 struct addrinfo ai_hints;
1126 struct addrinfo *ai_list;
1129 if (obj->addrfamily != AF_UNSPEC)
1131 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1134 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1135 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1136 #if defined(AI_ADDRCONFIG)
1137 ai_hints.ai_flags = AI_ADDRCONFIG;
1139 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1142 #if defined(EAI_SYSTEM)
1143 char errbuf[PING_ERRMSG_LEN];
1145 ping_set_error (obj, "getaddrinfo",
1146 #if defined(EAI_SYSTEM)
1147 (status == EAI_SYSTEM)
1148 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1150 gai_strerror (status));
1155 if (ai_list->ai_next != NULL)
1157 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1160 if (obj->srcaddr == NULL)
1162 obj->srcaddrlen = 0;
1163 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1164 if (obj->srcaddr == NULL)
1166 ping_set_errno (obj, errno);
1168 freeaddrinfo (ai_list);
1172 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1173 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1174 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1175 ai_list->ai_addrlen);
1176 obj->srcaddrlen = ai_list->ai_addrlen;
1177 obj->addrfamily = ai_list->ai_family;
1179 freeaddrinfo (ai_list);
1180 } /* case PING_OPT_SOURCE */
1183 case PING_OPT_DEVICE:
1185 #ifdef SO_BINDTODEVICE
1186 char *device = strdup ((char *) value);
1190 ping_set_errno (obj, errno);
1195 if (obj->device != NULL)
1197 obj->device = device;
1198 #else /* ! SO_BINDTODEVICE */
1199 ping_set_errno (obj, ENOTSUP);
1201 #endif /* ! SO_BINDTODEVICE */
1202 } /* case PING_OPT_DEVICE */
1207 } /* switch (option) */
1210 } /* int ping_setopt */
1213 int ping_send (pingobj_t *obj)
1220 if (ping_send_all (obj) < 0)
1223 if ((ret = ping_receive_all (obj)) < 0)
1229 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1233 if (strcasecmp (ph->username, host) == 0)
1242 int ping_host_add (pingobj_t *obj, const char *host)
1246 struct addrinfo ai_hints;
1247 struct addrinfo *ai_list, *ai_ptr;
1250 if ((obj == NULL) || (host == NULL))
1253 dprintf ("host = %s\n", host);
1255 if (ping_host_search (obj->head, host) != NULL)
1258 memset (&ai_hints, '\0', sizeof (ai_hints));
1259 ai_hints.ai_flags = 0;
1260 #ifdef AI_ADDRCONFIG
1261 ai_hints.ai_flags |= AI_ADDRCONFIG;
1264 ai_hints.ai_flags |= AI_CANONNAME;
1266 ai_hints.ai_family = obj->addrfamily;
1267 ai_hints.ai_socktype = SOCK_RAW;
1269 if ((ph = ping_alloc ()) == NULL)
1271 dprintf ("Out of memory!\n");
1275 if ((ph->username = strdup (host)) == NULL)
1277 dprintf ("Out of memory!\n");
1278 ping_set_errno (obj, errno);
1283 if ((ph->hostname = strdup (host)) == NULL)
1285 dprintf ("Out of memory!\n");
1286 ping_set_errno (obj, errno);
1291 /* obj->data is not garuanteed to be != NULL */
1292 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1294 dprintf ("Out of memory!\n");
1295 ping_set_errno (obj, errno);
1300 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1302 #if defined(EAI_SYSTEM)
1303 char errbuf[PING_ERRMSG_LEN];
1305 dprintf ("getaddrinfo failed\n");
1306 ping_set_error (obj, "getaddrinfo",
1307 #if defined(EAI_SYSTEM)
1308 (ai_return == EAI_SYSTEM)
1309 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1311 gai_strerror (ai_return));
1316 if (ai_list == NULL)
1317 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1319 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1323 if (ai_ptr->ai_family == AF_INET)
1325 ai_ptr->ai_socktype = SOCK_RAW;
1326 ai_ptr->ai_protocol = IPPROTO_ICMP;
1328 else if (ai_ptr->ai_family == AF_INET6)
1330 ai_ptr->ai_socktype = SOCK_RAW;
1331 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1335 char errmsg[PING_ERRMSG_LEN];
1337 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1338 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1341 ping_set_error (obj, "getaddrinfo", errmsg);
1345 /* TODO: Move this to a static function `ping_open_socket' and
1346 * call it whenever the socket dies. */
1347 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1351 char errbuf[PING_ERRMSG_LEN];
1352 dprintf ("socket: %s\n",
1353 sstrerror (errno, errbuf, sizeof (errbuf)));
1355 ping_set_errno (obj, errno);
1359 if (obj->srcaddr != NULL)
1361 assert (obj->srcaddrlen > 0);
1362 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1364 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1367 char errbuf[PING_ERRMSG_LEN];
1368 dprintf ("bind: %s\n",
1369 sstrerror (errno, errbuf, sizeof (errbuf)));
1371 ping_set_errno (obj, errno);
1378 #ifdef SO_BINDTODEVICE
1379 if (obj->device != NULL)
1381 if (setsockopt (ph->fd, SOL_SOCKET, SO_BINDTODEVICE,
1382 obj->device, strlen (obj->device) + 1) != 0)
1385 char errbuf[PING_ERRMSG_LEN];
1386 dprintf ("setsockopt: %s\n",
1387 sstrerror (errno, errbuf, sizeof (errbuf)));
1389 ping_set_errno (obj, errno);
1395 #endif /* SO_BINDTODEVICE */
1397 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1398 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1399 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1400 ph->addrlen = ai_ptr->ai_addrlen;
1401 ph->addrfamily = ai_ptr->ai_family;
1404 if ((ai_ptr->ai_canonname != NULL)
1405 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1409 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1410 ph->hostname, ai_ptr->ai_canonname);
1412 old_hostname = ph->hostname;
1413 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1415 /* strdup failed, falling back to old hostname */
1416 ph->hostname = old_hostname;
1418 else if (old_hostname != NULL)
1420 free (old_hostname);
1423 #endif /* AI_CANONNAME */
1425 if (ph->addrfamily == AF_INET)
1429 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1430 &opt, sizeof (opt));
1432 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1433 else if (ph->addrfamily == AF_INET6)
1437 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1438 &opt, sizeof (opt));
1445 freeaddrinfo (ai_list);
1454 * Adding in the front is much easier, but then the iterator will
1455 * return the host that was added last as first host. That's just not
1458 if (obj->head == NULL)
1467 while (hptr->next != NULL)
1470 assert ((hptr != NULL) && (hptr->next == NULL));
1474 ping_set_ttl (ph, obj->ttl);
1477 } /* int ping_host_add */
1479 int ping_host_remove (pingobj_t *obj, const char *host)
1481 pinghost_t *pre, *cur;
1483 if ((obj == NULL) || (host == NULL))
1491 if (strcasecmp (host, cur->username) == 0)
1500 ping_set_error (obj, "ping_host_remove", "Host not found");
1505 obj->head = cur->next;
1507 pre->next = cur->next;
1514 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1518 return ((pingobj_iter_t *) obj->head);
1521 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1525 return ((pingobj_iter_t *) iter->next);
1528 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1529 void *buffer, size_t *buffer_len)
1533 size_t orig_buffer_len = *buffer_len;
1535 if ((iter == NULL) || (buffer == NULL) || (buffer_len == NULL))
1540 case PING_INFO_USERNAME:
1542 *buffer_len = strlen (iter->username) + 1;
1543 if (orig_buffer_len <= *buffer_len)
1545 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1546 * will copy `*buffer_len' and pad the rest of
1547 * `buffer' with null-bytes */
1548 strncpy (buffer, iter->username, orig_buffer_len);
1552 case PING_INFO_HOSTNAME:
1554 *buffer_len = strlen (iter->hostname) + 1;
1555 if (orig_buffer_len < *buffer_len)
1557 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1558 * will copy `*buffer_len' and pad the rest of
1559 * `buffer' with null-bytes */
1560 strncpy (buffer, iter->hostname, orig_buffer_len);
1564 case PING_INFO_ADDRESS:
1565 ret = getnameinfo ((struct sockaddr *) iter->addr,
1573 if ((ret == EAI_MEMORY)
1575 || (ret == EAI_OVERFLOW)
1579 #if defined(EAI_SYSTEM)
1580 else if (ret == EAI_SYSTEM)
1588 case PING_INFO_FAMILY:
1590 *buffer_len = sizeof (int);
1591 if (orig_buffer_len < sizeof (int))
1593 *((int *) buffer) = iter->addrfamily;
1597 case PING_INFO_LATENCY:
1599 *buffer_len = sizeof (double);
1600 if (orig_buffer_len < sizeof (double))
1602 *((double *) buffer) = iter->latency;
1606 case PING_INFO_DROPPED:
1608 *buffer_len = sizeof (uint32_t);
1609 if (orig_buffer_len < sizeof (uint32_t))
1611 *((uint32_t *) buffer) = iter->dropped;
1615 case PING_INFO_SEQUENCE:
1617 *buffer_len = sizeof (unsigned int);
1618 if (orig_buffer_len < sizeof (unsigned int))
1620 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1624 case PING_INFO_IDENT:
1626 *buffer_len = sizeof (uint16_t);
1627 if (orig_buffer_len < sizeof (uint16_t))
1629 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1633 case PING_INFO_DATA:
1635 *buffer_len = strlen (iter->data);
1636 if (orig_buffer_len < *buffer_len)
1638 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1642 case PING_INFO_RECV_TTL:
1644 *buffer_len = sizeof (int);
1645 if (orig_buffer_len < sizeof (int))
1647 *((int *) buffer) = iter->recv_ttl;
1653 } /* ping_iterator_get_info */
1655 void *ping_iterator_get_context (pingobj_iter_t *iter)
1659 return (iter->context);
1662 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1666 iter->context = context;