diff options
author | Joshua Colp <jcolp@digium.com> | 2015-03-25 12:32:26 +0000 |
---|---|---|
committer | Joshua Colp <jcolp@digium.com> | 2015-03-25 12:32:26 +0000 |
commit | abf3e40902abe9a3b32aba0d1691b209b4d32e66 (patch) | |
tree | da9c79e72fc4e06ce512f7aa678d6d5ea88afb80 /main | |
parent | 4c2fc5b81103f7942277bf3dd6481742500ddb2d (diff) |
dns: Add core DNS API + unit tests and res_resolver_unbound module + unit tests.
This change adds an abstracted core DNS API which resembles the API described
here[1]. The API provides a pluggable mechanism for resolvers and also a
consistent view for records. Both synchronous and asynchronous queries are
supported.
This change also adds a res_resolver_unbound module which uses the libunbound
library to provide resolution.
Unit tests have also been written for all of the above to confirm the API and
functionality.
ASTERISK-24834 #close
Reported by: Matt Jordan
ASTERISK-24836 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/4474/
Review: https://reviewboard.asterisk.org/r/4512/
[1] https://wiki.asterisk.org/wiki/display/AST/Asterisk+DNS+API
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@433370 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main')
-rw-r--r-- | main/dns_core.c | 566 | ||||
-rw-r--r-- | main/dns_naptr.c | 65 | ||||
-rw-r--r-- | main/dns_query_set.c | 93 | ||||
-rw-r--r-- | main/dns_recurring.c | 149 | ||||
-rw-r--r-- | main/dns_srv.c | 55 | ||||
-rw-r--r-- | main/dns_tlsa.c | 55 |
6 files changed, 983 insertions, 0 deletions
diff --git a/main/dns_core.c b/main/dns_core.c new file mode 100644 index 000000000..394eaa514 --- /dev/null +++ b/main/dns_core.c @@ -0,0 +1,566 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2015, Digium, Inc. + * + * Joshua Colp <jcolp@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Core DNS Functionality + * + * \author Joshua Colp <jcolp@digium.com> + */ + +/*** MODULEINFO + <support_level>core</support_level> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/linkedlists.h" +#include "asterisk/vector.h" +#include "asterisk/astobj2.h" +#include "asterisk/strings.h" +#include "asterisk/sched.h" +#include "asterisk/dns_core.h" +#include "asterisk/dns_srv.h" +#include "asterisk/dns_tlsa.h" +#include "asterisk/dns_recurring.h" +#include "asterisk/dns_resolver.h" +#include "asterisk/dns_internal.h" + +#include <arpa/nameser.h> + +AST_RWLIST_HEAD_STATIC(resolvers, ast_dns_resolver); + +static struct ast_sched_context *sched; + +struct ast_sched_context *ast_dns_get_sched(void) +{ + return sched; +} + +const char *ast_dns_query_get_name(const struct ast_dns_query *query) +{ + return query->name; +} + +int ast_dns_query_get_rr_type(const struct ast_dns_query *query) +{ + return query->rr_type; +} + +int ast_dns_query_get_rr_class(const struct ast_dns_query *query) +{ + return query->rr_class; +} + +void *ast_dns_query_get_data(const struct ast_dns_query *query) +{ + return query->user_data; +} + +struct ast_dns_result *ast_dns_query_get_result(const struct ast_dns_query *query) +{ + return query->result; +} + +unsigned int ast_dns_result_get_secure(const struct ast_dns_result *result) +{ + return result->secure; +} + +unsigned int ast_dns_result_get_bogus(const struct ast_dns_result *result) +{ + return result->bogus; +} + +unsigned int ast_dns_result_get_rcode(const struct ast_dns_result *result) +{ + return result->rcode; +} + +const char *ast_dns_result_get_canonical(const struct ast_dns_result *result) +{ + return result->canonical; +} + +const struct ast_dns_record *ast_dns_result_get_records(const struct ast_dns_result *result) +{ + return AST_LIST_FIRST(&result->records); +} + +const char *ast_dns_result_get_answer(const struct ast_dns_result *result) +{ + return result->answer; +} + +int ast_dns_result_get_lowest_ttl(const struct ast_dns_result *result) +{ + int ttl = 0; + const struct ast_dns_record *record; + + if (ast_dns_result_get_rcode(result) == ns_r_nxdomain) { + return 0; + } + + for (record = ast_dns_result_get_records(result); record; record = ast_dns_record_get_next(record)) { + if (!ttl || (ast_dns_record_get_ttl(record) && (ast_dns_record_get_ttl(record) < ttl))) { + ttl = ast_dns_record_get_ttl(record); + } + } + + return ttl; +} + +void ast_dns_result_free(struct ast_dns_result *result) +{ + struct ast_dns_record *record; + + if (!result) { + return; + } + + while ((record = AST_LIST_REMOVE_HEAD(&result->records, list))) { + ast_free(record); + } + + ast_free(result); +} + +int ast_dns_record_get_rr_type(const struct ast_dns_record *record) +{ + return record->rr_type; +} + +int ast_dns_record_get_rr_class(const struct ast_dns_record *record) +{ + return record->rr_class; +} + +int ast_dns_record_get_ttl(const struct ast_dns_record *record) +{ + return record->ttl; +} + +const char *ast_dns_record_get_data(const struct ast_dns_record *record) +{ + return record->data; +} + +const struct ast_dns_record *ast_dns_record_get_next(const struct ast_dns_record *record) +{ + return AST_LIST_NEXT(record, list); +} + +/*! \brief Destructor for an active DNS query */ +static void dns_query_active_destroy(void *data) +{ + struct ast_dns_query_active *active = data; + + ao2_cleanup(active->query); +} + +/*! \brief \brief Destructor for a DNS query */ +static void dns_query_destroy(void *data) +{ + struct ast_dns_query *query = data; + + ao2_cleanup(query->user_data); + ao2_cleanup(query->resolver_data); + ast_dns_result_free(query->result); +} + +struct ast_dns_query_active *ast_dns_resolve_async(const char *name, int rr_type, int rr_class, ast_dns_resolve_callback callback, void *data) +{ + struct ast_dns_query_active *active; + + if (ast_strlen_zero(name)) { + ast_log(LOG_WARNING, "Could not perform asynchronous resolution, no name provided\n"); + return NULL; + } else if (rr_type > ns_t_max) { + ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', resource record type '%d' exceeds maximum\n", + name, rr_type); + return NULL; + } else if (rr_type < 0) { + ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', invalid resource record type '%d'\n", + name, rr_type); + return NULL; + } else if (rr_class > ns_c_max) { + ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', resource record class '%d' exceeds maximum\n", + name, rr_class); + return NULL; + } else if (rr_class < 0) { + ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', invalid resource class '%d'\n", + name, rr_class); + return NULL; + } else if (!callback) { + ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', no callback provided\n", + name); + return NULL; + } + + active = ao2_alloc_options(sizeof(*active), dns_query_active_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK); + if (!active) { + return NULL; + } + + active->query = ao2_alloc_options(sizeof(*active->query) + strlen(name) + 1, dns_query_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK); + if (!active->query) { + ao2_ref(active, -1); + return NULL; + } + + active->query->callback = callback; + active->query->user_data = ao2_bump(data); + active->query->rr_type = rr_type; + active->query->rr_class = rr_class; + strcpy(active->query->name, name); /* SAFE */ + + AST_RWLIST_RDLOCK(&resolvers); + active->query->resolver = AST_RWLIST_FIRST(&resolvers); + AST_RWLIST_UNLOCK(&resolvers); + + if (!active->query->resolver) { + ast_log(LOG_ERROR, "Attempted to do a DNS query for '%s' of class '%d' and type '%d' but no resolver is available\n", + name, rr_class, rr_type); + ao2_ref(active, -1); + return NULL; + } + + if (active->query->resolver->resolve(active->query)) { + ast_log(LOG_ERROR, "Resolver '%s' returned an error when resolving '%s' of class '%d' and type '%d'\n", + active->query->resolver->name, name, rr_class, rr_type); + ao2_ref(active, -1); + return NULL; + } + + return active; +} + +int ast_dns_resolve_cancel(struct ast_dns_query_active *active) +{ + return active->query->resolver->cancel(active->query); +} + +/*! \brief Structure used for signaling back for synchronous resolution completion */ +struct dns_synchronous_resolve { + /*! \brief Lock used for signaling */ + ast_mutex_t lock; + /*! \brief Condition used for signaling */ + ast_cond_t cond; + /*! \brief Whether the query has completed */ + unsigned int completed; + /*! \brief The result from the query */ + struct ast_dns_result *result; +}; + +/*! \brief Destructor for synchronous resolution structure */ +static void dns_synchronous_resolve_destroy(void *data) +{ + struct dns_synchronous_resolve *synchronous = data; + + ast_mutex_destroy(&synchronous->lock); + ast_cond_destroy(&synchronous->cond); + + /* This purposely does not unref result as it has been passed to the caller */ +} + +/*! \brief Callback used to implement synchronous resolution */ +static void dns_synchronous_resolve_callback(const struct ast_dns_query *query) +{ + struct dns_synchronous_resolve *synchronous = ast_dns_query_get_data(query); + + synchronous->result = query->result; + ((struct ast_dns_query *)query)->result = NULL; + + ast_mutex_lock(&synchronous->lock); + synchronous->completed = 1; + ast_cond_signal(&synchronous->cond); + ast_mutex_unlock(&synchronous->lock); +} + +int ast_dns_resolve(const char *name, int rr_type, int rr_class, struct ast_dns_result **result) +{ + struct dns_synchronous_resolve *synchronous; + struct ast_dns_query_active *active; + + if (ast_strlen_zero(name)) { + ast_log(LOG_WARNING, "Could not perform synchronous resolution, no name provided\n"); + return -1; + } else if (rr_type > ns_t_max) { + ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', resource record type '%d' exceeds maximum\n", + name, rr_type); + return -1; + } else if (rr_type < 0) { + ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', invalid resource record type '%d'\n", + name, rr_type); + return -1; + } else if (rr_class > ns_c_max) { + ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', resource record class '%d' exceeds maximum\n", + name, rr_class); + return -1; + } else if (rr_class < 0) { + ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', invalid resource class '%d'\n", + name, rr_class); + return -1; + } else if (!result) { + ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', no result pointer provided for storing results\n", + name); + return -1; + } + + synchronous = ao2_alloc_options(sizeof(*synchronous), dns_synchronous_resolve_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK); + if (!synchronous) { + return -1; + } + + ast_mutex_init(&synchronous->lock); + ast_cond_init(&synchronous->cond, NULL); + + active = ast_dns_resolve_async(name, rr_type, rr_class, dns_synchronous_resolve_callback, synchronous); + if (active) { + /* Wait for resolution to complete */ + ast_mutex_lock(&synchronous->lock); + while (!synchronous->completed) { + ast_cond_wait(&synchronous->cond, &synchronous->lock); + } + ast_mutex_unlock(&synchronous->lock); + ao2_ref(active, -1); + } + + *result = synchronous->result; + ao2_ref(synchronous, -1); + + return *result ? 0 : -1; +} + +int ast_dns_resolver_set_data(struct ast_dns_query *query, void *data) +{ + if (query->resolver_data) { + return -1; + } + + query->resolver_data = ao2_bump(data); + + return 0; +} + +void *ast_dns_resolver_get_data(const struct ast_dns_query *query) +{ + return query->resolver_data; +} + +int ast_dns_resolver_set_result(struct ast_dns_query *query, unsigned int secure, unsigned int bogus, + unsigned int rcode, const char *canonical, const char *answer, size_t answer_size) +{ + char *buf_ptr; + + if (secure && bogus) { + ast_debug(2, "Query '%p': Could not set result information, it can not be both secure and bogus\n", + query); + return -1; + } + + if (ast_strlen_zero(canonical)) { + ast_debug(2, "Query '%p': Could not set result information since no canonical name was provided\n", + query); + return -1; + } + + if (!answer || answer_size == 0) { + ast_debug(2, "Query '%p': Could not set result information since no DNS answer was provided\n", + query); + return -1; + } + + ast_dns_result_free(query->result); + + query->result = ast_calloc(1, sizeof(*query->result) + strlen(canonical) + 1 + answer_size); + if (!query->result) { + return -1; + } + + query->result->secure = secure; + query->result->bogus = bogus; + query->result->rcode = rcode; + + buf_ptr = query->result->buf; + strcpy(buf_ptr, canonical); /* SAFE */ + query->result->canonical = buf_ptr; + + buf_ptr += strlen(canonical) + 1; + memcpy(buf_ptr, answer, answer_size); /* SAFE */ + query->result->answer = buf_ptr; + + return 0; +} + +int ast_dns_resolver_add_record(struct ast_dns_query *query, int rr_type, int rr_class, int ttl, const char *data, const size_t size) +{ + struct ast_dns_record *record; + + if (rr_type < 0) { + ast_debug(2, "Query '%p': Could not add record, invalid resource record type '%d'\n", + query, rr_type); + return -1; + } else if (rr_type > ns_t_max) { + ast_debug(2, "Query '%p': Could not add record, resource record type '%d' exceeds maximum\n", + query, rr_type); + return -1; + } else if (rr_class < 0) { + ast_debug(2, "Query '%p': Could not add record, invalid resource record class '%d'\n", + query, rr_class); + return -1; + } else if (rr_class > ns_c_max) { + ast_debug(2, "Query '%p': Could not add record, resource record class '%d' exceeds maximum\n", + query, rr_class); + return -1; + } else if (ttl < 0) { + ast_debug(2, "Query '%p': Could not add record, invalid TTL '%d'\n", + query, ttl); + return -1; + } else if (!data || !size) { + ast_debug(2, "Query '%p': Could not add record, no data specified\n", + query); + return -1; + } else if (!query->result) { + ast_debug(2, "Query '%p': No result was set on the query, thus records can not be added\n", + query); + return -1; + } + + record = ast_calloc(1, sizeof(*record) + size); + if (!record) { + return -1; + } + + record->rr_type = rr_type; + record->rr_class = rr_class; + record->ttl = ttl; + memcpy(record->data, data, size); + record->data_len = size; + + AST_LIST_INSERT_TAIL(&query->result->records, record, list); + + return 0; +} + +void ast_dns_resolver_completed(struct ast_dns_query *query) +{ + query->callback(query); +} + +static void dns_shutdown(void) +{ + if (sched) { + ast_sched_context_destroy(sched); + sched = NULL; + } +} + +int ast_dns_resolver_register(struct ast_dns_resolver *resolver) +{ + struct ast_dns_resolver *iter; + int inserted = 0; + + if (!resolver) { + return -1; + } else if (ast_strlen_zero(resolver->name)) { + ast_log(LOG_ERROR, "Registration of DNS resolver failed as it does not have a name\n"); + return -1; + } else if (!resolver->resolve) { + ast_log(LOG_ERROR, "DNS resolver '%s' does not implement the resolve callback which is required\n", + resolver->name); + return -1; + } else if (!resolver->cancel) { + ast_log(LOG_ERROR, "DNS resolver '%s' does not implement the cancel callback which is required\n", + resolver->name); + return -1; + } + + AST_RWLIST_WRLOCK(&resolvers); + + /* On the first registration of a resolver start a scheduler for recurring queries */ + if (AST_LIST_EMPTY(&resolvers) && !sched) { + sched = ast_sched_context_create(); + if (!sched) { + ast_log(LOG_ERROR, "DNS resolver '%s' could not be registered: Failed to create scheduler for recurring DNS queries\n", + resolver->name); + AST_RWLIST_UNLOCK(&resolvers); + return -1; + } + + if (ast_sched_start_thread(sched)) { + ast_log(LOG_ERROR, "DNS resolver '%s' could not be registered: Failed to start thread for recurring DNS queries\n", + resolver->name); + dns_shutdown(); + AST_RWLIST_UNLOCK(&resolvers); + return -1; + } + + ast_register_cleanup(dns_shutdown); + } + + AST_LIST_TRAVERSE(&resolvers, iter, next) { + if (!strcmp(iter->name, resolver->name)) { + ast_log(LOG_ERROR, "A DNS resolver with the name '%s' is already registered\n", resolver->name); + AST_RWLIST_UNLOCK(&resolvers); + return -1; + } + } + + AST_RWLIST_TRAVERSE_SAFE_BEGIN(&resolvers, iter, next) { + if (iter->priority > resolver->priority) { + AST_RWLIST_INSERT_BEFORE_CURRENT(resolver, next); + inserted = 1; + break; + } + } + AST_RWLIST_TRAVERSE_SAFE_END; + + if (!inserted) { + AST_RWLIST_INSERT_TAIL(&resolvers, resolver, next); + } + + AST_RWLIST_UNLOCK(&resolvers); + + ast_verb(2, "Registered DNS resolver '%s' with priority '%d'\n", resolver->name, resolver->priority); + + return 0; +} + +void ast_dns_resolver_unregister(struct ast_dns_resolver *resolver) +{ + struct ast_dns_resolver *iter; + + if (!resolver) { + return; + } + + AST_RWLIST_WRLOCK(&resolvers); + AST_RWLIST_TRAVERSE_SAFE_BEGIN(&resolvers, iter, next) { + if (resolver == iter) { + AST_RWLIST_REMOVE_CURRENT(next); + break; + } + } + AST_RWLIST_TRAVERSE_SAFE_END; + AST_RWLIST_UNLOCK(&resolvers); + + ast_verb(2, "Unregistered DNS resolver '%s'\n", resolver->name); +} diff --git a/main/dns_naptr.c b/main/dns_naptr.c new file mode 100644 index 000000000..f4facddae --- /dev/null +++ b/main/dns_naptr.c @@ -0,0 +1,65 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2015, Digium, Inc. + * + * Joshua Colp <jcolp@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief DNS NAPTR Record Support + * + * \author Joshua Colp <jcolp@digium.com> + */ + +/*** MODULEINFO + <support_level>core</support_level> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/dns_core.h" +#include "asterisk/dns_naptr.h" + +const char *ast_dns_naptr_get_flags(const struct ast_dns_record *record) +{ + return NULL; +} + +const char *ast_dns_naptr_get_service(const struct ast_dns_record *record) +{ + return NULL; +} + +const char *ast_dns_naptr_get_regexp(const struct ast_dns_record *record) +{ + return NULL; +} + +const char *ast_dns_naptr_get_replacement(const struct ast_dns_record *record) +{ + return NULL; +} + +unsigned short ast_dns_naptr_get_order(const struct ast_dns_record *record) +{ + return 0; +} + +unsigned short ast_dns_naptr_get_preference(const struct ast_dns_record *record) +{ + return 0; +}
\ No newline at end of file diff --git a/main/dns_query_set.c b/main/dns_query_set.c new file mode 100644 index 000000000..45626d1b9 --- /dev/null +++ b/main/dns_query_set.c @@ -0,0 +1,93 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2015, Digium, Inc. + * + * Joshua Colp <jcolp@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief DNS Query Set API + * + * \author Joshua Colp <jcolp@digium.com> + */ + +/*** MODULEINFO + <support_level>core</support_level> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/vector.h" +#include "asterisk/astobj2.h" +#include "asterisk/dns_core.h" +#include "asterisk/dns_query_set.h" + +/*! \brief A set of DNS queries */ +struct ast_dns_query_set { + /*! \brief DNS queries */ + AST_VECTOR(, struct ast_dns_query *) queries; + /*! \brief The total number of completed queries */ + unsigned int queries_completed; + /*! \brief Callback to invoke upon completion */ + ast_dns_query_set_callback callback; + /*! \brief User-specific data */ + void *user_data; +}; + +struct ast_dns_query_set *ast_dns_query_set_create(void) +{ + return NULL; +} + +int ast_dns_query_set_add(struct ast_dns_query_set *query_set, const char *name, int rr_type, int rr_class) +{ + return -1; +} + +size_t ast_dns_query_set_num_queries(const struct ast_dns_query_set *query_set) +{ + return 0; +} + +struct ast_dns_query *ast_dns_query_set_get(const struct ast_dns_query_set *query_set, unsigned int index) +{ + return NULL; +} + +void *ast_dns_query_set_get_data(const struct ast_dns_query_set *query_set) +{ + return query_set->user_data; +} + +void ast_dns_query_set_resolve_async(struct ast_dns_query_set *query_set, ast_dns_query_set_callback callback, void *data) +{ + query_set->callback = callback; + query_set->user_data = ao2_bump(data); +} + +void ast_query_set_resolve(struct ast_dns_query_set *query_set) +{ +} + +int ast_dns_query_set_resolve_cancel(struct ast_dns_query_set *query_set) +{ + return -1; +} + +void ast_dns_query_set_free(struct ast_dns_query_set *query_set) +{ +} diff --git a/main/dns_recurring.c b/main/dns_recurring.c new file mode 100644 index 000000000..3ebbab070 --- /dev/null +++ b/main/dns_recurring.c @@ -0,0 +1,149 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2015, Digium, Inc. + * + * Joshua Colp <jcolp@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief DNS Recurring Query Support + * + * \author Joshua Colp <jcolp@digium.com> + */ + +/*** MODULEINFO + <support_level>core</support_level> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/astobj2.h" +#include "asterisk/linkedlists.h" +#include "asterisk/sched.h" +#include "asterisk/strings.h" +#include "asterisk/dns_core.h" +#include "asterisk/dns_recurring.h" +#include "asterisk/dns_internal.h" + +#include <arpa/nameser.h> + +/*! \brief Destructor for a DNS query */ +static void dns_query_recurring_destroy(void *data) +{ + struct ast_dns_query_recurring *recurring = data; + + ao2_cleanup(recurring->user_data); +} + +static void dns_query_recurring_resolution_callback(const struct ast_dns_query *query); + +/*! \brief Scheduled recurring query callback */ +static int dns_query_recurring_scheduled_callback(const void *data) +{ + struct ast_dns_query_recurring *recurring = (struct ast_dns_query_recurring *)data; + + ao2_lock(recurring); + recurring->timer = -1; + if (!recurring->cancelled) { + recurring->active = ast_dns_resolve_async(recurring->name, recurring->rr_type, recurring->rr_class, dns_query_recurring_resolution_callback, + recurring); + } + ao2_unlock(recurring); + + ao2_ref(recurring, -1); + + return 0; +} + +/*! \brief Query resolution callback */ +static void dns_query_recurring_resolution_callback(const struct ast_dns_query *query) +{ + struct ast_dns_query_recurring *recurring = ast_dns_query_get_data(query); + + /* Replace the user data so the actual callback sees what it provided */ + ((struct ast_dns_query*)query)->user_data = ao2_bump(recurring->user_data); + recurring->callback(query); + + ao2_lock(recurring); + /* So.. if something has not externally cancelled this we can reschedule based on the TTL */ + if (!recurring->cancelled) { + const struct ast_dns_result *result = ast_dns_query_get_result(query); + int ttl = MIN(ast_dns_result_get_lowest_ttl(result), INT_MAX / 1000); + + if (ttl) { + recurring->timer = ast_sched_add(ast_dns_get_sched(), ttl * 1000, dns_query_recurring_scheduled_callback, ao2_bump(recurring)); + if (recurring->timer < 0) { + /* It is impossible for this to be the last reference as this callback function holds a reference itself */ + ao2_ref(recurring, -1); + } + } + } + + ao2_replace(recurring->active, NULL); + ao2_unlock(recurring); + + /* Since we stole the reference from the query we need to drop it ourselves */ + ao2_ref(recurring, -1); +} + +struct ast_dns_query_recurring *ast_dns_resolve_recurring(const char *name, int rr_type, int rr_class, ast_dns_resolve_callback callback, void *data) +{ + struct ast_dns_query_recurring *recurring; + + if (ast_strlen_zero(name) || !callback || !ast_dns_get_sched()) { + return NULL; + } + + recurring = ao2_alloc(sizeof(*recurring) + strlen(name) + 1, dns_query_recurring_destroy); + if (!recurring) { + return NULL; + } + + recurring->callback = callback; + recurring->user_data = ao2_bump(data); + recurring->timer = -1; + recurring->rr_type = rr_type; + recurring->rr_class = rr_class; + strcpy(recurring->name, name); /* SAFE */ + + recurring->active = ast_dns_resolve_async(name, rr_type, rr_class, dns_query_recurring_resolution_callback, recurring); + if (!recurring->active) { + ao2_ref(recurring, -1); + return NULL; + } + + return recurring; +} + +int ast_dns_resolve_recurring_cancel(struct ast_dns_query_recurring *recurring) +{ + int res = 0; + + ao2_lock(recurring); + + recurring->cancelled = 1; + AST_SCHED_DEL_UNREF(ast_dns_get_sched(), recurring->timer, ao2_ref(recurring, -1)); + + if (recurring->active) { + res = ast_dns_resolve_cancel(recurring->active); + ao2_replace(recurring->active, NULL); + } + + ao2_unlock(recurring); + + return res; +} diff --git a/main/dns_srv.c b/main/dns_srv.c new file mode 100644 index 000000000..eeba9a679 --- /dev/null +++ b/main/dns_srv.c @@ -0,0 +1,55 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2015, Digium, Inc. + * + * Joshua Colp <jcolp@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief DNS SRV Record Support + * + * \author Joshua Colp <jcolp@digium.com> + */ + +/*** MODULEINFO + <support_level>core</support_level> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/dns_core.h" +#include "asterisk/dns_srv.h" + +const char *ast_dns_srv_get_host(const struct ast_dns_record *record) +{ + return NULL; +} + +unsigned short ast_dns_srv_get_priority(const struct ast_dns_record *record) +{ + return 0; +} + +unsigned short ast_dns_srv_get_weight(const struct ast_dns_record *record) +{ + return 0; +} + +unsigned short ast_dns_srv_get_port(const struct ast_dns_record *record) +{ + return 0; +}
\ No newline at end of file diff --git a/main/dns_tlsa.c b/main/dns_tlsa.c new file mode 100644 index 000000000..aa6f5308f --- /dev/null +++ b/main/dns_tlsa.c @@ -0,0 +1,55 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2015, Digium, Inc. + * + * Joshua Colp <jcolp@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief DNS TLSA Record Support + * + * \author Joshua Colp <jcolp@digium.com> + */ + +/*** MODULEINFO + <support_level>core</support_level> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/dns_core.h" +#include "asterisk/dns_tlsa.h" + +unsigned int ast_dns_tlsa_get_usage(const struct ast_dns_record *record) +{ + return 0; +} + +unsigned int ast_dns_tlsa_get_selector(const struct ast_dns_record *record) +{ + return 0; +} + +unsigned int ast_dns_tlsa_get_matching_type(const struct ast_dns_record *record) +{ + return 0; +} + +const char *ast_dns_tlsa_get_association_data(const struct ast_dns_record *record) +{ + return NULL; +}
\ No newline at end of file |