Index: /pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c
===================================================================
--- /pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c	(revision 3043)
+++ /pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c	(revision 3044)
@@ -74,16 +74,26 @@
     /* Check if nodename is IP address */
     pj_bzero(&ai[0], sizeof(ai[0]));
-    ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
-    if (pj_inet_pton(af, nodename, pj_sockaddr_get_addr(&ai[0].ai_addr)) 
-	 == PJ_SUCCESS) 
-    {
-	pj_str_t tmp;
-
-	tmp.ptr = ai[0].ai_canonname;
-	pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME);
-	ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
-	*count = 1;
-
-	return PJ_SUCCESS;
+    if (af == PJ_AF_UNSPEC) {
+	if (pj_inet_pton(PJ_AF_INET, nodename, 
+			 &ai[0].ai_addr.ipv4.sin_addr) == PJ_SUCCESS)
+	{
+	    af = PJ_AF_INET;
+	}
+	else if (pj_inet_pton(PJ_AF_INET6, nodename, 
+			      &ai[0].ai_addr.ipv6.sin6_addr) == PJ_SUCCESS)
+	{
+	    af = PJ_AF_INET6;
+	}
+
+	if (af != PJ_AF_UNSPEC) {
+	    pj_str_t tmp;
+
+	    tmp.ptr = ai[0].ai_canonname;
+	    pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME);
+	    ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
+	    *count = 1;
+
+	    return PJ_SUCCESS;
+	}
     }
 
@@ -122,4 +132,5 @@
 	PJ_ASSERT_ON_FAIL(res->ai_addrlen <= sizeof(pj_sockaddr), continue);
 	pj_memcpy(&ai[i].ai_addr, res->ai_addr, res->ai_addrlen);
+	PJ_SOCKADDR_RESET_LEN(&ai[i].ai_addr);
 
 	/* Next slot */
@@ -140,16 +151,26 @@
     /* Check if nodename is IP address */
     pj_bzero(&ai[0], sizeof(ai[0]));
-    ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
-    if (pj_inet_pton(af, nodename, pj_sockaddr_get_addr(&ai[0].ai_addr)) 
-	 == PJ_SUCCESS) 
-    {
-	pj_str_t tmp;
-
-	tmp.ptr = ai[0].ai_canonname;
-	pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME);
-	ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
-	*count = 1;
-
-	return PJ_SUCCESS;
+    if (af == PJ_AF_UNSPEC) {
+	if (pj_inet_pton(PJ_AF_INET, nodename, 
+			 &ai[0].ai_addr.ipv4.sin_addr) == PJ_SUCCESS)
+	{
+	    af = PJ_AF_INET;
+	}
+	else if (pj_inet_pton(PJ_AF_INET6, nodename, 
+			      &ai[0].ai_addr.ipv6.sin6_addr) == PJ_SUCCESS)
+	{
+	    af = PJ_AF_INET6;
+	}
+
+	if (af != PJ_AF_UNSPEC) {
+	    pj_str_t tmp;
+
+	    tmp.ptr = ai[0].ai_canonname;
+	    pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME);
+	    ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
+	    *count = 1;
+
+	    return PJ_SUCCESS;
+	}
     }
 
@@ -181,4 +202,5 @@
 	    pj_memcpy(&ai[*count].ai_addr.ipv4.sin_addr,
 		      he.h_addr_list[i], he.h_length);
+	    PJ_SOCKADDR_RESET_LEN(&ai[*count].ai_addr);
 
 	    (*count)++;
Index: /pjproject/trunk/pjlib/src/pj/ip_helper_generic.c
===================================================================
--- /pjproject/trunk/pjlib/src/pj/ip_helper_generic.c	(revision 3043)
+++ /pjproject/trunk/pjlib/src/pj/ip_helper_generic.c	(revision 3044)
@@ -122,4 +122,5 @@
 	pj_bzero(&ifs[*p_cnt], sizeof(ifs[0]));
 	pj_memcpy(&ifs[*p_cnt], ad, pj_sockaddr_get_len(ad));
+	PJ_SOCKADDR_RESET_LEN(&ifs[*p_cnt]);
 	(*p_cnt)++;
     }
@@ -216,4 +217,5 @@
 	pj_bzero(&ifs[*p_cnt], sizeof(ifs[0]));
 	pj_memcpy(&ifs[*p_cnt], ad, pj_sockaddr_get_len(ad));
+	PJ_SOCKADDR_RESET_LEN(&ifs[*p_cnt]);
 	(*p_cnt)++;
     }
@@ -306,4 +308,5 @@
 	pj_bzero(&ifs[*p_cnt], sizeof(ifs[0]));
 	pj_memcpy(&ifs[*p_cnt], ad, pj_sockaddr_get_len(ad));
