[svn-commits] file: branch file/pjsip-dns r410488 - in /team/file/pjsip-dns/res: ./ res_pjsip/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Mar 12 12:59:08 CDT 2014


Author: file
Date: Wed Mar 12 12:59:01 2014
New Revision: 410488

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=410488
Log:
Move nameserver configuration to global which unlocks reload support.

Modified:
    team/file/pjsip-dns/res/res_pjsip.c
    team/file/pjsip-dns/res/res_pjsip/config_global.c
    team/file/pjsip-dns/res/res_pjsip/config_system.c
    team/file/pjsip-dns/res/res_pjsip/pjsip_configuration.c

Modified: team/file/pjsip-dns/res/res_pjsip.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pjsip-dns/res/res_pjsip.c?view=diff&rev=410488&r1=410487&r2=410488
==============================================================================
--- team/file/pjsip-dns/res/res_pjsip.c (original)
+++ team/file/pjsip-dns/res/res_pjsip.c Wed Mar 12 12:59:01 2014
@@ -1067,15 +1067,6 @@
 					<synopsis>Maximum number of threads in the res_pjsip threadpool.
 					A value of 0 indicates no maximum.</synopsis>
 				</configOption>
-				<configOption name="nameservers" default="auto">
-					<synopsis>Set nameservers to use for DNS resolution.</synopsis>
-					<description><para>
-						The nameservers can be specified by order of preference using ',' as a separator.
-						To have the nameservers configured on the system automatically used you can specify
-						the special value of "auto". To disable DNS support and resort to using the system
-						for resolution you can specify the special value of "disabled".
-					</para></description>
-				</configOption>
 				<configOption name="type">
 					<synopsis>Must be of type 'system'.</synopsis>
 				</configOption>
@@ -1101,6 +1092,15 @@
 				<configOption name="debug" default="no">
 					<synopsis>Enable/Disable SIP debug logging.  Valid options include yes|no or
                                         a host address</synopsis>
+				</configOption>
+				<configOption name="nameservers" default="auto">
+					<synopsis>Set nameservers to use for DNS resolution.</synopsis>
+					<description><para>
+						The nameservers can be specified by order of preference using ',' as a separator.
+						To have the nameservers configured on the system automatically used you can specify
+						the special value of "auto". To disable DNS support and resort to using the system
+						for resolution you can specify the special value of "disabled".
+					</para></description>
 				</configOption>
 			</configObject>
 		</configFile>
@@ -2314,17 +2314,6 @@
 		return AST_MODULE_LOAD_DECLINE;
 	}
 
-	if (ast_sip_initialize_dns()) {
-		ast_log(LOG_ERROR, "Failed to initialize DNS. Aborting load\n");
-		ast_sip_destroy_system();
-		pj_pool_release(memory_pool);
-		memory_pool = NULL;
-		pjsip_endpt_destroy(ast_pjsip_endpoint);
-		ast_pjsip_endpoint = NULL;
-		pj_caching_pool_destroy(&caching_pool);
-		return AST_MODULE_LOAD_DECLINE;
-	}
-
 	pjsip_tsx_layer_init_module(ast_pjsip_endpoint);
 	pjsip_ua_init_module(ast_pjsip_endpoint, NULL);
 

Modified: team/file/pjsip-dns/res/res_pjsip/config_global.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pjsip-dns/res/res_pjsip/config_global.c?view=diff&rev=410488&r1=410487&r2=410488
==============================================================================
--- team/file/pjsip-dns/res/res_pjsip/config_global.c (original)
+++ team/file/pjsip-dns/res/res_pjsip/config_global.c Wed Mar 12 12:59:01 2014
@@ -25,10 +25,12 @@
 #include "include/res_pjsip_private.h"
 #include "asterisk/sorcery.h"
 #include "asterisk/ast_version.h"
+#include "asterisk/dns.h"
 
 #define DEFAULT_MAX_FORWARDS 70
 #define DEFAULT_USERAGENT_PREFIX "Asterisk PBX"
 #define DEFAULT_OUTBOUND_ENDPOINT "default_outbound_endpoint"
+#define DEFAULT_NAMESERVERS "auto"
 
 static char default_useragent[128];
 
