Index: pjproject/trunk/pjlib-util/build/pjlib_util.dsp
===================================================================
--- pjproject/trunk/pjlib-util/build/pjlib_util.dsp	(revision 1101)
+++ pjproject/trunk/pjlib-util/build/pjlib_util.dsp	(revision 1102)
@@ -142,4 +142,8 @@
 # Begin Source File
 
+SOURCE="..\src\pjlib-util\srv_resolver.c"
+# End Source File
+# Begin Source File
+
 SOURCE="..\src\pjlib-util\string.c"
 # End Source File
@@ -219,4 +223,8 @@
 
 SOURCE="..\include\pjlib-util\sha1.h"
+# End Source File
+# Begin Source File
+
+SOURCE="..\include\pjlib-util\srv_resolver.h"
 # End Source File
 # Begin Source File
Index: pjproject/trunk/pjlib-util/build/pjlib_util.vcproj
===================================================================
--- pjproject/trunk/pjlib-util/build/pjlib_util.vcproj	(revision 1101)
+++ pjproject/trunk/pjlib-util/build/pjlib_util.vcproj	(revision 1102)
@@ -5,4 +5,5 @@
 	Name="pjlib_util"
 	ProjectGUID="{FE07F272-AE7F-4549-9E9F-EF9B80CB1693}"
+	RootNamespace="pjlib_util"
 	>
 	<Platforms>
@@ -466,4 +467,8 @@
 			</File>
 			<File
+				RelativePath="..\src\pjlib-util\srv_resolver.c"
+				>
+			</File>
+			<File
 				RelativePath="..\src\pjlib-util\string.c"
 				>
@@ -637,4 +642,8 @@
 			</File>
 			<File
+				RelativePath="..\include\pjlib-util\srv_resolver.h"
+				>
+			</File>
+			<File
 				RelativePath="..\include\pjlib-util\string.h"
 				>
Index: pjproject/trunk/pjlib-util/include/pjlib-util.h
===================================================================
--- pjproject/trunk/pjlib-util/include/pjlib-util.h	(revision 1101)
+++ pjproject/trunk/pjlib-util/include/pjlib-util.h	(revision 1102)
@@ -42,4 +42,5 @@
 #include <pjlib-util/dns.h>
 #include <pjlib-util/resolver.h>
+#include <pjlib-util/srv_resolver.h>
 
 /* Text scanner */
