55d71435b7007a3cc5b5ca2764176c2bfc64cca5
[project/uhttpd.git] / listen.c
1 /*
2  * uhttpd - Tiny single-threaded httpd
3  *
4  *   Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/tcp.h>
23 #include <netdb.h>
24
25 #include "uhttpd.h"
26
27 struct listener {
28         struct list_head list;
29         struct uloop_fd fd;
30         int socket;
31         int n_clients;
32         struct sockaddr_in6 addr;
33         bool tls;
34         bool blocked;
35 };
36
37 static LIST_HEAD(listeners);
38 static int n_blocked;
39
40 static void uh_block_listener(struct listener *l)
41 {
42         uloop_fd_delete(&l->fd);
43         n_blocked++;
44         l->blocked = true;
45 }
46
47 void uh_unblock_listeners(void)
48 {
49         struct listener *l;
50
51         if (!n_blocked && conf.max_requests &&
52             n_clients >= conf.max_requests)
53                 return;
54
55         list_for_each_entry(l, &listeners, list) {
56                 if (!l->blocked)
57                         continue;
58
59                 n_blocked--;
60                 l->blocked = false;
61                 uloop_fd_add(&l->fd, ULOOP_READ);
62         }
63 }
64
65 static void listener_cb(struct uloop_fd *fd, unsigned int events)
66 {
67         struct listener *l = container_of(fd, struct listener, fd);
68
69         uh_accept_client(fd->fd);
70
71         if (conf.max_requests && n_clients >= conf.max_requests)
72                 uh_block_listener(l);
73 }
74
75 void uh_setup_listeners(void)
76 {
77         struct listener *l;
78         int yes = 1;
79
80         list_for_each_entry(l, &listeners, list) {
81                 int sock = l->fd.fd;
82
83                 /* TCP keep-alive */
84                 if (conf.tcp_keepalive > 0) {
85 #ifdef linux
86                         int tcp_ka_idl, tcp_ka_int, tcp_ka_cnt;
87
88                         tcp_ka_idl = 1;
89                         tcp_ka_cnt = 3;
90                         tcp_ka_int = conf.tcp_keepalive;
91
92                         setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,  &tcp_ka_idl, sizeof(tcp_ka_idl));
93                         setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &tcp_ka_int, sizeof(tcp_ka_int));
94                         setsockopt(sock, SOL_TCP, TCP_KEEPCNT,   &tcp_ka_cnt, sizeof(tcp_ka_cnt));
95 #endif
96
97                         setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes));
98                 }
99
100                 l->fd.cb = listener_cb;
101                 uloop_fd_add(&l->fd, ULOOP_READ);
102         }
103 }
104
105 int uh_socket_bind(const char *host, const char *port, bool tls)
106 {
107         int sock = -1;
108         int yes = 1;
109         int status;
110         int bound = 0;
111         struct listener *l = NULL;
112         struct addrinfo *addrs = NULL, *p = NULL;
113         static struct addrinfo hints = {
114                 .ai_family = AF_UNSPEC,
115                 .ai_socktype = SOCK_STREAM,
116                 .ai_flags = AI_PASSIVE,
117         };
118
119         if ((status = getaddrinfo(host, port, &hints, &addrs)) != 0) {
120                 fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(status));
121                 return 0;
122         }
123
124         /* try to bind a new socket to each found address */
125         for (p = addrs; p; p = p->ai_next) {
126                 /* get the socket */
127                 sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
128                 if (sock < 0) {
129                         perror("socket()");
130                         goto error;
131                 }
132
133                 /* "address already in use" */
134                 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))) {
135                         perror("setsockopt()");
136                         goto error;
137                 }
138
139                 /* required to get parallel v4 + v6 working */
140                 if (p->ai_family == AF_INET6 &&
141                     setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
142                         perror("setsockopt()");
143                         goto error;
144                 }
145
146                 /* bind */
147                 if (bind(sock, p->ai_addr, p->ai_addrlen) < 0) {
148                         perror("bind()");
149                         goto error;
150                 }
151
152                 /* listen */
153                 if (listen(sock, UH_LIMIT_CLIENTS) < 0) {
154                         perror("listen()");
155                         goto error;
156                 }
157
158                 fd_cloexec(sock);
159
160                 l = calloc(1, sizeof(*l));
161                 if (!l)
162                         goto error;
163
164                 l->fd.fd = sock;
165                 l->tls = tls;
166                 list_add_tail(&l->list, &listeners);
167                 bound++;
168
169                 continue;
170
171 error:
172                 if (sock > -1)
173                         close(sock);
174         }
175
176         freeaddrinfo(addrs);
177
178         return bound;
179 }