@cryptotaxi247 / netdata-1 / commits / 57138feab

re-worked connect_to() to properly support IPv6; #1432

Costa Tsaousis (ktsaou) committed Dec 24, 2016 at 17:25 UTC 57138feab40021a8e9578b42a8bf871f9f13ab17
7 files changed +195 -162
CMakeLists.txt
+1 -1
@@ -94,7 +94,7 @@ set(NETDATA_SOURCE_FILES
94 src/web_client.h
95 src/web_server.c
96 src/web_server.h
97 -)
97 + src/socket.c src/socket.h)
98
99 set(APPS_PLUGIN_SOURCE_FILES
100 src/appconfig.c
src/Makefile.am
+1
@@ -45,6 +45,7 @@ netdata_SOURCES = \
45 plugin_tc.c plugin_tc.h \
46 plugins_d.c plugins_d.h \
47 popen.c popen.h \
48 + socket.c socket.h \
49 sys_fs_cgroup.c \
50 procfile.c procfile.h \
51 proc_self_mountinfo.c proc_self_mountinfo.h \
src/backends.c
+1 -161
@@ -4,166 +4,6 @@
4 #define BACKEND_SOURCE_DATA_AVERAGE 0x00000002
5 #define BACKEND_SOURCE_DATA_SUM 0x00000004
6
7 -int connect_to_socket4(const char *ip, int port, struct timeval *timeout) {
8 - int sock;
9 -
10 - debug(D_LISTENER, "IPv4 connecting to ip '%s' port %d", ip, port);
11 -
12 - sock = socket(AF_INET, SOCK_STREAM, 0);
13 - if(sock < 0) {
14 - error("IPv4 socket() on ip '%s' port %d failed.", ip, port);
15 - return -1;
16 - }
17 -
18 - if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)timeout, sizeof(struct timeval)) < 0)
19 - error("Failed to set timeout on the socket to ip '%s' port %d", ip, port);
20 -
21 - struct sockaddr_in name;
22 - memset(&name, 0, sizeof(struct sockaddr_in));
23 - name.sin_family = AF_INET;
24 - name.sin_port = htons(port);
25 -
26 - int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
27 - if(ret != 1) {
28 - error("Failed to convert '%s' to a valid IPv4 address.", ip);
29 - close(sock);
30 - return -1;
31 - }
32 -
33 - if(connect(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
34 - close(sock);
35 - error("IPv4 failed to connect to '%s', port %d", ip, port);
36 - return -1;
37 - }
38 -
39 - debug(D_LISTENER, "Connected to IPv4 ip '%s' port %d", ip, port);
40 - return sock;
41 -}
42 -
43 -int connect_to_socket6(const char *ip, int port, struct timeval *timeout) {
44 - int sock = -1;
45 - int ipv6only = 1;
46 -
47 - debug(D_LISTENER, "IPv6 connecting to ip '%s' port %d", ip, port);
48 -
49 - sock = socket(AF_INET6, SOCK_STREAM, 0);
50 - if (sock < 0) {
51 - error("IPv6 socket() on ip '%s' port %d failed.", ip, port);
52 - return -1;
53 - }
54 -
55 - if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)timeout, sizeof(struct timeval)) < 0)
56 - error("Failed to set timeout on the socket to ip '%s' port %d", ip, port);
57 -
58 - /* IPv6 only */
59 - if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&ipv6only, sizeof(ipv6only)) != 0)
60 - error("Cannot set IPV6_V6ONLY on ip '%s' port's %d.", ip, port);
61 -
62 - struct sockaddr_in6 name;
63 - memset(&name, 0, sizeof(struct sockaddr_in6));
64 - name.sin6_family = AF_INET6;
65 - name.sin6_port = htons ((uint16_t) port);
66 -
67 - int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
68 - if(ret != 1) {
69 - error("Failed to convert IP '%s' to a valid IPv6 address.", ip);
70 - close(sock);
71 - return -1;
72 - }
73 -
74 - name.sin6_scope_id = 0;
75 -
76 - if(connect(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
77 - close(sock);
78 - error("IPv6 failed to connect to '%s', port %d", ip, port);
79 - return -1;
80 - }
81 -
82 - debug(D_LISTENER, "Connected to IPv6 ip '%s' port %d", ip, port);
83 - return sock;
84 -}
85 -
86 -
87 -static inline int connect_to_one(const char *definition, int default_port, struct timeval *timeout) {
88 - struct addrinfo hints;
89 - struct addrinfo *result = NULL, *rp = NULL;
90 -
91 - char buffer[strlen(definition) + 1];
92 - strcpy(buffer, definition);
93 -
94 - char buffer2[10 + 1];
95 - snprintfz(buffer2, 10, "%d", default_port);
96 -
97 - char *ip = buffer, *port = buffer2;
98 -
99 - char *e = ip;
100 - if(*e == '[') {
101 - e = ++ip;
102 - while(*e && *e != ']') e++;
103 - if(*e == ']') {
104 - *e = '\0';
105 - e++;
106 - }
107 - }
108 - else {
109 - while(*e && *e != ':') e++;
110 - }
111 -
112 - if(*e == ':') {
113 - port = e + 1;
114 - *e = '\0';
115 - }
116 -
117 - if(!*ip)
118 - return -1;
119 -
120 - if(!*port)
121 - port = buffer2;
122 -
123 - memset(&hints, 0, sizeof(struct addrinfo));
124 - hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
125 - hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
126 - hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
127 - hints.ai_protocol = 0; /* Any protocol */
128 - hints.ai_canonname = NULL;
129 - hints.ai_addr = NULL;
130 - hints.ai_next = NULL;
131 -
132 - int r = getaddrinfo(ip, port, &hints, &result);
133 - if (r != 0) {
134 - error("Cannot resolve host '%s', port '%s': %s\n", ip, port, gai_strerror(r));
135 - return -1;
136 - }
137 -
138 - int fd = -1;
139 - for (rp = result; rp != NULL && fd == -1; rp = rp->ai_next) {
140 - char rip[INET_ADDRSTRLEN + INET6_ADDRSTRLEN] = "INVALID";
141 - int rport;
142 -
143 - switch (rp->ai_addr->sa_family) {
144 - case AF_INET: {
145 - struct sockaddr_in *sin = (struct sockaddr_in *) rp->ai_addr;
146 - inet_ntop(AF_INET, &sin->sin_addr, rip, INET_ADDRSTRLEN);
147 - rport = ntohs(sin->sin_port);
148 - fd = connect_to_socket4(rip, rport, timeout);
149 - break;
150 - }
151 -
152 - case AF_INET6: {
153 - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) rp->ai_addr;
154 - inet_ntop(AF_INET6, &sin6->sin6_addr, rip, INET6_ADDRSTRLEN);
155 - rport = ntohs(sin6->sin6_port);
156 - fd = connect_to_socket6(rip, rport, timeout);
157 - break;
158 - }
159 - }
160 - }
161 -
162 - freeaddrinfo(result);
163 -
164 - return fd;
165 -}
166 -
7 static inline calculated_number backend_calculate_value_from_stored_data(RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
8 time_t first_t = rrdset_first_entry_t(st);
9 time_t last_t = rrdset_last_entry_t(st);
@@ -482,7 +322,7 @@ void *backends_main(void *ptr) {
322 char buf[e - s + 1];
323 strncpyz(buf, s, e - s);
324 chart_backend_reconnects++;
485 - sock = connect_to_one(buf, default_port, &timeout);
325 + sock = connect_to(buf, default_port, &timeout);
326 if(sock != -1) break;
327 s = e;
328 }
src/common.h
+4
@@ -60,6 +60,8 @@
60 #include <netdb.h>
61 #endif
62
63 +#include <net/if.h>
64 +
65 #include <poll.h>
66 #include <signal.h>
67 #include <syslog.h>
@@ -142,6 +144,8 @@
144 #include "plugin_tc.h"
145 #include "plugins_d.h"
146
147 +#include "socket.h"
148 +
149 #include "eval.h"
150 #include "health.h"
151
src/log.h
+1
@@ -25,6 +25,7 @@
25 #define D_REGISTRY 0x00200000
26 #define D_VARIABLES 0x00400000
27 #define D_HEALTH 0x00800000
28 +#define D_CONNECT_TO 0x01000000
29 #define D_SYSTEM 0x80000000
30
31 //#define DEBUG (D_WEB_CLIENT_ACCESS|D_LISTENER|D_RRD_STATS)
src/socket.c new
+177
@@ -0,0 +1,177 @@
1 +#include "common.h"
2 +
3 +// connect_to()
4 +//
5 +// definition format:
6 +//
7 +// [PROTOCOL:]IP[%INTERFACE][:PORT]
8 +//
9 +// PROTOCOL = tcp or udp
10 +// IP = IPv4 or IPv6 IP or hostname, optionaly enclosed in [] (required for IPv6)
11 +// INTERFACE = for IPv6 only, the network interface to use
12 +// PORT = port number or service name
13 +
14 +int connect_to(const char *definition, int default_port, struct timeval *timeout) {
15 + struct addrinfo hints;
16 + struct addrinfo *ai_head = NULL, *ai = NULL;
17 +
18 + char buffer[strlen(definition) + 1];
19 + strcpy(buffer, definition);
20 +
21 + char default_service[10 + 1];
22 + snprintfz(default_service, 10, "%d", default_port);
23 +
24 + char *host = buffer, *service = default_service, *interface = "";
25 + int protocol = 0;
26 + uint32_t scope_id = 0;
27 +
28 + if(strncmp(host, "tcp:", 4) == 0) {
29 + host += 4;
30 + protocol = IPPROTO_TCP;
31 + }
32 + else if(strncmp(host, "udp:", 4) == 0) {
33 + host += 4;
34 + protocol = IPPROTO_UDP;
35 + }
36 +
37 + char *e = host;
38 + if(*e == '[') {
39 + e = ++host;
40 + while(*e && *e != ']') e++;
41 + if(*e == ']') {
42 + *e = '\0';
43 + e++;
44 + }
45 + }
46 + else {
47 + while(*e && *e != ':' && *e != '%') e++;
48 + }
49 +
50 + if(*e == '%') {
51 + *e = '\0';
52 + e++;
53 + interface = e;
54 + while(*e && *e != ':') e++;
55 + }
56 +
57 + if(*e == ':') {
58 + *e = '\0';
59 + e++;
60 + service = e;
61 + }
62 +
63 + 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);
64 +
65 + if(!*host) {
66 + error("Definition '%s' does not specify a host.", definition);
67 + return -1;
68 + }
69 +
70 + if(*interface) {
71 + scope_id = if_nametoindex(interface);
72 + if(!scope_id)
73 + error("Cannot find a network interface named '%s'. Continuing with limiting the network interface", interface);
74 + }
75 +
76 + if(!*service)
77 + service = default_service;
78 +
79 + memset(&hints, 0, sizeof(struct addrinfo));
80 + hints.ai_family = PF_UNSPEC; /* Allow IPv4 or IPv6 */
81 + hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
82 + hints.ai_protocol = protocol; /* The required protocol */
83 +
84 + int ai_err = getaddrinfo(host, service, &hints, &ai_head);
85 + if (ai_err != 0) {
86 + error("Cannot resolve host '%s', port '%s': %s\n", host, service, gai_strerror(ai_err));
87 + return -1;
88 + }
89 +
90 + int fd = -1;
91 + for (ai = ai_head; ai != NULL && fd == -1; ai = ai->ai_next) {
92 +
93 + if (ai->ai_family == PF_INET6) {
94 + struct sockaddr_in6 *pSadrIn6 = (struct sockaddr_in6 *) ai->ai_addr;
95 + if(pSadrIn6->sin6_scope_id == 0) {
96 + pSadrIn6->sin6_scope_id = scope_id;
97 + }
98 + }
99 +
100 + char hostBfr[NI_MAXHOST + 1];
101 + char servBfr[NI_MAXSERV + 1];
102 +
103 + getnameinfo(ai->ai_addr,
104 + ai->ai_addrlen,
105 + hostBfr,
106 + sizeof(hostBfr),
107 + servBfr,
108 + sizeof(servBfr),
109 + NI_NUMERICHOST | NI_NUMERICSERV);
110 +
111 + 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)",
112 + hostBfr,
113 + servBfr,
114 + (unsigned int)ai->ai_flags,
115 + ai->ai_family,
116 + PF_INET,
117 + PF_INET6,
118 + ai->ai_socktype,
119 + SOCK_STREAM,
120 + SOCK_DGRAM,
121 + ai->ai_protocol,
122 + IPPROTO_TCP,
123 + IPPROTO_UDP,
124 + (unsigned long)ai->ai_addrlen,
125 + (unsigned long)sizeof(struct sockaddr_in),
126 + (unsigned long)sizeof(struct sockaddr_in6));
127 +
128 + switch (ai->ai_addr->sa_family) {
129 + case PF_INET: {
130 + struct sockaddr_in *pSadrIn = (struct sockaddr_in *)ai->ai_addr;
131 + debug(D_CONNECT_TO, "ai_addr = sin_family: %d (AF_INET = %d, AF_INET6 = %d), sin_addr: '%s', sin_port: '%s'",
132 + pSadrIn->sin_family,
133 + AF_INET,
134 + AF_INET6,
135 + hostBfr,
136 + servBfr);
137 + break;
138 + }
139 +
140 + case PF_INET6: {
141 + struct sockaddr_in6 *pSadrIn6 = (struct sockaddr_in6 *) ai->ai_addr;
142 + 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",
143 + pSadrIn6->sin6_family,
144 + AF_INET,
145 + AF_INET6,
146 + hostBfr,
147 + servBfr,
148 + pSadrIn6->sin6_flowinfo,
149 + pSadrIn6->sin6_scope_id);
150 + break;
151 + }
152 +
153 + default: {
154 + debug(D_CONNECT_TO, "Unknown protocol family %d.", ai->ai_family);
155 + continue;
156 + }
157 + }
158 +
159 + fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
160 + if(fd != -1) {
161 + if(setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)timeout, sizeof(struct timeval)) < 0)
162 + error("Failed to set timeout on the socket to ip '%s' port '%s'", hostBfr, servBfr);
163 +
164 + if(connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
165 + error("Failed to connect to '%s', port '%s'", hostBfr, servBfr);
166 + close(fd);
167 + fd = -1;
168 + }
169 +
170 + debug(D_CONNECT_TO, "Connected to '%s' on port '%s'.", hostBfr, servBfr);
171 + }
172 + }
173 +
174 + freeaddrinfo(ai_head);
175 +
176 + return fd;
177 +}
src/socket.h new
+10
@@ -0,0 +1,10 @@
1 +//
2 +// Created by costa on 24/12/2016.
3 +//
4 +
5 +#ifndef NETDATA_SOCKET_H
6 +#define NETDATA_SOCKET_H
7 +
8 +extern int connect_to(const char *definition, int default_port, struct timeval *timeout);
9 +
10 +#endif //NETDATA_SOCKET_H