Index: pjproject/trunk/pjlib-util/include/pjlib-util/srv_resolver.h
===================================================================
--- pjproject/trunk/pjlib-util/include/pjlib-util/srv_resolver.h	(revision 1102)
+++ pjproject/trunk/pjlib-util/include/pjlib-util/srv_resolver.h	(revision 1102)
@@ -0,0 +1,162 @@
+/* $Id$ */
+/* 
+ * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
+ */
+#ifndef __PJLIB_UTIL_SRV_RESOLVER_H__
+#define __PJLIB_UTIL_SRV_RESOLVER_H__
+
+/**
+ * @file srv_resolver.h
+ * @brief DNS SRV resolver
+ */
+#include <pjlib-util/resolver.h>
+
+PJ_BEGIN_DECL
+
+/**
+ * @defgroup PJ_DNS_SRV_RESOLVER DNS SRV Resolution Helper
+ * @ingroup PJ_DNS
+ * @{
+ *
+ * \subsection PJ_DNS_SRV_RESOLVER_INTRO DNS SRV Resolution Helper
+ *
+ * This module provides an even higher layer of abstraction for the DNS
+ * resolution framework, to resolve DNS SRV names.
+ *
+ * The #pj_dns_srv_resolve() function will asynchronously resolve the server
+ * name into IP address(es) with a single function call. If the SRV name
+ * contains multiple names, then each will be resolved with individual
+ * DNS A resolution to get the IP addresses. Upon successful completion, 
+ * application callback will be called with each IP address of the
+ * target selected based on the load-balancing and fail-over criteria
+ * below.
+ *
+ * When the resolver fails to resolve the name using DNS SRV resolution
+ * (for example when the DNS SRV record is not present in the DNS server),
+ * the resolver will fallback to using DNS A record resolution to resolve
+ * the name.
+ *
+ * \subsection PJ_DNS_SRV_RESOLVER_FAILOVER_LOADBALANCE Load-Balancing and Fail-Over
+ *
+ * When multiple targets are returned in the DNS SRV response, server entries
+ * are selected based on the following rule (which is described in RFC 2782):
+ *  - targets will be sorted based on the priority first.
+ *  - for targets with the same priority, #pj_dns_srv_resolve() will select
+ *    only one target according to its weight. To select this one target,
+ *    the function associates running-sum for all targets, and generates 
+ *    a random number between zero and the total running-sum (inclusive).
+ *    The target selected is the first target with running-sum greater than
+ *    or equal to this random number.
+ *
+ * The above procedure will select one target for each priority, allowing
+ * application to fail-over to the next target when the previous target fails.
+ * These targets are returned in the #pj_dns_srv_record structure 
+ * argument of the callback. 
+ *
+ * \section PJ_DNS_SRV_RESOLVER_REFERENCE Reference
+ *
+ * Reference:
+ *  - <A HREF="http://www.ietf.org/rfc/rfc2782.txt">RFC 2782</A>: 
+ *	A DNS RR for specifying the location of services (DNS SRV)
+ */
+
+/**
+ * Maximum server address entries per one SRV record
+ */
+#ifndef PJ_DNS_SRV_MAX_ADDR
+#   define PJ_DNS_SRV_MAX_ADDR	    8
+#endif
+
+
+/**
+ * The server addresses returned by the resolver.
+ */
+typedef struct pj_dns_srv_record
+{
+    /** Number of address records. */
+    unsigned	count;
+
+    /** Address records. */
+    struct
+    {
+	/** Server priority (the lower the higher the priority). */
+	unsigned		priority;
+
+	/** Server weight (the higher the more load it can handle). */
+	unsigned		weight;
+
+	/** The server's address. */
+	pj_sockaddr		addr;
+
+	/** Address length. */
+	int			addr_len;
+
+    } entry[PJ_DNS_SRV_MAX_ADDR];
+
+} pj_dns_srv_record;
+
+
+/**
+ * Type of callback function to receive notification from the resolver
+ * when the resolution process completes.
+ */
+typedef void pj_dns_srv_resolver_cb(void *user_data,
+				    pj_status_t status,
+				    const pj_dns_srv_record *rec);
+
+
+/**
+ * Start DNS SRV resolution for the specified name. The full name of the
+ * entry will be concatenated from \a res_name and \a domain_name fragments.
+ *
+ * @param domain_name	The domain name part of the name.
+ * @param res_name	The full service name, including the transport name
+ *			and with all the leading underscore characters and
+ *			ending dot (e.g. "_sip._udp.", "_stun._udp.").
+ * @param def_port	The port number to be assigned to the resolved address
+ *			when the DNS SRV resolution fails and the name is 
+ *			resolved with DNS A resolution.
+ * @param pool		Memory pool used to allocate memory for the query.
+ * @param resolver	The resolver instance.
+ * @param fallback_a	Specify if the resolver should fallback with DNS A
+ *			resolution when the SRV resolution fails.
+ * @param token		Arbitrary data to be associated with this query when
+ *			the calback is called.
+ * @param cb		Pointer to callback function to receive the
+ *			notification when the resolution process completes.
+ *
+ * @return		PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pj_dns_srv_resolve(const pj_str_t *domain_name,
+					const pj_str_t *res_name,
+					unsigned def_port,
+					pj_pool_t *pool,
+					pj_dns_resolver *resolver,
+					pj_bool_t fallback_a,
+					void *token,
+					pj_dns_srv_resolver_cb *cb);
+
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+
+#endif	/* __PJLIB_UTIL_SRV_RESOLVER_H__ */
+
Index: pjproject/trunk/pjlib-util/src/pjlib-util/srv_resolver.c
===================================================================
--- pjproject/trunk/pjlib-util/src/pjlib-util/srv_resolver.c	(revision 1102)
+++ pjproject/trunk/pjlib-util/src/pjlib-util/srv_resolver.c	(revision 1102)
@@ -0,0 +1,647 @@
+/* $Id$ */
+/* 
+ * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
+ */
+#include <pjlib-util/srv_resolver.h>
+#include <pjlib-util/errno.h>
+#include <pj/array.h>
+#include <pj/assert.h>
+#include <pj/log.h>
+#include <pj/pool.h>
+#include <pj/rand.h>
+#include <pj/string.h>
+
+
+#define THIS_FILE   "srv_resolver.c"
+
+#define ADDR_MAX_COUNT	    8
+
+struct srv_target
+{
+    pj_str_t		    target_name;
+    char		    target_buf[PJ_MAX_HOSTNAME];
+    pj_str_t		    cname;
+    char		    cname_buf[PJ_MAX_HOSTNAME];
+    unsigned		    port;
+    unsigned		    priority;
+    unsigned		    weight;
+    unsigned		    sum;
+    unsigned		    addr_cnt;
+    pj_in_addr		    addr[ADDR_MAX_COUNT];
+};
+
+typedef struct pj_dns_srv_resolver_job
+{
+    char		    *objname;
+
+    pj_dns_resolver	    *resolver;	    /**< Resolver SIP instance.	    */
+    pj_dns_type		     dns_state;	    /**< DNS type being resolved.   */
+    void		    *token;
+    pj_dns_srv_resolver_cb  *cb;
+    pj_dns_async_query	    *qobject;
+    pj_status_t		     last_error;
+
+    /* Original request: */
+    pj_bool_t		     fallback_a;
+    pj_str_t		     full_name;
+    pj_str_t		     domain_part;
+    pj_uint16_t		     def_port;
+
+    /* SRV records and their resolved IP addresses: */
+    unsigned		     srv_cnt;
+    struct srv_target	     srv[PJ_DNS_SRV_MAX_ADDR];
+
+    /* Number of hosts in SRV records that the IP address has been resolved */
+    unsigned		     host_resolved;
+
+} pj_dns_srv_resolver_job;
+
+
+/* Async resolver callback, forward decl. */
+static void dns_callback(void *user_data,
+			 pj_status_t status,
+			 pj_dns_parsed_packet *pkt);
+
+
+
+/*
+ * The public API to invoke DNS SRV resolution.
+ */
+PJ_DEF(pj_status_t) pj_dns_srv_resolve( const pj_str_t *domain_name,
+				        const pj_str_t *res_name,
+					unsigned def_port,
+					pj_pool_t *pool,
+					pj_dns_resolver *resolver,
+					pj_bool_t fallback_a,
+					void *token,
+					pj_dns_srv_resolver_cb *cb)
+{
+    int len;
+    pj_str_t target_name;
+    pj_dns_srv_resolver_job *query_job;
+    pj_status_t status;
+
+    PJ_ASSERT_RETURN(domain_name && domain_name->slen &&
+		     res_name && res_name->slen &&
+		     pool && resolver && cb, PJ_EINVAL);
+
+    /* Build full name */
+    len = domain_name->slen + res_name->slen + 2;
+    target_name.ptr = (char*) pj_pool_alloc(pool, len);
+    pj_strcpy(&target_name, res_name);
+    if (res_name->ptr[res_name->slen-1] != '.')
+	pj_strcat2(&target_name, ".");
+    len = target_name.slen;
+    pj_strcat(&target_name, domain_name);
+    target_name.ptr[target_name.slen] = '\0';
+
+
+    /* Build the query_job state */
+    query_job = PJ_POOL_ZALLOC_T(pool, pj_dns_srv_resolver_job);
+    query_job->objname = target_name.ptr;
+    query_job->resolver = resolver;
+    query_job->token = token;
+    query_job->cb = cb;
+    query_job->fallback_a = fallback_a;
+    query_job->full_name = target_name;
+    query_job->domain_part.ptr = target_name.ptr + len;
+    query_job->domain_part.slen = target_name.slen - len;
+    query_job->def_port = (pj_uint16_t)def_port;
+
+    /* Start the asynchronous query_job */
+
+    query_job->dns_state = PJ_DNS_TYPE_SRV;
+
+    PJ_LOG(5, (query_job->objname, 
+	       "Starting async DNS %s query_job: target=%.*",
+	       pj_dns_get_type_name(query_job->dns_state),
+	       (int)target_name.slen, target_name.ptr));
+
+    status = pj_dns_resolver_start_query(resolver, &target_name, 
+				         query_job->dns_state, 0, 
+					 &dns_callback,
+    					 query_job, &query_job->qobject);
+    return status;
+}
+
+
+
+#define SWAP(type,ptr1,ptr2) if (ptr1 != ptr2) { \
+				type tmp; \
+				pj_memcpy(&tmp, ptr1, sizeof(type)); \
+				pj_memcpy(ptr1, ptr2, sizeof(type)); \
+				(ptr1)->target_name.ptr = (ptr1)->target_buf;\
+				pj_memcpy(ptr2, &tmp, sizeof(type)); \
+				(ptr2)->target_name.ptr = (ptr2)->target_buf;\
+			     } else {}
+
+
+/* Build server entries in the query_job based on received SRV response */
+static void build_server_entries(pj_dns_srv_resolver_job *query_job, 
+				 pj_dns_parsed_packet *response)
+{
+    unsigned i;
+
+    /* Save the Resource Records in DNS answer into SRV targets. */
+    query_job->srv_cnt = 0;
+    for (i=0; i<response->hdr.anscount && 
+	      query_job->srv_cnt < PJ_DNS_SRV_MAX_ADDR; ++i) 
+    {
+	pj_dns_parsed_rr *rr = &response->ans[i];
+	struct srv_target *srv = &query_job->srv[query_job->srv_cnt];
+
+	if (rr->type != PJ_DNS_TYPE_SRV) {
+	    PJ_LOG(4,(query_job->objname, 
+		      "Received non SRV answer for SRV query_job!"));
+	    continue;
+	}
+
+	if (rr->rdata.srv.target.slen > PJ_MAX_HOSTNAME) {
+	    PJ_LOG(4,(query_job->objname, "Hostname is too long!"));
+	    continue;
+	}
+
+	/* Build the SRV entry for RR */
+	pj_bzero(srv, sizeof(*srv));
+	srv->target_name.ptr = srv->target_buf;
+	pj_strncpy(&srv->target_name, &rr->rdata.srv.target,
+		   sizeof(srv->target_buf));
+	srv->port = rr->rdata.srv.port;
+	srv->priority = rr->rdata.srv.prio;
+	srv->weight = rr->rdata.srv.weight;
+	
+	++query_job->srv_cnt;
+    }
+
+    /* First pass: 
+     *	order the entries based on priority.
+     */
+    for (i=0; i<query_job->srv_cnt-1; ++i) {
+	unsigned min = i, j;
+	for (j=i+1; j<query_job->srv_cnt; ++j) {
+	    if (query_job->srv[j].priority < query_job->srv[min].priority)
+		min = j;
+	}
+	SWAP(struct srv_target, &query_job->srv[i], &query_job->srv[min]);
+    }
+
+    /* Second pass:
+     *	pick one host among hosts with the same priority, according
+     *	to its weight. The idea is when one server fails, client should
+     *	contact the next server with higher priority rather than contacting
+     *	server with the same priority as the failed one.
+     *
+     *  The algorithm for selecting server among servers with the same
+     *  priority is described in RFC 2782.
+     */
+    for (i=0; i<query_job->srv_cnt; ++i) {
+	unsigned j, count=1, sum;
+
+	/* Calculate running sum for servers with the same priority */
+	sum = query_job->srv[i].sum = query_job->srv[i].weight;
+	for (j=i+1; j<query_job->srv_cnt && 
+		    query_job->srv[j].priority == query_job->srv[i].priority; ++j)
+	{
+	    sum += query_job->srv[j].weight;
+	    query_job->srv[j].sum = sum;
+	    ++count;
+	}
+
+	if (count > 1) {
+	    unsigned r;
+
+	    /* Elect one random number between zero and the total sum of
+	     * weight (inclusive).
+	     */
+	    r = pj_rand() % (sum + 1);
+
+	    /* Select the first server which running sum is greater than or
+	     * equal to the random number.
+	     */
+	    for (j=i; j<i+count; ++j) {
+		if (query_job->srv[j].sum >= r)
+		    break;
+	    }
+
+	    /* Must have selected one! */
+	    pj_assert(j != i+count);
+
+	    /* Put this entry in front (of entries with same priority) */
+	    SWAP(struct srv_target, &query_job->srv[i], &query_job->srv[j]);
+
+	    /* Remove all other entries (of the same priority) */
+	    while (count > 1) {
+		pj_array_erase(query_job->srv, sizeof(struct srv_target), 
+			       query_job->srv_cnt, i+1);
+		--count;
+		--query_job->srv_cnt;
+	    }
+	}
+    }
+
+    /* Since we've been moving around SRV entries, update the pointers
+     * in target_name.
+     */
+    for (i=0; i<query_job->srv_cnt; ++i) {
+	query_job->srv[i].target_name.ptr = query_job->srv[i].target_buf;
+    }
+
+    /* Check for Additional Info section if A records are available, and
+     * fill in the IP address (so that we won't need to resolve the A 
+     * record with another DNS query_job). 
+     */
+    for (i=0; i<response->hdr.arcount; ++i) {
+	pj_dns_parsed_rr *rr = &response->arr[i];
+	unsigned j;
+
+	if (rr->type != PJ_DNS_TYPE_A)
+	    continue;
+
+	/* Yippeaiyee!! There is an "A" record! 
+	 * Update the IP address of the corresponding SRV record.
+	 */
+	for (j=0; j<query_job->srv_cnt; ++j) {
+	    if (pj_stricmp(&rr->name, &query_job->srv[j].target_name)==0) {
+		unsigned cnt = query_job->srv[j].addr_cnt;
+		query_job->srv[j].addr[cnt].s_addr = rr->rdata.a.ip_addr.s_addr;
+		++query_job->srv[j].addr_cnt;
+		++query_job->host_resolved;
+		break;
+	    }
+	}
+
+	/* Not valid message; SRV entry might have been deleted in
+	 * server selection process.
+	 */
+	/*
+	if (j == query_job->srv_cnt) {
+	    PJ_LOG(4,(query_job->objname, 
+		      "Received DNS SRV answer with A record, but "
+		      "couldn't find matching name (name=%.*s)",
+		      (int)rr->name.slen,
+		      rr->name.ptr));
+	}
+	*/
+    }
+
+    /* Rescan again the name specified in the SRV record to see if IP
+     * address is specified as the target name (unlikely, but well, who 
+     * knows..).
+     */
+    for (i=0; i<query_job->srv_cnt; ++i) {
+	pj_in_addr addr;
+
+	if (query_job->srv[i].addr_cnt != 0) {
+	    /* IP address already resolved */
+	    continue;
+	}
+
+	if (pj_inet_aton(&query_job->srv[i].target_name, &addr) != 0) {
+	    query_job->srv[i].addr[query_job->srv[i].addr_cnt++] = addr;
+	    ++query_job->host_resolved;
+	}
+    }
+
+    /* Print resolved entries to the log */
+    PJ_LOG(5,(query_job->objname, 
+	      "SRV query_job for %.*s completed, "
+	      "%d of %d total entries selected%c",
+	      (int)query_job->full_name.slen,
+	      query_job->full_name.ptr,
+	      query_job->srv_cnt,
+	      response->hdr.anscount,
+	      (query_job->srv_cnt ? ':' : ' ')));
+
+    for (i=0; i<query_job->srv_cnt; ++i) {
+	const char *addr;
+
+	if (query_job->srv[i].addr_cnt != 0)
+	    addr = pj_inet_ntoa(query_job->srv[i].addr[0]);
+	else
+	    addr = "-";
+
+	PJ_LOG(5,(query_job->objname, 
+		  " %d: SRV %d %d %d %.*s (%s)",
+		  i, query_job->srv[i].priority, 
+		  query_job->srv[i].weight, 
+		  query_job->srv[i].port, 
+		  (int)query_job->srv[i].target_name.slen, 
+		  query_job->srv[i].target_name.ptr,
+		  addr));
+    }
+}
+
+
+/* Start DNS A record queries for all SRV records in the query_job structure */
+static pj_status_t resolve_hostnames(pj_dns_srv_resolver_job *query_job)
+{
+    unsigned i;
+    pj_status_t err=PJ_SUCCESS, status;
+
+    query_job->dns_state = PJ_DNS_TYPE_A;
+    for (i=0; i<query_job->srv_cnt; ++i) {
+	PJ_LOG(5, (query_job->objname, 
+		   "Starting async DNS A query_job for %.*s",
+		   (int)query_job->srv[i].target_name.slen, 
+		   query_job->srv[i].target_name.ptr));
+
+	status = pj_dns_resolver_start_query(query_job->resolver,
+					     &query_job->srv[i].target_name,
+					     PJ_DNS_TYPE_A, 0,
+					     &dns_callback,
+					     query_job, NULL);
+	if (status != PJ_SUCCESS) {
+	    query_job->host_resolved++;
+	    err = status;
+	}
+    }
+    
+    return (query_job->host_resolved == query_job->srv_cnt) ? err : PJ_SUCCESS;
+}
+
+/* 
+ * This callback is called by PJLIB-UTIL DNS resolver when asynchronous
+ * query_job has completed (successfully or with error).
+ */
+static void dns_callback(void *user_data,
+			 pj_status_t status,
+			 pj_dns_parsed_packet *pkt)
+{
+    pj_dns_srv_resolver_job *query_job = user_data;
+    unsigned i;
+
+    /* Proceed to next stage */
+
+    if (query_job->dns_state == PJ_DNS_TYPE_SRV) {
+
+	/* We are getting SRV response */
+
+	if (status == PJ_SUCCESS && pkt->hdr.anscount != 0) {
+	    /* Got SRV response, build server entry. If A records are available
+	     * in additional records section of the DNS response, save them too.
+	     */
+	    build_server_entries(query_job, pkt);
+
+	} else if (status != PJ_SUCCESS) {
+	    char errmsg[PJ_ERR_MSG_SIZE];
+
+	    /* Update query_job last error */
+	    query_job->last_error = status;
+
+	    pj_strerror(status, errmsg, sizeof(errmsg));
+	    PJ_LOG(4,(query_job->objname, 
+		      "DNS SRV resolution failed for %.*s: %s", 
+		      (int)query_job->full_name.slen, 
+		      query_job->full_name.ptr,
+		      errmsg));
+
+	    /* Trigger error when fallback is disabled */
+	    if (query_job->fallback_a == PJ_FALSE) {
+		goto on_error;
+	    }
+	}
+
+	/* If we can't build SRV record, assume the original target is
+	 * an A record and resolve with DNS A resolution.
+	 */
+	if (query_job->srv_cnt == 0) {
+	    /* Looks like we aren't getting any SRV responses.
+	     * Resolve the original target as A record by creating a 
+	     * single "dummy" srv record and start the hostname resolution.
+	     */
+	    PJ_LOG(4, (query_job->objname, 
+		       "DNS SRV resolution failed for %.*s, trying "
+		       "resolving A record for %.*s",
+		       (int)query_job->full_name.slen, 
+		       query_job->full_name.ptr,
+		       (int)query_job->domain_part.slen,
+		       query_job->domain_part.ptr));
+
+	    /* Create a "dummy" srv record using the original target */
+	    i = query_job->srv_cnt++;
+	    pj_bzero(&query_job->srv[i], sizeof(query_job->srv[i]));
+	    query_job->srv[i].target_name = query_job->domain_part;
+	    query_job->srv[i].priority = 0;
+	    query_job->srv[i].weight = 0;
+	    query_job->srv[i].port = query_job->def_port;
+	} 
+	
+
+	/* Resolve server hostnames (DNS A record) for hosts which don't have
+	 * A record yet.
+	 */
+	if (query_job->host_resolved != query_job->srv_cnt) {
+	    status = resolve_hostnames(query_job);
+	    if (status != PJ_SUCCESS)
+		goto on_error;
+
+	    /* Must return now. Callback may have been called and query_job
+	     * may have been destroyed.
+	     */
+	    return;
+	}
+
+    } else if (query_job->dns_state == PJ_DNS_TYPE_A) {
+
+	/* Check that we really have answer */
+	if (status==PJ_SUCCESS && pkt->hdr.anscount != 0) {
+	    int ans_idx = -1;
+
+	    /* Find the first DNS A record in the answer while processing
+	     * the CNAME info found in the response.
+	     */
+	    for (i=0; i < pkt->hdr.anscount; ++i) {
+
+		pj_dns_parsed_rr *rr = &pkt->ans[i];
+
+		if (rr->type == PJ_DNS_TYPE_A) {
+
+		    if (ans_idx == -1)
+			ans_idx = i;
+
+		} else if (rr->type == PJ_DNS_TYPE_CNAME) {
+		    /* Find which server entry to be updated with
+		     * the CNAME information.
+		     */
+		    unsigned j;
+		    pj_str_t cname = rr->rdata.cname.name;
+
+		    for (j=0; j<query_job->srv_cnt; ++j) 
+		    {
+			struct srv_target *srv = &query_job->srv[j];
+			if (pj_stricmp(&rr->name, &srv->target_name)==0)
+			{
+			    /* Update CNAME info for this server entry */
+			    srv->cname.ptr = srv->cname_buf;
+			    pj_strncpy(&srv->cname, &cname, 
+				       sizeof(srv->cname_buf));
+			    break;
+			}
+		    }
+		}
+	    }
+
+	    if (ans_idx == -1) {
+		/* There's no DNS A answer! */
+		PJ_LOG(5,(query_job->objname, 
+			  "No DNS A record in response!"));
+		status = PJLIB_UTIL_EDNSNOANSWERREC;
+		goto on_error;
+	    }
+
+	    /* Update IP address of the corresponding hostname or CNAME */
+	    for (i=0; i<query_job->srv_cnt; ++i) {
+		pj_dns_parsed_rr *rr = &pkt->ans[ans_idx];
+		struct srv_target *srv = &query_job->srv[i];
+
+		if (pj_stricmp(&rr->name, &srv->target_name)==0 ||
+		    pj_stricmp(&rr->name, &srv->cname)==0) 
+		{
+		    break;
+		}
+	    }
+
+	    if (i == query_job->srv_cnt) {
+		PJ_LOG(4,(query_job->objname, 
+			  "Received answer to DNS A request with no matching "
+			  "SRV record! The unknown name is %.*s",
+			  (int)pkt->ans[ans_idx].name.slen, 
+			  pkt->ans[ans_idx].name.ptr));
+	    } else {
+		unsigned j;
+
+		query_job->srv[i].addr[query_job->srv[i].addr_cnt++].s_addr =
+		    pkt->ans[ans_idx].rdata.a.ip_addr.s_addr;
+
+		PJ_LOG(5,(query_job->objname, 
+			  "DNS A for %.*s: %s",
+			  (int)query_job->srv[i].target_name.slen, 
+			  query_job->srv[i].target_name.ptr,
+			  pj_inet_ntoa(pkt->ans[ans_idx].rdata.a.ip_addr)));
+
+		/* Check for multiple IP addresses */
+		for (j=ans_idx+1; j<pkt->hdr.anscount && 
+			    query_job->srv[i].addr_cnt < ADDR_MAX_COUNT; ++j)
+		{
+		    query_job->srv[i].addr[query_job->srv[i].addr_cnt++].s_addr = 
+			pkt->ans[j].rdata.a.ip_addr.s_addr;
+
+		    PJ_LOG(5,(query_job->objname, 
+			      "Additional DNS A for %.*s: %s",
+			      (int)query_job->srv[i].target_name.slen, 
+			      query_job->srv[i].target_name.ptr,
+			      pj_inet_ntoa(pkt->ans[j].rdata.a.ip_addr)));
+		}
+	    }
+
+	} else if (status != PJ_SUCCESS) {
+	    char errmsg[PJ_ERR_MSG_SIZE];
+
+	    /* Update last error */
+	    query_job->last_error = status;
+
+	    /* Log error */
+	    pj_strerror(status, errmsg, sizeof(errmsg));
+	    PJ_LOG(4,(query_job->objname, "DNS A record resolution failed: %s", 
+		      errmsg));
+	}
+
+	++query_job->host_resolved;
+
+    } else {
+	pj_assert(!"Unexpected state!");
+	query_job->last_error = status = PJ_EINVALIDOP;
+	goto on_error;
+    }
+
+    /* Check if all hosts have been resolved */
+    if (query_job->host_resolved == query_job->srv_cnt) {
+	/* Got all answers, build server addresses */
+	pj_dns_srv_record svr_addr;
+
+	svr_addr.count = 0;
+	for (i=0; i<query_job->srv_cnt; ++i) {
+	    unsigned j;
+
+	    /* Do we have IP address for this server? */
+	    /* This log is redundant really.
+	    if (query_job->srv[i].addr_cnt == 0) {
+		PJ_LOG(5,(query_job->objname, 
+			  " SRV target %.*s:%d does not have IP address!",
+			  (int)query_job->srv[i].target_name.slen,
+			  query_job->srv[i].target_name.ptr,
+			  query_job->srv[i].port));
+		continue;
+	    }
+	    */
+
+	    for (j=0; j<query_job->srv[i].addr_cnt; ++j) {
+		unsigned idx = svr_addr.count;
+		pj_sockaddr_in *addr;
+
+		svr_addr.entry[idx].priority = query_job->srv[i].priority;
+		svr_addr.entry[idx].weight = query_job->srv[i].weight;
+		svr_addr.entry[idx].addr_len = sizeof(pj_sockaddr_in);
+	     
+		addr = (pj_sockaddr_in*)&svr_addr.entry[idx].addr;
+		pj_bzero(addr, sizeof(pj_sockaddr_in));
+		addr->sin_family = PJ_AF_INET;
+		addr->sin_addr = query_job->srv[i].addr[j];
+		addr->sin_port = pj_htons((pj_uint16_t)query_job->srv[i].port);
+
+		++svr_addr.count;
+	    }
+	}
+
+	PJ_LOG(5,(query_job->objname, 
+		  "Server resolution complete, %d server entry(s) found",
+		  svr_addr.count));
+
+
+	if (svr_addr.count > 0)
+	    status = PJ_SUCCESS;
+	else {
+	    status = query_job->last_error;
+	    if (status == PJ_SUCCESS)
+		status = PJLIB_UTIL_EDNSNOANSWERREC;
+	}
+
+	/* Call the callback */
+	(*query_job->cb)(query_job->token, status, &svr_addr);
+    }
+
+
+    return;
+
+on_error:
+    /* Check for failure */
+    if (status != PJ_SUCCESS) {
+	char errmsg[PJ_ERR_MSG_SIZE];
+	PJ_LOG(4,(query_job->objname, 
+		  "DNS %s record resolution error for '%.*s'."
+		  " Err=%d (%s)",
+		  pj_dns_get_type_name(query_job->dns_state),
+		  (int)query_job->domain_part.slen,
+		  query_job->domain_part.ptr,
+		  status,
+		  pj_strerror(status,errmsg,sizeof(errmsg)).ptr));
+	(*query_job->cb)(query_job->token, status, NULL);
+	return;
+    }
+}
+
+
Index: pjproject/trunk/pjsip-apps/build/sample_debug.dsp
===================================================================
--- pjproject/trunk/pjsip-apps/build/sample_debug.dsp	(revision 1101)
+++ pjproject/trunk/pjsip-apps/build/sample_debug.dsp	(revision 1102)
@@ -43,5 +43,5 @@
 # PROP Target_Dir ""
 # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
