Debian Codegraph @

crt/aws-c-io/source/host_resolver.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/io/event_loop.h>
6 #include <aws/io/host_resolver.h>
7
8 #include <aws/common/atomics.h>
9 #include <aws/common/clock.h>
10 #include <aws/common/condition_variable.h>
11 #include <aws/common/hash_table.h>
12 #include <aws/common/lru_cache.h>
13 #include <aws/common/mutex.h>
14 #include <aws/common/string.h>
15 #include <aws/common/task_scheduler.h>
16 #include <aws/common/thread.h>
17
18 #include <aws/io/logging.h>
19
20 #include <inttypes.h>
21
22 const uint64_t NS_PER_SEC = 1000000000;
23 const size_t AWS_DEFAULT_DNS_TTL = 30;
24
25 int aws_host_address_copy(const struct aws_host_address *from, struct aws_host_address *to) {
26 to->allocator = from->allocator;
27 to->address = aws_string_new_from_string(to->allocator, from->address);
28 to->host = aws_string_new_from_string(to->allocator, from->host);
29 to->record_type = from->record_type;
30 to->use_count = from->use_count;
31 to->connection_failure_count = from->connection_failure_count;
32 to->expiry = from->expiry;
33 to->weight = from->weight;
34
35 return AWS_OP_SUCCESS;
36 }
37
38 void aws_host_address_move(struct aws_host_address *from, struct aws_host_address *to) {
39 to->allocator = from->allocator;
40 to->address = from->address;
41 to->host = from->host;
42 to->record_type = from->record_type;
43 to->use_count = from->use_count;
44 to->connection_failure_count = from->connection_failure_count;
45 to->expiry = from->expiry;
46 to->weight = from->weight;
47 AWS_ZERO_STRUCT(*from);
48 }
49
50 void aws_host_address_clean_up(struct aws_host_address *address) {
51 if (address->address) {
52 aws_string_destroy((void *)address->address);
53 }
54 if (address->host) {
55 aws_string_destroy((void *)address->host);
56 }
57 AWS_ZERO_STRUCT(*address);
58 }
59
60 int aws_host_resolver_resolve_host(
61 struct aws_host_resolver *resolver,
62 const struct aws_string *host_name,
63 aws_on_host_resolved_result_fn *res,
64 const struct aws_host_resolution_config *config,
65 void *user_data) {
66 AWS_ASSERT(resolver->vtable && resolver->vtable->resolve_host);
67 return resolver->vtable->resolve_host(resolver, host_name, res, config, user_data);
68 }
69
70 int aws_host_resolver_purge_cache(struct aws_host_resolver *resolver) {
71 AWS_ASSERT(resolver->vtable && resolver->vtable->purge_cache);
72 return resolver->vtable->purge_cache(resolver);
73 }
74
75 int aws_host_resolver_purge_cache_with_callback(
76 struct aws_host_resolver *resolver,
77 aws_simple_completion_callback *on_purge_cache_complete_callback,
78 void *user_data) {
79 AWS_PRECONDITION(resolver);
80 AWS_PRECONDITION(resolver->vtable);
81
82 if (!resolver->vtable->purge_cache_with_callback) {
83 AWS_LOGF_ERROR(AWS_LS_IO_DNS, "purge_cache_with_callback function is not supported");
84 return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
85 }
86
87 return resolver->vtable->purge_cache_with_callback(resolver, on_purge_cache_complete_callback, user_data);
88 }
89
90 int aws_host_resolver_purge_host_cache(
91 struct aws_host_resolver *resolver,
92 const struct aws_host_resolver_purge_host_options *options) {
93 AWS_PRECONDITION(resolver);
94 AWS_PRECONDITION(resolver->vtable);
95
96 if (!resolver->vtable->purge_host_cache) {
97 AWS_LOGF_ERROR(AWS_LS_IO_DNS, "purge_host_cache function is not supported");
98 return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
99 }
100 return resolver->vtable->purge_host_cache(resolver, options);
101 }
102
103 int aws_host_resolver_record_connection_failure(
104 struct aws_host_resolver *resolver,
105 const struct aws_host_address *address) {
106 AWS_ASSERT(resolver->vtable && resolver->vtable->record_connection_failure);
107 return resolver->vtable->record_connection_failure(resolver, address);
108 }
109
110 /*
111 * Used by both the resolver for its lifetime state as well as individual host entries for theirs.
112 */
113 enum default_resolver_state {
114 DRS_ACTIVE,
115 DRS_SHUTTING_DOWN,
116 };
117
118 struct default_host_resolver {
119 struct aws_allocator *allocator;
120
121 /*
122 * Mutually exclusion for the whole resolver, includes all member data and all host_entry_table operations. Once
123 * an entry is retrieved, this lock MAY be dropped but certain logic may hold both the resolver and the entry lock.
124 * The two locks must be taken in that order.
125 */
126 struct aws_mutex resolver_lock;
127
128 /* host_name (aws_string*) -> host_entry* */
129 struct aws_hash_table host_entry_table;
130
131 enum default_resolver_state state;
132
133 /*
134 * Tracks the number of launched resolution threads that have not yet invoked their shutdown completion
135 * callback.
136 */
137 uint32_t pending_host_entry_shutdown_completion_callbacks;
138
139 /*
140 * Function to use to query current time. Overridable in construction options.
141 */
142 aws_io_clock_fn *system_clock_fn;
143
144 struct aws_event_loop_group *event_loop_group;
145 };
146
147 struct host_entry {
148 /* immutable post-creation */
149 struct aws_allocator *allocator;
150 struct aws_host_resolver *resolver;
151 struct aws_thread resolver_thread;
152 const struct aws_string *host_name;
153 int64_t resolve_frequency_ns;
154 struct aws_host_resolution_config resolution_config;
155
156 /* synchronized data and its lock */
157 struct aws_mutex entry_lock;
158 struct aws_condition_variable entry_signal;
159 struct aws_cache *aaaa_records;
160 struct aws_cache *a_records;
161 struct aws_cache *failed_connection_aaaa_records;
162 struct aws_cache *failed_connection_a_records;
163 struct aws_linked_list pending_resolution_callbacks;
164 uint32_t resolves_since_last_request;
165 uint64_t last_resolve_request_timestamp_ns;
166 enum default_resolver_state state;
167 struct aws_array_list new_addresses;
168 struct aws_array_list expired_addresses;
169 aws_simple_completion_callback *on_host_purge_complete;
170 void *on_host_purge_complete_user_data;
171 };
172
173 /*
174 * A host entry's caches hold things of this type. By using this and not the host_address directly, our
175 * on_remove callbacks for the cache have access to the host_entry. We wouldn't need to do this if those
176 * callbacks supported user data injection, but they don't and too many internal code bases already depend
177 * on the public API.
178 */
179 struct aws_host_address_cache_entry {
180 struct aws_host_address address;
181 struct host_entry *entry;
182 };
183
184 int aws_host_address_cache_entry_copy(
185 const struct aws_host_address_cache_entry *from,
186 struct aws_host_address_cache_entry *to) {
187 if (aws_host_address_copy(&from->address, &to->address)) {
188 return AWS_OP_ERR;
189 }
190
191 to->entry = from->entry;
192
193 return AWS_OP_SUCCESS;
194 }
195
196 static void s_shutdown_host_entry(struct host_entry *entry) {
197 aws_mutex_lock(&entry->entry_lock);
198 entry->state = DRS_SHUTTING_DOWN;
199 /*
200 * intentionally signal under the lock; we can't guarantee the resolver
201 * is still around once the lock is released.
202 */
203 aws_condition_variable_notify_all(&entry->entry_signal);
204 aws_mutex_unlock(&entry->entry_lock);
205 }
206
207 struct host_purge_callback_options {
208 struct aws_allocator *allocator;
209 struct aws_ref_count ref_count;
210 aws_simple_completion_callback *on_purge_cache_complete_callback;
211 void *user_data;
212 };
213
214 static void s_host_purge_callback_options_destroy(void *user_data) {
215 struct host_purge_callback_options *options = user_data;
216 options->on_purge_cache_complete_callback(options->user_data);
217 aws_mem_release(options->allocator, options);
218 }
219
220 static struct host_purge_callback_options *s_host_purge_callback_options_new(
221 struct aws_allocator *allocator,
222 aws_simple_completion_callback *on_purge_cache_complete_callback,
223 void *user_data) {
224 struct host_purge_callback_options *purge_callback_options =
225 aws_mem_calloc(allocator, 1, sizeof(struct host_purge_callback_options));
226 purge_callback_options->allocator = allocator;
227 aws_ref_count_init(
228 &purge_callback_options->ref_count, purge_callback_options, s_host_purge_callback_options_destroy);
229 purge_callback_options->on_purge_cache_complete_callback = on_purge_cache_complete_callback;
230 purge_callback_options->user_data = user_data;
231 return purge_callback_options;
232 }
233
234 static void s_purge_cache_callback(void *user_data) {
235 struct host_purge_callback_options *purge_callback_options = user_data;
236 aws_ref_count_release(&purge_callback_options->ref_count);
237 }
238
239 /*
240 * resolver lock must be held before calling this function
241 */
242 static void s_clear_default_resolver_entry_table_synced(struct default_host_resolver *resolver) {
243 struct aws_hash_table *table = &resolver->host_entry_table;
244 for (struct aws_hash_iter iter = aws_hash_iter_begin(table); !aws_hash_iter_done(&iter);
245 aws_hash_iter_next(&iter)) {
246 struct host_entry *entry = iter.element.value;
247 s_shutdown_host_entry(entry);
248 }
249
250 aws_hash_table_clear(table);
251 }
252
253 static int s_resolver_purge_cache(struct aws_host_resolver *resolver) {
254 struct default_host_resolver *default_host_resolver = resolver->impl;
255 aws_mutex_lock(&default_host_resolver->resolver_lock);
256 s_clear_default_resolver_entry_table_synced(default_host_resolver);
257 aws_mutex_unlock(&default_host_resolver->resolver_lock);
258
259 return AWS_OP_SUCCESS;
260 }
261
262 static void s_purge_host_cache_callback_task(struct aws_task *task, void *arg, enum aws_task_status status) {
263 (void)status;
264 struct host_purge_callback_options *options = arg;
265 aws_mem_release(options->allocator, task);
266 aws_ref_count_release(&options->ref_count);
267 }
268
269 static void s_sechdule_purge_cache_callback_async(
270 struct default_host_resolver *default_host_resolver,
271 struct host_purge_callback_options *purge_callback_options) {
272
273 struct aws_task *task = aws_mem_calloc(default_host_resolver->allocator, 1, sizeof(struct aws_task));
274 aws_task_init(task, s_purge_host_cache_callback_task, purge_callback_options, "async_purge_host_callback_task");
275
276 struct aws_event_loop *loop = aws_event_loop_group_get_next_loop(default_host_resolver->event_loop_group);
277 AWS_FATAL_ASSERT(loop != NULL);
278 aws_event_loop_schedule_task_now(loop, task);
279 }
280
281 static int s_resolver_purge_cache_with_callback(
282 struct aws_host_resolver *resolver,
283 aws_simple_completion_callback *on_purge_cache_complete_callback,
284 void *user_data) {
285
286 if (!on_purge_cache_complete_callback) {
287 return s_resolver_purge_cache(resolver);
288 }
289
290 struct default_host_resolver *default_host_resolver = resolver->impl;
291 aws_mutex_lock(&default_host_resolver->resolver_lock);
292 struct aws_hash_table *table = &default_host_resolver->host_entry_table;
293
294 struct host_purge_callback_options *purge_callback_options = s_host_purge_callback_options_new(
295 default_host_resolver->allocator, on_purge_cache_complete_callback, user_data);
296
297 /* purge all cache */
298 for (struct aws_hash_iter iter = aws_hash_iter_begin(table); !aws_hash_iter_done(&iter);
299 aws_hash_iter_next(&iter)) {
300 struct host_entry *entry = iter.element.value;
301 /* acquire a refernce to wait for the callback to trigger */
302 aws_ref_count_acquire(&purge_callback_options->ref_count);
303 aws_mutex_lock(&entry->entry_lock);
304 entry->on_host_purge_complete = s_purge_cache_callback;
305 entry->on_host_purge_complete_user_data = purge_callback_options;
306 entry->state = DRS_SHUTTING_DOWN;
307 aws_mutex_unlock(&entry->entry_lock);
308 }
309
310 aws_hash_table_clear(table);
311 aws_mutex_unlock(&default_host_resolver->resolver_lock);
312
313 /* release the original reference async */
314 s_sechdule_purge_cache_callback_async(default_host_resolver, purge_callback_options);
315 return AWS_OP_SUCCESS;
316 }
317
318 static void s_cleanup_default_resolver(struct aws_host_resolver *resolver) {
319 struct default_host_resolver *default_host_resolver = resolver->impl;
320
321 aws_event_loop_group_release(default_host_resolver->event_loop_group);
322 aws_hash_table_clean_up(&default_host_resolver->host_entry_table);
323
324 aws_mutex_clean_up(&default_host_resolver->resolver_lock);
325
326 aws_simple_completion_callback *shutdown_callback = resolver->shutdown_options.shutdown_callback_fn;
327 void *shutdown_completion_user_data = resolver->shutdown_options.shutdown_callback_user_data;
328
329 aws_mem_release(resolver->allocator, resolver);
330
331 /* invoke shutdown completion finally */
332 if (shutdown_callback != NULL) {
333 shutdown_callback(shutdown_completion_user_data);
334 }
335 }
336
337 static void resolver_destroy(struct aws_host_resolver *resolver) {
338 struct default_host_resolver *default_host_resolver = resolver->impl;
339
340 bool cleanup_resolver = false;
341
342 aws_mutex_lock(&default_host_resolver->resolver_lock);
343
344 AWS_FATAL_ASSERT(default_host_resolver->state == DRS_ACTIVE);
345
346 s_clear_default_resolver_entry_table_synced(default_host_resolver);
347 default_host_resolver->state = DRS_SHUTTING_DOWN;
348 if (default_host_resolver->pending_host_entry_shutdown_completion_callbacks == 0) {
349 cleanup_resolver = true;
350 }
351 aws_mutex_unlock(&default_host_resolver->resolver_lock);
352
353 if (cleanup_resolver) {
354 s_cleanup_default_resolver(resolver);
355 }
356 }
357
358 struct pending_callback {
359 aws_on_host_resolved_result_fn *callback;
360 void *user_data;
361 struct aws_linked_list_node node;
362 };
363
364 static void s_clear_address_list(struct aws_array_list *address_list) {
365 for (size_t i = 0; i < aws_array_list_length(address_list); ++i) {
366 struct aws_host_address *address = NULL;
367 aws_array_list_get_at_ptr(address_list, (void **)&address, i);
368 aws_host_address_clean_up(address);
369 }
370
371 aws_array_list_clear(address_list);
372 }
373
374 static void s_clean_up_host_entry(struct host_entry *entry) {
375 if (entry == NULL) {
376 return;
377 }
378
379 /*
380 * This can happen if the resolver's final reference drops while an unanswered query is pending on an entry.
381 *
382 * You could add an assertion that the resolver is in the shut down state if this condition hits but that
383 * requires additional locking just to make the assert.
384 */
385 if (!aws_linked_list_empty(&entry->pending_resolution_callbacks)) {
386 aws_raise_error(AWS_IO_DNS_HOST_REMOVED_FROM_CACHE);
387 }
388
389 while (!aws_linked_list_empty(&entry->pending_resolution_callbacks)) {
390 struct aws_linked_list_node *resolution_callback_node =
391 aws_linked_list_pop_front(&entry->pending_resolution_callbacks);
392 struct pending_callback *pending_callback =
393 AWS_CONTAINER_OF(resolution_callback_node, struct pending_callback, node);
394
395 pending_callback->callback(
396 entry->resolver, entry->host_name, AWS_IO_DNS_HOST_REMOVED_FROM_CACHE, NULL, pending_callback->user_data);
397
398 aws_mem_release(entry->allocator, pending_callback);
399 }
400
401 aws_cache_destroy(entry->aaaa_records);
402 aws_cache_destroy(entry->a_records);
403 aws_cache_destroy(entry->failed_connection_a_records);
404 aws_cache_destroy(entry->failed_connection_aaaa_records);
405 aws_string_destroy((void *)entry->host_name);
406
407 s_clear_address_list(&entry->new_addresses);
408 aws_array_list_clean_up(&entry->new_addresses);
409
410 s_clear_address_list(&entry->expired_addresses);
411 aws_array_list_clean_up(&entry->expired_addresses);
412
413 aws_mem_release(entry->allocator, entry);
414 }
415
416 static void s_on_host_entry_shutdown_completion(void *user_data) {
417 struct host_entry *entry = user_data;
418 struct aws_host_resolver *resolver = entry->resolver;
419 struct default_host_resolver *default_host_resolver = resolver->impl;
420
421 s_clean_up_host_entry(entry);
422
423 bool cleanup_resolver = false;
424
425 aws_mutex_lock(&default_host_resolver->resolver_lock);
426 --default_host_resolver->pending_host_entry_shutdown_completion_callbacks;
427 if (default_host_resolver->state == DRS_SHUTTING_DOWN &&
428 default_host_resolver->pending_host_entry_shutdown_completion_callbacks == 0) {
429 cleanup_resolver = true;
430 }
431 aws_mutex_unlock(&default_host_resolver->resolver_lock);
432
433 if (cleanup_resolver) {
434 s_cleanup_default_resolver(resolver);
435 }
436 }
437
438 static int s_copy_address_into_array_list(struct aws_host_address *address, struct aws_array_list *address_list) {
439
440 /*
441 * This is the worst.
442 *
443 * We have to copy the cache address while we still have a write lock. Otherwise, connection failures
444 * can sneak in and destroy our address by moving the address to/from the various lru caches.
445 *
446 * But there's no nice copy construction into an array list, so we get to
447 * (1) Push a zeroed dummy element onto the array list
448 * (2) Get its pointer
449 * (3) Call aws_host_address_copy onto it. If that fails, pop the dummy element.
450 */
451 struct aws_host_address dummy;
452 AWS_ZERO_STRUCT(dummy);
453
454 if (aws_array_list_push_back(address_list, &dummy)) {
455 return AWS_OP_ERR;
456 }
457
458 struct aws_host_address *dest_copy = NULL;
459 aws_array_list_get_at_ptr(address_list, (void **)&dest_copy, aws_array_list_length(address_list) - 1);
460 AWS_FATAL_ASSERT(dest_copy != NULL);
461
462 if (aws_host_address_copy(address, dest_copy)) {
463 aws_array_list_pop_back(address_list);
464 return AWS_OP_ERR;
465 }
466
467 return AWS_OP_SUCCESS;
468 }
469
470 static uint64_t s_get_system_time_for_default_resolver(struct aws_host_resolver *resolver) {
471 struct default_host_resolver *default_resolver = resolver->impl;
472
473 uint64_t timestamp = 0;
474 (*default_resolver->system_clock_fn)(&timestamp);
475
476 return timestamp;
477 }
478
479 /* this only ever gets called after resolution has already run. We expect that the entry's lock
480 has been acquired for writing before this function is called and released afterwards. */
481 static inline void process_records(
482 struct host_entry *host_entry,
483 struct aws_cache *records,
484 struct aws_cache *failed_records) {
485
486 struct aws_host_resolver *resolver = host_entry->resolver;
487 uint64_t timestamp = s_get_system_time_for_default_resolver(resolver);
488
489 size_t record_count = aws_cache_get_element_count(records);
490 size_t expired_records = 0;
491
492 /* since this only ever gets called after resolution has already run, we're in a dns outage
493 * if everything is expired. Leave an element so we can keep trying. */
494 for (size_t index = 0; index < record_count && expired_records < record_count - 1; ++index) {
495 struct aws_host_address_cache_entry *lru_element_entry = aws_lru_cache_use_lru_element(records);
496
497 if (lru_element_entry->address.expiry < timestamp) {
498 AWS_LOGF_DEBUG(
499 AWS_LS_IO_DNS,
500 "static: purging expired record %s for %s",
501 lru_element_entry->address.address->bytes,
502 lru_element_entry->address.host->bytes);
503 expired_records++;
504
505 aws_cache_remove(records, lru_element_entry->address.address);
506 }
507 }
508
509 record_count = aws_cache_get_element_count(records);
510 AWS_LOGF_TRACE(AWS_LS_IO_DNS, "static: remaining record count for host %d", (int)record_count);
511
512 /* if we don't have any known good addresses, take the least recently used, but not expired address with a history
513 * of spotty behavior and upgrade it for reuse. */
514 bool should_promote = (record_count == 0);
515 size_t failed_count = aws_cache_get_element_count(failed_records);
516 for (size_t index = 0; index < failed_count; ++index) {
517 struct aws_host_address_cache_entry *lru_element_entry = aws_lru_cache_use_lru_element(failed_records);
518
519 /* remove expired entries */
520 if (timestamp >= lru_element_entry->address.expiry) {
521 AWS_LOGF_DEBUG(
522 AWS_LS_IO_DNS,
523 "static: purging expired bad record %s for %s",
524 lru_element_entry->address.address->bytes,
525 lru_element_entry->address.host->bytes);
526 aws_cache_remove(failed_records, lru_element_entry->address.address);
527
528 } else if (should_promote) {
529
530 struct aws_host_address_cache_entry *to_add =
531 aws_mem_calloc(host_entry->allocator, 1, sizeof(struct aws_host_address_cache_entry));
532 if (to_add == NULL) {
533 continue;
534 }
535
536 if (aws_host_address_cache_entry_copy(lru_element_entry, to_add) ||
537 aws_cache_put(records, to_add->address.address, to_add)) {
538 aws_host_address_clean_up(&to_add->address);
539 aws_mem_release(host_entry->allocator, to_add);
540 continue;
541 }
542
543 /*
544 * Promoting an address from failed to good should trigger the new address callback
545 */
546 s_copy_address_into_array_list(&lru_element_entry->address, &host_entry->new_addresses);
547
548 AWS_LOGF_INFO(
549 AWS_LS_IO_DNS,
550 "static: promoting spotty record %s for %s back to good list",
551 lru_element_entry->address.address->bytes,
552 lru_element_entry->address.host->bytes);
553
554 aws_cache_remove(failed_records, lru_element_entry->address.address);
555
556 /* we only want to promote one per process run.*/
557 should_promote = false;
558 }
559 }
560 }
561
562 static int s_resolver_purge_host_cache(
563 struct aws_host_resolver *resolver,
564 const struct aws_host_resolver_purge_host_options *options) {
565
566 AWS_PRECONDITION(resolver);
567 if (options == NULL) {
568 AWS_LOGF_ERROR(AWS_LS_IO_DNS, "Cannot purge host cache; options structure is NULL.");
569 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
570 }
571
572 struct default_host_resolver *default_host_resolver = resolver->impl;
573 AWS_LOGF_INFO(AWS_LS_IO_DNS, "id=%p: purging record for %s", (void *)resolver, options->host->bytes);
574
575 aws_mutex_lock(&default_host_resolver->resolver_lock);
576
577 struct aws_hash_element *element = NULL;
578 aws_hash_table_find(&default_host_resolver->host_entry_table, options->host, &element);
579
580 /* Success if entry doesn't exist in cache. */
581 if (element == NULL) {
582 aws_mutex_unlock(&default_host_resolver->resolver_lock);
583 if (options->on_host_purge_complete_callback != NULL) {
584 /* Schedule completion callback asynchronouly */
585 struct host_purge_callback_options *purge_callback_options = s_host_purge_callback_options_new(
586 default_host_resolver->allocator, options->on_host_purge_complete_callback, options->user_data);
587
588 s_sechdule_purge_cache_callback_async(default_host_resolver, purge_callback_options);
589 }
590 return AWS_OP_SUCCESS;
591 }
592
593 struct host_entry *host_entry = element->value;
594 AWS_FATAL_ASSERT(host_entry);
595
596 /* Setup the on_host_purge_complete callback. */
597 aws_mutex_lock(&host_entry->entry_lock);
598 AWS_FATAL_ASSERT(!host_entry->on_host_purge_complete);
599 AWS_FATAL_ASSERT(!host_entry->on_host_purge_complete_user_data);
600 host_entry->on_host_purge_complete = options->on_host_purge_complete_callback;
601 host_entry->on_host_purge_complete_user_data = options->user_data;
602 aws_mutex_unlock(&host_entry->entry_lock);
603
604 s_shutdown_host_entry(host_entry);
605 aws_hash_table_remove_element(&default_host_resolver->host_entry_table, element);
606
607 aws_mutex_unlock(&default_host_resolver->resolver_lock);
608 return AWS_OP_SUCCESS;
609 }
610
611 static int resolver_record_connection_failure(
612 struct aws_host_resolver *resolver,
613 const struct aws_host_address *address) {
614 struct default_host_resolver *default_host_resolver = resolver->impl;
615
616 AWS_LOGF_INFO(
617 AWS_LS_IO_DNS,
618 "id=%p: recording failure for record %s for %s, moving to bad list",
619 (void *)resolver,
620 address->address->bytes,
621 address->host->bytes);
622
623 aws_mutex_lock(&default_host_resolver->resolver_lock);
624
625 struct aws_hash_element *element = NULL;
626 if (aws_hash_table_find(&default_host_resolver->host_entry_table, address->host, &element)) {
627 aws_mutex_unlock(&default_host_resolver->resolver_lock);
628 return AWS_OP_ERR;
629 }
630
631 struct host_entry *host_entry = NULL;
632 if (element != NULL) {
633 host_entry = element->value;
634 AWS_FATAL_ASSERT(host_entry);
635 }
636
637 if (host_entry) {
638 struct aws_host_address_cache_entry *cached_address_entry = NULL;
639
640 aws_mutex_lock(&host_entry->entry_lock);
641 aws_mutex_unlock(&default_host_resolver->resolver_lock);
642 struct aws_cache *address_table =
643 address->record_type == AWS_ADDRESS_RECORD_TYPE_AAAA ? host_entry->aaaa_records : host_entry->a_records;
644
645 struct aws_cache *failed_table = address->record_type == AWS_ADDRESS_RECORD_TYPE_AAAA
646 ? host_entry->failed_connection_aaaa_records
647 : host_entry->failed_connection_a_records;
648
649 aws_cache_find(address_table, address->address, (void **)&cached_address_entry);
650
651 struct aws_host_address_cache_entry *address_entry_copy = NULL;
652 if (cached_address_entry) {
653 address_entry_copy = aws_mem_calloc(resolver->allocator, 1, sizeof(struct aws_host_address_cache_entry));
654
655 if (!address_entry_copy || aws_host_address_cache_entry_copy(cached_address_entry, address_entry_copy)) {
656 goto error_host_entry_cleanup;
657 }
658
659 /*
660 * This will trigger an expiration callback since the good caches add the removed address to the
661 * host_entry's expired list, via the cache's on_delete callback
662 */
663 if (aws_cache_remove(address_table, cached_address_entry->address.address)) {
664 goto error_host_entry_cleanup;
665 }
666
667 address_entry_copy->address.connection_failure_count += 1;
668
669 if (aws_cache_put(failed_table, address_entry_copy->address.address, address_entry_copy)) {
670 goto error_host_entry_cleanup;
671 }
672 } else {
673 if (aws_cache_find(failed_table, address->address, (void **)&cached_address_entry)) {
674 goto error_host_entry_cleanup;
675 }
676
677 if (cached_address_entry) {
678 cached_address_entry->address.connection_failure_count += 1;
679 }
680 }
681 aws_mutex_unlock(&host_entry->entry_lock);
682 return AWS_OP_SUCCESS;
683
684 error_host_entry_cleanup:
685 if (address_entry_copy) {
686 aws_host_address_clean_up(&address_entry_copy->address);
687 aws_mem_release(resolver->allocator, address_entry_copy);
688 }
689 aws_mutex_unlock(&host_entry->entry_lock);
690 return AWS_OP_ERR;
691 }
692
693 aws_mutex_unlock(&default_host_resolver->resolver_lock);
694
695 return AWS_OP_SUCCESS;
696 }
697
698 /*
699 * A bunch of convenience functions for the host resolver background thread function
700 */
701
702 struct aws_host_address_cache_entry_lookup {
703 struct aws_host_address_cache_entry *entry; /* if lookup succeeds, this is non-NULL */
704 bool is_fallback;
705 };
706
707 static struct aws_host_address_cache_entry_lookup s_find_cached_address_entry_aux(
708 struct aws_cache *primary_records,
709 struct aws_cache *fallback_records,
710 const struct aws_string *address) {
711
712 struct aws_host_address_cache_entry *found = NULL;
713 bool is_fallback = false;
714 aws_cache_find(primary_records, address, (void **)&found);
715 if (found == NULL) {
716 aws_cache_find(fallback_records, address, (void **)&found);
717 if (found != NULL) {
718 is_fallback = true;
719 }
720 }
721
722 return (struct aws_host_address_cache_entry_lookup){.entry = found, .is_fallback = is_fallback};
723 }
724
725 /*
726 * Looks in both the good and failed connection record sets for a given host record
727 */
728 static struct aws_host_address_cache_entry_lookup s_find_cached_address_entry(
729 struct host_entry *entry,
730 const struct aws_string *address,
731 enum aws_address_record_type record_type) {
732
733 switch (record_type) {
734 case AWS_ADDRESS_RECORD_TYPE_AAAA:
735 return s_find_cached_address_entry_aux(entry->aaaa_records, entry->failed_connection_aaaa_records, address);
736
737 case AWS_ADDRESS_RECORD_TYPE_A:
738 return s_find_cached_address_entry_aux(entry->a_records, entry->failed_connection_a_records, address);
739
740 default:
741 return (struct aws_host_address_cache_entry_lookup){.entry = NULL};
742 }
743 }
744
745 static struct aws_host_address_cache_entry *s_get_lru_address_entry_aux(
746 struct aws_cache *primary_records,
747 struct aws_cache *fallback_records) {
748
749 struct aws_host_address_cache_entry *address_entry = aws_lru_cache_use_lru_element(primary_records);
750 if (address_entry == NULL) {
751 aws_lru_cache_use_lru_element(fallback_records);
752 }
753
754 return address_entry;
755 }
756
757 /*
758 * Looks in both the good and failed connection record sets for the LRU host record
759 */
760 static struct aws_host_address_cache_entry *s_get_lru_address(
761 struct host_entry *entry,
762 enum aws_address_record_type record_type) {
763 switch (record_type) {
764 case AWS_ADDRESS_RECORD_TYPE_AAAA:
765 return s_get_lru_address_entry_aux(entry->aaaa_records, entry->failed_connection_aaaa_records);
766
767 case AWS_ADDRESS_RECORD_TYPE_A:
768 return s_get_lru_address_entry_aux(entry->a_records, entry->failed_connection_a_records);
769
770 default:
771 return NULL;
772 }
773 }
774
775 static void s_update_address_cache(
776 struct host_entry *host_entry,
777 struct aws_array_list *address_list,
778 uint64_t new_expiration) {
779
780 AWS_PRECONDITION(host_entry);
781 AWS_PRECONDITION(address_list);
782
783 for (size_t i = 0; i < aws_array_list_length(address_list); ++i) {
784 struct aws_host_address *fresh_resolved_address = NULL;
785 aws_array_list_get_at_ptr(address_list, (void **)&fresh_resolved_address, i);
786
787 struct aws_host_address_cache_entry_lookup cache_lookup = s_find_cached_address_entry(
788 host_entry, fresh_resolved_address->address, fresh_resolved_address->record_type);
789
790 if (cache_lookup.entry) {
791 struct aws_host_address_cache_entry *address_to_cache_entry = cache_lookup.entry;
792 if (cache_lookup.is_fallback) {
793 AWS_LOGF_TRACE(
794 AWS_LS_IO_DNS,
795 "static: NOT updating expiry for %s for host %s because it's in bad list."
796 " Keeping old expiry of %" PRIu64,
797 aws_string_c_str(address_to_cache_entry->address.address),
798 aws_string_c_str(host_entry->host_name),
799 address_to_cache_entry->address.expiry);
800 } else {
801 address_to_cache_entry->address.expiry = new_expiration;
802 AWS_LOGF_TRACE(
803 AWS_LS_IO_DNS,
804 "static: updating expiry for %s for host %s to %" PRIu64,
805 aws_string_c_str(address_to_cache_entry->address.address),
806 aws_string_c_str(host_entry->host_name),
807 new_expiration);
808 }
809 } else {
810 struct aws_host_address_cache_entry *address_to_cache_entry =
811 aws_mem_calloc(host_entry->allocator, 1, sizeof(struct aws_host_address_cache_entry));
812
813 aws_host_address_move(fresh_resolved_address, &address_to_cache_entry->address);
814 address_to_cache_entry->address.expiry = new_expiration;
815 address_to_cache_entry->entry = host_entry;
816
817 struct aws_cache *address_table =
818 address_to_cache_entry->address.record_type == AWS_ADDRESS_RECORD_TYPE_AAAA ? host_entry->aaaa_records
819 : host_entry->a_records;
820
821 if (aws_cache_put(address_table, address_to_cache_entry->address.address, address_to_cache_entry)) {
822 AWS_LOGF_ERROR(
823 AWS_LS_IO_DNS,
824 "static: could not add new address to host entry cache for host '%s' in "
825 "s_update_address_cache.",
826 host_entry->host_name->bytes);
827
828 continue;
829 }
830
831 AWS_LOGF_DEBUG(
832 AWS_LS_IO_DNS,
833 "static: new address resolved %s for host %s caching",
834 address_to_cache_entry->address.address->bytes,
835 host_entry->host_name->bytes);
836
837 struct aws_host_address new_address_copy;
838
839 if (aws_host_address_copy(&address_to_cache_entry->address, &new_address_copy)) {
840 AWS_LOGF_ERROR(
841 AWS_LS_IO_DNS,
842 "static: could not copy address for new-address list for host '%s' in s_update_address_cache.",
843 host_entry->host_name->bytes);
844
845 continue;
846 }
847
848 if (aws_array_list_push_back(&host_entry->new_addresses, &new_address_copy)) {
849 aws_host_address_clean_up(&new_address_copy);
850
851 AWS_LOGF_ERROR(
852 AWS_LS_IO_DNS,
853 "static: could not push address to new-address list for host '%s' in s_update_address_cache.",
854 host_entry->host_name->bytes);
855
856 continue;
857 }
858 }
859 }
860 }
861
862 static void s_copy_address_into_callback_set(
863 struct aws_host_address_cache_entry *entry,
864 struct aws_array_list *callback_addresses,
865 const struct aws_string *host_name) {
866
867 if (entry != NULL) {
868 if (s_copy_address_into_array_list(&entry->address, callback_addresses)) {
869 AWS_LOGF_ERROR(
870 AWS_LS_IO_DNS,
871 "static: failed to vend address %s for host %s to caller",
872 entry->address.address->bytes,
873 host_name->bytes);
874 return;
875 }
876
877 entry->address.use_count += 1;
878
879 AWS_LOGF_TRACE(
880 AWS_LS_IO_DNS,
881 "static: vending address %s for host %s to caller",
882 entry->address.address->bytes,
883 host_name->bytes);
884 }
885 }
886
887 static bool s_host_entry_finished_pred(void *user_data) {
888 struct host_entry *entry = user_data;
889
890 return entry->state == DRS_SHUTTING_DOWN;
891 }
892
893 static bool s_host_entry_finished_or_pending_request_pred(void *user_data) {
894 struct host_entry *entry = user_data;
895
896 return entry->state == DRS_SHUTTING_DOWN || !aws_linked_list_empty(&entry->pending_resolution_callbacks);
897 }
898
899 static const uint64_t AWS_MINIMUM_WAIT_BETWEEN_DNS_QUERIES_NS = 100000000; /* 100 ms */
900
901 static void aws_host_resolver_thread(void *arg) {
902 struct host_entry *host_entry = arg;
903
904 uint64_t max_no_solicitation_interval = aws_timestamp_convert(
905 aws_max_u64(1, host_entry->resolution_config.max_ttl), AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
906
907 uint64_t wait_between_resolves_interval =
908 aws_min_u64(max_no_solicitation_interval, host_entry->resolve_frequency_ns);
909
910 uint64_t shutdown_only_wait_time = AWS_MINIMUM_WAIT_BETWEEN_DNS_QUERIES_NS;
911 uint64_t request_interruptible_wait_time = 0;
912 if (wait_between_resolves_interval > shutdown_only_wait_time) {
913 request_interruptible_wait_time = wait_between_resolves_interval - shutdown_only_wait_time;
914 }
915
916 bool keep_going = true;
917
918 struct aws_array_list address_list;
919 AWS_ZERO_STRUCT(address_list);
920 struct aws_array_list new_address_list;
921 AWS_ZERO_STRUCT(new_address_list);
922 struct aws_array_list expired_address_list;
923 AWS_ZERO_STRUCT(expired_address_list);
924
925 if (aws_array_list_init_dynamic(&address_list, host_entry->allocator, 4, sizeof(struct aws_host_address))) {
926 goto done;
927 }
928
929 if (aws_array_list_init_dynamic(&new_address_list, host_entry->allocator, 4, sizeof(struct aws_host_address))) {
930 goto done;
931 }
932
933 if (aws_array_list_init_dynamic(&expired_address_list, host_entry->allocator, 4, sizeof(struct aws_host_address))) {
934 goto done;
935 }
936
937 while (keep_going) {
938
939 /* resolve and then process each record */
940 int err_code = AWS_ERROR_SUCCESS;
941 if (host_entry->resolution_config.impl(
942 host_entry->allocator, host_entry->host_name, &address_list, host_entry->resolution_config.impl_data)) {
943
944 err_code = aws_last_error();
945 }
946
947 if (err_code == AWS_ERROR_SUCCESS) {
948 AWS_LOGF_DEBUG(
949 AWS_LS_IO_DNS,
950 "static, resolving host %s successful, returned %d addresses",
951 aws_string_c_str(host_entry->host_name),
952 (int)aws_array_list_length(&address_list));
953 } else {
954 AWS_LOGF_WARN(
955 AWS_LS_IO_DNS,
956 "static, resolving host %s failed, ec %d (%s)",
957 aws_string_c_str(host_entry->host_name),
958 err_code,
959 aws_error_debug_str(err_code));
960 }
961
962 uint64_t timestamp = s_get_system_time_for_default_resolver(host_entry->resolver);
963 uint64_t new_expiry = timestamp + (host_entry->resolution_config.max_ttl * NS_PER_SEC);
964
965 struct aws_linked_list pending_resolve_copy;
966 aws_linked_list_init(&pending_resolve_copy);
967
968 /*
969 * Within the lock we
970 * (1) Update the cache with the newly resolved addresses
971 * (2) Process all held addresses looking for expired or promotable ones
972 * (3) Prep for callback invocations
973 */
974 aws_mutex_lock(&host_entry->entry_lock);
975
976 if (!err_code) {
977 s_update_address_cache(host_entry, &address_list, new_expiry);
978 }
979
980 /*
981 * process and clean_up records in the entry. occasionally, failed connect records will be upgraded
982 * for retry.
983 */
984 process_records(host_entry, host_entry->aaaa_records, host_entry->failed_connection_aaaa_records);
985 process_records(host_entry, host_entry->a_records, host_entry->failed_connection_a_records);
986
987 aws_linked_list_swap_contents(&pending_resolve_copy, &host_entry->pending_resolution_callbacks);
988
989 aws_mutex_unlock(&host_entry->entry_lock);
990
991 /*
992 * Clean up resolved addressed outside of the lock
993 */
994 s_clear_address_list(&address_list);
995
996 struct aws_host_address address_array[2];
997 AWS_ZERO_ARRAY(address_array);
998
999 /*
1000 * Perform the actual subscriber notifications
1001 */
1002 while (!aws_linked_list_empty(&pending_resolve_copy)) {
1003 struct aws_linked_list_node *resolution_callback_node = aws_linked_list_pop_front(&pending_resolve_copy);
1004 struct pending_callback *pending_callback =
1005 AWS_CONTAINER_OF(resolution_callback_node, struct pending_callback, node);
1006
1007 struct aws_array_list callback_address_list;
1008 aws_array_list_init_static(&callback_address_list, address_array, 2, sizeof(struct aws_host_address));
1009
1010 aws_mutex_lock(&host_entry->entry_lock);
1011 s_copy_address_into_callback_set(
1012 s_get_lru_address(host_entry, AWS_ADDRESS_RECORD_TYPE_AAAA),
1013 &callback_address_list,
1014 host_entry->host_name);
1015 s_copy_address_into_callback_set(
1016 s_get_lru_address(host_entry, AWS_ADDRESS_RECORD_TYPE_A),
1017 &callback_address_list,
1018 host_entry->host_name);
1019 aws_mutex_unlock(&host_entry->entry_lock);
1020
1021 size_t callback_address_list_size = aws_array_list_length(&callback_address_list);
1022 if (callback_address_list_size > 0) {
1023 AWS_LOGF_DEBUG(
1024 AWS_LS_IO_DNS,
1025 "static, invoking resolution callback for host %s with %d addresses",
1026 aws_string_c_str(host_entry->host_name),
1027 (int)callback_address_list_size);
1028 } else {
1029 AWS_LOGF_DEBUG(
1030 AWS_LS_IO_DNS,
1031 "static, invoking resolution callback for host %s with failure",
1032 aws_string_c_str(host_entry->host_name));
1033 }
1034
1035 if (callback_address_list_size > 0) {
1036 pending_callback->callback(
1037 host_entry->resolver,
1038 host_entry->host_name,
1039 AWS_OP_SUCCESS,
1040 &callback_address_list,
1041 pending_callback->user_data);
1042
1043 } else {
1044 int error_code = (err_code != AWS_ERROR_SUCCESS) ? err_code : AWS_IO_DNS_QUERY_FAILED;
1045 pending_callback->callback(
1046 host_entry->resolver, host_entry->host_name, error_code, NULL, pending_callback->user_data);
1047 }
1048
1049 s_clear_address_list(&callback_address_list);
1050
1051 aws_mem_release(host_entry->allocator, pending_callback);
1052 }
1053
1054 aws_mutex_lock(&host_entry->entry_lock);
1055
1056 ++host_entry->resolves_since_last_request;
1057
1058 /*
1059 * A long resolve frequency matched with a connection failure can induce a state of DNS starvation, where
1060 * additional resolution requests go into the queue but since there's no good records and the thread is sleeping
1061 * for a long time, nothing happens.
1062 *
1063 * While we could make the wait predicate also check the queue of requests, there is a worry that a
1064 * host that can't be resolved (user error, dns record removal, etc...) could lead to a "spammy" scenario
1065 * where the thread generates DNS requests extremely quickly, ie, the sleep becomes almost instant.
1066 *
1067 * We'd like to be able to express the wait here as something a bit more complex:
1068 *
1069 * "Wait until either (1) shutdown notice, or (2) a small amount of time has passed and there are pending
1070 * requests, or (3) the resolution interval has passed"
1071 *
1072 * While seemingly complicated, we can do this actually just by chaining two waits:
1073 *
1074 * (1) The first wait is for a short amount of time and only predicates on the shutdown notice
1075 * (2) The second wait is for the remaining frequency interval and predicates on either the shutdown notice
1076 * or a pending resolve request
1077 *
1078 * This leaves us with wait behavior where:
1079 * (1) Shutdown always fully interrupts and immediately causes the thread function to complete
1080 * (2) Absent shutdown, there is always a controllable, non-trivial sleep between resolves
1081 * (3) Starvation is avoided as pending requests can wake the resolver thread independent of resolution
1082 * frequency
1083 */
1084 aws_condition_variable_wait_for_pred(
1085 &host_entry->entry_signal,
1086 &host_entry->entry_lock,
1087 shutdown_only_wait_time,
1088 s_host_entry_finished_pred,
1089 host_entry);
1090
1091 if (request_interruptible_wait_time > 0) {
1092 aws_condition_variable_wait_for_pred(
1093 &host_entry->entry_signal,
1094 &host_entry->entry_lock,
1095 request_interruptible_wait_time,
1096 s_host_entry_finished_or_pending_request_pred,
1097 host_entry);
1098 }
1099
1100 aws_mutex_unlock(&host_entry->entry_lock);
1101
1102 /*
1103 * This is a bit awkward that we unlock the entry and then relock both the resolver and the entry, but it
1104 * is mandatory that -- in order to maintain the consistent view of the resolver table (entry exist => entry
1105 * is alive and can be queried) -- we have the resolver lock as well before making the decision to remove
1106 * the entry from the table and terminate the thread.
1107 */
1108 struct default_host_resolver *resolver = host_entry->resolver->impl;
1109 aws_mutex_lock(&resolver->resolver_lock);
1110
1111 aws_mutex_lock(&host_entry->entry_lock);
1112
1113 uint64_t now = s_get_system_time_for_default_resolver(host_entry->resolver);
1114
1115 /*
1116 * The only way we terminate the loop with pending queries is if the resolver itself has no more references
1117 * to it and is going away. In that case, the pending queries will be completed (with failure) by the
1118 * final clean up of this entry.
1119 */
1120 if (aws_linked_list_empty(&host_entry->pending_resolution_callbacks) &&
1121 host_entry->last_resolve_request_timestamp_ns + max_no_solicitation_interval < now) {
1122 host_entry->state = DRS_SHUTTING_DOWN;
1123 }
1124
1125 keep_going = host_entry->state == DRS_ACTIVE;
1126 if (!keep_going) {
1127 aws_hash_table_remove(&resolver->host_entry_table, host_entry->host_name, NULL, NULL);
1128 }
1129
1130 aws_array_list_swap_contents(&host_entry->new_addresses, &new_address_list);
1131 aws_array_list_swap_contents(&host_entry->expired_addresses, &expired_address_list);
1132
1133 aws_mutex_unlock(&host_entry->entry_lock);
1134 aws_mutex_unlock(&resolver->resolver_lock);
1135
1136 s_clear_address_list(&new_address_list);
1137 s_clear_address_list(&expired_address_list);
1138 }
1139
1140 AWS_LOGF_DEBUG(
1141 AWS_LS_IO_DNS,
1142 "static: Either no requests have been made for an address for %s for the duration "
1143 "of the ttl, or this thread is being forcibly shutdown. Killing thread.",
1144 host_entry->host_name->bytes);
1145
1146 done:
1147
1148 AWS_FATAL_ASSERT(aws_array_list_length(&address_list) == 0);
1149 AWS_FATAL_ASSERT(aws_array_list_length(&new_address_list) == 0);
1150 AWS_FATAL_ASSERT(aws_array_list_length(&expired_address_list) == 0);
1151
1152 aws_array_list_clean_up(&address_list);
1153 aws_array_list_clean_up(&new_address_list);
1154 aws_array_list_clean_up(&expired_address_list);
1155
1156 /* trigger the purge complete callback */
1157 if (host_entry->on_host_purge_complete != NULL) {
1158 host_entry->on_host_purge_complete(host_entry->on_host_purge_complete_user_data);
1159 }
1160 /* please don't fail */
1161 aws_thread_current_at_exit(s_on_host_entry_shutdown_completion, host_entry);
1162 }
1163
1164 static void on_cache_entry_removed_helper(struct aws_host_address_cache_entry *entry) {
1165 AWS_LOGF_DEBUG(
1166 AWS_LS_IO_DNS,
1167 "static: purging address %s for host %s from "
1168 "the cache due to cache eviction or shutdown",
1169 entry->address.address->bytes,
1170 entry->address.host->bytes);
1171
1172 struct aws_allocator *allocator = entry->address.allocator;
1173 aws_host_address_clean_up(&entry->address);
1174 aws_mem_release(allocator, entry);
1175 }
1176
1177 static void on_good_address_entry_removed(void *value) {
1178 struct aws_host_address_cache_entry *entry = value;
1179 if (entry == NULL) {
1180 return;
1181 }
1182
1183 s_copy_address_into_array_list(&entry->address, &entry->entry->expired_addresses);
1184
1185 on_cache_entry_removed_helper(entry);
1186 }
1187
1188 static void on_failed_address_entry_removed(void *value) {
1189 struct aws_host_address_cache_entry *entry = value;
1190
1191 on_cache_entry_removed_helper(entry);
1192 }
1193
1194 /*
1195 * The resolver lock must be held before calling this function
1196 */
1197 static inline int create_and_init_host_entry(
1198 struct aws_host_resolver *resolver,
1199 const struct aws_string *host_name,
1200 aws_on_host_resolved_result_fn *res,
1201 const struct aws_host_resolution_config *config,
1202 uint64_t timestamp,
1203 void *user_data) {
1204 struct host_entry *new_host_entry = aws_mem_calloc(resolver->allocator, 1, sizeof(struct host_entry));
1205 if (!new_host_entry) {
1206 return AWS_OP_ERR;
1207 }
1208
1209 new_host_entry->resolver = resolver;
1210 new_host_entry->allocator = resolver->allocator;
1211 new_host_entry->last_resolve_request_timestamp_ns = timestamp;
1212 new_host_entry->resolves_since_last_request = 0;
1213 new_host_entry->resolve_frequency_ns =
1214 (config->resolve_frequency_ns != 0) ? config->resolve_frequency_ns : NS_PER_SEC;
1215 new_host_entry->state = DRS_ACTIVE;
1216
1217 bool thread_init = false;
1218 struct pending_callback *pending_callback = NULL;
1219 const struct aws_string *host_string_copy = aws_string_new_from_string(resolver->allocator, host_name);
1220 if (AWS_UNLIKELY(!host_string_copy)) {
1221 goto setup_host_entry_error;
1222 }
1223
1224 new_host_entry->host_name = host_string_copy;
1225 new_host_entry->a_records = aws_cache_new_lru(
1226 new_host_entry->allocator,
1227 aws_hash_string,
1228 aws_hash_callback_string_eq,
1229 NULL,
1230 on_good_address_entry_removed,
1231 config->max_ttl);
1232 if (AWS_UNLIKELY(!new_host_entry->a_records)) {
1233 goto setup_host_entry_error;
1234 }
1235
1236 new_host_entry->aaaa_records = aws_cache_new_lru(
1237 new_host_entry->allocator,
1238 aws_hash_string,
1239 aws_hash_callback_string_eq,
1240 NULL,
1241 on_good_address_entry_removed,
1242 config->max_ttl);
1243 if (AWS_UNLIKELY(!new_host_entry->aaaa_records)) {
1244 goto setup_host_entry_error;
1245 }
1246
1247 new_host_entry->failed_connection_a_records = aws_cache_new_lru(
1248 new_host_entry->allocator,
1249 aws_hash_string,
1250 aws_hash_callback_string_eq,
1251 NULL,
1252 on_failed_address_entry_removed,
1253 config->max_ttl);
1254 if (AWS_UNLIKELY(!new_host_entry->failed_connection_a_records)) {
1255 goto setup_host_entry_error;
1256 }
1257
1258 new_host_entry->failed_connection_aaaa_records = aws_cache_new_lru(
1259 new_host_entry->allocator,
1260 aws_hash_string,
1261 aws_hash_callback_string_eq,
1262 NULL,
1263 on_failed_address_entry_removed,
1264 config->max_ttl);
1265 if (AWS_UNLIKELY(!new_host_entry->failed_connection_aaaa_records)) {
1266 goto setup_host_entry_error;
1267 }
1268
1269 if (aws_array_list_init_dynamic(
1270 &new_host_entry->new_addresses, new_host_entry->allocator, 4, sizeof(struct aws_host_address))) {
1271 goto setup_host_entry_error;
1272 }
1273
1274 if (aws_array_list_init_dynamic(
1275 &new_host_entry->expired_addresses, new_host_entry->allocator, 4, sizeof(struct aws_host_address))) {
1276 goto setup_host_entry_error;
1277 }
1278
1279 aws_linked_list_init(&new_host_entry->pending_resolution_callbacks);
1280
1281 pending_callback = aws_mem_acquire(resolver->allocator, sizeof(struct pending_callback));
1282
1283 if (AWS_UNLIKELY(!pending_callback)) {
1284 goto setup_host_entry_error;
1285 }
1286
1287 /*add the current callback here */
1288 pending_callback->user_data = user_data;
1289 pending_callback->callback = res;
1290 aws_linked_list_push_back(&new_host_entry->pending_resolution_callbacks, &pending_callback->node);
1291
1292 aws_mutex_init(&new_host_entry->entry_lock);
1293 new_host_entry->resolution_config = *config;
1294 aws_condition_variable_init(&new_host_entry->entry_signal);
1295
1296 aws_thread_init(&new_host_entry->resolver_thread, resolver->allocator);
1297 thread_init = true;
1298 struct default_host_resolver *default_host_resolver = resolver->impl;
1299 if (AWS_UNLIKELY(
1300 aws_hash_table_put(&default_host_resolver->host_entry_table, host_string_copy, new_host_entry, NULL))) {
1301 goto setup_host_entry_error;
1302 }
1303
1304 struct aws_thread_options thread_options = *aws_default_thread_options();
1305 thread_options.join_strategy = AWS_TJS_MANAGED;
1306 thread_options.name = aws_byte_cursor_from_c_str("AwsHostResolver"); /* 15 characters is max for Linux */
1307 if (aws_thread_launch(
1308 &new_host_entry->resolver_thread, aws_host_resolver_thread, new_host_entry, &thread_options)) {
1309 goto setup_host_entry_error;
1310 }
1311 ++default_host_resolver->pending_host_entry_shutdown_completion_callbacks;
1312
1313 return AWS_OP_SUCCESS;
1314
1315 setup_host_entry_error:
1316
1317 if (thread_init) {
1318 aws_thread_clean_up(&new_host_entry->resolver_thread);
1319 }
1320 // If we registered a callback, clear it. So that we don’t trigger callback and return an error.
1321 if (!aws_linked_list_empty(&new_host_entry->pending_resolution_callbacks)) {
1322 aws_linked_list_remove(&pending_callback->node);
1323 }
1324
1325 s_clean_up_host_entry(new_host_entry);
1326
1327 return AWS_OP_ERR;
1328 }
1329
1330 static int default_resolve_host(
1331 struct aws_host_resolver *resolver,
1332 const struct aws_string *host_name,
1333 aws_on_host_resolved_result_fn *res,
1334 const struct aws_host_resolution_config *config,
1335 void *user_data) {
1336 int result = AWS_OP_SUCCESS;
1337
1338 AWS_LOGF_DEBUG(AWS_LS_IO_DNS, "id=%p: Host resolution requested for %s", (void *)resolver, host_name->bytes);
1339
1340 uint64_t timestamp = s_get_system_time_for_default_resolver(resolver);
1341
1342 struct default_host_resolver *default_host_resolver = resolver->impl;
1343 aws_mutex_lock(&default_host_resolver->resolver_lock);
1344
1345 struct aws_hash_element *element = NULL;
1346 /* we don't care about the error code here, only that the host_entry was found or not. */
1347 aws_hash_table_find(&default_host_resolver->host_entry_table, host_name, &element);
1348
1349 struct host_entry *host_entry = NULL;
1350 if (element != NULL) {
1351 host_entry = element->value;
1352 AWS_FATAL_ASSERT(host_entry != NULL);
1353 }
1354
1355 if (!host_entry) {
1356 AWS_LOGF_DEBUG(
1357 AWS_LS_IO_DNS,
1358 "id=%p: No cached entries found for %s starting new resolver thread.",
1359 (void *)resolver,
1360 host_name->bytes);
1361
1362 result = create_and_init_host_entry(resolver, host_name, res, config, timestamp, user_data);
1363 aws_mutex_unlock(&default_host_resolver->resolver_lock);
1364
1365 return result;
1366 }
1367
1368 aws_mutex_lock(&host_entry->entry_lock);
1369
1370 /*
1371 * We don't need to make any resolver side-affects in the remaining logic and it's impossible for the entry
1372 * to disappear underneath us while holding its lock, so its safe to release the resolver lock and let other
1373 * things query other entries.
1374 */
1375 aws_mutex_unlock(&default_host_resolver->resolver_lock);
1376 host_entry->last_resolve_request_timestamp_ns = timestamp;
1377 host_entry->resolves_since_last_request = 0;
1378
1379 struct aws_host_address_cache_entry *aaaa_entry = aws_lru_cache_use_lru_element(host_entry->aaaa_records);
1380 struct aws_host_address *aaaa_record = (aaaa_entry != NULL) ? &aaaa_entry->address : NULL;
1381 struct aws_host_address_cache_entry *a_entry = aws_lru_cache_use_lru_element(host_entry->a_records);
1382 struct aws_host_address *a_record = (a_entry != NULL) ? &a_entry->address : NULL;
1383
1384 struct aws_host_address address_array[2];
1385 AWS_ZERO_ARRAY(address_array);
1386 struct aws_array_list callback_address_list;
1387 aws_array_list_init_static(&callback_address_list, address_array, 2, sizeof(struct aws_host_address));
1388
1389 if ((aaaa_record || a_record)) {
1390 AWS_LOGF_DEBUG(
1391 AWS_LS_IO_DNS,
1392 "id=%p: cached entries found for %s returning to caller.",
1393 (void *)resolver,
1394 host_name->bytes);
1395
1396 /* these will all need to be copied so that we don't hold the lock during the callback. */
1397 if (aaaa_record) {
1398 struct aws_host_address aaaa_record_cpy;
1399 aws_host_address_copy(aaaa_record, &aaaa_record_cpy);
1400 aws_array_list_push_back(&callback_address_list, &aaaa_record_cpy);
1401 AWS_LOGF_TRACE(
1402 AWS_LS_IO_DNS,
1403 "id=%p: vending address %s for host %s to caller",
1404 (void *)resolver,
1405 aaaa_record->address->bytes,
1406 host_entry->host_name->bytes);
1407 }
1408 if (a_record) {
1409 struct aws_host_address a_record_cpy;
1410 aws_host_address_copy(a_record, &a_record_cpy);
1411 aws_array_list_push_back(&callback_address_list, &a_record_cpy);
1412 AWS_LOGF_TRACE(
1413 AWS_LS_IO_DNS,
1414 "id=%p: vending address %s for host %s to caller",
1415 (void *)resolver,
1416 a_record->address->bytes,
1417 host_entry->host_name->bytes);
1418 }
1419 aws_mutex_unlock(&host_entry->entry_lock);
1420
1421 /* we don't want to do the callback WHILE we hold the lock someone may reentrantly call us. */
1422 // TODO: Fire the callback asynchronously
1423 res(resolver, host_name, AWS_OP_SUCCESS, &callback_address_list, user_data);
1424
1425 for (size_t i = 0; i < aws_array_list_length(&callback_address_list); ++i) {
1426 struct aws_host_address *address_ptr = NULL;
1427 aws_array_list_get_at_ptr(&callback_address_list, (void **)&address_ptr, i);
1428 aws_host_address_clean_up(address_ptr);
1429 }
1430
1431 aws_array_list_clean_up(&callback_address_list);
1432
1433 return result;
1434 }
1435
1436 struct pending_callback *pending_callback =
1437 aws_mem_acquire(default_host_resolver->allocator, sizeof(struct pending_callback));
1438 if (pending_callback != NULL) {
1439 pending_callback->user_data = user_data;
1440 pending_callback->callback = res;
1441 aws_linked_list_push_back(&host_entry->pending_resolution_callbacks, &pending_callback->node);
1442 /*
1443 * intentionally signal under the lock; similar to the shutdown case, we can't guarantee the resolver
1444 * is still around once the lock is released.
1445 */
1446 aws_condition_variable_notify_all(&host_entry->entry_signal);
1447 } else {
1448 result = AWS_OP_ERR;
1449 }
1450
1451 aws_mutex_unlock(&host_entry->entry_lock);
1452
1453 return result;
1454 }
1455
1456 static size_t default_get_host_address_count(
1457 struct aws_host_resolver *host_resolver,
1458 const struct aws_string *host_name,
1459 uint32_t flags) {
1460 struct default_host_resolver *default_host_resolver = host_resolver->impl;
1461 size_t address_count = 0;
1462
1463 aws_mutex_lock(&default_host_resolver->resolver_lock);
1464
1465 struct aws_hash_element *element = NULL;
1466 aws_hash_table_find(&default_host_resolver->host_entry_table, host_name, &element);
1467 if (element != NULL) {
1468 struct host_entry *host_entry = element->value;
1469 if (host_entry != NULL) {
1470 aws_mutex_lock(&host_entry->entry_lock);
1471
1472 if ((flags & AWS_GET_HOST_ADDRESS_COUNT_RECORD_TYPE_A) != 0) {
1473 address_count += aws_cache_get_element_count(host_entry->a_records);
1474 }
1475
1476 if ((flags & AWS_GET_HOST_ADDRESS_COUNT_RECORD_TYPE_AAAA) != 0) {
1477 address_count += aws_cache_get_element_count(host_entry->aaaa_records);
1478 }
1479
1480 aws_mutex_unlock(&host_entry->entry_lock);
1481 }
1482 }
1483
1484 aws_mutex_unlock(&default_host_resolver->resolver_lock);
1485
1486 return address_count;
1487 }
1488
1489 static struct aws_host_resolver_vtable s_vtable = {
1490 .purge_cache = s_resolver_purge_cache,
1491 .purge_cache_with_callback = s_resolver_purge_cache_with_callback,
1492 .resolve_host = default_resolve_host,
1493 .record_connection_failure = resolver_record_connection_failure,
1494 .get_host_address_count = default_get_host_address_count,
1495 .destroy = resolver_destroy,
1496 .purge_host_cache = s_resolver_purge_host_cache,
1497 };
1498
1499 static void s_aws_host_resolver_destroy(void *user_data) {
1500 struct aws_host_resolver *resolver = user_data;
1501 AWS_ASSERT(resolver->vtable && resolver->vtable->destroy);
1502 resolver->vtable->destroy(resolver);
1503 }
1504
1505 struct aws_host_resolver *aws_host_resolver_new_default(
1506 struct aws_allocator *allocator,
1507 const struct aws_host_resolver_default_options *options) {
1508 AWS_FATAL_ASSERT(options != NULL);
1509
1510 AWS_ASSERT(options->el_group);
1511
1512 struct aws_host_resolver *resolver = NULL;
1513 struct default_host_resolver *default_host_resolver = NULL;
1514 if (!aws_mem_acquire_many(
1515 allocator,
1516 2,
1517 &resolver,
1518 sizeof(struct aws_host_resolver),
1519 &default_host_resolver,
1520 sizeof(struct default_host_resolver))) {
1521 return NULL;
1522 }
1523
1524 AWS_ZERO_STRUCT(*resolver);
1525 AWS_ZERO_STRUCT(*default_host_resolver);
1526
1527 AWS_LOGF_INFO(
1528 AWS_LS_IO_DNS,
1529 "id=%p: Initializing default host resolver with %llu max host entries.",
1530 (void *)resolver,
1531 (unsigned long long)options->max_entries);
1532
1533 resolver->vtable = &s_vtable;
1534 resolver->allocator = allocator;
1535 resolver->impl = default_host_resolver;
1536
1537 default_host_resolver->event_loop_group = aws_event_loop_group_acquire(options->el_group);
1538 default_host_resolver->allocator = allocator;
1539 default_host_resolver->pending_host_entry_shutdown_completion_callbacks = 0;
1540 default_host_resolver->state = DRS_ACTIVE;
1541 aws_mutex_init(&default_host_resolver->resolver_lock);
1542
1543 if (aws_hash_table_init(
1544 &default_host_resolver->host_entry_table,
1545 allocator,
1546 options->max_entries,
1547 aws_hash_string,
1548 aws_hash_callback_string_eq,
1549 NULL,
1550 NULL)) {
1551 goto on_error;
1552 }
1553
1554 aws_ref_count_init(&resolver->ref_count, resolver, (aws_simple_completion_callback *)s_aws_host_resolver_destroy);
1555
1556 if (options->shutdown_options != NULL) {
1557 resolver->shutdown_options = *options->shutdown_options;
1558 }
1559
1560 if (options->system_clock_override_fn != NULL) {
1561 default_host_resolver->system_clock_fn = options->system_clock_override_fn;
1562 } else {
1563 default_host_resolver->system_clock_fn = aws_high_res_clock_get_ticks;
1564 }
1565
1566 return resolver;
1567
1568 on_error:
1569
1570 s_cleanup_default_resolver(resolver);
1571
1572 return NULL;
1573 }
1574
1575 struct aws_host_resolver *aws_host_resolver_acquire(struct aws_host_resolver *resolver) {
1576 if (resolver != NULL) {
1577 aws_ref_count_acquire(&resolver->ref_count);
1578 }
1579
1580 return resolver;
1581 }
1582
1583 void aws_host_resolver_release(struct aws_host_resolver *resolver) {
1584 if (resolver != NULL) {
1585 aws_ref_count_release(&resolver->ref_count);
1586 }
1587 }
1588
1589 size_t aws_host_resolver_get_host_address_count(
1590 struct aws_host_resolver *resolver,
1591 const struct aws_string *host_name,
1592 uint32_t flags) {
1593 return resolver->vtable->get_host_address_count(resolver, host_name, flags);
1594 }
1595
1596 struct aws_host_resolution_config aws_host_resolver_init_default_resolution_config(void) {
1597 struct aws_host_resolution_config config = {
1598 .impl = aws_default_dns_resolve,
1599 .max_ttl = AWS_DEFAULT_DNS_TTL,
1600 .impl_data = NULL,
1601 .resolve_frequency_ns = NS_PER_SEC,
1602 };
1603
1604 return config;
1605 }