Code

svn merge -r773:775 branches/collectd-3.9 branches/collectd-3.8
[collectd.git] / src / liboping / liboping.c
index 43d5fd509eb0711e3c7c2d58c9354584b079db6d..46db24471be5dd8d84e93da91018d2b1033e47c1 100644 (file)
 /*
  * private (static) functions
  */
-static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
-               struct timeval *res)
-{
-       res->tv_sec  = tv1->tv_sec  + tv2->tv_sec;
-       res->tv_usec = tv1->tv_usec + tv2->tv_usec;
-
-       while (res->tv_usec > 1000000)
-       {
-               res->tv_usec -= 1000000;
-               res->tv_sec++;
-       }
-
-       return (0);
-}
-
 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
                struct timeval *res)
 {
@@ -213,7 +198,7 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
        for (ptr = ph; ptr != NULL; ptr = ptr->next)
        {
                dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
-                               ptr->hostname, ptr->ident, ptr->sequence - 1);
+                               ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
 
                if (ptr->addrfamily != AF_INET)
                        continue;
@@ -224,7 +209,7 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
                if (ptr->ident != ident)
                        continue;
 
-               if ((ptr->sequence - 1) != seq)
+               if (((ptr->sequence - 1) & 0xFFFF) != seq)
                        continue;
 
                dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
@@ -276,7 +261,7 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
        for (ptr = ph; ptr != NULL; ptr = ptr->next)
        {
                dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
-                               ptr->hostname, ptr->ident, ptr->sequence - 1);
+                               ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
 
                if (ptr->addrfamily != AF_INET6)
                        continue;
@@ -287,7 +272,7 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
                if (ptr->ident != ident)
                        continue;
 
-               if ((ptr->sequence - 1) != seq)
+               if (((ptr->sequence - 1) & 0xFFFF) != seq)
                        continue;
 
                dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
@@ -365,13 +350,12 @@ static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
        return (0);
 }
 
-static int ping_receive_all (pingobj_t *obj)
+static int ping_receive_all (pinghost_t *ph)
 {
        fd_set readfds;
        int num_readfds;
        int max_readfds;
 
-       pinghost_t *ph;
        pinghost_t *ptr;
 
        struct timeval endtime;
@@ -381,24 +365,14 @@ static int ping_receive_all (pingobj_t *obj)
 
        int ret;
 
-       ph = obj->head;
        ret = 0;
 
        for (ptr = ph; ptr != NULL; ptr = ptr->next)
                ptr->latency = -1.0;
 
-       if (gettimeofday (&nowtime, NULL) == -1)
+       if (gettimeofday (&endtime, NULL) == -1)
                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);
+       endtime.tv_sec += 1;
 
        while (1)
        {
@@ -624,25 +598,6 @@ static int ping_send_all (pinghost_t *ph)
        return (0);
 }
 
-/*
- * Set the TTL of a socket protocol independently.
- */
-static int ping_set_ttl (pinghost_t *ph, int ttl)
-{
-       int ret = -2;
-
-       if (ph->addrfamily == AF_INET)
-       {
-               ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
-       }
-       else if (ph->addrfamily == AF_INET6)
-       {
-               ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
-       }
-
-       return (ret);
-}
-
 static int ping_get_ident (void)
 {
        int fd;
@@ -796,7 +751,7 @@ int ping_send (pingobj_t *obj)
        if (ping_send_all (obj->head) < 0)
                return (-1);
 
-       if ((ret = ping_receive_all (obj)) < 0)
+       if ((ret = ping_receive_all (obj->head)) < 0)
                return (-2);
 
        return (ret);
@@ -836,7 +791,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
 #ifdef AI_ADDRCONFIG
        ai_hints.ai_flags    |= AI_ADDRCONFIG;
 #endif
-       ai_hints.ai_family    = obj->addrfamily;
+       ai_hints.ai_family    = PF_UNSPEC;
        ai_hints.ai_socktype  = SOCK_RAW;
 
        if ((ph = ping_alloc ()) == NULL)
@@ -901,6 +856,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        continue;
                }
 
+#if 0
                if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
                {
                        dprintf ("bind: %s\n", strerror (errno));
@@ -908,6 +864,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        ph->fd = -1;
                        continue;
                }
+#endif
 
                assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
                memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
@@ -930,8 +887,6 @@ int ping_host_add (pingobj_t *obj, const char *host)
        ph->next  = obj->head;
        obj->head = ph;
 
-       ping_set_ttl (ph, obj->ttl);
-
        return (0);
 }