-# ADD CPP /nologo /MD /W3 /Zi /O2 /I "../../pjsip/include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../../pjmedia/include" /D PJ_WIN32=1 /D PJ_M_I386=1 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FR /FD /c
+# ADD CPP /nologo /MD /W3 /Zi /O2 /I "../../pjsip/include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../../pjmedia/include" /I "../../pjnath/include" /D PJ_WIN32=1 /D PJ_M_I386=1 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FR /FD /c
 # SUBTRACT CPP /YX
 # ADD BASE RSC /l 0x409 /d "NDEBUG"
@@ -69,5 +69,5 @@
 # PROP Target_Dir ""
 # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
-# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "../../pjsip/include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../../pjmedia/include" /D PJ_WIN32=1 /D PJ_M_I386=1 /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FR /YX /FD /GZ /c
+# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "../../pjsip/include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../../pjmedia/include" /I "../../pjnath/include" /D PJ_WIN32=1 /D PJ_M_I386=1 /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FR /YX /FD /GZ /c
 # ADD BASE RSC /l 0x409 /d "_DEBUG"
 # ADD RSC /l 0x409 /d "_DEBUG"
Index: pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c
===================================================================
--- pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 1101)
+++ pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 1102)
@@ -326,5 +326,5 @@
 	   OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
 	   OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