@@ -39,6 +41,8 @@
 		AST_STRING_FIELD(default_outbound_endpoint);
 		/*! Debug logging yes|no|host */
 		AST_STRING_FIELD(debug);
+		/*! Nameservers for DNS */
+		AST_STRING_FIELD(nameservers);
 	);
 	/* Value to put in Max-Forwards header */
 	unsigned int max_forwards;
@@ -135,6 +139,138 @@
 			OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_outbound_endpoint));
 	ast_sorcery_object_field_register(sorcery, "global", "debug", "no",
 			OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, debug));
-
-	return 0;
-}
+	ast_sorcery_object_field_register(sorcery, "global", "nameservers", DEFAULT_NAMESERVERS,
+			OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, nameservers));
+
+	return 0;
+}
+
+
+/*! \brief Helper function which parses resolv.conf and automatically adds nameservers if found */
+static int system_add_resolv_conf_nameservers(pj_pool_t *pool, pj_str_t *nameservers, unsigned int *count)
+{
+	struct ao2_container *discovered_nameservers;
+	struct ao2_iterator it_nameservers;
+	char *nameserver;
+
+	discovered_nameservers = ast_dns_get_nameservers();
+	if (!discovered_nameservers) {
+		ast_log(LOG_ERROR, "Could not retrieve local system nameservers\n");
+		return -1;
+	}
+
+	if (!ao2_container_count(discovered_nameservers)) {
+		ast_log(LOG_ERROR, "There are no local system nameservers configured\n");
+		ao2_ref(discovered_nameservers, -1);
+		return -1;
+	}
+
+	it_nameservers = ao2_iterator_init(discovered_nameservers, 0);
+	while ((nameserver = ao2_iterator_next(&it_nameservers))) {
+		pj_strdup2(pool, &nameservers[(*count)++], nameserver);
+		ao2_ref(nameserver, -1);
+
+		if (*count == (PJ_DNS_RESOLVER_MAX_NS - 1)) {
+			break;
+		}
+	}
+	ao2_iterator_destroy(&it_nameservers);
+
+	ao2_ref(discovered_nameservers, -1);
+
+	return 0;
+}
+
+static int system_create_resolver_and_set_nameservers(void *data)
+{
+	struct global_config *cfg = get_global_cfg();
+	pj_status_t status;
+	pj_pool_t *pool = NULL;
+	pj_dns_resolver *resolver;
+	pj_str_t nameservers[PJ_DNS_RESOLVER_MAX_NS];
+	unsigned int count = 0;
+	char *nameserver, *remaining;
+
+	if (cfg) {
+		remaining = ast_strdupa(cfg->nameservers);
+	} else {
+		remaining = ast_strdupa(DEFAULT_NAMESERVERS);
+	}
+
+	ao2_cleanup(cfg);
+
+	/* If DNS support has been disabled don't even bother doing anything, just resort to the
+	 * system way of doing lookups
+	 */
+	if (!strcmp(remaining, "disabled")) {
+		return 0;
+	}
+
+	if (!pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint())) {
+		status = pjsip_endpt_create_resolver(ast_sip_get_pjsip_endpoint(), &resolver);
+		if (status != PJ_SUCCESS) {
+			ast_log(LOG_ERROR, "Could not create DNS resolver(%d)\n", status);
+			return -1;
+		}
+	}
+
+	while ((nameserver = strsep(&remaining, ","))) {
+		nameserver = ast_strip(nameserver);
+
+		if (!strcmp(nameserver, "auto")) {
+			if (!pool) {
+				pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Automatic Nameserver Discovery", 256, 256);
+			}
+			if (!pool) {
+				ast_log(LOG_ERROR, "Could not create memory pool for automatic nameserver discovery\n");
+				return -1;
+			} else if (system_add_resolv_conf_nameservers(pool, nameservers, &count)) {
+				/* A log message will have already been output by system_add_resolv_conf_nameservers */
+				pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
+				return -1;
+			}
+		} else {
+			pj_strset2(&nameservers[count++], nameserver);
+		}
+
+		/* If we have reached the max number of nameservers we can specify bail early */
+		if (count == (PJ_DNS_RESOLVER_MAX_NS - 1)) {
+			break;
+		}
+	}
+
+	if (!count) {
+		ast_log(LOG_ERROR, "No nameservers specified for DNS resolver, resorting to system resolution\n");
+		if (pool) {
+			pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
+		}
+		return 0;
+	}
+
+	status = pj_dns_resolver_set_ns(resolver, count, nameservers, NULL);
+
+	/* Since we no longer need the nameservers we can drop the memory pool they may be allocated from */
+	if (pool) {
+		pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
+	}
+
+	if (status != PJ_SUCCESS) {
+		ast_log(LOG_ERROR, "Could not set nameservers on DNS resolver in PJSIP(%d)\n", status);
+		return -1;
+	}
+
+	if (!pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint())) {
+		status = pjsip_endpt_set_resolver(ast_sip_get_pjsip_endpoint(), resolver);
+		if (status != PJ_SUCCESS) {
+			ast_log(LOG_ERROR, "Could not set DNS resolver in PJSIP(%d)\n", status);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
+int ast_sip_initialize_dns(void)
+{
+	return ast_sip_push_task_synchronous(NULL, system_create_resolver_and_set_nameservers, NULL);
+}

Modified: team/file/pjsip-dns/res/res_pjsip/config_system.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pjsip-dns/res/res_pjsip/config_system.c?view=diff&rev=410488&r1=410487&r2=410488
==============================================================================
--- team/file/pjsip-dns/res/res_pjsip/config_system.c (original)
+++ team/file/pjsip-dns/res/res_pjsip/config_system.c Wed Mar 12 12:59:01 2014
@@ -25,13 +25,10 @@
 #include "asterisk/sorcery.h"
 #include "include/res_pjsip_private.h"
 #include "asterisk/threadpool.h"
-#include "asterisk/dns.h"
 
 #define TIMER_T1_MIN 100
 #define DEFAULT_TIMER_T1 500
 #define DEFAULT_TIMER_B 32000
-
-#define NAMESERVERS_SIZE 512
 
 struct system_config {
 	SORCERY_OBJECT(details);
@@ -51,15 +48,11 @@
 		/*! Maxumum number of threads in the threadpool */
 		int max_size;
 	} threadpool;
-	/*! Configured nameservers */
-	char nameservers[NAMESERVERS_SIZE];
 };
 
 static struct ast_threadpool_options sip_threadpool_options = {
 	.version = AST_THREADPOOL_OPTIONS_VERSION,
 };
-
-static char sip_nameservers[NAMESERVERS_SIZE];
 
 void sip_get_threadpool_options(struct ast_threadpool_options *threadpool_options)
 {
@@ -109,8 +102,6 @@
 	sip_threadpool_options.idle_timeout = system->threadpool.idle_timeout;
 	sip_threadpool_options.max_size = system->threadpool.max_size;
 
-	ast_copy_string(sip_nameservers, system->nameservers, sizeof(sip_nameservers));
-
 	return 0;
 }
 
@@ -151,8 +142,6 @@
 			OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.idle_timeout));
 	ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_max_size", "0",
 			OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.max_size));
