[asterisk-commits] file: branch group/dns r432513 - /team/group/dns/main/dns_core.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Mar 6 07:10:58 CST 2015


Author: file
Date: Fri Mar  6 07:10:48 2015
New Revision: 432513

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=432513
Log:
Add a scheduler for recurring DNS queries.

Modified:
    team/group/dns/main/dns_core.c

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=432513&r1=432512&r2=432513
==============================================================================
--- team/group/dns/main/dns_core.c (original)
+++ team/group/dns/main/dns_core.c Fri Mar  6 07:10:48 2015
@@ -35,6 +35,7 @@
 #include "asterisk/vector.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/strings.h"
+#include "asterisk/sched.h"
 #include "asterisk/dns_core.h"
 #include "asterisk/dns_naptr.h"
 #include "asterisk/dns_srv.h"
@@ -45,6 +46,8 @@
 #include <arpa/nameser.h>
 
 AST_RWLIST_HEAD_STATIC(resolvers, ast_dns_resolver);
+
+static struct ast_sched_context *sched;
 
 const char *ast_dns_query_get_name(const struct ast_dns_query *query)
 {
@@ -447,6 +450,14 @@
 	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;
@@ -468,6 +479,27 @@
 	}
 
 	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)) {
@@ -513,6 +545,11 @@
 		}
 	}
 	AST_RWLIST_TRAVERSE_SAFE_END;
+
+	if (AST_LIST_EMPTY(&resolvers)) {
+		dns_shutdown();
+	}
+
 	AST_RWLIST_UNLOCK(&resolvers);
 
 	ast_verb(2, "Unregistered DNS resolver '%s'\n", resolver->name);




More information about the asterisk-commits mailing list