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;
119 struct pinghost *next;
129 struct sockaddr *srcaddr;
130 socklen_t srcaddrlen;
132 char errmsg[PING_ERRMSG_LEN];
138 * private (static) functions
140 /* Even though Posix requires "strerror_r" to return an "int",
141 * some systems (e.g. the GNU libc) return a "char *" _and_
142 * ignore the second argument ... -tokkee */
143 char *sstrerror (int errnum, char *buf, size_t buflen)
149 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
151 /* #endif !HAVE_STRERROR_R */
153 #elif STRERROR_R_CHAR_P
156 temp = strerror_r (errnum, buf, buflen);
159 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
160 strncpy (buf, temp, buflen);
162 strncpy (buf, "strerror_r did not return "
163 "an error message", buflen);
166 /* #endif STRERROR_R_CHAR_P */
169 if (strerror_r (errnum, buf, buflen) != 0)
171 snprintf (buf, buflen, "Error %i (%#x); "
172 "Additionally, strerror_r failed.",
175 #endif /* STRERROR_R_CHAR_P */
180 } /* char *sstrerror */
182 static void ping_set_error (pingobj_t *obj, const char *function,
185 snprintf (obj->errmsg, sizeof (obj->errmsg),
186 "%s: %s", function, message);
187 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
190 static void ping_set_errno (pingobj_t *obj, int error_number)
192 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
195 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
198 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
199 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
201 while (res->tv_usec > 1000000)
203 res->tv_usec -= 1000000;
210 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
213 if ((tv1->tv_sec < tv2->tv_sec)
214 || ((tv1->tv_sec == tv2->tv_sec)
215 && (tv1->tv_usec < tv2->tv_usec)))
218 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
219 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
221 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
223 while (res->tv_usec < 0)
225 res->tv_usec += 1000000;
232 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
239 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
244 *(char *) &ret = *(char *) ptr;
248 /* Do this twice to get all possible carries.. */
249 sum = (sum >> 16) + (sum & 0xFFFF);
250 sum = (sum >> 16) + (sum & 0xFFFF);
257 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
260 struct icmp *icmp_hdr;
264 uint16_t recv_checksum;
265 uint16_t calc_checksum;
272 if (buffer_len < sizeof (struct ip))
275 ip_hdr = (struct ip *) buffer;
276 ip_hdr_len = ip_hdr->ip_hl << 2;
278 if (buffer_len < ip_hdr_len)
281 buffer += ip_hdr_len;
282 buffer_len -= ip_hdr_len;
284 if (buffer_len < sizeof (struct icmp))
287 icmp_hdr = (struct icmp *) buffer;
288 buffer += sizeof (struct icmp);
289 buffer_len -= sizeof (struct icmp);
291 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
293 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
297 recv_checksum = icmp_hdr->icmp_cksum;
298 icmp_hdr->icmp_cksum = 0;
299 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
300 sizeof (struct icmp) + buffer_len);
302 if (recv_checksum != calc_checksum)
304 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
305 recv_checksum, calc_checksum);
309 ident = ntohs (icmp_hdr->icmp_id);
310 seq = ntohs (icmp_hdr->icmp_seq);
312 for (ptr = ph; ptr != NULL; ptr = ptr->next)
314 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
315 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
317 if (ptr->addrfamily != AF_INET)
320 if (!timerisset (ptr->timer))
323 if (ptr->ident != ident)
326 if (((ptr->sequence - 1) & 0xFFFF) != seq)
329 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
330 ptr->hostname, ident, seq);
337 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
344 #ifndef ICMP6_ECHO_REQUEST
345 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
346 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
348 # define ICMP6_ECHO_REQUEST 128
352 #ifndef ICMP6_ECHO_REPLY
353 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
354 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
356 # define ICMP6_ECHO_REPLY 129
360 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
362 struct icmp6_hdr *icmp_hdr;
369 if (buffer_len < sizeof (struct icmp6_hdr))
372 icmp_hdr = (struct icmp6_hdr *) buffer;
373 buffer += sizeof (struct icmp);
374 buffer_len -= sizeof (struct icmp);
376 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
378 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
382 if (icmp_hdr->icmp6_code != 0)
384 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
388 ident = ntohs (icmp_hdr->icmp6_id);
389 seq = ntohs (icmp_hdr->icmp6_seq);
391 for (ptr = ph; ptr != NULL; ptr = ptr->next)
393 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
394 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
396 if (ptr->addrfamily != AF_INET6)
399 if (!timerisset (ptr->timer))
402 if (ptr->ident != ident)
405 if (((ptr->sequence - 1) & 0xFFFF) != seq)
408 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
409 ptr->hostname, ident, seq);
416 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
423 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
430 pinghost_t *host = NULL;
432 struct sockaddr_storage sa;
435 sa_len = sizeof (sa);
437 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
438 (struct sockaddr *) &sa, &sa_len);
442 char errbuf[PING_ERRMSG_LEN];
443 dprintf ("recvfrom: %s\n",
444 sstrerror (errno, errbuf, sizeof (errbuf)));
449 dprintf ("Read %zi bytes from fd = %i\n", buffer_len, fd);
451 if (sa.ss_family == AF_INET)
453 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
456 else if (sa.ss_family == AF_INET6)
458 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
462 dprintf ("rcvd: %12i.%06i\n",
465 dprintf ("sent: %12i.%06i\n",
466 (int) host->timer->tv_sec,
467 (int) host->timer->tv_usec);
469 if (ping_timeval_sub (now, host->timer, &diff) < 0)
471 timerclear (host->timer);
475 dprintf ("diff: %12i.%06i\n",
479 host->latency = ((double) diff.tv_usec) / 1000.0;
480 host->latency += ((double) diff.tv_sec) * 1000.0;
482 timerclear (host->timer);
487 static int ping_receive_all (pingobj_t *obj)
496 struct timeval endtime;
497 struct timeval nowtime;
498 struct timeval timeout;
506 for (ptr = ph; ptr != NULL; ptr = ptr->next)
509 if (gettimeofday (&nowtime, NULL) == -1)
511 ping_set_errno (obj, errno);
516 timeout.tv_sec = (time_t) obj->timeout;
517 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
519 dprintf ("Set timeout to %i.%06i seconds\n",
520 (int) timeout.tv_sec,
521 (int) timeout.tv_usec);
523 ping_timeval_add (&nowtime, &timeout, &endtime);
531 for (ptr = ph; ptr != NULL; ptr = ptr->next)
533 if (!timerisset (ptr->timer))
536 FD_SET (ptr->fd, &readfds);
539 if (max_readfds < ptr->fd)
540 max_readfds = ptr->fd;
543 if (num_readfds == 0)
546 if (gettimeofday (&nowtime, NULL) == -1)
548 ping_set_errno (obj, errno);
552 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
555 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
556 (int) timeout.tv_sec,
557 (int) timeout.tv_usec);
559 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
561 if (gettimeofday (&nowtime, NULL) == -1)
563 ping_set_errno (obj, errno);
567 if ((status == -1) && (errno == EINTR))
569 dprintf ("select was interrupted by signal..\n");
575 char errbuf[PING_ERRMSG_LEN];
576 dprintf ("select: %s\n",
577 sstrerror (errno, errbuf, sizeof (errbuf)));
581 else if (status == 0)
583 dprintf ("select timed out\n");
584 for (ptr = ph; ptr != NULL; ptr = ptr->next)
585 if (ptr->latency < 0.0)
590 for (ptr = ph; ptr != NULL; ptr = ptr->next)
592 if (FD_ISSET (ptr->fd, &readfds))
593 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
601 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
602 * Sending functions: *
605 * +-> ping_send_one_ipv4 *
606 * `-> ping_send_one_ipv6 *
607 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
608 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
609 const void *buf, size_t buflen)
613 if (gettimeofday (ph->timer, NULL) == -1)
615 timerclear (ph->timer);
619 ret = sendto (ph->fd, buf, buflen, 0,
620 (struct sockaddr *) ph->addr, ph->addrlen);
624 #if defined(EHOSTUNREACH)
625 if (errno == EHOSTUNREACH)
628 #if defined(ENETUNREACH)
629 if (errno == ENETUNREACH)
632 ping_set_errno (obj, errno);
638 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
649 dprintf ("ph->hostname = %s\n", ph->hostname);
651 memset (buf, '\0', sizeof (buf));
652 icmp4 = (struct icmp *) buf;
653 data = (char *) (icmp4 + 1);
655 icmp4->icmp_type = ICMP_ECHO;
656 icmp4->icmp_code = 0;
657 icmp4->icmp_cksum = 0;
658 icmp4->icmp_id = htons (ph->ident);
659 icmp4->icmp_seq = htons (ph->sequence);
661 buflen = 4096 - sizeof (struct icmp);
662 strncpy (data, ph->data, buflen);
663 datalen = strlen (data);
665 buflen = datalen + sizeof (struct icmp);
667 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
669 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
671 status = ping_sendto (obj, ph, buf, buflen);
674 perror ("ping_sendto");
678 dprintf ("sendto: status = %i\n", status);
683 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
685 struct icmp6_hdr *icmp6;
694 dprintf ("ph->hostname = %s\n", ph->hostname);
696 memset (buf, '\0', sizeof (buf));
697 icmp6 = (struct icmp6_hdr *) buf;
698 data = (char *) (icmp6 + 1);
700 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
701 icmp6->icmp6_code = 0;
702 /* The checksum will be calculated by the TCP/IP stack. */
704 icmp6->icmp6_cksum = 0;
705 icmp6->icmp6_id = htons (ph->ident);
706 icmp6->icmp6_seq = htons (ph->sequence);
708 buflen = 4096 - sizeof (struct icmp6_hdr);
709 strncpy (data, ph->data, buflen);
710 datalen = strlen (data);
712 buflen = datalen + sizeof (struct icmp6_hdr);
714 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
716 status = ping_sendto (obj, ph, buf, buflen);
719 perror ("ping_sendto");
723 dprintf ("sendto: status = %i\n", status);
728 static int ping_send_all (pingobj_t *obj)
738 for (ptr = ph; ptr != NULL; ptr = ptr->next)
740 /* start timer.. The GNU `ping6' starts the timer before
741 * sending the packet, so I will do that too */
742 if (gettimeofday (ptr->timer, NULL) == -1)
745 char errbuf[PING_ERRMSG_LEN];
746 dprintf ("gettimeofday: %s\n",
747 sstrerror (errno, errbuf, sizeof (errbuf)));
749 timerclear (ptr->timer);
755 dprintf ("timer set for hostname = %s\n", ptr->hostname);
758 if (ptr->addrfamily == AF_INET6)
760 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
761 if (ping_send_one_ipv6 (obj, ptr) != 0)
763 timerclear (ptr->timer);
768 else if (ptr->addrfamily == AF_INET)
770 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
771 if (ping_send_one_ipv4 (obj, ptr) != 0)
773 timerclear (ptr->timer);
778 else /* this should not happen */
780 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
781 timerclear (ptr->timer);
793 * Set the TTL of a socket protocol independently.
795 static int ping_set_ttl (pinghost_t *ph, int ttl)
799 if (ph->addrfamily == AF_INET)
801 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
803 else if (ph->addrfamily == AF_INET6)
805 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
811 static int ping_get_ident (void)
814 static int did_seed = 0;
820 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
824 if (read (fd, &seed, sizeof (seed)) != -1)
827 dprintf ("Random seed: %#x\n", seed);
836 char errbuf[PING_ERRMSG_LEN];
837 dprintf ("open (/dev/urandom): %s\n",
838 sstrerror (errno, errbuf, sizeof (errbuf)));
843 retval = (int) random ();
845 dprintf ("Random number: %#x\n", retval);
850 static pinghost_t *ping_alloc (void)
855 ph_size = sizeof (pinghost_t)
856 + sizeof (struct sockaddr_storage)
857 + sizeof (struct timeval);
859 ph = (pinghost_t *) malloc (ph_size);
863 memset (ph, '\0', ph_size);
865 ph->timer = (struct timeval *) (ph + 1);
866 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
868 ph->addrlen = sizeof (struct sockaddr_storage);
872 ph->ident = ping_get_ident () & 0xFFFF;
877 static void ping_free (pinghost_t *ph)
882 if (ph->username != NULL)
885 if (ph->hostname != NULL)
888 if (ph->data != NULL)
897 const char *ping_get_error (pingobj_t *obj)
899 return (obj->errmsg);
902 pingobj_t *ping_construct (void)
906 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
908 memset (obj, '\0', sizeof (pingobj_t));
910 obj->timeout = PING_DEF_TIMEOUT;
911 obj->ttl = PING_DEF_TTL;
912 obj->addrfamily = PING_DEF_AF;
913 obj->data = strdup (PING_DEF_DATA);
918 void ping_destroy (pingobj_t *obj)
926 while (current != NULL)
928 next = current->next;
933 if (obj->data != NULL)
936 if (obj->srcaddr != NULL)
944 int ping_setopt (pingobj_t *obj, int option, void *value)
950 case PING_OPT_TIMEOUT:
951 obj->timeout = *((double *) value);
952 if (obj->timeout < 0.0)
954 obj->timeout = PING_DEF_TIMEOUT;
960 obj->ttl = *((int *) value);
961 if ((obj->ttl < 1) || (obj->ttl > 255))
963 obj->ttl = PING_DEF_TTL;
969 obj->addrfamily = *((int *) value);
970 if ((obj->addrfamily != AF_UNSPEC)
971 && (obj->addrfamily != AF_INET)
972 && (obj->addrfamily != AF_INET6))
974 obj->addrfamily = PING_DEF_AF;
977 if (obj->srcaddr != NULL)
985 if (obj->data != NULL)
990 obj->data = strdup ((const char *) value);
993 case PING_OPT_SOURCE:
995 char *hostname = (char *) value;
996 struct addrinfo ai_hints;
997 struct addrinfo *ai_list;
1000 if (obj->addrfamily != AF_UNSPEC)
1002 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1005 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1006 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1007 #if defined(AI_ADDRCONFIG)
1008 ai_hints.ai_flags = AI_ADDRCONFIG;
1010 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1013 #if defined(EAI_SYSTEM)
1014 char errbuf[PING_ERRMSG_LEN];
1016 ping_set_error (obj, "getaddrinfo",
1017 #if defined(EAI_SYSTEM)
1018 (status == EAI_SYSTEM)
1019 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1021 gai_strerror (status));
1026 if (ai_list->ai_next != NULL)
1028 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1031 if (obj->srcaddr == NULL)
1033 obj->srcaddrlen = 0;
1034 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1035 if (obj->srcaddr == NULL)
1037 ping_set_errno (obj, errno);
1039 freeaddrinfo (ai_list);
1043 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1044 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1045 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1046 ai_list->ai_addrlen);
1047 obj->srcaddrlen = ai_list->ai_addrlen;
1048 obj->addrfamily = ai_list->ai_family;
1050 freeaddrinfo (ai_list);
1051 } /* case PING_OPT_SOURCE */
1056 } /* switch (option) */
1059 } /* int ping_setopt */
1062 int ping_send (pingobj_t *obj)
1066 if (ping_send_all (obj) < 0)
1069 if ((ret = ping_receive_all (obj)) < 0)
1075 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1079 if (strcasecmp (ph->username, host) == 0)
1088 int ping_host_add (pingobj_t *obj, const char *host)
1092 struct addrinfo ai_hints;
1093 struct addrinfo *ai_list, *ai_ptr;
1096 dprintf ("host = %s\n", host);
1098 if (ping_host_search (obj->head, host) != NULL)
1101 memset (&ai_hints, '\0', sizeof (ai_hints));
1102 ai_hints.ai_flags = 0;
1103 #ifdef AI_ADDRCONFIG
1104 ai_hints.ai_flags |= AI_ADDRCONFIG;
1107 ai_hints.ai_flags |= AI_CANONNAME;
1109 ai_hints.ai_family = obj->addrfamily;
1110 ai_hints.ai_socktype = SOCK_RAW;
1112 if ((ph = ping_alloc ()) == NULL)
1114 dprintf ("Out of memory!\n");
1118 if ((ph->username = strdup (host)) == NULL)
1120 dprintf ("Out of memory!\n");
1121 ping_set_errno (obj, errno);
1126 if ((ph->hostname = strdup (host)) == NULL)
1128 dprintf ("Out of memory!\n");
1129 ping_set_errno (obj, errno);
1134 /* obj->data is not garuanteed to be != NULL */
1135 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1137 dprintf ("Out of memory!\n");
1138 ping_set_errno (obj, errno);
1143 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1145 #if defined(EAI_SYSTEM)
1146 char errbuf[PING_ERRMSG_LEN];
1148 dprintf ("getaddrinfo failed\n");
1149 ping_set_error (obj, "getaddrinfo",
1150 #if defined(EAI_SYSTEM)
1151 (ai_return == EAI_SYSTEM)
1152 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1154 gai_strerror (ai_return));
1159 if (ai_list == NULL)
1160 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1162 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1166 if (ai_ptr->ai_family == AF_INET)
1168 ai_ptr->ai_socktype = SOCK_RAW;
1169 ai_ptr->ai_protocol = IPPROTO_ICMP;
1171 else if (ai_ptr->ai_family == AF_INET6)
1173 ai_ptr->ai_socktype = SOCK_RAW;
1174 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1178 char errmsg[PING_ERRMSG_LEN];
1180 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1181 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1184 ping_set_error (obj, "getaddrinfo", errmsg);
1188 /* TODO: Move this to a static function `ping_open_socket' and
1189 * call it whenever the socket dies. */
1190 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1194 char errbuf[PING_ERRMSG_LEN];
1195 dprintf ("socket: %s\n",
1196 sstrerror (errno, errbuf, sizeof (errbuf)));
1198 ping_set_errno (obj, errno);
1202 if (obj->srcaddr != NULL)
1204 assert (obj->srcaddrlen > 0);
1205 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1207 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1210 char errbuf[PING_ERRMSG_LEN];
1211 dprintf ("bind: %s\n",
1212 sstrerror (errno, errbuf, sizeof (errbuf)));
1214 ping_set_errno (obj, errno);
1221 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1222 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1223 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1224 ph->addrlen = ai_ptr->ai_addrlen;
1225 ph->addrfamily = ai_ptr->ai_family;
1228 if ((ai_ptr->ai_canonname != NULL)
1229 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1233 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1234 ph->hostname, ai_ptr->ai_canonname);
1236 old_hostname = ph->hostname;
1237 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1239 /* strdup failed, falling back to old hostname */
1240 ph->hostname = old_hostname;
1242 else if (old_hostname != NULL)
1244 free (old_hostname);
1247 #endif /* AI_CANONNAME */
1252 freeaddrinfo (ai_list);
1261 * Adding in the front is much easier, but then the iterator will
1262 * return the host that was added last as first host. That's just not
1265 if (obj->head == NULL)
1274 while (hptr->next != NULL)
1277 assert ((hptr != NULL) && (hptr->next == NULL));
1281 ping_set_ttl (ph, obj->ttl);
1286 int ping_host_remove (pingobj_t *obj, const char *host)
1288 pinghost_t *pre, *cur;
1295 if (strcasecmp (host, cur->username) == 0)
1304 ping_set_error (obj, "ping_host_remove", "Host not found");
1309 obj->head = cur->next;
1311 pre->next = cur->next;
1318 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1320 return ((pingobj_iter_t *) obj->head);
1323 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1325 return ((pingobj_iter_t *) iter->next);
1328 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1329 void *buffer, size_t *buffer_len)
1333 size_t orig_buffer_len = *buffer_len;
1337 case PING_INFO_USERNAME:
1339 *buffer_len = strlen (iter->username) + 1;
1340 if (orig_buffer_len <= *buffer_len)
1342 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1343 * will copy `*buffer_len' and pad the rest of
1344 * `buffer' with null-bytes */
1345 strncpy (buffer, iter->username, orig_buffer_len);
1349 case PING_INFO_HOSTNAME:
1351 *buffer_len = strlen (iter->hostname) + 1;
1352 if (orig_buffer_len < *buffer_len)
1354 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1355 * will copy `*buffer_len' and pad the rest of
1356 * `buffer' with null-bytes */
1357 strncpy (buffer, iter->hostname, orig_buffer_len);
1361 case PING_INFO_ADDRESS:
1362 ret = getnameinfo ((struct sockaddr *) iter->addr,
1370 if ((ret == EAI_MEMORY)
1372 || (ret == EAI_OVERFLOW)
1376 #if defined(EAI_SYSTEM)
1377 else if (ret == EAI_SYSTEM)
1385 case PING_INFO_FAMILY:
1387 *buffer_len = sizeof (int);
1388 if (orig_buffer_len < sizeof (int))
1390 *((int *) buffer) = iter->addrfamily;
1394 case PING_INFO_LATENCY:
1396 *buffer_len = sizeof (double);
1397 if (orig_buffer_len < sizeof (double))
1399 *((double *) buffer) = iter->latency;
1403 case PING_INFO_DROPPED:
1405 *buffer_len = sizeof (uint32_t);
1406 if (orig_buffer_len < sizeof (uint32_t))
1408 *((uint32_t *) buffer) = iter->dropped;
1412 case PING_INFO_SEQUENCE:
1414 *buffer_len = sizeof (unsigned int);
1415 if (orig_buffer_len < sizeof (unsigned int))
1417 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1421 case PING_INFO_IDENT:
1423 *buffer_len = sizeof (uint16_t);
1424 if (orig_buffer_len < sizeof (uint16_t))
1426 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1430 case PING_INFO_DATA:
1432 *buffer_len = strlen (iter->data);
1433 if (orig_buffer_len < *buffer_len)
1435 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1441 } /* ping_iterator_get_info */
1443 void *ping_iterator_get_context (pingobj_iter_t *iter)
1445 return (iter->context);
1448 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1450 iter->context = context;