vhost.c revision 061c3874622b67beff724876b9bd1b1f291f2eaa
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess/* ====================================================================
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * Redistribution and use in source and binary forms, with or without
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * modification, are permitted provided that the following conditions
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * are met:
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * 1. Redistributions of source code must retain the above copyright
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * notice, this list of conditions and the following disclaimer.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * 2. Redistributions in binary form must reproduce the above copyright
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * notice, this list of conditions and the following disclaimer in
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * the documentation and/or other materials provided with the
2e545ce2450a9953665f701bb05350f0d3f26275nd * distribution.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 3. All advertising materials mentioning features or use of this
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * software must display the following acknowledgment:
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * "This product includes software developed by the Apache Group
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen * for use in the Apache HTTP server project (http://www.apache.org/)."
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * 4. The names "Apache Server" and "Apache Group" must not be used to
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * endorse or promote products derived from this software without
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * prior written permission. For written permission, please contact
3f08db06526d6901aa08c110b5bc7dde6bc39905nd * apache@apache.org.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * 5. Products derived from this software may not be called "Apache"
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * nor may "Apache" appear in their names without prior written
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * permission of the Apache Group.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * 6. Redistributions of any form whatsoever must retain the following
fac8c35bfb158112226ab43ddf84d59daca5dc30nd * acknowledgment:
d474d8ef01ec5c2a09341cd148851ed383c3287crbowen * "This product includes software developed by the Apache Group
d474d8ef01ec5c2a09341cd148851ed383c3287crbowen * for use in the Apache HTTP server project (http://www.apache.org/)."
4b575a6b6704b516f22d65a3ad35696d7b9ba372rpluem * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
4b575a6b6704b516f22d65a3ad35696d7b9ba372rpluem * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4b575a6b6704b516f22d65a3ad35696d7b9ba372rpluem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * OF THE POSSIBILITY OF SUCH DAMAGE.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * ====================================================================
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * This software consists of voluntary contributions made by many
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * individuals on behalf of the Apache Group and was originally based
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * on public domain software written at the National Center for
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * Supercomputing Applications, University of Illinois, Urbana-Champaign.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * For more information on the Apache Group and the Apache HTTP server
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * project, please see <http://www.apache.org/>.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * http_vhost.c: functions pertaining to virtual host addresses
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * (configuration and run-time)
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * After all the definitions there's an explanation of how it's all put
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * together.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess/* meta-list of name-vhosts. Each server_rec can be in possibly multiple
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * lists of name-vhosts.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess server_addr_rec *sar; /* the record causing it to be in
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * this chain (needed for port comparisons) */
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess/* meta-list of ip addresses. Each server_rec can be in possibly multiple
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * hash chains since it can have multiple ips.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess server_addr_rec *sar; /* the record causing it to be in
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * this chain (need for both ip addr and port
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * comparisons) */
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess server_rec *server; /* the server to use if this matches */
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess name_chain *names; /* if non-NULL then a list of name-vhosts
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * sharing this address */
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess/* This defines the size of the hash table used for hashing ip addresses
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * of virtual hosts. It must be a power of two.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess/* A (n) bucket hash table, each entry has a pointer to a server rec and
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * a pointer to the other entries in that bucket. Each individual address,
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * even for virtualhosts with multiple addresses, has an entry in this hash
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * table. There are extra buckets for _default_, and name-vhost entries.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * Note that after config time this is constant, so it is thread-safe.
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess/* dump out statistics about the hash function */
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess/* #define IPHASH_STATISTICS */
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess/* list of the _default_ servers */
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess/* list of the NameVirtualHost addresses */
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * How it's used:
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * The ip address determines which chain in iphash_table is interesting, then
1c8f2418892d98febb00a06b9a4f45f8bcfd80a3nd * a comparison is done down that chain to find the first ipaddr_chain whose
e655a84bbb62bb1c66993fda5e74b04feda14dc6kess * sar matches the address:port pair.
d474d8ef01ec5c2a09341cd148851ed383c3287crbowen * If that ipaddr_chain has names == NULL then you're done, it's an ip-vhost.
727872d18412fc021f03969b8641810d8896820bhumbedooh * Otherwise it's a name-vhost list, and the default is the server in the
0d0ba3a410038e179b695446bb149cce6264e0abnd * ipaddr_chain record. We tuck away the ipaddr_chain record in the
727872d18412fc021f03969b8641810d8896820bhumbedooh * conn_rec field vhost_lookup_data. Later on after the headers we get a
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh * second chance, and we use the name_chain to figure out what name-vhost
0d0ba3a410038e179b695446bb149cce6264e0abnd * matches the headers.
727872d18412fc021f03969b8641810d8896820bhumbedooh * If there was no ip address match in the iphash_table then do a lookup
0d0ba3a410038e179b695446bb149cce6264e0abnd * in the default_list.
0d0ba3a410038e179b695446bb149cce6264e0abnd * How it's put together ... well you should be able to figure that out
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh * from how it's used. Or something like that.
727872d18412fc021f03969b8641810d8896820bhumbedooh/* called at the beginning of the config */
unsigned port)
unsigned long my_addr;
int i, is_an_ip_addr;
return NULL;
port = 0;
port = i;
is_an_ip_addr = 0;
if (is_an_ip_addr) {
if (t != NULL)
return NULL;
if (t != NULL)
return NULL;
if (t != NULL)
return NULL;
const char *err;
while (hostname[0]) {
if (err) {
return err;
if (s->addrs) {
return NULL;
#ifdef IPHASH_STATISTICS
static int iphash_compare(const void *a, const void *b)
unsigned total;
total = 0;
for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
count[i] = 0;
++count[i];
if (i < IPHASH_TABLE_SIZE) {
++total;
++total;
return new;
return new;
unsigned port)
unsigned bucket;
unsigned addr;
|| port == 0)) {
return trav;
return NULL;
return trav;
return NULL;
for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
if (default_list) {
server_rec *s;
s = main_s;
if (!s->server_hostname) {
for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
else if (ic) {
if (!s->server_hostname) {
if (has_default_vhost_addr) {
else if (!s->addrs) {
s->server_hostname =
struct hostent *h;
s->server_hostname =
for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
while (*pic) {
#ifdef IPHASH_STATISTICS
size_t l;
if (names) {
if(!name[i]) continue;
if (names) {
if(!name[i]) continue;
unsigned port)
server_rec *s;
s = r->server;
server_rec *s;
* do the full ServerName/ServerAlias comparisons once for each
goto found;
if (s == last_s) {
last_s = s;
goto found;
r->server = s;
server_rec *s;
if (s == last_s) {
last_s = s;
r->server = s;
fix_hostname(r);
if (r->hostname)
check_hostalias(r);
check_serverpath(r);
if (trav) {
if (trav) {