-	   OPT_NAMESERVER, OPT_STUN_SRV,
+	   OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
 	   OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
 	   OPT_AUTO_ANSWER, OPT_AUTO_HANGUP, OPT_AUTO_PLAY, OPT_AUTO_LOOP,
@@ -367,4 +367,5 @@
 	{ "password",	1, 0, OPT_PASSWORD},
 	{ "nameserver", 1, 0, OPT_NAMESERVER},
+	{ "stun-domain",1, 0, OPT_STUN_DOMAIN},
 	{ "stun-srv",   1, 0, OPT_STUN_SRV},
 	{ "add-buddy",  1, 0, OPT_ADD_BUDDY},
@@ -632,6 +633,10 @@
 	    break;
 
+	case OPT_STUN_DOMAIN:   /* STUN domain */
+	    cfg->cfg.stun_domain = pj_str(pj_optarg);
+	    break;
+
 	case OPT_STUN_SRV:   /* STUN server */
-	    cfg->cfg.stun_srv = pj_str(pj_optarg);
+	    cfg->cfg.stun_host = pj_str(pj_optarg);
 	    break;
 
@@ -1084,8 +1089,14 @@
 
     /* STUN */
-    if (config->cfg.stun_srv.slen) {
+    if (config->cfg.stun_domain.slen) {
+	pj_ansi_sprintf(line, "--stun-domain %.*s\n",
+			(int)config->cfg.stun_domain.slen, 
+			config->cfg.stun_domain.ptr);
+	pj_strcat2(&cfg, line);
+    }
+    if (config->cfg.stun_host.slen) {
 	pj_ansi_sprintf(line, "--stun-srv %.*s\n",
-			(int)config->cfg.stun_srv.slen, 
-			config->cfg.stun_srv.ptr);
+			(int)config->cfg.stun_host.slen, 
+			config->cfg.stun_host.ptr);
 	pj_strcat2(&cfg, line);
     }
