add local/remote address env vars for cgi
[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 void uh_close_listen_fds(void)
41 {
42         struct listener *l;
43
44         list_for_each_entry(l, &listeners, list)
45                 close(l->fd.fd);
46 }
47
48 static void uh_block_listener(struct listener *l)
49 {
50         uloop_fd_delete(&l->fd);
51         n_blocked++;
52         l->blocked = true;
53 }
54
55 void uh_unblock_listeners(void)
56 {
57         struct listener *l;
58
59         if (!n_blocked && conf.max_requests &&
60             n_clients >= conf.max_requests)
61                 return;
62
63         list_for_each_entry(l, &listeners, list) {
64                 if (!l->blocked)
65                         continue;
66
67                 n_blocked--;
68                 l->blocked = false;
69                 uloop_fd_add(&l->fd, ULOOP_READ);
70         }
71 }
72
73 static void listener_cb(struct uloop_fd *fd, unsigned int events)
74 {
75         struct listener *l = container_of(fd, struct listener, fd);
76
77         uh_accept_client(fd->fd);
78
79         if (conf.max_requests && n_clients >= conf.max_requests)
80                 uh_block_listener(l);
81 }
82
83 void uh_setup_listeners(void)
84 {
85         struct listener *l;
86         int yes = 1;
87
88         list_for_each_entry(l, &listeners, list) {
89                 int sock = l->fd.fd;
90
91                 /* TCP keep-alive */
92                 if (conf.tcp_keepalive > 0) {
93 #ifdef linux
94                         int tcp_ka_idl, tcp_ka_int, tcp_ka_cnt;
95
96                         tcp_ka_idl = 1;
97                         tcp_ka_cnt = 3;
98                         tcp_ka_int = conf.tcp_keepalive;
99
100                         setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,  &tcp_ka_idl, sizeof(tcp_ka_idl));
101                         setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &tcp_ka_int, sizeof(tcp_ka_int));
102                         setsockopt(sock, SOL_TCP, TCP_KEEPCNT,   &tcp_ka_cnt, sizeof(tcp_ka_cnt));
103 #endif
104
105                         setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes));
106                 }
107
108                 l->fd.cb = listener_cb;
109                 uloop_fd_add(&l->fd, ULOOP_READ);
110         }
111 }
112
113 int uh_socket_bind(const char *host, const char *port, bool tls)
114 {
115         int sock = -1;
116         int yes = 1;
117         int status;
118         int bound = 0;
119         struct listener *l = NULL;
120         struct addrinfo *addrs = NULL, *p = NULL;
121         static struct addrinfo hints = {
122                 .ai_family = AF_UNSPEC,
123                 .ai_socktype = SOCK_STREAM,
124                 .ai_flags = AI_PASSIVE,
125         };
126
127         if ((status = getaddrinfo(host, port, &hints, &addrs)) != 0) {
128                 fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(status));
129                 return 0;
130         }
131
132         /* try to bind a new socket to each found address */
133         for (p = addrs; p; p = p->ai_next) {
134                 /* get the socket */
135                 sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
136                 if (sock < 0) {
137                         perror("socket()");
138                         goto error;
139                 }
140
141                 /* "address already in use" */
142                 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))) {
143                         perror("setsockopt()");
144                         goto error;
145                 }
146
147                 /* required to get parallel v4 + v6 working */
148                 if (p->ai_family == AF_INET6 &&
149                     setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
150                         perror("setsockopt()");
151                         goto error;
152                 }
153
154                 /* bind */
155                 if (bind(sock, p->ai_addr, p->ai_addrlen) < 0) {
156                         perror("bind()");
157                         goto error;
158                 }
159
160                 /* listen */
161                 if (listen(sock, UH_LIMIT_CLIENTS) < 0) {
162                         perror("listen()");
163                         goto error;
164                 }
165
166                 fd_cloexec(sock);
167
168                 l = calloc(1, sizeof(*l));
169                 if (!l)
170                         goto error;
171
172                 l->fd.fd = sock;
173                 l->tls = tls;
174                 list_add_tail(&l->list, &listeners);
175                 bound++;
176
177                 continue;
178
179 error:
180                 if (sock > -1)
181                         close(sock);
182         }
183
184         freeaddrinfo(addrs);
185
186         return bound;
187 }