[asterisk-commits] file: branch file/pjsip-dns r410487 - in /team/file/pjsip-dns: include/asteri...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Mar 12 12:31:14 CDT 2014


Author: file
Date: Wed Mar 12 12:31:09 2014
New Revision: 410487

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=410487
Log:
Use res_init or res_ninit to get the local system nameservers.

Modified:
    team/file/pjsip-dns/include/asterisk/dns.h
    team/file/pjsip-dns/main/dns.c
    team/file/pjsip-dns/res/res_pjsip/config_system.c

Modified: team/file/pjsip-dns/include/asterisk/dns.h
URL: http://svnview.digium.com/svn/asterisk/team/file/pjsip-dns/include/asterisk/dns.h?view=diff&rev=410487&r1=410486&r2=410487
==============================================================================
--- team/file/pjsip-dns/include/asterisk/dns.h (original)
+++ team/file/pjsip-dns/include/asterisk/dns.h Wed Mar 12 12:31:09 2014
@@ -36,4 +36,7 @@
 int ast_search_dns(void *context, const char *dname, int class, int type,
 	 int (*callback)(void *context, unsigned char *answer, int len, unsigned char *fullanswer));
 
+/*! \brief Retrieve the configured nameservers of the system */
+struct ao2_container *ast_dns_get_nameservers(void);
+
 #endif /* _ASTERISK_DNS_H */

Modified: team/file/pjsip-dns/main/dns.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pjsip-dns/main/dns.c?view=diff&rev=410487&r1=410486&r2=410487
==============================================================================
--- team/file/pjsip-dns/main/dns.c (original)
+++ team/file/pjsip-dns/main/dns.c Wed Mar 12 12:31:09 2014
@@ -296,3 +296,47 @@
 
 	return ret;
 }
+
+struct ao2_container *ast_dns_get_nameservers(void)
+{
+#ifdef HAVE_RES_NINIT
+	struct __res_state dnsstate;
+#endif
+	struct __res_state *state;
+	struct ao2_container *nameservers;
+	int i;
+
+	nameservers = ast_str_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 3);
+	if (!nameservers) {
+		return NULL;
+	}
+
+#ifdef HAVE_RES_NINIT
+	memset(&dnsstate, 0, sizeof(dnsstate));
+	res_ninit(&dnsstate);
+	state = &dnsstate;
+#else
+	ast_mutex_lock(&res_lock);
+	res_init();
+	state = &_res;
+#endif
+
+	for (i = 0; i < state->nscount; i++) {
+		ast_str_container_add(nameservers, ast_inet_ntoa(state->nsaddr_list[i].sin_addr));
+	}
+
+#ifdef HAVE_RES_NINIT
+#ifdef HAVE_RES_NDESTROY
+	res_ndestroy(&dnsstate);
+#else
+	res_nclose(&dnsstate);
+#endif
+#else
+#ifdef HAVE_RES_CLOSE
+	res_close();
+#endif
+	ast_mutex_unlock(&res_lock);
+#endif
+
+	return nameservers;
+}

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=410487&r1=410486&r2=410487
==============================================================================
--- 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:31:09 2014
@@ -25,6 +25,7 @@
 #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
@@ -187,39 +188,34 @@
 /*! \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)
 {
-	FILE *f;
-	char buf[256], *nameserver = buf + 10;
-
-	f = fopen("/etc/resolv.conf", "r");
-	if (!f) {
-		ast_log(LOG_ERROR, "Could not open '/etc/resolv.conf' for automatic nameserver discovery\n");
-		return -1;
-	}
-
-	while (!feof(f)) {
-		if (!fgets(buf, sizeof(buf), f)) {
-			if (ferror(f)) {
-				ast_log(LOG_ERROR, "Error reading from file /etc/resolv.conf: %s\n", strerror(errno));
-			}
-			continue;
-		} else if (strncmp(buf, "nameserver", 10)) {
-			continue;
-		}
-
-		nameserver = ast_strip((buf + 10));
-		if (ast_strlen_zero(nameserver)) {
-			continue;
-		}
-
-		/* Since the memory where the nameserver is held will be overwritten we duplicate it from a pool passed in*/
+	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;
 		}
 	}
-
-	fclose(f);
+	ao2_iterator_destroy(&it_nameservers);
+
+	ao2_ref(discovered_nameservers, -1);
 
 	return 0;
 }




More information about the asterisk-commits mailing list