7 #include <sys/socket.h>
8 #include <netinet/in.h>
14 #include <netinet/tcp.h>
19 #include "appconfig.h"
21 #include "web_buffer.h"
22 #include "web_client.h"
23 #include "web_server.h"
24 #include "global_statistics.h"
27 #include "../config.h"
29 int listen_backlog = LISTEN_BACKLOG;
32 int listen_port = LISTEN_PORT;
34 #ifdef NETDATA_INTERNAL_CHECKS
35 static void log_allocations(void)
42 if(mi.uordblks > mem) {
45 for(w = web_clients; w ; w = w->next) clients++;
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);
53 static int is_ip_anything(const char *ip)
60 || !strcmp(ip, "0.0.0.0")
66 int create_listen_socket4(const char *ip, int port, int listen_backlog)
71 debug(D_LISTENER, "IPv4 creating new listening socket on port %d", port);
73 sock = socket(AF_INET, SOCK_STREAM, 0);
75 error("IPv4 socket() failed.");
79 /* avoid "address already in use" */
80 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
82 struct sockaddr_in name;
83 memset(&name, 0, sizeof(struct sockaddr_in));
84 name.sin_family = AF_INET;
85 name.sin_port = htons (port);
87 if(is_ip_anything(ip)) {
88 name.sin_addr.s_addr = htonl(INADDR_ANY);
89 // info("Listening on any IPs (IPv4).");
92 int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
94 error("Failed to convert IP '%s' to a valid IPv4 address.", ip);
98 // info("Listening on IP '%s' (IPv4).", ip);
101 if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
103 error("IPv4 bind() failed.");
107 if(listen(sock, listen_backlog) < 0) {
109 fatal("IPv4 listen() failed.");
113 debug(D_LISTENER, "IPv4 listening port %d created", port);
117 int create_listen_socket6(const char *ip, int port, int listen_backlog)
122 debug(D_LISTENER, "IPv6 creating new listening socket on port %d", port);
124 sock = socket(AF_INET6, SOCK_STREAM, 0);
126 error("IPv6 socket() failed. Disabling IPv6.");
130 /* avoid "address already in use" */
131 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
133 struct sockaddr_in6 name;
134 memset(&name, 0, sizeof(struct sockaddr_in6));
135 name.sin6_family = AF_INET6;
136 name.sin6_port = htons ((uint16_t) port);
138 if(is_ip_anything(ip)) {
139 name.sin6_addr = in6addr_any;
140 // info("Listening on all IPs (IPv6 and IPv4)");
143 int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
145 error("Failed to convert IP '%s' to a valid IPv6 address. Disabling IPv6.", ip);
149 // info("Listening on IP '%s' (IPv6)", ip);
152 name.sin6_scope_id = 0;
154 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
156 error("IPv6 bind() failed. Disabling IPv6.");
160 if (listen(sock, listen_backlog) < 0) {
162 error("IPv6 listen() failed. Disabling IPv6.");
166 debug(D_LISTENER, "IPv6 listening port %d created", port);
171 int create_listen_socket(void) {
172 listen_backlog = (int) config_get_number("global", "http port listen backlog", LISTEN_BACKLOG);
174 listen_port = (int) config_get_number("global", "port", LISTEN_PORT);
175 if(listen_port < 1 || listen_port > 65535) {
176 error("Invalid listen port %d given. Defaulting to %d.", listen_port, LISTEN_PORT);
177 listen_port = LISTEN_PORT;
179 else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
182 char *ipv = config_get("global", "ip version", "any");
183 if(!strcmp(ipv, "any") || !strcmp(ipv, "both") || !strcmp(ipv, "all")) ip = 0;
184 else if(!strcmp(ipv, "ipv4") || !strcmp(ipv, "IPV4") || !strcmp(ipv, "IPv4") || !strcmp(ipv, "4")) ip = 4;
185 else if(!strcmp(ipv, "ipv6") || !strcmp(ipv, "IPV6") || !strcmp(ipv, "IPv6") || !strcmp(ipv, "6")) ip = 6;
186 else error("Cannot understand ip version '%s'. Assuming 'any'.", ipv);
188 if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(config_get("global", "bind socket to IP", "*"), listen_port, listen_backlog);
190 listen_fd = create_listen_socket4(config_get("global", "bind socket to IP", "*"), listen_port, listen_backlog);
191 // if(listen_fd >= 0 && ip != 4) info("Managed to open an IPv4 socket on port %d.", listen_port);
197 // --------------------------------------------------------------------------------------
198 // the main socket listener
200 // 1. it accepts new incoming requests on our port
201 // 2. creates a new web_client for each connection received
202 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
203 // 4. cleans up old web_clients that their pthreads have been exited
205 void *socket_listen_main_multi_threaded(void *ptr) {
208 info("Multi-threaded WEB SERVER thread created with task id %d", gettid());
210 struct web_client *w;
212 int retval, failures = 0;
216 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
217 error("Cannot set pthread cancel type to DEFERRED.");
219 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
220 error("Cannot set pthread cancel state to ENABLE.");
222 web_client_timeout = (int) config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
223 web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
225 if(listen_fd < 0) fatal("LISTENER: Listen socket is not ready.");
234 if(likely(listen_fd >= 0))
235 FD_SET(listen_fd, &ifds);
237 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
238 retval = select(listen_fd + 1, &ifds, NULL, NULL, &tv);
240 if(unlikely(retval == -1)) {
241 error("LISTENER: select() failed.");
245 error("LISTENER: our listen port %d seems dead. Re-opening it.", listen_fd);
249 create_listen_socket();
251 fatal("Cannot listen for web clients (connected clients %llu).", global_statistics.connected_clients);
258 else if(likely(retval)) {
259 // check for new incoming connections
260 if(likely(FD_ISSET(listen_fd, &ifds))) {
261 w = web_client_create(listen_fd);
263 // no need for error log - web_client_create already logged the error
267 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
268 error("%llu: failed to create new thread for web client.");
271 else if(pthread_detach(w->thread) != 0) {
272 error("%llu: Cannot request detach of newly created web client thread.", w->id);
276 else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
280 // debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
285 // cleanup unused clients
286 for (w = web_clients; w; ) {
288 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
289 // pthread_cancel(w->thread);
290 // pthread_join(w->thread, NULL);
291 w = web_client_free(w);
292 #ifdef NETDATA_INTERNAL_CHECKS
300 error("LISTENER: exit!");
302 if(listen_fd >= 0) close(listen_fd);
308 void *socket_listen_main_single_threaded(void *ptr) {
311 info("Single threaded WEB SERVER thread created with task id %d", gettid());
313 struct web_client *w;
314 int retval, failures = 0;
318 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
319 error("Cannot set pthread cancel type to DEFERRED.");
321 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
322 error("Cannot set pthread cancel state to ENABLE.");
324 web_client_timeout = (int) config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
325 web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
327 if(listen_fd < 0) fatal("LISTENER: Listen socket is not ready.");
329 fd_set ifds, ofds, efds;
330 int fdmax = listen_fd;
333 int has_obsolete = 0;
339 // debug(D_WEB_CLIENT_ACCESS, "LISTENER: adding listen socket %d to ifds, efds", listen_fd);
340 FD_SET(listen_fd, &ifds);
341 FD_SET(listen_fd, &efds);
344 for(w = web_clients; w ; w = w->next) {
345 if(unlikely(w->dead)) {
346 error("%llu: client is dead.");
349 else if(unlikely(!w->wait_receive && !w->wait_send)) {
350 error("%llu: client is not set for neither receiving nor sending data.");
354 if(unlikely(w->obsolete)) {
359 // debug(D_WEB_CLIENT_ACCESS, "%llu: adding input socket %d to efds", w->id, w->ifd);
360 FD_SET(w->ifd, &efds);
361 if(w->ifd > fdmax) fdmax = w->ifd;
363 if(w->ifd != w->ofd) {
364 // debug(D_WEB_CLIENT_ACCESS, "%llu: adding output socket %d to efds", w->id, w->ofd);
365 FD_SET(w->ofd, &efds);
366 if(w->ofd > fdmax) fdmax = w->ofd;
369 if (w->wait_receive) {
370 // debug(D_WEB_CLIENT_ACCESS, "%llu: adding input socket %d to ifds", w->id, w->ifd);
371 FD_SET(w->ifd, &ifds);
372 if(w->ifd > fdmax) fdmax = w->ifd;
376 // debug(D_WEB_CLIENT_ACCESS, "%llu: adding output socket %d to ofds", w->id, w->ofd);
377 FD_SET(w->ofd, &ofds);
378 if(w->ofd > fdmax) fdmax = w->ofd;
382 // cleanup unused clients
383 if(unlikely(has_obsolete)) {
384 for (w = web_clients; w; ) {
386 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
387 w = web_client_free(w);
388 #ifdef NETDATA_INTERNAL_CHECKS
396 debug(D_WEB_CLIENT_ACCESS, "LISTENER: Waiting...");
397 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
399 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
402 error("LISTENER: select() failed.");
404 if(errno != EAGAIN) {
405 // debug(D_WEB_CLIENT_ACCESS, "LISTENER: select() failed.");
406 error("REMOVING ALL %lu WEB CLIENTS !", global_statistics.connected_clients);
407 while (web_clients) web_client_free(web_clients);
412 error("LISTENER: our listen port %d seems dead. Re-opening it.", listen_fd);
416 create_listen_socket();
418 fatal("Cannot listen for web clients (connected clients %llu).", global_statistics.connected_clients);
426 for(w = web_clients; w ; w = w->next) {
427 if (unlikely(w->obsolete)) continue;
429 if (unlikely(FD_ISSET(w->ifd, &efds))) {
430 debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on input socket.", w->id);
436 if (unlikely(FD_ISSET(w->ofd, &efds))) {
437 debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on output socket.", w->id);
443 if (unlikely(w->wait_receive && FD_ISSET(w->ifd, &ifds))) {
445 if (unlikely((bytes = web_client_receive(w)) < 0)) {
446 debug(D_WEB_CLIENT, "%llu: Cannot receive data from client. Closing client.", w->id);
453 if (w->mode == WEB_CLIENT_MODE_NORMAL) {
454 debug(D_WEB_CLIENT, "%llu: Processing received data (%ld bytes).", w->id, bytes);
455 // info("%llu: Attempting to process received data (%ld bytes).", w->id, bytes);
456 web_client_process(w);
459 debug(D_WEB_CLIENT, "%llu: NO Processing for received data (%ld bytes).", w->id, bytes);
463 if (unlikely(w->wait_send && FD_ISSET(w->ofd, &ofds))) {
465 if (unlikely((bytes = web_client_send(w)) < 0)) {
466 debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client.", w->id);
475 // check for new incoming connections
476 if(FD_ISSET(listen_fd, &ifds)) {
477 debug(D_WEB_CLIENT_ACCESS, "LISTENER: new connection.");
478 web_client_create(listen_fd);
482 debug(D_WEB_CLIENT_ACCESS, "LISTENER: timeout.");
488 error("LISTENER: exit!");
490 if(listen_fd >= 0) close(listen_fd);