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