Index: pjproject/trunk/pjlib/include/pj/addr_resolv.h
===================================================================
--- pjproject/trunk/pjlib/include/pj/addr_resolv.h	(revision 1598)
+++ pjproject/trunk/pjlib/include/pj/addr_resolv.h	(revision 1599)
@@ -107,4 +107,16 @@
 
 /**
+ * Get the IP address of the default interface. Default interface is the
+ * interface of the default route.
+ *
+ * @param ip_addr   On successful resolution, this will be filled up with
+ *		    the IP address, in network byte order.
+ *
+ * @return	    PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pj_getdefaultipinterface(pj_in_addr *ip_addr);
+
+
+/**
  * This function translates the name of a service location (for example, 
  * a host name) and returns a set of addresses and associated information
Index: pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c	(revision 1598)
+++ pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c	(revision 1599)
@@ -21,4 +21,5 @@
 #include <pj/string.h>
 #include <pj/errno.h>
+#include <pj/ip_helper.h>
 #include <pj/compat/socket.h>
 
@@ -55,4 +56,43 @@
 }
 
+/* Get the default IP interface */
+PJ_DEF(pj_status_t) pj_getdefaultipinterface(pj_in_addr *addr)
+{
+    pj_sock_t fd;
+    pj_str_t cp;
+    pj_sockaddr_in a;
+    int len;
+    pj_status_t status;
+
+    status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &fd);
+    if (status != PJ_SUCCESS) {
+	return status;
+    }
+
+    cp = pj_str("1.1.1.1");
+    pj_sockaddr_in_init(&a, &cp, 53);
+
+    status = pj_sock_connect(fd, &a, sizeof(a));
+    if (status != PJ_SUCCESS) {
+	pj_sock_close(fd);
+	return status;
+    }
+
+    len = sizeof(a);
+    status = pj_sock_getsockname(fd, &a, &len);
+    if (status != PJ_SUCCESS) {
+	pj_sock_close(fd);
+	return status;
+    }
+
+    pj_sock_close(fd);
+
+    *addr = a.sin_addr;
+
+    /* Success */
+    return PJ_SUCCESS;
+}
+
+
 /* Resolve the IP address of local machine */
 PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr)
@@ -81,35 +121,21 @@
 	addr->s_addr == 0) 
     {
-	pj_sock_t fd;
-	pj_str_t cp;
-	pj_sockaddr_in a;
-	int len;
-
-	status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &fd);
-	if (status != PJ_SUCCESS) {
-	    return status;
+	status = pj_getdefaultipinterface(addr);
+    }
+
+    /* As the last resort, get the first available interface */
+    if (status != PJ_SUCCESS) {
+	pj_in_addr addrs[2];
+	unsigned count = PJ_ARRAY_SIZE(addrs);
+
+	status = pj_enum_ip_interface(&count, addrs);
+	if (status == PJ_SUCCESS) {
+	    if (count != 0) {
+		*addr = addrs[0];
+	    } else {
+		/* Just return 127.0.0.1 */
+		addr->s_addr = pj_htonl (0x7f000001);
+	    }
 	}
-
-	cp = pj_str("1.1.1.1");
-	pj_sockaddr_in_init(&a, &cp, 53);
-
-	status = pj_sock_connect(fd, &a, sizeof(a));
-	if (status != PJ_SUCCESS) {
-	    pj_sock_close(fd);
-	    /* Return 127.0.0.1 as the address */
-	    return PJ_SUCCESS;
-	}
-
-	len = sizeof(a);
-	status = pj_sock_getsockname(fd, &a, &len);
-	if (status != PJ_SUCCESS) {
-	    pj_sock_close(fd);
-	    /* Return 127.0.0.1 as the address */
-	    return PJ_SUCCESS;
-	}
-
-	pj_sock_close(fd);
-
-	*addr = a.sin_addr;
     }
 
Index: pjproject/trunk/pjlib/src/pj/addr_resolv_symbian.cpp
===================================================================
--- pjproject/trunk/pjlib/src/pj/addr_resolv_symbian.cpp	(revision 1598)
+++ pjproject/trunk/pjlib/src/pj/addr_resolv_symbian.cpp	(revision 1599)
@@ -20,4 +20,6 @@
 #include <pj/assert.h>
 #include <pj/errno.h>
+#include <pj/ip_helper.h>
+#include <pj/sock.h>
 #include <pj/string.h>
 #include <pj/unicode.h>
