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"
28 int listen_backlog = LISTEN_BACKLOG;
31 int listen_port = LISTEN_PORT;
33 #ifdef NETDATA_INTERNAL_CHECKS
34 static void log_allocations(void)
41 if(mi.uordblks > mem) {
44 for(w = web_clients; w ; w = w->next) clients++;
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);
52 static int is_ip_anything(const char *ip)
59 || !strcmp(ip, "0.0.0.0")
65 int create_listen_socket4(const char *ip, int port, int listen_backlog)
69 struct sockaddr_in name;
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 memset(&name, 0, sizeof(struct sockaddr_in));
83 name.sin_family = AF_INET;
84 name.sin_port = htons (port);
86 if(is_ip_anything(ip)) {
87 name.sin_addr.s_addr = htonl(INADDR_ANY);
88 info("Listening on any IPs (IPv4).");
91 int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
93 error("Failed to convert IP '%s' to a valid IPv4 address.", ip);
97 info("Listening on IP '%s' (IPv4).", ip);
100 if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
102 error("IPv4 bind() failed.");
106 if(listen(sock, listen_backlog) < 0) {
108 fatal("IPv4 listen() failed.");
112 debug(D_LISTENER, "IPv4 listening port %d created", port);
116 int create_listen_socket6(const char *ip, int port, int listen_backlog)
120 struct sockaddr_in6 name;
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 memset(&name, 0, sizeof(struct sockaddr_in6));
134 name.sin6_family = AF_INET6;
135 name.sin6_port = htons ((uint16_t) port);
137 if(is_ip_anything(ip)) {
138 name.sin6_addr = in6addr_any;
139 info("Listening on all IPs (IPv6 and IPv4)");
142 int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
144 error("Failed to convert IP '%s' to a valid IPv6 address. Disabling IPv6.", ip);
148 info("Listening on IP '%s' (IPv6)", ip);
151 name.sin6_scope_id = 0;
153 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
155 error("IPv6 bind() failed. Disabling IPv6.");
159 if (listen(sock, listen_backlog) < 0) {
161 error("IPv6 listen() failed. Disabling IPv6.");
165 debug(D_LISTENER, "IPv6 listening port %d created", port);
170 // --------------------------------------------------------------------------------------
171 // the main socket listener
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
178 void *socket_listen_main(void *ptr)
182 info("WEB SERVER thread created with task id %d", gettid());
184 struct web_client *w;
190 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
191 error("Cannot set pthread cancel type to DEFERRED.");
193 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
194 error("Cannot set pthread cancel state to ENABLE.");
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);
199 if(listen_fd < 0) fatal("LISTENER: Listen socket is not ready.");
201 fd_set ifds, ofds, efds;
202 int fdmax = listen_fd;
213 FD_SET(listen_fd, &ifds);
214 FD_SET(listen_fd, &efds);
217 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
218 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
221 error("LISTENER: select() failed.");
225 // check for new incoming connections
226 if(FD_ISSET(listen_fd, &ifds)) {
227 w = web_client_create(listen_fd);
229 // no need for error log - web_client_create already logged the error
233 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
234 error("%llu: failed to create new thread for web client.");
237 else if(pthread_detach(w->thread) != 0) {
238 error("%llu: Cannot request detach of newly created web client thread.", w->id);
242 else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
246 // debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
249 // cleanup unused clients
250 for(w = web_clients; w ; w = w?w->next:NULL) {
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
262 error("LISTENER: exit!");
264 if(listen_fd >= 0) close(listen_fd);