]> arthur.barton.de Git - netdata.git/blob - src/web_server.c
added support for binding to a configured IP address (IPv6 or IPv4)
[netdata.git] / src / web_server.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 #include <errno.h>
11 #include <pthread.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <netinet/tcp.h>
15 #include <malloc.h>
16
17 #include "common.h"
18 #include "log.h"
19 #include "appconfig.h"
20 #include "url.h"
21 #include "web_buffer.h"
22 #include "web_client.h"
23 #include "web_server.h"
24 #include "global_statistics.h"
25 #include "rrd.h"
26 #include "rrd2json.h"
27
28 int listen_backlog = LISTEN_BACKLOG;
29
30 int listen_fd = -1;
31 int listen_port = LISTEN_PORT;
32
33 #ifdef NETDATA_INTERNAL_CHECKS
34 static void log_allocations(void)
35 {
36         static int mem = 0;
37
38         struct mallinfo mi;
39
40         mi = mallinfo();
41         if(mi.uordblks > mem) {
42                 int clients = 0;
43                 struct web_client *w;
44                 for(w = web_clients; w ; w = w->next) clients++;
45
46                 info("Allocated memory increased from %d to %d (increased by %d bytes). There are %d web clients connected.", mem, mi.uordblks, mi.uordblks - mem, clients);
47                 mem = mi.uordblks;
48         }
49 }
50 #endif
51
52 static int is_ip_anything(const char *ip)
53 {
54         if(!ip || !*ip
55                         || !strcmp(ip, "any")
56                         || !strcmp(ip, "all")
57                         || !strcmp(ip, "*")
58                         || !strcmp(ip, "::")
59                         || !strcmp(ip, "0.0.0.0")
60                         ) return 1;
61
62         return 0;
63 }
64
65 int create_listen_socket4(const char *ip, int port, int listen_backlog)
66 {
67         int sock;
68         int sockopt = 1;
69         struct sockaddr_in name;
70
71         debug(D_LISTENER, "IPv4 creating new listening socket on port %d", port);
72
73         sock = socket(AF_INET, SOCK_STREAM, 0);
74         if(sock < 0) {
75                 error("IPv4 socket() failed.");
76                 return -1;
77         }
78
79         /* avoid "address already in use" */
80         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
81
82         memset(&name, 0, sizeof(struct sockaddr_in));
83         name.sin_family = AF_INET;
84         name.sin_port = htons (port);
85
86         if(is_ip_anything(ip)) {
87                 name.sin_addr.s_addr = htonl(INADDR_ANY);
88                 info("Listening on any IPs (IPv4).");
89         }
90         else {
91                 int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
92                 if(ret != 1) {
93                         error("Failed to convert IP '%s' to a valid IPv4 address.", ip);
94                         close(sock);
95                         return -1;
96                 }
97                 info("Listening on IP '%s' (IPv4).", ip);
98         }
99
100         if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
101                 close(sock);
102                 error("IPv4 bind() failed.");
103                 return -1;
104         }
105
106         if(listen(sock, listen_backlog) < 0) {
107                 close(sock);
108                 fatal("IPv4 listen() failed.");
109                 return -1;
110         }
111
112         debug(D_LISTENER, "IPv4 listening port %d created", port);
113         return sock;
114 }
115
116 int create_listen_socket6(const char *ip, int port, int listen_backlog)
117 {
118         int sock = -1;
119         int sockopt = 1;
120         struct sockaddr_in6 name;
121
122         debug(D_LISTENER, "IPv6 creating new listening socket on port %d", port);
123
124         sock = socket(AF_INET6, SOCK_STREAM, 0);
125         if (sock < 0) {
126                 error("IPv6 socket() failed. Disabling IPv6.");
127                 return -1;
128         }
129
130         /* avoid "address already in use" */
131         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
132
133         memset(&name, 0, sizeof(struct sockaddr_in6));
134         name.sin6_family = AF_INET6;
135         name.sin6_port = htons ((uint16_t) port);
136
137         if(is_ip_anything(ip)) {
138                 name.sin6_addr = in6addr_any;
139                 info("Listening on all IPs (IPv6 and IPv4)");
140         }
141         else {
142                 int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
143                 if(ret != 1) {
144                         error("Failed to convert IP '%s' to a valid IPv6 address. Disabling IPv6.", ip);
145                         close(sock);
146                         return -1;
147                 }
148                 info("Listening on IP '%s' (IPv6)", ip);
149         }
150
151         name.sin6_scope_id = 0;
152
153         if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
154                 close(sock);
155                 error("IPv6 bind() failed. Disabling IPv6.");
156                 return -1;
157         }
158
159         if (listen(sock, listen_backlog) < 0) {
160                 close(sock);
161                 error("IPv6 listen() failed. Disabling IPv6.");
162                 return -1;
163         }
164
165         debug(D_LISTENER, "IPv6 listening port %d created", port);
166         return sock;
167 }
168
169
170 // --------------------------------------------------------------------------------------
171 // the main socket listener
172
173 // 1. it accepts new incoming requests on our port
174 // 2. creates a new web_client for each connection received
175 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
176 // 4. cleans up old web_clients that their pthreads have been exited
177
178 void *socket_listen_main(void *ptr)
179 {
180         if(ptr) { ; }
181
182         info("WEB SERVER thread created with task id %d", gettid());
183
184         struct web_client *w;
185         struct timeval tv;
186         int retval;
187
188         if(ptr) { ; }
189
190         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
191                 error("Cannot set pthread cancel type to DEFERRED.");
192
193         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
194                 error("Cannot set pthread cancel state to ENABLE.");
195
196         web_client_timeout = (int) config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
197         web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
198
199         if(listen_fd < 0) fatal("LISTENER: Listen socket is not ready.");
200
201         fd_set ifds, ofds, efds;
202         int fdmax = listen_fd;
203
204         FD_ZERO (&ifds);
205         FD_ZERO (&ofds);
206         FD_ZERO (&efds);
207
208         for(;;) {
209                 tv.tv_sec = 0;
210                 tv.tv_usec = 200000;
211
212                 if(listen_fd >= 0) {
213                         FD_SET(listen_fd, &ifds);
214                         FD_SET(listen_fd, &efds);
215                 }
216
217                 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
218                 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
219
220                 if(retval == -1) {
221                         error("LISTENER: select() failed.");
222                         continue;
223                 }
224                 else if(retval) {
225                         // check for new incoming connections
226                         if(FD_ISSET(listen_fd, &ifds)) {
227                                 w = web_client_create(listen_fd);
228                                 if(unlikely(!w)) {
229                                         // no need for error log - web_client_create already logged the error
230                                         continue;
231                                 }
232
233                                 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
234                                         error("%llu: failed to create new thread for web client.");
235                                         w->obsolete = 1;
236                                 }
237                                 else if(pthread_detach(w->thread) != 0) {
238                                         error("%llu: Cannot request detach of newly created web client thread.", w->id);
239                                         w->obsolete = 1;
240                                 }
241                         }
242                         else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
243
244                 }
245                 //else {
246                 //      debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
247                 //}
248
249                 // cleanup unused clients
250                 for(w = web_clients; w ; w = w?w->next:NULL) {
251                         if(w->obsolete) {
252                                 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
253                                 // pthread_join(w->thread,  NULL);
254                                 w = web_client_free(w);
255 #ifdef NETDATA_INTERNAL_CHECKS
256                                 log_allocations();
257 #endif
258                         }
259                 }
260         }
261
262         error("LISTENER: exit!");
263
264         if(listen_fd >= 0) close(listen_fd);
265         exit(2);
266
267         return NULL;
268 }
269