From 9150099c0b45345cde9d738d7f0f344ade4788cc Mon Sep 17 00:00:00 2001
From: nanangizz <nanang@teluu.com>
Date: Tue, 11 Aug 2020 11:36:08 +0700
Subject: [PATCH 3/3] - update local preference for peer reflexive candidate -
 also update PRIORITY field value in performing connectivity check (must use
 peer reflexive type and local pref must be unique, is this necessary?) - fix
 local preference mask for non-standard ICE prio calculation

---
 pjnath/include/pjnath/ice_session.h |  5 +++++
 pjnath/src/pjnath/ice_session.c     | 27 ++++++++++++++++++---------
 pjnath/src/pjnath/ice_strans.c      |  6 +++---
 3 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/pjnath/include/pjnath/ice_session.h b/pjnath/include/pjnath/ice_session.h
index 8971220f0..a889020f9 100644
--- a/pjnath/include/pjnath/ice_session.h
+++ b/pjnath/include/pjnath/ice_session.h
@@ -237,6 +237,11 @@ typedef struct pj_ice_msg_data
  */
 typedef struct pj_ice_sess_cand
 {
+    /**
+     * The candidate ID.
+     */
+    unsigned		 id;
+
     /**
      * The candidate type, as described in #pj_ice_cand_type enumeration.
      */
diff --git a/pjnath/src/pjnath/ice_session.c b/pjnath/src/pjnath/ice_session.c
index 4eb5adea9..b2cfc3ec2 100644
--- a/pjnath/src/pjnath/ice_session.c
+++ b/pjnath/src/pjnath/ice_session.c
@@ -686,9 +686,9 @@ static pj_uint32_t CALC_CAND_PRIO(pj_ice_sess *ice,
 	   (((256 - comp_id) & 0xFF) << 0);
 #else
     enum {
-	type_mask   = ((2 << PJ_ICE_CAND_TYPE_PREF_BITS) - 1),
-	local_mask  = ((2 << PJ_ICE_LOCAL_PREF_BITS) - 1),
-	comp_mask   = ((2 << PJ_ICE_COMP_BITS) - 1),
+	type_mask   = ((1 << PJ_ICE_CAND_TYPE_PREF_BITS) - 1),
+	local_mask  = ((1 << PJ_ICE_LOCAL_PREF_BITS) - 1),
+	comp_mask   = ((1 << PJ_ICE_COMP_BITS) - 1),
 
 	comp_shift  = 0,
 	local_shift = (PJ_ICE_COMP_BITS),
@@ -918,11 +918,13 @@ PJ_DEF(pj_status_t) pj_ice_sess_add_cand(pj_ice_sess *ice,
     }
 
     lcand = &ice->lcand[ice->lcand_cnt];
+    lcand->id = ice->lcand_cnt;
     lcand->comp_id = (pj_uint8_t)comp_id;
     lcand->transport_id = (pj_uint8_t)transport_id;
     lcand->type = type;
     lcand->transport = transport;
     pj_strdup(ice->pool, &lcand->foundation, foundation);
+    lcand->local_pref = local_pref;
     lcand->prio = CALC_CAND_PRIO(ice, type, local_pref, lcand->comp_id);
     pj_sockaddr_cp(&lcand->addr, addr);
     pj_sockaddr_cp(&lcand->base_addr, base_addr);
@@ -950,7 +952,7 @@ PJ_DEF(pj_status_t) pj_ice_sess_add_cand(pj_ice_sess *ice,
     LOG4((ice->obj_name, 
 	 "Candidate %d added: comp_id=%d, type=%s, foundation=%.*s, "
 	 "addr=%s:%d, base=%s:%d, prio=0x%x (%u)",
-	 ice->lcand_cnt, 
+	 lcand->id,
 	 lcand->comp_id, 
 	 cand_type_names[lcand->type],
 	 (int)lcand->foundation.slen,
@@ -962,7 +964,7 @@ PJ_DEF(pj_status_t) pj_ice_sess_add_cand(pj_ice_sess *ice,
 	 lcand->prio, lcand->prio));
 
     if (p_cand_id)
-	*p_cand_id = ice->lcand_cnt;
+	*p_cand_id = lcand->id;
 
     ++ice->lcand_cnt;
 
@@ -1692,7 +1694,7 @@ PJ_DEF(pj_status_t) pj_ice_sess_create_check_list(
 
 	pj_memcpy(cn, &rem_cand[i], sizeof(pj_ice_sess_cand));
 	pj_strdup(ice->pool, &cn->foundation, &rem_cand[i].foundation);
-	ice->rcand_cnt++;
+	cn->id = ice->rcand_cnt++;
     }
 
     /* Generate checklist */
@@ -1823,10 +1825,11 @@ static pj_status_t perform_check(pj_ice_sess *ice,
 
     /* Add PRIORITY */
 #if PJNATH_ICE_PRIO_STD
-    prio = CALC_CAND_PRIO(ice, PJ_ICE_CAND_TYPE_PRFLX, 65535, 
+    prio = CALC_CAND_PRIO(ice, PJ_ICE_CAND_TYPE_PRFLX, 65535 - lcand->id,
 			  lcand->comp_id);
 #else
-    prio = CALC_CAND_PRIO(ice, PJ_ICE_CAND_TYPE_PRFLX, 0, 
+    prio = CALC_CAND_PRIO(ice, PJ_ICE_CAND_TYPE_PRFLX,
+			  ((1 << PJ_ICE_LOCAL_PREF_BITS) - 1) - lcand->id,
 			  lcand->comp_id);
 #endif
     pj_stun_msg_add_uint_attr(check->tdata->pool, check->tdata->msg, 
@@ -2422,7 +2425,13 @@ static void on_stun_request_complete(pj_stun_session *stun_sess,
 	status = pj_ice_sess_add_cand(ice, check->lcand->comp_id, 
 				      msg_data->transport_id,
 				      PJ_ICE_CAND_TYPE_PRFLX,
-				      65535, &foundation,
+#if PJNATH_ICE_PRIO_STD
+				      65535 - ice->lcand_cnt,
+#else
+				      ((1 << PJ_ICE_LOCAL_PREF_BITS) - 1) -
+				      ice->lcand_cnt,
+#endif
+				      &foundation,
 				      &xaddr->sockaddr, 
 				      &check->lcand->base_addr, 
 				      &check->lcand->base_addr,
diff --git a/pjnath/src/pjnath/ice_strans.c b/pjnath/src/pjnath/ice_strans.c
index 5630b69e5..2fc3c6c0c 100644
--- a/pjnath/src/pjnath/ice_strans.c
+++ b/pjnath/src/pjnath/ice_strans.c
@@ -64,9 +64,9 @@ enum tp_type
 #   define HOST_PREF   65535
 #   define RELAY_PREF  65535
 #else
-#   define SRFLX_PREF  0
-#   define HOST_PREF   0
-#   define RELAY_PREF  0
+#   define SRFLX_PREF  ((1 << PJ_ICE_LOCAL_PREF_BITS) - 1)
+#   define HOST_PREF   ((1 << PJ_ICE_LOCAL_PREF_BITS) - 1)
+#   define RELAY_PREF  ((1 << PJ_ICE_LOCAL_PREF_BITS) - 1)
 #endif
 
 
-- 
2.25.1

