Code

oping: Make the exit status the number of hosts failed.
[liboping.git] / src / liboping.c
index a272e2cb92f7051ad964be0f20f67874fefc2b21..76a7d53456c1fd31be8fef44b87695e6f74bfb70 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * Object oriented C module to send ICMP and ICMPv6 `echo's.
- * Copyright (C) 2006-2010  Florian octo Forster <octo at verplant.org>
+ * Copyright (C) 2006-2011  Florian octo Forster <ff at octo.it>
  *
  * 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
@@ -450,7 +450,7 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
         * reply. The right object will be returned by ping_receive_ipv*(). For
         * now, we can only rely on ph->fd and ph->addrfamily. */
 
-       struct timeval diff;
+       struct timeval diff, pkt_now = *now;
        pinghost_t *host = NULL;
        int recv_ttl;
        uint8_t recv_qos;
@@ -499,12 +499,19 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
 
        /* Iterate over all auxiliary data in msghdr */
        recv_ttl = -1;
-       recv_qos = 0xff;
+       recv_qos = 0;
        for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
                        cmsg != NULL;
                        cmsg = CMSG_NXTHDR (&msghdr, cmsg))
        {
-               if (ph->addrfamily == AF_INET) /* {{{ */
+               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 (ph->addrfamily == AF_INET) /* {{{ */
                {
                        if (cmsg->cmsg_level != IPPROTO_IP)
                                continue;
@@ -578,13 +585,13 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
        }
 
        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);
@@ -596,8 +603,7 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
 
        if (recv_ttl >= 0)
                host->recv_ttl = recv_ttl;
-       if (recv_qos != 0xffff)
-               host->recv_qos = recv_qos;
+       host->recv_qos = recv_qos;
 
        host->latency  = ((double) diff.tv_usec) / 1000.0;
        host->latency += ((double) diff.tv_sec)  * 1000.0;
@@ -1423,7 +1429,7 @@ 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;
                }
@@ -1469,7 +1475,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        {
 #if WITH_DEBUG
                                char errbuf[PING_ERRMSG_LEN];
-                               dprintf ("setsockopt: %s\n",
+                               dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
                                                sstrerror (errno, errbuf, sizeof (errbuf)));
 #endif
                                ping_set_errno (obj, errno);
@@ -1479,7 +1485,29 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        }
                }
 #endif /* SO_BINDTODEVICE */
+#ifdef SO_TIMESTAMP
+               if (1) /* {{{ */
+               {
+                       int status;
+                       int opt = 1;
 
+                       status = setsockopt (ph->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 (ph->fd);
+                               ph->fd = -1;
+                               continue;
+                       }
+               } /* }}} if (1) */
+#endif /* SO_TIMESTAMP */
                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);
@@ -1512,10 +1540,12 @@ int ping_host_add (pingobj_t *obj, const char *host)
                {
                        int opt;
 
+#ifdef IP_RECVTOS
                        /* Enable receiving the TOS field */
                        opt = 1;
                        setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
                                        &opt, sizeof (opt));
+#endif /* IP_RECVTOS */
 
                        /* Enable receiving the TTL field */
                        opt = 1;