Code

src/liboping.c: Performance improvements.
[liboping.git] / src / liboping.c
index c64060c0ba2ad7ef27bd1e30a13c43e2ef88e616..776f24f09e2af9f6b25ee248f759a76514ce3867 100644 (file)
@@ -1,20 +1,20 @@
 /**
  * Object oriented C module to send ICMP and ICMPv6 `echo's.
- * Copyright (C) 2006-2009  Florian octo Forster <octo at verplant.org>
+ * Copyright (C) 2006-2011  Florian octo Forster <ff at octo.it>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; only version 2 of the License is
- * applicable.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
 #if HAVE_CONFIG_H
@@ -25,7 +25,6 @@
 # include <stdlib.h>
 # include <stdio.h>
 # include <string.h>
-# include <stdint.h>
 # include <inttypes.h>
 # include <errno.h>
 # include <assert.h>
 # error "You don't have the standard C99 header files installed"
 #endif /* STDC_HEADERS */
 
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
+
 #if HAVE_UNISTD_H
 # include <unistd.h>
 #endif
@@ -61,6 +64,7 @@
 #if HAVE_SYS_SOCKET_H
 # include <sys/socket.h>
 #endif
+
 #if HAVE_NETDB_H
 # include <netdb.h>
 #endif
 #endif
 
 #define PING_ERRMSG_LEN 256
+#define PING_TABLE_LEN 5381
 
 struct pinghost
 {
@@ -106,17 +111,19 @@ struct pinghost
        struct sockaddr_storage *addr;
        socklen_t                addrlen;
        int                      addrfamily;
-       int                      fd;
        int                      ident;
        int                      sequence;
        struct timeval          *timer;
        double                   latency;
        uint32_t                 dropped;
+       int                      recv_ttl;
+       uint8_t                  recv_qos;
        char                    *data;
 
        void                    *context;
 
        struct pinghost         *next;
+       struct pinghost         *table_next;
 };
 
 struct pingobj
@@ -124,14 +131,21 @@ struct pingobj
        double                   timeout;
        int                      ttl;
        int                      addrfamily;
+       uint8_t                  qos;
        char                    *data;
 
-       struct sockaddr_storage *srcaddr;
+       int                      fd4;
+       int                      fd6;
+
+       struct sockaddr         *srcaddr;
        socklen_t                srcaddrlen;
 
+       char                    *device;
+
        char                     errmsg[PING_ERRMSG_LEN];
 
        pinghost_t              *head;
+       pinghost_t              *table[PING_TABLE_LEN];
 };
 
 /*
@@ -140,7 +154,7 @@ struct pingobj
 /* Even though Posix requires "strerror_r" to return an "int",
  * some systems (e.g. the GNU libc) return a "char *" _and_
  * ignore the second argument ... -tokkee */
-char *sstrerror (int errnum, char *buf, size_t buflen)
+static char *sstrerror (int errnum, char *buf, size_t buflen)
 {
        buf[0] = 0;
 
@@ -187,8 +201,7 @@ static void ping_set_error (pingobj_t *obj, const char *function,
        obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
 }
 
-static void ping_set_errno (pingobj_t *obj, const char *function,
-               int error_number)
+static void ping_set_errno (pingobj_t *obj, int error_number)
 {
        sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
 }
@@ -255,7 +268,8 @@ static uint16_t ping_icmp4_checksum (char *buf, size_t len)
        return (ret);
 }
 
-static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
+static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
+               size_t buffer_len)
 {
        struct ip *ip_hdr;
        struct icmp *icmp_hdr;
@@ -302,7 +316,8 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
 
        if (recv_checksum != calc_checksum)
        {
-               dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
+               dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
+                               "calculated 0x%04"PRIx16"\n",
                                recv_checksum, calc_checksum);
                return (NULL);
        }
@@ -310,7 +325,8 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
        ident = ntohs (icmp_hdr->icmp_id);
        seq   = ntohs (icmp_hdr->icmp_seq);
 
-       for (ptr = ph; ptr != NULL; ptr = ptr->next)
+       for (ptr = obj->table[ident % PING_TABLE_LEN];
+                       ptr != NULL; ptr = ptr->table_next)
        {
                dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
                                ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
@@ -327,7 +343,8 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
                if (((ptr->sequence - 1) & 0xFFFF) != seq)
                        continue;
 
-               dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
+               dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
+                               "seq = %"PRIu16"\n",
                                ptr->hostname, ident, seq);
 
                break;
@@ -335,10 +352,14 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
 
        if (ptr == NULL)
        {
-               dprintf ("No match found for ident = 0x%04x, seq = %i\n",
+               dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
                                ident, seq);
        }
 
+       if (ptr != NULL){
+               ptr->recv_ttl = (int)     ip_hdr->ip_ttl;
+               ptr->recv_qos = (uint8_t) ip_hdr->ip_tos;
+       }
        return (ptr);
 }
 
@@ -358,7 +379,8 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
 # endif
 #endif
 
