Ticket #382: Updated ICE from draft-ietf-mmusic-ice-14 to ice-18 specification, and also done some other tweaks as well

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@1450 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjlib/include/pj/doxygen.h b/pjlib/include/pj/doxygen.h
index fa09c40..f65066f 100644
--- a/pjlib/include/pj/doxygen.h
+++ b/pjlib/include/pj/doxygen.h
@@ -55,7 +55,7 @@
  *
  * @subsection doc_ver_subsec Version
  *
- * This document corresponds to PJLIB version 0.5.10.
+ * This document corresponds to PJLIB version 0.7.0-trunk.
  *
  *
  * @subsection find_samples_subsec How to Read This Document
diff --git a/pjlib/src/pj/ip_helper_win32.c b/pjlib/src/pj/ip_helper_win32.c
index 69ff16a..1a937d7 100644
--- a/pjlib/src/pj/ip_helper_win32.c
+++ b/pjlib/src/pj/ip_helper_win32.c
@@ -34,16 +34,22 @@
 #include <pj/errno.h>
 #include <pj/string.h>
 
+#ifndef PJ_IP_HELPER_IGNORE_LOOPBACK_IF
+#   define PJ_IP_HELPER_IGNORE_LOOPBACK_IF	1
+#endif
+
 typedef DWORD (WINAPI *PFN_GetIpAddrTable)(PMIB_IPADDRTABLE pIpAddrTable, 
 					   PULONG pdwSize, 
 					   BOOL bOrder);
 typedef DWORD (WINAPI *PFN_GetIpForwardTable)(PMIB_IPFORWARDTABLE pIpForwardTable,
 					      PULONG pdwSize, 
 					      BOOL bOrder);
+typedef DWORD (WINAPI *PFN_GetIfEntry)(PMIB_IFROW pIfRow);
 
 static HANDLE s_hDLL;
 static PFN_GetIpAddrTable s_pfnGetIpAddrTable;
 static PFN_GetIpForwardTable s_pfnGetIpForwardTable;
+static PFN_GetIfEntry s_pfnGetIfEntry;
 
 static void unload_iphlp_module(void)
 {
@@ -85,6 +91,23 @@
 }
 
 
+#if PJ_IP_HELPER_IGNORE_LOOPBACK_IF
+static DWORD MyGetIfEntry(MIB_IFROW *pIfRow)
+{
+    if(NULL == s_pfnGetIfEntry) {
+	s_pfnGetIfEntry = (PFN_GetIfEntry) 
+	    GetIpHlpApiProc(PJ_T("GetIfEntry"));
+    }
+    
+    if(NULL != s_pfnGetIfEntry) {
+	return s_pfnGetIfEntry(pIfRow);
+    }
+    
+    return ERROR_NOT_SUPPORTED;
+}
+#endif
+
+
 static DWORD MyGetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, 
 				 PULONG pdwSize, 
 				 BOOL bOrder)
@@ -134,9 +157,23 @@
     count = (pTab->dwNumEntries < *p_cnt) ? pTab->dwNumEntries : *p_cnt;
     *p_cnt = 0;
     for (i=0; i<count; ++i) {
+	MIB_IFROW ifRow;
+
 	/* Some Windows returns 0.0.0.0! */
 	if (pTab->table[i].dwAddr == 0)
 	    continue;
+
+#if PJ_IP_HELPER_IGNORE_LOOPBACK_IF
+	/* Investigate the type of this interface */
+	pj_bzero(&ifRow, sizeof(ifRow));
+	ifRow.dwIndex = pTab->table[i].dwIndex;
+	if (MyGetIfEntry(&ifRow) != 0)
+	    continue;
+
+	if (ifRow.dwType == MIB_IF_TYPE_LOOPBACK)
+	    continue;
+#endif
+
 	ifs[*p_cnt].s_addr = pTab->table[i].dwAddr;
 	(*p_cnt)++;
     }
