listen.c revision ab5581cc78e9d865b0a6ab1404c53347b3276968
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance/* ====================================================================
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * Copyright (c) 1998-1999 The Apache Group. All rights reserved.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * Redistribution and use in source and binary forms, with or without
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * modification, are permitted provided that the following conditions
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 1. Redistributions of source code must retain the above copyright
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * notice, this list of conditions and the following disclaimer.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 2. Redistributions in binary form must reproduce the above copyright
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * notice, this list of conditions and the following disclaimer in
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * the documentation and/or other materials provided with the
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * distribution.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 3. All advertising materials mentioning features or use of this
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * software must display the following acknowledgment:
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * "This product includes software developed by the Apache Group
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * for use in the Apache HTTP server project (http://www.apache.org/)."
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 4. The names "Apache Server" and "Apache Group" must not be used to
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * endorse or promote products derived from this software without
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * prior written permission. For written permission, please contact
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * apache@apache.org.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 5. Products derived from this software may not be called "Apache"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * nor may "Apache" appear in their names without prior written
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * permission of the Apache Group.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 6. Redistributions of any form whatsoever must retain the following
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * acknowledgment:
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * "This product includes software developed by the Apache Group
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * for use in the Apache HTTP server project (http://www.apache.org/)."
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * OF THE POSSIBILITY OF SUCH DAMAGE.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * ====================================================================
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * This software consists of voluntary contributions made by many
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * individuals on behalf of the Apache Group and was originally based
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * on public domain software written at the National Center for
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * Supercomputing Applications, University of Illinois, Urbana-Champaign.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * For more information on the Apache Group and the Apache HTTP server
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * project, please see <http://www.apache.org/>.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance/* TODO: make_sock is just begging and screaming for APR abstraction */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mancestatic ap_status_t make_sock(ap_context_t *p, ap_listen_rec *server)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance stat = ap_setsocketopt(s, APR_SO_REUSEADDR, one);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_log_error(APLOG_MARK, APLOG_CRIT, NULL,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance "make_sock: for %s, setsockopt: (SO_REUSEADDR)", addr);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance stat = ap_setsocketopt(s, APR_SO_KEEPALIVE, one);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_log_error(APLOG_MARK, APLOG_CRIT, NULL,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance "make_sock: for %s, setsockopt: (SO_KEEPALIVE)", addr);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * To send data over high bandwidth-delay connections at full
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * speed we must force the TCP window to open wide enough to keep the
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * pipe full. The default window size on many systems
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * is only 4kB. Cross-country WAN connections of 100ms
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * at 1Mb/s are not impossible for well connected sites.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * If we assume 100ms cross-country latency,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * a 4kB buffer limits throughput to 40kB/s.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * To avoid this problem I've added the SendBufferSize directive
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * to allow the web master to configure send buffer size.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * The trade-off of larger buffers is that more kernel memory
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * is consumed. YMMV, know your customers and your network!
a6526952d69bccd048c954eb920493a6a83e78faFelix Gabriel Mance * -John Heidemann <johnh@isi.edu> 25-Oct-96
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * If no size is specified, use the kernel default.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance stat = ap_setsocketopt(s, SO_SNDBUF, send_buffer_size);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_log_error(APLOG_MARK, APLOG_WARNING, NULL,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance "make_sock: failed to set SendBufferSize for %s, "
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance /* not a fatal error */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if ((stat = ap_bind(s)) != APR_SUCCESS) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_log_error(APLOG_MARK, APLOG_CRIT, NULL,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance "make_sock: could not bind to %s", addr);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if ((stat = ap_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_log_error(APLOG_MARK, APLOG_ERR, NULL,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance "make_sock: unable to listen for connections on %s", addr);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mancestatic ap_status_t close_listeners_on_exec(void *v)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance for (lr = ap_listeners; lr; lr = lr->next) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mancestatic void alloc_listener(char *addr, unsigned int port)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance /* see if we've got an old listener for this address:port */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance for (walk = &old_listeners; *walk; walk = &(*walk)->next) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (!strcmp(oldaddr, addr) && port == oldport) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance /* re-use existing record */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance /* this has to survive restarts */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (ap_create_tcp_socket(NULL, &new->sd) != APR_SUCCESS) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_log_error(APLOG_MARK, APLOG_CRIT, NULL,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance "make_sock: failed to get a socket for %s", addr);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceint ap_listen_open(ap_context_t *pconf, unsigned port)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance /* allocate a default listener if necessary */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance alloc_listener(APR_ANYADDR, port ? port : DEFAULT_HTTP_PORT);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance for (lr = ap_listeners; lr; lr = lr->next) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance /* close the old listeners */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance for (lr = old_listeners; lr; lr = next) {
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance ap_register_cleanup(pconf, NULL, ap_null_cleanup, close_listeners_on_exec);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_listenbacklog = DEFAULT_LISTENBACKLOG;
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Manceconst char *ap_set_listener(cmd_parms *cmd, void *dummy, char *ips)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance unsigned short port;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (!port) {
return NULL;
return err;
ap_listenbacklog = b;
return NULL;
return err;
send_buffer_size = s;
return NULL;