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;
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 */
480 family = ph->addrfamily;
482 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
484 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
486 if (cmsg->cmsg_level == IPPROTO_IP) /* {{{ */
489 if (cmsg->cmsg_type == IP_TTL)
491 memcpy (&recv_ttl, CMSG_DATA (cmsg),
493 dprintf ("TTLv4 = %i;\n", recv_ttl);
497 dprintf ("Not handling option %i.\n",
501 else if (cmsg->cmsg_level == IPPROTO_IPV6) /* {{{ */
504 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
506 memcpy (&recv_ttl, CMSG_DATA (cmsg),
508 dprintf ("TTLv6 = %i;\n", recv_ttl);
512 dprintf ("Not handling option %i.\n",
518 dprintf ("Don't know how to handle "
519 "unknown protocol %i.\n",
522 } /* }}} for (cmsg) */
524 if (family == AF_INET)
526 host = ping_receive_ipv4 (ph, payload_buffer, payload_buffer_len);
530 else if (family == AF_INET6)
532 host = ping_receive_ipv6 (ph, payload_buffer, payload_buffer_len);
538 dprintf ("ping_receive_one: Unknown address family %i.\n",
543 dprintf ("rcvd: %12i.%06i\n",
546 dprintf ("sent: %12i.%06i\n",
547 (int) host->timer->tv_sec,
548 (int) host->timer->tv_usec);
550 if (ping_timeval_sub (now, host->timer, &diff) < 0)
552 timerclear (host->timer);
556 dprintf ("diff: %12i.%06i\n",
561 host->recv_ttl = recv_ttl;
563 host->latency = ((double) diff.tv_usec) / 1000.0;
564 host->latency += ((double) diff.tv_sec) * 1000.0;
566 timerclear (host->timer);
571 static int ping_receive_all (pingobj_t *obj)
580 struct timeval endtime;
581 struct timeval nowtime;
582 struct timeval timeout;
590 for (ptr = ph; ptr != NULL; ptr = ptr->next)
593 if (gettimeofday (&nowtime, NULL) == -1)
595 ping_set_errno (obj, errno);
600 timeout.tv_sec = (time_t) obj->timeout;
601 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
603 dprintf ("Set timeout to %i.%06i seconds\n",
604 (int) timeout.tv_sec,
605 (int) timeout.tv_usec);
607 ping_timeval_add (&nowtime, &timeout, &endtime);
615 for (ptr = ph; ptr != NULL; ptr = ptr->next)
617 if (!timerisset (ptr->timer))
620 FD_SET (ptr->fd, &readfds);
623 if (max_readfds < ptr->fd)
624 max_readfds = ptr->fd;
627 if (num_readfds == 0)
630 if (gettimeofday (&nowtime, NULL) == -1)
632 ping_set_errno (obj, errno);
636 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
639 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
640 (int) timeout.tv_sec,
641 (int) timeout.tv_usec);
643 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
645 if (gettimeofday (&nowtime, NULL) == -1)
647 ping_set_errno (obj, errno);
651 if ((status == -1) && (errno == EINTR))
653 dprintf ("select was interrupted by signal..\n");
659 char errbuf[PING_ERRMSG_LEN];
660 dprintf ("select: %s\n",
661 sstrerror (errno, errbuf, sizeof (errbuf)));
665 else if (status == 0)
667 dprintf ("select timed out\n");
668 for (ptr = ph; ptr != NULL; ptr = ptr->next)
669 if (ptr->latency < 0.0)
674 for (ptr = ph; ptr != NULL; ptr = ptr->next)
676 if (FD_ISSET (ptr->fd, &readfds))
677 if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
685 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
686 * Sending functions: *
689 * +-> ping_send_one_ipv4 *
690 * `-> ping_send_one_ipv6 *
691 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
692 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
693 const void *buf, size_t buflen)
697 if (gettimeofday (ph->timer, NULL) == -1)
699 timerclear (ph->timer);
703 ret = sendto (ph->fd, buf, buflen, 0,
704 (struct sockaddr *) ph->addr, ph->addrlen);
708 #if defined(EHOSTUNREACH)
709 if (errno == EHOSTUNREACH)
712 #if defined(ENETUNREACH)
713 if (errno == ENETUNREACH)
716 ping_set_errno (obj, errno);
722 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
733 dprintf ("ph->hostname = %s\n", ph->hostname);
735 memset (buf, '\0', sizeof (buf));
736 icmp4 = (struct icmp *) buf;
737 data = (char *) (icmp4 + 1);
739 icmp4->icmp_type = ICMP_ECHO;
740 icmp4->icmp_code = 0;
741 icmp4->icmp_cksum = 0;
742 icmp4->icmp_id = htons (ph->ident);
743 icmp4->icmp_seq = htons (ph->sequence);
745 buflen = 4096 - sizeof (struct icmp);
746 strncpy (data, ph->data, buflen);
747 datalen = strlen (data);
749 buflen = datalen + sizeof (struct icmp);
751 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
753 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
755 status = ping_sendto (obj, ph, buf, buflen);
758 perror ("ping_sendto");
762 dprintf ("sendto: status = %i\n", status);
767 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
769 struct icmp6_hdr *icmp6;
778 dprintf ("ph->hostname = %s\n", ph->hostname);
780 memset (buf, '\0', sizeof (buf));
781 icmp6 = (struct icmp6_hdr *) buf;
782 data = (char *) (icmp6 + 1);
784 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
785 icmp6->icmp6_code = 0;
786 /* The checksum will be calculated by the TCP/IP stack. */
788 icmp6->icmp6_cksum = 0;
789 icmp6->icmp6_id = htons (ph->ident);
790 icmp6->icmp6_seq = htons (ph->sequence);
792 buflen = 4096 - sizeof (struct icmp6_hdr);
793 strncpy (data, ph->data, buflen);
794 datalen = strlen (data);
796 buflen = datalen + sizeof (struct icmp6_hdr);
798 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
800 status = ping_sendto (obj, ph, buf, buflen);
803 perror ("ping_sendto");
807 dprintf ("sendto: status = %i\n", status);
812 static int ping_send_all (pingobj_t *obj)
822 for (ptr = ph; ptr != NULL; ptr = ptr->next)
824 /* start timer.. The GNU `ping6' starts the timer before
825 * sending the packet, so I will do that too */
826 if (gettimeofday (ptr->timer, NULL) == -1)
829 char errbuf[PING_ERRMSG_LEN];
830 dprintf ("gettimeofday: %s\n",
831 sstrerror (errno, errbuf, sizeof (errbuf)));
833 timerclear (ptr->timer);
839 dprintf ("timer set for hostname = %s\n", ptr->hostname);
842 if (ptr->addrfamily == AF_INET6)
844 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
845 if (ping_send_one_ipv6 (obj, ptr) != 0)
847 timerclear (ptr->timer);
852 else if (ptr->addrfamily == AF_INET)
854 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
855 if (ping_send_one_ipv4 (obj, ptr) != 0)
857 timerclear (ptr->timer);
862 else /* this should not happen */
864 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
865 timerclear (ptr->timer);
877 * Set the TTL of a socket protocol independently.
879 static int ping_set_ttl (pinghost_t *ph, int ttl)
883 if (ph->addrfamily == AF_INET)
885 dprintf ("Setting TTLv4 to %i\n", ttl);
886 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
889 else if (ph->addrfamily == AF_INET6)
891 dprintf ("Setting TTLv6 to %i\n", ttl);
892 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
899 static int ping_get_ident (void)
902 static int did_seed = 0;
908 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
912 if (read (fd, &seed, sizeof (seed)) != -1)
915 dprintf ("Random seed: %#x\n", seed);
924 char errbuf[PING_ERRMSG_LEN];
925 dprintf ("open (/dev/urandom): %s\n",
926 sstrerror (errno, errbuf, sizeof (errbuf)));
931 retval = (int) random ();
933 dprintf ("Random number: %#x\n", retval);
938 static pinghost_t *ping_alloc (void)
943 ph_size = sizeof (pinghost_t)
944 + sizeof (struct sockaddr_storage)
945 + sizeof (struct timeval);
947 ph = (pinghost_t *) malloc (ph_size);
951 memset (ph, '\0', ph_size);
953 ph->timer = (struct timeval *) (ph + 1);
954 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
956 ph->addrlen = sizeof (struct sockaddr_storage);
960 ph->ident = ping_get_ident () & 0xFFFF;
965 static void ping_free (pinghost_t *ph)
970 if (ph->username != NULL)
973 if (ph->hostname != NULL)
976 if (ph->data != NULL)
985 const char *ping_get_error (pingobj_t *obj)
987 return (obj->errmsg);
990 pingobj_t *ping_construct (void)
994 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
996 memset (obj, '\0', sizeof (pingobj_t));
998 obj->timeout = PING_DEF_TIMEOUT;
999 obj->ttl = PING_DEF_TTL;
1000 obj->addrfamily = PING_DEF_AF;
1001 obj->data = strdup (PING_DEF_DATA);
1006 void ping_destroy (pingobj_t *obj)
1008 pinghost_t *current;
1011 current = obj->head;
1014 while (current != NULL)
1016 next = current->next;
1017 ping_free (current);
1021 if (obj->data != NULL)
1024 if (obj->srcaddr != NULL)
1025 free (obj->srcaddr);
1032 int ping_setopt (pingobj_t *obj, int option, void *value)
1038 case PING_OPT_TIMEOUT:
1039 obj->timeout = *((double *) value);
1040 if (obj->timeout < 0.0)
1042 obj->timeout = PING_DEF_TIMEOUT;
1048 obj->ttl = *((int *) value);
1049 if ((obj->ttl < 1) || (obj->ttl > 255))
1051 obj->ttl = PING_DEF_TTL;
1058 for (ph = obj->head; ph != NULL; ph = ph->next)
1059 ping_set_ttl (ph, obj->ttl);
1064 obj->addrfamily = *((int *) value);
1065 if ((obj->addrfamily != AF_UNSPEC)
1066 && (obj->addrfamily != AF_INET)
1067 && (obj->addrfamily != AF_INET6))
1069 obj->addrfamily = PING_DEF_AF;
1072 if (obj->srcaddr != NULL)
1074 free (obj->srcaddr);
1075 obj->srcaddr = NULL;
1080 if (obj->data != NULL)
1085 obj->data = strdup ((const char *) value);
1088 case PING_OPT_SOURCE:
1090 char *hostname = (char *) value;
1091 struct addrinfo ai_hints;
1092 struct addrinfo *ai_list;
1095 if (obj->addrfamily != AF_UNSPEC)
1097 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1100 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1101 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1102 #if defined(AI_ADDRCONFIG)
1103 ai_hints.ai_flags = AI_ADDRCONFIG;
1105 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1108 #if defined(EAI_SYSTEM)
1109 char errbuf[PING_ERRMSG_LEN];
1111 ping_set_error (obj, "getaddrinfo",
1112 #if defined(EAI_SYSTEM)
1113 (status == EAI_SYSTEM)
1114 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1116 gai_strerror (status));
1121 if (ai_list->ai_next != NULL)
1123 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1126 if (obj->srcaddr == NULL)
1128 obj->srcaddrlen = 0;
1129 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1130 if (obj->srcaddr == NULL)
1132 ping_set_errno (obj, errno);
1134 freeaddrinfo (ai_list);
1138 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1139 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1140 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1141 ai_list->ai_addrlen);
1142 obj->srcaddrlen = ai_list->ai_addrlen;
1143 obj->addrfamily = ai_list->ai_family;
1145 freeaddrinfo (ai_list);
1146 } /* case PING_OPT_SOURCE */
1151 } /* switch (option) */
1154 } /* int ping_setopt */
1157 int ping_send (pingobj_t *obj)
1161 if (ping_send_all (obj) < 0)
1164 if ((ret = ping_receive_all (obj)) < 0)
1170 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1174 if (strcasecmp (ph->username, host) == 0)
1183 int ping_host_add (pingobj_t *obj, const char *host)
1187 struct addrinfo ai_hints;
1188 struct addrinfo *ai_list, *ai_ptr;
1191 dprintf ("host = %s\n", host);
1193 if (ping_host_search (obj->head, host) != NULL)
1196 memset (&ai_hints, '\0', sizeof (ai_hints));
1197 ai_hints.ai_flags = 0;
1198 #ifdef AI_ADDRCONFIG
1199 ai_hints.ai_flags |= AI_ADDRCONFIG;
1202 ai_hints.ai_flags |= AI_CANONNAME;
1204 ai_hints.ai_family = obj->addrfamily;
1205 ai_hints.ai_socktype = SOCK_RAW;
1207 if ((ph = ping_alloc ()) == NULL)
1209 dprintf ("Out of memory!\n");
1213 if ((ph->username = strdup (host)) == NULL)
1215 dprintf ("Out of memory!\n");
1216 ping_set_errno (obj, errno);
1221 if ((ph->hostname = strdup (host)) == NULL)
1223 dprintf ("Out of memory!\n");
1224 ping_set_errno (obj, errno);
1229 /* obj->data is not garuanteed to be != NULL */
1230 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1232 dprintf ("Out of memory!\n");
1233 ping_set_errno (obj, errno);
1238 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1240 #if defined(EAI_SYSTEM)
1241 char errbuf[PING_ERRMSG_LEN];
1243 dprintf ("getaddrinfo failed\n");
1244 ping_set_error (obj, "getaddrinfo",
1245 #if defined(EAI_SYSTEM)
1246 (ai_return == EAI_SYSTEM)
1247 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1249 gai_strerror (ai_return));
1254 if (ai_list == NULL)
1255 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1257 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1261 if (ai_ptr->ai_family == AF_INET)
1263 ai_ptr->ai_socktype = SOCK_RAW;
1264 ai_ptr->ai_protocol = IPPROTO_ICMP;
1266 else if (ai_ptr->ai_family == AF_INET6)
1268 ai_ptr->ai_socktype = SOCK_RAW;
1269 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1273 char errmsg[PING_ERRMSG_LEN];
1275 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1276 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1279 ping_set_error (obj, "getaddrinfo", errmsg);
1283 /* TODO: Move this to a static function `ping_open_socket' and
1284 * call it whenever the socket dies. */
1285 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1289 char errbuf[PING_ERRMSG_LEN];
1290 dprintf ("socket: %s\n",
1291 sstrerror (errno, errbuf, sizeof (errbuf)));
1293 ping_set_errno (obj, errno);
1297 if (obj->srcaddr != NULL)
1299 assert (obj->srcaddrlen > 0);
1300 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1302 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1305 char errbuf[PING_ERRMSG_LEN];
1306 dprintf ("bind: %s\n",
1307 sstrerror (errno, errbuf, sizeof (errbuf)));
1309 ping_set_errno (obj, errno);
1316 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1317 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1318 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1319 ph->addrlen = ai_ptr->ai_addrlen;
1320 ph->addrfamily = ai_ptr->ai_family;
1323 if ((ai_ptr->ai_canonname != NULL)
1324 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1328 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1329 ph->hostname, ai_ptr->ai_canonname);
1331 old_hostname = ph->hostname;
1332 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1334 /* strdup failed, falling back to old hostname */
1335 ph->hostname = old_hostname;
1337 else if (old_hostname != NULL)
1339 free (old_hostname);
1342 #endif /* AI_CANONNAME */
1344 if (ph->addrfamily == AF_INET)
1348 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1349 &opt, sizeof (opt));
1351 else if (ph->addrfamily == AF_INET6)
1355 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1356 &opt, sizeof (opt));
1362 freeaddrinfo (ai_list);
1371 * Adding in the front is much easier, but then the iterator will
1372 * return the host that was added last as first host. That's just not
1375 if (obj->head == NULL)
1384 while (hptr->next != NULL)
1387 assert ((hptr != NULL) && (hptr->next == NULL));
1391 ping_set_ttl (ph, obj->ttl);
1394 } /* int ping_host_add */
1396 int ping_host_remove (pingobj_t *obj, const char *host)
1398 pinghost_t *pre, *cur;
1405 if (strcasecmp (host, cur->username) == 0)
1414 ping_set_error (obj, "ping_host_remove", "Host not found");
1419 obj->head = cur->next;
1421 pre->next = cur->next;
1428 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1430 return ((pingobj_iter_t *) obj->head);
1433 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1435 return ((pingobj_iter_t *) iter->next);
1438 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1439 void *buffer, size_t *buffer_len)
1443 size_t orig_buffer_len = *buffer_len;
1447 case PING_INFO_USERNAME:
1449 *buffer_len = strlen (iter->username) + 1;
1450 if (orig_buffer_len <= *buffer_len)
1452 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1453 * will copy `*buffer_len' and pad the rest of
1454 * `buffer' with null-bytes */
1455 strncpy (buffer, iter->username, orig_buffer_len);
1459 case PING_INFO_HOSTNAME:
1461 *buffer_len = strlen (iter->hostname) + 1;
1462 if (orig_buffer_len < *buffer_len)
1464 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1465 * will copy `*buffer_len' and pad the rest of
1466 * `buffer' with null-bytes */
1467 strncpy (buffer, iter->hostname, orig_buffer_len);
1471 case PING_INFO_ADDRESS:
1472 ret = getnameinfo ((struct sockaddr *) iter->addr,
1480 if ((ret == EAI_MEMORY)
1482 || (ret == EAI_OVERFLOW)
1486 #if defined(EAI_SYSTEM)
1487 else if (ret == EAI_SYSTEM)
1495 case PING_INFO_FAMILY:
1497 *buffer_len = sizeof (int);
1498 if (orig_buffer_len < sizeof (int))
1500 *((int *) buffer) = iter->addrfamily;
1504 case PING_INFO_LATENCY:
1506 *buffer_len = sizeof (double);
1507 if (orig_buffer_len < sizeof (double))
1509 *((double *) buffer) = iter->latency;
1513 case PING_INFO_DROPPED:
1515 *buffer_len = sizeof (uint32_t);
1516 if (orig_buffer_len < sizeof (uint32_t))
1518 *((uint32_t *) buffer) = iter->dropped;
1522 case PING_INFO_SEQUENCE:
1524 *buffer_len = sizeof (unsigned int);
1525 if (orig_buffer_len < sizeof (unsigned int))
1527 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1531 case PING_INFO_IDENT:
1533 *buffer_len = sizeof (uint16_t);
1534 if (orig_buffer_len < sizeof (uint16_t))
1536 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1540 case PING_INFO_DATA:
1542 *buffer_len = strlen (iter->data);
1543 if (orig_buffer_len < *buffer_len)
1545 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1549 case PING_INFO_RECV_TTL:
1551 *buffer_len = sizeof (int);
1552 if (orig_buffer_len < sizeof (int))
1554 *((int *) buffer) = iter->recv_ttl;
1560 } /* ping_iterator_get_info */
1562 void *ping_iterator_get_context (pingobj_iter_t *iter)
1564 return (iter->context);
1567 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1569 iter->context = context;