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