Code

libcollectdclient: Check return value of setsockopt().
[collectd.git] / src / libcollectdclient / network.c
index 02a8e966b99d4b61ab604c2f2e591fb93c6300d6..6733cda4de758929d842fef81d0f2df2b85d7861 100644 (file)
@@ -24,6 +24,8 @@
  *   Florian octo Forster <octo at collectd.org>
  **/
 
+#include "collectd.h"
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <netdb.h>
 
+#if HAVE_NETINET_IN_H
+# include <netinet/in.h>
+#endif
+
 #include "collectd/network.h"
 #include "collectd/network_buffer.h"
 
@@ -142,7 +148,6 @@ static int server_open_socket (lcc_server_t *srv) /* {{{ */
 
     if (ai_ptr->ai_family == AF_INET)
     {
-
       struct sockaddr_in *addr = (struct sockaddr_in *) ai_ptr->ai_addr;
       int optname;
 
@@ -151,9 +156,8 @@ static int server_open_socket (lcc_server_t *srv) /* {{{ */
       else
         optname = IP_TTL;
 
-      setsockopt (srv->fd, IPPROTO_IP, optname,
-          &srv->ttl,
-          sizeof (srv->ttl));
+      status = setsockopt (srv->fd, IPPROTO_IP, optname,
+          &srv->ttl, sizeof (srv->ttl));
     }
     else if (ai_ptr->ai_family == AF_INET6)
     {
@@ -166,9 +170,15 @@ static int server_open_socket (lcc_server_t *srv) /* {{{ */
       else
         optname = IPV6_UNICAST_HOPS;
 
-      setsockopt (srv->fd, IPPROTO_IPV6, optname,
-          &srv->ttl,
-          sizeof (srv->ttl));
+      status = setsockopt (srv->fd, IPPROTO_IPV6, optname,
+          &srv->ttl, sizeof (srv->ttl));
+    }
+    if (status != 0)
+    {
+      /* setsockopt failed. */
+      close (srv->fd);
+      srv->fd = -1;
+      continue;
     }
 
     srv->sa = malloc (ai_ptr->ai_addrlen);