Index: /branches/rel_apv_10_7/usr/click/lib/libdns_agent/dns_daemon.c
===================================================================
--- /branches/rel_apv_10_7/usr/click/lib/libdns_agent/dns_daemon.c	(revision 39091)
+++ /branches/rel_apv_10_7/usr/click/lib/libdns_agent/dns_daemon.c	(working copy)
@@ -53,6 +53,12 @@
 	struct gaicb req_item;
 	uint32_t ttl;
 };
+
+typedef struct {
+    uint32_t *ttl;
+    volatile int *done;
+} dns_cb_ctx_t;
+
 #define DNS_REQ_LIST_LEN 20
 #define DEFAULT_WEBAGENT_DNS_TTL 1
 
@@ -420,17 +426,21 @@
 static void
 addrinfo_cb(void *arg, int status, int timeouts, struct ares_addrinfo *result)
 {
-	uint32_t *ttl = (uint32_t *)arg;
+	dns_cb_ctx_t *ctx = (dns_cb_ctx_t *)arg;
+	uint32_t *ttl = ctx->ttl;
+	volatile int *done = ctx->done;
 
-	// fastlog_syslog(LOG_DEBUG,"%s: Result: %s, timeouts: %d", __FUNCTION__ ares_strerror(status), timeouts);
+	// fastlog_syslog(LOG_DEBUG,"%s: Result: %d(%s), timeouts: %d", __FUNCTION__, status, ares_strerror(status), timeouts);
+	fastlog_syslog(LOG_DEBUG,"%s: rv: %d, t: %d", __FUNCTION__, status, timeouts);
 	if (result) {
 		struct ares_addrinfo_node *node = result->nodes;
 		*ttl = node->ai_ttl;
 	}
 	else{
-		fastlog_syslog(LOG_DEBUG, "%s: no result from ares_getaddrinfo()", __FUNCTION__);
+		// fastlog_syslog(LOG_DEBUG, "%s: no result from ares_getaddrinfo()", __FUNCTION__);
 	}
 	ares_freeaddrinfo(result);
+	*done = 1;
 }
 
 static int
@@ -443,7 +453,11 @@
 	int	optmask = 0;
 	struct ares_addrinfo_hints hints;
 	uint32_t ttl = 0;
-	int timeout_ms = 1000;
+	volatile int done = 0;
+	dns_cb_ctx_t ctx = {
+		.ttl = &ttl,
+		.done = &done,
+	};
 
 	/* find first free slot and Queue nreqs_base..nreqs requests. */
 	while (free_idx < DNS_REQ_LIST_LEN) {
@@ -478,8 +492,10 @@
 	ares_library_init(ARES_LIB_INIT_ALL);
 
 	memset(&options, 0, sizeof(options));
-	optmask      |= ARES_OPT_EVENT_THREAD;
+	optmask = ARES_OPT_EVENT_THREAD | ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES | ARES_OPT_SERVER_FAILOVER;
 	options.evsys = ARES_EVSYS_DEFAULT;
+	options.timeout = 250;  // ms
+	options.tries = 1;      // per nameserver
 
 	if (ares_init_options(&channel, &options, optmask) != ARES_SUCCESS) {
 		fastlog_syslog(LOG_DEBUG, "c-ares initialization issue");
@@ -488,11 +504,22 @@
 		memset(&hints, 0, sizeof(hints));
 		hints.ai_family = AF_UNSPEC;
 		hints.ai_flags  = ARES_AI_CANONNAME;
-		ares_getaddrinfo(channel, host, NULL, &hints, addrinfo_cb, &ttl);
-		dns_req_wait_list[free_idx].ttl = ttl;
+		ares_getaddrinfo(channel, host, NULL, &hints, addrinfo_cb, &ctx);
+
+		int total_waited = 0;
+		while (!done && total_waited < 500) { // at most 0.5 sec
+			usleep(DNS_AGENT_SLEEP_TIME);     // 0.001 sec
+			total_waited += 1;
+		}
+		fastlog_syslog(LOG_DEBUG, "handle: %d", total_waited);
+
+		if (!done)
+		{
+			fastlog_syslog(LOG_DEBUG, "%s callback timed out", host);
+		}
 
-		ares_queue_wait_empty(channel, timeout_ms);	/* if empty or timeout in timeout_ms millisecond */
 		ares_destroy(channel);
+		dns_req_wait_list[free_idx].ttl = ttl;
 	}
 	ares_library_cleanup();
 
@@ -544,7 +571,6 @@
 				if (dns_req_wait_list[i].ttl){		/* check if ttl is valid */
 					webagent_ctx->iptable.ip4_dns_record_table[0].ttl = dns_req_wait_list[i].ttl;
 				}else{
-					fastlog_syslog(LOG_DEBUG, "Writing default value = %d to ttl", DEFAULT_WEBAGENT_DNS_TTL);
 					webagent_ctx->iptable.ip4_dns_record_table[0].ttl = DEFAULT_WEBAGENT_DNS_TTL;
 				}
 			} else {
@@ -553,7 +579,6 @@
 				if (dns_req_wait_list[i].ttl){
 					webagent_ctx->iptable.ip6_dns_record_table[0].ttl = dns_req_wait_list[i].ttl;
 				}else{
-					fastlog_syslog(LOG_DEBUG, "Writing default value = %d to ttl", DEFAULT_WEBAGENT_DNS_TTL);
 					webagent_ctx->iptable.ip6_dns_record_table[0].ttl = DEFAULT_WEBAGENT_DNS_TTL;
 				}
 			}
