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;
137 char errmsg[PING_ERRMSG_LEN];
143 * private (static) functions
145 /* Even though Posix requires "strerror_r" to return an "int",
146 * some systems (e.g. the GNU libc) return a "char *" _and_
147 * ignore the second argument ... -tokkee */
148 static char *sstrerror (int errnum, char *buf, size_t buflen)
154 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
156 /* #endif !HAVE_STRERROR_R */
158 #elif STRERROR_R_CHAR_P
161 temp = strerror_r (errnum, buf, buflen);
164 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
165 strncpy (buf, temp, buflen);
167 strncpy (buf, "strerror_r did not return "
168 "an error message", buflen);
171 /* #endif STRERROR_R_CHAR_P */
174 if (strerror_r (errnum, buf, buflen) != 0)
176 snprintf (buf, buflen, "Error %i (%#x); "
177 "Additionally, strerror_r failed.",
180 #endif /* STRERROR_R_CHAR_P */
185 } /* char *sstrerror */
187 static void ping_set_error (pingobj_t *obj, const char *function,
190 snprintf (obj->errmsg, sizeof (obj->errmsg),
191 "%s: %s", function, message);
192 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
195 static void ping_set_errno (pingobj_t *obj, int error_number)
197 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
200 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
203 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
204 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
206 while (res->tv_usec > 1000000)
208 res->tv_usec -= 1000000;
215 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
218 if ((tv1->tv_sec < tv2->tv_sec)
219 || ((tv1->tv_sec == tv2->tv_sec)
220 && (tv1->tv_usec < tv2->tv_usec)))
223 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
224 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
226 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
228 while (res->tv_usec < 0)
230 res->tv_usec += 1000000;
237 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
244 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
249 *(char *) &ret = *(char *) ptr;
253 /* Do this twice to get all possible carries.. */
254 sum = (sum >> 16) + (sum & 0xFFFF);
255 sum = (sum >> 16) + (sum & 0xFFFF);
262 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
265 struct icmp *icmp_hdr;
269 uint16_t recv_checksum;
270 uint16_t calc_checksum;
277 if (buffer_len < sizeof (struct ip))
280 ip_hdr = (struct ip *) buffer;
281 ip_hdr_len = ip_hdr->ip_hl << 2;
283 if (buffer_len < ip_hdr_len)
286 buffer += ip_hdr_len;
287 buffer_len -= ip_hdr_len;
289 if (buffer_len < sizeof (struct icmp))
292 icmp_hdr = (struct icmp *) buffer;
293 buffer += sizeof (struct icmp);
294 buffer_len -= sizeof (struct icmp);
296 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
298 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
302 recv_checksum = icmp_hdr->icmp_cksum;
303 icmp_hdr->icmp_cksum = 0;
304 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
305 sizeof (struct icmp) + buffer_len);
307 if (recv_checksum != calc_checksum)
309 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
310 recv_checksum, calc_checksum);
314 ident = ntohs (icmp_hdr->icmp_id);
315 seq = ntohs (icmp_hdr->icmp_seq);
317 for (ptr = ph; ptr != NULL; ptr = ptr->next)
319 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
320 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
322 if (ptr->addrfamily != AF_INET)
325 if (!timerisset (ptr->timer))
328 if (ptr->ident != ident)
331 if (((ptr->sequence - 1) & 0xFFFF) != seq)
334 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
335 ptr->hostname, ident, seq);
342 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
347 ptr->recv_ttl = ip_hdr->ip_ttl;
352 #ifndef ICMP6_ECHO_REQUEST
353 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
354 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
356 # define ICMP6_ECHO_REQUEST 128
360 #ifndef ICMP6_ECHO_REPLY
361 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
362 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
364 # define ICMP6_ECHO_REPLY 129
368 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
370 struct icmp6_hdr *icmp_hdr;
377 if (buffer_len < sizeof (struct icmp6_hdr))
380 icmp_hdr = (struct icmp6_hdr *) buffer;
381 buffer += sizeof (struct icmp);
382 buffer_len -= sizeof (struct icmp);
384 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
386 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
390 if (icmp_hdr->icmp6_code != 0)
392 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
396 ident = ntohs (icmp_hdr->icmp6_id);
397 seq = ntohs (icmp_hdr->icmp6_seq);
399 for (ptr = ph; ptr != NULL; ptr = ptr->next)
401 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
402 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
404 if (ptr->addrfamily != AF_INET6)
407 if (!timerisset (ptr->timer))
410 if (ptr->ident != ident)
413 if (((ptr->sequence - 1) & 0xFFFF) != seq)
416 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
417 ptr->hostname, ident, seq);
424 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
431 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
434 pinghost_t *host = NULL;
438 * Set up the receive buffer..
440 struct msghdr msghdr;
441 struct cmsghdr *cmsg;
442 char payload_buffer[4096];
443 ssize_t payload_buffer_len;
444 char control_buffer[4096];
445 struct iovec payload_iovec;
447 memset (&payload_iovec, 0, sizeof (payload_iovec));
448 payload_iovec.iov_base = payload_buffer;
449 payload_iovec.iov_len = sizeof (payload_buffer);
451 memset (&msghdr, 0, sizeof (msghdr));
452 /* unspecified source address */
453 msghdr.msg_name = NULL;
454 msghdr.msg_namelen = 0;
455 /* output buffer vector, see readv(2) */
456 msghdr.msg_iov = &payload_iovec;
457 msghdr.msg_iovlen = 1;
458 /* output buffer for control messages */
459 msghdr.msg_control = control_buffer;
460 msghdr.msg_controllen = sizeof (control_buffer);
461 /* flags; this is an output only field.. */
462 msghdr.msg_flags = 0;
464 msghdr.msg_flags |= MSG_XPG4_2;
467 payload_buffer_len = recvmsg (fd, &msghdr, /* flags = */ 0);
468 if (payload_buffer_len < 0)
471 char errbuf[PING_ERRMSG_LEN];
472 dprintf ("recvfrom: %s\n",
473 sstrerror (errno, errbuf, sizeof (errbuf)));
477 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, fd);
479 /* Iterate over all auxiliary data in msghdr */
482 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
484 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
486 if (ph->addrfamily == AF_INET) /* {{{ */
488 if (cmsg->cmsg_level != IPPROTO_IP)
491 if (cmsg->cmsg_type == IP_TTL)
493 memcpy (&recv_ttl, CMSG_DATA (cmsg),
495 dprintf ("TTLv4 = %i;\n", recv_ttl);
499 dprintf ("Not handling option %i.\n",
503 else if (ph->addrfamily == AF_INET6) /* {{{ */
505 if (cmsg->cmsg_level != IPPROTO_IPV6)
508 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
510 memcpy (&recv_ttl, CMSG_DATA (cmsg),
512 dprintf ("TTLv6 = %i;\n", recv_ttl);
516 dprintf ("Not handling option %i.\n",
522 dprintf ("Don't know how to handle "
523 "unknown protocol %i.\n",
526 } /* }}} for (cmsg) */
528 if (ph->addrfamily == AF_INET)
530 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
534 else if (ph->addrfamily == AF_INET6)
536 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
542 dprintf ("ping_receive_one: Unknown address family %i.\n",
547 dprintf ("rcvd: %12i.%06i\n",
550 dprintf ("sent: %12i.%06i\n",
551 (int) host->timer->tv_sec,
552 (int) host->timer->tv_usec);
554 if (ping_timeval_sub (now, host->timer, &diff) < 0)
556 timerclear (host->timer);
560 dprintf ("diff: %12i.%06i\n",
565 host->recv_ttl = recv_ttl;
567 host->latency = ((double) diff.tv_usec) / 1000.0;
568 host->latency += ((double) diff.tv_sec) * 1000.0;
570 timerclear (host->timer);
575 static int ping_receive_all (pingobj_t *obj)
584 struct timeval endtime;
585 struct timeval nowtime;
586 struct timeval timeout;
594 for (ptr = ph; ptr != NULL; ptr = ptr->next)
597 if (gettimeofday (&nowtime, NULL) == -1)
599 ping_set_errno (obj, errno);
604 timeout.tv_sec = (time_t) obj->timeout;
605 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
607 dprintf ("Set timeout to %i.%06i seconds\n",
608 (int) timeout.tv_sec,
609 (int) timeout.tv_usec);
611 ping_timeval_add (&nowtime, &timeout, &endtime);
619 for (ptr = ph; ptr != NULL; ptr = ptr->next)
621 if (!timerisset (ptr->timer))
624 FD_SET (ptr->fd, &readfds);
627 if (max_readfds < ptr->fd)
628 max_readfds = ptr->fd;
631 if (num_readfds == 0)
634 if (gettimeofday (&nowtime, NULL) == -1)
636 ping_set_errno (obj, errno);
640 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
643 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
644 (int) timeout.tv_sec,
645 (int) timeout.tv_usec);
647 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
649 if (gettimeofday (&nowtime, NULL) == -1)
651 ping_set_errno (obj, errno);
655 if ((status == -1) && (errno == EINTR))
657 dprintf ("select was interrupted by signal..\n");
663 char errbuf[PING_ERRMSG_LEN];
664 dprintf ("select: %s\n",
665 sstrerror (errno, errbuf, sizeof (errbuf)));
669 else if (status == 0)
671 dprintf ("select timed out\n");
672 for (ptr = ph; ptr != NULL; ptr = ptr->next)
673 if (ptr->latency < 0.0)
678 for (ptr = ph; ptr != NULL; ptr = ptr->next)
680 if (FD_ISSET (ptr->fd, &readfds))
681 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
689 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
690 * Sending functions: *
693 * +-> ping_send_one_ipv4 *
694 * `-> ping_send_one_ipv6 *
695 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
696 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
697 const void *buf, size_t buflen)
701 if (gettimeofday (ph->timer, NULL) == -1)
703 timerclear (ph->timer);
707 ret = sendto (ph->fd, buf, buflen, 0,
708 (struct sockaddr *) ph->addr, ph->addrlen);
712 #if defined(EHOSTUNREACH)
713 if (errno == EHOSTUNREACH)
716 #if defined(ENETUNREACH)
717 if (errno == ENETUNREACH)
720 ping_set_errno (obj, errno);
726 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
737 dprintf ("ph->hostname = %s\n", ph->hostname);
739 memset (buf, '\0', sizeof (buf));
740 icmp4 = (struct icmp *) buf;
741 data = (char *) (icmp4 + 1);
743 icmp4->icmp_type = ICMP_ECHO;
744 icmp4->icmp_code = 0;
745 icmp4->icmp_cksum = 0;
746 icmp4->icmp_id = htons (ph->ident);
747 icmp4->icmp_seq = htons (ph->sequence);
749 buflen = 4096 - sizeof (struct icmp);
750 strncpy (data, ph->data, buflen);
751 datalen = strlen (data);
753 buflen = datalen + sizeof (struct icmp);
755 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
757 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
759 status = ping_sendto (obj, ph, buf, buflen);
762 perror ("ping_sendto");
766 dprintf ("sendto: status = %i\n", status);
771 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
773 struct icmp6_hdr *icmp6;
782 dprintf ("ph->hostname = %s\n", ph->hostname);
784 memset (buf, '\0', sizeof (buf));
785 icmp6 = (struct icmp6_hdr *) buf;
786 data = (char *) (icmp6 + 1);
788 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
789 icmp6->icmp6_code = 0;
790 /* The checksum will be calculated by the TCP/IP stack. */
792 icmp6->icmp6_cksum = 0;
793 icmp6->icmp6_id = htons (ph->ident);
794 icmp6->icmp6_seq = htons (ph->sequence);
796 buflen = 4096 - sizeof (struct icmp6_hdr);
797 strncpy (data, ph->data, buflen);
798 datalen = strlen (data);
800 buflen = datalen + sizeof (struct icmp6_hdr);
802 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
804 status = ping_sendto (obj, ph, buf, buflen);
807 perror ("ping_sendto");
811 dprintf ("sendto: status = %i\n", status);
816 static int ping_send_all (pingobj_t *obj)
826 for (ptr = ph; ptr != NULL; ptr = ptr->next)
828 /* start timer.. The GNU `ping6' starts the timer before
829 * sending the packet, so I will do that too */
830 if (gettimeofday (ptr->timer, NULL) == -1)
833 char errbuf[PING_ERRMSG_LEN];
834 dprintf ("gettimeofday: %s\n",
835 sstrerror (errno, errbuf, sizeof (errbuf)));
837 timerclear (ptr->timer);
843 dprintf ("timer set for hostname = %s\n", ptr->hostname);
846 if (ptr->addrfamily == AF_INET6)
848 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
849 if (ping_send_one_ipv6 (obj, ptr) != 0)
851 timerclear (ptr->timer);
856 else if (ptr->addrfamily == AF_INET)
858 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
859 if (ping_send_one_ipv4 (obj, ptr) != 0)
861 timerclear (ptr->timer);
866 else /* this should not happen */
868 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
869 timerclear (ptr->timer);
881 * Set the TTL of a socket protocol independently.
883 static int ping_set_ttl (pinghost_t *ph, int ttl)
887 if (ph->addrfamily == AF_INET)
889 dprintf ("Setting TTLv4 to %i\n", ttl);
890 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
893 else if (ph->addrfamily == AF_INET6)
895 dprintf ("Setting TTLv6 to %i\n", ttl);
896 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
903 static int ping_get_ident (void)
906 static int did_seed = 0;
912 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
916 if (read (fd, &seed, sizeof (seed)) != -1)
919 dprintf ("Random seed: %#x\n", seed);
928 char errbuf[PING_ERRMSG_LEN];
929 dprintf ("open (/dev/urandom): %s\n",
930 sstrerror (errno, errbuf, sizeof (errbuf)));
935 retval = (int) random ();
937 dprintf ("Random number: %#x\n", retval);
942 static pinghost_t *ping_alloc (void)
947 ph_size = sizeof (pinghost_t)
948 + sizeof (struct sockaddr_storage)
949 + sizeof (struct timeval);
951 ph = (pinghost_t *) malloc (ph_size);
955 memset (ph, '\0', ph_size);
957 ph->timer = (struct timeval *) (ph + 1);
958 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
960 ph->addrlen = sizeof (struct sockaddr_storage);
964 ph->ident = ping_get_ident () & 0xFFFF;
969 static void ping_free (pinghost_t *ph)
974 if (ph->username != NULL)
977 if (ph->hostname != NULL)
980 if (ph->data != NULL)
989 const char *ping_get_error (pingobj_t *obj)
991 return (obj->errmsg);
994 pingobj_t *ping_construct (void)
998 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1000 memset (obj, '\0', sizeof (pingobj_t));
1002 obj->timeout = PING_DEF_TIMEOUT;
1003 obj->ttl = PING_DEF_TTL;
1004 obj->addrfamily = PING_DEF_AF;
1005 obj->data = strdup (PING_DEF_DATA);
1010 void ping_destroy (pingobj_t *obj)
1012 pinghost_t *current;
1015 current = obj->head;
1018 while (current != NULL)
1020 next = current->next;
1021 ping_free (current);
1025 if (obj->data != NULL)
1028 if (obj->srcaddr != NULL)
1029 free (obj->srcaddr);
1036 int ping_setopt (pingobj_t *obj, int option, void *value)
1042 case PING_OPT_TIMEOUT:
1043 obj->timeout = *((double *) value);
1044 if (obj->timeout < 0.0)
1046 obj->timeout = PING_DEF_TIMEOUT;
1052 obj->ttl = *((int *) value);
1053 if ((obj->ttl < 1) || (obj->ttl > 255))
1055 obj->ttl = PING_DEF_TTL;
1062 for (ph = obj->head; ph != NULL; ph = ph->next)
1063 ping_set_ttl (ph, obj->ttl);
1068 obj->addrfamily = *((int *) value);
1069 if ((obj->addrfamily != AF_UNSPEC)
1070 && (obj->addrfamily != AF_INET)
1071 && (obj->addrfamily != AF_INET6))
1073 obj->addrfamily = PING_DEF_AF;
1076 if (obj->srcaddr != NULL)
1078 free (obj->srcaddr);
1079 obj->srcaddr = NULL;
1084 if (obj->data != NULL)
1089 obj->data = strdup ((const char *) value);
1092 case PING_OPT_SOURCE:
1094 char *hostname = (char *) value;
1095 struct addrinfo ai_hints;
1096 struct addrinfo *ai_list;
1099 if (obj->addrfamily != AF_UNSPEC)
1101 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1104 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1105 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1106 #if defined(AI_ADDRCONFIG)
1107 ai_hints.ai_flags = AI_ADDRCONFIG;
1109 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1112 #if defined(EAI_SYSTEM)
1113 char errbuf[PING_ERRMSG_LEN];
1115 ping_set_error (obj, "getaddrinfo",
1116 #if defined(EAI_SYSTEM)
1117 (status == EAI_SYSTEM)
1118 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1120 gai_strerror (status));
1125 if (ai_list->ai_next != NULL)
1127 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1130 if (obj->srcaddr == NULL)
1132 obj->srcaddrlen = 0;
1133 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1134 if (obj->srcaddr == NULL)
1136 ping_set_errno (obj, errno);
1138 freeaddrinfo (ai_list);
1142 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1143 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1144 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1145 ai_list->ai_addrlen);
1146 obj->srcaddrlen = ai_list->ai_addrlen;
1147 obj->addrfamily = ai_list->ai_family;
1149 freeaddrinfo (ai_list);
1150 } /* case PING_OPT_SOURCE */
1155 } /* switch (option) */
1158 } /* int ping_setopt */
1161 int ping_send (pingobj_t *obj)
1165 if (ping_send_all (obj) < 0)
1168 if ((ret = ping_receive_all (obj)) < 0)
1174 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1178 if (strcasecmp (ph->username, host) == 0)
1187 int ping_host_add (pingobj_t *obj, const char *host)
1191 struct addrinfo ai_hints;
1192 struct addrinfo *ai_list, *ai_ptr;
1195 dprintf ("host = %s\n", host);
1197 if (ping_host_search (obj->head, host) != NULL)
1200 memset (&ai_hints, '\0', sizeof (ai_hints));
1201 ai_hints.ai_flags = 0;
1202 #ifdef AI_ADDRCONFIG
1203 ai_hints.ai_flags |= AI_ADDRCONFIG;
1206 ai_hints.ai_flags |= AI_CANONNAME;
1208 ai_hints.ai_family = obj->addrfamily;
1209 ai_hints.ai_socktype = SOCK_RAW;
1211 if ((ph = ping_alloc ()) == NULL)
1213 dprintf ("Out of memory!\n");
1217 if ((ph->username = strdup (host)) == NULL)
1219 dprintf ("Out of memory!\n");
1220 ping_set_errno (obj, errno);
1225 if ((ph->hostname = strdup (host)) == NULL)
1227 dprintf ("Out of memory!\n");
1228 ping_set_errno (obj, errno);
1233 /* obj->data is not garuanteed to be != NULL */
1234 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1236 dprintf ("Out of memory!\n");
1237 ping_set_errno (obj, errno);
1242 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1244 #if defined(EAI_SYSTEM)
1245 char errbuf[PING_ERRMSG_LEN];
1247 dprintf ("getaddrinfo failed\n");
1248 ping_set_error (obj, "getaddrinfo",
1249 #if defined(EAI_SYSTEM)
1250 (ai_return == EAI_SYSTEM)
1251 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1253 gai_strerror (ai_return));
1258 if (ai_list == NULL)
1259 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1261 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1265 if (ai_ptr->ai_family == AF_INET)
1267 ai_ptr->ai_socktype = SOCK_RAW;
1268 ai_ptr->ai_protocol = IPPROTO_ICMP;
1270 else if (ai_ptr->ai_family == AF_INET6)
1272 ai_ptr->ai_socktype = SOCK_RAW;
1273 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1277 char errmsg[PING_ERRMSG_LEN];
1279 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1280 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1283 ping_set_error (obj, "getaddrinfo", errmsg);
1287 /* TODO: Move this to a static function `ping_open_socket' and
1288 * call it whenever the socket dies. */
1289 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1293 char errbuf[PING_ERRMSG_LEN];
1294 dprintf ("socket: %s\n",
1295 sstrerror (errno, errbuf, sizeof (errbuf)));
1297 ping_set_errno (obj, errno);
1301 if (obj->srcaddr != NULL)
1303 assert (obj->srcaddrlen > 0);
1304 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1306 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1309 char errbuf[PING_ERRMSG_LEN];
1310 dprintf ("bind: %s\n",
1311 sstrerror (errno, errbuf, sizeof (errbuf)));
1313 ping_set_errno (obj, errno);
1320 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1321 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1322 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1323 ph->addrlen = ai_ptr->ai_addrlen;
1324 ph->addrfamily = ai_ptr->ai_family;
1327 if ((ai_ptr->ai_canonname != NULL)
1328 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1332 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1333 ph->hostname, ai_ptr->ai_canonname);
1335 old_hostname = ph->hostname;
1336 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1338 /* strdup failed, falling back to old hostname */
1339 ph->hostname = old_hostname;
1341 else if (old_hostname != NULL)
1343 free (old_hostname);
1346 #endif /* AI_CANONNAME */
1348 if (ph->addrfamily == AF_INET)
1352 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1353 &opt, sizeof (opt));
1355 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1356 else if (ph->addrfamily == AF_INET6)
1360 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1361 &opt, sizeof (opt));
1368 freeaddrinfo (ai_list);
1377 * Adding in the front is much easier, but then the iterator will
1378 * return the host that was added last as first host. That's just not
1381 if (obj->head == NULL)
1390 while (hptr->next != NULL)
1393 assert ((hptr != NULL) && (hptr->next == NULL));
1397 ping_set_ttl (ph, obj->ttl);
1400 } /* int ping_host_add */
1402 int ping_host_remove (pingobj_t *obj, const char *host)
1404 pinghost_t *pre, *cur;
1411 if (strcasecmp (host, cur->username) == 0)
1420 ping_set_error (obj, "ping_host_remove", "Host not found");
1425 obj->head = cur->next;
1427 pre->next = cur->next;
1434 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1436 return ((pingobj_iter_t *) obj->head);
1439 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1441 return ((pingobj_iter_t *) iter->next);
1444 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1445 void *buffer, size_t *buffer_len)
1449 size_t orig_buffer_len = *buffer_len;
1453 case PING_INFO_USERNAME:
1455 *buffer_len = strlen (iter->username) + 1;
1456 if (orig_buffer_len <= *buffer_len)
1458 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1459 * will copy `*buffer_len' and pad the rest of
1460 * `buffer' with null-bytes */
1461 strncpy (buffer, iter->username, orig_buffer_len);
1465 case PING_INFO_HOSTNAME:
1467 *buffer_len = strlen (iter->hostname) + 1;
1468 if (orig_buffer_len < *buffer_len)
1470 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1471 * will copy `*buffer_len' and pad the rest of
1472 * `buffer' with null-bytes */
1473 strncpy (buffer, iter->hostname, orig_buffer_len);
1477 case PING_INFO_ADDRESS:
1478 ret = getnameinfo ((struct sockaddr *) iter->addr,
1486 if ((ret == EAI_MEMORY)
1488 || (ret == EAI_OVERFLOW)
1492 #if defined(EAI_SYSTEM)
1493 else if (ret == EAI_SYSTEM)
1501 case PING_INFO_FAMILY:
1503 *buffer_len = sizeof (int);
1504 if (orig_buffer_len < sizeof (int))
1506 *((int *) buffer) = iter->addrfamily;
1510 case PING_INFO_LATENCY:
1512 *buffer_len = sizeof (double);
1513 if (orig_buffer_len < sizeof (double))
1515 *((double *) buffer) = iter->latency;
1519 case PING_INFO_DROPPED:
1521 *buffer_len = sizeof (uint32_t);
1522 if (orig_buffer_len < sizeof (uint32_t))
1524 *((uint32_t *) buffer) = iter->dropped;
1528 case PING_INFO_SEQUENCE:
1530 *buffer_len = sizeof (unsigned int);
1531 if (orig_buffer_len < sizeof (unsigned int))
1533 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1537 case PING_INFO_IDENT:
1539 *buffer_len = sizeof (uint16_t);
1540 if (orig_buffer_len < sizeof (uint16_t))
1542 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1546 case PING_INFO_DATA:
1548 *buffer_len = strlen (iter->data);
1549 if (orig_buffer_len < *buffer_len)
1551 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1555 case PING_INFO_RECV_TTL:
1557 *buffer_len = sizeof (int);
1558 if (orig_buffer_len < sizeof (int))
1560 *((int *) buffer) = iter->recv_ttl;
1566 } /* ping_iterator_get_info */
1568 void *ping_iterator_get_context (pingobj_iter_t *iter)
1570 return (iter->context);
1573 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1575 iter->context = context;