listen.c revision 3c4f451140dd2ba70ee77274ed8155f451184917
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* ====================================================================
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * The Apache Software License, Version 1.1
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * reserved.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Redistribution and use in source and binary forms, with or without
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * modification, are permitted provided that the following conditions
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * are met:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 1. Redistributions of source code must retain the above copyright
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * notice, this list of conditions and the following disclaimer.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * 2. Redistributions in binary form must reproduce the above copyright
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * notice, this list of conditions and the following disclaimer in
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * the documentation and/or other materials provided with the
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * distribution.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 3. The end-user documentation included with the redistribution,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * if any, must include the following acknowledgment:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * "This product includes software developed by the
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Apache Software Foundation (http://www.apache.org/)."
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Alternately, this acknowledgment may appear in the software itself,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * if and wherever such third-party acknowledgments normally appear.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 4. The names "Apache" and "Apache Software Foundation" must
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * not be used to endorse or promote products derived from this
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * software without prior written permission. For written
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * permission, please contact apache@apache.org.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * 5. Products derived from this software may not be called "Apache",
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * nor may "Apache" appear in their name, without prior written
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * permission of the Apache Software Foundation.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * SUCH DAMAGE.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * ====================================================================
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * This software consists of voluntary contributions made by many
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * individuals on behalf of the Apache Software Foundation. For more
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * information on the Apache Software Foundation, please see
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * <http://www.apache.org/>.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * Portions of this software are based upon public domain software
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * originally written at the National Center for Supercomputing Applications,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * University of Illinois, Urbana-Champaign.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "apr_network_io.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "apr_strings.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#define APR_WANT_STRFUNC
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "apr_want.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#define CORE_PRIVATE
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "ap_config.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "httpd.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "http_config.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "ap_listen.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "http_log.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "mpm.h"
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse#include "mpm_common.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseap_listen_rec *ap_listeners;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#if APR_HAVE_IPV6
05413593151a238718198cc04ca849b2426be106rsestatic int default_family = APR_UNSPEC;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#else
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic int default_family = APR_INET;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#endif
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic ap_listen_rec *old_listeners;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic int ap_listenbacklog;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic int send_buffer_size;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* TODO: make_sock is just begging and screaming for APR abstraction */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_socket_t *s = server->sd;
05413593151a238718198cc04ca849b2426be106rse int one = 1;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_status_t stat;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse stat = apr_setsocketopt(s, APR_SO_REUSEADDR, one);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "make_sock: for address %pI, setsockopt: (SO_REUSEADDR)",
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse server->bind_addr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_socket_close(s);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return stat;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse stat = apr_setsocketopt(s, APR_SO_KEEPALIVE, one);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "make_sock: for address %pI, setsockopt: (SO_KEEPALIVE)",
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse server->bind_addr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_socket_close(s);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return stat;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /*
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * To send data over high bandwidth-delay connections at full
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * speed we must force the TCP window to open wide enough to keep the
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * pipe full. The default window size on many systems
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * is only 4kB. Cross-country WAN connections of 100ms
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * at 1Mb/s are not impossible for well connected sites.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * If we assume 100ms cross-country latency,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * a 4kB buffer limits throughput to 40kB/s.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * To avoid this problem I've added the SendBufferSize directive
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * to allow the web master to configure send buffer size.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * The trade-off of larger buffers is that more kernel memory
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * is consumed. YMMV, know your customers and your network!
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * -John Heidemann <johnh@isi.edu> 25-Oct-96
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * If no size is specified, use the kernel default.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (send_buffer_size) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse stat = apr_setsocketopt(s, APR_SO_SNDBUF, send_buffer_size);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_log_perror(APLOG_MARK, APLOG_WARNING, stat, p,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "make_sock: failed to set SendBufferSize for "
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "address %pI, using default",
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse server->bind_addr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* not a fatal error */
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse#if APR_TCP_NODELAY_INHERITED
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_sock_disable_nagle(s);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse#endif
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse if ((stat = apr_bind(s, server->bind_addr)) != APR_SUCCESS) {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse "make_sock: could not bind to address %pI",
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse server->bind_addr);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse apr_socket_close(s);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse return stat;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse }
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse if ((stat = apr_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse ap_log_perror(APLOG_MARK, APLOG_ERR, stat, p,
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse "make_sock: unable to listen for connections on address %pI",
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse server->bind_addr);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse apr_socket_close(s);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return stat;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#ifdef APR_HAS_SO_ACCEPTFILTER
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse#ifndef ACCEPT_FILTER_NAME
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#define ACCEPT_FILTER_NAME "dataready"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#endif
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_socket_accept_filter(s, ACCEPT_FILTER_NAME, "");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#endif
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse server->sd = s;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse server->active = 1;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return APR_SUCCESS;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rsestatic apr_status_t close_listeners_on_exec(void *v)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_listen_rec *lr;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse for (lr = ap_listeners; lr; lr = lr->next) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_socket_close(lr->sd);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse lr->active = 0;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return APR_SUCCESS;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic void find_default_family(apr_pool_t *p)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#if APR_HAVE_IPV6
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* We know the platform supports IPv6, but this particular
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * system may not have IPv6 enabled. See if we can get an
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * AF_INET6 socket.
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (default_family == APR_UNSPEC) {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse apr_socket_t *tmp_sock;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (apr_socket_create(&tmp_sock, APR_INET6, SOCK_STREAM, APR_INHERIT,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse p) == APR_SUCCESS) {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse apr_socket_close(tmp_sock);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse default_family = APR_INET6;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse else {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse default_family = APR_INET;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse#endif
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic void alloc_listener(process_rec *process, char *addr, apr_port_t port)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_listen_rec **walk;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_listen_rec *new;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_status_t status;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *oldaddr;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_port_t oldport;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_sockaddr_t *sa;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (!addr) { /* don't bind to specific interface */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse find_default_family(process->pool);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse switch(default_family) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse case APR_INET:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse addr = "0.0.0.0";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse break;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#if APR_HAVE_IPV6
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse case APR_INET6:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse addr = "::";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse break;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#endif
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse default:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_assert(1 != 1); /* should not occur */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* see if we've got an old listener for this address:port */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse for (walk = &old_listeners; *walk; walk = &(*walk)->next) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse sa = (*walk)->bind_addr;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_sockaddr_port_get(&oldport, sa);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_sockaddr_ip_get(&oldaddr, sa);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (!strcmp(oldaddr, addr) && port == oldport) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* re-use existing record */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse new = *walk;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *walk = new->next;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse new->next = ap_listeners;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_listeners = new;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* this has to survive restarts */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse new = apr_palloc(process->pool, sizeof(ap_listen_rec));
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse new->active = 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if ((status = apr_sockaddr_info_get(&new->bind_addr, addr, APR_UNSPEC, port, 0,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse process->pool)) != APR_SUCCESS) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "alloc_listener: failed to set up sockaddr for %s", addr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if ((status = apr_socket_create(&new->sd, new->bind_addr->sa.sin.sin_family,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse SOCK_STREAM, APR_INHERIT, process->pool)) != APR_SUCCESS) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "alloc_listener: failed to get a socket for %s", addr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse new->next = ap_listeners;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_listeners = new;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#if !defined(SPMT_OS2_MPM)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#endif
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseint ap_listen_open(process_rec *process, apr_port_t port)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_pool_t *pconf = process->pconf;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_listen_rec *lr;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_listen_rec *next;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int num_open;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* allocate a default listener if necessary */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (ap_listeners == NULL) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse alloc_listener(process, NULL, port ? port : DEFAULT_HTTP_PORT);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse num_open = 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse for (lr = ap_listeners; lr; lr = lr->next) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (lr->active) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ++num_open;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (make_sock(pconf, lr) == APR_SUCCESS) {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse ++num_open;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse lr->active = 1;
184f5da95d14895f7f33c90b8b8f70653afb0d92wrowe }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
184f5da95d14895f7f33c90b8b8f70653afb0d92wrowe }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* close the old listeners */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse for (lr = old_listeners; lr; lr = next) {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse apr_socket_close(lr->sd);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse lr->active = 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse next = lr->next;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* free(lr);*/
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
a943533fd4d91d114af622731a405407990c4fb1rse old_listeners = NULL;
a943533fd4d91d114af622731a405407990c4fb1rse
a943533fd4d91d114af622731a405407990c4fb1rse apr_pool_cleanup_register(pconf, NULL, apr_pool_cleanup_null, close_listeners_on_exec);
a943533fd4d91d114af622731a405407990c4fb1rse
a943533fd4d91d114af622731a405407990c4fb1rse return num_open ? 0 : -1;
a943533fd4d91d114af622731a405407990c4fb1rse}
a943533fd4d91d114af622731a405407990c4fb1rse
a943533fd4d91d114af622731a405407990c4fb1rseint ap_setup_listeners(server_rec *s)
a943533fd4d91d114af622731a405407990c4fb1rse{
a943533fd4d91d114af622731a405407990c4fb1rse ap_listen_rec *lr;
a943533fd4d91d114af622731a405407990c4fb1rse int num_listeners = 0;
a943533fd4d91d114af622731a405407990c4fb1rse if (ap_listen_open(s->process, s->port)) {
a943533fd4d91d114af622731a405407990c4fb1rse return 0;
a943533fd4d91d114af622731a405407990c4fb1rse }
a943533fd4d91d114af622731a405407990c4fb1rse for (lr = ap_listeners; lr; lr = lr->next) {
a943533fd4d91d114af622731a405407990c4fb1rse num_listeners++;
a943533fd4d91d114af622731a405407990c4fb1rse }
a943533fd4d91d114af622731a405407990c4fb1rse return num_listeners;
a943533fd4d91d114af622731a405407990c4fb1rse}
a943533fd4d91d114af622731a405407990c4fb1rse
a943533fd4d91d114af622731a405407990c4fb1rsevoid ap_listen_pre_config(void)
a943533fd4d91d114af622731a405407990c4fb1rse{
a943533fd4d91d114af622731a405407990c4fb1rse old_listeners = ap_listeners;
a943533fd4d91d114af622731a405407990c4fb1rse ap_listeners = NULL;
a943533fd4d91d114af622731a405407990c4fb1rse ap_listenbacklog = DEFAULT_LISTENBACKLOG;
a943533fd4d91d114af622731a405407990c4fb1rse}
a943533fd4d91d114af622731a405407990c4fb1rse
a943533fd4d91d114af622731a405407990c4fb1rse
a943533fd4d91d114af622731a405407990c4fb1rseconst char *ap_set_listener(cmd_parms *cmd, void *dummy, const char *ips)
a943533fd4d91d114af622731a405407990c4fb1rse{
a943533fd4d91d114af622731a405407990c4fb1rse 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;
}