-static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
+static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
+               size_t buffer_len)
 {
        struct icmp6_hdr *icmp_hdr;
 
@@ -389,7 +411,9 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
        ident = ntohs (icmp_hdr->icmp6_id);
        seq   = ntohs (icmp_hdr->icmp6_seq);
 
-       for (ptr = ph; ptr != NULL; ptr = ptr->next)
+       /* We have to iterate over all hosts, since ICMPv6 packets may
+        * be received on any raw v6 socket. */
+       for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
        {
                dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
                                ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
@@ -406,7 +430,8 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
                if (((ptr->sequence - 1) & 0xFFFF) != seq)
                        continue;
 
-               dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
+               dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
+                               "seq = %"PRIu16"\n",
                                ptr->hostname, ident, seq);
 
                break;
@@ -414,30 +439,54 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
 
        if (ptr == NULL)
        {
-               dprintf ("No match found for ident = 0x%04x, seq = %i\n",
+               dprintf ("No match found for ident = 0x%04"PRIx16", "
+                               "seq = %"PRIu16"\n",
                                ident, seq);
        }
 
        return (ptr);
 }
 
-static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
+static int ping_receive_one (pingobj_t *obj, struct timeval *now, int addrfam)
 {
-       char   buffer[4096];
-       ssize_t buffer_len;
-
-       struct timeval diff;
-
+       int fd = addrfam == AF_INET6 ? obj->fd6 : obj->fd4;
+       struct timeval diff, pkt_now = *now;
        pinghost_t *host = NULL;
+       int recv_ttl;
+       uint8_t recv_qos;
+       
+       /*
+        * Set up the receive buffer..
+        */
+       struct msghdr msghdr;
+       struct cmsghdr *cmsg;
+       char payload_buffer[4096];
+       ssize_t payload_buffer_len;
+       char control_buffer[4096];
+       struct iovec payload_iovec;
+
+       memset (&payload_iovec, 0, sizeof (payload_iovec));
+       payload_iovec.iov_base = payload_buffer;
+       payload_iovec.iov_len = sizeof (payload_buffer);
+
+       memset (&msghdr, 0, sizeof (msghdr));
+       /* unspecified source address */
+       msghdr.msg_name = NULL;
+       msghdr.msg_namelen = 0;
+       /* output buffer vector, see readv(2) */
+       msghdr.msg_iov = &payload_iovec;
+       msghdr.msg_iovlen = 1;
+       /* output buffer for control messages */
+       msghdr.msg_control = control_buffer;
+       msghdr.msg_controllen = sizeof (control_buffer);
+       /* flags; this is an output only field.. */
+       msghdr.msg_flags = 0;
+#ifdef MSG_XPG4_2
+       msghdr.msg_flags |= MSG_XPG4_2;
+#endif
 
-       struct sockaddr_storage sa;
-       socklen_t               sa_len;
-
-       sa_len = sizeof (sa);
-
-       buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
-                       (struct sockaddr *) &sa, &sa_len);
-       if (buffer_len < 0)
+       payload_buffer_len = recvmsg (fd, &msghdr, /* flags = */ 0);
+       if (payload_buffer_len < 0)
        {
 #if WITH_DEBUG
                char errbuf[PING_ERRMSG_LEN];
@@ -446,28 +495,103 @@ static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
 #endif
                return (-1);
        }
+       dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, fd);
+
+       /* Iterate over all auxiliary data in msghdr */
+       recv_ttl = -1;
+       recv_qos = 0;
+       for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
+                       cmsg != NULL;
+                       cmsg = CMSG_NXTHDR (&msghdr, cmsg))
+       {
+               if (cmsg->cmsg_level == SOL_SOCKET)
+               {
+#ifdef SO_TIMESTAMP
+                       if (cmsg->cmsg_type == SO_TIMESTAMP)
+                               memcpy (&pkt_now, CMSG_DATA (cmsg), sizeof (pkt_now));
+#endif /* SO_TIMESTAMP */
+               }
+               else if (addrfam == AF_INET) /* {{{ */
+               {
+                       if (cmsg->cmsg_level != IPPROTO_IP)
+                               continue;
+
+                       if (cmsg->cmsg_type == IP_TOS)
+                       {
+                               memcpy (&recv_qos, CMSG_DATA (cmsg),
+                                               sizeof (recv_qos));
+                               dprintf ("TOSv4 = 0x%02"PRIx8";\n", recv_qos);
+                       } else
+                       if (cmsg->cmsg_type == IP_TTL)
+                       {
+                               memcpy (&recv_ttl, CMSG_DATA (cmsg),
+                                               sizeof (recv_ttl));
+                               dprintf ("TTLv4 = %i;\n", recv_ttl);
+                       }
+                       else
+                       {
+                               dprintf ("Not handling option %i.\n",
+                                               cmsg->cmsg_type);
+                       }
+               } /* }}} */
+               else if (addrfam == AF_INET6) /* {{{ */
+               {
+                       if (cmsg->cmsg_level != IPPROTO_IPV6)
+                               continue;
 
-       dprintf ("Read %zi bytes from fd = %i\n", buffer_len, fd);
+                       if (cmsg->cmsg_type == IPV6_TCLASS)
+                       {
+                               memcpy (&recv_qos, CMSG_DATA (cmsg),
+                                               sizeof (recv_qos));
+                               dprintf ("TOSv6 = 0x%02"PRIx8";\n", recv_qos);
+                       } else
+                       if (cmsg->cmsg_type == IPV6_HOPLIMIT)
+                       {
+                               memcpy (&recv_ttl, CMSG_DATA (cmsg),
+                                               sizeof (recv_ttl));
+                               dprintf ("TTLv6 = %i;\n", recv_ttl);
+                       }
+                       else
+                       {
+                               dprintf ("Not handling option %i.\n",
+                                               cmsg->cmsg_type);
+                       }
+               } /* }}} */
+               else
+               {
+                       dprintf ("Don't know how to handle "
+                                       "unknown protocol %i.\n",
+                                       cmsg->cmsg_level);
+               }
+       } /* }}} for (cmsg) */
 
-       if (sa.ss_family == AF_INET)
+       if (addrfam == AF_INET)
        {
-               if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
+               host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
+               if (host == NULL)
                        return (-1);
        }
-       else if (sa.ss_family == AF_INET6)
+       else if (addrfam == AF_INET6)
        {
-               if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
+               host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
+               if (host == NULL)
                        return (-1);
        }
