Index: pjproject/trunk/pjmedia/src/pjmedia/transport_srtp.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/transport_srtp.c	(revision 4723)
+++ pjproject/trunk/pjmedia/src/pjmedia/transport_srtp.c	(revision 4728)
@@ -1,4 +1,4 @@
 /* $Id$ */
-/* 
+/*
  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
@@ -16,5 +16,5 @@
  * 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 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
 
@@ -46,5 +46,5 @@
 #define MAX_KEY_LEN		    128
 
-/* Initial value of probation counter. When probation counter > 0, 
+/* Initial value of probation counter. When probation counter > 0,
  * it means SRTP is in probation state, and it may restart when
  * srtp_unprotect() returns err_status_replay_*
@@ -77,5 +77,5 @@
 
     /* cipher AES_CM, auth HMAC_SHA1, auth tag len = 10 octets */
-    {"AES_CM_128_HMAC_SHA1_80", AES_128_ICM, 30, HMAC_SHA1, 20, 10, 10, 
+    {"AES_CM_128_HMAC_SHA1_80", AES_128_ICM, 30, HMAC_SHA1, 20, 10, 10,
 	sec_serv_conf_and_auth},
 
@@ -84,5 +84,5 @@
 	sec_serv_conf_and_auth},
 
-    /* 
+    /*
      * F8_128_HMAC_SHA1_8 not supported by libsrtp?
      * {"F8_128_HMAC_SHA1_8", NULL_CIPHER, 0, NULL_AUTH, 0, 0, 0, sec_serv_none}
@@ -125,5 +125,5 @@
 				   void *pkt,
 				   pj_ssize_t size);
-        
+
     /* Transport information */
     pjmedia_transport	*member_tp; /**< Underlying transport.       */
@@ -131,10 +131,10 @@
     /* SRTP usage policy of peer. This field is updated when media is starting.
      * This is useful when SRTP is in optional mode and peer is using mandatory
-     * mode, so when local is about to reinvite/update, it should offer 
+     * mode, so when local is about to reinvite/update, it should offer
      * RTP/SAVP instead of offering RTP/AVP.
      */
     pjmedia_srtp_use	 peer_use;
 
-    /* When probation counter > 0, it means SRTP is in probation state, 
+    /* When probation counter > 0, it means SRTP is in probation state,
      * and it may restart when srtp_unprotect() returns err_status_replay_*
      */
