2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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 static void uh_poll_listeners(struct uloop_timeout
*timeout
)
59 if ((!n_blocked
&& conf
.max_connections
) ||
60 n_clients
>= conf
.max_connections
)
63 list_for_each_entry(l
, &listeners
, list
) {
67 l
->fd
.cb(&l
->fd
, ULOOP_READ
);
68 if (n_clients
>= conf
.max_connections
)
73 uloop_fd_add(&l
->fd
, ULOOP_READ
);
77 void uh_unblock_listeners(void)
79 static struct uloop_timeout poll_timer
= {
80 .cb
= uh_poll_listeners
83 uloop_timeout_set(&poll_timer
, 1);
86 static void listener_cb(struct uloop_fd
*fd
, unsigned int events
)
88 struct listener
*l
= container_of(fd
, struct listener
, fd
);
91 if (!uh_accept_client(fd
->fd
, l
->tls
))
95 if (conf
.max_connections
&& n_clients
>= conf
.max_connections
)
99 void uh_setup_listeners(void)
104 list_for_each_entry(l
, &listeners
, list
) {
108 if (conf
.tcp_keepalive
> 0) {
110 int tcp_ka_idl
, tcp_ka_int
, tcp_ka_cnt
, tcp_fstopn
;
114 tcp_ka_int
= conf
.tcp_keepalive
;
117 setsockopt(sock
, SOL_TCP
, TCP_KEEPIDLE
, &tcp_ka_idl
, sizeof(tcp_ka_idl
));
118 setsockopt(sock
, SOL_TCP
, TCP_KEEPINTVL
, &tcp_ka_int
, sizeof(tcp_ka_int
));
119 setsockopt(sock
, SOL_TCP
, TCP_KEEPCNT
, &tcp_ka_cnt
, sizeof(tcp_ka_cnt
));
120 setsockopt(sock
, SOL_TCP
, TCP_FASTOPEN
, &tcp_fstopn
, sizeof(tcp_fstopn
));
123 setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
, &yes
, sizeof(yes
));
126 l
->fd
.cb
= listener_cb
;
127 uloop_fd_add(&l
->fd
, ULOOP_READ
);
131 int uh_socket_bind(const char *host
, const char *port
, bool tls
)
137 struct listener
*l
= NULL
;
138 struct addrinfo
*addrs
= NULL
, *p
= NULL
;
139 static struct addrinfo hints
= {
140 .ai_family
= AF_UNSPEC
,
141 .ai_socktype
= SOCK_STREAM
,
142 .ai_flags
= AI_PASSIVE
,
145 if ((status
= getaddrinfo(host
, port
, &hints
, &addrs
)) != 0) {
146 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(status
));
150 /* try to bind a new socket to each found address */
151 for (p
= addrs
; p
; p
= p
->ai_next
) {
153 sock
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
159 /* "address already in use" */
160 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
))) {
161 perror("setsockopt()");
165 /* required to get parallel v4 + v6 working */
166 if (p
->ai_family
== AF_INET6
&&
167 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &yes
, sizeof(yes
)) < 0) {
168 perror("setsockopt()");
173 if (bind(sock
, p
->ai_addr
, p
->ai_addrlen
) < 0) {
179 if (listen(sock
, UH_LIMIT_CLIENTS
) < 0) {
186 l
= calloc(1, sizeof(*l
));
192 memcpy(&l
->addr
, p
->ai_addr
, p
->ai_addrlen
);
193 list_add_tail(&l
->list
, &listeners
);
208 int uh_first_tls_port(int family
)
213 list_for_each_entry(l
, &listeners
, list
) {
214 if (!l
->tls
|| l
->addr
.sin6_family
!= family
)
217 if (tls_port
!= -1 && ntohs(l
->addr
.sin6_port
) != 443)
220 tls_port
= ntohs(l
->addr
.sin6_port
);