Skip to content
Snippets Groups Projects
Commit ee565da9 authored by Iryna Antsyferova's avatar Iryna Antsyferova
Browse files

Implement dns cache handlers

parent 9556ab31
No related branches found
No related tags found
1 merge request!180DNS cache support, REF 14614
This commit is part of merge request !180. Comments created here will be created in the context of that merge request.
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment