2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2011 Florian octo Forster <ff at octo.it>
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by the
7 * Free Software Foundation; either version 2.1 of the License, or (at your
8 * option) any later version.
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, 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;
125 struct pinghost *next;
136 struct sockaddr *srcaddr;
137 socklen_t srcaddrlen;
141 char errmsg[PING_ERRMSG_LEN];
147 * private (static) functions
149 /* Even though Posix requires "strerror_r" to return an "int",
150 * some systems (e.g. the GNU libc) return a "char *" _and_
151 * ignore the second argument ... -tokkee */
152 static char *sstrerror (int errnum, char *buf, size_t buflen)
158 snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
160 /* #endif !HAVE_STRERROR_R */
162 #elif STRERROR_R_CHAR_P
165 temp = strerror_r (errnum, buf, buflen);
168 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
169 strncpy (buf, temp, buflen);
171 strncpy (buf, "strerror_r did not return "
172 "an error message", buflen);
175 /* #endif STRERROR_R_CHAR_P */
178 if (strerror_r (errnum, buf, buflen) != 0)
180 snprintf (buf, buflen, "Error %i (%#x); "
181 "Additionally, strerror_r failed.",
184 #endif /* STRERROR_R_CHAR_P */
189 } /* char *sstrerror */
191 static void ping_set_error (pingobj_t *obj, const char *function,
194 snprintf (obj->errmsg, sizeof (obj->errmsg),
195 "%s: %s", function, message);
196 obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
199 static void ping_set_errno (pingobj_t *obj, int error_number)
201 sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
204 static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
207 res->tv_sec = tv1->tv_sec + tv2->tv_sec;
208 res->tv_usec = tv1->tv_usec + tv2->tv_usec;
210 while (res->tv_usec > 1000000)
212 res->tv_usec -= 1000000;
219 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
222 if ((tv1->tv_sec < tv2->tv_sec)
223 || ((tv1->tv_sec == tv2->tv_sec)
224 && (tv1->tv_usec < tv2->tv_usec)))
227 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
228 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
230 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0)));
232 while (res->tv_usec < 0)
234 res->tv_usec += 1000000;
241 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
248 for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
253 *(char *) &ret = *(char *) ptr;
257 /* Do this twice to get all possible carries.. */
258 sum = (sum >> 16) + (sum & 0xFFFF);
259 sum = (sum >> 16) + (sum & 0xFFFF);
266 static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
270 struct icmp *icmp_hdr;
274 uint16_t recv_checksum;
275 uint16_t calc_checksum;
282 if (buffer_len < sizeof (struct ip))
285 ip_hdr = (struct ip *) buffer;
286 ip_hdr_len = ip_hdr->ip_hl << 2;
288 if (buffer_len < ip_hdr_len)
291 buffer += ip_hdr_len;
292 buffer_len -= ip_hdr_len;
294 if (buffer_len < sizeof (struct icmp))
297 icmp_hdr = (struct icmp *) buffer;
298 buffer += sizeof (struct icmp);
299 buffer_len -= sizeof (struct icmp);
301 if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
303 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
307 recv_checksum = icmp_hdr->icmp_cksum;
308 icmp_hdr->icmp_cksum = 0;
309 calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
310 sizeof (struct icmp) + buffer_len);
312 if (recv_checksum != calc_checksum)
314 dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
315 "calculated 0x%04"PRIx16"\n",
316 recv_checksum, calc_checksum);
320 ident = ntohs (icmp_hdr->icmp_id);
321 seq = ntohs (icmp_hdr->icmp_seq);
323 /* We have to iterate over all hosts, since ICMPv4 packets may
324 * be received on any raw v4 socket. */
325 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
327 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
328 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
330 if (ptr->addrfamily != AF_INET)
333 if (!timerisset (ptr->timer))
336 if (ptr->ident != ident)
339 if (((ptr->sequence - 1) & 0xFFFF) != seq)
342 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
344 ptr->hostname, ident, seq);
351 dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
356 ptr->recv_ttl = (int) ip_hdr->ip_ttl;
357 ptr->recv_qos = (uint8_t) ip_hdr->ip_tos;
362 #ifndef ICMP6_ECHO_REQUEST
363 # ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */
364 # define ICMP6_ECHO_REQUEST ICMP6_ECHO
366 # define ICMP6_ECHO_REQUEST 128
370 #ifndef ICMP6_ECHO_REPLY
371 # ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */
372 # define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY
374 # define ICMP6_ECHO_REPLY 129
378 static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
381 struct icmp6_hdr *icmp_hdr;
388 if (buffer_len < sizeof (struct icmp6_hdr))
391 icmp_hdr = (struct icmp6_hdr *) buffer;
392 buffer += sizeof (struct icmp);
393 buffer_len -= sizeof (struct icmp);
395 if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
397 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
401 if (icmp_hdr->icmp6_code != 0)
403 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
407 ident = ntohs (icmp_hdr->icmp6_id);
408 seq = ntohs (icmp_hdr->icmp6_seq);
410 /* We have to iterate over all hosts, since ICMPv6 packets may
411 * be received on any raw v6 socket. */
412 for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
414 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
415 ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
417 if (ptr->addrfamily != AF_INET6)
420 if (!timerisset (ptr->timer))
423 if (ptr->ident != ident)
426 if (((ptr->sequence - 1) & 0xFFFF) != seq)
429 dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
431 ptr->hostname, ident, seq);
438 dprintf ("No match found for ident = 0x%04"PRIx16", "
446 static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
449 /* Note: 'ph' is not necessarily the host object for which we receive a
450 * reply. The right object will be returned by ping_receive_ipv*(). For
451 * now, we can only rely on ph->fd and ph->addrfamily. */
453 struct timeval diff, pkt_now = *now;
454 pinghost_t *host = NULL;
459 * Set up the receive buffer..
461 struct msghdr msghdr;
462 struct cmsghdr *cmsg;
463 char payload_buffer[4096];
464 ssize_t payload_buffer_len;
465 char control_buffer[4096];
466 struct iovec payload_iovec;
468 memset (&payload_iovec, 0, sizeof (payload_iovec));
469 payload_iovec.iov_base = payload_buffer;
470 payload_iovec.iov_len = sizeof (payload_buffer);
472 memset (&msghdr, 0, sizeof (msghdr));
473 /* unspecified source address */
474 msghdr.msg_name = NULL;
475 msghdr.msg_namelen = 0;
476 /* output buffer vector, see readv(2) */
477 msghdr.msg_iov = &payload_iovec;
478 msghdr.msg_iovlen = 1;
479 /* output buffer for control messages */
480 msghdr.msg_control = control_buffer;
481 msghdr.msg_controllen = sizeof (control_buffer);
482 /* flags; this is an output only field.. */
483 msghdr.msg_flags = 0;
485 msghdr.msg_flags |= MSG_XPG4_2;
488 payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
489 if (payload_buffer_len < 0)
492 char errbuf[PING_ERRMSG_LEN];
493 dprintf ("recvfrom: %s\n",
494 sstrerror (errno, errbuf, sizeof (errbuf)));
498 dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
500 /* Iterate over all auxiliary data in msghdr */
503 for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
505 cmsg = CMSG_NXTHDR (&msghdr, cmsg))
507 if (cmsg->cmsg_level == SOL_SOCKET)
510 if (cmsg->cmsg_type == SO_TIMESTAMP)
511 memcpy (&pkt_now, CMSG_DATA (cmsg), sizeof (pkt_now));
512 #endif /* SO_TIMESTAMP */
514 else if (ph->addrfamily == AF_INET) /* {{{ */
516 if (cmsg->cmsg_level != IPPROTO_IP)
519 if (cmsg->cmsg_type == IP_TOS)
521 memcpy (&recv_qos, CMSG_DATA (cmsg),
523 dprintf ("TOSv4 = 0x%02"PRIx8";\n", recv_qos);
525 if (cmsg->cmsg_type == IP_TTL)
527 memcpy (&recv_ttl, CMSG_DATA (cmsg),
529 dprintf ("TTLv4 = %i;\n", recv_ttl);
533 dprintf ("Not handling option %i.\n",
537 else if (ph->addrfamily == AF_INET6) /* {{{ */
539 if (cmsg->cmsg_level != IPPROTO_IPV6)
542 if (cmsg->cmsg_type == IPV6_TCLASS)
544 memcpy (&recv_qos, CMSG_DATA (cmsg),
546 dprintf ("TOSv6 = 0x%02"PRIx8";\n", recv_qos);
548 if (cmsg->cmsg_type == IPV6_HOPLIMIT)
550 memcpy (&recv_ttl, CMSG_DATA (cmsg),
552 dprintf ("TTLv6 = %i;\n", recv_ttl);
556 dprintf ("Not handling option %i.\n",
562 dprintf ("Don't know how to handle "
563 "unknown protocol %i.\n",
566 } /* }}} for (cmsg) */
568 if (ph->addrfamily == AF_INET)
570 host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
574 else if (ph->addrfamily == AF_INET6)
576 host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
582 dprintf ("ping_receive_one: Unknown address family %i.\n",
587 dprintf ("rcvd: %12i.%06i\n",
588 (int) pkt_now.tv_sec,
589 (int) pkt_now.tv_usec);
590 dprintf ("sent: %12i.%06i\n",
591 (int) host->timer->tv_sec,
592 (int) host->timer->tv_usec);
594 if (ping_timeval_sub (&pkt_now, host->timer, &diff) < 0)
596 timerclear (host->timer);
600 dprintf ("diff: %12i.%06i\n",
605 host->recv_ttl = recv_ttl;
606 host->recv_qos = recv_qos;
608 host->latency = ((double) diff.tv_usec) / 1000.0;
609 host->latency += ((double) diff.tv_sec) * 1000.0;
611 timerclear (host->timer);
616 static int ping_receive_all (pingobj_t *obj)
626 struct timeval endtime;
627 struct timeval nowtime;
628 struct timeval timeout;
636 for (ptr = ph; ptr != NULL; ptr = ptr->next)
642 if (gettimeofday (&nowtime, NULL) == -1)
644 ping_set_errno (obj, errno);
649 timeout.tv_sec = (time_t) obj->timeout;
650 timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
652 dprintf ("Set timeout to %i.%06i seconds\n",
653 (int) timeout.tv_sec,
654 (int) timeout.tv_usec);
656 ping_timeval_add (&nowtime, &timeout, &endtime);
665 for (ptr = ph; ptr != NULL; ptr = ptr->next)
667 if (!timerisset (ptr->timer))
670 FD_SET (ptr->fd, &read_fds);
671 FD_SET (ptr->fd, &err_fds);
674 if (max_fd < ptr->fd)
681 if (gettimeofday (&nowtime, NULL) == -1)
683 ping_set_errno (obj, errno);
687 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
690 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_fds,
691 (int) timeout.tv_sec,
692 (int) timeout.tv_usec);
694 status = select (max_fd + 1, &read_fds, NULL, &err_fds, &timeout);
696 if (gettimeofday (&nowtime, NULL) == -1)
698 ping_set_errno (obj, errno);
702 if ((status == -1) && (errno == EINTR))
704 dprintf ("select was interrupted by signal..\n");
710 char errbuf[PING_ERRMSG_LEN];
711 dprintf ("select: %s\n",
712 sstrerror (errno, errbuf, sizeof (errbuf)));
716 else if (status == 0)
718 dprintf ("select timed out\n");
719 for (ptr = ph; ptr != NULL; ptr = ptr->next)
720 if (ptr->latency < 0.0)
725 for (ptr = ph; ptr != NULL; ptr = ptr->next)
727 if (FD_ISSET (ptr->fd, &read_fds))
729 if (ping_receive_one (obj, ptr, &nowtime) == 0)
732 else if (FD_ISSET (ptr->fd, &err_fds))
734 /* clear the timer in this case so that we
735 * don't run into an endless loop. */
736 /* TODO: Set an error flag in this case. */
737 timerclear (ptr->timer);
745 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
746 * Sending functions: *
749 * +-> ping_send_one_ipv4 *
750 * `-> ping_send_one_ipv6 *
751 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
752 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
753 const void *buf, size_t buflen)
757 if (gettimeofday (ph->timer, NULL) == -1)
759 timerclear (ph->timer);
763 ret = sendto (ph->fd, buf, buflen, 0,
764 (struct sockaddr *) ph->addr, ph->addrlen);
768 #if defined(EHOSTUNREACH)
769 if (errno == EHOSTUNREACH)
772 #if defined(ENETUNREACH)
773 if (errno == ENETUNREACH)
776 ping_set_errno (obj, errno);
782 static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
793 dprintf ("ph->hostname = %s\n", ph->hostname);
795 memset (buf, '\0', sizeof (buf));
796 icmp4 = (struct icmp *) buf;
797 data = (char *) (icmp4 + 1);
799 icmp4->icmp_type = ICMP_ECHO;
800 icmp4->icmp_code = 0;
801 icmp4->icmp_cksum = 0;
802 icmp4->icmp_id = htons (ph->ident);
803 icmp4->icmp_seq = htons (ph->sequence);
805 buflen = 4096 - sizeof (struct icmp);
806 strncpy (data, ph->data, buflen);
807 datalen = strlen (data);
809 buflen = datalen + sizeof (struct icmp);
811 icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
813 dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
815 status = ping_sendto (obj, ph, buf, buflen);
818 perror ("ping_sendto");
822 dprintf ("sendto: status = %i\n", status);
827 static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
829 struct icmp6_hdr *icmp6;
838 dprintf ("ph->hostname = %s\n", ph->hostname);
840 memset (buf, '\0', sizeof (buf));
841 icmp6 = (struct icmp6_hdr *) buf;
842 data = (char *) (icmp6 + 1);
844 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
845 icmp6->icmp6_code = 0;
846 /* The checksum will be calculated by the TCP/IP stack. */
848 icmp6->icmp6_cksum = 0;
849 icmp6->icmp6_id = htons (ph->ident);
850 icmp6->icmp6_seq = htons (ph->sequence);
852 buflen = 4096 - sizeof (struct icmp6_hdr);
853 strncpy (data, ph->data, buflen);
854 datalen = strlen (data);
856 buflen = datalen + sizeof (struct icmp6_hdr);
858 dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
860 status = ping_sendto (obj, ph, buf, buflen);
863 perror ("ping_sendto");
867 dprintf ("sendto: status = %i\n", status);
872 static int ping_send_all (pingobj_t *obj)
882 for (ptr = ph; ptr != NULL; ptr = ptr->next)
884 /* start timer.. The GNU `ping6' starts the timer before
885 * sending the packet, so I will do that too */
886 if (gettimeofday (ptr->timer, NULL) == -1)
889 char errbuf[PING_ERRMSG_LEN];
890 dprintf ("gettimeofday: %s\n",
891 sstrerror (errno, errbuf, sizeof (errbuf)));
893 timerclear (ptr->timer);
899 dprintf ("timer set for hostname = %s\n", ptr->hostname);
902 if (ptr->addrfamily == AF_INET6)
904 dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
905 if (ping_send_one_ipv6 (obj, ptr) != 0)
907 timerclear (ptr->timer);
912 else if (ptr->addrfamily == AF_INET)
914 dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
915 if (ping_send_one_ipv4 (obj, ptr) != 0)
917 timerclear (ptr->timer);
922 else /* this should not happen */
924 dprintf ("Unknown address family: %i\n", ptr->addrfamily);
925 timerclear (ptr->timer);
937 * Set the TTL of a socket protocol independently.
939 static int ping_set_ttl (pinghost_t *ph, int ttl)
943 if (ph->addrfamily == AF_INET)
945 dprintf ("Setting TTLv4 to %i\n", ttl);
946 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
949 else if (ph->addrfamily == AF_INET6)
951 dprintf ("Setting TTLv6 to %i\n", ttl);
952 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
960 * Set the TOS of a socket protocol independently.
962 * Using SOL_SOCKET / SO_PRIORITY might be a protocol independent way to
963 * set this. See socket(7) for details.
965 static int ping_set_qos (pingobj_t *obj, pinghost_t *ph, uint8_t qos)
968 char errbuf[PING_ERRMSG_LEN];
970 if (ph->addrfamily == AF_INET)
972 dprintf ("Setting TP_TOS to %#04"PRIx8"\n", qos);
973 ret = setsockopt (ph->fd, IPPROTO_IP, IP_TOS,
978 ping_set_error (obj, "ping_set_qos",
979 sstrerror (ret, errbuf, sizeof (errbuf)));
980 dprintf ("Setting TP_TOS failed: %s\n", errbuf);
983 else if (ph->addrfamily == AF_INET6)
985 /* IPV6_TCLASS requires an "int". */
988 dprintf ("Setting IPV6_TCLASS to %#04"PRIx8" (%i)\n", qos, tmp);
989 ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_TCLASS,
994 ping_set_error (obj, "ping_set_qos",
995 sstrerror (ret, errbuf, sizeof (errbuf)));
996 dprintf ("Setting IPV6_TCLASS failed: %s\n", errbuf);
1003 static int ping_get_ident (void)
1006 static int did_seed = 0;
1012 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
1016 if (read (fd, &seed, sizeof (seed)) != -1)
1019 dprintf ("Random seed: %#x\n", seed);
1028 char errbuf[PING_ERRMSG_LEN];
1029 dprintf ("open (/dev/urandom): %s\n",
1030 sstrerror (errno, errbuf, sizeof (errbuf)));
1035 retval = (int) random ();
1037 dprintf ("Random number: %#x\n", retval);
1042 static pinghost_t *ping_alloc (void)
1047 ph_size = sizeof (pinghost_t)
1048 + sizeof (struct sockaddr_storage)
1049 + sizeof (struct timeval);
1051 ph = (pinghost_t *) malloc (ph_size);
1055 memset (ph, '\0', ph_size);
1057 ph->timer = (struct timeval *) (ph + 1);
1058 ph->addr = (struct sockaddr_storage *) (ph->timer + 1);
1060 ph->addrlen = sizeof (struct sockaddr_storage);
1064 ph->ident = ping_get_ident () & 0xFFFF;
1069 static void ping_free (pinghost_t *ph)
1074 if (ph->username != NULL)
1075 free (ph->username);
1077 if (ph->hostname != NULL)
1078 free (ph->hostname);
1080 if (ph->data != NULL)
1089 const char *ping_get_error (pingobj_t *obj)
1093 return (obj->errmsg);
1096 pingobj_t *ping_construct (void)
1100 if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
1102 memset (obj, 0, sizeof (pingobj_t));
1104 obj->timeout = PING_DEF_TIMEOUT;
1105 obj->ttl = PING_DEF_TTL;
1106 obj->addrfamily = PING_DEF_AF;
1107 obj->data = strdup (PING_DEF_DATA);
1113 void ping_destroy (pingobj_t *obj)
1115 pinghost_t *current;
1121 current = obj->head;
1124 while (current != NULL)
1126 next = current->next;
1127 ping_free (current);
1131 if (obj->data != NULL)
1134 if (obj->srcaddr != NULL)
1135 free (obj->srcaddr);
1137 if (obj->device != NULL)
1145 int ping_setopt (pingobj_t *obj, int option, void *value)
1149 if ((obj == NULL) || (value == NULL))
1158 obj->qos = *((uint8_t *) value);
1159 for (ph = obj->head; ph != NULL; ph = ph->next)
1160 ping_set_qos (obj, ph, obj->qos);
1164 case PING_OPT_TIMEOUT:
1165 obj->timeout = *((double *) value);
1166 if (obj->timeout < 0.0)
1168 obj->timeout = PING_DEF_TIMEOUT;
1174 obj->ttl = *((int *) value);
1175 if ((obj->ttl < 1) || (obj->ttl > 255))
1177 obj->ttl = PING_DEF_TTL;
1184 for (ph = obj->head; ph != NULL; ph = ph->next)
1185 ping_set_ttl (ph, obj->ttl);
1190 obj->addrfamily = *((int *) value);
1191 if ((obj->addrfamily != AF_UNSPEC)
1192 && (obj->addrfamily != AF_INET)
1193 && (obj->addrfamily != AF_INET6))
1195 obj->addrfamily = PING_DEF_AF;
1198 if (obj->srcaddr != NULL)
1200 free (obj->srcaddr);
1201 obj->srcaddr = NULL;
1206 if (obj->data != NULL)
1211 obj->data = strdup ((const char *) value);
1214 case PING_OPT_SOURCE:
1216 char *hostname = (char *) value;
1217 struct addrinfo ai_hints;
1218 struct addrinfo *ai_list;
1221 if (obj->addrfamily != AF_UNSPEC)
1223 dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
1226 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
1227 ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
1228 #if defined(AI_ADDRCONFIG)
1229 ai_hints.ai_flags = AI_ADDRCONFIG;
1231 status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
1234 #if defined(EAI_SYSTEM)
1235 char errbuf[PING_ERRMSG_LEN];
1237 ping_set_error (obj, "getaddrinfo",
1238 #if defined(EAI_SYSTEM)
1239 (status == EAI_SYSTEM)
1240 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1242 gai_strerror (status));
1247 if (ai_list->ai_next != NULL)
1249 dprintf ("hostname = `%s' is ambiguous.\n", hostname);
1252 if (obj->srcaddr == NULL)
1254 obj->srcaddrlen = 0;
1255 obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
1256 if (obj->srcaddr == NULL)
1258 ping_set_errno (obj, errno);
1260 freeaddrinfo (ai_list);
1264 memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
1265 assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
1266 memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
1267 ai_list->ai_addrlen);
1268 obj->srcaddrlen = ai_list->ai_addrlen;
1269 obj->addrfamily = ai_list->ai_family;
1271 freeaddrinfo (ai_list);
1272 } /* case PING_OPT_SOURCE */
1275 case PING_OPT_DEVICE:
1277 #ifdef SO_BINDTODEVICE
1278 char *device = strdup ((char *) value);
1282 ping_set_errno (obj, errno);
1287 if (obj->device != NULL)
1289 obj->device = device;
1290 #else /* ! SO_BINDTODEVICE */
1291 ping_set_errno (obj, ENOTSUP);
1293 #endif /* ! SO_BINDTODEVICE */
1294 } /* case PING_OPT_DEVICE */
1299 } /* switch (option) */
1302 } /* int ping_setopt */
1305 int ping_send (pingobj_t *obj)
1312 if (ping_send_all (obj) < 0)
1315 if ((ret = ping_receive_all (obj)) < 0)
1321 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
1325 if (strcasecmp (ph->username, host) == 0)
1334 int ping_host_add (pingobj_t *obj, const char *host)
1338 struct addrinfo ai_hints;
1339 struct addrinfo *ai_list, *ai_ptr;
1342 if ((obj == NULL) || (host == NULL))
1345 dprintf ("host = %s\n", host);
1347 if (ping_host_search (obj->head, host) != NULL)
1350 memset (&ai_hints, '\0', sizeof (ai_hints));
1351 ai_hints.ai_flags = 0;
1352 #ifdef AI_ADDRCONFIG
1353 ai_hints.ai_flags |= AI_ADDRCONFIG;
1356 ai_hints.ai_flags |= AI_CANONNAME;
1358 ai_hints.ai_family = obj->addrfamily;
1359 ai_hints.ai_socktype = SOCK_RAW;
1361 if ((ph = ping_alloc ()) == NULL)
1363 dprintf ("Out of memory!\n");
1367 if ((ph->username = strdup (host)) == NULL)
1369 dprintf ("Out of memory!\n");
1370 ping_set_errno (obj, errno);
1375 if ((ph->hostname = strdup (host)) == NULL)
1377 dprintf ("Out of memory!\n");
1378 ping_set_errno (obj, errno);
1383 /* obj->data is not garuanteed to be != NULL */
1384 if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
1386 dprintf ("Out of memory!\n");
1387 ping_set_errno (obj, errno);
1392 if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
1394 #if defined(EAI_SYSTEM)
1395 char errbuf[PING_ERRMSG_LEN];
1397 dprintf ("getaddrinfo failed\n");
1398 ping_set_error (obj, "getaddrinfo",
1399 #if defined(EAI_SYSTEM)
1400 (ai_return == EAI_SYSTEM)
1401 ? sstrerror (errno, errbuf, sizeof (errbuf)) :
1403 gai_strerror (ai_return));
1408 if (ai_list == NULL)
1409 ping_set_error (obj, "getaddrinfo", "No hosts returned");
1411 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1415 if (ai_ptr->ai_family == AF_INET)
1417 ai_ptr->ai_socktype = SOCK_RAW;
1418 ai_ptr->ai_protocol = IPPROTO_ICMP;
1420 else if (ai_ptr->ai_family == AF_INET6)
1422 ai_ptr->ai_socktype = SOCK_RAW;
1423 ai_ptr->ai_protocol = IPPROTO_ICMPV6;
1427 char errmsg[PING_ERRMSG_LEN];
1429 snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
1430 errmsg[PING_ERRMSG_LEN - 1] = '\0';
1432 dprintf ("%s", errmsg);
1433 ping_set_error (obj, "getaddrinfo", errmsg);
1437 /* TODO: Move this to a static function `ping_open_socket' and
1438 * call it whenever the socket dies. */
1439 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
1443 char errbuf[PING_ERRMSG_LEN];
1444 dprintf ("socket: %s\n",
1445 sstrerror (errno, errbuf, sizeof (errbuf)));
1447 ping_set_errno (obj, errno);
1451 if (obj->srcaddr != NULL)
1453 assert (obj->srcaddrlen > 0);
1454 assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
1456 if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
1459 char errbuf[PING_ERRMSG_LEN];
1460 dprintf ("bind: %s\n",
1461 sstrerror (errno, errbuf, sizeof (errbuf)));
1463 ping_set_errno (obj, errno);
1470 #ifdef SO_BINDTODEVICE
1471 if (obj->device != NULL)
1473 if (setsockopt (ph->fd, SOL_SOCKET, SO_BINDTODEVICE,
1474 obj->device, strlen (obj->device) + 1) != 0)
1477 char errbuf[PING_ERRMSG_LEN];
1478 dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
1479 sstrerror (errno, errbuf, sizeof (errbuf)));
1481 ping_set_errno (obj, errno);
1487 #endif /* SO_BINDTODEVICE */
1494 status = setsockopt (ph->fd,
1495 SOL_SOCKET, SO_TIMESTAMP,
1496 &opt, sizeof (opt));
1500 char errbuf[PING_ERRMSG_LEN];
1501 dprintf ("setsockopt (SO_TIMESTAMP): %s\n",
1502 sstrerror (errno, errbuf, sizeof (errbuf)));
1504 ping_set_errno (obj, errno);
1510 #endif /* SO_TIMESTAMP */
1511 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1512 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
1513 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1514 ph->addrlen = ai_ptr->ai_addrlen;
1515 ph->addrfamily = ai_ptr->ai_family;
1518 if ((ai_ptr->ai_canonname != NULL)
1519 && (strcmp (ph->hostname, ai_ptr->ai_canonname) != 0))
1523 dprintf ("ph->hostname = %s; ai_ptr->ai_canonname = %s;\n",
1524 ph->hostname, ai_ptr->ai_canonname);
1526 old_hostname = ph->hostname;
1527 if ((ph->hostname = strdup (ai_ptr->ai_canonname)) == NULL)
1529 /* strdup failed, falling back to old hostname */
1530 ph->hostname = old_hostname;
1532 else if (old_hostname != NULL)
1534 free (old_hostname);
1537 #endif /* AI_CANONNAME */
1539 if (ph->addrfamily == AF_INET)
1543 /* Enable receiving the TOS field */
1545 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
1546 &opt, sizeof (opt));
1548 /* Enable receiving the TTL field */
1550 setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
1551 &opt, sizeof (opt));
1553 #if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
1554 else if (ph->addrfamily == AF_INET6)
1558 # if defined(IPV6_RECVHOPLIMIT)
1559 /* For details see RFC 3542, section 6.3. */
1561 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1562 &opt, sizeof (opt));
1563 # endif /* IPV6_RECVHOPLIMIT */
1565 # if defined(IPV6_RECVTCLASS)
1566 /* For details see RFC 3542, section 6.5. */
1568 setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
1569 &opt, sizeof (opt));
1570 # endif /* IPV6_RECVTCLASS */
1572 #endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
1575 } /* for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
1577 freeaddrinfo (ai_list);
1586 * Adding in the front is much easier, but then the iterator will
1587 * return the host that was added last as first host. That's just not
1590 if (obj->head == NULL)
1599 while (hptr->next != NULL)
1602 assert ((hptr != NULL) && (hptr->next == NULL));
1606 ping_set_ttl (ph, obj->ttl);
1607 ping_set_qos (obj, ph, obj->qos);
1610 } /* int ping_host_add */
1612 int ping_host_remove (pingobj_t *obj, const char *host)
1614 pinghost_t *pre, *cur;
1616 if ((obj == NULL) || (host == NULL))
1624 if (strcasecmp (host, cur->username) == 0)
1633 ping_set_error (obj, "ping_host_remove", "Host not found");
1638 obj->head = cur->next;
1640 pre->next = cur->next;
1647 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
1651 return ((pingobj_iter_t *) obj->head);
1654 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
1658 return ((pingobj_iter_t *) iter->next);
1661 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
1662 void *buffer, size_t *buffer_len)
1666 size_t orig_buffer_len = *buffer_len;
1668 if ((iter == NULL) || (buffer_len == NULL))
1671 if ((buffer == NULL) && (*buffer_len != 0 ))
1676 case PING_INFO_USERNAME:
1678 *buffer_len = strlen (iter->username) + 1;
1679 if (orig_buffer_len <= *buffer_len)
1681 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1682 * will copy `*buffer_len' and pad the rest of
1683 * `buffer' with null-bytes */
1684 strncpy (buffer, iter->username, orig_buffer_len);
1688 case PING_INFO_HOSTNAME:
1690 *buffer_len = strlen (iter->hostname) + 1;
1691 if (orig_buffer_len < *buffer_len)
1693 /* Since (orig_buffer_len > *buffer_len) `strncpy'
1694 * will copy `*buffer_len' and pad the rest of
1695 * `buffer' with null-bytes */
1696 strncpy (buffer, iter->hostname, orig_buffer_len);
1700 case PING_INFO_ADDRESS:
1701 ret = getnameinfo ((struct sockaddr *) iter->addr,
1709 if ((ret == EAI_MEMORY)
1711 || (ret == EAI_OVERFLOW)
1715 #if defined(EAI_SYSTEM)
1716 else if (ret == EAI_SYSTEM)
1724 case PING_INFO_FAMILY:
1726 *buffer_len = sizeof (int);
1727 if (orig_buffer_len < sizeof (int))
1729 *((int *) buffer) = iter->addrfamily;
1733 case PING_INFO_LATENCY:
1735 *buffer_len = sizeof (double);
1736 if (orig_buffer_len < sizeof (double))
1738 *((double *) buffer) = iter->latency;
1742 case PING_INFO_DROPPED:
1744 *buffer_len = sizeof (uint32_t);
1745 if (orig_buffer_len < sizeof (uint32_t))
1747 *((uint32_t *) buffer) = iter->dropped;
1751 case PING_INFO_SEQUENCE:
1753 *buffer_len = sizeof (unsigned int);
1754 if (orig_buffer_len < sizeof (unsigned int))
1756 *((unsigned int *) buffer) = (unsigned int) iter->sequence;
1760 case PING_INFO_IDENT:
1762 *buffer_len = sizeof (uint16_t);
1763 if (orig_buffer_len < sizeof (uint16_t))
1765 *((uint16_t *) buffer) = (uint16_t) iter->ident;
1769 case PING_INFO_DATA:
1771 *buffer_len = strlen (iter->data);
1772 if (orig_buffer_len < *buffer_len)
1774 strncpy ((char *) buffer, iter->data, orig_buffer_len);
1778 case PING_INFO_RECV_TTL:
1780 *buffer_len = sizeof (int);
1781 if (orig_buffer_len < sizeof (int))
1783 *((int *) buffer) = iter->recv_ttl;
1787 case PING_INFO_RECV_QOS:
1789 if (*buffer_len>sizeof(unsigned)) *buffer_len=sizeof(unsigned);
1790 if (!*buffer_len) *buffer_len=1;
1791 if (orig_buffer_len < *buffer_len)
1793 memcpy(buffer,&iter->recv_qos,*buffer_len);
1799 } /* ping_iterator_get_info */
1801 void *ping_iterator_get_context (pingobj_iter_t *iter)
1805 return (iter->context);
1808 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
1812 iter->context = context;