+	PJ_SOCKADDR_RESET_LEN(&ifs[*p_cnt]);
 	(*p_cnt)++;
     }
Index: /pjproject/trunk/pjlib/src/pjlib-test/sock.c
===================================================================
--- /pjproject/trunk/pjlib/src/pjlib-test/sock.c	(revision 3043)
+++ /pjproject/trunk/pjlib/src/pjlib-test/sock.c	(revision 3044)
@@ -72,4 +72,16 @@
 static char bigdata[BIG_DATA_LEN];
 static char bigbuffer[BIG_DATA_LEN];
+
+/* Macro for checking the value of "sin_len" member of sockaddr
+ * (it must always be zero).
+ */
+#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
+#   define CHECK_SA_ZERO_LEN(addr, ret) \
+	if (((pj_addr_hdr*)(addr))->sa_zero_len != 0) \
+	    return ret
+#else
+#   define CHECK_SA_ZERO_LEN(addr, ret)
+#endif
+
 
 static int format_test(void)
@@ -285,4 +297,7 @@
 	}
 
+	/* Check "sin_len" member of parse result */
+	CHECK_SA_ZERO_LEN(&addr, -20);
+
 	/* Build the correct result */
 	status = pj_sockaddr_init(valid_tests[i].result_af,
@@ -312,4 +327,7 @@
 	    return -50;
 	}
+
+	/* Check "sin_len" member of parse result */
+	CHECK_SA_ZERO_LEN(&addr, -55);
 
 	/* Compare the result again */
@@ -348,4 +366,66 @@
 	}
     }
+
+    return 0;
+}
+
+static int purity_test(void)
+{
+    PJ_LOG(3,("test", "...purity_test()"));
+
+#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
+    /* Check on "sin_len" member of sockaddr */
+    {
+	const pj_str_t str_ip = {"1.1.1.1", 7};
+	pj_sockaddr addr[16];
+	pj_addrinfo ai[16];
+	unsigned cnt;
+	pj_status_t rc;
+
+	/* pj_enum_ip_interface() */
+	cnt = PJ_ARRAY_SIZE(addr);
+	rc = pj_enum_ip_interface(pj_AF_UNSPEC(), &cnt, addr);
+	if (rc == PJ_SUCCESS) {
+	    while (cnt--)
+		CHECK_SA_ZERO_LEN(&addr[cnt], -10);
+	}
+
+	/* pj_gethostip() on IPv4 */
+	rc = pj_gethostip(pj_AF_INET(), &addr[0]);
+	if (rc == PJ_SUCCESS)
+	    CHECK_SA_ZERO_LEN(&addr[0], -20);
+
+	/* pj_gethostip() on IPv6 */
+	rc = pj_gethostip(pj_AF_INET6(), &addr[0]);
+	if (rc == PJ_SUCCESS)
+	    CHECK_SA_ZERO_LEN(&addr[0], -30);
+
+	/* pj_getdefaultipinterface() on IPv4 */
+	rc = pj_getdefaultipinterface(pj_AF_INET(), &addr[0]);
+	if (rc == PJ_SUCCESS)
+	    CHECK_SA_ZERO_LEN(&addr[0], -40);
+
+	/* pj_getdefaultipinterface() on IPv6 */
+	rc = pj_getdefaultipinterface(pj_AF_INET6(), &addr[0]);
+	if (rc == PJ_SUCCESS)
+	    CHECK_SA_ZERO_LEN(&addr[0], -50);
+
+	/* pj_getaddrinfo() on a host name */
+	cnt = PJ_ARRAY_SIZE(ai);
+	rc = pj_getaddrinfo(pj_AF_UNSPEC(), pj_gethostname(), &cnt, ai);
+	if (rc == PJ_SUCCESS) {
+	    while (cnt--)
+		CHECK_SA_ZERO_LEN(&ai[cnt].ai_addr, -60);
+	}
+
+	/* pj_getaddrinfo() on an IP address */
+	cnt = PJ_ARRAY_SIZE(ai);
+	rc = pj_getaddrinfo(pj_AF_UNSPEC(), &str_ip, &cnt, ai);
+	if (rc == PJ_SUCCESS) {
+	    pj_assert(cnt == 1);
+	    CHECK_SA_ZERO_LEN(&ai[0].ai_addr, -70);
+	}
+    }
+#endif
 
     return 0;
@@ -761,4 +841,8 @@
 	return rc;
 
+    rc = purity_test();
+    if (rc != 0)
+	return rc;
+
     rc = gethostbyname_test();
     if (rc != 0)