diff --git a/pjmedia/include/pjmedia/doxygen.h b/pjmedia/include/pjmedia/doxygen.h
index fc254fd..8d93175 100644
--- a/pjmedia/include/pjmedia/doxygen.h
+++ b/pjmedia/include/pjmedia/doxygen.h
@@ -91,8 +91,8 @@
  * @ref enc_dec_codec,
  * @ref plc_codec, 
  * @ref PJMEDIA_CONF, 
- * @ref PJMED_G711 "G711/G.711 (PCMA/PCMU) codec with PLC",
- * @ref PJMED_GSM "GSM codec with PLC", 
+ * @ref PJMED_G711 "G711/G.711 (PCMA/PCMU) codec",
+ * @ref PJMED_GSM "GSM codec", 
  * @ref PJMED_L16 "linear codecs (multiple clockrate, stereo support, etc)",
  * @ref PJMED_SPEEX "Speex codec (narrowband, wideband, ultra-wideband)",
  * @ref PJMED_JBUF "portable, adaptive jitter buffer with PLC support",
diff --git a/pjnath/include/pjnath/config.h b/pjnath/include/pjnath/config.h
index 25eb5b7..9ae03d2 100644
--- a/pjnath/include/pjnath/config.h
+++ b/pjnath/include/pjnath/config.h
@@ -234,6 +234,16 @@
 #endif
 
 
+/**
+ * This constant specifies the length of random string generated for ICE
+ * ufrag and password.
+ *
+ * Default: 8 (characters)
+ */
+#ifndef PJ_ICE_UFRAG_LEN
+#   define PJ_ICE_UFRAG_LEN			    8
+#endif
+
 
 /**
  * @}
diff --git a/pjnath/include/pjnath/types.h b/pjnath/include/pjnath/types.h
index 0714c4c..31683eb 100644
--- a/pjnath/include/pjnath/types.h
+++ b/pjnath/include/pjnath/types.h
@@ -157,8 +157,8 @@
  * Interactive Connectivity Establishment (ICE) is a standard based 
  * methodology for traversing Network Address Translator (NAT), and
  * is described in 
- * <A HREF="http://www.ietf.org/internet-drafts/draft-ietf-mmusic-ice-14.txt">
- * <B>draft-ietf-mmusic-ice-14.txt</B></A> draft. The PJNATH ICE
+ * <A HREF="http://www.ietf.org/internet-drafts/draft-ietf-mmusic-ice-18.txt">
+ * <B>draft-ietf-mmusic-ice-18.txt</B></A> draft. The PJNATH ICE
  * implementation is aimed to provide a usable and generic ICE transports
  * for different types of application, including but not limited to
  * the usage of ICE in SIP/SDP offer/answer.
diff --git a/pjnath/src/pjnath/ice_session.c b/pjnath/src/pjnath/ice_session.c
index 94e5989..695c66d 100644
--- a/pjnath/src/pjnath/ice_session.c
+++ b/pjnath/src/pjnath/ice_session.c
@@ -214,7 +214,7 @@
 
     /* Create STUN session for this candidate */
     status = pj_stun_session_create(&ice->stun_cfg, NULL, 
-			            &sess_cb, PJ_FALSE,
+			            &sess_cb, PJ_TRUE,
 				    &comp->stun_sess);
     if (status != PJ_SUCCESS)
 	return status;
