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 static void log_allocations(void)
40 if(mi.uordblks > mem) {
43 for(w = web_clients; w ; w = w->next) clients++;
45 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);
50 int create_listen_socket4(int port, int listen_backlog)
54 struct sockaddr_in name;
56 debug(D_LISTENER, "IPv4 creating new listening socket on port %d", port);
58 sock = socket(AF_INET, SOCK_STREAM, 0);
60 error("IPv4 socket() failed.");
64 /* avoid "address already in use" */
65 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
67 memset(&name, 0, sizeof(struct sockaddr_in));
68 name.sin_family = AF_INET;
69 name.sin_port = htons (port);
70 name.sin_addr.s_addr = htonl (INADDR_ANY);
72 if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
74 error("IPv4 bind() failed.");
78 if(listen(sock, listen_backlog) < 0) {
80 fatal("IPv4 listen() failed.");
84 debug(D_LISTENER, "IPv4 listening port %d created", port);
88 int create_listen_socket6(int port, int listen_backlog)
92 struct sockaddr_in6 name;
94 debug(D_LISTENER, "IPv6 creating new listening socket on port %d", port);
96 sock = socket(AF_INET6, SOCK_STREAM, 0);
98 error("IPv6 socket() failed.");
102 /* avoid "address already in use" */
103 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
105 memset(&name, 0, sizeof(struct sockaddr_in6));
106 name.sin6_family = AF_INET6;
107 name.sin6_port = htons (port);
108 name.sin6_addr = in6addr_any;
109 name.sin6_scope_id = 0;
111 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
113 error("IPv6 bind() failed.");
117 if (listen(sock, listen_backlog) < 0) {
119 fatal("IPv6 listen() failed.");
123 debug(D_LISTENER, "IPv6 listening port %d created", port);
128 // --------------------------------------------------------------------------------------
129 // the main socket listener
131 // 1. it accepts new incoming requests on our port
132 // 2. creates a new web_client for each connection received
133 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
134 // 4. cleans up old web_clients that their pthreads have been exited
136 void *socket_listen_main(void *ptr)
140 info("WEB SERVER thread created with task id %d", gettid());
142 struct web_client *w;
148 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
149 error("Cannot set pthread cancel type to DEFERRED.");
151 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
152 error("Cannot set pthread cancel state to ENABLE.");
154 web_client_timeout = config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
155 web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
157 if(listen_fd < 0) fatal("LISTENER: Listen socket is not ready.");
159 fd_set ifds, ofds, efds;
160 int fdmax = listen_fd;
171 FD_SET(listen_fd, &ifds);
172 FD_SET(listen_fd, &efds);
175 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
176 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
179 error("LISTENER: select() failed.");
183 // check for new incoming connections
184 if(FD_ISSET(listen_fd, &ifds)) {
185 w = web_client_create(listen_fd);
187 // no need for error log - web_client_create already logged the error
191 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
192 error("%llu: failed to create new thread for web client.");
195 else if(pthread_detach(w->thread) != 0) {
196 error("%llu: Cannot request detach of newly created web client thread.", w->id);
200 else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
204 // debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
207 // cleanup unused clients
208 for(w = web_clients; w ; w = w?w->next:NULL) {
210 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
211 // pthread_join(w->thread, NULL);
212 w = web_client_free(w);
218 error("LISTENER: exit!");
220 if(listen_fd >= 0) close(listen_fd);