Remove numeric conversion from resolver, it is done directly in host_t
[strongswan.git] / src / libstrongswan / networking / host_resolver.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netdb.h>
19
20 #include "host_resolver.h"
21
22 #include <library.h>
23 #include <utils/debug.h>
24 #include <threading/condvar.h>
25 #include <threading/mutex.h>
26 #include <threading/thread.h>
27 #include <collections/hashtable.h>
28 #include <collections/linked_list.h>
29
30 /**
31 * Default minimum and maximum number of threads
32 */
33 #define MIN_THREADS_DEFAULT 0
34 #define MAX_THREADS_DEFAULT 3
35
36 /**
37 * Timeout in seconds to wait for new queries until a thread may be stopped
38 */
39 #define NEW_QUERY_WAIT_TIMEOUT 30
40
41 typedef struct private_host_resolver_t private_host_resolver_t;
42
43 /**
44 * Private data of host_resolver_t
45 */
46 struct private_host_resolver_t {
47
48 /**
49 * Public interface
50 */
51 host_resolver_t public;
52
53 /**
54 * Hashtable to check for queued queries, query_t*
55 */
56 hashtable_t *queries;
57
58 /**
59 * Queue for queries, query_t*
60 */
61 linked_list_t *queue;
62
63 /**
64 * Mutex to safely access private data
65 */
66 mutex_t *mutex;
67
68 /**
69 * Condvar to signal arrival of new queries
70 */
71 condvar_t *new_query;
72
73 /**
74 * Minimum number of resolver threads
75 */
76 u_int min_threads;
77
78 /**
79 * Maximum number of resolver threads
80 */
81 u_int max_threads;
82
83 /**
84 * Current number of threads
85 */
86 u_int threads;
87
88 /**
89 * Current number of busy threads
90 */
91 u_int busy_threads;
92
93 /**
94 * Pool of threads, thread_t*
95 */
96 linked_list_t *pool;
97
98 /**
99 * TRUE if no new queries are accepted
100 */
101 bool disabled;
102
103 };
104
105 typedef struct {
106 /** DNS name we are looking for */
107 char *name;
108 /** address family we request */
109 int family;
110 /** Condvar to signal completion of a query */
111 condvar_t *done;
112 /** refcount */
113 refcount_t refcount;
114 /** the result if successful */
115 host_t *result;
116 } query_t;
117
118 /**
119 * Destroy the given query_t object if refcount is zero
120 */
121 static void query_destroy(query_t *this)
122 {
123 if (ref_put(&this->refcount))
124 {
125 DESTROY_IF(this->result);
126 this->done->destroy(this->done);
127 free(this->name);
128 free(this);
129 }
130 }
131
132 /**
133 * Signals all waiting threads and destroys the query
134 */
135 static void query_signal_and_destroy(query_t *this)
136 {
137 this->done->broadcast(this->done);
138 query_destroy(this);
139 }
140
141 /**
142 * Hash a queued query
143 */
144 static u_int query_hash(query_t *this)
145 {
146 return chunk_hash_inc(chunk_create(this->name, strlen(this->name)),
147 chunk_hash(chunk_from_thing(this->family)));
148 }
149
150 /**
151 * Compare two queued queries
152 */
153 static bool query_equals(query_t *this, query_t *other)
154 {
155 return this->family == other->family && streq(this->name, other->name);
156 }
157
158 /**
159 * Main function of resolver threads
160 */
161 static void *resolve_hosts(private_host_resolver_t *this)
162 {
163 struct addrinfo hints, *result;
164 query_t *query;
165 int error;
166 bool old, timed_out;
167
168 while (TRUE)
169 {
170 this->mutex->lock(this->mutex);
171 thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
172 while (this->queue->remove_first(this->queue,
173 (void**)&query) != SUCCESS)
174 {
175 old = thread_cancelability(TRUE);
176 timed_out = this->new_query->timed_wait(this->new_query,
177 this->mutex, NEW_QUERY_WAIT_TIMEOUT * 1000);
178 thread_cancelability(old);
179 if (this->disabled)
180 {
181 thread_cleanup_pop(TRUE);
182 return NULL;
183 }
184 else if (timed_out && (this->threads > this->min_threads))
185 { /* terminate this thread by detaching it */
186 thread_t *thread = thread_current();
187
188 this->threads--;
189 this->pool->remove(this->pool, thread, NULL);
190 thread_cleanup_pop(TRUE);
191 thread->detach(thread);
192 return NULL;
193 }
194 }
195 this->busy_threads++;
196 thread_cleanup_pop(TRUE);
197
198 memset(&hints, 0, sizeof(hints));
199 hints.ai_family = query->family;
200 hints.ai_socktype = SOCK_DGRAM;
201
202 thread_cleanup_push((thread_cleanup_t)query_signal_and_destroy, query);
203 old = thread_cancelability(TRUE);
204 error = getaddrinfo(query->name, NULL, &hints, &result);
205 thread_cancelability(old);
206 thread_cleanup_pop(FALSE);
207
208 this->mutex->lock(this->mutex);
209 this->busy_threads--;
210 if (error != 0)
211 {
212 DBG1(DBG_LIB, "resolving '%s' failed: %s", query->name,
213 gai_strerror(error));
214 }
215 else
216 { /* result is a linked list, but we use only the first address */
217 query->result = host_create_from_sockaddr(result->ai_addr);
218 freeaddrinfo(result);
219 }
220 this->queries->remove(this->queries, query);
221 query->done->broadcast(query->done);
222 this->mutex->unlock(this->mutex);
223 query_destroy(query);
224 }
225 return NULL;
226 }
227
228 METHOD(host_resolver_t, resolve, host_t*,
229 private_host_resolver_t *this, char *name, int family)
230 {
231 query_t *query, lookup = {
232 .name = name,
233 .family = family,
234 };
235 host_t *result;
236
237 if (streq(name, "%any") || streq(name, "0.0.0.0"))
238 {
239 return host_create_any(family ? family : AF_INET);
240 }
241 if (streq(name, "%any6") || streq(name, "::"))
242 {
243 return host_create_any(family ? family : AF_INET6);
244 }
245 if (family == AF_INET && strchr(name, ':'))
246 { /* do not try to convert v6 addresses for v4 family */
247 return NULL;
248 }
249 this->mutex->lock(this->mutex);
250 if (this->disabled)
251 {
252 this->mutex->unlock(this->mutex);
253 return NULL;
254 }
255 query = this->queries->get(this->queries, &lookup);
256 if (!query)
257 {
258 INIT(query,
259 .name = strdup(name),
260 .family = family,
261 .done = condvar_create(CONDVAR_TYPE_DEFAULT),
262 .refcount = 1,
263 );
264 this->queries->put(this->queries, query, query);
265 this->queue->insert_last(this->queue, query);
266 this->new_query->signal(this->new_query);
267 }
268 ref_get(&query->refcount);
269 if (this->busy_threads == this->threads &&
270 this->threads < this->max_threads)
271 {
272 thread_t *thread;
273
274 thread = thread_create((thread_main_t)resolve_hosts, this);
275 if (thread)
276 {
277 this->threads++;
278 this->pool->insert_last(this->pool, thread);
279 }
280 }
281 query->done->wait(query->done, this->mutex);
282 this->mutex->unlock(this->mutex);
283
284 result = query->result ? query->result->clone(query->result) : NULL;
285 query_destroy(query);
286 return result;
287 }
288
289 METHOD(host_resolver_t, flush, void,
290 private_host_resolver_t *this)
291 {
292 enumerator_t *enumerator;
293 query_t *query;
294
295 this->mutex->lock(this->mutex);
296 enumerator = this->queries->create_enumerator(this->queries);
297 while (enumerator->enumerate(enumerator, &query, NULL))
298 { /* use the hashtable here as we also want to signal dequeued queries */
299 this->queries->remove_at(this->queries, enumerator);
300 query->done->broadcast(query->done);
301 }
302 enumerator->destroy(enumerator);
303 this->queue->destroy_function(this->queue, (void*)query_destroy);
304 this->queue = linked_list_create();
305 this->disabled = TRUE;
306 /* this will already terminate most idle threads */
307 this->new_query->broadcast(this->new_query);
308 this->mutex->unlock(this->mutex);
309 }
310
311 METHOD(host_resolver_t, destroy, void,
312 private_host_resolver_t *this)
313 {
314 thread_t *thread;
315
316 flush(this);
317 this->pool->invoke_offset(this->pool, offsetof(thread_t, cancel));
318 while (this->pool->remove_first(this->pool, (void**)&thread) == SUCCESS)
319 {
320 thread->join(thread);
321 }
322 this->pool->destroy(this->pool);
323 this->queue->destroy(this->queue);
324 this->queries->destroy(this->queries);
325 this->new_query->destroy(this->new_query);
326 this->mutex->destroy(this->mutex);
327 free(this);
328 }
329
330 /*
331 * Described in header
332 */
333 host_resolver_t *host_resolver_create()
334 {
335 private_host_resolver_t *this;
336
337 INIT(this,
338 .public = {
339 .resolve = _resolve,
340 .flush = _flush,
341 .destroy = _destroy,
342 },
343 .queries = hashtable_create((hashtable_hash_t)query_hash,
344 (hashtable_equals_t)query_equals, 8),
345 .queue = linked_list_create(),
346 .pool = linked_list_create(),
347 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
348 .new_query = condvar_create(CONDVAR_TYPE_DEFAULT),
349 );
350
351 this->min_threads = max(0, lib->settings->get_int(lib->settings,
352 "libstrongswan.host_resolver.min_threads",
353 MIN_THREADS_DEFAULT));
354 this->max_threads = max(this->min_threads ?: 1,
355 lib->settings->get_int(lib->settings,
356 "libstrongswan.host_resolver.max_threads",
357 MAX_THREADS_DEFAULT));
358 return &this->public;
359 }