asterisk/main/dns_query_set.c

267 lines
7.4 KiB
C
Raw Normal View History

/*
* 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"
git migration: Refactor the ASTERISK_FILE_VERSION macro Git does not support the ability to replace a token with a version string during check-in. While it does have support for replacing a token on clone, this is somewhat sub-optimal: the token is replaced with the object hash, which is not particularly easy for human consumption. What's more, in practice, the source file version was often not terribly useful. Generally, when triaging bugs, the overall version of Asterisk is far more useful than an individual SVN version of a file. As a result, this patch removes Asterisk's support for showing source file versions. Specifically, it does the following: * Rename ASTERISK_FILE_VERSION macro to ASTERISK_REGISTER_FILE, and remove passing the version in with the macro. Other facilities than 'core show file version' make use of the file names, such as setting a debug level only on a specific file. As such, the act of registering source files with the Asterisk core still has use. The macro rename now reflects the new macro purpose. * main/asterisk: - Refactor the file_version structure to reflect that it no longer tracks a version field. - Remove the "core show file version" CLI command. Without the file version, it is no longer useful. - Remove the ast_file_version_find function. The file version is no longer tracked. - Rename ast_register_file_version/ast_unregister_file_version to ast_register_file/ast_unregister_file, respectively. * main/manager: Remove value from the Version key of the ModuleCheck Action. The actual key itself has not been removed, as doing so would absolutely constitute a backwards incompatible change. However, since the file version is no longer tracked, there is no need to attempt to include it in the Version key. * UPGRADE: Add notes for: - Modification to the ModuleCheck AMI Action - Removal of the "core show file version" CLI command Change-Id: I6cf0ff280e1668bf4957dc21f32a5ff43444a40e
2015-04-12 02:38:22 +00:00
ASTERISK_REGISTER_FILE()
#include "asterisk/vector.h"
#include "asterisk/astobj2.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
#include "asterisk/dns_core.h"
#include "asterisk/dns_query_set.h"
#include "asterisk/dns_internal.h"
#include "asterisk/dns_resolver.h"
/*! \brief The default number of expected queries to be added to the query set */
#define DNS_QUERY_SET_EXPECTED_QUERY_COUNT 5
/*! \brief Destructor for DNS query set */
static void dns_query_set_destroy(void *data)
{
struct ast_dns_query_set *query_set = data;
int idx;
for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
ao2_ref(query->query, -1);
}
AST_VECTOR_FREE(&query_set->queries);
ao2_cleanup(query_set->user_data);
}
struct ast_dns_query_set *ast_dns_query_set_create(void)
{
struct ast_dns_query_set *query_set;
query_set = ao2_alloc_options(sizeof(*query_set), dns_query_set_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!query_set) {
return NULL;
}
if (AST_VECTOR_INIT(&query_set->queries, DNS_QUERY_SET_EXPECTED_QUERY_COUNT)) {
ao2_ref(query_set, -1);
return NULL;
}
return query_set;
}
/*! \brief Callback invoked upon completion of a DNS query */
static void dns_query_set_callback(const struct ast_dns_query *query)
{
struct ast_dns_query_set *query_set = ast_dns_query_get_data(query);
/* The reference count of the query set is bumped here in case this query holds the last reference */
ao2_ref(query_set, +1);
/* Drop the query set from the query so the query set can be destroyed if this is the last one */
ao2_cleanup(((struct ast_dns_query *)query)->user_data);
((struct ast_dns_query *)query)->user_data = NULL;
if (ast_atomic_fetchadd_int(&query_set->queries_completed, +1) != (AST_VECTOR_SIZE(&query_set->queries) - 1)) {
ao2_ref(query_set, -1);
return;
}
/* All queries have been completed, invoke final callback */
if (query_set->queries_cancelled != AST_VECTOR_SIZE(&query_set->queries)) {
query_set->callback(query_set);
}
ao2_cleanup(query_set->user_data);
query_set->user_data = NULL;
ao2_ref(query_set, -1);
}
int ast_dns_query_set_add(struct ast_dns_query_set *query_set, const char *name, int rr_type, int rr_class)
{
struct dns_query_set_query query = {
.started = 0,
};
ast_assert(!query_set->in_progress);
if (query_set->in_progress) {
ast_log(LOG_ERROR, "Attempted to add additional query to query set '%p' after resolution has started\n",
query_set);
return -1;
}
/*
* We are intentionally passing NULL for the user data even
* though dns_query_set_callback() is not NULL tolerant. Doing
* this avoids a circular reference chain until the queries are
* started. ast_dns_query_set_resolve_async() will set the
* query user_data for us later when we actually kick off the
* queries.
*/
query.query = dns_query_alloc(name, rr_type, rr_class, dns_query_set_callback, NULL);
if (!query.query) {
return -1;
}
AST_VECTOR_APPEND(&query_set->queries, query);
return 0;
}
size_t ast_dns_query_set_num_queries(const struct ast_dns_query_set *query_set)
{
return AST_VECTOR_SIZE(&query_set->queries);
}
struct ast_dns_query *ast_dns_query_set_get(const struct ast_dns_query_set *query_set, unsigned int index)
{
/* Only once all queries have been completed can results be retrieved */
if (query_set->queries_completed != AST_VECTOR_SIZE(&query_set->queries)) {
return NULL;
}
/* If the index exceeds the number of queries... no query for you */
if (index >= AST_VECTOR_SIZE(&query_set->queries)) {
return NULL;
}
return AST_VECTOR_GET_ADDR(&query_set->queries, index)->query;
}
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)
{
int idx;
ast_assert(!query_set->in_progress);
if (query_set->in_progress) {
ast_log(LOG_ERROR, "Attempted to start asynchronous resolution of query set '%p' when it has already started\n",
query_set);
return;
}
query_set->in_progress = 1;
query_set->callback = callback;
query_set->user_data = ao2_bump(data);
for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
query->query->user_data = ao2_bump(query_set);
if (!query->query->resolver->resolve(query->query)) {
query->started = 1;
continue;
}
dns_query_set_callback(query->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 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);
}
/*! \brief Callback used to implement synchronous resolution */
static void dns_synchronous_resolve_callback(const struct ast_dns_query_set *query_set)
{
struct dns_synchronous_resolve *synchronous = ast_dns_query_set_get_data(query_set);
ast_mutex_lock(&synchronous->lock);
synchronous->completed = 1;
ast_cond_signal(&synchronous->cond);
ast_mutex_unlock(&synchronous->lock);
}
int ast_query_set_resolve(struct ast_dns_query_set *query_set)
{
struct dns_synchronous_resolve *synchronous;
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);
ast_dns_query_set_resolve_async(query_set, dns_synchronous_resolve_callback, synchronous);
/* 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(synchronous, -1);
return 0;
}
int ast_dns_query_set_resolve_cancel(struct ast_dns_query_set *query_set)
{
int idx;
size_t query_count = AST_VECTOR_SIZE(&query_set->queries);
for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
if (query->started) {
if (!query->query->resolver->cancel(query->query)) {
query_set->queries_cancelled++;
dns_query_set_callback(query->query);
}
} else {
query_set->queries_cancelled++;
}
}
return (query_set->queries_cancelled == query_count) ? 0 : -1;
}