Merge remote-tracking branch 'github/pr/11'
[liboping.git] / src / liboping.c
index fd6e942..b6c80f2 100644 (file)
@@ -1367,8 +1367,6 @@ int ping_send (pingobj_t *obj)
        struct timeval nowtime;
        struct timeval timeout;
 
-       int ret = 0;
-
        _Bool need_ipv4_socket = 0;
        _Bool need_ipv6_socket = 0;
 
@@ -1432,6 +1430,12 @@ int ping_send (pingobj_t *obj)
         * receive a "pong" yet. */
        int pings_in_flight = 0;
 
+       /* pongs_received is the number of echo replies received. Unless there
+        * is an error, this is used as the return value of ping_send(). */
+       int pongs_received = 0;
+
+       int error_count = 0;
+
        while (pings_in_flight > 0 || host_to_ping != NULL)
        {
                fd_set read_fds;
@@ -1478,9 +1482,10 @@ int ping_send (pingobj_t *obj)
                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);
+               dprintf ("Waiting on %i sockets for %u.%06u seconds\n",
+                               ((obj->fd4 != -1) ? 1 : 0) + ((obj->fd6 != -1) ? 1 : 0),
+                               (unsigned) timeout.tv_sec,
+                               (unsigned) timeout.tv_usec);
 
                int status = select (max_fd + 1, &read_fds, &write_fds, NULL, &timeout);
 
@@ -1511,13 +1516,19 @@ int ping_send (pingobj_t *obj)
                if (obj->fd6  != -1 && FD_ISSET (obj->fd6, &read_fds))
                {
                        if (ping_receive_one (obj, &nowtime, AF_INET6) == 0)
+                       {
                                pings_in_flight--;
+                               pongs_received++;
+                       }
                        continue;
                }
                if (obj->fd4 != -1 && FD_ISSET (obj->fd4, &read_fds))
                {
                        if (ping_receive_one (obj, &nowtime, AF_INET) == 0)
+                       {
                                pings_in_flight--;
+                               pongs_received++;
+                       }
                        continue;
                }
 
@@ -1532,13 +1543,15 @@ int ping_send (pingobj_t *obj)
                        if (ping_send_one (obj, host_to_ping, write_fd) == 0)
                                pings_in_flight++;
                        else
-                               ret--;
+                               error_count++;
                        host_to_ping = host_to_ping->next;
                        continue;
                }
        } /* while (1) */
 
-       return (ret);
+       if (error_count)
+               return (-1 * error_count);
+       return (pongs_received);
 } /* int ping_send */
 
 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
@@ -1788,6 +1801,20 @@ pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
        return ((pingobj_iter_t *) iter->next);
 }
 
+int ping_iterator_count (pingobj_t *obj)
+{
+       if (obj == NULL)
+               return 0;
+
+       int count = 0;
+       pingobj_iter_t *iter = obj->head;
+       while (iter) {
+               count++;
+               iter = iter->next;
+       }
+       return count;
+}
+
 int ping_iterator_get_info (pingobj_iter_t *iter, int info,
                void *buffer, size_t *buffer_len)
 {