]> granicus.if.org Git - apache/blob - server/listen.c
8ec09dc026098e8a7a44298d525fba5d461818b9
[apache] / server / listen.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_network_io.h"
18 #include "apr_strings.h"
19
20 #define APR_WANT_STRFUNC
21 #include "apr_want.h"
22
23 #include "ap_config.h"
24 #include "httpd.h"
25 #include "http_config.h"
26 #include "http_core.h"
27 #include "ap_listen.h"
28 #include "http_log.h"
29 #include "mpm.h"
30 #include "mpm_common.h"
31
32 AP_DECLARE_DATA ap_listen_rec *ap_listeners = NULL;
33
34 static ap_listen_rec *old_listeners;
35 static int ap_listenbacklog;
36 static int send_buffer_size;
37 static int receive_buffer_size;
38
39 /* TODO: make_sock is just begging and screaming for APR abstraction */
40 static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
41 {
42     apr_socket_t *s = server->sd;
43     int one = 1;
44 #if APR_HAVE_IPV6
45 #ifdef AP_ENABLE_V4_MAPPED
46     int v6only_setting = 0;
47 #else
48     int v6only_setting = 1;
49 #endif
50 #endif
51     apr_status_t stat;
52
53 #ifndef WIN32
54     stat = apr_socket_opt_set(s, APR_SO_REUSEADDR, one);
55     if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
56         ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
57                       "make_sock: for address %pI, apr_socket_opt_set: (SO_REUSEADDR)",
58                       server->bind_addr);
59         apr_socket_close(s);
60         return stat;
61     }
62 #endif
63
64     stat = apr_socket_opt_set(s, APR_SO_KEEPALIVE, one);
65     if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
66         ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
67                       "make_sock: for address %pI, apr_socket_opt_set: (SO_KEEPALIVE)",
68                       server->bind_addr);
69         apr_socket_close(s);
70         return stat;
71     }
72
73 #if APR_HAVE_IPV6
74     if (server->bind_addr->family == APR_INET6) {
75         stat = apr_socket_opt_set(s, APR_IPV6_V6ONLY, v6only_setting);
76         if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
77             ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
78                           "make_sock: for address %pI, apr_socket_opt_set: "
79                           "(IPV6_V6ONLY)",
80                           server->bind_addr);
81             apr_socket_close(s);
82             return stat;
83         }
84     }
85 #endif
86
87     /*
88      * To send data over high bandwidth-delay connections at full
89      * speed we must force the TCP window to open wide enough to keep the
90      * pipe full.  The default window size on many systems
91      * is only 4kB.  Cross-country WAN connections of 100ms
92      * at 1Mb/s are not impossible for well connected sites.
93      * If we assume 100ms cross-country latency,
94      * a 4kB buffer limits throughput to 40kB/s.
95      *
96      * To avoid this problem I've added the SendBufferSize directive
97      * to allow the web master to configure send buffer size.
98      *
99      * The trade-off of larger buffers is that more kernel memory
100      * is consumed.  YMMV, know your customers and your network!
101      *
102      * -John Heidemann <johnh@isi.edu> 25-Oct-96
103      *
104      * If no size is specified, use the kernel default.
105      */
106     if (send_buffer_size) {
107         stat = apr_socket_opt_set(s, APR_SO_SNDBUF,  send_buffer_size);
108         if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
109             ap_log_perror(APLOG_MARK, APLOG_WARNING, stat, p,
110                           "make_sock: failed to set SendBufferSize for "
111                           "address %pI, using default",
112                           server->bind_addr);
113             /* not a fatal error */
114         }
115     }
116     if (receive_buffer_size) {
117         stat = apr_socket_opt_set(s, APR_SO_RCVBUF, receive_buffer_size);
118         if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
119             ap_log_perror(APLOG_MARK, APLOG_WARNING, stat, p,
120                           "make_sock: failed to set ReceiveBufferSize for "
121                           "address %pI, using default",
122                           server->bind_addr);
123             /* not a fatal error */
124         }
125     }
126
127 #if APR_TCP_NODELAY_INHERITED
128     ap_sock_disable_nagle(s);
129 #endif
130
131     if ((stat = apr_socket_bind(s, server->bind_addr)) != APR_SUCCESS) {
132         ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p,
133                       "make_sock: could not bind to address %pI",
134                       server->bind_addr);
135         apr_socket_close(s);
136         return stat;
137     }
138
139     if ((stat = apr_socket_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
140         ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p,
141                       "make_sock: unable to listen for connections "
142                       "on address %pI",
143                       server->bind_addr);
144         apr_socket_close(s);
145         return stat;
146     }
147
148 #ifdef WIN32
149     /* I seriously doubt that this would work on Unix; I have doubts that
150      * it entirely solves the problem on Win32.  However, since setting
151      * reuseaddr on the listener -prior- to binding the socket has allowed
152      * us to attach to the same port as an already running instance of
153      * Apache, or even another web server, we cannot identify that this
154      * port was exclusively granted to this instance of Apache.
155      *
156      * So set reuseaddr, but do not attempt to do so until we have the
157      * parent listeners successfully bound.
158      */
159     stat = apr_socket_opt_set(s, APR_SO_REUSEADDR, one);
160     if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
161         ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p,
162                     "make_sock: for address %pI, apr_socket_opt_set: (SO_REUSEADDR)",
163                      server->bind_addr);
164         apr_socket_close(s);
165         return stat;
166     }
167 #endif
168
169     server->sd = s;
170     server->active = 1;
171
172 #ifdef MPM_ACCEPT_FUNC
173     server->accept_func = MPM_ACCEPT_FUNC;
174 #else
175     server->accept_func = NULL;
176 #endif
177
178     return APR_SUCCESS;
179 }
180
181 static const char* find_accf_name(server_rec *s, const char *proto)
182 {
183     const char* accf;
184     core_server_config *conf = ap_get_module_config(s->module_config,
185                                                     &core_module);
186     if (!proto) {
187         return NULL;
188     }
189
190     accf = apr_table_get(conf->accf_map, proto);
191
192     if (accf && !strcmp("none", accf)) {
193         return NULL;
194     }
195
196     return accf;
197 }
198
199 static void ap_apply_accept_filter(apr_pool_t *p, ap_listen_rec *lis,
200                                            server_rec *server)
201 {
202     apr_socket_t *s = lis->sd;
203     const char *accf;
204     apr_status_t rv;
205     const char *proto;
206
207     proto = lis->protocol;
208
209     if (!proto) {
210         proto = ap_get_server_protocol(server);
211     }
212
213
214     accf = find_accf_name(server, proto);
215
216     if (accf) {
217 #if APR_HAS_SO_ACCEPTFILTER
218         rv = apr_socket_accept_filter(s, apr_pstrdup(p, accf),
219                                       apr_pstrdup(p,""));
220         if (rv != APR_SUCCESS && !APR_STATUS_IS_ENOTIMPL(rv)) {
221             ap_log_perror(APLOG_MARK, APLOG_WARNING, rv, p,
222                           "Failed to enable the '%s' Accept Filter",
223                           accf);
224         }
225 #else
226 #ifdef APR_TCP_DEFER_ACCEPT
227         rv = apr_socket_opt_set(s, APR_TCP_DEFER_ACCEPT, 30);
228         if (rv != APR_SUCCESS && !APR_STATUS_IS_ENOTIMPL(rv)) {
229             ap_log_perror(APLOG_MARK, APLOG_WARNING, rv, p,
230                               "Failed to enable APR_TCP_DEFER_ACCEPT");
231         }
232 #endif
233 #endif
234     }
235 }
236
237 static apr_status_t close_listeners_on_exec(void *v)
238 {
239     ap_close_listeners();
240     return APR_SUCCESS;
241 }
242
243 static const char *alloc_listener(process_rec *process, char *addr,
244                                   apr_port_t port, const char* proto)
245 {
246     ap_listen_rec **walk, *last;
247     apr_status_t status;
248     apr_sockaddr_t *sa;
249     int found_listener = 0;
250
251     /* see if we've got an old listener for this address:port */
252     for (walk = &old_listeners; *walk;) {
253         sa = (*walk)->bind_addr;
254         /* Some listeners are not real so they will not have a bind_addr. */
255         if (sa) {
256             ap_listen_rec *new;
257             apr_port_t oldport;
258
259             oldport = sa->port;
260             /* If both ports are equivalent, then if their names are equivalent,
261              * then we will re-use the existing record.
262              */
263             if (port == oldport &&
264                 ((!addr && !sa->hostname) ||
265                  ((addr && sa->hostname) && !strcmp(sa->hostname, addr)))) {
266                 new = *walk;
267                 *walk = new->next;
268                 new->next = ap_listeners;
269                 ap_listeners = new;
270                 found_listener = 1;
271                 continue;
272             }
273         }
274
275         walk = &(*walk)->next;
276     }
277
278     if (found_listener) {
279         return NULL;
280     }
281
282     if ((status = apr_sockaddr_info_get(&sa, addr, APR_UNSPEC, port, 0,
283                                         process->pool))
284         != APR_SUCCESS) {
285         ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool,
286                       "alloc_listener: failed to set up sockaddr for %s",
287                       addr);
288         return "Listen setup failed";
289     }
290
291     /* Initialize to our last configured ap_listener. */
292     last = ap_listeners;
293     while (last && last->next) {
294         last = last->next;
295     }
296
297     while (sa) {
298         ap_listen_rec *new;
299
300         /* this has to survive restarts */
301         new = apr_palloc(process->pool, sizeof(ap_listen_rec));
302         new->active = 0;
303         new->next = 0;
304         new->bind_addr = sa;
305         new->protocol = apr_pstrdup(process->pool, proto);
306
307         /* Go to the next sockaddr. */
308         sa = sa->next;
309
310         status = apr_socket_create(&new->sd, new->bind_addr->family,
311                                     SOCK_STREAM, 0, process->pool);
312
313 #if APR_HAVE_IPV6
314         /* What could happen is that we got an IPv6 address, but this system
315          * doesn't actually support IPv6.  Try the next address.
316          */
317         if (status != APR_SUCCESS && !addr &&
318             new->bind_addr->family == APR_INET6) {
319             continue;
320         }
321 #endif
322         if (status != APR_SUCCESS) {
323             ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool,
324                           "alloc_listener: failed to get a socket for %s",
325                           addr);
326             return "Listen setup failed";
327         }
328
329         /* We need to preserve the order returned by getaddrinfo() */
330         if (last == NULL) {
331             ap_listeners = last = new;
332         } else {
333             last->next = new;
334             last = new;
335         }
336     }
337
338     return NULL;
339 }
340 /* Evaluates to true if the (apr_sockaddr_t *) addr argument is the
341  * IPv4 match-any-address, 0.0.0.0. */
342 #define IS_INADDR_ANY(addr) ((addr)->family == APR_INET \
343                              && (addr)->sa.sin.sin_addr.s_addr == INADDR_ANY)
344
345 /* Evaluates to true if the (apr_sockaddr_t *) addr argument is the
346  * IPv6 match-any-address, [::]. */
347 #define IS_IN6ADDR_ANY(addr) ((addr)->family == APR_INET6 \
348                               && IN6_IS_ADDR_UNSPECIFIED(&(addr)->sa.sin6.sin6_addr))
349
350 /**
351  * Create, open, listen, and bind all sockets.
352  * @param process The process record for the currently running server
353  * @return The number of open sockets
354  */
355 static int open_listeners(apr_pool_t *pool)
356 {
357     ap_listen_rec *lr;
358     ap_listen_rec *next;
359     ap_listen_rec *previous;
360     int num_open;
361     const char *userdata_key = "ap_open_listeners";
362     void *data;
363 #if AP_NONBLOCK_WHEN_MULTI_LISTEN
364     int use_nonblock;
365 #endif
366
367     /* Don't allocate a default listener.  If we need to listen to a
368      * port, then the user needs to have a Listen directive in their
369      * config file.
370      */
371     num_open = 0;
372     previous = NULL;
373     for (lr = ap_listeners; lr; previous = lr, lr = lr->next) {
374         if (lr->active) {
375             ++num_open;
376         }
377         else {
378 #if APR_HAVE_IPV6
379             ap_listen_rec *cur;
380             int v6only_setting;
381             int skip = 0;
382
383             /* If we have the unspecified IPv4 address (0.0.0.0) and
384              * the unspecified IPv6 address (::) is next, we need to
385              * swap the order of these in the list. We always try to
386              * bind to IPv6 first, then IPv4, since an IPv6 socket
387              * might be able to receive IPv4 packets if V6ONLY is not
388              * enabled, but never the other way around.
389              * Note: In some configurations, the unspecified IPv6 address
390              * could be even later in the list.  This logic only corrects
391              * the situation where it is next in the list, such as when
392              * apr_sockaddr_info_get() returns an IPv4 and an IPv6 address,
393              * in that order.
394              */
395             if (lr->next != NULL
396                 && IS_INADDR_ANY(lr->bind_addr)
397                 && lr->bind_addr->port == lr->next->bind_addr->port
398                 && IS_IN6ADDR_ANY(lr->next->bind_addr)) {
399                 /* Exchange lr and lr->next */
400                 next = lr->next;
401                 lr->next = next->next;
402                 next->next = lr;
403                 if (previous) {
404                     previous->next = next;
405                 }
406                 else {
407                     ap_listeners = next;
408                 }
409                 lr = next;
410             }
411
412             /* If we are trying to bind to 0.0.0.0 and a previous listener
413              * was :: on the same port and in turn that socket does not have
414              * the IPV6_V6ONLY flag set; we must skip the current attempt to
415              * listen (which would generate an error). IPv4 will be handled
416              * on the established IPv6 socket.
417              */
418             if (IS_INADDR_ANY(lr->bind_addr)) {
419                 for (cur = ap_listeners; cur != lr; cur = cur->next) {
420                     if (lr->bind_addr->port == cur->bind_addr->port
421                         && IS_IN6ADDR_ANY(cur->bind_addr)
422                         && apr_socket_opt_get(cur->sd, APR_IPV6_V6ONLY,
423                                               &v6only_setting) == APR_SUCCESS
424                         && v6only_setting == 0) {
425
426                         /* Remove the current listener from the list */
427                         previous->next = lr->next;
428                         lr = previous; /* maintain current value of previous after
429                                         * post-loop expression is evaluated
430                                         */
431                         skip = 1;
432                         break;
433                     }
434                 }
435                 if (skip) {
436                     continue;
437                 }
438             }
439 #endif
440             if (make_sock(pool, lr) == APR_SUCCESS) {
441                 ++num_open;
442                 lr->active = 1;
443             }
444             else {
445 #if APR_HAVE_IPV6
446                 /* If we tried to bind to ::, and the next listener is
447                  * on 0.0.0.0 with the same port, don't give a fatal
448                  * error. The user will still get a warning from make_sock
449                  * though.
450                  */
451                 if (lr->next != NULL
452                     && IS_IN6ADDR_ANY(lr->bind_addr)
453                     && lr->bind_addr->port == lr->next->bind_addr->port
454                     && IS_INADDR_ANY(lr->next->bind_addr)) {
455
456                     /* Remove the current listener from the list */
457                     if (previous) {
458                         previous->next = lr->next;
459                     }
460                     else {
461                         ap_listeners = lr->next;
462                     }
463
464                     /* Although we've removed ourselves from the list,
465                      * we need to make sure that the next iteration won't
466                      * consider "previous" a working IPv6 '::' socket.
467                      * Changing the family is enough to make sure the
468                      * conditions before make_sock() fail.
469                      */
470                     lr->bind_addr->family = AF_INET;
471
472                     continue;
473                 }
474 #endif
475                 /* fatal error */
476                 return -1;
477             }
478         }
479     }
480
481     /* close the old listeners */
482     for (lr = old_listeners; lr; lr = next) {
483         apr_socket_close(lr->sd);
484         lr->active = 0;
485         next = lr->next;
486     }
487     old_listeners = NULL;
488
489 #if AP_NONBLOCK_WHEN_MULTI_LISTEN
490     /* if multiple listening sockets, make them non-blocking so that
491      * if select()/poll() reports readability for a reset connection that
492      * is already forgotten about by the time we call accept, we won't
493      * be hung until another connection arrives on that port
494      */
495     use_nonblock = (ap_listeners && ap_listeners->next);
496     for (lr = ap_listeners; lr; lr = lr->next) {
497         apr_status_t status;
498
499         status = apr_socket_opt_set(lr->sd, APR_SO_NONBLOCK, use_nonblock);
500         if (status != APR_SUCCESS) {
501             ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, status, pool,
502                           "unable to control socket non-blocking status");
503             return -1;
504         }
505     }
506 #endif /* AP_NONBLOCK_WHEN_MULTI_LISTEN */
507
508     /* we come through here on both passes of the open logs phase
509      * only register the cleanup once... otherwise we try to close
510      * listening sockets twice when cleaning up prior to exec
511      */
512     apr_pool_userdata_get(&data, userdata_key, pool);
513     if (!data) {
514         apr_pool_userdata_set((const void *)1, userdata_key,
515                               apr_pool_cleanup_null, pool);
516         apr_pool_cleanup_register(pool, NULL, apr_pool_cleanup_null,
517                                   close_listeners_on_exec);
518     }
519
520     return num_open ? 0 : -1;
521 }
522
523 AP_DECLARE(int) ap_setup_listeners(server_rec *s)
524 {
525     server_rec *ls;
526     server_addr_rec *addr;
527     ap_listen_rec *lr;
528     int num_listeners = 0;
529     const char* proto;
530     int found;
531
532     for (ls = s; ls; ls = ls->next) {
533         proto = ap_get_server_protocol(ls);
534         if (!proto) {
535             found = 0;
536             /* No protocol was set for this vhost,
537              * use the default for this listener.
538              */
539             for (addr = ls->addrs; addr && !found; addr = addr->next) {
540                 for (lr = ap_listeners; lr; lr = lr->next) {
541                     if (apr_sockaddr_equal(lr->bind_addr, addr->host_addr) &&
542                         lr->bind_addr->port == addr->host_port) {
543                         ap_set_server_protocol(ls, lr->protocol);
544                         found = 1;
545                         break;
546                     }
547                 }
548             }
549
550             if (!found) {
551                 /* TODO: set protocol defaults per-Port, eg 25=smtp */
552                 ap_set_server_protocol(ls, "http");
553             }
554         }
555     }
556
557     if (open_listeners(s->process->pool)) {
558        return 0;
559     }
560
561     for (lr = ap_listeners; lr; lr = lr->next) {
562         num_listeners++;
563         found = 0;
564         for (ls = s; ls && !found; ls = ls->next) {
565             for (addr = ls->addrs; addr && !found; addr = addr->next) {
566                 if (apr_sockaddr_equal(lr->bind_addr, addr->host_addr) &&
567                     lr->bind_addr->port == addr->host_port) {
568                     found = 1;
569                     ap_apply_accept_filter(s->process->pool, lr, ls);
570                 }
571             }
572         }
573
574         if (!found) {
575             ap_apply_accept_filter(s->process->pool, lr, s);
576         }
577     }
578
579     return num_listeners;
580 }
581
582 AP_DECLARE_NONSTD(void) ap_close_listeners(void)
583 {
584     ap_listen_rec *lr;
585
586     for (lr = ap_listeners; lr; lr = lr->next) {
587         apr_socket_close(lr->sd);
588         lr->active = 0;
589     }
590 }
591
592 AP_DECLARE(void) ap_listen_pre_config(void)
593 {
594     old_listeners = ap_listeners;
595     ap_listeners = NULL;
596     ap_listenbacklog = DEFAULT_LISTENBACKLOG;
597 }
598
599
600 AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
601                                                 int argc, char *const argv[])
602 {
603     char *host, *scope_id, *proto;
604     apr_port_t port;
605     apr_status_t rv;
606     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
607
608     if (err != NULL) {
609         return err;
610     }
611
612     if (argc < 1 || argc > 2) {
613         return "Listen requires 1 or 2 arguments.";
614     }
615
616     rv = apr_parse_addr_port(&host, &scope_id, &port, argv[0], cmd->pool);
617     if (rv != APR_SUCCESS) {
618         return "Invalid address or port";
619     }
620
621     if (host && !strcmp(host, "*")) {
622         host = NULL;
623     }
624
625     if (scope_id) {
626         /* XXX scope id support is useful with link-local IPv6 addresses */
627         return "Scope id is not supported";
628     }
629
630     if (!port) {
631         return "Port must be specified";
632     }
633
634     if (argc != 2) {
635         if (port == 443) {
636             proto = "https";
637         } else {
638             proto = "http";
639         }
640     }
641     else {
642         proto = apr_pstrdup(cmd->pool, argv[1]);
643         ap_str_tolower(proto);
644     }
645
646     return alloc_listener(cmd->server->process, host, port, proto);
647 }
648
649 AP_DECLARE_NONSTD(const char *) ap_set_listenbacklog(cmd_parms *cmd,
650                                                      void *dummy,
651                                                      const char *arg)
652 {
653     int b;
654     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
655
656     if (err != NULL) {
657         return err;
658     }
659
660     b = atoi(arg);
661     if (b < 1) {
662         return "ListenBacklog must be > 0";
663     }
664
665     ap_listenbacklog = b;
666     return NULL;
667 }
668
669 AP_DECLARE_NONSTD(const char *) ap_set_send_buffer_size(cmd_parms *cmd,
670                                                         void *dummy,
671                                                         const char *arg)
672 {
673     int s = atoi(arg);
674     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
675
676     if (err != NULL) {
677         return err;
678     }
679
680     if (s < 512 && s != 0) {
681         return "SendBufferSize must be >= 512 bytes, or 0 for system default.";
682     }
683
684     send_buffer_size = s;
685     return NULL;
686 }
687
688 AP_DECLARE_NONSTD(const char *) ap_set_receive_buffer_size(cmd_parms *cmd,
689                                                            void *dummy,
690                                                            const char *arg)
691 {
692     int s = atoi(arg);
693     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
694
695     if (err != NULL) {
696         return err;
697     }
698
699     if (s < 512 && s != 0) {
700         return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
701     }
702
703     receive_buffer_size = s;
704     return NULL;
705 }