2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006 Florian octo Forster <octo at verplant.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; 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
31 # error "You don't have the standard C99 header files installed"
32 #endif /* STDC_HEADERS */
42 # include <sys/types.h>
45 # include <sys/stat.h>
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
53 # include <sys/time.h>
60 # include <sys/socket.h>
66 #if HAVE_NETINET_IN_SYSTM_H
67 # include <netinet/in_systm.h>
70 # include <netinet/in.h>
73 # include <netinet/ip.h>
75 #if HAVE_NETINET_IP_ICMP_H
76 # include <netinet/ip_icmp.h>
78 #ifdef HAVE_NETINET_IP_VAR_H
79 # include <netinet/ip_var.h>
81 #if HAVE_NETINET_IP6_H
82 # include <netinet/ip6.h>
84 #if HAVE_NETINET_ICMP6_H
85 # include <netinet/icmp6.h>
91 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
93 # define dprintf(...) /**/
96 #define PING_ERRMSG_LEN 256
101 struct sockaddr_storage *addr;
107 struct timeval *timer;
113 struct pinghost *next;
123 struct sockaddr_storage *srcaddr;
124 socklen_t srcaddrlen;
126 char errmsg[PING_ERRMSG_LEN];
132 * private (static) functions
134 static void ping_set_error (pingobj_t *obj, const char *function,
137 snprintf (obj->errmsg, PING_ERRMSG_LEN, "%s: %s", function, message);
138 obj->errmsg[PING_ERRMSG_LEN - 1] = '\0';
141 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
144 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
145 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
147 while (res->tv_usec > 1000000)
149 res->tv_usec -= 1000000;
156 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
160 if ((tv1->tv_sec < tv2->tv_sec)
161 || ((tv1->tv_sec == tv2->tv_sec)
162 && (tv1->tv_usec < tv2->tv_usec)))
165 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
166 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
168 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
170 while (res->tv_usec < 0)
172 res->tv_usec += 1000000;
179 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
186 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
191 *(char *) &ret = *(char *) ptr;
195 /* Do this twice to get all possible carries.. */
196 sum = (sum >> 16) + (sum & 0xFFFF);
197 sum = (sum >> 16) + (sum & 0xFFFF);
204 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
207 struct icmp *icmp_hdr;
211 uint16_t recv_checksum;
212 uint16_t calc_checksum;
219 if (buffer_len < sizeof (struct ip))
222 ip_hdr = (struct ip *) buffer;
223 ip_hdr_len = ip_hdr->ip_hl << 2;
225 if (buffer_len < ip_hdr_len)
228 buffer += ip_hdr_len;
229 buffer_len -= ip_hdr_len;
231 if (buffer_len < sizeof (struct icmp))
234 icmp_hdr = (struct icmp *) buffer;
235 buffer += sizeof (struct icmp);
236 buffer_len -= sizeof (struct icmp);
238 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
240 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
244 recv_checksum = icmp_hdr->icmp_cksum;
245 icmp_hdr->icmp_cksum = 0;
246 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
247 sizeof (struct icmp) + buffer_len);
249 if (recv_checksum != calc_checksum)
251 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
252 recv_checksum, calc_checksum);
256 ident = ntohs (icmp_hdr->icmp_id);
257 seq = ntohs (icmp_hdr->icmp_seq);
259 for (ptr = ph; ptr != NULL; ptr = ptr->next)
261 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
262 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
264 if (ptr->addrfamily != AF_INET)
267 if (!timerisset (ptr->timer))
270 if (ptr->ident != ident)
273 if (((ptr->sequence - 1) & 0xFFFF) != seq)
276 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
277 ptr->hostname, ident, seq);
284 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
291 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
293 struct icmp6_hdr *icmp_hdr;
300 if (buffer_len < sizeof (struct icmp6_hdr))
303 icmp_hdr = (struct icmp6_hdr *) buffer;
304 buffer += sizeof (struct icmp);
305 buffer_len -= sizeof (struct icmp);
307 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
309 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
313 if (icmp_hdr->icmp6_code != 0)
315 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
319 ident = ntohs (icmp_hdr->icmp6_id);
320 seq = ntohs (icmp_hdr->icmp6_seq);
322 for (ptr = ph; ptr != NULL; ptr = ptr->next)
324 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
325 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
327 if (ptr->addrfamily != AF_INET6)
330 if (!timerisset (ptr->timer))
333 if (ptr->ident != ident)
336 if (((ptr->sequence - 1) & 0xFFFF) != seq)
339 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
340 ptr->hostname, ident, seq);
347 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
354 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
361 pinghost_t *host = NULL;
363 struct sockaddr_storage sa;
366 sa_len = sizeof (sa);
368 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
369 (struct sockaddr *) &sa, &sa_len);
370 if (buffer_len == -1)
372 dprintf ("recvfrom: %s\n", strerror (errno));
376 dprintf ("Read %u bytes from fd = %i\n", (unsigned int) buffer_len, fd);
378 if (sa.ss_family == AF_INET)
380 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
383 else if (sa.ss_family == AF_INET6)
385 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
389 dprintf ("rcvd: %12i.%06i\n",
392 dprintf ("sent: %12i.%06i\n",
393 (int) host->timer->tv_sec,
394 (int) host->timer->tv_usec);
396 if (ping_timeval_sub (now, host->timer, &diff) < 0)
398 timerclear (host->timer);
402 dprintf ("diff: %12i.%06i\n",
406 host->latency = ((double) diff.tv_usec) / 1000.0;
407 host->latency += ((double) diff.tv_sec) * 1000.0;
409 timerclear (host->timer);
414 static int ping_receive_all (pingobj_t *obj)
423 struct timeval endtime;
424 struct timeval nowtime;
425 struct timeval timeout;
433 for (ptr = ph; ptr != NULL; ptr = ptr->next)
436 if (gettimeofday (&nowtime, NULL) == -1)
438 ping_set_error (obj, "gettimeofday", strerror (errno));
443 timeout.tv_sec = (time_t) obj->timeout;
444 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
446 dprintf ("Set timeout to %i.%06i seconds\n",
447 (int) timeout.tv_sec,
448 (int) timeout.tv_usec);
450 ping_timeval_add (&nowtime, &timeout, &endtime);
458 for (ptr = ph; ptr != NULL; ptr = ptr->next)
460 if (!timerisset (ptr->timer))
463 FD_SET (ptr->fd, &readfds);
466 if (max_readfds < ptr->fd)
467 max_readfds = ptr->fd;
470 if (num_readfds == 0)
473 if (gettimeofday (&nowtime, NULL) == -1)
475 ping_set_error (obj, "gettimeofday", strerror (errno));
479 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
482 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
483 (int) timeout.tv_sec,
484 (int) timeout.tv_usec);
486 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
488 if (gettimeofday (&nowtime, NULL) == -1)
490 ping_set_error (obj, "gettimeofday", strerror (errno));
494 if ((status == -1) && (errno == EINTR))
496 dprintf ("select was interrupted by signal..\n");
501 dprintf ("select: %s\n", strerror (errno));
504 else if (status == 0)
506 dprintf ("select timed out\n");
510 for (ptr = ph; ptr != NULL; ptr = ptr->next)
512 if (FD_ISSET (ptr->fd, &readfds))
513 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
521 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
522 * Sending functions: *
525 * +-> ping_send_one_ipv4 *
526 * `-> ping_send_one_ipv6 *
527 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
528 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
529 const void *buf, size_t buflen)
533 if (gettimeofday (ph->timer, NULL) == -1)
535 timerclear (ph->timer);
539 ret = sendto (ph->fd, buf, buflen, 0,
540 (struct sockaddr *) ph->addr, ph->addrlen);
544 #if defined(EHOSTUNREACH)
545 if (errno == EHOSTUNREACH)
548 #if defined(ENETUNREACH)
549 if (errno == ENETUNREACH)
552 ping_set_error (obj, "sendto", strerror (errno));
558 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
569 dprintf ("ph->hostname = %s\n", ph->hostname);
571 memset (buf, '\0', sizeof (buf));
572 icmp4 = (struct icmp *) buf;
573 data = (char *) (icmp4 + 1);
575 icmp4->icmp_type = ICMP_ECHO;
576 icmp4->icmp_code = 0;
577 icmp4->icmp_cksum = 0;
578 icmp4->icmp_id = htons (ph->ident);
579 icmp4->icmp_seq = htons (ph->sequence);
581 buflen = 4096 - sizeof (struct icmp);
582 strncpy (data, ph->data, buflen);
583 datalen = strlen (data);
585 buflen = datalen + sizeof (struct icmp);
587 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
589 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
591 status = ping_sendto (obj, ph, buf, buflen);
594 perror ("ping_sendto");
598 dprintf ("sendto: status = %i\n", status);
603 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
605 struct icmp6_hdr *icmp6;
614 dprintf ("ph->hostname = %s\n", ph->hostname);
616 memset (buf, '\0', sizeof (buf));
617 icmp6 = (struct icmp6_hdr *) buf;
618 data = (char *) (icmp6 + 1);
620 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
621 icmp6->icmp6_code = 0;
622 /* The checksum will be calculated by the TCP/IP stack. */
624 icmp6->icmp6_cksum = 0;
625 icmp6->icmp6_id = htons (ph->ident);
626 icmp6->icmp6_seq = htons (ph->sequence);
628 buflen = 4096 - sizeof (struct icmp6_hdr);
629 strncpy (data, ph->data, buflen);
630 datalen = strlen (data);
632 buflen = datalen + sizeof (struct icmp6_hdr);
634 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
636 status = ping_sendto (obj, ph, buf, buflen);
639 perror ("ping_sendto");
643 dprintf ("sendto: status = %i\n", status);
648 static int ping_send_all (pingobj_t *obj)
658 for (ptr = ph; ptr != NULL; ptr = ptr->next)
660 /* start timer.. The GNU `ping6' starts the timer before
661 * sending the packet, so I will do that too */
662 if (gettimeofday (ptr->timer, NULL) == -1)
664 dprintf ("gettimeofday: %s\n", strerror (errno));
665 timerclear (ptr->timer);
671 dprintf ("timer set for hostname = %s\n", ptr->hostname);
674 if (ptr->addrfamily == AF_INET6)
676 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
677 if (ping_send_one_ipv6 (obj, ptr) != 0)
679 timerclear (ptr->timer);
684 else if (ptr->addrfamily == AF_INET)
686 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
687 if (ping_send_one_ipv4 (obj, ptr) != 0)
689 timerclear (ptr->timer);
694 else /* this should not happen */
696 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
697 timerclear (ptr->timer);
709 * Set the TTL of a socket protocol independently.
711 static int ping_set_ttl (pinghost_t *ph, int ttl)
715 if (ph->addrfamily == AF_INET)
717 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
719 else if (ph->addrfamily == AF_INET6)
721 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
727 static int ping_get_ident (void)
730 static int did_seed = 0;
736 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
740 if (read (fd, &seed, sizeof (seed)) != -1)
743 dprintf ("Random seed: %i\n", seed);
751 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
755 retval = (int) random ();
757 dprintf ("Random number: %i\n", retval);
762 static pinghost_t *ping_alloc (void)
767 ph_size = sizeof (pinghost_t)
768 + sizeof (struct sockaddr_storage)
769 + sizeof (struct timeval);
771 ph = (pinghost_t *) malloc (ph_size);
775 memset (ph, '\0', ph_size);
777 ph->timer = (struct timeval *) (ph + 1);
778 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
780 ph->addrlen = sizeof (struct sockaddr_storage);
782 ph->ident = ping_get_ident () & 0xFFFF;
787 static void ping_free (pinghost_t *ph)
789 if (ph->hostname != NULL)
792 if (ph->data != NULL)
801 const char *ping_get_error (pingobj_t *obj)
803 return (obj->errmsg);
806 pingobj_t *ping_construct (void)
810 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
812 memset (obj, '\0', sizeof (pingobj_t));
814 obj->timeout = PING_DEF_TIMEOUT;
815 obj->ttl = PING_DEF_TTL;
816 obj->addrfamily = PING_DEF_AF;
817 obj->data = strdup (PING_DEF_DATA);
822 void ping_destroy (pingobj_t *obj)
830 while (current != NULL)
832 next = current->next;
837 if (obj->data != NULL)
840 if (obj->srcaddr != NULL)
848 int ping_setopt (pingobj_t *obj, int option, void *value)
854 case PING_OPT_TIMEOUT:
855 obj->timeout = *((double *) value);
856 if (obj->timeout < 0.0)
858 obj->timeout = PING_DEF_TIMEOUT;
864 obj->ttl = *((int *) value);
865 if ((obj->ttl < 1) || (obj->ttl > 255))
867 obj->ttl = PING_DEF_TTL;
873 obj->addrfamily = *((int *) value);
874 if ((obj->addrfamily != AF_UNSPEC)
875 && (obj->addrfamily != AF_INET)
876 && (obj->addrfamily != AF_INET6))
878 obj->addrfamily = PING_DEF_AF;
881 if (obj->srcaddr != NULL)
889 if (obj->data != NULL)
894 obj->data = strdup ((const char *) value);
897 case PING_OPT_SOURCE:
899 char *hostname = (char *) value;
900 struct addrinfo ai_hints;
901 struct addrinfo *ai_list;
904 if (obj->addrfamily != AF_UNSPEC)
906 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
909 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
910 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
911 #if defined(AI_ADDRCONFIG)
912 ai_hints.ai_flags = AI_ADDRCONFIG;
914 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
917 ping_set_error (obj, "getaddrinfo",
920 : gai_strerror (status));
925 if (ai_list->ai_next != NULL)
927 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
930 if (obj->srcaddr == NULL)
933 obj->srcaddr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage));
934 if (obj->srcaddr == NULL)
936 ping_set_error (obj, "malloc",
939 freeaddrinfo (ai_list);
943 memset ((void *) obj->srcaddr, '\0', sizeof (struct sockaddr_storage));
944 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
945 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
946 ai_list->ai_addrlen);
947 obj->srcaddrlen = ai_list->ai_addrlen;
948 obj->addrfamily = ai_list->ai_family;
950 freeaddrinfo (ai_list);
951 } /* case PING_OPT_SOURCE */
956 } /* switch (option) */
959 } /* int ping_setopt */
962 int ping_send (pingobj_t *obj)
966 if (ping_send_all (obj) < 0)
969 if ((ret = ping_receive_all (obj)) < 0)
975 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
979 if (strcasecmp (ph->hostname, host) == 0)
988 int ping_host_add (pingobj_t *obj, const char *host)
992 struct sockaddr_storage sockaddr;
993 socklen_t sockaddr_len;
995 struct addrinfo ai_hints;
996 struct addrinfo *ai_list, *ai_ptr;
999 dprintf ("host = %s\n", host);
1001 if (ping_host_search (obj->head, host) != NULL)
1004 memset (&ai_hints, '\0', sizeof (ai_hints));
1005 ai_hints.ai_flags = 0;
1006 #ifdef AI_ADDRCONFIG
1007 ai_hints.ai_flags |= AI_ADDRCONFIG;
1010 ai_hints.ai_flags |= AI_CANONNAME;
1012 ai_hints.ai_family = obj->addrfamily;
1013 ai_hints.ai_socktype = SOCK_RAW;
1015 if ((ph = ping_alloc ()) == NULL)
1017 dprintf ("Out of memory!\n");
1021 if ((ph->hostname = strdup (host)) == NULL)
1023 dprintf ("Out of memory!\n");
1024 ping_set_error (obj, "strdup", strerror (errno));
1029 /* obj->data is not garuanteed to be != NULL */
1030 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1032 dprintf ("Out of memory!\n");
1033 ping_set_error (obj, "strdup", strerror (errno));
1038 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1040 dprintf ("getaddrinfo failed\n");
1041 ping_set_error (obj, "getaddrinfo",
1042 (ai_return == EAI_SYSTEM)
1044 : gai_strerror (ai_return));
1049 if (ai_list == NULL)
1050 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1052 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1056 sockaddr_len = sizeof (sockaddr);
1057 memset (&sockaddr, '\0', sockaddr_len);
1059 if (ai_ptr->ai_family == AF_INET)
1061 struct sockaddr_in *si;
1063 si = (struct sockaddr_in *) &sockaddr;
1064 si->sin_family = AF_INET;
1065 si->sin_port = htons (ph->ident);
1066 si->sin_addr.s_addr = htonl (INADDR_ANY);
1068 ai_ptr->ai_socktype = SOCK_RAW;
1069 ai_ptr->ai_protocol = IPPROTO_ICMP;
1071 else if (ai_ptr->ai_family == AF_INET6)
1073 struct sockaddr_in6 *si;
1075 si = (struct sockaddr_in6 *) &sockaddr;
1076 si->sin6_family = AF_INET6;
1077 si->sin6_port = htons (ph->ident);
1078 si->sin6_addr = in6addr_any;
1080 ai_ptr->ai_socktype = SOCK_RAW;
1081 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1085 char errmsg[PING_ERRMSG_LEN];
1087 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1088 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1091 ping_set_error (obj, "getaddrinfo", errmsg);
1095 /* TODO: Move this to a static function `ping_open_socket' and
1096 * call it whenever the socket dies. */
1097 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1100 dprintf ("socket: %s\n", strerror (errno));
1101 ping_set_error (obj, "socket", strerror (errno));
1105 if (obj->srcaddr != NULL)
1107 assert (obj->srcaddrlen > 0);
1108 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1110 if (bind (ph->fd, (struct sockaddr *) obj->srcaddr, obj->srcaddrlen) == -1)
1112 dprintf ("bind: %s\n", strerror (errno));
1113 ping_set_error (obj, "bind", strerror (errno));
1120 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1121 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1122 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1123 ph->addrlen = ai_ptr->ai_addrlen;
1124 ph->addrfamily = ai_ptr->ai_family;
1127 if ((ai_ptr->ai_canonname != NULL)
1128 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1132 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1133 ph->hostname, ai_ptr->ai_canonname);
1135 old_hostname = ph->hostname;
1136 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1138 /* strdup failed, falling back to old hostname */
1139 ph->hostname = old_hostname;
1141 else if (old_hostname != NULL)
1143 free (old_hostname);
1146 #endif /* AI_CANONNAME */
1151 freeaddrinfo (ai_list);
1155 free (ph->hostname);
1161 * Adding in the front is much easier, but then the iterator will
1162 * return the host that was added last as first host. That's just not
1165 if (obj->head == NULL)
1174 while (hptr->next != NULL)
1177 assert ((hptr != NULL) && (hptr->next == NULL));
1181 ping_set_ttl (ph, obj->ttl);
1186 int ping_host_remove (pingobj_t *obj, const char *host)
1188 pinghost_t *pre, *cur;
1195 if (strcasecmp (host, cur->hostname))
1204 ping_set_error (obj, "ping_host_remove", "Host not found");
1209 obj->head = cur->next;
1211 pre->next = cur->next;
1221 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1223 return ((pingobj_iter_t *) obj->head);
1226 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1228 return ((pingobj_iter_t *) iter->next);
1231 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1232 void *buffer, size_t *buffer_len)
1236 size_t orig_buffer_len = *buffer_len;
1240 case PING_INFO_HOSTNAME:
1242 *buffer_len = strlen (iter->hostname);
1243 if (orig_buffer_len <= *buffer_len)
1245 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1246 * will copy `*buffer_len' and pad the rest of
1247 * `buffer' with null-bytes */
1248 strncpy (buffer, iter->hostname, orig_buffer_len);
1252 case PING_INFO_ADDRESS:
1253 ret = getnameinfo ((struct sockaddr *) iter->addr,
1261 if ((ret == EAI_MEMORY)
1263 || (ret == EAI_OVERFLOW)
1267 else if (ret == EAI_SYSTEM)
1268 /* XXX: Not thread-safe! */
1275 case PING_INFO_FAMILY:
1277 *buffer_len = sizeof (int);
1278 if (orig_buffer_len < sizeof (int))
1280 *((int *) buffer) = iter->addrfamily;
1284 case PING_INFO_LATENCY:
1286 *buffer_len = sizeof (double);
1287 if (orig_buffer_len < sizeof (double))
1289 *((double *) buffer) = iter->latency;
1293 case PING_INFO_SEQUENCE:
1295 *buffer_len = sizeof (unsigned int);
1296 if (orig_buffer_len < sizeof (unsigned int))
1298 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1302 case PING_INFO_IDENT:
1304 *buffer_len = sizeof (uint16_t);
1305 if (orig_buffer_len < sizeof (uint16_t))
1307 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1311 case PING_INFO_DATA:
1313 *buffer_len = strlen (iter->data);
1314 if (orig_buffer_len < *buffer_len)
1316 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1324 void *ping_iterator_get_context (pingobj_iter_t *iter)
1326 return (iter->context);
1329 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1331 iter->context = context;