+       else
+       {
+               dprintf ("ping_receive_one: Unknown address family %i.\n",
+                               addrfam);
+               return (-1);
+       }
 
        dprintf ("rcvd: %12i.%06i\n",
-                       (int) now->tv_sec,
-                       (int) now->tv_usec);
+                       (int) pkt_now.tv_sec,
+                       (int) pkt_now.tv_usec);
        dprintf ("sent: %12i.%06i\n",
                        (int) host->timer->tv_sec,
                        (int) host->timer->tv_usec);
 
-       if (ping_timeval_sub (now, host->timer, &diff) < 0)
+       if (ping_timeval_sub (&pkt_now, host->timer, &diff) < 0)
        {
                timerclear (host->timer);
                return (-1);
@@ -477,6 +601,10 @@ static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
                        (int) diff.tv_sec,
                        (int) diff.tv_usec);
 
+       if (recv_ttl >= 0)
+               host->recv_ttl = recv_ttl;
+       host->recv_qos = recv_qos;
+
        host->latency  = ((double) diff.tv_usec) / 1000.0;
        host->latency += ((double) diff.tv_sec)  * 1000.0;
 
@@ -485,120 +613,6 @@ static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
        return (0);
 }
 
-static int ping_receive_all (pingobj_t *obj)
-{
-       fd_set readfds;
-       int num_readfds;
-       int max_readfds;
-
-       pinghost_t *ph;
-       pinghost_t *ptr;
-
-       struct timeval endtime;
-       struct timeval nowtime;
-       struct timeval timeout;
-       int status;
-
-       int ret;
-
-       ph = obj->head;
-       ret = 0;
-
-       for (ptr = ph; ptr != NULL; ptr = ptr->next)
-               ptr->latency = -1.0;
-
-       if (gettimeofday (&nowtime, NULL) == -1)
-       {
-               ping_set_errno (obj, "gettimeofday", errno);
-               return (-1);
-       }
-
-       /* Set up timeout */
-       timeout.tv_sec = (time_t) obj->timeout;
-       timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
-
-       dprintf ("Set timeout to %i.%06i seconds\n",
-                       (int) timeout.tv_sec,
-                       (int) timeout.tv_usec);
-
-       ping_timeval_add (&nowtime, &timeout, &endtime);
-
-       while (1)
-       {
-               FD_ZERO (&readfds);
-               num_readfds =  0;
-               max_readfds = -1;
-
-               for (ptr = ph; ptr != NULL; ptr = ptr->next)
-               {
-                       if (!timerisset (ptr->timer))
-                               continue;
-
-                       FD_SET (ptr->fd, &readfds);
-                       num_readfds++;
-
-                       if (max_readfds < ptr->fd)
-                               max_readfds = ptr->fd;
-               }
-
-               if (num_readfds == 0)
-                       break;
-
-               if (gettimeofday (&nowtime, NULL) == -1)
-               {
-                       ping_set_errno (obj, "gettimeofday", errno);
-                       return (-1);
-               }
-
-               if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
-                       break;
-
-               dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
-                               (int) timeout.tv_sec,
-                               (int) timeout.tv_usec);
-
-               status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
-
-               if (gettimeofday (&nowtime, NULL) == -1)
-               {
-                       ping_set_errno (obj, "gettimeofday", errno);
-                       return (-1);
-               }
-               
-               if ((status == -1) && (errno == EINTR))
-               {
-                       dprintf ("select was interrupted by signal..\n");
-                       continue;
-               }
-               else if (status < 0)
-               {
-#if WITH_DEBUG
-                       char errbuf[PING_ERRMSG_LEN];
-                       dprintf ("select: %s\n",
-                                       sstrerror (errno, errbuf, sizeof (errbuf)));
-#endif
-                       break;
-               }
-               else if (status == 0)
-               {
-                       dprintf ("select timed out\n");
-                       for (ptr = ph; ptr != NULL; ptr = ptr->next)
-                               if (ptr->latency < 0.0)
-                                       ptr->dropped++;
-                       break;
-               }
-
-               for (ptr = ph; ptr != NULL; ptr = ptr->next)
-               {
-                       if (FD_ISSET (ptr->fd, &readfds))
-                               if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
-                                       ret++;
-               }
-       } /* while (1) */
-       
-       return (ret);
-}
-
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  * Sending functions:                                                        *
  *                                                                           *
@@ -607,7 +621,7 @@ static int ping_receive_all (pingobj_t *obj)
  * `-> ping_send_one_ipv6                                                    *
  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
-               const void *buf, size_t buflen)
+               const void *buf, size_t buflen, int fd)
 {
        ssize_t ret;
 
@@ -617,7 +631,7 @@ static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
                return (-1);
        }
 
-       ret = sendto (ph->fd, buf, buflen, 0,
+       ret = sendto (fd, buf, buflen, 0,
                        (struct sockaddr *) ph->addr, ph->addrlen);
 
        if (ret < 0)
@@ -630,13 +644,13 @@ static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
                if (errno == ENETUNREACH)
                        return (0);
 #endif
-               ping_set_errno (obj, "sendto", errno);
+               ping_set_errno (obj, errno);
        }
 
        return (ret);
 }
 
-static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
+static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph, int fd)
 {
        struct icmp *icmp4;
        int status;
@@ -669,7 +683,7 @@ static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
 
        dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
 
-       status = ping_sendto (obj, ph, buf, buflen);
+       status = ping_sendto (obj, ph, buf, buflen, fd);
        if (status < 0)
        {
                perror ("ping_sendto");
@@ -681,7 +695,7 @@ static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph)
        return (0);
 }
 
-static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
+static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph, int fd)
 {
        struct icmp6_hdr *icmp6;
        int status;
@@ -714,7 +728,7 @@ static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
 
        dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
 
-       status = ping_sendto (obj, ph, buf, buflen);
+       status = ping_sendto (obj, ph, buf, buflen, fd);
        if (status < 0)
        {
                perror ("ping_sendto");
@@ -726,84 +740,129 @@ static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph)
        return (0);
 }
 
-static int ping_send_all (pingobj_t *obj)
+static int ping_send_one (pingobj_t *obj, pinghost_t *ptr, int fd)
 {
-       pinghost_t *ph;
-       pinghost_t *ptr;
-
-       int ret;
-
-       ret = 0;
-       ph = obj->head;
-
-       for (ptr = ph; ptr != NULL; ptr = ptr->next)
+       if (gettimeofday (ptr->timer, NULL) == -1)
        {
                /* start timer.. The GNU `ping6' starts the timer before
                 * sending the packet, so I will do that too */
