]> arthur.barton.de Git - netdata.git/commitdiff
re-worked connect_to() to properly support IPv6; #1432
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 24 Dec 2016 15:25:43 +0000 (17:25 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 24 Dec 2016 15:25:43 +0000 (17:25 +0200)
CMakeLists.txt
src/Makefile.am
src/backends.c
src/common.h
src/log.h
src/socket.c [new file with mode: 0644]
src/socket.h [new file with mode: 0644]

index d58e66cbe0ee67dde922cb18f750fb6d4a0d8195..eb56d2a0c9b69baca77a5c71f27c0beeac764d7d 100755 (executable)
@@ -94,7 +94,7 @@ set(NETDATA_SOURCE_FILES
         src/web_client.h
         src/web_server.c
         src/web_server.h
-)
+        src/socket.c src/socket.h)
 
 set(APPS_PLUGIN_SOURCE_FILES
         src/appconfig.c
index f2ddef3d11081e2253675dcc0a8c37b6f709595a..c2918744a215fcdb8a3863f41a6b722fc85c009a 100644 (file)
@@ -45,6 +45,7 @@ netdata_SOURCES = \
        plugin_tc.c plugin_tc.h \
        plugins_d.c plugins_d.h \
        popen.c popen.h \
+       socket.c socket.h \
        sys_fs_cgroup.c \
        procfile.c procfile.h \
        proc_self_mountinfo.c proc_self_mountinfo.h \
index 34e032fa9ed3f046ea1c0e0e8f1cc918f2688d66..64f25524c4fc9b3f468737222a99e4817a866985 100644 (file)
@@ -4,166 +4,6 @@
 #define BACKEND_SOURCE_DATA_AVERAGE      0x00000002
 #define BACKEND_SOURCE_DATA_SUM          0x00000004
 
-int connect_to_socket4(const char *ip, int port, struct timeval *timeout) {
-    int sock;
-
-    debug(D_LISTENER, "IPv4 connecting to ip '%s' port %d", ip, port);
-
-    sock = socket(AF_INET, SOCK_STREAM, 0);
-    if(sock < 0) {
-        error("IPv4 socket() on ip '%s' port %d failed.", ip, port);
-        return -1;
-    }
-
-    if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)timeout, sizeof(struct timeval)) < 0)
-        error("Failed to set timeout on the socket to ip '%s' port %d", ip, port);
-
-    struct sockaddr_in name;
-    memset(&name, 0, sizeof(struct sockaddr_in));
-    name.sin_family = AF_INET;
-    name.sin_port = htons(port);
-
-    int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
-    if(ret != 1) {
-        error("Failed to convert '%s' to a valid IPv4 address.", ip);
-        close(sock);
-        return -1;
-    }
-
-    if(connect(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
-        close(sock);
-        error("IPv4 failed to connect to '%s', port %d", ip, port);
-        return -1;
-    }
-
-    debug(D_LISTENER, "Connected to IPv4 ip '%s' port %d", ip, port);
-    return sock;
-}
-
-int connect_to_socket6(const char *ip, int port, struct timeval *timeout) {
-    int sock = -1;
-    int ipv6only = 1;
-
-    debug(D_LISTENER, "IPv6 connecting to ip '%s' port %d", ip, port);
-
-    sock = socket(AF_INET6, SOCK_STREAM, 0);
-    if (sock < 0) {
-        error("IPv6 socket() on ip '%s' port %d failed.", ip, port);
-        return -1;
-    }
-
-    if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)timeout, sizeof(struct timeval)) < 0)
-        error("Failed to set timeout on the socket to ip '%s' port %d", ip, port);
-
-    /* IPv6 only */
-    if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&ipv6only, sizeof(ipv6only)) != 0)
-        error("Cannot set IPV6_V6ONLY on ip '%s' port's %d.", ip, port);
-
-    struct sockaddr_in6 name;
-    memset(&name, 0, sizeof(struct sockaddr_in6));
-    name.sin6_family = AF_INET6;
-    name.sin6_port = htons ((uint16_t) port);
-
-    int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
-    if(ret != 1) {
-        error("Failed to convert IP '%s' to a valid IPv6 address.", ip);
-        close(sock);
-        return -1;
-    }
-
-    name.sin6_scope_id = 0;
-
-    if(connect(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
-        close(sock);
-        error("IPv6 failed to connect to '%s', port %d", ip, port);
-        return -1;
-    }
-
-    debug(D_LISTENER, "Connected to IPv6 ip '%s' port %d", ip, port);
-    return sock;
-}
-
-
-static inline int connect_to_one(const char *definition, int default_port, struct timeval *timeout) {
-    struct addrinfo hints;
-    struct addrinfo *result = NULL, *rp = NULL;
-
-    char buffer[strlen(definition) + 1];
-    strcpy(buffer, definition);
-
-    char buffer2[10 + 1];
-    snprintfz(buffer2, 10, "%d", default_port);
-
-    char *ip = buffer, *port = buffer2;
-
-    char *e = ip;
-    if(*e == '[') {
-        e = ++ip;
-        while(*e && *e != ']') e++;
-        if(*e == ']') {
-            *e = '\0';
-            e++;
-        }
-    }
-    else {
-        while(*e && *e != ':') e++;
-    }
-
-    if(*e == ':') {
-        port = e + 1;
-        *e = '\0';
-    }
-
-    if(!*ip)
-        return -1;
-
-    if(!*port)
-        port = buffer2;
-
-    memset(&hints, 0, sizeof(struct addrinfo));
-    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
-    hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
-    hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
-    hints.ai_protocol = 0;          /* Any protocol */
-    hints.ai_canonname = NULL;
-    hints.ai_addr = NULL;
-    hints.ai_next = NULL;
-
-    int r = getaddrinfo(ip, port, &hints, &result);
-    if (r != 0) {
-        error("Cannot resolve host '%s', port '%s': %s\n", ip, port, gai_strerror(r));
-        return -1;
-    }
-
-    int fd = -1;
-    for (rp = result; rp != NULL && fd == -1; rp = rp->ai_next) {
-        char rip[INET_ADDRSTRLEN + INET6_ADDRSTRLEN] = "INVALID";
-        int rport;
-
-        switch (rp->ai_addr->sa_family) {
-            case AF_INET: {
-                struct sockaddr_in *sin = (struct sockaddr_in *) rp->ai_addr;
-                inet_ntop(AF_INET, &sin->sin_addr, rip, INET_ADDRSTRLEN);
-                rport = ntohs(sin->sin_port);
-                fd = connect_to_socket4(rip, rport, timeout);
-                break;
-            }
-
-            case AF_INET6: {
-                struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) rp->ai_addr;
-                inet_ntop(AF_INET6, &sin6->sin6_addr, rip, INET6_ADDRSTRLEN);
-                rport = ntohs(sin6->sin6_port);
-                fd = connect_to_socket6(rip, rport, timeout);
-                break;
-            }
-        }
-    }
-
-    freeaddrinfo(result);
-
-    return fd;
-}
-
 static inline calculated_number backend_calculate_value_from_stored_data(RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
     time_t first_t = rrdset_first_entry_t(st);
     time_t last_t = rrdset_last_entry_t(st);
@@ -482,7 +322,7 @@ void *backends_main(void *ptr) {
                 char buf[e - s + 1];
                 strncpyz(buf, s, e - s);
                 chart_backend_reconnects++;
-                sock = connect_to_one(buf, default_port, &timeout);
+                sock = connect_to(buf, default_port, &timeout);
                 if(sock != -1) break;
                 s = e;
             }
index 26fd61d68611c5032c233263c09cc6227e585640..d0084f6c0045754f09d517e69c5b1a1f8da27fce 100644 (file)
@@ -60,6 +60,8 @@
 #include <netdb.h>
 #endif
 
+#include <net/if.h>
+
 #include <poll.h>
 #include <signal.h>
 #include <syslog.h>
 #include "plugin_tc.h"
 #include "plugins_d.h"
 
+#include "socket.h"
+
 #include "eval.h"
 #include "health.h"
 
index b7323fcf47bd5e73ebe3542e32ddb378ff10f536..92ffe6f1edcb8c9b92d571ae7d0ef0e863934834 100644 (file)
--- a/src/log.h
+++ b/src/log.h
@@ -25,6 +25,7 @@
 #define D_REGISTRY          0x00200000
 #define D_VARIABLES         0x00400000
 #define D_HEALTH            0x00800000
+#define D_CONNECT_TO        0x01000000
 #define D_SYSTEM            0x80000000
 
 //#define DEBUG (D_WEB_CLIENT_ACCESS|D_LISTENER|D_RRD_STATS)
diff --git a/src/socket.c b/src/socket.c
new file mode 100644 (file)
index 0000000..6d60be7
--- /dev/null
@@ -0,0 +1,177 @@
+#include "common.h"
+
+// connect_to()
+//
+// definition format:
+//
+//    [PROTOCOL:]IP[%INTERFACE][:PORT]
+//
+// PROTOCOL  = tcp or udp
+// IP        = IPv4 or IPv6 IP or hostname, optionaly enclosed in [] (required for IPv6)
+// INTERFACE = for IPv6 only, the network interface to use
+// PORT      = port number or service name
+
+int connect_to(const char *definition, int default_port, struct timeval *timeout) {
+    struct addrinfo hints;
+    struct addrinfo *ai_head = NULL, *ai = NULL;
+
+    char buffer[strlen(definition) + 1];
+    strcpy(buffer, definition);
+
+    char default_service[10 + 1];
+    snprintfz(default_service, 10, "%d", default_port);
+
+    char *host = buffer, *service = default_service, *interface = "";
+    int protocol = 0;
+    uint32_t scope_id = 0;
+
+    if(strncmp(host, "tcp:", 4) == 0) {
+        host += 4;
+        protocol = IPPROTO_TCP;
+    }
+    else if(strncmp(host, "udp:", 4) == 0) {
+        host += 4;
+        protocol = IPPROTO_UDP;
+    }
+
+    char *e = host;
+    if(*e == '[') {
+        e = ++host;
+        while(*e && *e != ']') e++;
+        if(*e == ']') {
+            *e = '\0';
+            e++;
+        }
+    }
+    else {
+        while(*e && *e != ':' && *e != '%') e++;
+    }
+
+    if(*e == '%') {
+        *e = '\0';
+        e++;
+        interface = e;
+        while(*e && *e != ':') e++;
+    }
+
+    if(*e == ':') {
+        *e = '\0';
+        e++;
+        service = e;
+    }
+
+    debug(D_CONNECT_TO, "Attempting connection to host = '%s', service = '%s', interface = '%s', protocol = %d (tcp = %d, udp = %d)", host, service, interface, protocol, IPPROTO_TCP, IPPROTO_UDP);
+
+    if(!*host) {
+        error("Definition '%s' does not specify a host.", definition);
+        return -1;
+    }
+
+    if(*interface) {
+        scope_id = if_nametoindex(interface);
+        if(!scope_id)
+            error("Cannot find a network interface named '%s'. Continuing with limiting the network interface", interface);
+    }
+
+    if(!*service)
+        service = default_service;
+
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family   = PF_UNSPEC;   /* Allow IPv4 or IPv6 */
+    hints.ai_socktype = SOCK_DGRAM;  /* Datagram socket */
+    hints.ai_protocol = protocol;    /* The required protocol */
+
+    int ai_err = getaddrinfo(host, service, &hints, &ai_head);
+    if (ai_err != 0) {
+        error("Cannot resolve host '%s', port '%s': %s\n", host, service, gai_strerror(ai_err));
+        return -1;
+    }
+
+    int fd = -1;
+    for (ai = ai_head; ai != NULL && fd == -1; ai = ai->ai_next) {
+
+        if (ai->ai_family == PF_INET6) {
+            struct sockaddr_in6 *pSadrIn6 = (struct sockaddr_in6 *) ai->ai_addr;
+            if(pSadrIn6->sin6_scope_id == 0) {
+                pSadrIn6->sin6_scope_id = scope_id;
+            }
+        }
+
+        char hostBfr[NI_MAXHOST + 1];
+        char servBfr[NI_MAXSERV + 1];
+
+        getnameinfo(ai->ai_addr,
+                ai->ai_addrlen,
+                hostBfr,
+                sizeof(hostBfr),
+                servBfr,
+                sizeof(servBfr),
+                NI_NUMERICHOST | NI_NUMERICSERV);
+
+        debug(D_CONNECT_TO, "Address info: host = '%s', service = '%s', ai_flags = 0x%02X, ai_family = %d (PF_INET = %d, PF_INET6 = %d), ai_socktype = %d (SOCK_STREAM = %d, SOCK_DGRAM = %d), ai_protocol = %d (IPPROTO_TCP = %d, IPPROTO_UDP = %d), ai_addrlen = %lu (sockaddr_in = %lu, sockaddr_in6 = %lu)",
+                hostBfr,
+                servBfr,
+                (unsigned int)ai->ai_flags,
+                ai->ai_family,
+                PF_INET,
+                PF_INET6,
+                ai->ai_socktype,
+                SOCK_STREAM,
+                SOCK_DGRAM,
+                ai->ai_protocol,
+                IPPROTO_TCP,
+                IPPROTO_UDP,
+                (unsigned long)ai->ai_addrlen,
+                (unsigned long)sizeof(struct sockaddr_in),
+                (unsigned long)sizeof(struct sockaddr_in6));
+
+        switch (ai->ai_addr->sa_family) {
+            case PF_INET: {
+                struct sockaddr_in *pSadrIn = (struct sockaddr_in *)ai->ai_addr;
+                debug(D_CONNECT_TO, "ai_addr = sin_family: %d (AF_INET = %d, AF_INET6 = %d), sin_addr: '%s', sin_port: '%s'",
+                        pSadrIn->sin_family,
+                        AF_INET,
+                        AF_INET6,
+                        hostBfr,
+                        servBfr);
+                break;
+            }
+
+            case PF_INET6: {
+                struct sockaddr_in6 *pSadrIn6 = (struct sockaddr_in6 *) ai->ai_addr;
+                debug(D_CONNECT_TO,"ai_addr = sin6_family: %d (AF_INET = %d, AF_INET6 = %d), sin6_addr: '%s', sin6_port: '%s', sin6_flowinfo: %u, sin6_scope_id: %u",
+                        pSadrIn6->sin6_family,
+                        AF_INET,
+                        AF_INET6,
+                        hostBfr,
+                        servBfr,
+                        pSadrIn6->sin6_flowinfo,
+                        pSadrIn6->sin6_scope_id);
+                break;
+            }
+
+            default: {
+                debug(D_CONNECT_TO, "Unknown protocol family %d.", ai->ai_family);
+                continue;
+            }
+        }
+
+        fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+        if(fd != -1) {
+            if(setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)timeout, sizeof(struct timeval)) < 0)
+                error("Failed to set timeout on the socket to ip '%s' port '%s'", hostBfr, servBfr);
+
+            if(connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
+                error("Failed to connect to '%s', port '%s'", hostBfr, servBfr);
+                close(fd);
+                fd = -1;
+            }
+
+            debug(D_CONNECT_TO, "Connected to '%s' on port '%s'.", hostBfr, servBfr);
+        }
+    }
+
+    freeaddrinfo(ai_head);
+
+    return fd;
+}
diff --git a/src/socket.h b/src/socket.h
new file mode 100644 (file)
index 0000000..791c0ce
--- /dev/null
@@ -0,0 +1,10 @@
+//
+// Created by costa on 24/12/2016.
+//
+
+#ifndef NETDATA_SOCKET_H
+#define NETDATA_SOCKET_H
+
+extern int connect_to(const char *definition, int default_port, struct timeval *timeout);
+
+#endif //NETDATA_SOCKET_H