listen.c revision ebf4099fd4921bbbcef21dc872b9cd4fc73e9f55
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek/* ====================================================================
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * The Apache Software License, Version 1.1
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek *
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * reserved.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek *
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 * are met:
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek *
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 *
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 * distribution.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek *
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.
21ac6ff143cc8bebfbd1818af28e8c6f82cd5265Zbigniew Jędrzejewski-Szmek *
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 *
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 *
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 * SUCH DAMAGE.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * ====================================================================
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 * <http://www.apache.org/>.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek *
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-Szmek */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "apr_network_io.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "apr_strings.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "apr_lock.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#define APR_WANT_STRFUNC
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "apr_want.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#define CORE_PRIVATE
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "ap_config.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "httpd.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "http_config.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "ap_listen.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "http_log.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "mpm.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#include "mpm_common.h"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekap_listen_rec *ap_listeners = NULL;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#if APR_HAVE_IPV6
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic int default_family = APR_UNSPEC;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#else
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic int default_family = APR_INET;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#endif
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic ap_listen_rec *old_listeners;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic int ap_listenbacklog;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic int send_buffer_size;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
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)
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek{
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_socket_t *s = server->sd;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek int one = 1;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering apr_status_t stat;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
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 server->bind_addr);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering apr_socket_close(s);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering return stat;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering }
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
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 server->bind_addr);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering apr_socket_close(s);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering return stat;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering }
63ba209d8bc64137ff1585d5878328c93cb81b61Jan Engelhardt
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering /*
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 *
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * To avoid this problem I've added the SendBufferSize directive
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * to allow the web master to configure send buffer size.
63ba209d8bc64137ff1585d5878328c93cb81b61Jan Engelhardt *
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 *
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering * -John Heidemann <johnh@isi.edu> 25-Oct-96
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek *
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek * If no size is specified, use the kernel default.
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if (send_buffer_size) {
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 server->bind_addr);
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* not a fatal error */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#if APR_TCP_NODELAY_INHERITED
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_sock_disable_nagle(s);
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#endif
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
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 server->bind_addr);
f7f74d8ec46532f13a1dc418d550eaf76b339fa3Lennart Poettering apr_socket_close(s);
7b870f607bcb8f445d4d29a776d12d9de3bf1487Jan Engelhardt return stat;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
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 server->bind_addr);
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_socket_close(s);
3fde5f30bda2a70d97f3dc8fa918e42e1c07cc2cLennart Poettering return stat;
3fde5f30bda2a70d97f3dc8fa918e42e1c07cc2cLennart Poettering }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#if APR_HAS_SO_ACCEPTFILTER
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#ifndef ACCEPT_FILTER_NAME
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#define ACCEPT_FILTER_NAME "dataready"
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#endif
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_socket_accept_filter(s, ACCEPT_FILTER_NAME, "");
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#endif
f7f74d8ec46532f13a1dc418d550eaf76b339fa3Lennart Poettering
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek server->sd = s;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek server->active = 1;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#ifdef MPM_ACCEPT_FUNC
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek server->accept_func = MPM_ACCEPT_FUNC;
f7f74d8ec46532f13a1dc418d550eaf76b339fa3Lennart Poettering#else
f7f74d8ec46532f13a1dc418d550eaf76b339fa3Lennart Poettering server->accept_func = NULL;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#endif
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek return APR_SUCCESS;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek}
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic apr_status_t close_listeners_on_exec(void *v)
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek{
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_listen_rec *lr;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
fbce11397f4d19821a9dfe66ee3ebe11cad90057Jan Engelhardt for (lr = ap_listeners; lr; lr = lr->next) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_socket_close(lr->sd);
f7f74d8ec46532f13a1dc418d550eaf76b339fa3Lennart Poettering lr->active = 0;
f7f74d8ec46532f13a1dc418d550eaf76b339fa3Lennart Poettering }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek return APR_SUCCESS;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek}
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic void find_default_family(apr_pool_t *p)
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek{
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#if APR_HAVE_IPV6
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 */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if (default_family == APR_UNSPEC) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_socket_t *tmp_sock;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if (apr_socket_create(&tmp_sock, APR_INET6, SOCK_STREAM,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek p) == APR_SUCCESS) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_socket_close(tmp_sock);
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek default_family = APR_INET6;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek else {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek default_family = APR_INET;
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#endif
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek}
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmekstatic void alloc_listener(process_rec *process, char *addr, apr_port_t port)
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek{
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_listen_rec **walk;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_listen_rec *new;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_status_t status;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek char *oldaddr;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_port_t oldport;
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek apr_sockaddr_t *sa;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
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 switch(default_family) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek case APR_INET:
7b870f607bcb8f445d4d29a776d12d9de3bf1487Jan Engelhardt addr = "0.0.0.0";
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek break;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#if APR_HAVE_IPV6
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek case APR_INET6:
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek addr = "::";
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek break;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek#endif
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek default:
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_assert(1 != 1); /* should not occur */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
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 sa = (*walk)->bind_addr;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* Some listeners are not real so they will not have a bind_addr. */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek if (sa) {
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 new = *walk;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek *walk = new->next;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek new->next = ap_listeners;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ap_listeners = new;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek return;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek
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 new->active = 0;
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 return;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
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);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering }
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering new->next = ap_listeners;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering ap_listeners = new;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering}
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poetteringstatic int ap_listen_open(process_rec *process, apr_port_t port)
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering{
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering apr_pool_t *pconf = process->pconf;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering ap_listen_rec *lr;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering ap_listen_rec *next;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering int num_open;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
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 */
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering num_open = 0;
63ba209d8bc64137ff1585d5878328c93cb81b61Jan Engelhardt for (lr = ap_listeners; lr; lr = lr->next) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (lr->active) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering ++num_open;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering else {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (make_sock(pconf, lr) == APR_SUCCESS) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek ++num_open;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek lr->active = 1;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek else {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* fatal error */
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek return -1;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /* close the old listeners */
21ac6ff143cc8bebfbd1818af28e8c6f82cd5265Zbigniew Jędrzejewski-Szmek for (lr = old_listeners; lr; lr = next) {
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_socket_close(lr->sd);
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek lr->active = 0;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek next = lr->next;
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek /*free(lr);*/
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek }
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering old_listeners = NULL;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek apr_pool_cleanup_register(pconf, NULL, apr_pool_cleanup_null,
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek close_listeners_on_exec);
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek return num_open ? 0 : -1;
}
int ap_setup_listeners(server_rec *s)
{
ap_listen_rec *lr;
int num_listeners = 0;
if (ap_listen_open(s->process, s->port)) {
return 0;
}
for (lr = ap_listeners; lr; lr = lr->next) {
num_listeners++;
}
return num_listeners;
}
void ap_listen_pre_config(void)
{
old_listeners = ap_listeners;
ap_listeners = NULL;
ap_listenbacklog = DEFAULT_LISTENBACKLOG;
}
const char *ap_set_listener(cmd_parms *cmd, void *dummy, const char *ips)
{
char *host, *scope_id;
apr_port_t port;
apr_status_t rv;
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
rv = apr_parse_addr_port(&host, &scope_id, &port, ips, cmd->pool);
if (rv != APR_SUCCESS) {
return "Invalid address or port";
}
if (host && !strcmp(host, "*")) {
host = NULL;
}
if (scope_id) {
/* XXX scope id support is useful with link-local IPv6 addresses */
return "Scope id is not supported";
}
if (!port) {
return "Port must be specified";
}
alloc_listener(cmd->server->process, host, port);
return NULL;
}
const char *ap_set_listenbacklog(cmd_parms *cmd, void *dummy, const char *arg)
{
int b;
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
b = atoi(arg);
if (b < 1) {
return "ListenBacklog must be > 0";
}
ap_listenbacklog = b;
return NULL;
}
const char *ap_set_send_buffer_size(cmd_parms *cmd, void *dummy,
const char *arg)
{
int s = atoi(arg);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
if (s < 512 && s != 0) {
return "SendBufferSize must be >= 512 bytes, or 0 for system default.";
}
send_buffer_size = s;
return NULL;
}