From 0e90ddb7e94b4102d2e36414db01e4523d9febd2 Mon Sep 17 00:00:00 2001
From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Date: Fri, 21 Oct 2016 13:58:52 -0400
Subject: [PATCH] turn: use official PJSIP patch to fix TURN

Commit 0bc5829b88a58b4c7595e75c52eaee522286e47e has introduced
a patch recently partialy integrated in PJSIP (see [1]).
But it doesn't take in account TURN error during ALLOCATING state.
This patch replace our patch by the PJSIP one and modify it
to handle the ALLOCATING case.

[1] https://trac.pjsip.org/repos/changeset/5465

Change-Id: Ic45458609e11b55641f13c3e6294eeda599d304e
Reviewed-by: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
---
 contrib/src/pjproject/fix_turn_fallback.patch | 62 ++++++++++++-------
 1 file changed, 38 insertions(+), 24 deletions(-)

diff --git a/contrib/src/pjproject/fix_turn_fallback.patch b/contrib/src/pjproject/fix_turn_fallback.patch
index 2a54ee15cd..50229dd01c 100644
--- a/contrib/src/pjproject/fix_turn_fallback.patch
+++ b/contrib/src/pjproject/fix_turn_fallback.patch
@@ -13,27 +13,41 @@
 +			continue;
 +
  	    /* Gather remote addresses for this component */
-@@ -2013,4 +2016,21 @@
- 	    if (comp->ice_st->state < PJ_ICE_STRANS_STATE_READY) {
--		sess_fail(comp->ice_st, PJ_ICE_STRANS_OP_INIT,
--			  "TURN allocation failed", info.last_status);
-+			PJ_LOG(4,(comp->ice_st->obj_name,
-+					  "TURN allocation failed, disable the associated candidate"));
-+			pj_ice_sess_cand *cand = NULL;
-+			unsigned i;
-+			/* Wait until initialization completes */
-+			pj_grp_lock_acquire(comp->ice_st->grp_lock);
-+			/* Find relayed candidate in the component */
-+			for (i=0; i<comp->cand_cnt; ++i) {
-+				if (comp->cand_list[i].type == PJ_ICE_CAND_TYPE_RELAYED &&
-+					comp->cand_list[i].transport_id == data->transport_id) {
-+					cand = &comp->cand_list[i];
-+					break;
-+				}
-+			}
-+			pj_grp_lock_release(comp->ice_st->grp_lock);
-+			pj_assert(cand != NULL);
-+			// Make session init succeed but disable this TURN server to be used
-+			cand->status = PJNATH_ESTUNTIMEDOUT;
-+			sess_init_update(comp->ice_st);
- 	    } else if (comp->turn[tp_idx].err_cnt > 1) {
+@@ -1995,4 +1998,37 @@
+         sess_init_update(comp->ice_st);
+
++    } else if ((old_state == PJ_TURN_STATE_RESOLVING || old_state == PJ_TURN_STATE_ALLOCATING) &&
++               new_state >= PJ_TURN_STATE_DEALLOCATING)
++    {
++        pj_ice_sess_cand *cand = NULL;
++        unsigned i;
++
++        /* DNS resolution has failed! */
++        ++comp->turn[tp_idx].err_cnt;
++
++        /* Unregister ourself from the TURN relay */
++        pj_turn_sock_set_user_data(turn_sock, NULL);
++        comp->turn[tp_idx].sock = NULL;
++
++        /* Wait until initialization completes */
++        pj_grp_lock_acquire(comp->ice_st->grp_lock);
++
++        /* Find relayed candidate in the component */
++        for (i=0; i<comp->cand_cnt; ++i) {
++            if (comp->cand_list[i].type == PJ_ICE_CAND_TYPE_RELAYED &&
++                comp->cand_list[i].transport_id == data->transport_id)
++            {
++                cand = &comp->cand_list[i];
++                break;
++            }
++        }
++        pj_assert(cand != NULL);
++
++        pj_grp_lock_release(comp->ice_st->grp_lock);
++
++        cand->status = old_state == PJ_TURN_STATE_RESOLVING ? PJ_ERESOLVE : PJ_EINVALIDOP;
++
++        sess_init_update(comp->ice_st);
++
+     } else if (new_state >= PJ_TURN_STATE_DEALLOCATING) {
+         pj_turn_session_info info;
-- 
GitLab