-               if (gettimeofday (ptr->timer, NULL) == -1)
-               {
 #if WITH_DEBUG
-                       char errbuf[PING_ERRMSG_LEN];
-                       dprintf ("gettimeofday: %s\n",
-                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+               char errbuf[PING_ERRMSG_LEN];
+               dprintf ("gettimeofday: %s\n",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
 #endif
+               timerclear (ptr->timer);
+               return (-1);
+       }
+       else
+       {
+               dprintf ("timer set for hostname = %s\n", ptr->hostname);
+       }
+
+       if (ptr->addrfamily == AF_INET6)
+       {
+               dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
+               if (ping_send_one_ipv6 (obj, ptr, fd) != 0)
+               {
                        timerclear (ptr->timer);
-                       ret--;
-                       continue;
+                       return (-1);
                }
-               else
+       }
+       else if (ptr->addrfamily == AF_INET)
+       {
+               dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
+               if (ping_send_one_ipv4 (obj, ptr, fd) != 0)
                {
-                       dprintf ("timer set for hostname = %s\n", ptr->hostname);
+                       timerclear (ptr->timer);
+                       return (-1);
                }
+       }
+       else /* this should not happen */
+       {
+               dprintf ("Unknown address family: %i\n", ptr->addrfamily);
+               timerclear (ptr->timer);
+               return (-1);
+       }
 
-               if (ptr->addrfamily == AF_INET6)
-               {       
-                       dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
-                       if (ping_send_one_ipv6 (obj, ptr) != 0)
-                       {
-                               timerclear (ptr->timer);
-                               ret--;
-                               continue;
-                       }
-               }
-               else if (ptr->addrfamily == AF_INET)
+       ptr->sequence++;
+
+       return (0);
+}
+
+/*
+ * Set the TTL of a socket protocol independently.
+ */
+static int ping_set_ttl (pingobj_t *obj, int ttl)
+{
+       int ret = 0;
+       char errbuf[PING_ERRMSG_LEN];
+
+       if (obj->fd4 != -1)
+       {
+               if (setsockopt (obj->fd4, IPPROTO_IP, IP_TTL,
+                               &ttl, sizeof (ttl)))
                {
-                       dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
-                       if (ping_send_one_ipv4 (obj, ptr) != 0)
-                       {
-                               timerclear (ptr->timer);
-                               ret--;
-                               continue;
-                       }
+                       ret = errno;
+                       ping_set_error (obj, "ping_set_ttl",
+                                       sstrerror (ret, errbuf, sizeof (errbuf)));
+                       dprintf ("Setting TTLv4 failed: %s\n", errbuf);
                }
-               else /* this should not happen */
+       }
+
+       if (obj->fd6 != -1)
+       {
+               dprintf ("Setting TTLv6 to %i\n", ttl);
+               if (setsockopt (obj->fd6, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
+                               &ttl, sizeof (ttl)))
                {
-                       dprintf ("Unknown address family: %i\n", ptr->addrfamily);
-                       timerclear (ptr->timer);
-                       ret--;
-                       continue;
+                       ret = errno;
+                       ping_set_error (obj, "ping_set_ttl",
+                                       sstrerror (ret, errbuf, sizeof (errbuf)));
+                       dprintf ("Setting TTLv6 failed: %s\n", errbuf);
                }
-
-               ptr->sequence++;
        }
 
        return (ret);
 }
 
 /*
- * Set the TTL of a socket protocol independently.
+ * Set the TOS of a socket protocol independently.
+ *
+ * Using SOL_SOCKET / SO_PRIORITY might be a protocol independent way to
+ * set this. See socket(7) for details.
  */
-static int ping_set_ttl (pinghost_t *ph, int ttl)
+static int ping_set_qos (pingobj_t *obj, uint8_t qos)
 {
-       int ret = -2;
+       int ret = 0;
+       char errbuf[PING_ERRMSG_LEN];
 
-       if (ph->addrfamily == AF_INET)
+       if (obj->fd4 != -1)
        {
-               ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
+               dprintf ("Setting TP_TOS to %#04"PRIx8"\n", qos);
+               if (setsockopt (obj->fd4, IPPROTO_IP, IP_TOS,
+                               &qos, sizeof (qos)))
+               {
+                       ret = errno;
+                       ping_set_error (obj, "ping_set_qos",
+                                       sstrerror (ret, errbuf, sizeof (errbuf)));
+                       dprintf ("Setting TP_TOS failed: %s\n", errbuf);
+               }
        }
-       else if (ph->addrfamily == AF_INET6)
+
+       if (obj->fd6 != -1)
        {
-               ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
+               /* IPV6_TCLASS requires an "int". */
+               int tmp = (int) qos;
+
+               dprintf ("Setting IPV6_TCLASS to %#04"PRIx8" (%i)\n", qos, tmp);
+               if (setsockopt (obj->fd6, IPPROTO_IPV6, IPV6_TCLASS,
+                       &tmp, sizeof (tmp)))
+               {
+                       ret = errno;
+                       ping_set_error (obj, "ping_set_qos",
+                                       sstrerror (ret, errbuf, sizeof (errbuf)));
+                       dprintf ("Setting IPV6_TCLASS failed: %s\n", errbuf);
+               }
        }
 
        return (ret);
@@ -867,7 +926,6 @@ static pinghost_t *ping_alloc (void)
        ph->addr    = (struct sockaddr_storage *) (ph->timer + 1);
 
        ph->addrlen = sizeof (struct sockaddr_storage);
-       ph->fd      = -1;
        ph->latency = -1.0;
        ph->dropped = 0;
        ph->ident   = ping_get_ident () & 0xFFFF;
@@ -877,9 +935,6 @@ static pinghost_t *ping_alloc (void)
 
 static void ping_free (pinghost_t *ph)
 {
-       if (ph->fd >= 0)
-               close (ph->fd);
-       
        if (ph->username != NULL)
                free (ph->username);
 
@@ -892,11 +947,137 @@ static void ping_free (pinghost_t *ph)
        free (ph);
 }
 
+static int ping_open_socket(pingobj_t *obj, int addrfam)
+{
+       int fd;
+       if (addrfam == AF_INET6)
+       {
+               fd = socket(addrfam, SOCK_RAW, IPPROTO_ICMPV6);
+       }
+       else if (addrfam == AF_INET)
+       {
+               fd = socket(addrfam, SOCK_RAW, IPPROTO_ICMP);
+       }
+       else /* this should not happen */
+       {
+               dprintf ("Unknown address family: %i\n", addrfam);
+               return (-1);
+       }
+
+       if (fd == -1)
+       {
+#if WITH_DEBUG
+               char errbuf[PING_ERRMSG_LEN];
+               dprintf ("socket: %s\n",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+               ping_set_errno (obj, errno);
+               return -1;
+       }
+
+       if (obj->srcaddr != NULL)
+       {
+               assert (obj->srcaddrlen > 0);
+               assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
+
+               if (bind (fd, obj->srcaddr, obj->srcaddrlen) == -1)
+               {
+#if WITH_DEBUG
+                       char errbuf[PING_ERRMSG_LEN];
+                       dprintf ("bind: %s\n",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+                       ping_set_errno (obj, errno);
+                       close (fd);
+                       return -1;
+               }
+       }
+
+#ifdef SO_BINDTODEVICE
+       if (obj->device != NULL)
+       {
+               if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
+                               obj->device, strlen (obj->device) + 1) != 0)
+               {
+#if WITH_DEBUG
+                       char errbuf[PING_ERRMSG_LEN];
+                       dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+                       ping_set_errno (obj, errno);
+                       close (fd);
+                       return -1;
+               }
+       }
+#endif /* SO_BINDTODEVICE */
+#ifdef SO_TIMESTAMP
+       if (1) /* {{{ */
+       {
+               int status;
+               int opt = 1;
+
+               status = setsockopt (fd,
+                               SOL_SOCKET, SO_TIMESTAMP,
+                               &opt, sizeof (opt));
+               if (status != 0)
+               {
+#if WITH_DEBUG
+                       char errbuf[PING_ERRMSG_LEN];
+                       dprintf ("setsockopt (SO_TIMESTAMP): %s\n",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+                       ping_set_errno (obj, errno);
+                       close (fd);
+                       return -1;
+               }
+       } /* }}} if (1) */
+#endif /* SO_TIMESTAMP */
+
+       if (addrfam == AF_INET)
+       {
+               int opt;
+
+               /* Enable receiving the TOS field */
+               opt = 1;
+               setsockopt (fd, IPPROTO_IP, IP_RECVTOS,
+                               &opt, sizeof (opt));
+
+               /* Enable receiving the TTL field */
+               opt = 1;
+               setsockopt (fd, IPPROTO_IP, IP_RECVTTL,
+                               &opt, sizeof (opt));
+       }
+#if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
+       else if (addrfam == AF_INET6)
+       {
+               int opt;
+
+# if defined(IPV6_RECVHOPLIMIT)
+               /* For details see RFC 3542, section 6.3. */
+               opt = 1;
+               setsockopt (fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
+                               &opt, sizeof (opt));
+# endif /* IPV6_RECVHOPLIMIT */
+
+# if defined(IPV6_RECVTCLASS)
+               /* For details see RFC 3542, section 6.5. */
+               opt = 1;
+               setsockopt (fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
+                               &opt, sizeof (opt));
+# endif /* IPV6_RECVTCLASS */
+       }
+#endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
+
+       return fd;
+}
+
 /*
  * public methods
  */
 const char *ping_get_error (pingobj_t *obj)
 {
+       if (obj == NULL)
+               return (NULL);
        return (obj->errmsg);
 }
 
@@ -906,12 +1087,15 @@ pingobj_t *ping_construct (void)
 
        if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
                return (NULL);
-       memset (obj, '\0', sizeof (pingobj_t));
+       memset (obj, 0, sizeof (pingobj_t));
 
        obj->timeout    = PING_DEF_TIMEOUT;
        obj->ttl        = PING_DEF_TTL;
        obj->addrfamily = PING_DEF_AF;
        obj->data       = strdup (PING_DEF_DATA);
+       obj->qos        = 0;
+       obj->fd4        = -1;
+       obj->fd6        = -1;
 
        return (obj);
 }
@@ -921,6 +1105,9 @@ void ping_destroy (pingobj_t *obj)
        pinghost_t *current;
        pinghost_t *next;
 
+       if (obj == NULL)
+               return;
+
        current = obj->head;
        next = NULL;
 
@@ -937,6 +1124,15 @@ void ping_destroy (pingobj_t *obj)
        if (obj->srcaddr != NULL)
                free (obj->srcaddr);
 
+       if (obj->device != NULL)
+               free (obj->device);
+
+       if (obj->fd4 != -1)
+               close(obj->fd4);
+
+       if (obj->fd6 != -1)
+               close(obj->fd6);
+
        free (obj);
 
        return;
@@ -946,8 +1142,18 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
 {
        int ret = 0;
 
+       if ((obj == NULL) || (value == NULL))
+               return (-1);
+
        switch (option)
        {
+               case PING_OPT_QOS:
+               {
+                       obj->qos = *((uint8_t *) value);
+                       ret = ping_set_qos (obj, obj->qos);
+                       break;
+               }
+
                case PING_OPT_TIMEOUT:
                        obj->timeout = *((double *) value);
                        if (obj->timeout < 0.0)
@@ -958,12 +1164,17 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
                        break;
 
                case PING_OPT_TTL:
-                       obj->ttl = *((int *) value);
-                       if ((obj->ttl < 1) || (obj->ttl > 255))
+                       ret = *((int *) value);
+                       if ((ret < 1) || (ret > 255))
                        {
                                obj->ttl = PING_DEF_TTL;
                                ret = -1;
                        }
+                       else
+                       {
+                               obj->ttl = ret;
+                               ret = ping_set_ttl (obj, obj->ttl);
+                       }
                        break;
 
                case PING_OPT_AF:
@@ -1032,16 +1243,16 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
                        if (obj->srcaddr == NULL)
                        {
                                obj->srcaddrlen = 0;
-                               obj->srcaddr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage));
+                               obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
                                if (obj->srcaddr == NULL)
                                {
-                                       ping_set_errno (obj, "malloc", errno);
+                                       ping_set_errno (obj, errno);
                                        ret = -1;
                                        freeaddrinfo (ai_list);
                                        break;
                                }
                        }
-                       memset ((void *) obj->srcaddr, '\0', sizeof (struct sockaddr_storage));
+                       memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
                        assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
                        memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
                                        ai_list->ai_addrlen);
@@ -1052,6 +1263,28 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
                } /* case PING_OPT_SOURCE */
                break;
 