@@ -206,5 +206,5 @@
 
 
-static pjmedia_transport_op transport_srtp_op = 
+static pjmedia_transport_op transport_srtp_op =
 {
     &transport_get_info,
@@ -239,15 +239,15 @@
 	"couldn't deallocate properly",	    /* err_status_dealloc_fail  = 4  */
 	"couldn't initialize",		    /* err_status_init_fail     = 5  */
-	"can't process as much data as requested", 
+	"can't process as much data as requested",
 					    /* err_status_terminus      = 6  */
 	"authentication failure",	    /* err_status_auth_fail     = 7  */
 	"cipher failure",		    /* err_status_cipher_fail   = 8  */
 	"replay check failed (bad index)",  /* err_status_replay_fail   = 9  */
-	"replay check failed (index too old)", 
+	"replay check failed (index too old)",
 					    /* err_status_replay_old    = 10 */
 	"algorithm failed test routine",    /* err_status_algo_fail     = 11 */
 	"unsupported operation",	    /* err_status_no_such_op    = 12 */
 	"no appropriate context found",	    /* err_status_no_ctx        = 13 */
-	"unable to perform desired validation", 
+	"unable to perform desired validation",
 					    /* err_status_cant_check    = 14 */
 	"can't use key any more",	    /* err_status_key_expired   = 15 */
@@ -286,6 +286,6 @@
 
 	err = srtp_init();
-	if (err != err_status_ok) { 
-	    PJ_LOG(4, (THIS_FILE, "Failed to initialize libsrtp: %s", 
+	if (err != err_status_ok) {
+	    PJ_LOG(4, (THIS_FILE, "Failed to initialize libsrtp: %s",
 		       get_libsrtp_errstr(err)));
 	    return PJMEDIA_ERRNO_FROM_LIBSRTP(err);
@@ -294,7 +294,7 @@
 	if (pjmedia_endpt_atexit(endpt, pjmedia_srtp_deinit_lib) != PJ_SUCCESS)
 	{
-	    /* There will be memory leak when it fails to schedule libsrtp 
+	    /* There will be memory leak when it fails to schedule libsrtp
 	     * deinitialization, however the memory leak could be harmless,
-	     * since in modern OS's memory used by an application is released 
+	     * since in modern OS's memory used by an application is released
 	     * when the application terminates.
 	     */
@@ -307,5 +307,5 @@
     PJ_UNUSED_ARG(endpt);
 #endif
-    
+
     return PJ_SUCCESS;
 }
@@ -338,5 +338,5 @@
 #endif
     if (err != err_status_ok) {
-	PJ_LOG(4, (THIS_FILE, "Failed to deinitialize libsrtp: %s", 
+	PJ_LOG(4, (THIS_FILE, "Failed to deinitialize libsrtp: %s",
 		   get_libsrtp_errstr(err)));
     }
@@ -350,5 +350,5 @@
     int i;
     int cs_cnt = sizeof(crypto_suites)/sizeof(crypto_suites[0]);
-    
+
     /* treat unspecified crypto_name as crypto 'NULL' */
     if (crypto_name->slen == 0)
@@ -421,5 +421,5 @@
 
     /* Check crypto availability */
-    if (opt && opt->crypto_count == 0 && 
+    if (opt && opt->crypto_count == 0 &&
 	opt->use == PJMEDIA_SRTP_MANDATORY)
 	return PJMEDIA_SRTP_ESDPREQCRYPTO;
@@ -435,6 +435,6 @@
 
 	    /* check key length */
-	    if (opt->crypto[i].key.slen && 
-		opt->crypto[i].key.slen < 
+	    if (opt->crypto[i].key.slen &&
+		opt->crypto[i].key.slen <
 		(pj_ssize_t)crypto_suites[cs_idx].cipher_key_len)
 		return PJMEDIA_SRTP_EINKEYLEN;
@@ -506,5 +506,5 @@
  */
 PJ_DEF(pj_status_t) pjmedia_transport_srtp_start(
-			   pjmedia_transport *tp, 
+			   pjmedia_transport *tp,
 			   const pjmedia_srtp_crypto *tx,
 			   const pjmedia_srtp_crypto *rx)
@@ -518,5 +518,4 @@
     int		     cr_rx_idx = 0;
     int		     au_rx_idx = 0;
-    int		     crypto_suites_cnt;
     pj_status_t	     status = PJ_SUCCESS;
 
@@ -528,6 +527,4 @@
 	pjmedia_transport_srtp_stop(tp);
     }
-
-    crypto_suites_cnt = sizeof(crypto_suites)/sizeof(crypto_suites[0]);
 
     /* Get encryption and authentication method */
@@ -545,5 +542,5 @@
 
     /* Check whether the crypto-suite requested is supported */
-    if (cr_tx_idx == -1 || cr_rx_idx == -1 || au_tx_idx == -1 || 
+    if (cr_tx_idx == -1 || cr_rx_idx == -1 || au_tx_idx == -1 ||
 	au_rx_idx == -1)
     {
@@ -648,5 +645,5 @@
 	else
 	    b64[b64_len] = '\0';
-        
+
 	PJ_LOG(5, (srtp->pool->obj_name, "TX: %s key=%s",
 		   srtp->tx_policy.name.ptr, b64));
@@ -700,5 +697,5 @@
     err = srtp_dealloc(p_srtp->srtp_rx_ctx);
     if (err != err_status_ok) {
-	PJ_LOG(4, (p_srtp->pool->obj_name, 
+	PJ_LOG(4, (p_srtp->pool->obj_name,
 		   "Failed to dealloc RX SRTP context: %s",
 		   get_libsrtp_errstr(err)));
@@ -706,5 +703,5 @@
     err = srtp_dealloc(p_srtp->srtp_tx_ctx);
     if (err != err_status_ok) {
-	PJ_LOG(4, (p_srtp->pool->obj_name, 
+	PJ_LOG(4, (p_srtp->pool->obj_name,
 		   "Failed to dealloc TX SRTP context: %s",
 		   get_libsrtp_errstr(err)));
@@ -753,5 +750,5 @@
     info->spc_info[spc_info_idx].type = PJMEDIA_TRANSPORT_TYPE_SRTP;
     info->spc_info[spc_info_idx].cbsize = sizeof(srtp_info);
-    pj_memcpy(&info->spc_info[spc_info_idx].buffer, &srtp_info, 
+    pj_memcpy(&info->spc_info[spc_info_idx].buffer, &srtp_info,
 	      sizeof(srtp_info));
 
@@ -782,5 +779,5 @@
 
     /* Attach itself to transport */
-    status = pjmedia_transport_attach(srtp->member_tp, srtp, rem_addr, 
+    status = pjmedia_transport_attach(srtp->member_tp, srtp, rem_addr,
 				      rem_rtcp, addr_len, &srtp_rtp_cb,
 				      &srtp_rtcp_cb);
@@ -842,10 +839,10 @@
 
     if (err == err_status_ok) {
-	status = pjmedia_transport_send_rtp(srtp->member_tp, 
+	status = pjmedia_transport_send_rtp(srtp->member_tp,
 					    srtp->rtp_tx_buffer, len);
     } else {
 	status = PJMEDIA_ERRNO_FROM_LIBSRTP(err);
     }
-    
+
     return status;
 }
@@ -870,5 +867,5 @@
 
     if (srtp->bypass_srtp) {
-	return pjmedia_transport_send_rtcp2(srtp->member_tp, addr, addr_len, 
+	return pjmedia_transport_send_rtcp2(srtp->member_tp, addr, addr_len,
 	                                    pkt, size);
     }
@@ -903,5 +900,5 @@
 {
     transport_srtp *srtp = (transport_srtp *) tp;
-    
+
     PJ_ASSERT_RETURN(tp, PJ_EINVAL);
 
@@ -965,11 +962,11 @@
     }
     err = srtp_unprotect(srtp->srtp_rx_ctx, (pj_uint8_t*)pkt, &len);
-    if (srtp->probation_cnt > 0 && 
-	(err == err_status_replay_old || err == err_status_replay_fail)) 
+    if (srtp->probation_cnt > 0 &&
+	(err == err_status_replay_old || err == err_status_replay_fail))
     {
 	/* Handle such condition that stream is updated (RTP seq is reinited
-	 * & SRTP is restarted), but some old packets are still coming 
+	 * & SRTP is restarted), but some old packets are still coming
 	 * so SRTP is learning wrong RTP seq. While the newly inited RTP seq
-	 * comes, SRTP thinks the RTP seq is replayed, so srtp_unprotect() 
+	 * comes, SRTP thinks the RTP seq is replayed, so srtp_unprotect()
 	 * will return err_status_replay_*. Restarting SRTP can resolve this.
 	 */
@@ -982,5 +979,5 @@
 					      &tx, &rx);
 	if (status != PJ_SUCCESS) {
-	    PJ_LOG(5,(srtp->pool->obj_name, "Failed to restart SRTP, err=%s", 
+	    PJ_LOG(5,(srtp->pool->obj_name, "Failed to restart SRTP, err=%s",
 		      get_libsrtp_errstr(err)));
 	} else if (!srtp->bypass_srtp) {
@@ -990,6 +987,6 @@
 
     if (err != err_status_ok) {
-	PJ_LOG(5,(srtp->pool->obj_name, 
-		  "Failed to unprotect SRTP, pkt size=%d, err=%s", 
+	PJ_LOG(5,(srtp->pool->obj_name,
+		  "Failed to unprotect SRTP, pkt size=%d, err=%s",
 		  size, get_libsrtp_errstr(err)));
     } else {
@@ -1036,5 +1033,5 @@
     err = srtp_unprotect_rtcp(srtp->srtp_rx_ctx, (pj_uint8_t*)pkt, &len);
     if (err != err_status_ok) {
-	PJ_LOG(5,(srtp->pool->obj_name, 
+	PJ_LOG(5,(srtp->pool->obj_name,
 		  "Failed to unprotect SRTCP, pkt size=%d, err=%s",
 		  size, get_libsrtp_errstr(err)));
@@ -1056,5 +1053,5 @@
  */
 static pj_status_t generate_crypto_attr_value(pj_pool_t *pool,
-					      char *buffer, int *buffer_len, 
+					      char *buffer, int *buffer_len,
 					      pjmedia_srtp_crypto *crypto,
 					      int tag)
@@ -1088,5 +1085,5 @@
 	    key_ok = PJ_TRUE;
 
-	    err = crypto_get_random((unsigned char*)key, 
+	    err = crypto_get_random((unsigned char*)key,
 				     crypto_suites[cs_idx].cipher_key_len);
 	    if (err != err_status_ok) {
@@ -1100,5 +1097,5 @@
 	} while (!key_ok);
 	crypto->key.ptr = (char*)
-			  pj_pool_zalloc(pool, 
+			  pj_pool_zalloc(pool,
 					 crypto_suites[cs_idx].cipher_key_len);
 	pj_memcpy(crypto->key.ptr, key, crypto_suites[cs_idx].cipher_key_len);
@@ -1118,5 +1115,5 @@
 
     b64_key[b64_key_len] = '\0';
-    
+
     PJ_ASSERT_RETURN(*buffer_len >= (crypto->name.slen + \
 		     b64_key_len + 16), PJ_ETOOSMALL);
@@ -1124,5 +1121,5 @@
     /* Print the crypto attribute value. */
     print_len = pj_ansi_snprintf(buffer, *buffer_len, "%d %s inline:%s",
-				   tag, 
+				   tag,
 				   crypto_suites[cs_idx].name,
 				   b64_key);
@@ -1206,5 +1203,5 @@
     crypto->key.ptr = (char*) pj_pool_zalloc(pool, MAX_KEY_LEN);
     itmp = MAX_KEY_LEN;
-    status = pj_base64_decode(&tmp, (pj_uint8_t*)crypto->key.ptr, 
+    status = pj_base64_decode(&tmp, (pj_uint8_t*)crypto->key.ptr,
 			      &itmp);
     if (status != PJ_SUCCESS) {
@@ -1227,5 +1224,5 @@
 
     PJ_ASSERT_RETURN(tp, PJ_EINVAL);
-    
+
     pj_bzero(&srtp->rx_policy_neg, sizeof(srtp->rx_policy_neg));
     pj_bzero(&srtp->tx_policy_neg, sizeof(srtp->tx_policy_neg));
@@ -1275,5 +1272,5 @@
 
 PROPAGATE_MEDIA_CREATE:
-    return pjmedia_transport_media_create(srtp->member_tp, sdp_pool, 
+    return pjmedia_transport_media_create(srtp->member_tp, sdp_pool,
 					  member_tp_option, sdp_remote,
 					  media_index);
@@ -1297,5 +1294,5 @@
 
     PJ_ASSERT_RETURN(tp && sdp_pool && sdp_local, PJ_EINVAL);
-    
+
     pj_bzero(&srtp->rx_policy_neg, sizeof(srtp->rx_policy_neg));
     pj_bzero(&srtp->tx_policy_neg, sizeof(srtp->tx_policy_neg));
@@ -1307,5 +1304,5 @@
 
     /* Bypass if media transport is not RTP/AVP or RTP/SAVP */
-    if (pj_stricmp(&m_loc->desc.transport, &ID_RTP_AVP)  != 0 && 
+    if (pj_stricmp(&m_loc->desc.transport, &ID_RTP_AVP)  != 0 &&
 	pj_stricmp(&m_loc->desc.transport, &ID_RTP_SAVP) != 0)
 	goto BYPASS_SRTP;
@@ -1318,10 +1315,10 @@
      */
     /*
-    if (pjmedia_sdp_media_find_attr(m_loc, &ID_INACTIVE, NULL) || 
+    if (pjmedia_sdp_media_find_attr(m_loc, &ID_INACTIVE, NULL) ||
 	(m_rem && pjmedia_sdp_media_find_attr(m_rem, &ID_INACTIVE, NULL)))
 	goto BYPASS_SRTP;
     */
 
-    /* Check remote media transport & set local media transport 
+    /* Check remote media transport & set local media transport
      * based on SRTP usage option.
      */
@@ -1333,5 +1330,5 @@
 		goto BYPASS_SRTP;
 	    case PJMEDIA_SRTP_OPTIONAL:
-		m_loc->desc.transport = 
+		m_loc->desc.transport =
 				(srtp->peer_use == PJMEDIA_SRTP_MANDATORY)?
 				ID_RTP_SAVP : ID_RTP_AVP;
@@ -1356,5 +1353,5 @@
 		if (buffer_len) {
 		    pj_strset(&attr_value, buffer, buffer_len);
-		    attr = pjmedia_sdp_attr_create(srtp->pool, ID_CRYPTO.ptr, 
+		    attr = pjmedia_sdp_attr_create(srtp->pool, ID_CRYPTO.ptr,
 						   &attr_value);
 		    m_loc->attr[m_loc->attr_count++] = attr;
@@ -1401,9 +1398,9 @@
 		has_crypto_attr = PJ_TRUE;
 
-		status = parse_attr_crypto(srtp->pool, m_rem->attr[i], 
+		status = parse_attr_crypto(srtp->pool, m_rem->attr[i],
 					   &tmp_rx_crypto, &tags[cr_attr_count]);
 		if (status != PJ_SUCCESS)
 		    return status;
-    	 
+
 		/* Check duplicated tag */
 		for (j=0; j<cr_attr_count; ++j) {
@@ -1417,5 +1414,5 @@
 		    /* lets see if the crypto-suite offered is supported */
 		    for (j=0; j<srtp->setting.crypto_count; ++j)
-			if (pj_stricmp(&tmp_rx_crypto.name, 
+			if (pj_stricmp(&tmp_rx_crypto.name,
 				       &srtp->setting.crypto[j].name) == 0)
 			{
@@ -1434,9 +1431,9 @@
 			    //		hex_test_key,
 			    //		strlen(hex_test_key));
-			    //pj_strset(test_key, raw_test_key, 
+			    //pj_strset(test_key, raw_test_key,
 			    //	  crypto_suites[cs_idx].cipher_key_len);
 			    /* EO Force to use test key */
 
-			    if (tmp_rx_crypto.key.slen != 
+			    if (tmp_rx_crypto.key.slen !=
 				(int)crypto_suites[cs_idx].cipher_key_len)
 				return PJMEDIA_SRTP_EINKEYLEN;
@@ -1459,9 +1456,9 @@
 		case PJMEDIA_SRTP_OPTIONAL:
 		    /* bypass SRTP when no crypto-attr and remote uses RTP/AVP */
-		    if (!has_crypto_attr && 
+		    if (!has_crypto_attr &&
 			pj_stricmp(&m_rem->desc.transport, &ID_RTP_AVP) == 0)
 			goto BYPASS_SRTP;
 		    /* bypass SRTP when nothing match and remote uses RTP/AVP */
-		    else if (matched_idx == -1 && 
+		    else if (matched_idx == -1 &&
 			pj_stricmp(&m_rem->desc.transport, &ID_RTP_AVP) == 0)
 			goto BYPASS_SRTP;
@@ -1485,5 +1482,5 @@
 	    }
 
-	    /* we have to generate crypto answer, 
+	    /* we have to generate crypto answer,
 	     * with srtp->tx_policy_neg matched the offer
 	     * and rem_tag contains matched offer tag.
@@ -1497,9 +1494,9 @@
 
 	    srtp->tx_policy_neg = srtp->setting.crypto[matched_idx];
-	    
+
 	    /* If buffer_len==0, just skip the crypto attribute. */
 	    if (buffer_len) {
 		pj_strset(&attr_value, buffer, buffer_len);
-		attr = pjmedia_sdp_attr_create(sdp_pool, ID_CRYPTO.ptr, 
+		attr = pjmedia_sdp_attr_create(sdp_pool, ID_CRYPTO.ptr,
 					       &attr_value);
 		m_loc->attr[m_loc->attr_count++] = attr;
@@ -1508,5 +1505,5 @@
 	    /* At this point, we get valid rx_policy_neg & tx_policy_neg. */
 	}
-	    
+
     }
     goto PROPAGATE_MEDIA_CREATE;
@@ -1519,5 +1516,5 @@
 
 PROPAGATE_MEDIA_CREATE:
-    return pjmedia_transport_encode_sdp(srtp->member_tp, sdp_pool, 
+    return pjmedia_transport_encode_sdp(srtp->member_tp, sdp_pool,
 					sdp_local, sdp_remote, media_index);
 }
@@ -1548,5 +1545,5 @@
     /* For answerer side, this function will just have to start SRTP */
 
-    /* Check remote media transport & set local media transport 
+    /* Check remote media transport & set local media transport
      * based on SRTP usage option.
      */
@@ -1561,5 +1558,5 @@
 	    // Regardless the answer's transport type (RTP/AVP or RTP/SAVP),
 	    // the answer must be processed through in optional mode.
-	    // Please note that at this point transport type is ensured to be 
+	    // Please note that at this point transport type is ensured to be
 	    // RTP/AVP or RTP/SAVP, see transport_media_create()
 	    //if (pj_stricmp(&m_rem->desc.transport, &m_loc->desc.transport)) {
@@ -1574,5 +1571,5 @@
 	}
     }
-    
+
     if (srtp->offerer_side) {
 	/* find supported crypto-suite, get the tag, and assign policy_local */
@@ -1593,5 +1590,5 @@
 	    has_crypto_attr = PJ_TRUE;
 
-	    status = parse_attr_crypto(srtp->pool, m_rem->attr[i], 
+	    status = parse_attr_crypto(srtp->pool, m_rem->attr[i],
 				       &tmp_tx_crypto, &rem_tag);
 	    if (status != PJ_SUCCESS)
@@ -1606,5 +1603,5 @@
 
 	    /* match the crypto name */
-	    if (pj_stricmp(&tmp_tx_crypto.name, 
+	    if (pj_stricmp(&tmp_tx_crypto.name,
 		&srtp->setting.crypto[rem_tag-1].name) != 0)
 	    {
@@ -1634,5 +1631,5 @@
 
     /* Make sure we have the SRTP policies */
-    if (srtp_crypto_empty(&srtp->tx_policy_neg) || 
+    if (srtp_crypto_empty(&srtp->tx_policy_neg) ||
 	srtp_crypto_empty(&srtp->rx_policy_neg))
     {
@@ -1671,5 +1668,5 @@
 
 PROPAGATE_MEDIA_START:
-    return pjmedia_transport_media_start(srtp->member_tp, pool, 
+    return pjmedia_transport_media_start(srtp->member_tp, pool,
 					 sdp_local, sdp_remote,
 				         media_index);
@@ -1685,5 +1682,5 @@
     status = pjmedia_transport_media_stop(srtp->member_tp);
     if (status != PJ_SUCCESS)
-	PJ_LOG(4, (srtp->pool->obj_name, 
+	PJ_LOG(4, (srtp->pool->obj_name,
 		   "SRTP failed stop underlying media transport."));
 
@@ -1720,8 +1717,8 @@
     else
 	err = srtp_unprotect_rtcp(srtp->srtp_rx_ctx, pkt, pkt_len);
-    
+
     if (err != err_status_ok) {
-	PJ_LOG(5,(srtp->pool->obj_name, 
-		  "Failed to unprotect SRTP, pkt size=%d, err=%s", 
+	PJ_LOG(5,(srtp->pool->obj_name,
+		  "Failed to unprotect SRTP, pkt size=%d, err=%s",
 		  *pkt_len, get_libsrtp_errstr(err)));
     }
