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 = { .sin_family = AF_INET, .sin_port = htons(port) };
84 if(is_ip_anything(ip)) {
85 name.sin_addr.s_addr = htonl(INADDR_ANY);
86 info("Listening on any IPs (IPv4).");
89 int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
91 error("Failed to convert IP '%s' to a valid IPv4 address.", ip);
95 info("Listening on IP '%s' (IPv4).", ip);
98 if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
100 error("IPv4 bind() failed.");
104 if(listen(sock, listen_backlog) < 0) {
106 fatal("IPv4 listen() failed.");
110 debug(D_LISTENER, "IPv4 listening port %d created", port);
114 int create_listen_socket6(const char *ip, int port, int listen_backlog)
119 debug(D_LISTENER, "IPv6 creating new listening socket on port %d", port);
121 sock = socket(AF_INET6, SOCK_STREAM, 0);
123 error("IPv6 socket() failed. Disabling IPv6.");
127 /* avoid "address already in use" */
128 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
130 struct sockaddr_in6 name = { .sin6_family = AF_INET6, .sin6_port = htons((uint16_t)port) };
132 if(is_ip_anything(ip)) {
133 name.sin6_addr = in6addr_any;
134 info("Listening on all IPs (IPv6 and IPv4)");
137 int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
139 error("Failed to convert IP '%s' to a valid IPv6 address. Disabling IPv6.", ip);
143 info("Listening on IP '%s' (IPv6)", ip);
146 name.sin6_scope_id = 0;
148 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
150 error("IPv6 bind() failed. Disabling IPv6.");
154 if (listen(sock, listen_backlog) < 0) {
156 error("IPv6 listen() failed. Disabling IPv6.");
160 debug(D_LISTENER, "IPv6 listening port %d created", port);
165 // --------------------------------------------------------------------------------------
166 // the main socket listener
168 // 1. it accepts new incoming requests on our port
169 // 2. creates a new web_client for each connection received
170 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
171 // 4. cleans up old web_clients that their pthreads have been exited
173 void *socket_listen_main(void *ptr)
177 info("WEB SERVER thread created with task id %d", gettid());
179 struct web_client *w;
185 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
186 error("Cannot set pthread cancel type to DEFERRED.");
188 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
189 error("Cannot set pthread cancel state to ENABLE.");
191 web_client_timeout = (int) config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
192 web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
194 if(listen_fd < 0) fatal("LISTENER: Listen socket is not ready.");
196 fd_set ifds, ofds, efds;
197 int fdmax = listen_fd;
208 FD_SET(listen_fd, &ifds);
209 FD_SET(listen_fd, &efds);
212 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
213 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
216 error("LISTENER: select() failed.");
220 // check for new incoming connections
221 if(FD_ISSET(listen_fd, &ifds)) {
222 w = web_client_create(listen_fd);
224 // no need for error log - web_client_create already logged the error
228 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
229 error("%llu: failed to create new thread for web client.");
232 else if(pthread_detach(w->thread) != 0) {
233 error("%llu: Cannot request detach of newly created web client thread.", w->id);
237 else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
241 // debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
244 // cleanup unused clients
245 for(w = web_clients; w ; w = w?w->next:NULL) {
247 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
248 // pthread_join(w->thread, NULL);
249 w = web_client_free(w);
250 #ifdef NETDATA_INTERNAL_CHECKS
257 error("LISTENER: exit!");
259 if(listen_fd >= 0) close(listen_fd);