vhost.c revision 7afdaa3ee342af2e075f62c619b60628556f7205
* 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * ==================================================================== * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. * http_vhost.c: functions pertaining to virtual host addresses * (configuration and run-time) * After all the definitions there's an explanation of how it's all put /* meta-list of name-vhosts. Each server_rec can be in possibly multiple * this chain (needed for port comparisons) */ /* meta-list of ip addresses. Each server_rec can be in possibly multiple * hash chains since it can have multiple ips. * this chain (need for both ip addr and port * sharing this address */ /* This defines the size of the hash apr_table_t used for hashing ip addresses * of virtual hosts. It must be a power of two. /* A (n) bucket hash table, each entry has a pointer to a server rec and * a pointer to the other entries in that bucket. Each individual address, * even for virtualhosts with multiple addresses, has an entry in this hash * table. There are extra buckets for _default_, and name-vhost entries. * Note that after config time this is constant, so it is thread-safe. /* dump out statistics about the hash function */ /* #define IPHASH_STATISTICS */ /* list of the _default_ servers */ /* list of the NameVirtualHost addresses */ * The ip address determines which chain in iphash_table is interesting, then * a comparison is done down that chain to find the first ipaddr_chain whose * sar matches the address:port pair. * If that ipaddr_chain has names == NULL then you're done, it's an ip-vhost. * Otherwise it's a name-vhost list, and the default is the server in the * ipaddr_chain record. We tuck away the ipaddr_chain record in the * conn_rec field vhost_lookup_data. Later on after the headers we get a * second chance, and we use the name_chain to figure out what name-vhost * If there was no ip address match in the iphash_table then do a lookup * How it's put together ... well you should be able to figure that out * from how it's used. Or something like that. /* called at the beginning of the config */ * Parses a host of the form <address>[:port] * paddr is used to create a list in the order of input * **paddr is the ->next pointer of the last entry (or s->addrs) * *paddr is the variable used to keep track of **paddr between calls * port is the default port to assume if (
strcmp(t +
1,
"*") == 0) {
else if ((i =
atoi(t +
1))) {
return ":port must be numeric";
||
strcmp(w,
"255.255.255.255") == 0) {
"Cannot resolve host name %s --- ignoring!", w);
/* parse the <VirtualHost> addresses */ /* start the list of addreses */ /* override the default port which is inherited from main_server */ /* use whatever port the main server has at this point */ /* hash apr_table_t statistics, keep this in here for the beta period so * we can find out if the hash function is ok return (*(
const int *) b - *(
const int *) a);
/* don't count the slop buckets in the total */ "iphash: total hashed = %u, avg chain = %u, " "chain lengths (count x len):",
/* This hashing function is designed to get good distribution in the cases * where the server is handling entire "networks" of servers. i.e. a * whack of /24s. This is probably the most common configuration for * ISPs with large virtual servers. * NOTE: This function is symmetric (i.e. collapses all 4 octets * into one), so machine byte order (big/little endianness) does not matter. * Hash function provided by David Hankins. /* scan the hash apr_table_t for an exact match first */ "%22s default server %s (%s:%u)\n",
apr_fprintf(f,
"wildcard NameVirtualHosts and _default_ servers:\n");
* Two helper functions for ap_fini_vhost_config() /* the first time we encounter a NameVirtualHost address * ic->server will be NULL, on subsequent encounters * ic->names will be non-NULL. /* one of the two is a * port, the other isn't */ "VirtualHost %s:%u -- mixing * " "ports and non-* ports with " "a NameVirtualHost address is not supported," " proceeding with undefined results",
/* IP-based vhosts are handled by the caller */ "NameVirtualHost %s:%u has no VirtualHosts",
/* if server != NULL and names == NULL then we're done * looking at NameVirtualHosts /* compile the tables and such we need to do the run-time vhost lookups */ /* terminate the name_vhost list */ /* initialize the tails */ /* The first things to go into the hash apr_table_t are the NameVirtualHosts * Since name_vhost_list is in the same order that the directives * occured in the config file, we'll copy it in that order. /* A wildcard NameVirtualHost goes on the default_list so * that it can catch incoming requests on any address. /* Notice that what we've done is insert an ipaddr_chain with * both server and names NULL. This fact is used to spot name- * based vhosts in add_name_vhost_config(). /* The next things to go into the hash apr_table_t are the virtual hosts * themselves. They're listed off of main_s->next in the reverse * order they occured in the config file, so we insert them at * the iphash_table_tail but don't advance the tail. 0,
main_s,
"_default_ VirtualHost overlap on port %u," /* see if it matches something we've already got */ "VirtualHost %s:%u overlaps with " "VirtualHost %s:%u, the first has precedence, " "perhaps you need a NameVirtualHost directive",
/* Ok now we want to set up a server_hostname if the user was * silly enough to forget one. * XXX: This is silly we should just crash and burn. /* what else can we do? at this point this vhost has no configured name, probably because they used DNS in the VirtualHost statement. It's disabled anyhow by the host matching code. -djg */ /* again, what can we do? They didn't specify a ServerName, and their DNS isn't working. -djg */ "Failed to resolve server name " "for %s (check DNS) -- or specify an explicit " /* now go through and delete any NameVirtualHosts that didn't have any * hosts associated with them. Lamers. /***************************************************************************** * run-time vhost matching functions /* Lowercase and remove any trailing dot and/or :port from the hostname, * and check that it is sane. /* check and copy the host part */ /* check the port part */ /* strip trailing gubbins */ "Client sent malformed Host header");
/* return 1 if host matches ServerName or ServerAliases */ /* search all the aliases from ServerAlias directive */ /* Suppose a request came in on the same socket as this r, and included * a header "Host: host:port", would it map to r->server? It's more * than just that though. When we do the normal matches for each request * we don't even bother considering Host: etc on non-namevirtualhosts, * we just call it a match. But here we require the host:port to match * the ServerName and/or ServerAliases. /* search all the <VirtualHost> values */ /* XXX: If this is a NameVirtualHost then we may not be doing the Right Thing * NameVirtualHost 10.1.1.1 * Suppose r->server is v2, and we're asked to match "10.1.1.1". We'll say * "yup it's v2", when really it isn't... if a request came in for 10.1.1.1 * it would really go to v1. /* the Port has to match now, because the rest don't have ports associated * Even if the request has a Host: header containing a port we ignore * that port. We always use the physical port of the socket. There * are a few reasons for this: * - the default of 80 or 443 for SSL is easier to handle this way * - there is less of a possibility of a security problem * - it simplifies the data structure * - the client may have no idea that a proxy somewhere along the way * translated the request to another ip:port * - except for the addresses from the VirtualHost line, none of the other * names we'll match have ports associated with them /* Recall that the name_chain is a list of server_addr_recs, some of * whose ports may not match. Also each server may appear more than * once in the chain -- specifically, it will appear once for each * address from its VirtualHost line which matched. We only want to * server, fortunately we know that all the VirtualHost addresses for * a single server are adjacent to each other. /* We only consider addresses on the name_chain which have a matching /* does it match the virthost from the sar? */ /* we've already done ServerName and ServerAlias checks for this /* s is the first matching server, we're done */ * This is in conjunction with the ServerPath code in http_core, so we * get the right host attached to a non- Host-sending request. * See the comment in check_hostalias about how each vhost can be /* We only consider addresses on the name_chain which have a matching /* must set this for HTTP/1.1 support */ /* check if we tucked away a name_chain */ /* Called for a new connection which has a known local_addr. Note that the * new connection is assumed to have conn->server == main server. /* scan the hash apr_table_t for an exact match first */ /* save the name_chain for later in case this is a name-vhost */ /* maybe there's a default server or wildcard name-based vhost /* otherwise we're stuck with just the main server * and no name-based vhosts