Index: pjproject/trunk/pjsip/build/test_pjsip.dsp
===================================================================
--- pjproject/trunk/pjsip/build/test_pjsip.dsp	(revision 1101)
+++ pjproject/trunk/pjsip/build/test_pjsip.dsp	(revision 1102)
@@ -43,5 +43,5 @@
 # PROP Target_Dir ""
 # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
-# ADD CPP /nologo /MD /W3 /Zi /O2 /Ob2 /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "PJ_WIN32" /D "PJ_M_I386" /FR /FD /c
+# ADD CPP /nologo /MD /W3 /Zi /O2 /Ob2 /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../../pjnath/include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "PJ_WIN32" /D "PJ_M_I386" /FR /FD /c
 # SUBTRACT CPP /YX
 # ADD BASE RSC /l 0x409 /d "NDEBUG"
@@ -68,5 +68,5 @@
 # PROP Target_Dir ""
 # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
-# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "PJ_WIN32" /D "PJ_M_I386" /FR /FD /GZ /c
+# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../../pjnath/include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "PJ_WIN32" /D "PJ_M_I386" /FR /FD /GZ /c
 # SUBTRACT CPP /YX
 # ADD BASE RSC /l 0x409 /d "_DEBUG"
Index: pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h
===================================================================
--- pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 1101)
+++ pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 1102)
@@ -904,10 +904,22 @@
 
     /**
-     * Specify STUN server. This server will be first resolved with DNS SRV
-     * to get the actual server address. If DNS SRV resolution failed, or
-     * when nameserver is not configured, the server will be resolved using
-     * DNS A resolution (i.e. gethostbyname()).
-     */
-    pj_str_t	    stun_srv;
+     * Specify domain name to be resolved with DNS SRV resolution to get the
+     * address of the STUN servers. Alternatively application may specify
+     * \a stun_host and \a stun_relay_host instead.
+     *
+     * If DNS SRV resolution failed for this domain, then DNS A resolution
+     * will be performed only if \a stun_host is specified.
+     */
+    pj_str_t	    stun_domain;
+
+    /**
+     * Specify STUN server to be used.
+     */
+    pj_str_t	    stun_host;
+
+    /**
+     * Specify STUN relay server to be used.
+     */
+    pj_str_t	    stun_relay_host;
 
     /** 
@@ -1010,5 +1022,7 @@
 
     pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent);
-    pj_strdup_with_null(pool, &dst->stun_srv, &src->stun_srv);
+    pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain);
+    pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host);
+    pj_strdup_with_null(pool, &dst->stun_relay_host, &src->stun_relay_host);
 }
 
Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 1101)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 1102)
@@ -653,4 +653,73 @@
 }
 
+
+static pj_status_t resolve_stun_server(pj_bool_t use_dns_srv);
+
+/*
+ * Callback function to receive notification from the resolver
+ * when the resolution process completes.
+ */
+static void stun_dns_srv_resolver_cb(void *user_data,
+				     pj_status_t status,
+				     const pj_dns_srv_record *rec)
+{
+    unsigned i;
+
+    PJ_UNUSED_ARG(user_data);
+
+    pjsua_var.stun_status = status;
+    
+    if (status != PJ_SUCCESS) {
+	/* DNS SRV resolution failed. If stun_host is specified, resolve
+	 * it with gethostbyname()
+	 */
+	if (pjsua_var.ua_cfg.stun_host.slen) {
+	    pj_hostent he;
+
+	    pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he);
+
+	    if (pjsua_var.stun_status == PJ_SUCCESS) {
+		pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
+		pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
+		pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478);
+
+		PJ_LOG(3,(THIS_FILE, 
+			  "STUN server %.*s resolved, address is %s:%d",
+			  (int)pjsua_var.ua_cfg.stun_host.slen,
+			  pjsua_var.ua_cfg.stun_host.ptr,
+			  pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
+			  (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
+	    }
+	} else {
+	    char errmsg[PJ_ERR_MSG_SIZE];
+
+	    pj_strerror(status, errmsg, sizeof(errmsg));
+	    PJ_LOG(1,(THIS_FILE, 
+		     "DNS SRV resolution failed for STUN server %.*s: %s",
+		     (int)pjsua_var.ua_cfg.stun_domain.slen,
+		     pjsua_var.ua_cfg.stun_domain.ptr,
+		     errmsg));
+	}
+	return;
+    }
+
+    pj_memcpy(&pjsua_var.stun_srv, &rec->entry[0].addr, 
+	      rec->entry[0].addr_len);
+
+    PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
+	      (int)pjsua_var.ua_cfg.stun_domain.slen,
+	      pjsua_var.ua_cfg.stun_domain.ptr,
+	      rec->count));
+
+    for (i=0; i<rec->count; ++i) {
+	PJ_LOG(3,(THIS_FILE, 
+		  " %d: prio=%d, weight=%d  %s:%d", 
+		  i, rec->entry[i].priority, rec->entry[i].weight,
+		  pj_inet_ntoa(rec->entry[i].addr.ipv4.sin_addr),
+		  (int)pj_ntohs(rec->entry[i].addr.ipv4.sin_port)));
+    }
+
+}
+
 /*
  * Resolve STUN server.
@@ -665,13 +734,38 @@
 
 	/* Start STUN server resolution */
