7 #include <sys/socket.h>
8 #include <netinet/in.h>
14 #include <netinet/tcp.h>
20 #include "appconfig.h"
22 #include "web_buffer.h"
23 #include "web_client.h"
24 #include "web_server.h"
25 #include "global_statistics.h"
28 #include "../config.h"
30 int listen_backlog = LISTEN_BACKLOG;
32 int listen_port = LISTEN_PORT;
33 int web_server_mode = WEB_SERVER_MODE_MULTI_THREADED;
35 #ifdef NETDATA_INTERNAL_CHECKS
36 static void log_allocations(void)
43 if(mi.uordblks > mem) {
46 for(w = web_clients; w ; w = w->next) clients++;
48 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);
55 int accept4(int sock, struct sockaddr *addr, socklen_t *addrlen, int flags) {
56 int fd = accept(sock, addr, addrlen);
59 if (fd < 0) return fd;
61 if (flags & SOCK_NONBLOCK) {
62 newflags |= O_NONBLOCK;
63 flags &= ~SOCK_NONBLOCK;
66 if (flags & SOCK_CLOEXEC) {
67 newflags |= O_CLOEXEC;
68 flags &= ~SOCK_CLOEXEC;
76 if (fcntl(fd, F_SETFL, newflags) < 0) {
77 int saved_errno = errno;
87 static int is_ip_anything(const char *ip)
94 || !strcmp(ip, "0.0.0.0")
100 int create_listen_socket4(const char *ip, int port, int listen_backlog)
105 debug(D_LISTENER, "IPv4 creating new listening socket on port %d", port);
107 sock = socket(AF_INET, SOCK_STREAM, 0);
109 error("IPv4 socket() failed.");
113 /* avoid "address already in use" */
114 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
116 struct sockaddr_in name;
117 memset(&name, 0, sizeof(struct sockaddr_in));
118 name.sin_family = AF_INET;
119 name.sin_port = htons (port);
121 if(is_ip_anything(ip)) {
122 name.sin_addr.s_addr = htonl(INADDR_ANY);
123 // info("Listening on any IPs (IPv4).");
126 int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
128 error("Failed to convert IP '%s' to a valid IPv4 address.", ip);
132 // info("Listening on IP '%s' (IPv4).", ip);
135 if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
137 error("IPv4 bind() failed.");
141 if(listen(sock, listen_backlog) < 0) {
143 fatal("IPv4 listen() failed.");
147 debug(D_LISTENER, "IPv4 listening port %d created", port);
151 int create_listen_socket6(const char *ip, int port, int listen_backlog)
156 debug(D_LISTENER, "IPv6 creating new listening socket on port %d", port);
158 sock = socket(AF_INET6, SOCK_STREAM, 0);
160 error("IPv6 socket() failed. Disabling IPv6.");
164 /* avoid "address already in use" */
165 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
167 struct sockaddr_in6 name;
168 memset(&name, 0, sizeof(struct sockaddr_in6));
169 name.sin6_family = AF_INET6;
170 name.sin6_port = htons ((uint16_t) port);
172 if(is_ip_anything(ip)) {
173 name.sin6_addr = in6addr_any;
174 // info("Listening on all IPs (IPv6 and IPv4)");
177 int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
179 error("Failed to convert IP '%s' to a valid IPv6 address. Disabling IPv6.", ip);
183 // info("Listening on IP '%s' (IPv6)", ip);
186 name.sin6_scope_id = 0;
188 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
190 error("IPv6 bind() failed. Disabling IPv6.");
194 if (listen(sock, listen_backlog) < 0) {
196 error("IPv6 listen() failed. Disabling IPv6.");
200 debug(D_LISTENER, "IPv6 listening port %d created", port);
205 int create_listen_socket(void) {
206 listen_backlog = (int) config_get_number("global", "http port listen backlog", LISTEN_BACKLOG);
208 listen_port = (int) config_get_number("global", "port", LISTEN_PORT);
209 if(listen_port < 1 || listen_port > 65535) {
210 error("Invalid listen port %d given. Defaulting to %d.", listen_port, LISTEN_PORT);
211 listen_port = LISTEN_PORT;
213 else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
216 char *ipv = config_get("global", "ip version", "any");
217 if(!strcmp(ipv, "any") || !strcmp(ipv, "both") || !strcmp(ipv, "all")) ip = 0;
218 else if(!strcmp(ipv, "ipv4") || !strcmp(ipv, "IPV4") || !strcmp(ipv, "IPv4") || !strcmp(ipv, "4")) ip = 4;
219 else if(!strcmp(ipv, "ipv6") || !strcmp(ipv, "IPV6") || !strcmp(ipv, "IPv6") || !strcmp(ipv, "6")) ip = 6;
220 else error("Cannot understand ip version '%s'. Assuming 'any'.", ipv);
222 if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(config_get("global", "bind socket to IP", "*"), listen_port, listen_backlog);
224 listen_fd = create_listen_socket4(config_get("global", "bind socket to IP", "*"), listen_port, listen_backlog);
225 // if(listen_fd >= 0 && ip != 4) info("Managed to open an IPv4 socket on port %d.", listen_port);
231 // --------------------------------------------------------------------------------------
232 // the main socket listener
234 // 1. it accepts new incoming requests on our port
235 // 2. creates a new web_client for each connection received
236 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
237 // 4. cleans up old web_clients that their pthreads have been exited
239 #define CLEANUP_EVERY_EVENTS 100
241 void *socket_listen_main_multi_threaded(void *ptr) {
244 web_server_mode = WEB_SERVER_MODE_MULTI_THREADED;
245 info("Multi-threaded WEB SERVER thread created with task id %d", gettid());
247 struct web_client *w;
248 int retval, failures = 0, counter = 0;
250 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
251 error("Cannot set pthread cancel type to DEFERRED.");
253 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
254 error("Cannot set pthread cancel state to ENABLE.");
257 fatal("LISTENER: Listen socket %d is not ready, or invalid.", listen_fd);
260 struct pollfd fd = { .fd = listen_fd, .events = POLLIN, .revents = 0 };
261 int timeout = 10 * 1000;
263 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
264 retval = poll(&fd, 1, timeout);
266 if(unlikely(retval == -1)) {
267 debug(D_WEB_CLIENT, "LISTENER: poll() failed.");
271 error("LISTENER: our listen port %d seems dead. Re-opening it.", listen_fd);
275 create_listen_socket();
277 fatal("Cannot listen for web clients (connected clients %llu).", global_statistics.connected_clients);
284 else if(likely(retval)) {
285 // check for new incoming connections
286 if(fd.revents & POLLIN || fd.revents & POLLPRI) {
287 w = web_client_create(listen_fd);
289 // no need for error log - web_client_create already logged the error
293 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
294 error("%llu: failed to create new thread for web client.");
297 else if(pthread_detach(w->thread) != 0) {
298 error("%llu: Cannot request detach of newly created web client thread.", w->id);
304 debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
309 debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
310 counter = CLEANUP_EVERY_EVENTS;
313 // cleanup unused clients
315 if(counter > CLEANUP_EVERY_EVENTS) {
317 for (w = web_clients; w;) {
319 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
320 // pthread_cancel(w->thread);
321 // pthread_join(w->thread, NULL);
322 w = web_client_free(w);
323 #ifdef NETDATA_INTERNAL_CHECKS
334 debug(D_WEB_CLIENT, "LISTENER: exit!");
340 struct web_client *single_threaded_clients[FD_SETSIZE];
342 static inline int single_threaded_link_client(struct web_client *w, fd_set *ifds, fd_set *ofds, fd_set *efds, int *max) {
343 if(unlikely(w->obsolete || w->dead || (!w->wait_receive && !w->wait_send)))
346 if(unlikely(w->ifd < 0 || w->ifd >= FD_SETSIZE || w->ofd < 0 || w->ofd >= FD_SETSIZE)) {
347 error("%llu: invalid file descriptor, ifd = %d, ofd = %d (required 0 <= fd < FD_SETSIZE (%d)", w->id, w->ifd, w->ofd, FD_SETSIZE);
351 FD_SET(w->ifd, efds);
352 if(unlikely(*max < w->ifd)) *max = w->ifd;
354 if(unlikely(w->ifd != w->ofd)) {
355 if(*max < w->ofd) *max = w->ofd;
356 FD_SET(w->ofd, efds);
359 if(w->wait_receive) FD_SET(w->ifd, ifds);
360 if(w->wait_send) FD_SET(w->ofd, ofds);
362 single_threaded_clients[w->ifd] = w;
363 single_threaded_clients[w->ofd] = w;
368 static inline int single_threaded_unlink_client(struct web_client *w, fd_set *ifds, fd_set *ofds, fd_set *efds) {
369 FD_CLR(w->ifd, efds);
370 if(unlikely(w->ifd != w->ofd)) FD_CLR(w->ofd, efds);
372 if(w->wait_receive) FD_CLR(w->ifd, ifds);
373 if(w->wait_send) FD_CLR(w->ofd, ofds);
375 single_threaded_clients[w->ifd] = NULL;
376 single_threaded_clients[w->ofd] = NULL;
378 if(unlikely(w->obsolete || w->dead || (!w->wait_receive && !w->wait_send)))
384 void *socket_listen_main_single_threaded(void *ptr) {
387 web_server_mode = WEB_SERVER_MODE_SINGLE_THREADED;
389 info("Single threaded WEB SERVER thread created with task id %d", gettid());
391 struct web_client *w;
392 int retval, failures = 0;
396 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
397 error("Cannot set pthread cancel type to DEFERRED.");
399 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
400 error("Cannot set pthread cancel state to ENABLE.");
402 if(listen_fd < 0 || listen_fd >= FD_SETSIZE)
403 fatal("LISTENER: Listen socket %d is not ready, or invalid.", listen_fd);
406 for(i = 0; i < FD_SETSIZE ; i++)
407 single_threaded_clients[i] = NULL;
409 fd_set ifds, ofds, efds, rifds, rofds, refds;
413 FD_SET(listen_fd, &ifds);
414 FD_SET(listen_fd, &efds);
415 int fdmax = listen_fd;
418 debug(D_WEB_CLIENT_ACCESS, "LISTENER: single threaded web server waiting (listen fd = %d, fdmax = %d)...", listen_fd, fdmax);
420 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
424 retval = select(fdmax+1, &rifds, &rofds, &refds, &tv);
426 if(unlikely(retval == -1)) {
427 debug(D_WEB_CLIENT, "LISTENER: select() failed.");
430 if(global_statistics.connected_clients) {
431 error("REMOVING ALL %lu WEB CLIENTS !", global_statistics.connected_clients);
432 while (web_clients) {
433 single_threaded_unlink_client(web_clients, &ifds, &ofds, &efds);
434 web_client_free(web_clients);
438 error("LISTENER: our listen port %d seems dead. Re-opening it.", listen_fd);
444 create_listen_socket();
445 if(listen_fd < 0 || listen_fd >= FD_SETSIZE)
446 fatal("Cannot listen for web clients (connected clients %llu).", global_statistics.connected_clients);
451 FD_SET(listen_fd, &ifds);
452 FD_SET(listen_fd, &efds);
456 else if(likely(retval)) {
458 debug(D_WEB_CLIENT_ACCESS, "LISTENER: got something.");
460 if(FD_ISSET(listen_fd, &rifds)) {
461 debug(D_WEB_CLIENT_ACCESS, "LISTENER: new connection.");
462 w = web_client_create(listen_fd);
463 if(single_threaded_link_client(w, &ifds, &ofds, &ifds, &fdmax) != 0) {
468 for(i = 0 ; i <= fdmax ; i++) {
469 if(likely(!FD_ISSET(i, &rifds) && !FD_ISSET(i, &rofds) && !FD_ISSET(i, &refds)))
472 w = single_threaded_clients[i];
476 if(unlikely(single_threaded_unlink_client(w, &ifds, &ofds, &efds) != 0)) {
481 if (unlikely(FD_ISSET(w->ifd, &refds) || FD_ISSET(w->ofd, &refds))) {
486 if (unlikely(w->wait_receive && FD_ISSET(w->ifd, &rifds))) {
487 if (unlikely(web_client_receive(w) < 0)) {
492 if (w->mode != WEB_CLIENT_MODE_FILECOPY) {
493 debug(D_WEB_CLIENT, "%llu: Processing received data.", w->id);
494 web_client_process(w);
498 if (unlikely(w->wait_send && FD_ISSET(w->ofd, &rofds))) {
499 if (unlikely(web_client_send(w) < 0)) {
500 debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client.", w->id);
506 if(unlikely(single_threaded_link_client(w, &ifds, &ofds, &efds, &fdmax) != 0)) {
512 debug(D_WEB_CLIENT_ACCESS, "LISTENER: single threaded web server timeout.");
513 #ifdef NETDATA_INTERNAL_CHECKS
519 debug(D_WEB_CLIENT, "LISTENER: exit!");