/[hydra]/hydra/src/virthost.c
ViewVC logotype

Annotation of /hydra/src/virthost.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Fri Sep 27 23:40:29 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
Changes since 1.6: +2 -1 lines
File MIME type: text/plain
Improvements on HIC support.

1 nmav 1.1 /*
2     * Boa, an http server
3 nmav 1.2 * Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org>
4 nmav 1.1 *
5     * This program is free software; you can redistribute it and/or modify
6     * it under the terms of the GNU General Public License as published by
7     * the Free Software Foundation; either version 1, or (at your option)
8     * any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18     *
19     */
20    
21 nmav 1.7 /* $Id: virthost.c,v 1.6 2002/09/25 19:55:53 nmav Exp $ */
22 nmav 1.1
23     #include "boa.h"
24    
25 nmav 1.6 static virthost *virthost_hashtable[VIRTHOST_HASHTABLE_SIZE];
26 nmav 1.1
27     /*
28     * Name: get_host_hash_value
29     *
30     * Here we simply use the algorithm used in get_alias_hash_value();
31     */
32    
33     int get_host_hash_value(const char *file)
34     {
35     unsigned int hash = 0;
36     unsigned int index = 0;
37     unsigned char c;
38    
39     hash = file[index++];
40     while ((c = file[index++]) && c != '/')
41     hash += (unsigned int) c;
42    
43     return hash % VIRTHOST_HASHTABLE_SIZE;
44     }
45    
46     /*
47     * Name: add_virthost
48     *
49     * Description: add a virtual host to the virthost hash table.
50     */
51    
52     void add_virthost(const char *host, const char *ip, const char* document_root,
53     const char* user_dir)
54     {
55     int hash;
56     virthost *old, *new;
57     int hostlen, iplen, document_root_len, user_dir_len = 0;
58    
59     /* sanity checking */
60     if (host == NULL || ip == NULL || document_root == NULL) {
61     DIE("NULL values sent to add_virthost");
62     }
63    
64     iplen = strlen(ip);
65    
66     if (iplen > NI_MAXHOST) {
67     DIE("IP in virthost is tooooo long");
68     }
69     hostlen = strlen(host);
70     document_root_len = strlen( document_root);
71     if (user_dir) user_dir_len = strlen( user_dir);
72    
73     if (iplen == 0 || hostlen == 0 || document_root_len == 0) {
74     DIE("empty values sent to add_virthost");
75 nmav 1.4 }
76    
77 nmav 1.5 if ( document_root_len > MAX_PATH_LENGTH || user_dir_len > MAX_USER_DIR_LENGTH) {
78     DIE("DocumentRoot or UserDir length is too long.");
79 nmav 1.1 }
80    
81     hash = get_host_hash_value( host);
82    
83     old = virthost_hashtable[hash];
84    
85     if (old) {
86     while (old->next) {
87     if (!strcmp( host, old->host)) /* don't add twice */
88     return;
89     old = old->next;
90     }
91     }
92    
93 nmav 1.5 new = (virthost *) calloc(1, sizeof (virthost));
94 nmav 1.1 if (!new) {
95     DIE("out of memory adding virthost to hash");
96     }
97    
98     if (old)
99     old->next = new;
100     else
101     virthost_hashtable[hash] = new;
102    
103     new->host = strdup( host);
104     if (!new->host) {
105     DIE("failed strdup");
106     }
107     new->host_len = hostlen;
108    
109     if (user_dir && user_dir_len > 0) {
110     new->user_dir = strdup(user_dir);
111     new->user_dir_len = user_dir_len;
112     } else {
113     new->user_dir = NULL;
114     new->user_dir_len = 0;
115     }
116    
117     if (iplen == 0 || !strchr( ip, '*')) { /* if the IP part is '*' then
118     * we don't bind this virthost to a
119     * specific ip */
120     new->ip = strdup( ip);
121     if (!new->ip) {
122     DIE("failed strdup");
123     }
124     new->ip_len = iplen;
125     } else {
126     new->ip = NULL;
127     new->ip_len = 0;
128     }
129    
130     /* check for "here" */
131     if (document_root[0] == '.' && document_root[1] == '/') {
132     new->document_root = normalize_path( (char*)(document_root+2));
133     if (!new->document_root) {
134     /* superfluous - normalize_path checks for NULL return values. */
135     DIE("normalize_path returned NULL");
136     }
137     document_root_len = strlen(new->document_root);
138     } else {
139     new->document_root = strdup(document_root);
140     if (!new->document_root) {
141     DIE("strdup of document_root failed");
142     }
143     }
144     new->document_root_len = document_root_len;
145    
146     new->next = NULL;
147     }
148    
149     /*
150     * Name: find_virthost
151     *
152     * Description: Locates host in the virthost hashtable if it exists.
153     *
154     * Returns:
155     *
156     * virthost structure or NULL if not found
157     */
158    
159     virthost *find_virthost(const char *host, int hostlen)
160     {
161     virthost *current;
162     int hash;
163    
164     /* Find Hostname, IP, document root */
165 nmav 1.7 if (host==NULL) return NULL;
166 nmav 1.1
167     if (hostlen == 0)
168     hostlen = strlen(host);
169    
170     hash = get_host_hash_value( host);
171    
172     current = virthost_hashtable[hash];
173     while (current) {
174     #ifdef FASCIST_LOGGING
175     fprintf(stderr,
176     "%s:%d - comparing \"%s\" (request) to \"%s\" (virthost): ",
177     __FILE__, __LINE__, host, current->host);
178     #endif
179     /* current->host_len must always be:
180     * equal to the host
181     */
182     if (current->host_len == hostlen &&
183     !memcmp(host, current->host, current->host_len)) {
184     #ifdef FASCIST_LOGGING
185     fprintf(stderr, "Got it!\n");
186     #endif
187     return current;
188     }
189     #ifdef FASCIST_LOGGING
190     else
191     fprintf(stderr, "Don't Got it!\n");
192     #endif
193     current = current->next;
194     }
195     return current;
196     }
197    
198    
199    
200     /*
201     * Empties the virthost hashtable, deallocating any allocated memory.
202     */
203    
204     void dump_virthost(void)
205     {
206     int i;
207     virthost *temp;
208    
209     for (i = 0; i < VIRTHOST_HASHTABLE_SIZE; ++i) { /* these limits OK? */
210     if (virthost_hashtable[i]) {
211     temp = virthost_hashtable[i];
212     while (temp) {
213     virthost *temp_next;
214    
215     if (temp->host)
216     free(temp->host);
217     if (temp->ip)
218     free(temp->ip);
219     if (temp->document_root)
220     free(temp->document_root);
221 nmav 1.3 if (temp->user_dir)
222     free(temp->user_dir);
223 nmav 1.5 dump_alias( temp); /* clear all aliases */
224    
225 nmav 1.1 temp_next = temp->next;
226     free(temp);
227     temp = temp_next;
228     }
229     virthost_hashtable[i] = NULL;
230     }
231     }
232     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26