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 (pingobj_t *obj, char *buffer,
266 struct icmp *icmp_hdr;
270 uint16_t recv_checksum;
271 uint16_t calc_checksum;
278 if (buffer_len < sizeof (struct ip))
281 ip_hdr = (struct ip *) buffer;
282 ip_hdr_len = ip_hdr->ip_hl << 2;
284 if (buffer_len < ip_hdr_len)
287 buffer += ip_hdr_len;
288 buffer_len -= ip_hdr_len;
290 if (buffer_len < sizeof (struct icmp))
293 icmp_hdr = (struct icmp *) buffer;
294 buffer += sizeof (struct icmp);
295 buffer_len -= sizeof (struct icmp);
297 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
299 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
303 recv_checksum = icmp_hdr->icmp_cksum;
304 icmp_hdr->icmp_cksum = 0;
305 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
306 sizeof (struct icmp) + buffer_len);
308 if (recv_checksum != calc_checksum)
310 dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
311 "calculated 0x%04"PRIx16"\n",
312 recv_checksum, calc_checksum);
316 ident = ntohs (icmp_hdr->icmp_id);
317 seq = ntohs (icmp_hdr->icmp_seq);
319 /* We have to iterate over all hosts, since ICMPv4 packets may
320 * be received on any raw v4 socket. */
321 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
323 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
324 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
326 if (ptr->addrfamily != AF_INET)
329 if (!timerisset (ptr->timer))
332 if (ptr->ident != ident)
335 if (((ptr->sequence - 1) & 0xFFFF) != seq)
338 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
340 ptr->hostname, ident, seq);
347 dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
352 ptr->recv_ttl = ip_hdr->ip_ttl;
357 #ifndef ICMP6_ECHO_REQUEST
358 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
359 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
361 # define ICMP6_ECHO_REQUEST 128
365 #ifndef ICMP6_ECHO_REPLY
366 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
367 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
369 # define ICMP6_ECHO_REPLY 129
373 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
376 struct icmp6_hdr *icmp_hdr;
383 if (buffer_len < sizeof (struct icmp6_hdr))
386 icmp_hdr = (struct icmp6_hdr *) buffer;
387 buffer += sizeof (struct icmp);
388 buffer_len -= sizeof (struct icmp);
390 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
392 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
396 if (icmp_hdr->icmp6_code != 0)
398 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
402 ident = ntohs (icmp_hdr->icmp6_id);
403 seq = ntohs (icmp_hdr->icmp6_seq);
405 /* We have to iterate over all hosts, since ICMPv6 packets may
406 * be received on any raw v6 socket. */
407 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
409 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
410 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
412 if (ptr->addrfamily != AF_INET6)
415 if (!timerisset (ptr->timer))
418 if (ptr->ident != ident)
421 if (((ptr->sequence - 1) & 0xFFFF) != seq)
424 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
426 ptr->hostname, ident, seq);
433 dprintf ("No match found for ident = 0x%04"PRIx16", "
441 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
444 /* Note: 'ph' is not necessarily the host object for which we receive a
445 * reply. The right object will be returned by ping_receive_ipv*(). For
446 * now, we can only rely on ph->fd and ph->addrfamily. */
449 pinghost_t *host = NULL;
453 * Set up the receive buffer..
455 struct msghdr msghdr;
456 struct cmsghdr *cmsg;
457 char payload_buffer[4096];
458 ssize_t payload_buffer_len;
459 char control_buffer[4096];
460 struct iovec payload_iovec;
462 memset (&payload_iovec, 0, sizeof (payload_iovec));
463 payload_iovec.iov_base = payload_buffer;
464 payload_iovec.iov_len = sizeof (payload_buffer);
466 memset (&msghdr, 0, sizeof (msghdr));
467 /* unspecified source address */
468 msghdr.msg_name = NULL;
469 msghdr.msg_namelen = 0;
470 /* output buffer vector, see readv(2) */
471 msghdr.msg_iov = &payload_iovec;
472 msghdr.msg_iovlen = 1;
473 /* output buffer for control messages */
474 msghdr.msg_control = control_buffer;
475 msghdr.msg_controllen = sizeof (control_buffer);
476 /* flags; this is an output only field.. */
477 msghdr.msg_flags = 0;
479 msghdr.msg_flags |= MSG_XPG4_2;
482 payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
483 if (payload_buffer_len < 0)
486 char errbuf[PING_ERRMSG_LEN];
487 dprintf ("recvfrom: %s\n",
488 sstrerror (errno, errbuf, sizeof (errbuf)));
492 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
494 /* Iterate over all auxiliary data in msghdr */
496 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
498 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
500 if (ph->addrfamily == AF_INET) /* {{{ */
502 if (cmsg->cmsg_level != IPPROTO_IP)
505 if (cmsg->cmsg_type == IP_TTL)
507 memcpy (&recv_ttl, CMSG_DATA (cmsg),
509 dprintf ("TTLv4 = %i;\n", recv_ttl);
513 dprintf ("Not handling option %i.\n",
517 else if (ph->addrfamily == AF_INET6) /* {{{ */
519 if (cmsg->cmsg_level != IPPROTO_IPV6)
522 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
524 memcpy (&recv_ttl, CMSG_DATA (cmsg),
526 dprintf ("TTLv6 = %i;\n", recv_ttl);
530 dprintf ("Not handling option %i.\n",
536 dprintf ("Don't know how to handle "
537 "unknown protocol %i.\n",
540 } /* }}} for (cmsg) */
542 if (ph->addrfamily == AF_INET)
544 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
548 else if (ph->addrfamily == AF_INET6)
550 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
556 dprintf ("ping_receive_one: Unknown address family %i.\n",
561 dprintf ("rcvd: %12i.%06i\n",
564 dprintf ("sent: %12i.%06i\n",
565 (int) host->timer->tv_sec,
566 (int) host->timer->tv_usec);
568 if (ping_timeval_sub (now, host->timer, &diff) < 0)
570 timerclear (host->timer);
574 dprintf ("diff: %12i.%06i\n",
579 host->recv_ttl = recv_ttl;
581 host->latency = ((double) diff.tv_usec) / 1000.0;
582 host->latency += ((double) diff.tv_sec) * 1000.0;
584 timerclear (host->timer);
589 static int ping_receive_all (pingobj_t *obj)
598 struct timeval endtime;
599 struct timeval nowtime;
600 struct timeval timeout;
608 for (ptr = ph; ptr != NULL; ptr = ptr->next)
614 if (gettimeofday (&nowtime, NULL) == -1)
616 ping_set_errno (obj, errno);
621 timeout.tv_sec = (time_t) obj->timeout;
622 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
624 dprintf ("Set timeout to %i.%06i seconds\n",
625 (int) timeout.tv_sec,
626 (int) timeout.tv_usec);
628 ping_timeval_add (&nowtime, &timeout, &endtime);
636 for (ptr = ph; ptr != NULL; ptr = ptr->next)
638 if (!timerisset (ptr->timer))
641 FD_SET (ptr->fd, &readfds);
644 if (max_readfds < ptr->fd)
645 max_readfds = ptr->fd;
648 if (num_readfds == 0)
651 if (gettimeofday (&nowtime, NULL) == -1)
653 ping_set_errno (obj, errno);
657 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
660 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
661 (int) timeout.tv_sec,
662 (int) timeout.tv_usec);
664 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
666 if (gettimeofday (&nowtime, NULL) == -1)
668 ping_set_errno (obj, errno);
672 if ((status == -1) && (errno == EINTR))
674 dprintf ("select was interrupted by signal..\n");
680 char errbuf[PING_ERRMSG_LEN];
681 dprintf ("select: %s\n",
682 sstrerror (errno, errbuf, sizeof (errbuf)));
686 else if (status == 0)
688 dprintf ("select timed out\n");
689 for (ptr = ph; ptr != NULL; ptr = ptr->next)
690 if (ptr->latency < 0.0)
695 for (ptr = ph; ptr != NULL; ptr = ptr->next)
697 if (FD_ISSET (ptr->fd, &readfds))
698 if (ping_receive_one (obj, ptr, &nowtime) == 0)
706 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
707 * Sending functions: *
710 * +-> ping_send_one_ipv4 *
711 * `-> ping_send_one_ipv6 *
712 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
713 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
714 const void *buf, size_t buflen)
718 if (gettimeofday (ph->timer, NULL) == -1)
720 timerclear (ph->timer);
724 ret = sendto (ph->fd, buf, buflen, 0,
725 (struct sockaddr *) ph->addr, ph->addrlen);
729 #if defined(EHOSTUNREACH)
730 if (errno == EHOSTUNREACH)
733 #if defined(ENETUNREACH)
734 if (errno == ENETUNREACH)
737 ping_set_errno (obj, errno);
743 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
754 dprintf ("ph->hostname = %s\n", ph->hostname);
756 memset (buf, '\0', sizeof (buf));
757 icmp4 = (struct icmp *) buf;
758 data = (char *) (icmp4 + 1);
760 icmp4->icmp_type = ICMP_ECHO;
761 icmp4->icmp_code = 0;
762 icmp4->icmp_cksum = 0;
763 icmp4->icmp_id = htons (ph->ident);
764 icmp4->icmp_seq = htons (ph->sequence);
766 buflen = 4096 - sizeof (struct icmp);
767 strncpy (data, ph->data, buflen);
768 datalen = strlen (data);
770 buflen = datalen + sizeof (struct icmp);
772 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
774 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
776 status = ping_sendto (obj, ph, buf, buflen);
779 perror ("ping_sendto");
783 dprintf ("sendto: status = %i\n", status);
788 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
790 struct icmp6_hdr *icmp6;
799 dprintf ("ph->hostname = %s\n", ph->hostname);
801 memset (buf, '\0', sizeof (buf));
802 icmp6 = (struct icmp6_hdr *) buf;
803 data = (char *) (icmp6 + 1);
805 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
806 icmp6->icmp6_code = 0;
807 /* The checksum will be calculated by the TCP/IP stack. */
809 icmp6->icmp6_cksum = 0;
810 icmp6->icmp6_id = htons (ph->ident);
811 icmp6->icmp6_seq = htons (ph->sequence);
813 buflen = 4096 - sizeof (struct icmp6_hdr);
814 strncpy (data, ph->data, buflen);
815 datalen = strlen (data);
817 buflen = datalen + sizeof (struct icmp6_hdr);
819 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
821 status = ping_sendto (obj, ph, buf, buflen);
824 perror ("ping_sendto");
828 dprintf ("sendto: status = %i\n", status);
833 static int ping_send_all (pingobj_t *obj)
843 for (ptr = ph; ptr != NULL; ptr = ptr->next)
845 /* start timer.. The GNU `ping6' starts the timer before
846 * sending the packet, so I will do that too */
847 if (gettimeofday (ptr->timer, NULL) == -1)
850 char errbuf[PING_ERRMSG_LEN];
851 dprintf ("gettimeofday: %s\n",
852 sstrerror (errno, errbuf, sizeof (errbuf)));
854 timerclear (ptr->timer);
860 dprintf ("timer set for hostname = %s\n", ptr->hostname);
863 if (ptr->addrfamily == AF_INET6)
865 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
866 if (ping_send_one_ipv6 (obj, ptr) != 0)
868 timerclear (ptr->timer);
873 else if (ptr->addrfamily == AF_INET)
875 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
876 if (ping_send_one_ipv4 (obj, ptr) != 0)
878 timerclear (ptr->timer);
883 else /* this should not happen */
885 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
886 timerclear (ptr->timer);
898 * Set the TTL of a socket protocol independently.
900 static int ping_set_ttl (pinghost_t *ph, int ttl)
904 if (ph->addrfamily == AF_INET)
906 dprintf ("Setting TTLv4 to %i\n", ttl);
907 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
910 else if (ph->addrfamily == AF_INET6)
912 dprintf ("Setting TTLv6 to %i\n", ttl);
913 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
920 static int ping_get_ident (void)
923 static int did_seed = 0;
929 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
933 if (read (fd, &seed, sizeof (seed)) != -1)
936 dprintf ("Random seed: %#x\n", seed);
945 char errbuf[PING_ERRMSG_LEN];
946 dprintf ("open (/dev/urandom): %s\n",
947 sstrerror (errno, errbuf, sizeof (errbuf)));
952 retval = (int) random ();
954 dprintf ("Random number: %#x\n", retval);
959 static pinghost_t *ping_alloc (void)
964 ph_size = sizeof (pinghost_t)
965 + sizeof (struct sockaddr_storage)
966 + sizeof (struct timeval);
968 ph = (pinghost_t *) malloc (ph_size);
972 memset (ph, '\0', ph_size);
974 ph->timer = (struct timeval *) (ph + 1);
975 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
977 ph->addrlen = sizeof (struct sockaddr_storage);
981 ph->ident = ping_get_ident () & 0xFFFF;
986 static void ping_free (pinghost_t *ph)
991 if (ph->username != NULL)
994 if (ph->hostname != NULL)
997 if (ph->data != NULL)
1006 const char *ping_get_error (pingobj_t *obj)
1010 return (obj->errmsg);
1013 pingobj_t *ping_construct (void)
1017 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1019 memset (obj, '\0', sizeof (pingobj_t));
1021 obj->timeout = PING_DEF_TIMEOUT;
1022 obj->ttl = PING_DEF_TTL;
1023 obj->addrfamily = PING_DEF_AF;
1024 obj->data = strdup (PING_DEF_DATA);
1029 void ping_destroy (pingobj_t *obj)
1031 pinghost_t *current;
1037 current = obj->head;
1040 while (current != NULL)
1042 next = current->next;
1043 ping_free (current);
1047 if (obj->data != NULL)
1050 if (obj->srcaddr != NULL)
1051 free (obj->srcaddr);
1058 int ping_setopt (pingobj_t *obj, int option, void *value)
1062 if ((obj == NULL) || (value == NULL))
1067 case PING_OPT_TIMEOUT:
1068 obj->timeout = *((double *) value);
1069 if (obj->timeout < 0.0)
1071 obj->timeout = PING_DEF_TIMEOUT;
1077 obj->ttl = *((int *) value);
1078 if ((obj->ttl < 1) || (obj->ttl > 255))
1080 obj->ttl = PING_DEF_TTL;
1087 for (ph = obj->head; ph != NULL; ph = ph->next)
1088 ping_set_ttl (ph, obj->ttl);
1093 obj->addrfamily = *((int *) value);
1094 if ((obj->addrfamily != AF_UNSPEC)
1095 && (obj->addrfamily != AF_INET)
1096 && (obj->addrfamily != AF_INET6))
1098 obj->addrfamily = PING_DEF_AF;
1101 if (obj->srcaddr != NULL)
1103 free (obj->srcaddr);
1104 obj->srcaddr = NULL;
1109 if (obj->data != NULL)
1114 obj->data = strdup ((const char *) value);
1117 case PING_OPT_SOURCE:
1119 char *hostname = (char *) value;
1120 struct addrinfo ai_hints;
1121 struct addrinfo *ai_list;
1124 if (obj->addrfamily != AF_UNSPEC)
1126 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1129 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1130 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1131 #if defined(AI_ADDRCONFIG)
1132 ai_hints.ai_flags = AI_ADDRCONFIG;
1134 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1137 #if defined(EAI_SYSTEM)
1138 char errbuf[PING_ERRMSG_LEN];
1140 ping_set_error (obj, "getaddrinfo",
1141 #if defined(EAI_SYSTEM)
1142 (status == EAI_SYSTEM)
1143 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1145 gai_strerror (status));
1150 if (ai_list->ai_next != NULL)
1152 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1155 if (obj->srcaddr == NULL)
1157 obj->srcaddrlen = 0;
1158 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1159 if (obj->srcaddr == NULL)
1161 ping_set_errno (obj, errno);
1163 freeaddrinfo (ai_list);
1167 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1168 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1169 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1170 ai_list->ai_addrlen);
1171 obj->srcaddrlen = ai_list->ai_addrlen;
1172 obj->addrfamily = ai_list->ai_family;
1174 freeaddrinfo (ai_list);
1175 } /* case PING_OPT_SOURCE */
1180 } /* switch (option) */
1183 } /* int ping_setopt */
1186 int ping_send (pingobj_t *obj)
1193 if (ping_send_all (obj) < 0)
1196 if ((ret = ping_receive_all (obj)) < 0)
1202 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1206 if (strcasecmp (ph->username, host) == 0)
1215 int ping_host_add (pingobj_t *obj, const char *host)
1219 struct addrinfo ai_hints;
1220 struct addrinfo *ai_list, *ai_ptr;
1223 if ((obj == NULL) || (host == NULL))
1226 dprintf ("host = %s\n", host);
1228 if (ping_host_search (obj->head, host) != NULL)
1231 memset (&ai_hints, '\0', sizeof (ai_hints));
1232 ai_hints.ai_flags = 0;
1233 #ifdef AI_ADDRCONFIG
1234 ai_hints.ai_flags |= AI_ADDRCONFIG;
1237 ai_hints.ai_flags |= AI_CANONNAME;
1239 ai_hints.ai_family = obj->addrfamily;
1240 ai_hints.ai_socktype = SOCK_RAW;
1242 if ((ph = ping_alloc ()) == NULL)
1244 dprintf ("Out of memory!\n");
1248 if ((ph->username = strdup (host)) == NULL)
1250 dprintf ("Out of memory!\n");
1251 ping_set_errno (obj, errno);
1256 if ((ph->hostname = strdup (host)) == NULL)
1258 dprintf ("Out of memory!\n");
1259 ping_set_errno (obj, errno);
1264 /* obj->data is not garuanteed to be != NULL */
1265 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1267 dprintf ("Out of memory!\n");
1268 ping_set_errno (obj, errno);
1273 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1275 #if defined(EAI_SYSTEM)
1276 char errbuf[PING_ERRMSG_LEN];
1278 dprintf ("getaddrinfo failed\n");
1279 ping_set_error (obj, "getaddrinfo",
1280 #if defined(EAI_SYSTEM)
1281 (ai_return == EAI_SYSTEM)
1282 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1284 gai_strerror (ai_return));
1289 if (ai_list == NULL)
1290 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1292 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1296 if (ai_ptr->ai_family == AF_INET)
1298 ai_ptr->ai_socktype = SOCK_RAW;
1299 ai_ptr->ai_protocol = IPPROTO_ICMP;
1301 else if (ai_ptr->ai_family == AF_INET6)
1303 ai_ptr->ai_socktype = SOCK_RAW;
1304 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1308 char errmsg[PING_ERRMSG_LEN];
1310 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1311 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1314 ping_set_error (obj, "getaddrinfo", errmsg);
1318 /* TODO: Move this to a static function `ping_open_socket' and
1319 * call it whenever the socket dies. */
1320 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1324 char errbuf[PING_ERRMSG_LEN];
1325 dprintf ("socket: %s\n",
1326 sstrerror (errno, errbuf, sizeof (errbuf)));
1328 ping_set_errno (obj, errno);
1332 if (obj->srcaddr != NULL)
1334 assert (obj->srcaddrlen > 0);
1335 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1337 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1340 char errbuf[PING_ERRMSG_LEN];
1341 dprintf ("bind: %s\n",
1342 sstrerror (errno, errbuf, sizeof (errbuf)));
1344 ping_set_errno (obj, errno);
1351 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1352 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1353 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1354 ph->addrlen = ai_ptr->ai_addrlen;
1355 ph->addrfamily = ai_ptr->ai_family;
1358 if ((ai_ptr->ai_canonname != NULL)
1359 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1363 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1364 ph->hostname, ai_ptr->ai_canonname);
1366 old_hostname = ph->hostname;
1367 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1369 /* strdup failed, falling back to old hostname */
1370 ph->hostname = old_hostname;
1372 else if (old_hostname != NULL)
1374 free (old_hostname);
1377 #endif /* AI_CANONNAME */
1379 if (ph->addrfamily == AF_INET)
1383 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1384 &opt, sizeof (opt));
1386 #if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
1387 else if (ph->addrfamily == AF_INET6)
1391 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1392 &opt, sizeof (opt));
1399 freeaddrinfo (ai_list);
1408 * Adding in the front is much easier, but then the iterator will
1409 * return the host that was added last as first host. That's just not
1412 if (obj->head == NULL)
1421 while (hptr->next != NULL)
1424 assert ((hptr != NULL) && (hptr->next == NULL));
1428 ping_set_ttl (ph, obj->ttl);
1431 } /* int ping_host_add */
1433 int ping_host_remove (pingobj_t *obj, const char *host)
1435 pinghost_t *pre, *cur;
1437 if ((obj == NULL) || (host == NULL))
1445 if (strcasecmp (host, cur->username) == 0)
1454 ping_set_error (obj, "ping_host_remove", "Host not found");
1459 obj->head = cur->next;
1461 pre->next = cur->next;
1468 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1472 return ((pingobj_iter_t *) obj->head);
1475 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1479 return ((pingobj_iter_t *) iter->next);
1482 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1483 void *buffer, size_t *buffer_len)
1487 size_t orig_buffer_len = *buffer_len;
1489 if ((iter == NULL) || (buffer == NULL) || (buffer_len == NULL))
1494 case PING_INFO_USERNAME:
1496 *buffer_len = strlen (iter->username) + 1;
1497 if (orig_buffer_len <= *buffer_len)
1499 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1500 * will copy `*buffer_len' and pad the rest of
1501 * `buffer' with null-bytes */
1502 strncpy (buffer, iter->username, orig_buffer_len);
1506 case PING_INFO_HOSTNAME:
1508 *buffer_len = strlen (iter->hostname) + 1;
1509 if (orig_buffer_len < *buffer_len)
1511 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1512 * will copy `*buffer_len' and pad the rest of
1513 * `buffer' with null-bytes */
1514 strncpy (buffer, iter->hostname, orig_buffer_len);
1518 case PING_INFO_ADDRESS:
1519 ret = getnameinfo ((struct sockaddr *) iter->addr,
1527 if ((ret == EAI_MEMORY)
1529 || (ret == EAI_OVERFLOW)
1533 #if defined(EAI_SYSTEM)
1534 else if (ret == EAI_SYSTEM)
1542 case PING_INFO_FAMILY:
1544 *buffer_len = sizeof (int);
1545 if (orig_buffer_len < sizeof (int))
1547 *((int *) buffer) = iter->addrfamily;
1551 case PING_INFO_LATENCY:
1553 *buffer_len = sizeof (double);
1554 if (orig_buffer_len < sizeof (double))
1556 *((double *) buffer) = iter->latency;
1560 case PING_INFO_DROPPED:
1562 *buffer_len = sizeof (uint32_t);
1563 if (orig_buffer_len < sizeof (uint32_t))
1565 *((uint32_t *) buffer) = iter->dropped;
1569 case PING_INFO_SEQUENCE:
1571 *buffer_len = sizeof (unsigned int);
1572 if (orig_buffer_len < sizeof (unsigned int))
1574 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1578 case PING_INFO_IDENT:
1580 *buffer_len = sizeof (uint16_t);
1581 if (orig_buffer_len < sizeof (uint16_t))
1583 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1587 case PING_INFO_DATA:
1589 *buffer_len = strlen (iter->data);
1590 if (orig_buffer_len < *buffer_len)
1592 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1596 case PING_INFO_RECV_TTL:
1598 *buffer_len = sizeof (int);
1599 if (orig_buffer_len < sizeof (int))
1601 *((int *) buffer) = iter->recv_ttl;
1607 } /* ping_iterator_get_info */
1609 void *ping_iterator_get_context (pingobj_iter_t *iter)
1613 return (iter->context);
1616 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1620 iter->context = context;