2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/tcp.h>
28 struct list_head list
;
32 struct sockaddr_in6 addr
;
37 static LIST_HEAD(listeners
);
40 void uh_close_listen_fds(void)
44 list_for_each_entry(l
, &listeners
, list
)
48 static void uh_block_listener(struct listener
*l
)
50 uloop_fd_delete(&l
->fd
);
55 void uh_unblock_listeners(void)
59 if ((!n_blocked
&& conf
.max_requests
) ||
60 n_clients
>= conf
.max_requests
)
63 list_for_each_entry(l
, &listeners
, list
) {
67 l
->fd
.cb(&l
->fd
, ULOOP_READ
);
68 if (n_clients
>= conf
.max_requests
)
73 uloop_fd_add(&l
->fd
, ULOOP_READ
);
77 static void listener_cb(struct uloop_fd
*fd
, unsigned int events
)
79 struct listener
*l
= container_of(fd
, struct listener
, fd
);
82 if (!uh_accept_client(fd
->fd
, l
->tls
))
86 if (conf
.max_requests
&& n_clients
>= conf
.max_requests
)
90 void uh_setup_listeners(void)
95 list_for_each_entry(l
, &listeners
, list
) {
99 if (conf
.tcp_keepalive
> 0) {
101 int tcp_ka_idl
, tcp_ka_int
, tcp_ka_cnt
;
105 tcp_ka_int
= conf
.tcp_keepalive
;
107 setsockopt(sock
, SOL_TCP
, TCP_KEEPIDLE
, &tcp_ka_idl
, sizeof(tcp_ka_idl
));
108 setsockopt(sock
, SOL_TCP
, TCP_KEEPINTVL
, &tcp_ka_int
, sizeof(tcp_ka_int
));
109 setsockopt(sock
, SOL_TCP
, TCP_KEEPCNT
, &tcp_ka_cnt
, sizeof(tcp_ka_cnt
));
112 setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
, &yes
, sizeof(yes
));
115 l
->fd
.cb
= listener_cb
;
116 uloop_fd_add(&l
->fd
, ULOOP_READ
);
120 int uh_socket_bind(const char *host
, const char *port
, bool tls
)
126 struct listener
*l
= NULL
;
127 struct addrinfo
*addrs
= NULL
, *p
= NULL
;
128 static struct addrinfo hints
= {
129 .ai_family
= AF_UNSPEC
,
130 .ai_socktype
= SOCK_STREAM
,
131 .ai_flags
= AI_PASSIVE
,
134 if ((status
= getaddrinfo(host
, port
, &hints
, &addrs
)) != 0) {
135 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(status
));
139 /* try to bind a new socket to each found address */
140 for (p
= addrs
; p
; p
= p
->ai_next
) {
142 sock
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
148 /* "address already in use" */
149 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
))) {
150 perror("setsockopt()");
154 /* required to get parallel v4 + v6 working */
155 if (p
->ai_family
== AF_INET6
&&
156 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &yes
, sizeof(yes
)) < 0) {
157 perror("setsockopt()");
162 if (bind(sock
, p
->ai_addr
, p
->ai_addrlen
) < 0) {
168 if (listen(sock
, UH_LIMIT_CLIENTS
) < 0) {
175 l
= calloc(1, sizeof(*l
));
181 list_add_tail(&l
->list
, &listeners
);