2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2008 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
100 /* username: name passed in by the user */
102 /* hostname: name returned by the reverse lookup */
104 struct sockaddr_storage *addr;
110 struct timeval *timer;
116 struct pinghost *next;
126 struct sockaddr_storage *srcaddr;
127 socklen_t srcaddrlen;
129 char errmsg[PING_ERRMSG_LEN];
135 * private (static) functions
137 static void ping_set_error (pingobj_t *obj, const char *function,
140 snprintf (obj->errmsg, PING_ERRMSG_LEN, "%s: %s", function, message);
141 obj->errmsg[PING_ERRMSG_LEN - 1] = '\0';
144 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
147 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
148 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
150 while (res->tv_usec > 1000000)
152 res->tv_usec -= 1000000;
159 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
163 if ((tv1->tv_sec < tv2->tv_sec)
164 || ((tv1->tv_sec == tv2->tv_sec)
165 && (tv1->tv_usec < tv2->tv_usec)))
168 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
169 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
171 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
173 while (res->tv_usec < 0)
175 res->tv_usec += 1000000;
182 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
189 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
194 *(char *) &ret = *(char *) ptr;
198 /* Do this twice to get all possible carries.. */
199 sum = (sum >> 16) + (sum & 0xFFFF);
200 sum = (sum >> 16) + (sum & 0xFFFF);
207 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
210 struct icmp *icmp_hdr;
214 uint16_t recv_checksum;
215 uint16_t calc_checksum;
222 if (buffer_len < sizeof (struct ip))
225 ip_hdr = (struct ip *) buffer;
226 ip_hdr_len = ip_hdr->ip_hl << 2;
228 if (buffer_len < ip_hdr_len)
231 buffer += ip_hdr_len;
232 buffer_len -= ip_hdr_len;
234 if (buffer_len < sizeof (struct icmp))
237 icmp_hdr = (struct icmp *) buffer;
238 buffer += sizeof (struct icmp);
239 buffer_len -= sizeof (struct icmp);
241 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
243 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
247 recv_checksum = icmp_hdr->icmp_cksum;
248 icmp_hdr->icmp_cksum = 0;
249 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
250 sizeof (struct icmp) + buffer_len);
252 if (recv_checksum != calc_checksum)
254 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
255 recv_checksum, calc_checksum);
259 ident = ntohs (icmp_hdr->icmp_id);
260 seq = ntohs (icmp_hdr->icmp_seq);
262 for (ptr = ph; ptr != NULL; ptr = ptr->next)
264 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
265 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
267 if (ptr->addrfamily != AF_INET)
270 if (!timerisset (ptr->timer))
273 if (ptr->ident != ident)
276 if (((ptr->sequence - 1) & 0xFFFF) != seq)
279 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
280 ptr->hostname, ident, seq);
287 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
294 #ifndef ICMP6_ECHO_REQUEST
295 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
296 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
298 # define ICMP6_ECHO_REQUEST 128
302 #ifndef ICMP6_ECHO_REPLY
303 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
304 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
306 # define ICMP6_ECHO_REPLY 129
310 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
312 struct icmp6_hdr *icmp_hdr;
319 if (buffer_len < sizeof (struct icmp6_hdr))
322 icmp_hdr = (struct icmp6_hdr *) buffer;
323 buffer += sizeof (struct icmp);
324 buffer_len -= sizeof (struct icmp);
326 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
328 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
332 if (icmp_hdr->icmp6_code != 0)
334 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
338 ident = ntohs (icmp_hdr->icmp6_id);
339 seq = ntohs (icmp_hdr->icmp6_seq);
341 for (ptr = ph; ptr != NULL; ptr = ptr->next)
343 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
344 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
346 if (ptr->addrfamily != AF_INET6)
349 if (!timerisset (ptr->timer))
352 if (ptr->ident != ident)
355 if (((ptr->sequence - 1) & 0xFFFF) != seq)
358 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
359 ptr->hostname, ident, seq);
366 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
373 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
380 pinghost_t *host = NULL;
382 struct sockaddr_storage sa;
385 sa_len = sizeof (sa);
387 buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
388 (struct sockaddr *) &sa, &sa_len);
389 if (buffer_len == -1)
391 dprintf ("recvfrom: %s\n", strerror (errno));
395 dprintf ("Read %u bytes from fd = %i\n", (unsigned int) buffer_len, fd);
397 if (sa.ss_family == AF_INET)
399 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
402 else if (sa.ss_family == AF_INET6)
404 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
408 dprintf ("rcvd: %12i.%06i\n",
411 dprintf ("sent: %12i.%06i\n",
412 (int) host->timer->tv_sec,
413 (int) host->timer->tv_usec);
415 if (ping_timeval_sub (now, host->timer, &diff) < 0)
417 timerclear (host->timer);
421 dprintf ("diff: %12i.%06i\n",
425 host->latency = ((double) diff.tv_usec) / 1000.0;
426 host->latency += ((double) diff.tv_sec) * 1000.0;
428 timerclear (host->timer);
433 static int ping_receive_all (pingobj_t *obj)
442 struct timeval endtime;
443 struct timeval nowtime;
444 struct timeval timeout;
452 for (ptr = ph; ptr != NULL; ptr = ptr->next)
455 if (gettimeofday (&nowtime, NULL) == -1)
457 ping_set_error (obj, "gettimeofday", strerror (errno));
462 timeout.tv_sec = (time_t) obj->timeout;
463 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
465 dprintf ("Set timeout to %i.%06i seconds\n",
466 (int) timeout.tv_sec,
467 (int) timeout.tv_usec);
469 ping_timeval_add (&nowtime, &timeout, &endtime);
477 for (ptr = ph; ptr != NULL; ptr = ptr->next)
479 if (!timerisset (ptr->timer))
482 FD_SET (ptr->fd, &readfds);
485 if (max_readfds < ptr->fd)
486 max_readfds = ptr->fd;
489 if (num_readfds == 0)
492 if (gettimeofday (&nowtime, NULL) == -1)
494 ping_set_error (obj, "gettimeofday", strerror (errno));
498 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
501 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
502 (int) timeout.tv_sec,
503 (int) timeout.tv_usec);
505 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
507 if (gettimeofday (&nowtime, NULL) == -1)
509 ping_set_error (obj, "gettimeofday", strerror (errno));
513 if ((status == -1) && (errno == EINTR))
515 dprintf ("select was interrupted by signal..\n");
520 dprintf ("select: %s\n", strerror (errno));
523 else if (status == 0)
525 dprintf ("select timed out\n");
529 for (ptr = ph; ptr != NULL; ptr = ptr->next)
531 if (FD_ISSET (ptr->fd, &readfds))
532 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
540 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
541 * Sending functions: *
544 * +-> ping_send_one_ipv4 *
545 * `-> ping_send_one_ipv6 *
546 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
547 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
548 const void *buf, size_t buflen)
552 if (gettimeofday (ph->timer, NULL) == -1)
554 timerclear (ph->timer);
558 ret = sendto (ph->fd, buf, buflen, 0,
559 (struct sockaddr *) ph->addr, ph->addrlen);
563 #if defined(EHOSTUNREACH)
564 if (errno == EHOSTUNREACH)
567 #if defined(ENETUNREACH)
568 if (errno == ENETUNREACH)
571 ping_set_error (obj, "sendto", strerror (errno));
577 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
588 dprintf ("ph->hostname = %s\n", ph->hostname);
590 memset (buf, '\0', sizeof (buf));
591 icmp4 = (struct icmp *) buf;
592 data = (char *) (icmp4 + 1);
594 icmp4->icmp_type = ICMP_ECHO;
595 icmp4->icmp_code = 0;
596 icmp4->icmp_cksum = 0;
597 icmp4->icmp_id = htons (ph->ident);
598 icmp4->icmp_seq = htons (ph->sequence);
600 buflen = 4096 - sizeof (struct icmp);
601 strncpy (data, ph->data, buflen);
602 datalen = strlen (data);
604 buflen = datalen + sizeof (struct icmp);
606 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
608 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
610 status = ping_sendto (obj, ph, buf, buflen);
613 perror ("ping_sendto");
617 dprintf ("sendto: status = %i\n", status);
622 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
624 struct icmp6_hdr *icmp6;
633 dprintf ("ph->hostname = %s\n", ph->hostname);
635 memset (buf, '\0', sizeof (buf));
636 icmp6 = (struct icmp6_hdr *) buf;
637 data = (char *) (icmp6 + 1);
639 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
640 icmp6->icmp6_code = 0;
641 /* The checksum will be calculated by the TCP/IP stack. */
643 icmp6->icmp6_cksum = 0;
644 icmp6->icmp6_id = htons (ph->ident);
645 icmp6->icmp6_seq = htons (ph->sequence);
647 buflen = 4096 - sizeof (struct icmp6_hdr);
648 strncpy (data, ph->data, buflen);
649 datalen = strlen (data);
651 buflen = datalen + sizeof (struct icmp6_hdr);
653 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
655 status = ping_sendto (obj, ph, buf, buflen);
658 perror ("ping_sendto");
662 dprintf ("sendto: status = %i\n", status);
667 static int ping_send_all (pingobj_t *obj)
677 for (ptr = ph; ptr != NULL; ptr = ptr->next)
679 /* start timer.. The GNU `ping6' starts the timer before
680 * sending the packet, so I will do that too */
681 if (gettimeofday (ptr->timer, NULL) == -1)
683 dprintf ("gettimeofday: %s\n", strerror (errno));
684 timerclear (ptr->timer);
690 dprintf ("timer set for hostname = %s\n", ptr->hostname);
693 if (ptr->addrfamily == AF_INET6)
695 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
696 if (ping_send_one_ipv6 (obj, ptr) != 0)
698 timerclear (ptr->timer);
703 else if (ptr->addrfamily == AF_INET)
705 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
706 if (ping_send_one_ipv4 (obj, ptr) != 0)
708 timerclear (ptr->timer);
713 else /* this should not happen */
715 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
716 timerclear (ptr->timer);
728 * Set the TTL of a socket protocol independently.
730 static int ping_set_ttl (pinghost_t *ph, int ttl)
734 if (ph->addrfamily == AF_INET)
736 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
738 else if (ph->addrfamily == AF_INET6)
740 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
746 static int ping_get_ident (void)
749 static int did_seed = 0;
755 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
759 if (read (fd, &seed, sizeof (seed)) != -1)
762 dprintf ("Random seed: %i\n", seed);
770 dprintf ("open (/dev/urandom): %s\n", strerror (errno));
774 retval = (int) random ();
776 dprintf ("Random number: %i\n", retval);
781 static pinghost_t *ping_alloc (void)
786 ph_size = sizeof (pinghost_t)
787 + sizeof (struct sockaddr_storage)
788 + sizeof (struct timeval);
790 ph = (pinghost_t *) malloc (ph_size);
794 memset (ph, '\0', ph_size);
796 ph->timer = (struct timeval *) (ph + 1);
797 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
799 ph->addrlen = sizeof (struct sockaddr_storage);
802 ph->ident = ping_get_ident () & 0xFFFF;
807 static void ping_free (pinghost_t *ph)
812 if (ph->username != NULL)
815 if (ph->hostname != NULL)
818 if (ph->data != NULL)
827 const char *ping_get_error (pingobj_t *obj)
829 return (obj->errmsg);
832 pingobj_t *ping_construct (void)
836 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
838 memset (obj, '\0', sizeof (pingobj_t));
840 obj->timeout = PING_DEF_TIMEOUT;
841 obj->ttl = PING_DEF_TTL;
842 obj->addrfamily = PING_DEF_AF;
843 obj->data = strdup (PING_DEF_DATA);
848 void ping_destroy (pingobj_t *obj)
856 while (current != NULL)
858 next = current->next;
863 if (obj->data != NULL)
866 if (obj->srcaddr != NULL)
874 int ping_setopt (pingobj_t *obj, int option, void *value)
880 case PING_OPT_TIMEOUT:
881 obj->timeout = *((double *) value);
882 if (obj->timeout < 0.0)
884 obj->timeout = PING_DEF_TIMEOUT;
890 obj->ttl = *((int *) value);
891 if ((obj->ttl < 1) || (obj->ttl > 255))
893 obj->ttl = PING_DEF_TTL;
899 obj->addrfamily = *((int *) value);
900 if ((obj->addrfamily != AF_UNSPEC)
901 && (obj->addrfamily != AF_INET)
902 && (obj->addrfamily != AF_INET6))
904 obj->addrfamily = PING_DEF_AF;
907 if (obj->srcaddr != NULL)
915 if (obj->data != NULL)
920 obj->data = strdup ((const char *) value);
923 case PING_OPT_SOURCE:
925 char *hostname = (char *) value;
926 struct addrinfo ai_hints;
927 struct addrinfo *ai_list;
930 if (obj->addrfamily != AF_UNSPEC)
932 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
935 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
936 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
937 #if defined(AI_ADDRCONFIG)
938 ai_hints.ai_flags = AI_ADDRCONFIG;
940 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
943 ping_set_error (obj, "getaddrinfo",
946 : gai_strerror (status));
951 if (ai_list->ai_next != NULL)
953 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
956 if (obj->srcaddr == NULL)
959 obj->srcaddr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage));
960 if (obj->srcaddr == NULL)
962 ping_set_error (obj, "malloc",
965 freeaddrinfo (ai_list);
969 memset ((void *) obj->srcaddr, '\0', sizeof (struct sockaddr_storage));
970 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
971 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
972 ai_list->ai_addrlen);
973 obj->srcaddrlen = ai_list->ai_addrlen;
974 obj->addrfamily = ai_list->ai_family;
976 freeaddrinfo (ai_list);
977 } /* case PING_OPT_SOURCE */
982 } /* switch (option) */
985 } /* int ping_setopt */
988 int ping_send (pingobj_t *obj)
992 if (ping_send_all (obj) < 0)
995 if ((ret = ping_receive_all (obj)) < 0)
1001 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1005 if (strcasecmp (ph->username, host) == 0)
1014 int ping_host_add (pingobj_t *obj, const char *host)
1018 struct sockaddr_storage sockaddr;
1019 socklen_t sockaddr_len;
1021 struct addrinfo ai_hints;
1022 struct addrinfo *ai_list, *ai_ptr;
1025 dprintf ("host = %s\n", host);
1027 if (ping_host_search (obj->head, host) != NULL)
1030 memset (&ai_hints, '\0', sizeof (ai_hints));
1031 ai_hints.ai_flags = 0;
1032 #ifdef AI_ADDRCONFIG
1033 ai_hints.ai_flags |= AI_ADDRCONFIG;
1036 ai_hints.ai_flags |= AI_CANONNAME;
1038 ai_hints.ai_family = obj->addrfamily;
1039 ai_hints.ai_socktype = SOCK_RAW;
1041 if ((ph = ping_alloc ()) == NULL)
1043 dprintf ("Out of memory!\n");
1047 if ((ph->username = strdup (host)) == NULL)
1049 dprintf ("Out of memory!\n");
1050 ping_set_error (obj, "strdup", strerror (errno));
1055 if ((ph->hostname = strdup (host)) == NULL)
1057 dprintf ("Out of memory!\n");
1058 ping_set_error (obj, "strdup", strerror (errno));
1063 /* obj->data is not garuanteed to be != NULL */
1064 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1066 dprintf ("Out of memory!\n");
1067 ping_set_error (obj, "strdup", strerror (errno));
1072 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1074 dprintf ("getaddrinfo failed\n");
1075 ping_set_error (obj, "getaddrinfo",
1076 (ai_return == EAI_SYSTEM)
1078 : gai_strerror (ai_return));
1083 if (ai_list == NULL)
1084 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1086 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1090 sockaddr_len = sizeof (sockaddr);
1091 memset (&sockaddr, '\0', sockaddr_len);
1093 if (ai_ptr->ai_family == AF_INET)
1095 struct sockaddr_in *si;
1097 si = (struct sockaddr_in *) &sockaddr;
1098 si->sin_family = AF_INET;
1099 si->sin_port = htons (ph->ident);
1100 si->sin_addr.s_addr = htonl (INADDR_ANY);
1102 ai_ptr->ai_socktype = SOCK_RAW;
1103 ai_ptr->ai_protocol = IPPROTO_ICMP;
1105 else if (ai_ptr->ai_family == AF_INET6)
1107 struct sockaddr_in6 *si;
1109 si = (struct sockaddr_in6 *) &sockaddr;
1110 si->sin6_family = AF_INET6;
1111 si->sin6_port = htons (ph->ident);
1112 si->sin6_addr = in6addr_any;
1114 ai_ptr->ai_socktype = SOCK_RAW;
1115 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1119 char errmsg[PING_ERRMSG_LEN];
1121 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1122 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1125 ping_set_error (obj, "getaddrinfo", errmsg);
1129 /* TODO: Move this to a static function `ping_open_socket' and
1130 * call it whenever the socket dies. */
1131 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1134 dprintf ("socket: %s\n", strerror (errno));
1135 ping_set_error (obj, "socket", strerror (errno));
1139 if (obj->srcaddr != NULL)
1141 assert (obj->srcaddrlen > 0);
1142 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1144 if (bind (ph->fd, (struct sockaddr *) obj->srcaddr, obj->srcaddrlen) == -1)
1146 dprintf ("bind: %s\n", strerror (errno));
1147 ping_set_error (obj, "bind", strerror (errno));
1154 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1155 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1156 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1157 ph->addrlen = ai_ptr->ai_addrlen;
1158 ph->addrfamily = ai_ptr->ai_family;
1161 if ((ai_ptr->ai_canonname != NULL)
1162 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1166 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1167 ph->hostname, ai_ptr->ai_canonname);
1169 old_hostname = ph->hostname;
1170 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1172 /* strdup failed, falling back to old hostname */
1173 ph->hostname = old_hostname;
1175 else if (old_hostname != NULL)
1177 free (old_hostname);
1180 #endif /* AI_CANONNAME */
1185 freeaddrinfo (ai_list);
1194 * Adding in the front is much easier, but then the iterator will
1195 * return the host that was added last as first host. That's just not
1198 if (obj->head == NULL)
1207 while (hptr->next != NULL)
1210 assert ((hptr != NULL) && (hptr->next == NULL));
1214 ping_set_ttl (ph, obj->ttl);
1219 int ping_host_remove (pingobj_t *obj, const char *host)
1221 pinghost_t *pre, *cur;
1228 if (strcasecmp (host, cur->username) == 0)
1237 ping_set_error (obj, "ping_host_remove", "Host not found");
1242 obj->head = cur->next;
1244 pre->next = cur->next;
1251 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1253 return ((pingobj_iter_t *) obj->head);
1256 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1258 return ((pingobj_iter_t *) iter->next);
1261 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1262 void *buffer, size_t *buffer_len)
1266 size_t orig_buffer_len = *buffer_len;
1270 case PING_INFO_USERNAME:
1272 *buffer_len = strlen (iter->username) + 1;
1273 if (orig_buffer_len <= *buffer_len)
1275 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1276 * will copy `*buffer_len' and pad the rest of
1277 * `buffer' with null-bytes */
1278 strncpy (buffer, iter->username, orig_buffer_len);
1282 case PING_INFO_HOSTNAME:
1284 *buffer_len = strlen (iter->hostname) + 1;
1285 if (orig_buffer_len <= *buffer_len)
1287 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1288 * will copy `*buffer_len' and pad the rest of
1289 * `buffer' with null-bytes */
1290 strncpy (buffer, iter->hostname, orig_buffer_len);
1294 case PING_INFO_ADDRESS:
1295 ret = getnameinfo ((struct sockaddr *) iter->addr,
1303 if ((ret == EAI_MEMORY)
1305 || (ret == EAI_OVERFLOW)
1309 else if (ret == EAI_SYSTEM)
1316 case PING_INFO_FAMILY:
1318 *buffer_len = sizeof (int);
1319 if (orig_buffer_len < sizeof (int))
1321 *((int *) buffer) = iter->addrfamily;
1325 case PING_INFO_LATENCY:
1327 *buffer_len = sizeof (double);
1328 if (orig_buffer_len < sizeof (double))
1330 *((double *) buffer) = iter->latency;
1334 case PING_INFO_SEQUENCE:
1336 *buffer_len = sizeof (unsigned int);
1337 if (orig_buffer_len < sizeof (unsigned int))
1339 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1343 case PING_INFO_IDENT:
1345 *buffer_len = sizeof (uint16_t);
1346 if (orig_buffer_len < sizeof (uint16_t))
1348 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1352 case PING_INFO_DATA:
1354 *buffer_len = strlen (iter->data);
1355 if (orig_buffer_len < *buffer_len)
1357 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1365 void *ping_iterator_get_context (pingobj_iter_t *iter)
1367 return (iter->context);
1370 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1372 iter->context = context;