1 /* ====================================================================
2 * Copyright (c) 1998-1999 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
58 #include "apr_network_io.h"
60 #include "http_config.h"
61 #include "ap_listen.h"
64 ap_listen_rec *ap_listeners;
65 static ap_listen_rec *old_listeners;
66 static int ap_listenbacklog;
67 static int send_buffer_size;
69 /* TODO: make_sock is just begging and screaming for APR abstraction */
70 static ap_status_t make_sock(ap_context_t *p, ap_listen_rec *server)
72 ap_socket_t *s = server->sd;
77 ap_cpystrn(addr, "[@main/listen.c:make_sock(): inet_ntoa(server->sin_addr)]", sizeof addr);
79 stat = ap_setsocketopt(s, APR_SO_REUSEADDR, one);
80 if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
81 ap_log_error(APLOG_MARK, APLOG_CRIT, stat, NULL,
82 "make_sock: for %s, setsockopt: (SO_REUSEADDR)", addr);
87 stat = ap_setsocketopt(s, APR_SO_KEEPALIVE, one);
88 if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
89 ap_log_error(APLOG_MARK, APLOG_CRIT, stat, NULL,
90 "make_sock: for %s, setsockopt: (SO_KEEPALIVE)", addr);
96 * To send data over high bandwidth-delay connections at full
97 * speed we must force the TCP window to open wide enough to keep the
98 * pipe full. The default window size on many systems
99 * is only 4kB. Cross-country WAN connections of 100ms
100 * at 1Mb/s are not impossible for well connected sites.
101 * If we assume 100ms cross-country latency,
102 * a 4kB buffer limits throughput to 40kB/s.
104 * To avoid this problem I've added the SendBufferSize directive
105 * to allow the web master to configure send buffer size.
107 * The trade-off of larger buffers is that more kernel memory
108 * is consumed. YMMV, know your customers and your network!
110 * -John Heidemann <johnh@isi.edu> 25-Oct-96
112 * If no size is specified, use the kernel default.
114 if (send_buffer_size) {
115 stat = ap_setsocketopt(s, APR_SO_SNDBUF, send_buffer_size);
116 if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
117 ap_log_error(APLOG_MARK, APLOG_WARNING, stat, NULL,
118 "make_sock: failed to set SendBufferSize for %s, "
119 "using default", addr);
120 /* not a fatal error */
124 if ((stat = ap_bind(s)) != APR_SUCCESS) {
125 ap_log_error(APLOG_MARK, APLOG_CRIT, stat, NULL,
126 "make_sock: could not bind to %s", addr);
131 if ((stat = ap_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
132 ap_log_error(APLOG_MARK, APLOG_ERR, stat, NULL,
133 "make_sock: unable to listen for connections on %s", addr);
144 static ap_status_t close_listeners_on_exec(void *v)
148 for (lr = ap_listeners; lr; lr = lr->next) {
149 ap_close_socket(lr->sd);
156 static void alloc_listener(process_rec *process, char *addr, unsigned int port)
158 ap_listen_rec **walk;
162 unsigned int oldport;
164 /* see if we've got an old listener for this address:port */
165 for (walk = &old_listeners; *walk; walk = &(*walk)->next) {
166 ap_get_local_port(&oldport, (*walk)->sd);
167 ap_get_local_ipaddr(&oldaddr,(*walk)->sd);
168 if (!strcmp(oldaddr, addr) && port == oldport) {
169 /* re-use existing record */
172 new->next = ap_listeners;
178 /* this has to survive restarts */
179 /* XXX - We need to deal with freeing this structure properly. */
180 new = ap_palloc(process->pool, sizeof(ap_listen_rec));
182 if ((status = ap_create_tcp_socket(&new->sd, NULL)) != APR_SUCCESS) {
183 ap_log_error(APLOG_MARK, APLOG_CRIT, status, NULL,
184 "make_sock: failed to get a socket for %s", addr);
187 ap_set_local_port(new->sd, port);
188 ap_set_local_ipaddr(new->sd, addr);
189 new->next = ap_listeners;
194 int ap_listen_open(process_rec *process, unsigned port)
196 ap_context_t *pconf = process->pconf;
201 /* allocate a default listener if necessary */
202 if (ap_listeners == NULL) {
203 alloc_listener(process, APR_ANYADDR, port ? port : DEFAULT_HTTP_PORT);
207 for (lr = ap_listeners; lr; lr = lr->next) {
212 if (make_sock(pconf, lr) == APR_SUCCESS) {
219 /* close the old listeners */
220 for (lr = old_listeners; lr; lr = next) {
221 ap_close_socket(lr->sd);
226 old_listeners = NULL;
228 ap_register_cleanup(pconf, NULL, ap_null_cleanup, close_listeners_on_exec);
230 return num_open ? 0 : -1;
234 void ap_listen_pre_config(void)
236 old_listeners = ap_listeners;
238 ap_listenbacklog = DEFAULT_LISTENBACKLOG;
242 const char *ap_set_listener(cmd_parms *cmd, void *dummy, char *ips)
247 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
252 ports = strchr(ips, ':');
255 return "Missing IP address";
257 else if (ports[1] == '\0') {
258 return "Address must end in :<port-number>";
268 return "Port must be numeric";
271 if (ports == ips) { /* no address */
272 alloc_listener(cmd->server->process, APR_ANYADDR, port);
275 ips[(ports - ips) - 1] = '\0';
276 alloc_listener(cmd->server->process, ips, port);
282 const char *ap_set_listenbacklog(cmd_parms *cmd, void *dummy, char *arg)
286 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
293 return "ListenBacklog must be > 0";
295 ap_listenbacklog = b;
299 const char *ap_set_send_buffer_size(cmd_parms *cmd, void *dummy, char *arg)
302 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
307 if (s < 512 && s != 0) {
308 return "SendBufferSize must be >= 512 bytes, or 0 for system default.";
310 send_buffer_size = s;