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, const char *function,
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)
431 pinghost_t *host = NULL;
433 struct sockaddr_storage sa;
436 sa_len = sizeof (sa);
438 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
439 (struct sockaddr *) &sa, &sa_len);
443 char errbuf[PING_ERRMSG_LEN];
444 dprintf ("recvfrom: %s\n",
445 sstrerror (errno, errbuf, sizeof (errbuf)));
450 dprintf ("Read %zi bytes from fd = %i\n", buffer_len, fd);
452 if (sa.ss_family == AF_INET)
454 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
457 else if (sa.ss_family == AF_INET6)
459 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
463 dprintf ("rcvd: %12i.%06i\n",
466 dprintf ("sent: %12i.%06i\n",
467 (int) host->timer->tv_sec,
468 (int) host->timer->tv_usec);
470 if (ping_timeval_sub (now, host->timer, &diff) < 0)
472 timerclear (host->timer);
476 dprintf ("diff: %12i.%06i\n",
480 host->latency = ((double) diff.tv_usec) / 1000.0;
481 host->latency += ((double) diff.tv_sec) * 1000.0;
483 timerclear (host->timer);
488 static int ping_receive_all (pingobj_t *obj)
497 struct timeval endtime;
498 struct timeval nowtime;
499 struct timeval timeout;
507 for (ptr = ph; ptr != NULL; ptr = ptr->next)
510 if (gettimeofday (&nowtime, NULL) == -1)
512 ping_set_errno (obj, "gettimeofday", errno);
517 timeout.tv_sec = (time_t) obj->timeout;
518 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
520 dprintf ("Set timeout to %i.%06i seconds\n",
521 (int) timeout.tv_sec,
522 (int) timeout.tv_usec);
524 ping_timeval_add (&nowtime, &timeout, &endtime);
532 for (ptr = ph; ptr != NULL; ptr = ptr->next)
534 if (!timerisset (ptr->timer))
537 FD_SET (ptr->fd, &readfds);
540 if (max_readfds < ptr->fd)
541 max_readfds = ptr->fd;
544 if (num_readfds == 0)
547 if (gettimeofday (&nowtime, NULL) == -1)
549 ping_set_errno (obj, "gettimeofday", errno);
553 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
556 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
557 (int) timeout.tv_sec,
558 (int) timeout.tv_usec);
560 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
562 if (gettimeofday (&nowtime, NULL) == -1)
564 ping_set_errno (obj, "gettimeofday", errno);
568 if ((status == -1) && (errno == EINTR))
570 dprintf ("select was interrupted by signal..\n");
576 char errbuf[PING_ERRMSG_LEN];
577 dprintf ("select: %s\n",
578 sstrerror (errno, errbuf, sizeof (errbuf)));
582 else if (status == 0)
584 dprintf ("select timed out\n");
585 for (ptr = ph; ptr != NULL; ptr = ptr->next)
586 if (ptr->latency < 0.0)
591 for (ptr = ph; ptr != NULL; ptr = ptr->next)
593 if (FD_ISSET (ptr->fd, &readfds))
594 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
602 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
603 * Sending functions: *
606 * +-> ping_send_one_ipv4 *
607 * `-> ping_send_one_ipv6 *
608 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
609 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
610 const void *buf, size_t buflen)
614 if (gettimeofday (ph->timer, NULL) == -1)
616 timerclear (ph->timer);
620 ret = sendto (ph->fd, buf, buflen, 0,
621 (struct sockaddr *) ph->addr, ph->addrlen);
625 #if defined(EHOSTUNREACH)
626 if (errno == EHOSTUNREACH)
629 #if defined(ENETUNREACH)
630 if (errno == ENETUNREACH)
633 ping_set_errno (obj, "sendto", errno);
639 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
650 dprintf ("ph->hostname = %s\n", ph->hostname);
652 memset (buf, '\0', sizeof (buf));
653 icmp4 = (struct icmp *) buf;
654 data = (char *) (icmp4 + 1);
656 icmp4->icmp_type = ICMP_ECHO;
657 icmp4->icmp_code = 0;
658 icmp4->icmp_cksum = 0;
659 icmp4->icmp_id = htons (ph->ident);
660 icmp4->icmp_seq = htons (ph->sequence);
662 buflen = 4096 - sizeof (struct icmp);
663 strncpy (data, ph->data, buflen);
664 datalen = strlen (data);
666 buflen = datalen + sizeof (struct icmp);
668 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
670 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
672 status = ping_sendto (obj, ph, buf, buflen);
675 perror ("ping_sendto");
679 dprintf ("sendto: status = %i\n", status);
684 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
686 struct icmp6_hdr *icmp6;
695 dprintf ("ph->hostname = %s\n", ph->hostname);
697 memset (buf, '\0', sizeof (buf));
698 icmp6 = (struct icmp6_hdr *) buf;
699 data = (char *) (icmp6 + 1);
701 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
702 icmp6->icmp6_code = 0;
703 /* The checksum will be calculated by the TCP/IP stack. */
705 icmp6->icmp6_cksum = 0;
706 icmp6->icmp6_id = htons (ph->ident);
707 icmp6->icmp6_seq = htons (ph->sequence);
709 buflen = 4096 - sizeof (struct icmp6_hdr);
710 strncpy (data, ph->data, buflen);
711 datalen = strlen (data);
713 buflen = datalen + sizeof (struct icmp6_hdr);
715 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
717 status = ping_sendto (obj, ph, buf, buflen);
720 perror ("ping_sendto");
724 dprintf ("sendto: status = %i\n", status);
729 static int ping_send_all (pingobj_t *obj)
739 for (ptr = ph; ptr != NULL; ptr = ptr->next)
741 /* start timer.. The GNU `ping6' starts the timer before
742 * sending the packet, so I will do that too */
743 if (gettimeofday (ptr->timer, NULL) == -1)
746 char errbuf[PING_ERRMSG_LEN];
747 dprintf ("gettimeofday: %s\n",
748 sstrerror (errno, errbuf, sizeof (errbuf)));
750 timerclear (ptr->timer);
756 dprintf ("timer set for hostname = %s\n", ptr->hostname);
759 if (ptr->addrfamily == AF_INET6)
761 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
762 if (ping_send_one_ipv6 (obj, ptr) != 0)
764 timerclear (ptr->timer);
769 else if (ptr->addrfamily == AF_INET)
771 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
772 if (ping_send_one_ipv4 (obj, ptr) != 0)
774 timerclear (ptr->timer);
779 else /* this should not happen */
781 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
782 timerclear (ptr->timer);
794 * Set the TTL of a socket protocol independently.
796 static int ping_set_ttl (pinghost_t *ph, int ttl)
800 if (ph->addrfamily == AF_INET)
802 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
804 else if (ph->addrfamily == AF_INET6)
806 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
812 static int ping_get_ident (void)
815 static int did_seed = 0;
821 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
825 if (read (fd, &seed, sizeof (seed)) != -1)
828 dprintf ("Random seed: %#x\n", seed);
837 char errbuf[PING_ERRMSG_LEN];
838 dprintf ("open (/dev/urandom): %s\n",
839 sstrerror (errno, errbuf, sizeof (errbuf)));
844 retval = (int) random ();
846 dprintf ("Random number: %#x\n", retval);
851 static pinghost_t *ping_alloc (void)
856 ph_size = sizeof (pinghost_t)
857 + sizeof (struct sockaddr_storage)
858 + sizeof (struct timeval);
860 ph = (pinghost_t *) malloc (ph_size);
864 memset (ph, '\0', ph_size);
866 ph->timer = (struct timeval *) (ph + 1);
867 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
869 ph->addrlen = sizeof (struct sockaddr_storage);
873 ph->ident = ping_get_ident () & 0xFFFF;
878 static void ping_free (pinghost_t *ph)
883 if (ph->username != NULL)
886 if (ph->hostname != NULL)
889 if (ph->data != NULL)
898 const char *ping_get_error (pingobj_t *obj)
900 return (obj->errmsg);
903 pingobj_t *ping_construct (void)
907 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
909 memset (obj, '\0', sizeof (pingobj_t));
911 obj->timeout = PING_DEF_TIMEOUT;
912 obj->ttl = PING_DEF_TTL;
913 obj->addrfamily = PING_DEF_AF;
914 obj->data = strdup (PING_DEF_DATA);
919 void ping_destroy (pingobj_t *obj)
927 while (current != NULL)
929 next = current->next;
934 if (obj->data != NULL)
937 if (obj->srcaddr != NULL)
945 int ping_setopt (pingobj_t *obj, int option, void *value)
951 case PING_OPT_TIMEOUT:
952 obj->timeout = *((double *) value);
953 if (obj->timeout < 0.0)
955 obj->timeout = PING_DEF_TIMEOUT;
961 obj->ttl = *((int *) value);
962 if ((obj->ttl < 1) || (obj->ttl > 255))
964 obj->ttl = PING_DEF_TTL;
970 obj->addrfamily = *((int *) value);
971 if ((obj->addrfamily != AF_UNSPEC)
972 && (obj->addrfamily != AF_INET)
973 && (obj->addrfamily != AF_INET6))
975 obj->addrfamily = PING_DEF_AF;
978 if (obj->srcaddr != NULL)
986 if (obj->data != NULL)
991 obj->data = strdup ((const char *) value);
994 case PING_OPT_SOURCE:
996 char *hostname = (char *) value;
997 struct addrinfo ai_hints;
998 struct addrinfo *ai_list;
1001 if (obj->addrfamily != AF_UNSPEC)
1003 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1006 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1007 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1008 #if defined(AI_ADDRCONFIG)
1009 ai_hints.ai_flags = AI_ADDRCONFIG;
1011 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1014 #if defined(EAI_SYSTEM)
1015 char errbuf[PING_ERRMSG_LEN];
1017 ping_set_error (obj, "getaddrinfo",
1018 #if defined(EAI_SYSTEM)
1019 (status == EAI_SYSTEM)
1020 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1022 gai_strerror (status));
1027 if (ai_list->ai_next != NULL)
1029 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1032 if (obj->srcaddr == NULL)
1034 obj->srcaddrlen = 0;
1035 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1036 if (obj->srcaddr == NULL)
1038 ping_set_errno (obj, "malloc", errno);
1040 freeaddrinfo (ai_list);
1044 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1045 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1046 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1047 ai_list->ai_addrlen);
1048 obj->srcaddrlen = ai_list->ai_addrlen;
1049 obj->addrfamily = ai_list->ai_family;
1051 freeaddrinfo (ai_list);
1052 } /* case PING_OPT_SOURCE */
1057 } /* switch (option) */
1060 } /* int ping_setopt */
1063 int ping_send (pingobj_t *obj)
1067 if (ping_send_all (obj) < 0)
1070 if ((ret = ping_receive_all (obj)) < 0)
1076 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1080 if (strcasecmp (ph->username, host) == 0)
1089 int ping_host_add (pingobj_t *obj, const char *host)
1093 struct addrinfo ai_hints;
1094 struct addrinfo *ai_list, *ai_ptr;
1097 dprintf ("host = %s\n", host);
1099 if (ping_host_search (obj->head, host) != NULL)
1102 memset (&ai_hints, '\0', sizeof (ai_hints));
1103 ai_hints.ai_flags = 0;
1104 #ifdef AI_ADDRCONFIG
1105 ai_hints.ai_flags |= AI_ADDRCONFIG;
1108 ai_hints.ai_flags |= AI_CANONNAME;
1110 ai_hints.ai_family = obj->addrfamily;
1111 ai_hints.ai_socktype = SOCK_RAW;
1113 if ((ph = ping_alloc ()) == NULL)
1115 dprintf ("Out of memory!\n");
1119 if ((ph->username = strdup (host)) == NULL)
1121 dprintf ("Out of memory!\n");
1122 ping_set_errno (obj, "strdup", errno);
1127 if ((ph->hostname = strdup (host)) == NULL)
1129 dprintf ("Out of memory!\n");
1130 ping_set_errno (obj, "strdup", errno);
1135 /* obj->data is not garuanteed to be != NULL */
1136 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1138 dprintf ("Out of memory!\n");
1139 ping_set_errno (obj, "strdup", errno);
1144 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1146 #if defined(EAI_SYSTEM)
1147 char errbuf[PING_ERRMSG_LEN];
1149 dprintf ("getaddrinfo failed\n");
1150 ping_set_error (obj, "getaddrinfo",
1151 #if defined(EAI_SYSTEM)
1152 (ai_return == EAI_SYSTEM)
1153 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1155 gai_strerror (ai_return));
1160 if (ai_list == NULL)
1161 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1163 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1167 if (ai_ptr->ai_family == AF_INET)
1169 ai_ptr->ai_socktype = SOCK_RAW;
1170 ai_ptr->ai_protocol = IPPROTO_ICMP;
1172 else if (ai_ptr->ai_family == AF_INET6)
1174 ai_ptr->ai_socktype = SOCK_RAW;
1175 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1179 char errmsg[PING_ERRMSG_LEN];
1181 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1182 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1185 ping_set_error (obj, "getaddrinfo", errmsg);
1189 /* TODO: Move this to a static function `ping_open_socket' and
1190 * call it whenever the socket dies. */
1191 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1195 char errbuf[PING_ERRMSG_LEN];
1196 dprintf ("socket: %s\n",
1197 sstrerror (errno, errbuf, sizeof (errbuf)));
1199 ping_set_errno (obj, "socket", errno);
1203 if (obj->srcaddr != NULL)
1205 assert (obj->srcaddrlen > 0);
1206 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1208 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1211 char errbuf[PING_ERRMSG_LEN];
1212 dprintf ("bind: %s\n",
1213 sstrerror (errno, errbuf, sizeof (errbuf)));
1215 ping_set_errno (obj, "bind", errno);
1222 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1223 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1224 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1225 ph->addrlen = ai_ptr->ai_addrlen;
1226 ph->addrfamily = ai_ptr->ai_family;
1229 if ((ai_ptr->ai_canonname != NULL)
1230 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1234 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1235 ph->hostname, ai_ptr->ai_canonname);
1237 old_hostname = ph->hostname;
1238 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1240 /* strdup failed, falling back to old hostname */
1241 ph->hostname = old_hostname;
1243 else if (old_hostname != NULL)
1245 free (old_hostname);
1248 #endif /* AI_CANONNAME */
1253 freeaddrinfo (ai_list);
1262 * Adding in the front is much easier, but then the iterator will
1263 * return the host that was added last as first host. That's just not
1266 if (obj->head == NULL)
1275 while (hptr->next != NULL)
1278 assert ((hptr != NULL) && (hptr->next == NULL));
1282 ping_set_ttl (ph, obj->ttl);
1287 int ping_host_remove (pingobj_t *obj, const char *host)
1289 pinghost_t *pre, *cur;
1296 if (strcasecmp (host, cur->username) == 0)
1305 ping_set_error (obj, "ping_host_remove", "Host not found");
1310 obj->head = cur->next;
1312 pre->next = cur->next;
1319 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1321 return ((pingobj_iter_t *) obj->head);
1324 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1326 return ((pingobj_iter_t *) iter->next);
1329 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1330 void *buffer, size_t *buffer_len)
1334 size_t orig_buffer_len = *buffer_len;
1338 case PING_INFO_USERNAME:
1340 *buffer_len = strlen (iter->username) + 1;
1341 if (orig_buffer_len <= *buffer_len)
1343 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1344 * will copy `*buffer_len' and pad the rest of
1345 * `buffer' with null-bytes */
1346 strncpy (buffer, iter->username, orig_buffer_len);
1350 case PING_INFO_HOSTNAME:
1352 *buffer_len = strlen (iter->hostname) + 1;
1353 if (orig_buffer_len < *buffer_len)
1355 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1356 * will copy `*buffer_len' and pad the rest of
1357 * `buffer' with null-bytes */
1358 strncpy (buffer, iter->hostname, orig_buffer_len);
1362 case PING_INFO_ADDRESS:
1363 ret = getnameinfo ((struct sockaddr *) iter->addr,
1371 if ((ret == EAI_MEMORY)
1373 || (ret == EAI_OVERFLOW)
1377 #if defined(EAI_SYSTEM)
1378 else if (ret == EAI_SYSTEM)
1386 case PING_INFO_FAMILY:
1388 *buffer_len = sizeof (int);
1389 if (orig_buffer_len < sizeof (int))
1391 *((int *) buffer) = iter->addrfamily;
1395 case PING_INFO_LATENCY:
1397 *buffer_len = sizeof (double);
1398 if (orig_buffer_len < sizeof (double))
1400 *((double *) buffer) = iter->latency;
1404 case PING_INFO_DROPPED:
1406 *buffer_len = sizeof (uint32_t);
1407 if (orig_buffer_len < sizeof (uint32_t))
1409 *((uint32_t *) buffer) = iter->dropped;
1413 case PING_INFO_SEQUENCE:
1415 *buffer_len = sizeof (unsigned int);
1416 if (orig_buffer_len < sizeof (unsigned int))
1418 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1422 case PING_INFO_IDENT:
1424 *buffer_len = sizeof (uint16_t);
1425 if (orig_buffer_len < sizeof (uint16_t))
1427 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1431 case PING_INFO_DATA:
1433 *buffer_len = strlen (iter->data);
1434 if (orig_buffer_len < *buffer_len)
1436 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1442 } /* ping_iterator_get_info */
1444 void *ping_iterator_get_context (pingobj_iter_t *iter)
1446 return (iter->context);
1449 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1451 iter->context = context;