ping_set_tos: Improve error handling.
[liboping.git] / src / liboping.c
index 139fa97..6aee297 100644 (file)
@@ -532,7 +532,7 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
                        if (cmsg->cmsg_level != IPPROTO_IPV6)
                                continue;
 
-                       if (cmsg->cmsg_type == IPV6_RECVTCLASS)
+                       if (cmsg->cmsg_type == IPV6_TCLASS)
                        {
                                memcpy (&recv_tos, CMSG_DATA (cmsg),
                                                sizeof (recv_tos));
@@ -952,22 +952,43 @@ static int ping_set_ttl (pinghost_t *ph, int ttl)
 
 /*
  * 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_tos (pinghost_t *ph, uint8_t tos)
+static int ping_set_tos (pingobj_t *obj, pinghost_t *ph, uint8_t tos)
 {
-       int ret = -2;
+       int ret = EINVAL;
+       char errbuf[PING_ERRMSG_LEN];
 
        if (ph->addrfamily == AF_INET)
        {
-               dprintf ("Setting TP_TOS to %i\n", ttl);
+               dprintf ("Setting TP_TOS to %#04"PRIx8"\n", tos);
                ret = setsockopt (ph->fd, IPPROTO_IP, IP_TOS,
                                &tos, sizeof (tos));
+               if (ret != 0)
+               {
+                       ret = errno;
+                       ping_set_error (obj, "ping_set_tos",
+                                       sstrerror (ret, errbuf, sizeof (errbuf)));
+                       dprintf ("Setting TP_TOS failed: %s\n", errbuf);
+               }
        }
        else if (ph->addrfamily == AF_INET6)
        {
-               dprintf ("Setting IPV6_TCLASS to %i\n", ttl);
+               /* IPV6_TCLASS requires an "int". */
+               int tmp = (int) tos;
+
+               dprintf ("Setting IPV6_TCLASS to %#04"PRIx8" (%i)\n", tos, tmp);
                ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_TCLASS,
-                               &tos, sizeof (tos));
+                               &tmp, sizeof (tmp));
+               if (ret != 0)
+               {
+                       ret = errno;
+                       ping_set_error (obj, "ping_set_tos",
+                                       sstrerror (ret, errbuf, sizeof (errbuf)));
+                       dprintf ("Setting IPV6_TCLASS failed: %s\n", errbuf);
+               }
        }
 
        return (ret);
@@ -1130,7 +1151,7 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
 
                        obj->tos = *((uint8_t *) value);
                        for (ph = obj->head; ph != NULL; ph = ph->next)
-                               ping_set_tos (ph, obj->tos);
+                               ping_set_tos (obj, ph, obj->tos);
                        break;
                }
 
@@ -1489,23 +1510,41 @@ int ping_host_add (pingobj_t *obj, const char *host)
 
                if (ph->addrfamily == AF_INET)
                {
-                       int opt = 1;
+                       int opt;
+
+                       /* Enable receiving the TOS field */
+                       opt = 1;
+                       setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
+                                       &opt, sizeof (opt));
 
+                       /* Enable receiving the TTL field */
+                       opt = 1;
                        setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
                                        &opt, sizeof (opt));
                }
-#if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
+#if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
                else if (ph->addrfamily == AF_INET6)
                {
-                       int opt = 1;
+                       int opt;
 
+# if defined(IPV6_RECVHOPLIMIT)
+                       /* For details see RFC 3542, section 6.3. */
+                       opt = 1;
                        setsockopt (ph->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 (ph->fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
+                                       &opt, sizeof (opt));
+# endif /* IPV6_RECVTCLASS */
                }
-#endif
+#endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
 
                break;
-       }
+       } /* for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
 
        freeaddrinfo (ai_list);
 
@@ -1537,7 +1576,7 @@ int ping_host_add (pingobj_t *obj, const char *host)
        }
 
        ping_set_ttl (ph, obj->ttl);
-       ping_set_tos (ph, obj->tos);
+       ping_set_tos (obj, ph, obj->tos);
 
        return (0);
 } /* int ping_host_add */
@@ -1717,7 +1756,7 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info,
                        ret = 0;
                        break;
 
-               case PING_INFO_TOS:
+               case PING_INFO_RECV_TOS:
                        ret = ENOMEM;
                        if (*buffer_len>sizeof(unsigned)) *buffer_len=sizeof(unsigned);
                        if (!*buffer_len) *buffer_len=1;