1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_network_io.h"
18 #include "apr_strings.h"
20 #define APR_WANT_STRFUNC
23 #include "ap_config.h"
25 #include "http_config.h"
26 #include "http_core.h"
27 #include "ap_listen.h"
29 #include "mpm_common.h"
31 /* we know core's module_index is 0 */
32 #undef APLOG_MODULE_INDEX
33 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
35 AP_DECLARE_DATA ap_listen_rec *ap_listeners = NULL;
37 static ap_listen_rec *old_listeners;
38 static int ap_listenbacklog;
39 static int send_buffer_size;
40 static int receive_buffer_size;
42 /* TODO: make_sock is just begging and screaming for APR abstraction */
43 static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
45 apr_socket_t *s = server->sd;
48 #ifdef AP_ENABLE_V4_MAPPED
49 int v6only_setting = 0;
51 int v6only_setting = 1;
57 stat = apr_socket_opt_set(s, APR_SO_REUSEADDR, one);
58 if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
59 ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
60 "make_sock: for address %pI, apr_socket_opt_set: (SO_REUSEADDR)",
67 stat = apr_socket_opt_set(s, APR_SO_KEEPALIVE, one);
68 if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
69 ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
70 "make_sock: for address %pI, apr_socket_opt_set: (SO_KEEPALIVE)",
77 if (server->bind_addr->family == APR_INET6) {
78 stat = apr_socket_opt_set(s, APR_IPV6_V6ONLY, v6only_setting);
79 if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
80 ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
81 "make_sock: for address %pI, apr_socket_opt_set: "
91 * To send data over high bandwidth-delay connections at full
92 * speed we must force the TCP window to open wide enough to keep the
93 * pipe full. The default window size on many systems
94 * is only 4kB. Cross-country WAN connections of 100ms
95 * at 1Mb/s are not impossible for well connected sites.
96 * If we assume 100ms cross-country latency,
97 * a 4kB buffer limits throughput to 40kB/s.
99 * To avoid this problem I've added the SendBufferSize directive
100 * to allow the web master to configure send buffer size.
102 * The trade-off of larger buffers is that more kernel memory
103 * is consumed. YMMV, know your customers and your network!
105 * -John Heidemann <johnh@isi.edu> 25-Oct-96
107 * If no size is specified, use the kernel default.
109 if (send_buffer_size) {
110 stat = apr_socket_opt_set(s, APR_SO_SNDBUF, send_buffer_size);
111 if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
112 ap_log_perror(APLOG_MARK, APLOG_WARNING, stat, p,
113 "make_sock: failed to set SendBufferSize for "
114 "address %pI, using default",
116 /* not a fatal error */
119 if (receive_buffer_size) {
120 stat = apr_socket_opt_set(s, APR_SO_RCVBUF, receive_buffer_size);
121 if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
122 ap_log_perror(APLOG_MARK, APLOG_WARNING, stat, p,
123 "make_sock: failed to set ReceiveBufferSize for "
124 "address %pI, using default",
126 /* not a fatal error */
130 #if APR_TCP_NODELAY_INHERITED
131 ap_sock_disable_nagle(s);
134 if ((stat = apr_socket_bind(s, server->bind_addr)) != APR_SUCCESS) {
135 ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p,
136 "make_sock: could not bind to address %pI",
142 if ((stat = apr_socket_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
143 ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p,
144 "make_sock: unable to listen for connections "
152 /* I seriously doubt that this would work on Unix; I have doubts that
153 * it entirely solves the problem on Win32. However, since setting
154 * reuseaddr on the listener -prior- to binding the socket has allowed
155 * us to attach to the same port as an already running instance of
156 * Apache, or even another web server, we cannot identify that this
157 * port was exclusively granted to this instance of Apache.
159 * So set reuseaddr, but do not attempt to do so until we have the
160 * parent listeners successfully bound.
162 stat = apr_socket_opt_set(s, APR_SO_REUSEADDR, one);
163 if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
164 ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
165 "make_sock: for address %pI, apr_socket_opt_set: (SO_REUSEADDR)",
175 server->accept_func = NULL;
180 static const char* find_accf_name(server_rec *s, const char *proto)
183 core_server_config *conf = ap_get_core_module_config(s->module_config);
188 accf = apr_table_get(conf->accf_map, proto);
190 if (accf && !strcmp("none", accf)) {
197 static void ap_apply_accept_filter(apr_pool_t *p, ap_listen_rec *lis,
200 apr_socket_t *s = lis->sd;
205 proto = lis->protocol;
208 proto = ap_get_server_protocol(server);
212 accf = find_accf_name(server, proto);
215 #if APR_HAS_SO_ACCEPTFILTER
216 rv = apr_socket_accept_filter(s, apr_pstrdup(p, accf),
218 if (rv != APR_SUCCESS && !APR_STATUS_IS_ENOTIMPL(rv)) {
219 ap_log_perror(APLOG_MARK, APLOG_WARNING, rv, p,
220 "Failed to enable the '%s' Accept Filter",
224 rv = apr_socket_opt_set(s, APR_TCP_DEFER_ACCEPT, 30);
225 if (rv != APR_SUCCESS && !APR_STATUS_IS_ENOTIMPL(rv)) {
226 ap_log_perror(APLOG_MARK, APLOG_WARNING, rv, p,
227 "Failed to enable APR_TCP_DEFER_ACCEPT");
233 static apr_status_t close_listeners_on_exec(void *v)
235 ap_close_listeners();
239 static const char *alloc_listener(process_rec *process, char *addr,
240 apr_port_t port, const char* proto,
243 ap_listen_rec **walk, *last;
246 int found_listener = 0;
248 /* see if we've got an old listener for this address:port */
249 for (walk = &old_listeners; *walk;) {
250 sa = (*walk)->bind_addr;
251 /* Some listeners are not real so they will not have a bind_addr. */
257 /* If both ports are equivalent, then if their names are equivalent,
258 * then we will re-use the existing record.
260 if (port == oldport &&
261 ((!addr && !sa->hostname) ||
262 ((addr && sa->hostname) && !strcmp(sa->hostname, addr)))) {
265 new->next = ap_listeners;
272 walk = &(*walk)->next;
275 if (found_listener) {
276 if (ap_listeners->slave != dummy) {
277 return "Cannot define a slave on the same IP:port as a Listener";
282 if ((status = apr_sockaddr_info_get(&sa, addr, APR_UNSPEC, port, 0,
285 ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool,
286 "alloc_listener: failed to set up sockaddr for %s",
288 return "Listen setup failed";
291 /* Initialize to our last configured ap_listener. */
293 while (last && last->next) {
300 /* this has to survive restarts */
301 new = apr_palloc(process->pool, sizeof(ap_listen_rec));
305 new->protocol = apr_pstrdup(process->pool, proto);
307 /* Go to the next sockaddr. */
310 status = apr_socket_create(&new->sd, new->bind_addr->family,
311 SOCK_STREAM, 0, process->pool);
314 /* What could happen is that we got an IPv6 address, but this system
315 * doesn't actually support IPv6. Try the next address.
317 if (status != APR_SUCCESS && !addr &&
318 new->bind_addr->family == APR_INET6) {
322 if (status != APR_SUCCESS) {
323 ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool,
324 "alloc_listener: failed to get a socket for %s",
326 return "Listen setup failed";
329 /* We need to preserve the order returned by getaddrinfo() */
331 ap_listeners = last = new;
341 /* Evaluates to true if the (apr_sockaddr_t *) addr argument is the
342 * IPv4 match-any-address, 0.0.0.0. */
343 #define IS_INADDR_ANY(addr) ((addr)->family == APR_INET \
344 && (addr)->sa.sin.sin_addr.s_addr == INADDR_ANY)
346 /* Evaluates to true if the (apr_sockaddr_t *) addr argument is the
347 * IPv6 match-any-address, [::]. */
348 #define IS_IN6ADDR_ANY(addr) ((addr)->family == APR_INET6 \
349 && IN6_IS_ADDR_UNSPECIFIED(&(addr)->sa.sin6.sin6_addr))
352 * Create, open, listen, and bind all sockets.
353 * @param process The process record for the currently running server
354 * @return The number of open sockets
356 static int open_listeners(apr_pool_t *pool)
360 ap_listen_rec *previous;
362 const char *userdata_key = "ap_open_listeners";
364 #if AP_NONBLOCK_WHEN_MULTI_LISTEN
368 /* Don't allocate a default listener. If we need to listen to a
369 * port, then the user needs to have a Listen directive in their
374 for (lr = ap_listeners; lr; previous = lr, lr = lr->next) {
384 /* If we have the unspecified IPv4 address (0.0.0.0) and
385 * the unspecified IPv6 address (::) is next, we need to
386 * swap the order of these in the list. We always try to
387 * bind to IPv6 first, then IPv4, since an IPv6 socket
388 * might be able to receive IPv4 packets if V6ONLY is not
389 * enabled, but never the other way around.
390 * Note: In some configurations, the unspecified IPv6 address
391 * could be even later in the list. This logic only corrects
392 * the situation where it is next in the list, such as when
393 * apr_sockaddr_info_get() returns an IPv4 and an IPv6 address,
397 && IS_INADDR_ANY(lr->bind_addr)
398 && lr->bind_addr->port == lr->next->bind_addr->port
399 && IS_IN6ADDR_ANY(lr->next->bind_addr)) {
400 /* Exchange lr and lr->next */
402 lr->next = next->next;
405 previous->next = next;
413 /* If we are trying to bind to 0.0.0.0 and a previous listener
414 * was :: on the same port and in turn that socket does not have
415 * the IPV6_V6ONLY flag set; we must skip the current attempt to
416 * listen (which would generate an error). IPv4 will be handled
417 * on the established IPv6 socket.
419 if (IS_INADDR_ANY(lr->bind_addr) && previous) {
420 for (cur = ap_listeners; cur != lr; cur = cur->next) {
421 if (lr->bind_addr->port == cur->bind_addr->port
422 && IS_IN6ADDR_ANY(cur->bind_addr)
423 && apr_socket_opt_get(cur->sd, APR_IPV6_V6ONLY,
424 &v6only_setting) == APR_SUCCESS
425 && v6only_setting == 0) {
427 /* Remove the current listener from the list */
428 previous->next = lr->next;
429 lr = previous; /* maintain current value of previous after
430 * post-loop expression is evaluated
441 if (make_sock(pool, lr) == APR_SUCCESS) {
446 /* If we tried to bind to ::, and the next listener is
447 * on 0.0.0.0 with the same port, don't give a fatal
448 * error. The user will still get a warning from make_sock
452 && IS_IN6ADDR_ANY(lr->bind_addr)
453 && lr->bind_addr->port == lr->next->bind_addr->port
454 && IS_INADDR_ANY(lr->next->bind_addr)) {
456 /* Remove the current listener from the list */
458 previous->next = lr->next;
461 ap_listeners = lr->next;
464 /* Although we've removed ourselves from the list,
465 * we need to make sure that the next iteration won't
466 * consider "previous" a working IPv6 '::' socket.
467 * Changing the family is enough to make sure the
468 * conditions before make_sock() fail.
470 lr->bind_addr->family = AF_INET;
481 /* close the old listeners */
482 for (lr = old_listeners; lr; lr = next) {
483 apr_socket_close(lr->sd);
487 old_listeners = NULL;
489 #if AP_NONBLOCK_WHEN_MULTI_LISTEN
490 /* if multiple listening sockets, make them non-blocking so that
491 * if select()/poll() reports readability for a reset connection that
492 * is already forgotten about by the time we call accept, we won't
493 * be hung until another connection arrives on that port
495 use_nonblock = (ap_listeners && ap_listeners->next);
496 for (lr = ap_listeners; lr; lr = lr->next) {
499 status = apr_socket_opt_set(lr->sd, APR_SO_NONBLOCK, use_nonblock);
500 if (status != APR_SUCCESS) {
501 ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, status, pool,
502 "unable to control socket non-blocking status");
506 #endif /* AP_NONBLOCK_WHEN_MULTI_LISTEN */
508 /* we come through here on both passes of the open logs phase
509 * only register the cleanup once... otherwise we try to close
510 * listening sockets twice when cleaning up prior to exec
512 apr_pool_userdata_get(&data, userdata_key, pool);
514 apr_pool_userdata_set((const void *)1, userdata_key,
515 apr_pool_cleanup_null, pool);
516 apr_pool_cleanup_register(pool, NULL, apr_pool_cleanup_null,
517 close_listeners_on_exec);
520 return num_open ? 0 : -1;
523 AP_DECLARE(int) ap_setup_listeners(server_rec *s)
526 server_addr_rec *addr;
528 int num_listeners = 0;
532 for (ls = s; ls; ls = ls->next) {
533 proto = ap_get_server_protocol(ls);
536 /* No protocol was set for this vhost,
537 * use the default for this listener.
539 for (addr = ls->addrs; addr && !found; addr = addr->next) {
540 for (lr = ap_listeners; lr; lr = lr->next) {
541 if (apr_sockaddr_equal(lr->bind_addr, addr->host_addr) &&
542 lr->bind_addr->port == addr->host_port) {
543 ap_set_server_protocol(ls, lr->protocol);
551 /* TODO: set protocol defaults per-Port, eg 25=smtp */
552 ap_set_server_protocol(ls, "http");
557 if (open_listeners(s->process->pool)) {
561 for (lr = ap_listeners; lr; lr = lr->next) {
564 for (ls = s; ls && !found; ls = ls->next) {
565 for (addr = ls->addrs; addr && !found; addr = addr->next) {
566 if (apr_sockaddr_equal(lr->bind_addr, addr->host_addr) &&
567 lr->bind_addr->port == addr->host_port) {
569 ap_apply_accept_filter(s->process->pool, lr, ls);
575 ap_apply_accept_filter(s->process->pool, lr, s);
579 return num_listeners;
582 AP_DECLARE_NONSTD(void) ap_close_listeners(void)
586 for (lr = ap_listeners; lr; lr = lr->next) {
587 apr_socket_close(lr->sd);
591 AP_DECLARE_NONSTD(int) ap_close_selected_listeners(ap_slave_t *slave)
596 for (lr = ap_listeners; lr; lr = lr->next) {
597 if (lr->slave != slave) {
598 apr_socket_close(lr->sd);
608 AP_DECLARE(void) ap_listen_pre_config(void)
610 old_listeners = ap_listeners;
612 ap_listenbacklog = DEFAULT_LISTENBACKLOG;
615 /* Hack: populate an extra field
616 * When this gets called from a Listen directive, dummy is null.
617 * So we can use non-null dummy to pass a data pointer without conflict
619 AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
620 int argc, char *const argv[])
622 char *host, *scope_id, *proto;
625 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
631 if (argc < 1 || argc > 2) {
632 return "Listen requires 1 or 2 arguments.";
635 rv = apr_parse_addr_port(&host, &scope_id, &port, argv[0], cmd->pool);
636 if (rv != APR_SUCCESS) {
637 return "Invalid address or port";
640 if (host && !strcmp(host, "*")) {
645 /* XXX scope id support is useful with link-local IPv6 addresses */
646 return "Scope id is not supported";
650 return "Port must be specified";
661 proto = apr_pstrdup(cmd->pool, argv[1]);
662 ap_str_tolower(proto);
665 return alloc_listener(cmd->server->process, host, port, proto, dummy);
668 AP_DECLARE_NONSTD(const char *) ap_set_listenbacklog(cmd_parms *cmd,
673 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
681 return "ListenBacklog must be > 0";
684 ap_listenbacklog = b;
688 AP_DECLARE_NONSTD(const char *) ap_set_send_buffer_size(cmd_parms *cmd,
693 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
699 if (s < 512 && s != 0) {
700 return "SendBufferSize must be >= 512 bytes, or 0 for system default.";
703 send_buffer_size = s;
707 AP_DECLARE_NONSTD(const char *) ap_set_receive_buffer_size(cmd_parms *cmd,
712 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
718 if (s < 512 && s != 0) {
719 return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
722 receive_buffer_size = s;