-	ast_sorcery_object_field_register(system_sorcery, "system", "nameservers", "auto",
-			OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct system_config, nameservers));
 
 	ast_sorcery_load(system_sorcery);
 
@@ -185,118 +174,3 @@
 	ast_sorcery_unref(system_sorcery);
 }
 
-/*! \brief Helper function which parses resolv.conf and automatically adds nameservers if found */
-static int system_add_resolv_conf_nameservers(pj_pool_t *pool, pj_str_t *nameservers, unsigned int *count)
-{
-	struct ao2_container *discovered_nameservers;
-	struct ao2_iterator it_nameservers;
-	char *nameserver;
-
-	discovered_nameservers = ast_dns_get_nameservers();
-	if (!discovered_nameservers) {
-		ast_log(LOG_ERROR, "Could not retrieve local system nameservers\n");
-		return -1;
-	}
-
-	if (!ao2_container_count(discovered_nameservers)) {
-		ast_log(LOG_ERROR, "There are no local system nameservers configured\n");
-		ao2_ref(discovered_nameservers, -1);
-		return -1;
-	}
-
-	it_nameservers = ao2_iterator_init(discovered_nameservers, 0);
-	while ((nameserver = ao2_iterator_next(&it_nameservers))) {
-		pj_strdup2(pool, &nameservers[(*count)++], nameserver);
-		ao2_ref(nameserver, -1);
-
-		if (*count == (PJ_DNS_RESOLVER_MAX_NS - 1)) {
-			break;
-		}
-	}
-	ao2_iterator_destroy(&it_nameservers);
-
-	ao2_ref(discovered_nameservers, -1);
-
-	return 0;
-}
-
-static int system_create_resolver_and_set_nameservers(void *data)
-{
-	pj_status_t status;
-	pj_pool_t *pool = NULL;
-	pj_dns_resolver *resolver;
-	pj_str_t nameservers[PJ_DNS_RESOLVER_MAX_NS];
-	unsigned int count = 0;
-	char *nameserver, *remaining = sip_nameservers;
-
-	status = pjsip_endpt_create_resolver(ast_sip_get_pjsip_endpoint(), &resolver);
-	if (status != PJ_SUCCESS) {
-		ast_log(LOG_ERROR, "Could not create DNS resolver(%d)\n", status);
-		return -1;
-	}
-
-	while ((nameserver = strsep(&remaining, ","))) {
-		nameserver = ast_strip(nameserver);
-
-		if (!strcmp(nameserver, "auto")) {
-			if (!pool) {
-				pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Automatic Nameserver Discovery", 256, 256);
-			}
-			if (!pool) {
-				ast_log(LOG_ERROR, "Could not create memory pool for automatic nameserver discovery\n");
-				return -1;
-			} else if (system_add_resolv_conf_nameservers(pool, nameservers, &count)) {
-				/* A log message will have already been output by system_add_resolv_conf_nameservers */
-				pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
-				return -1;
-			}
-		} else {
-			pj_strset2(&nameservers[count++], nameserver);
-		}
-
-		/* If we have reached the max number of nameservers we can specify bail early */
-		if (count == (PJ_DNS_RESOLVER_MAX_NS - 1)) {
-			break;
-		}
-	}
-
-	if (!count) {
-		ast_log(LOG_ERROR, "No nameservers specified for DNS resolver, resorting to system resolution\n");
-		if (pool) {
-			pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
-		}
-		return 0;
-	}
-
-	status = pj_dns_resolver_set_ns(resolver, count, nameservers, NULL);
-
-	/* Since we no longer need the nameservers we can drop the memory pool they may be allocated from */
-	if (pool) {
-		pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
-	}
-
-	if (status != PJ_SUCCESS) {
-		ast_log(LOG_ERROR, "Could not set nameservers on DNS resolver in PJSIP(%d)\n", status);
-		return -1;
-	}
-
-	status = pjsip_endpt_set_resolver(ast_sip_get_pjsip_endpoint(), resolver);
-	if (status != PJ_SUCCESS) {
-		ast_log(LOG_ERROR, "Could not set DNS resolver in PJSIP(%d)\n", status);
-		return -1;
-	}
-
-	return 0;
-}
-
-int ast_sip_initialize_dns(void)
-{
-	/* If DNS support has been disabled don't even bother doing anything, just resort to the
-	 * system way of doing lookups
-	 */
-	if (!strcmp(sip_nameservers, "disabled")) {
-		return 0;
-	}
-
-	return ast_sip_push_task_synchronous(NULL, system_create_resolver_and_set_nameservers, NULL);
-}

Modified: team/file/pjsip-dns/res/res_pjsip/pjsip_configuration.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pjsip-dns/res/res_pjsip/pjsip_configuration.c?view=diff&rev=410488&r1=410487&r2=410488
==============================================================================
--- team/file/pjsip-dns/res/res_pjsip/pjsip_configuration.c (original)
+++ team/file/pjsip-dns/res/res_pjsip/pjsip_configuration.c Wed Mar 12 12:59:01 2014
@@ -1760,6 +1760,13 @@
 		return -1;
 	}
 
+	if (ast_sip_initialize_dns()) {
+		ast_log(LOG_ERROR, "Failed to initialize DNS support\n");
+		ast_sorcery_unref(sip_sorcery);
+		sip_sorcery = NULL;
+		return -1;
+	}
+
 	channel_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
 	if (!channel_formatter) {
 		ast_log(LOG_ERROR, "Unable to allocate memory for channel_formatter\n");




More information about the svn-commits mailing list