-	/* For now just do DNS A resolution */
-
-	if (pjsua_var.ua_cfg.stun_srv.slen == 0) {
-	    pjsua_var.stun_status = PJ_SUCCESS;
-	} else {
+	
+	pjsua_var.stun_status = PJ_EPENDING;
+
+	/* If stun_domain is specified, resolve STUN servers with DNS
+	 * SRV resolution.
+	 */
+	if (pjsua_var.ua_cfg.stun_domain.slen) {
+	    pj_str_t res_type;
+
+	    /* Fail if resolver is not configured */
+	    if (pjsua_var.resolver == NULL) {
+		PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
+				     "stun_domain is specified"));
+		pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
+		return PJLIB_UTIL_EDNSNONS;
+	    }
+	    res_type = pj_str("_stun._udp");
+	    pjsua_var.stun_status = 
+		pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
+				   3478, pjsua_var.pool, pjsua_var.resolver,
+				   0, NULL, stun_dns_srv_resolver_cb);
+	    if (pjsua_var.stun_status != PJ_SUCCESS) {
+		pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution", 
+			     pjsua_var.stun_status);
+		return pjsua_var.stun_status;
+	    }
+	}
+	/* Otherwise if stun_host is specified, resolve STUN server with
+	 * gethostbyname().
+	 */
+	else if (pjsua_var.ua_cfg.stun_host.slen) {
 	    pj_hostent he;
 
-	    pjsua_var.stun_status = 
-		pj_gethostbyname(&pjsua_var.ua_cfg.stun_srv, &he);
+	    pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he);
 
 	    if (pjsua_var.stun_status == PJ_SUCCESS) {
@@ -680,12 +774,19 @@
 		pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478);
 
