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

Annotation of /hydra/src/virthost.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Tue Sep 24 18:08:52 2002 UTC (21 years, 7 months ago) by nmav
Branch: MAIN
Changes since 1.2: +3 -1 lines
File MIME type: text/plain
*** empty log message ***

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.3 /* $Id: virthost.c,v 1.2 2002/09/24 18:07:58 nmav Exp $ */
22 nmav 1.1
23     #include "boa.h"
24    
25     static virthost *virthost_hashtable[ALIAS_HASHTABLE_SIZE];
26    
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     }
76    
77     hash = get_host_hash_value( host);
78    
79     old = virthost_hashtable[hash];
80    
81     if (old) {
82     while (old->next) {
83     if (!strcmp( host, old->host)) /* don't add twice */
84     return;
85     old = old->next;
86     }
87     }
88    
89     new = (virthost *) malloc(sizeof (virthost));
90     if (!new) {
91     DIE("out of memory adding virthost to hash");
92     }
93    
94     if (old)
95     old->next = new;
96     else
97     virthost_hashtable[hash] = new;
98    
99     new->host = strdup( host);
100     if (!new->host) {
101     DIE("failed strdup");
102     }
103     new->host_len = hostlen;
104    
105     if (user_dir && user_dir_len > 0) {
106     new->user_dir = strdup(user_dir);
107     new->user_dir_len = user_dir_len;
108     } else {
109     new->user_dir = NULL;
110     new->user_dir_len = 0;
111     }
112    
113     if (iplen == 0 || !strchr( ip, '*')) { /* if the IP part is '*' then
114     * we don't bind this virthost to a
115     * specific ip */
116     new->ip = strdup( ip);
117     if (!new->ip) {
118     DIE("failed strdup");
119     }
120     new->ip_len = iplen;
121     } else {
122     new->ip = NULL;
123     new->ip_len = 0;
124     }
125    
126     /* check for "here" */
127     if (document_root[0] == '.' && document_root[1] == '/') {
128     new->document_root = normalize_path( (char*)(document_root+2));
129     if (!new->document_root) {
130     /* superfluous - normalize_path checks for NULL return values. */
131     DIE("normalize_path returned NULL");
132     }
133     document_root_len = strlen(new->document_root);
134     } else {
135     new->document_root = strdup(document_root);
136     if (!new->document_root) {
137     DIE("strdup of document_root failed");
138     }
139     }
140     new->document_root_len = document_root_len;
141    
142     new->next = NULL;
143     }
144    
145     /*
146     * Name: find_virthost
147     *
148     * Description: Locates host in the virthost hashtable if it exists.
149     *
150     * Returns:
151     *
152     * virthost structure or NULL if not found
153     */
154    
155     virthost *find_virthost(const char *host, int hostlen)
156     {
157     virthost *current;
158     int hash;
159    
160     /* Find Hostname, IP, document root */
161    
162     if (hostlen == 0)
163     hostlen = strlen(host);
164    
165     hash = get_host_hash_value( host);
166    
167     current = virthost_hashtable[hash];
168     while (current) {
169     #ifdef FASCIST_LOGGING
170     fprintf(stderr,
171     "%s:%d - comparing \"%s\" (request) to \"%s\" (virthost): ",
172     __FILE__, __LINE__, host, current->host);
173     #endif
174     /* current->host_len must always be:
175     * equal to the host
176     */
177     if (current->host_len == hostlen &&
178     !memcmp(host, current->host, current->host_len)) {
179     #ifdef FASCIST_LOGGING
180     fprintf(stderr, "Got it!\n");
181     #endif
182     return current;
183     }
184     #ifdef FASCIST_LOGGING
185     else
186     fprintf(stderr, "Don't Got it!\n");
187     #endif
188     current = current->next;
189     }
190     return current;
191     }
192    
193    
194    
195     /*
196     * Empties the virthost hashtable, deallocating any allocated memory.
197     */
198    
199     void dump_virthost(void)
200     {
201     int i;
202     virthost *temp;
203    
204     for (i = 0; i < VIRTHOST_HASHTABLE_SIZE; ++i) { /* these limits OK? */
205     if (virthost_hashtable[i]) {
206     temp = virthost_hashtable[i];
207     while (temp) {
208     virthost *temp_next;
209    
210     if (temp->host)
211     free(temp->host);
212     if (temp->ip)
213     free(temp->ip);
214     if (temp->document_root)
215     free(temp->document_root);
216 nmav 1.3 if (temp->user_dir)
217     free(temp->user_dir);
218 nmav 1.1 temp_next = temp->next;
219     free(temp);
220     temp = temp_next;
221     }
222     virthost_hashtable[i] = NULL;
223     }
224     }
225     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26