Use native threads in host resolver so that it works even if processor has no threads
[strongswan.git] / src / libstrongswan / 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 <debug.h>
23 #include <library.h>
24 #include <threading/condvar.h>
25 #include <threading/mutex.h>
26 #include <threading/thread.h>
27 #include <utils/hashtable.h>
28 #include <utils/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
201 thread_cleanup_push((thread_cleanup_t)query_signal_and_destroy, query);
202 old = thread_cancelability(TRUE);
203 error = getaddrinfo(query->name, NULL, &hints, &result);
204 thread_cancelability(old);
205 thread_cleanup_pop(FALSE);
206
207 this->mutex->lock(this->mutex);
208 this->busy_threads--;
209 if (error != 0)
210 {
211 DBG1(DBG_LIB, "resolving '%s' failed: %s", query->name,
212 gai_strerror(error));
213 }
214 else
215 { /* result is a linked list, but we use only the first address */
216 query->result = host_create_from_sockaddr(result->ai_addr);
217 freeaddrinfo(result);
218 }
219 this->queries->remove(this->queries, query);
220 query->done->broadcast(query->done);
221 this->mutex->unlock(this->mutex);
222 query_destroy(query);
223 }
224 return NULL;
225 }
226
227 METHOD(host_resolver_t, resolve, host_t*,
228 private_host_resolver_t *this, char *name, int family)
229 {
230 query_t *query, lookup = {
231 .name = name,
232 .family = family,
233 };
234 host_t *result;
235
236 if (streq(name, "%any") || streq(name, "0.0.0.0"))
237 {
238 return host_create_any(family ? family : AF_INET);
239 }
240 if (streq(name, "%any6") || streq(name, "::"))
241 {
242 return host_create_any(family ? family : AF_INET6);
243 }
244 if (family == AF_INET && strchr(name, ':'))
245 { /* do not try to convert v6 addresses for v4 family */
246 return NULL;
247 }
248 this->mutex->lock(this->mutex);
249 if (this->disabled)
250 {
251 this->mutex->unlock(this->mutex);
252 return NULL;
253 }
254 query = this->queries->get(this->queries, &lookup);
255 if (!query)
256 {
257 INIT(query,
258 .name = strdup(name),
259 .family = family,
260 .done = condvar_create(CONDVAR_TYPE_DEFAULT),
261 .refcount = 1,
262 );
263 this->queries->put(this->queries, query, query);
264 this->queue->insert_last(this->queue, query);
265 this->new_query->signal(this->new_query);
266 }
267 ref_get(&query->refcount);
268 if (this->busy_threads == this->threads &&
269 this->threads < this->max_threads)
270 {
271 thread_t *thread;
272
273 thread = thread_create((thread_main_t)resolve_hosts, this);
274 if (thread)
275 {
276 this->threads++;
277 this->pool->insert_last(this->pool, thread);
278 }
279 }
280 query->done->wait(query->done, this->mutex);
281 this->mutex->unlock(this->mutex);
282
283 result = query->result ? query->result->clone(query->result) : NULL;
284 query_destroy(query);
285 return result;
286 }
287
288 METHOD(host_resolver_t, flush, void,
289 private_host_resolver_t *this)
290 {
291 enumerator_t *enumerator;
292 query_t *query;
293
294 this->mutex->lock(this->mutex);
295 enumerator = this->queries->create_enumerator(this->queries);
296 while (enumerator->enumerate(enumerator, &query, NULL))
297 { /* use the hashtable here as we also want to signal dequeued queries */
298 this->queries->remove_at(this->queries, enumerator);
299 query->done->broadcast(query->done);
300 }
301 enumerator->destroy(enumerator);
302 this->queue->destroy_function(this->queue, (void*)query_destroy);
303 this->queue = linked_list_create();
304 this->disabled = TRUE;
305 /* this will already terminate most idle threads */
306 this->new_query->broadcast(this->new_query);
307 this->mutex->unlock(this->mutex);
308 }
309
310 METHOD(host_resolver_t, destroy, void,
311 private_host_resolver_t *this)
312 {
313 thread_t *thread;
314
315 flush(this);
316 this->pool->invoke_offset(this->pool, offsetof(thread_t, cancel));
317 while (this->pool->remove_first(this->pool, (void**)&thread) == SUCCESS)
318 {
319 thread->join(thread);
320 }
321 this->pool->destroy(this->pool);
322 this->queue->destroy(this->queue);
323 this->queries->destroy(this->queries);
324 this->new_query->destroy(this->new_query);
325 this->mutex->destroy(this->mutex);
326 free(this);
327 }
328
329 /*
330 * Described in header
331 */
332 host_resolver_t *host_resolver_create()
333 {
334 private_host_resolver_t *this;
335
336 INIT(this,
337 .public = {
338 .resolve = _resolve,
339 .flush = _flush,
340 .destroy = _destroy,
341 },
342 .queries = hashtable_create((hashtable_hash_t)query_hash,
343 (hashtable_equals_t)query_equals, 8),
344 .queue = linked_list_create(),
345 .pool = linked_list_create(),
346 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
347 .new_query = condvar_create(CONDVAR_TYPE_DEFAULT),
348 );
349
350 this->min_threads = max(0, lib->settings->get_int(lib->settings,
351 "libstrongswan.host_resolver.min_threads",
352 MIN_THREADS_DEFAULT));
353 this->max_threads = max(this->min_threads ?: 1,
354 lib->settings->get_int(lib->settings,
355 "libstrongswan.host_resolver.max_threads",
356 MAX_THREADS_DEFAULT));
357 return &this->public;
358 }