From ee565da9460a79893be7c030b7a0ec51e8ad6fa2 Mon Sep 17 00:00:00 2001 From: Iryna Antsyferova <iryna.antsyferova@iopsys.eu> Date: Tue, 25 Jun 2024 13:10:38 +0200 Subject: [PATCH] Implement dns cache handlers --- res/res_pjsip/pjsip_resolver.c | 67 +++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/res/res_pjsip/pjsip_resolver.c b/res/res_pjsip/pjsip_resolver.c index 792062087c..1547bed314 100644 --- a/res/res_pjsip/pjsip_resolver.c +++ b/res/res_pjsip/pjsip_resolver.c @@ -68,8 +68,7 @@ struct host_track_entry { struct dns_cache_entry { char *host; pjsip_server_addresses addresses; - int ttl; - time_t entry_time; + int expiration_time; AST_LIST_ENTRY(dns_cache_entry) next; }; @@ -921,6 +920,69 @@ static void sip_check_transport(pj_pool_t *pool, pjsip_transport_type_e transpor } } +static int dns_cache_get_addr(const char *host, pjsip_server_addresses *addresses) +{ + struct dns_cache_entry *cache_entry = NULL; + int ret = -1; + + if (!host || !addresses) + return -1; + + AST_LIST_LOCK(&sip_dns_cache); + + AST_LIST_TRAVERSE_SAFE_BEGIN(&sip_dns_cache, cache_entry, next) { + if (strcmp(host, cache_entry->host) == 0) { + if (cache_entry->expiration_time > time(NULL)) { + ast_debug(5, "cache_entry '%s': record found\n", cache_entry->host); + pj_memcpy(addresses, &cache_entry->addresses, sizeof(*addresses)); + ret = 0; + } + break; + } + } + AST_LIST_TRAVERSE_SAFE_END; + + AST_LIST_UNLOCK(&sip_dns_cache); + + return ret; +} + +static int dns_cache_update(const char *host, pjsip_server_addresses *addresses, int ttl) +{ + struct dns_cache_entry *cache_entry = NULL; + struct dns_cache_entry *new_cache_entry = NULL; + int found = 0; + + if (!host || !addresses) + return -1; + + AST_LIST_LOCK(&sip_dns_cache); + + AST_LIST_TRAVERSE_SAFE_BEGIN(&sip_dns_cache, cache_entry, next) { + if (cache_entry && strcmp(host, cache_entry->host) == 0) { + ast_debug(5, "cache_entry '%s' recond found: updating\n", cache_entry->host); + pj_memcpy(&cache_entry->addresses, addresses, sizeof(*addresses)); + cache_entry->expiration_time = time(NULL) + ttl; + found = 1; + break; + } + } + AST_LIST_TRAVERSE_SAFE_END; + + if (!found) { + new_cache_entry = ast_calloc(1, sizeof(*new_cache_entry)); + new_cache_entry->host = strdup(host); + pj_memcpy(&new_cache_entry->addresses, addresses, sizeof(*addresses)); + new_cache_entry->expiration_time = time(NULL) + ttl; + AST_LIST_INSERT_HEAD(&sip_dns_cache, new_cache_entry, next); + ast_debug(5, "cache_entry '%s': record added\n", new_cache_entry->host); + } + + AST_LIST_UNLOCK(&sip_dns_cache); + + return 0; +} + /* External API to set DNS cache function pointers */ void pjsip_resolver_set_dns_cache(dns_cache_get_addr_t get_addr, dns_cache_update_t update_cache) { @@ -978,6 +1040,7 @@ void ast_sip_initialize_resolver(void) { /* Replace the existing PJSIP resolver with our own implementation */ ast_sip_push_task_wait_servant(NULL, sip_replace_resolver, NULL); + pjsip_resolver_set_dns_cache(dns_cache_get_addr, dns_cache_update); } #else -- GitLab