+               case PING_OPT_DEVICE:
+               {
+#ifdef SO_BINDTODEVICE
+                       char *device = strdup ((char *) value);
+
+                       if (device == NULL)
+                       {
+                               ping_set_errno (obj, errno);
+                               ret = -1;
+                               break;
+                       }
+
+                       if (obj->device != NULL)
+                               free (obj->device);
+                       obj->device = device;
+#else /* ! SO_BINDTODEVICE */
+                       ping_set_errno (obj, ENOTSUP);
+                       ret = -1;
+#endif /* ! SO_BINDTODEVICE */
+               } /* case PING_OPT_DEVICE */
+               break;
+
                default:
                        ret = -2;
        } /* switch (option) */
@@ -1059,19 +1292,200 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
        return (ret);
 } /* int ping_setopt */
 
-
 int ping_send (pingobj_t *obj)
 {
-       int ret;
+       fd_set read_fds;
+       fd_set write_fds;
+       fd_set err_fds;
+
+       int num_fds;
+       int max_fd;
+
+       pinghost_t *ph;
+       pinghost_t *ptr;
 
-       if (ping_send_all (obj) < 0)
+       struct timeval endtime;
+       struct timeval nowtime;
+       struct timeval timeout;
+       int status;
+
+       int pings = 0;
+       int ret = 0;
+
+       ph = obj->head;
+
+       int fd4 = obj->fd4;
+       int fd6 = obj->fd6;
+
+       for (ptr = ph; ptr != NULL; ptr = ptr->next)
+       {
+               if (fd6 == -1 && ptr->addrfamily == AF_INET6)
+               {
+                       obj->fd6 = fd6 = ping_open_socket(obj, AF_INET6);
+                       ping_set_ttl (obj, obj->ttl);
+                       ping_set_qos (obj, obj->qos);
+               }
+               else if (fd4 == -1 && ptr->addrfamily == AF_INET)
+               {
+                       obj->fd4 = fd4 = ping_open_socket(obj, AF_INET);
+                       ping_set_ttl (obj, obj->ttl);
+                       ping_set_qos (obj, obj->qos);
+               }
+
+               if ((fd6 == -1 && ptr->addrfamily == AF_INET6)
+                       || (fd4 == -1 && ptr->addrfamily == AF_INET))
+               {
+#if WITH_DEBUG
+                       char errbuf[PING_ERRMSG_LEN];
+                       dprintf ("socket: %s\n",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+                       ping_set_errno (obj, errno);
+                       return (-1);
+               }
+
+               ptr->latency  = -1.0;
+               ptr->recv_ttl = -1;
+       }
+
+       if (fd4 == -1 && fd6 == -1)
+       {
+               dprintf("No sockets to use\n");
+               return (-1);
+       }
+
+       if (gettimeofday (&nowtime, NULL) == -1)
+       {
+               ping_set_errno (obj, errno);
                return (-1);
+       }
+
+       /* Set up timeout */
+       timeout.tv_sec = (time_t) obj->timeout;
+       timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
+
+       dprintf ("Set timeout to %i.%06i seconds\n",
+                       (int) timeout.tv_sec,
+                       (int) timeout.tv_usec);
+
+       ping_timeval_add (&nowtime, &timeout, &endtime);
+
+       ptr = ph;
+       num_fds = 0;
+       if (fd4 != -1) num_fds++;
+       if (fd6 != -1) num_fds++;
+       max_fd = fd4 > fd6 ? fd4 : fd6;
+
+       while (pings > 0 || ptr != NULL)
+       {
+               FD_ZERO (&read_fds);
+               FD_ZERO (&write_fds);
+               FD_ZERO (&err_fds);
+
+               if (fd4 != -1) FD_SET(fd4, &read_fds);
+               if (fd6 != -1) FD_SET(fd6, &read_fds);
+
+               if (fd4 != -1 && ptr != NULL && ptr->addrfamily == AF_INET)
+                       FD_SET(fd4, &write_fds);
+               if (fd6 != -1 && ptr != NULL && ptr->addrfamily == AF_INET6)
+                       FD_SET(fd6, &write_fds);
+               if (fd4 != -1) FD_SET(fd4, &err_fds);
+               if (fd6 != -1) FD_SET(fd6, &err_fds);
+
+               if (gettimeofday (&nowtime, NULL) == -1)
+               {
+                       ping_set_errno (obj, errno);
+                       return (-1);
+               }
+
+               if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
+                       break;
+
+               dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_fds,
+                               (int) timeout.tv_sec,
+                               (int) timeout.tv_usec);
+
+               status = select (max_fd + 1, &read_fds, &write_fds, &err_fds, &timeout);
+
+               if (gettimeofday (&nowtime, NULL) == -1)
+               {
+                       ping_set_errno (obj, errno);
+                       return (-1);
+               }
+
+               if ((status == -1) && (errno == EINTR))
+               {
+                       dprintf ("select was interrupted by signal..\n");
+                       continue;
+               }
+               else if (status < 0)
+               {
+#if WITH_DEBUG
+                       char errbuf[PING_ERRMSG_LEN];
+                       dprintf ("select: %s\n",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+                       break;
+               }
+               else if (status == 0)
+               {
+                       dprintf ("select timed out\n");
+                       for (ptr = ph; ptr != NULL; ptr = ptr->next)
+                               if (ptr->latency < 0.0)
+                                       ptr->dropped++;
+                       
+                       break;
+               }
+
+               if (fd4 != -1)
+               {
+                       if (FD_ISSET (fd4, &read_fds))
+                       {
+                               if (!ping_receive_one(obj, &nowtime, AF_INET))
+                                       --pings;
+                       }
+                       else if (ptr != NULL && ptr->addrfamily == AF_INET && 
+                                               FD_ISSET (fd4, &write_fds))
+                       {
+                               if (!ping_send_one(obj, ptr, fd4))
+                               {
+                                       ptr = ptr->next;
+                                       ++pings;
+                               }
+                               else
+                               {
+                                       --ret;
+                               }
+                       }
+
+               }
+
+               if (fd6  != -1)
+               {
+                       if (FD_ISSET (fd6, &read_fds))
+                       {
+                               if (!ping_receive_one(obj, &nowtime, AF_INET6))
+                                       --pings;
+                       }
+                       else if (ptr != NULL && ptr->addrfamily == AF_INET6 &&
+                                               FD_ISSET (fd6, &write_fds))
+                       {
+                               if (!ping_send_one(obj, ptr, fd6))
+                               {
+                                       ++pings;
+                                       ptr = ptr->next;
+                               }
+                               else
+                               {
+                                       --ret;
+                               }
+                       }
+               }
 
-       if ((ret = ping_receive_all (obj)) < 0)
-               return (-2);
+       } /* while (1) */
 
        return (ret);
-}
+} /* int ping_send */
 
 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
 {
@@ -1090,13 +1504,13 @@ int ping_host_add (pingobj_t *obj, const char *host)
 {
        pinghost_t *ph;
 
-       struct sockaddr_storage sockaddr;
-       socklen_t               sockaddr_len;
-
        struct addrinfo  ai_hints;
        struct addrinfo *ai_list, *ai_ptr;
        int              ai_return;
 
+       if ((obj == NULL) || (host == NULL))
+               return (-1);
+
        dprintf ("host = %s\n", host);
 
        if (ping_host_search (obj->head, host) != NULL)
@@ -1122,7 +1536,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
        if ((ph->username = strdup (host)) == NULL)
        {
                dprintf ("Out of memory!\n");
-               ping_set_errno (obj, "strdup", errno);
+               ping_set_errno (obj, errno);
                ping_free (ph);
                return (-1);
        }
@@ -1130,7 +1544,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
        if ((ph->hostname = strdup (host)) == NULL)
        {
                dprintf ("Out of memory!\n");
-               ping_set_errno (obj, "strdup", errno);
+               ping_set_errno (obj, errno);
                ping_free (ph);
                return (-1);
        }
@@ -1139,7 +1553,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
        if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
        {
                dprintf ("Out of memory!\n");
-               ping_set_errno (obj, "strdup", errno);
+               ping_set_errno (obj, errno);
                ping_free (ph);
                return (-1);
        }
@@ -1165,32 +1579,13 @@ int ping_host_add (pingobj_t *obj, const char *host)
 
        for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
        {
-               ph->fd = -1;
-
-               sockaddr_len = sizeof (sockaddr);
-               memset (&sockaddr, '\0', sockaddr_len);
-
                if (ai_ptr->ai_family == AF_INET)
                {
-                       struct sockaddr_in *si;
-
-                       si = (struct sockaddr_in *) &sockaddr;
-                       si->sin_family = AF_INET;
-                       si->sin_port   = htons (ph->ident);
-                       si->sin_addr.s_addr = htonl (INADDR_ANY);
-
                        ai_ptr->ai_socktype = SOCK_RAW;
                        ai_ptr->ai_protocol = IPPROTO_ICMP;
                }
                else if (ai_ptr->ai_family == AF_INET6)
                {
-                       struct sockaddr_in6 *si;
-
-                       si = (struct sockaddr_in6 *) &sockaddr;
-                       si->sin6_family = AF_INET6;
-                       si->sin6_port   = htons (ph->ident);
-                       si->sin6_addr   = in6addr_any;
-
                        ai_ptr->ai_socktype = SOCK_RAW;
                        ai_ptr->ai_protocol = IPPROTO_ICMPV6;
                }
@@ -1201,44 +1596,11 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
                        errmsg[PING_ERRMSG_LEN - 1] = '\0';
 
-                       dprintf (errmsg);
+                       dprintf ("%s", errmsg);
                        ping_set_error (obj, "getaddrinfo", errmsg);
                        continue;
                }
 
-               /* TODO: Move this to a static function `ping_open_socket' and
-                * call it whenever the socket dies. */
-               ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
-               if (ph->fd == -1)
-               {
-#if WITH_DEBUG
-                       char errbuf[PING_ERRMSG_LEN];
-                       dprintf ("socket: %s\n",
-                                       sstrerror (errno, errbuf, sizeof (errbuf)));
-#endif
-                       ping_set_errno (obj, "socket", errno);
-                       continue;
-               }
-
-               if (obj->srcaddr != NULL)
-               {
-                       assert (obj->srcaddrlen > 0);
-                       assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
-
-                       if (bind (ph->fd, (struct sockaddr *) obj->srcaddr, obj->srcaddrlen) == -1)
-                       {
-#if WITH_DEBUG
-                               char errbuf[PING_ERRMSG_LEN];
-                               dprintf ("bind: %s\n",
-                                               sstrerror (errno, errbuf, sizeof (errbuf)));
-#endif
-                               ping_set_errno (obj, "bind", errno);
-                               close (ph->fd);
-                               ph->fd = -1;
-                               continue;
-                       }
-               }
-
                assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
                memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
                memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
@@ -1267,17 +1629,10 @@ int ping_host_add (pingobj_t *obj, const char *host)
                }
 #endif /* AI_CANONNAME */
 
-               break;
-       }
+       } /* for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
 
        freeaddrinfo (ai_list);
 
-       if (ph->fd < 0)
-       {
-               ping_free (ph);
-               return (-1);
-       }
-
        /*
         * Adding in the front is much easier, but then the iterator will
         * return the host that was added last as first host. That's just not
@@ -1299,14 +1654,18 @@ int ping_host_add (pingobj_t *obj, const char *host)
                hptr->next = ph;
        }
 
-       ping_set_ttl (ph, obj->ttl);
+       ph->table_next = obj->table[ph->ident % PING_TABLE_LEN];
+       obj->table[ph->ident % PING_TABLE_LEN] = ph;
 
        return (0);
-}
+} /* int ping_host_add */
 
 int ping_host_remove (pingobj_t *obj, const char *host)
 {
-       pinghost_t *pre, *cur;
+       pinghost_t *pre, *cur, *target;
+
+       if ((obj == NULL) || (host == NULL))
+               return (-1);
 
        pre = NULL;
        cur = obj->head;
@@ -1331,6 +1690,32 @@ int ping_host_remove (pingobj_t *obj, const char *host)
        else
                pre->next = cur->next;
        
+       target = cur;
+       pre = NULL;
+       
+       cur = obj->table[target->ident % PING_TABLE_LEN];
+
+       while (cur != NULL)
+       {
+               if (cur == target)
+                       break;
+
+               pre = cur;
+               cur = cur->table_next;
+       }
+
+       if (cur == NULL)
+       {
+               ping_set_error(obj, "ping_host_remove", "Host not found (T)");
+               ping_free(target);
+               return (-1);
+       }
+
+       if (pre == NULL)
+               obj->table[target->ident % PING_TABLE_LEN] = cur->table_next;
+       else
+               pre->table_next = cur->table_next;
+
        ping_free (cur);
 
        return (0);
@@ -1338,11 +1723,15 @@ int ping_host_remove (pingobj_t *obj, const char *host)
 
 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
 {
+       if (obj == NULL)
+               return (NULL);
        return ((pingobj_iter_t *) obj->head);
 }
 
 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
 {
+       if (iter == NULL)
+               return (NULL);
        return ((pingobj_iter_t *) iter->next);
 }
 
@@ -1353,6 +1742,12 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info,
 
        size_t orig_buffer_len = *buffer_len;
 
+       if ((iter == NULL) || (buffer_len == NULL))
+               return (-1);
+
+       if ((buffer == NULL) && (*buffer_len != 0 ))
+               return (-1);
+
        switch (info)
        {
                case PING_INFO_USERNAME:
@@ -1456,6 +1851,25 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info,
                        strncpy ((char *) buffer, iter->data, orig_buffer_len);
                        ret = 0;
                        break;
+
+               case PING_INFO_RECV_TTL:
+                       ret = ENOMEM;
+                       *buffer_len = sizeof (int);
+                       if (orig_buffer_len < sizeof (int))
+                               break;
+                       *((int *) buffer) = iter->recv_ttl;
+                       ret = 0;
+                       break;
+
+               case PING_INFO_RECV_QOS:
+                       ret = ENOMEM;
+                       if (*buffer_len>sizeof(unsigned)) *buffer_len=sizeof(unsigned);
+                       if (!*buffer_len) *buffer_len=1;
+                       if (orig_buffer_len < *buffer_len)
+                               break;
+                       memcpy(buffer,&iter->recv_qos,*buffer_len);
+                       ret = 0;
+                       break;
        }
 
        return (ret);
@@ -1463,10 +1877,14 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info,
 
 void *ping_iterator_get_context (pingobj_iter_t *iter)
 {
+       if (iter == NULL)
+               return (NULL);
        return (iter->context);
 }
 
 void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
 {
+       if (iter == NULL)
+               return;
        iter->context = context;
 }