@@ -85,4 +87,43 @@
 
 
+/* Get the default IP interface */
+PJ_DEF(pj_status_t) pj_getdefaultipinterface(pj_in_addr *addr)
+{
+    pj_sock_t fd;
+    pj_str_t cp;
+    pj_sockaddr_in a;
+    int len;
+    pj_status_t status;
+
+    status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &fd);
+    if (status != PJ_SUCCESS) {
+	return status;
+    }
+
+    cp = pj_str("1.1.1.1");
+    pj_sockaddr_in_init(&a, &cp, 53);
+
+    status = pj_sock_connect(fd, &a, sizeof(a));
+    if (status != PJ_SUCCESS) {
+	pj_sock_close(fd);
+	return status;
+    }
+
+    len = sizeof(a);
+    status = pj_sock_getsockname(fd, &a, &len);
+    if (status != PJ_SUCCESS) {
+	pj_sock_close(fd);
+	return status;
+    }
+
+    pj_sock_close(fd);
+
+    *addr = a.sin_addr;
+
+    /* Success */
+    return PJ_SUCCESS;
+}
+
+
 /* Resolve the IP address of local machine */
 PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr)
@@ -90,10 +131,6 @@
     const pj_str_t *hostname = pj_gethostname();
     struct pj_hostent he;
-    pj_str_t cp;
-    pj_in_addr loopip;
     pj_status_t status;
 
-    cp = pj_str("127.0.0.1");
-    loopip = pj_inet_addr(&cp);
 
     /* Try with resolving local hostname first */
@@ -104,40 +141,27 @@
 
 
-    /* If we end up with 127.0.0.1 or 0.0.0.0, resolve the IP by getting 
-     * the default interface to connect to some public host.
+    /* If we end up with 127.x.x.x, resolve the IP by getting the default
+     * interface to connect to some public host.
      */
-    if (status!=PJ_SUCCESS || addr->s_addr == loopip.s_addr || !addr->s_addr) {
-	pj_sock_t fd;
-	pj_sockaddr_in a;
-	int len;
+    if (status != PJ_SUCCESS || (pj_ntohl(addr->s_addr) >> 24)==127 ||
+	addr->s_addr == 0) 
+    {
+	status = pj_getdefaultipinterface(addr);
+    }
 
-	status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &fd);
-	if (status != PJ_SUCCESS) {
-	    return status;
+    /* As the last resort, get the first available interface */
+    if (status != PJ_SUCCESS) {
+	pj_in_addr addrs[2];
+	unsigned count = PJ_ARRAY_SIZE(addrs);
+
+	status = pj_enum_ip_interface(&count, addrs);
+	if (status == PJ_SUCCESS) {
+	    if (count != 0) {
+		*addr = addrs[0];
+	    } else {
+		/* Just return 127.0.0.1 */
+		addr->s_addr = pj_htonl(0x7f000001);
+	    }
 	}
-
-	cp = pj_str("1.1.1.1");
-	pj_sockaddr_in_init(&a, &cp, 53);
-
-	status = pj_sock_connect(fd, &a, sizeof(a));
-	if (status != PJ_SUCCESS) {
-	    pj_sock_close(fd);
-	    return status;
-	}
-
-	len = sizeof(a);
-	status = pj_sock_getsockname(fd, &a, &len);
-	if (status != PJ_SUCCESS || a.sin_addr.s_addr==0) {
-	    pj_sock_close(fd);
-	    /* May return 127.0.0.1 */
-	    return status;
-	}
-
-	pj_sock_close(fd);
-	
-	*addr = a.sin_addr;
-
-	if (a.sin_addr.s_addr == 0)
-	    return PJ_ENOTFOUND;
     }
 
@@ -146,3 +170,2 @@
 
 
-
Index: pjproject/trunk/pjlib/src/pj/ip_helper_generic.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/ip_helper_generic.c	(revision 1598)
+++ pjproject/trunk/pjlib/src/pj/ip_helper_generic.c	(revision 1599)
@@ -34,5 +34,5 @@
 
     /* Just get one default route */
-    status = pj_gethostip(&ifs[0]);
+    status = pj_getdefaultipinterface(&ifs[0]);
     if (status != PJ_SUCCESS)
 	return status;
@@ -112,5 +112,5 @@
 
     /* Just get one default route */
-    status = pj_gethostip(&routes[0].ipv4.if_addr);
+    status = pj_getdefaultipinterface(&routes[0].ipv4.if_addr);
     if (status != PJ_SUCCESS)
 	return status;
