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 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",
346 ptr->recv_ttl = ip_hdr->ip_ttl;
351 #ifndef ICMP6_ECHO_REQUEST
352 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
353 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
355 # define ICMP6_ECHO_REQUEST 128
359 #ifndef ICMP6_ECHO_REPLY
360 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
361 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
363 # define ICMP6_ECHO_REPLY 129
367 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
369 struct icmp6_hdr *icmp_hdr;
376 if (buffer_len < sizeof (struct icmp6_hdr))
379 icmp_hdr = (struct icmp6_hdr *) buffer;
380 buffer += sizeof (struct icmp);
381 buffer_len -= sizeof (struct icmp);
383 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
385 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
389 if (icmp_hdr->icmp6_code != 0)
391 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
395 ident = ntohs (icmp_hdr->icmp6_id);
396 seq = ntohs (icmp_hdr->icmp6_seq);
398 for (ptr = ph; ptr != NULL; ptr = ptr->next)
400 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
401 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
403 if (ptr->addrfamily != AF_INET6)
406 if (!timerisset (ptr->timer))
409 if (ptr->ident != ident)
412 if (((ptr->sequence - 1) & 0xFFFF) != seq)
415 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
416 ptr->hostname, ident, seq);
423 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
430 static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
433 pinghost_t *host = NULL;
437 * Set up the receive buffer..
439 struct msghdr msghdr;
440 struct cmsghdr *cmsg;
441 char payload_buffer[4096];
442 ssize_t payload_buffer_len;
443 char control_buffer[4096];
444 struct iovec payload_iovec;
446 memset (&payload_iovec, 0, sizeof (payload_iovec));
447 payload_iovec.iov_base = payload_buffer;
448 payload_iovec.iov_len = sizeof (payload_buffer);
450 memset (&msghdr, 0, sizeof (msghdr));
451 /* unspecified source address */
452 msghdr.msg_name = NULL;
453 msghdr.msg_namelen = 0;
454 /* output buffer vector, see readv(2) */
455 msghdr.msg_iov = &payload_iovec;
456 msghdr.msg_iovlen = 1;
457 /* output buffer for control messages */
458 msghdr.msg_control = control_buffer;
459 msghdr.msg_controllen = sizeof (control_buffer);
460 /* flags; this is an output only field.. */
461 msghdr.msg_flags = 0;
463 msghdr.msg_flags |= MSG_XPG4_2;
466 payload_buffer_len = recvmsg (fd, &msghdr, /* flags = */ 0);
467 if (payload_buffer_len < 0)
470 char errbuf[PING_ERRMSG_LEN];
471 dprintf ("recvfrom: %s\n",
472 sstrerror (errno, errbuf, sizeof (errbuf)));
476 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, fd);
478 /* Iterate over all auxiliary data in msghdr */
481 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
483 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
485 if (ph->addrfamily == AF_INET) /* {{{ */
487 if (cmsg->cmsg_level != IPPROTO_IP)
490 if (cmsg->cmsg_type == IP_TTL)
492 memcpy (&recv_ttl, CMSG_DATA (cmsg),
494 dprintf ("TTLv4 = %i;\n", recv_ttl);
498 dprintf ("Not handling option %i.\n",
502 else if (ph->addrfamily == AF_INET6) /* {{{ */
504 if (cmsg->cmsg_level != IPPROTO_IPV6)
507 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
509 memcpy (&recv_ttl, CMSG_DATA (cmsg),
511 dprintf ("TTLv6 = %i;\n", recv_ttl);
515 dprintf ("Not handling option %i.\n",
521 dprintf ("Don't know how to handle "
522 "unknown protocol %i.\n",
525 } /* }}} for (cmsg) */
527 if (ph->addrfamily == AF_INET)
529 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
533 else if (ph->addrfamily == AF_INET6)
535 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
541 dprintf ("ping_receive_one: Unknown address family %i.\n",
546 dprintf ("rcvd: %12i.%06i\n",
549 dprintf ("sent: %12i.%06i\n",
550 (int) host->timer->tv_sec,
551 (int) host->timer->tv_usec);
553 if (ping_timeval_sub (now, host->timer, &diff) < 0)
555 timerclear (host->timer);
559 dprintf ("diff: %12i.%06i\n",
564 host->recv_ttl = recv_ttl;
566 host->latency = ((double) diff.tv_usec) / 1000.0;
567 host->latency += ((double) diff.tv_sec) * 1000.0;
569 timerclear (host->timer);
574 static int ping_receive_all (pingobj_t *obj)
583 struct timeval endtime;
584 struct timeval nowtime;
585 struct timeval timeout;
593 for (ptr = ph; ptr != NULL; ptr = ptr->next)
596 if (gettimeofday (&nowtime, NULL) == -1)
598 ping_set_errno (obj, errno);
603 timeout.tv_sec = (time_t) obj->timeout;
604 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
606 dprintf ("Set timeout to %i.%06i seconds\n",
607 (int) timeout.tv_sec,
608 (int) timeout.tv_usec);
610 ping_timeval_add (&nowtime, &timeout, &endtime);
618 for (ptr = ph; ptr != NULL; ptr = ptr->next)
620 if (!timerisset (ptr->timer))
623 FD_SET (ptr->fd, &readfds);
626 if (max_readfds < ptr->fd)
627 max_readfds = ptr->fd;
630 if (num_readfds == 0)
633 if (gettimeofday (&nowtime, NULL) == -1)
635 ping_set_errno (obj, errno);
639 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
642 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
643 (int) timeout.tv_sec,
644 (int) timeout.tv_usec);
646 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
648 if (gettimeofday (&nowtime, NULL) == -1)
650 ping_set_errno (obj, errno);
654 if ((status == -1) && (errno == EINTR))
656 dprintf ("select was interrupted by signal..\n");
662 char errbuf[PING_ERRMSG_LEN];
663 dprintf ("select: %s\n",
664 sstrerror (errno, errbuf, sizeof (errbuf)));
668 else if (status == 0)
670 dprintf ("select timed out\n");
671 for (ptr = ph; ptr != NULL; ptr = ptr->next)
672 if (ptr->latency < 0.0)
677 for (ptr = ph; ptr != NULL; ptr = ptr->next)
679 if (FD_ISSET (ptr->fd, &readfds))
680 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
688 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
689 * Sending functions: *
692 * +-> ping_send_one_ipv4 *
693 * `-> ping_send_one_ipv6 *
694 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
695 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
696 const void *buf, size_t buflen)
700 if (gettimeofday (ph->timer, NULL) == -1)
702 timerclear (ph->timer);
706 ret = sendto (ph->fd, buf, buflen, 0,
707 (struct sockaddr *) ph->addr, ph->addrlen);
711 #if defined(EHOSTUNREACH)
712 if (errno == EHOSTUNREACH)
715 #if defined(ENETUNREACH)
716 if (errno == ENETUNREACH)
719 ping_set_errno (obj, errno);
725 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
736 dprintf ("ph->hostname = %s\n", ph->hostname);
738 memset (buf, '\0', sizeof (buf));
739 icmp4 = (struct icmp *) buf;
740 data = (char *) (icmp4 + 1);
742 icmp4->icmp_type = ICMP_ECHO;
743 icmp4->icmp_code = 0;
744 icmp4->icmp_cksum = 0;
745 icmp4->icmp_id = htons (ph->ident);
746 icmp4->icmp_seq = htons (ph->sequence);
748 buflen = 4096 - sizeof (struct icmp);
749 strncpy (data, ph->data, buflen);
750 datalen = strlen (data);
752 buflen = datalen + sizeof (struct icmp);
754 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
756 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
758 status = ping_sendto (obj, ph, buf, buflen);
761 perror ("ping_sendto");
765 dprintf ("sendto: status = %i\n", status);
770 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
772 struct icmp6_hdr *icmp6;
781 dprintf ("ph->hostname = %s\n", ph->hostname);
783 memset (buf, '\0', sizeof (buf));
784 icmp6 = (struct icmp6_hdr *) buf;
785 data = (char *) (icmp6 + 1);
787 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
788 icmp6->icmp6_code = 0;
789 /* The checksum will be calculated by the TCP/IP stack. */
791 icmp6->icmp6_cksum = 0;
792 icmp6->icmp6_id = htons (ph->ident);
793 icmp6->icmp6_seq = htons (ph->sequence);
795 buflen = 4096 - sizeof (struct icmp6_hdr);
796 strncpy (data, ph->data, buflen);
797 datalen = strlen (data);
799 buflen = datalen + sizeof (struct icmp6_hdr);
801 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
803 status = ping_sendto (obj, ph, buf, buflen);
806 perror ("ping_sendto");
810 dprintf ("sendto: status = %i\n", status);
815 static int ping_send_all (pingobj_t *obj)
825 for (ptr = ph; ptr != NULL; ptr = ptr->next)
827 /* start timer.. The GNU `ping6' starts the timer before
828 * sending the packet, so I will do that too */
829 if (gettimeofday (ptr->timer, NULL) == -1)
832 char errbuf[PING_ERRMSG_LEN];
833 dprintf ("gettimeofday: %s\n",
834 sstrerror (errno, errbuf, sizeof (errbuf)));
836 timerclear (ptr->timer);
842 dprintf ("timer set for hostname = %s\n", ptr->hostname);
845 if (ptr->addrfamily == AF_INET6)
847 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
848 if (ping_send_one_ipv6 (obj, ptr) != 0)
850 timerclear (ptr->timer);
855 else if (ptr->addrfamily == AF_INET)
857 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
858 if (ping_send_one_ipv4 (obj, ptr) != 0)
860 timerclear (ptr->timer);
865 else /* this should not happen */
867 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
868 timerclear (ptr->timer);
880 * Set the TTL of a socket protocol independently.
882 static int ping_set_ttl (pinghost_t *ph, int ttl)
886 if (ph->addrfamily == AF_INET)
888 dprintf ("Setting TTLv4 to %i\n", ttl);
889 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
892 else if (ph->addrfamily == AF_INET6)
894 dprintf ("Setting TTLv6 to %i\n", ttl);
895 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
902 static int ping_get_ident (void)
905 static int did_seed = 0;
911 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
915 if (read (fd, &seed, sizeof (seed)) != -1)
918 dprintf ("Random seed: %#x\n", seed);
927 char errbuf[PING_ERRMSG_LEN];
928 dprintf ("open (/dev/urandom): %s\n",
929 sstrerror (errno, errbuf, sizeof (errbuf)));
934 retval = (int) random ();
936 dprintf ("Random number: %#x\n", retval);
941 static pinghost_t *ping_alloc (void)
946 ph_size = sizeof (pinghost_t)
947 + sizeof (struct sockaddr_storage)
948 + sizeof (struct timeval);
950 ph = (pinghost_t *) malloc (ph_size);
954 memset (ph, '\0', ph_size);
956 ph->timer = (struct timeval *) (ph + 1);
957 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
959 ph->addrlen = sizeof (struct sockaddr_storage);
963 ph->ident = ping_get_ident () & 0xFFFF;
968 static void ping_free (pinghost_t *ph)
973 if (ph->username != NULL)
976 if (ph->hostname != NULL)
979 if (ph->data != NULL)
988 const char *ping_get_error (pingobj_t *obj)
990 return (obj->errmsg);
993 pingobj_t *ping_construct (void)
997 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
999 memset (obj, '\0', sizeof (pingobj_t));
1001 obj->timeout = PING_DEF_TIMEOUT;
1002 obj->ttl = PING_DEF_TTL;
1003 obj->addrfamily = PING_DEF_AF;
1004 obj->data = strdup (PING_DEF_DATA);
1009 void ping_destroy (pingobj_t *obj)
1011 pinghost_t *current;
1014 current = obj->head;
1017 while (current != NULL)
1019 next = current->next;
1020 ping_free (current);
1024 if (obj->data != NULL)
1027 if (obj->srcaddr != NULL)
1028 free (obj->srcaddr);
1035 int ping_setopt (pingobj_t *obj, int option, void *value)
1041 case PING_OPT_TIMEOUT:
1042 obj->timeout = *((double *) value);
1043 if (obj->timeout < 0.0)
1045 obj->timeout = PING_DEF_TIMEOUT;
1051 obj->ttl = *((int *) value);
1052 if ((obj->ttl < 1) || (obj->ttl > 255))
1054 obj->ttl = PING_DEF_TTL;
1061 for (ph = obj->head; ph != NULL; ph = ph->next)
1062 ping_set_ttl (ph, obj->ttl);
1067 obj->addrfamily = *((int *) value);
1068 if ((obj->addrfamily != AF_UNSPEC)
1069 && (obj->addrfamily != AF_INET)
1070 && (obj->addrfamily != AF_INET6))
1072 obj->addrfamily = PING_DEF_AF;
1075 if (obj->srcaddr != NULL)
1077 free (obj->srcaddr);
1078 obj->srcaddr = NULL;
1083 if (obj->data != NULL)
1088 obj->data = strdup ((const char *) value);
1091 case PING_OPT_SOURCE:
1093 char *hostname = (char *) value;
1094 struct addrinfo ai_hints;
1095 struct addrinfo *ai_list;
1098 if (obj->addrfamily != AF_UNSPEC)
1100 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1103 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1104 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1105 #if defined(AI_ADDRCONFIG)
1106 ai_hints.ai_flags = AI_ADDRCONFIG;
1108 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1111 #if defined(EAI_SYSTEM)
1112 char errbuf[PING_ERRMSG_LEN];
1114 ping_set_error (obj, "getaddrinfo",
1115 #if defined(EAI_SYSTEM)
1116 (status == EAI_SYSTEM)
1117 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1119 gai_strerror (status));
1124 if (ai_list->ai_next != NULL)
1126 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1129 if (obj->srcaddr == NULL)
1131 obj->srcaddrlen = 0;
1132 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1133 if (obj->srcaddr == NULL)
1135 ping_set_errno (obj, errno);
1137 freeaddrinfo (ai_list);
1141 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1142 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1143 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1144 ai_list->ai_addrlen);
1145 obj->srcaddrlen = ai_list->ai_addrlen;
1146 obj->addrfamily = ai_list->ai_family;
1148 freeaddrinfo (ai_list);
1149 } /* case PING_OPT_SOURCE */
1154 } /* switch (option) */
1157 } /* int ping_setopt */
1160 int ping_send (pingobj_t *obj)
1164 if (ping_send_all (obj) < 0)
1167 if ((ret = ping_receive_all (obj)) < 0)
1173 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1177 if (strcasecmp (ph->username, host) == 0)
1186 int ping_host_add (pingobj_t *obj, const char *host)
1190 struct addrinfo ai_hints;
1191 struct addrinfo *ai_list, *ai_ptr;
1194 dprintf ("host = %s\n", host);
1196 if (ping_host_search (obj->head, host) != NULL)
1199 memset (&ai_hints, '\0', sizeof (ai_hints));
1200 ai_hints.ai_flags = 0;
1201 #ifdef AI_ADDRCONFIG
1202 ai_hints.ai_flags |= AI_ADDRCONFIG;
1205 ai_hints.ai_flags |= AI_CANONNAME;
1207 ai_hints.ai_family = obj->addrfamily;
1208 ai_hints.ai_socktype = SOCK_RAW;
1210 if ((ph = ping_alloc ()) == NULL)
1212 dprintf ("Out of memory!\n");
1216 if ((ph->username = strdup (host)) == NULL)
1218 dprintf ("Out of memory!\n");
1219 ping_set_errno (obj, errno);
1224 if ((ph->hostname = strdup (host)) == NULL)
1226 dprintf ("Out of memory!\n");
1227 ping_set_errno (obj, errno);
1232 /* obj->data is not garuanteed to be != NULL */
1233 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1235 dprintf ("Out of memory!\n");
1236 ping_set_errno (obj, errno);
1241 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1243 #if defined(EAI_SYSTEM)
1244 char errbuf[PING_ERRMSG_LEN];
1246 dprintf ("getaddrinfo failed\n");
1247 ping_set_error (obj, "getaddrinfo",
1248 #if defined(EAI_SYSTEM)
1249 (ai_return == EAI_SYSTEM)
1250 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1252 gai_strerror (ai_return));
1257 if (ai_list == NULL)
1258 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1260 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1264 if (ai_ptr->ai_family == AF_INET)
1266 ai_ptr->ai_socktype = SOCK_RAW;
1267 ai_ptr->ai_protocol = IPPROTO_ICMP;
1269 else if (ai_ptr->ai_family == AF_INET6)
1271 ai_ptr->ai_socktype = SOCK_RAW;
1272 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1276 char errmsg[PING_ERRMSG_LEN];
1278 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1279 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1282 ping_set_error (obj, "getaddrinfo", errmsg);
1286 /* TODO: Move this to a static function `ping_open_socket' and
1287 * call it whenever the socket dies. */
1288 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1292 char errbuf[PING_ERRMSG_LEN];
1293 dprintf ("socket: %s\n",
1294 sstrerror (errno, errbuf, sizeof (errbuf)));
1296 ping_set_errno (obj, errno);
1300 if (obj->srcaddr != NULL)
1302 assert (obj->srcaddrlen > 0);
1303 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1305 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1308 char errbuf[PING_ERRMSG_LEN];
1309 dprintf ("bind: %s\n",
1310 sstrerror (errno, errbuf, sizeof (errbuf)));
1312 ping_set_errno (obj, errno);
1319 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1320 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1321 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1322 ph->addrlen = ai_ptr->ai_addrlen;
1323 ph->addrfamily = ai_ptr->ai_family;
1326 if ((ai_ptr->ai_canonname != NULL)
1327 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1331 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1332 ph->hostname, ai_ptr->ai_canonname);
1334 old_hostname = ph->hostname;
1335 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1337 /* strdup failed, falling back to old hostname */
1338 ph->hostname = old_hostname;
1340 else if (old_hostname != NULL)
1342 free (old_hostname);
1345 #endif /* AI_CANONNAME */
1347 if (ph->addrfamily == AF_INET)
1351 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1352 &opt, sizeof (opt));
1354 else if (ph->addrfamily == AF_INET6)
1358 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1359 &opt, sizeof (opt));
1365 freeaddrinfo (ai_list);
1374 * Adding in the front is much easier, but then the iterator will
1375 * return the host that was added last as first host. That's just not
1378 if (obj->head == NULL)
1387 while (hptr->next != NULL)
1390 assert ((hptr != NULL) && (hptr->next == NULL));
1394 ping_set_ttl (ph, obj->ttl);
1397 } /* int ping_host_add */
1399 int ping_host_remove (pingobj_t *obj, const char *host)
1401 pinghost_t *pre, *cur;
1408 if (strcasecmp (host, cur->username) == 0)
1417 ping_set_error (obj, "ping_host_remove", "Host not found");
1422 obj->head = cur->next;
1424 pre->next = cur->next;
1431 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1433 return ((pingobj_iter_t *) obj->head);
1436 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1438 return ((pingobj_iter_t *) iter->next);
1441 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1442 void *buffer, size_t *buffer_len)
1446 size_t orig_buffer_len = *buffer_len;
1450 case PING_INFO_USERNAME:
1452 *buffer_len = strlen (iter->username) + 1;
1453 if (orig_buffer_len <= *buffer_len)
1455 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1456 * will copy `*buffer_len' and pad the rest of
1457 * `buffer' with null-bytes */
1458 strncpy (buffer, iter->username, orig_buffer_len);
1462 case PING_INFO_HOSTNAME:
1464 *buffer_len = strlen (iter->hostname) + 1;
1465 if (orig_buffer_len < *buffer_len)
1467 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1468 * will copy `*buffer_len' and pad the rest of
1469 * `buffer' with null-bytes */
1470 strncpy (buffer, iter->hostname, orig_buffer_len);
1474 case PING_INFO_ADDRESS:
1475 ret = getnameinfo ((struct sockaddr *) iter->addr,
1483 if ((ret == EAI_MEMORY)
1485 || (ret == EAI_OVERFLOW)
1489 #if defined(EAI_SYSTEM)
1490 else if (ret == EAI_SYSTEM)
1498 case PING_INFO_FAMILY:
1500 *buffer_len = sizeof (int);
1501 if (orig_buffer_len < sizeof (int))
1503 *((int *) buffer) = iter->addrfamily;
1507 case PING_INFO_LATENCY:
1509 *buffer_len = sizeof (double);
1510 if (orig_buffer_len < sizeof (double))
1512 *((double *) buffer) = iter->latency;
1516 case PING_INFO_DROPPED:
1518 *buffer_len = sizeof (uint32_t);
1519 if (orig_buffer_len < sizeof (uint32_t))
1521 *((uint32_t *) buffer) = iter->dropped;
1525 case PING_INFO_SEQUENCE:
1527 *buffer_len = sizeof (unsigned int);
1528 if (orig_buffer_len < sizeof (unsigned int))
1530 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1534 case PING_INFO_IDENT:
1536 *buffer_len = sizeof (uint16_t);
1537 if (orig_buffer_len < sizeof (uint16_t))
1539 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1543 case PING_INFO_DATA:
1545 *buffer_len = strlen (iter->data);
1546 if (orig_buffer_len < *buffer_len)
1548 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1552 case PING_INFO_RECV_TTL:
1554 *buffer_len = sizeof (int);
1555 if (orig_buffer_len < sizeof (int))
1557 *((int *) buffer) = iter->recv_ttl;
1563 } /* ping_iterator_get_info */
1565 void *ping_iterator_get_context (pingobj_iter_t *iter)
1567 return (iter->context);
1570 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1572 iter->context = context;