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