[asterisk-commits] file: branch group/dns r432516 - in /team/group/dns: include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Mar 6 09:02:57 CST 2015


Author: file
Date: Fri Mar  6 09:02:54 2015
New Revision: 432516

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=432516
Log:
Add a recurring query API.

Added:
    team/group/dns/include/asterisk/dns_recurring.h   (with props)
Modified:
    team/group/dns/include/asterisk/dns_internal.h
    team/group/dns/main/dns_core.c

Modified: team/group/dns/include/asterisk/dns_internal.h
URL: http://svnview.digium.com/svn/asterisk/team/group/dns/include/asterisk/dns_internal.h?view=diff&rev=432516&r1=432515&r2=432516
==============================================================================
--- team/group/dns/include/asterisk/dns_internal.h (original)
+++ team/group/dns/include/asterisk/dns_internal.h Fri Mar  6 09:02:54 2015
@@ -107,3 +107,23 @@
 	char name[0];
 };
 
+/*! \brief A recurring DNS query */
+struct ast_dns_query_recurring
+{
+	/*! \brief Callback to invoke upon completion */
+	ast_dns_resolve_callback callback;
+	/*! \brief User-specific data */
+	void *user_data;
+	/*! \brief Current active query */
+	struct ast_dns_query *query;
+	/*! \brief The recurring query has been cancelled */
+	unsigned int cancelled;
+	/*! \brief Scheduled timer for next resolution */
+	int timer;
+	/*! \brief Resource record type */
+	int rr_type;
+	/*! \brief Resource record class */
+	int rr_class;
+	/*! \brief The name of what is being resolved */
+	char name[0];	
+};

Added: team/group/dns/include/asterisk/dns_recurring.h
URL: http://svnview.digium.com/svn/asterisk/team/group/dns/include/asterisk/dns_recurring.h?view=auto&rev=432516
==============================================================================
--- team/group/dns/include/asterisk/dns_recurring.h (added)
+++ team/group/dns/include/asterisk/dns_recurring.h Fri Mar  6 09:02:54 2015
@@ -1,0 +1,70 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2015, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at 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 Resolution API
+ * \author Joshua Colp <jcolp at digium.com>
+ */
+
+#ifndef _ASTERISK_DNS_RECURRING_H
+#define _ASTERISK_DNS_RECURRING_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+/*! \brief Opaque structure for a recurring DNS query */
+struct ast_dns_query_recurring;
+
+/*!
+ * \brief Asynchronously resolve a DNS query, and continue resolving it according to the lowest TTL available
+ *
+ * \param name The name of what to resolve
+ * \param rr_type Resource record type
+ * \param rr_class Resource record class
+ * \param callback The callback to invoke upon completion
+ * \param data User data to make available on the query
+ *
+ * \retval non-NULL success - query has been sent for resolution
+ * \retval NULL failure
+ *
+ * \note The user data passed in to this function must be ao2 allocated
+ *
+ * \note This query will continue to happen according to the lowest TTL unless cancelled using ast_dns_resolve_cancel
+ *
+ * \note It is NOT possible for the callback to be invoked concurrently for the query multiple times
+ */
+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);
+
+/*!
+ * \brief Cancel an asynchronous recurring DNS resolution
+ *
+ * \param query The DNS query returned from ast_dns_resolve_recurring
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ *
+ * \note If successfully cancelled the callback will not be invoked
+ */
+int ast_dns_resolve_recurring_cancel(struct ast_dns_query_recurring *recurring);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif /* _ASTERISK_DNS_RECURRING_H */

Propchange: team/group/dns/include/asterisk/dns_recurring.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/group/dns/include/asterisk/dns_recurring.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/group/dns/include/asterisk/dns_recurring.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/group/dns/main/dns_core.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns/main/dns_core.c?view=diff&rev=432516&r1=432515&r2=432516
==============================================================================
--- team/group/dns/main/dns_core.c (original)
+++ team/group/dns/main/dns_core.c Fri Mar  6 09:02:54 2015
@@ -40,6 +40,7 @@
 #include "asterisk/dns_naptr.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"
 
@@ -434,6 +435,131 @@
 	query->callback(query);
 }
 
+/*! \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);
+}
+
+/*! \brief Determine the TTL to use when scheduling recurring resolution */
+static int dns_query_recurring_get_ttl(const struct ast_dns_query *query)
+{
+	int ttl = 0;
+	const struct ast_dns_result *result = ast_dns_query_get_result(query);
+	const struct ast_dns_record *record;
+
+	if (ast_dns_result_get_nxdomain(result)) {
+		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) < ttl)) {
+			ttl = ast_dns_record_get_ttl(record);
+		}
+	}
+
+	return ttl;
+}
+
+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);
+	if (!recurring->cancelled) {
+		recurring->query = 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);
+	recurring->timer = -1;
+
+	/* So.. if something has not externally cancelled this we can reschedule based on the TTL */
+	if (!recurring->cancelled) {
+		int ttl = dns_query_recurring_get_ttl(query);
+
+		if (ttl) {
+			recurring->timer = ast_sched_add(sched, ttl, dns_query_recurring_scheduled_callback, ao2_bump(recurring));
+			if (recurring->timer < 0) {
+				ao2_ref(recurring, -1);
+			}
+		}
+	}
+
+	ao2_replace(recurring->query, 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) {
+		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 */
+
+	/* The scheduler callback expects a reference, so bump it up */
+	recurring->query = ast_dns_resolve_async(name, rr_type, rr_class, dns_query_recurring_resolution_callback, recurring);
+	if (!recurring->query) {
+		ao2_ref(recurring, -1);
+		return NULL;
+	}
+
+	return recurring;
+}
+
+int ast_dns_resolve_recurring_cancel(struct ast_dns_query_recurring *recurring)
+{
+	ao2_lock(recurring);
+
+	recurring->cancelled = 1;
+	AST_SCHED_DEL_UNREF(sched, recurring->timer, ao2_ref(recurring, -1));
+
+	if (recurring->query) {
+		ast_dns_resolve_cancel(recurring->query);
+		ao2_replace(recurring->query, NULL);
+	}
+
+	ao2_unlock(recurring);
+
+	return 0;
+}
+
 static void dns_shutdown(void)
 {
 	if (sched) {




More information about the asterisk-commits mailing list