6 #include <sys/socket.h>
7 #include <netinet/in.h>
13 #include <netinet/tcp.h>
20 #include "web_buffer.h"
21 #include "web_client.h"
22 #include "web_server.h"
23 #include "global_statistics.h"
27 int listen_backlog = LISTEN_BACKLOG;
30 int listen_port = LISTEN_PORT;
32 static void log_allocations(void)
39 if(mi.uordblks > mem) {
42 for(w = web_clients; w ; w = w->next) clients++;
44 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);
49 int create_listen_socket4(int port, int listen_backlog)
53 struct sockaddr_in name;
55 debug(D_LISTENER, "IPv4 creating new listening socket on port %d", port);
57 sock = socket(AF_INET, SOCK_STREAM, 0);
59 error("IPv4 socket() failed.");
63 /* avoid "address already in use" */
64 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
66 memset(&name, 0, sizeof(struct sockaddr_in));
67 name.sin_family = AF_INET;
68 name.sin_port = htons (port);
69 name.sin_addr.s_addr = htonl (INADDR_ANY);
71 if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
73 error("IPv4 bind() failed.");
77 if(listen(sock, listen_backlog) < 0) {
79 fatal("IPv4 listen() failed.");
83 debug(D_LISTENER, "IPv4 listening port %d created", port);
87 int create_listen_socket6(int port, int listen_backlog)
91 struct sockaddr_in6 name;
93 debug(D_LISTENER, "IPv6 creating new listening socket on port %d", port);
95 sock = socket(AF_INET6, SOCK_STREAM, 0);
97 error("IPv6 socket() failed.");
101 /* avoid "address already in use" */
102 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
104 memset(&name, 0, sizeof(struct sockaddr_in6));
105 name.sin6_family = AF_INET6;
106 name.sin6_port = htons (port);
107 name.sin6_addr = in6addr_any;
108 name.sin6_scope_id = 0;
110 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
112 error("IPv6 bind() failed.");
116 if (listen(sock, listen_backlog) < 0) {
118 fatal("IPv6 listen() failed.");
122 debug(D_LISTENER, "IPv6 listening port %d created", port);
127 // --------------------------------------------------------------------------------------
128 // the main socket listener
130 // 1. it accepts new incoming requests on our port
131 // 2. creates a new web_client for each connection received
132 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
133 // 4. cleans up old web_clients that their pthreads have been exited
135 void *socket_listen_main(void *ptr)
138 struct web_client *w;
144 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
145 error("Cannot set pthread cancel type to DEFERRED.");
147 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
148 error("Cannot set pthread cancel state to ENABLE.");
150 web_client_timeout = config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
151 web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
153 if(listen_fd < 0) fatal("LISTENER: Listen socket is not ready.");
155 fd_set ifds, ofds, efds;
156 int fdmax = listen_fd;
167 FD_SET(listen_fd, &ifds);
168 FD_SET(listen_fd, &efds);
171 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
172 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
175 error("LISTENER: select() failed.");
179 // check for new incoming connections
180 if(FD_ISSET(listen_fd, &ifds)) {
181 w = web_client_create(listen_fd);
183 // no need for error log - web_client_create already logged the error
187 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
188 error("%llu: failed to create new thread for web client.");
191 else if(pthread_detach(w->thread) != 0) {
192 error("%llu: Cannot request detach of newly created web client thread.", w->id);
196 log_access("%llu: %s port %s connected", w->id, w->client_ip, w->client_port);
198 else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
202 // debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
205 // cleanup unused clients
206 for(w = web_clients; w ; w = w?w->next:NULL) {
208 log_access("%llu: %s port %s disconnected", w->id, w->client_ip, w->client_port);
209 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
210 // pthread_join(w->thread, NULL);
211 w = web_client_free(w);
217 error("LISTENER: exit!");
219 if(listen_fd >= 0) close(listen_fd);