Index: main/dns.c
===================================================================
--- main/dns.c	(revision 115572)
+++ main/dns.c	(working copy)
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2006 Thorsten Lockert
+ * Copyright (C) 1999 - 2005 Thorsten Lockert
  *
  * Written by Thorsten Lockert <tholo@trollphone.org>
  *
@@ -107,40 +107,40 @@
  */
 
 typedef struct {
-	unsigned	id:16;          /*!< query identification number */
+	unsigned	id :16;		/* query identification number */
 #if __BYTE_ORDER == __BIG_ENDIAN
 			/* fields in third byte */
-	unsigned	qr:1;           /*!< response flag */
-	unsigned	opcode:4;       /*!< purpose of message */
-	unsigned	aa:1;           /*!< authoritive answer */
-	unsigned	tc:1;           /*!< truncated message */
-	unsigned	rd:1;           /*!< recursion desired */
+	unsigned	qr: 1;		/* response flag */
+	unsigned	opcode: 4;	/* purpose of message */
+	unsigned	aa: 1;		/* authoritive answer */
+	unsigned	tc: 1;		/* truncated message */
+	unsigned	rd: 1;		/* recursion desired */
 			/* fields in fourth byte */
-	unsigned	ra:1;           /*!< recursion available */
-	unsigned	unused:1;       /*!< unused bits (MBZ as of 4.9.3a3) */
-	unsigned	ad:1;           /*!< authentic data from named */
-	unsigned	cd:1;           /*!< checking disabled by resolver */
-	unsigned	rcode:4;        /*!< response code */
+	unsigned	ra: 1;		/* recursion available */
+	unsigned	unused :1;	/* unused bits (MBZ as of 4.9.3a3) */
+	unsigned	ad: 1;		/* authentic data from named */
+	unsigned	cd: 1;		/* checking disabled by resolver */
+	unsigned	rcode :4;	/* response code */
 #endif
 #if __BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __PDP_ENDIAN
 			/* fields in third byte */
-	unsigned	rd:1;           /*!< recursion desired */
-	unsigned	tc:1;           /*!< truncated message */
-	unsigned	aa:1;           /*!< authoritive answer */
-	unsigned	opcode:4;       /*!< purpose of message */
-	unsigned	qr:1;           /*!< response flag */
+	unsigned	rd :1;		/* recursion desired */
+	unsigned	tc :1;		/* truncated message */
+	unsigned	aa :1;		/* authoritive answer */
+	unsigned	opcode :4;	/* purpose of message */
+	unsigned	qr :1;		/* response flag */
 			/* fields in fourth byte */
-	unsigned	rcode:4;        /*!< response code */
-	unsigned	cd:1;           /*!< checking disabled by resolver */
-	unsigned	ad:1;           /*!< authentic data from named */
-	unsigned	unused:1;       /*!< unused bits (MBZ as of 4.9.3a3) */
-	unsigned	ra:1;           /*!< recursion available */
+	unsigned	rcode :4;	/* response code */
+	unsigned	cd: 1;		/* checking disabled by resolver */
+	unsigned	ad: 1;		/* authentic data from named */
+	unsigned	unused :1;	/* unused bits (MBZ as of 4.9.3a3) */
+	unsigned	ra :1;		/* recursion available */
 #endif
 			/* remaining bytes */
-	unsigned	qdcount:16;     /*!< number of question entries */
-	unsigned	ancount:16;     /*!< number of answer entries */
-	unsigned	nscount:16;     /*!< number of authority entries */
-	unsigned	arcount:16;     /*!< number of resource entries */
+	unsigned	qdcount :16;	/* number of question entries */
+	unsigned	ancount :16;	/* number of answer entries */
+	unsigned	nscount :16;	/* number of authority entries */
+	unsigned	arcount :16;	/* number of resource entries */
 } dns_HEADER;
 
 struct dn_answer {
@@ -173,7 +173,7 @@
 	return x;
 }
 
-/*! \brief Parse DNS lookup result, call callback */
+/*--- dns_parse_answer: Parse DNS lookup result, call callback */
 static int dns_parse_answer(void *context,
 	int class, int type, unsigned char *answer, int len,
 	int (*callback)(void *context, unsigned char *answer, int len, unsigned char *fullanswer))
@@ -236,28 +236,35 @@
 	return 0;
 }
 
-#ifndef HAVE_RES_NINIT
+#if defined(res_ninit)
+#define HAS_RES_NINIT
+#if defined(res_ndestroy)
+#define HAS_RES_NDESTROY
+#endif
+#else
 AST_MUTEX_DEFINE_STATIC(res_lock);
+#if 0
+#warning "Warning, res_ninit is missing...  Could have reentrancy issues"
 #endif
+#endif
 
-/*! \brief Lookup record in DNS 
-\note Asterisk DNS is synchronus at this time. This means that if your DNS does
-not work properly, Asterisk might not start properly or a channel may lock.
-*/
+/*--- ast_search_dns: Lookup record in DNS */
 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))
 {
-#ifdef HAVE_RES_NINIT
+#ifdef HAS_RES_NINIT
 	struct __res_state dnsstate;
 #endif
 	unsigned char answer[MAX_SIZE];
 	int res, ret = -1;
 
-#ifdef HAVE_RES_NINIT
+#ifdef HAS_RES_NINIT
+#ifdef MAKE_VALGRIND_HAPPY
 	memset(&dnsstate, 0, sizeof(dnsstate));
+#endif	
 	res_ninit(&dnsstate);
-	res = res_nsearch(&dnsstate, dname, class, type, answer, sizeof(answer));
+	res = res_nsearch(&dnsstate, dname, class, type, (unsigned char *)answer, sizeof(answer));
 #else
 	ast_mutex_lock(&res_lock);
 	res_init();
@@ -268,15 +275,15 @@
 			ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname);
 			ret = -1;
 		}
-		else if (res == 0) {
+		else if (ret == 0) {
 			ast_log(LOG_DEBUG, "No matches found in DNS for %s\n", dname);
 			ret = 0;
 		}
 		else
 			ret = 1;
 	}
-#ifdef HAVE_RES_NINIT
-#ifdef HAVE_RES_NDESTROY
+#ifdef HAS_RES_NINIT
+#ifdef HAS_RES_NDESTROY
 	res_ndestroy(&dnsstate);
 #else
 	res_nclose(&dnsstate);
@@ -287,6 +294,5 @@
 #endif
 	ast_mutex_unlock(&res_lock);
 #endif
-
 	return ret;
 }
