listen.c revision ebf4099fd4921bbbcef21dc872b9cd4fc73e9f55
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek/* ====================================================================
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * The Apache Software License, Version 1.1
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * Redistribution and use in source and binary forms, with or without
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * modification, are permitted provided that the following conditions
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * 1. Redistributions of source code must retain the above copyright
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * notice, this list of conditions and the following disclaimer.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * 2. Redistributions in binary form must reproduce the above copyright
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * notice, this list of conditions and the following disclaimer in
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * the documentation and/or other materials provided with the
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * 3. The end-user documentation included with the redistribution,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * if any, must include the following acknowledgment:
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * "This product includes software developed by the
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * Apache Software Foundation (http://www.apache.org/)."
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * Alternately, this acknowledgment may appear in the software itself,
21ac6ff143cc8bebfbd1818af28e8c6f82cd5265Zbigniew Jędrzejewski-Szmek * if and wherever such third-party acknowledgments normally appear.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * 4. The names "Apache" and "Apache Software Foundation" must
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * not be used to endorse or promote products derived from this
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * software without prior written permission. For written
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * permission, please contact apache@apache.org.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * 5. Products derived from this software may not be called "Apache",
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * nor may "Apache" appear in their name, without prior written
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * permission of the Apache Software Foundation.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * ====================================================================
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * This software consists of voluntary contributions made by many
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * individuals on behalf of the Apache Software Foundation. For more
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * information on the Apache Software Foundation, please see
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * Portions of this software are based upon public domain software
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * originally written at the National Center for Supercomputing Applications,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * University of Illinois, Urbana-Champaign.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekap_listen_rec *ap_listeners = NULL;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic int default_family = APR_UNSPEC;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic int default_family = APR_INET;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic ap_listen_rec *old_listeners;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek/* TODO: make_sock is just begging and screaming for APR abstraction */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering stat = apr_setsocketopt(s, APR_SO_REUSEADDR, one);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering "make_sock: for address %pI, setsockopt: (SO_REUSEADDR)",
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering stat = apr_setsocketopt(s, APR_SO_KEEPALIVE, one);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering "make_sock: for address %pI, setsockopt: (SO_KEEPALIVE)",
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * To send data over high bandwidth-delay connections at full
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * speed we must force the TCP window to open wide enough to keep the
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * pipe full. The default window size on many systems
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * is only 4kB. Cross-country WAN connections of 100ms
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * at 1Mb/s are not impossible for well connected sites.
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * If we assume 100ms cross-country latency,
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * a 4kB buffer limits throughput to 40kB/s.
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * To avoid this problem I've added the SendBufferSize directive
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * to allow the web master to configure send buffer size.
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * The trade-off of larger buffers is that more kernel memory
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * is consumed. YMMV, know your customers and your network!
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * -John Heidemann <johnh@isi.edu> 25-Oct-96
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * If no size is specified, use the kernel default.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek stat = apr_setsocketopt(s, APR_SO_SNDBUF, send_buffer_size);
f7f74d8ec46532f13a1dc418d550eaf76b339fa3Lennart Poettering if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
7b870f607bcb8f445d4d29a776d12d9de3bf1487Jan Engelhardt ap_log_perror(APLOG_MARK, APLOG_WARNING, stat, p,
7b870f607bcb8f445d4d29a776d12d9de3bf1487Jan Engelhardt "make_sock: failed to set SendBufferSize for "
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek "address %pI, using default",
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* not a fatal error */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if ((stat = apr_bind(s, server->bind_addr)) != APR_SUCCESS) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek "make_sock: could not bind to address %pI",
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if ((stat = apr_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek "make_sock: unable to listen for connections on address %pI",
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#define ACCEPT_FILTER_NAME "dataready"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_socket_accept_filter(s, ACCEPT_FILTER_NAME, "");
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek server->accept_func = MPM_ACCEPT_FUNC;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic apr_status_t close_listeners_on_exec(void *v)
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic void find_default_family(apr_pool_t *p)
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* We know the platform supports IPv6, but this particular
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * system may not have IPv6 enabled. See if we can get an
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * AF_INET6 socket.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if (default_family == APR_UNSPEC) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if (apr_socket_create(&tmp_sock, APR_INET6, SOCK_STREAM,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic void alloc_listener(process_rec *process, char *addr, apr_port_t port)
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if (!addr) { /* don't bind to specific interface */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek find_default_family(process->pool);
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_assert(1 != 1); /* should not occur */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* see if we've got an old listener for this address:port */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek for (walk = &old_listeners; *walk; walk = &(*walk)->next) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* Some listeners are not real so they will not have a bind_addr. */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_sockaddr_port_get(&oldport, sa);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek apr_sockaddr_ip_get(&oldaddr, sa);
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if (!strcmp(oldaddr, addr) && port == oldport) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* re-use existing record */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* this has to survive restarts */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek new = apr_palloc(process->pool, sizeof(ap_listen_rec));
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if ((status = apr_sockaddr_info_get(&new->bind_addr, addr, APR_UNSPEC,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek port, 0, process->pool)) != APR_SUCCESS) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek "alloc_listener: failed to set up sockaddr for %s", addr);
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if ((status = apr_socket_create(&new->sd, new->bind_addr->sa.sin.sin_family,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek SOCK_STREAM, process->pool)) != APR_SUCCESS) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool,
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "alloc_listener: failed to get a socket for %s", addr);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poetteringstatic int ap_listen_open(process_rec *process, apr_port_t port)
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering /* Don't allocate a default listener. If we need to listen to a
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering * port, then the user needs to have a Listen directive in their
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering * config file.
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (make_sock(pconf, lr) == APR_SUCCESS) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* fatal error */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* close the old listeners */
21ac6ff143cc8bebfbd1818af28e8c6f82cd5265Zbigniew Jędrzejewski-Szmek for (lr = old_listeners; lr; lr = next) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_pool_cleanup_register(pconf, NULL, apr_pool_cleanup_null,
int num_listeners = 0;
return num_listeners;
void ap_listen_pre_config(void)
return err;
if (scope_id) {
if (!port) {
return NULL;
return err;
ap_listenbacklog = b;
return NULL;
const char *arg)
return err;
send_buffer_size = s;
return NULL;