@@ -296,17 +296,17 @@
     }
 
     if (local_ufrag == NULL) {
-	ice->rx_ufrag.ptr = (char*) pj_pool_alloc(ice->pool, 16);
-	pj_create_random_string(ice->rx_ufrag.ptr, 16);
-	ice->rx_ufrag.slen = 16;
+	ice->rx_ufrag.ptr = (char*) pj_pool_alloc(ice->pool, PJ_ICE_UFRAG_LEN);
+	pj_create_random_string(ice->rx_ufrag.ptr, PJ_ICE_UFRAG_LEN);
+	ice->rx_ufrag.slen = PJ_ICE_UFRAG_LEN;
     } else {
 	pj_strdup(ice->pool, &ice->rx_ufrag, local_ufrag);
     }
 
     if (local_passwd == NULL) {
-	ice->rx_pass.ptr = (char*) pj_pool_alloc(ice->pool, 16);
-	pj_create_random_string(ice->rx_pass.ptr, 16);
-	ice->rx_pass.slen = 16;
+	ice->rx_pass.ptr = (char*) pj_pool_alloc(ice->pool, PJ_ICE_UFRAG_LEN);
+	pj_create_random_string(ice->rx_pass.ptr, PJ_ICE_UFRAG_LEN);
+	ice->rx_pass.slen = PJ_ICE_UFRAG_LEN;
     } else {
 	pj_strdup(ice->pool, &ice->rx_pass, local_passwd);
     }
diff --git a/pjnath/src/pjnath/ice_strans.c b/pjnath/src/pjnath/ice_strans.c
index 6278f9f..f8a20d1 100644
--- a/pjnath/src/pjnath/ice_strans.c
+++ b/pjnath/src/pjnath/ice_strans.c
@@ -590,7 +590,9 @@
 
 
 	/* Send STUN binding request */
-	PJ_LOG(5,(ice_st->obj_name, "Sending STUN keep-alive"));
+	PJ_LOG(5,(ice_st->obj_name, "Sending STUN keep-alive from %s;%d",
+		  pj_inet_ntoa(comp->local_addr.ipv4.sin_addr),
+		  pj_ntohs(comp->local_addr.ipv4.sin_port)));
 	status = pj_stun_session_send_msg(comp->stun_sess, PJ_FALSE, 
 					  &ice_st->stun_srv, 
 					  sizeof(pj_sockaddr_in), tdata);
@@ -1137,15 +1139,20 @@
 	return;
     }
 
+    /* Save IP address for logging */
+    pj_ansi_strcpy(ip, pj_inet_ntoa(comp->local_addr.ipv4.sin_addr));
+
     /* Ignore response if it reports the same address */
-    if (cand->addr.ipv4.sin_addr.s_addr == mapped_addr->ipv4.sin_addr.s_addr &&
-	cand->addr.ipv4.sin_port == mapped_addr->ipv4.sin_port)
+    if (comp->local_addr.ipv4.sin_addr.s_addr == mapped_addr->ipv4.sin_addr.s_addr &&
+	comp->local_addr.ipv4.sin_port == mapped_addr->ipv4.sin_port)
     {
+	PJ_LOG(4,(comp->ice_st->obj_name, 
+		  "Candidate %s:%d is directly connected to Internet, "
+		  "STUN mapped address is ignored",
+		  ip, pj_ntohs(comp->local_addr.ipv4.sin_port)));
 	return;
     }
 
-    pj_ansi_strcpy(ip, pj_inet_ntoa(comp->local_addr.ipv4.sin_addr));
-
     PJ_LOG(4,(comp->ice_st->obj_name, 
 	      "STUN mapped address for %s:%d is %s:%d",
 	      ip, (int)pj_ntohs(comp->local_addr.ipv4.sin_port),
diff --git a/pjnath/src/pjnath/stun_msg_dump.c b/pjnath/src/pjnath/stun_msg_dump.c
index 3745ebe..3ca9a80 100644
--- a/pjnath/src/pjnath/stun_msg_dump.c
+++ b/pjnath/src/pjnath/stun_msg_dump.c
@@ -115,7 +115,7 @@
 
 	    attr = (const pj_stun_uint_attr*)ahdr;
 	    len = pj_ansi_snprintf(p, end-p,
-				   ", value=%d (0x%x)\n",
+				   ", value=%u (0x%x)\n",
 				   (pj_uint32_t)attr->value,
 				   (pj_uint32_t)attr->value);
 	    APPLY();