-		PJ_LOG(4,(THIS_FILE, 
+		PJ_LOG(3,(THIS_FILE, 
 			  "STUN server %.*s resolved, address is %s:%d",
-			  (int)pjsua_var.ua_cfg.stun_srv.slen,
-			  pjsua_var.ua_cfg.stun_srv.ptr,
+			  (int)pjsua_var.ua_cfg.stun_host.slen,
+			  pjsua_var.ua_cfg.stun_host.ptr,
 			  pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
 			  (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
 	    }
-	}
+
+	}
+	/* Otherwise disable STUN. */
+	else {
+	    pjsua_var.stun_status = PJ_SUCCESS;
+	}
+
+
 	return pjsua_var.stun_status;
 
@@ -694,6 +795,10 @@
 	 * result.
 	 */
-	pj_assert(!"Should not happen");
-	return PJ_EBUG;
+	if (wait) {
+	    while (pjsua_var.stun_status == PJ_EPENDING)
+		pjsua_handle_events(10);
+	}
+
+	return pjsua_var.stun_status;
 
     } else {
@@ -947,5 +1052,5 @@
 				         p_pub_addr);
 	if (status != PJ_SUCCESS) {
-	    pjsua_perror(THIS_FILE, "Error resolving with STUN", status);
+	    pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
 	    pj_sock_close(sock);
 	    return status;
