From c33f1ff775bb3cfe6f40e2b8d11c7d8f0438548b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Tue, 24 Sep 2019 14:36:28 -0400
Subject: [PATCH] lan: improve pjsip behavior in a LAN

1. Resolving the TURN server but receiving connection timeout are
breaking the ICE init for now. This should not be the case. This
patch only removes the candidate from the candidate's list if a
conection timeout to the TURN occurs.
2. pj_getaddrinfo is very slow. Moreover, it's unecessary because
we succeed to get correct hosts candidates. So, just disable this.

Change-Id: Ic17628032c66ab1eeab43036bc340c81808aea5c
---
 .../pjproject/disable_local_resolution.patch    | 17 +++++++++++++++++
 contrib/src/pjproject/fetch_and_patch.bat       |  2 ++
 .../pjproject/fix_turn_connection_failure.patch | 16 ++++++++++++++++
 contrib/src/pjproject/rules.mak                 |  2 ++
 4 files changed, 37 insertions(+)
 create mode 100644 contrib/src/pjproject/disable_local_resolution.patch
 create mode 100644 contrib/src/pjproject/fix_turn_connection_failure.patch

diff --git a/contrib/src/pjproject/disable_local_resolution.patch b/contrib/src/pjproject/disable_local_resolution.patch
new file mode 100644
index 0000000000..0f06cbaaa2
--- /dev/null
+++ b/contrib/src/pjproject/disable_local_resolution.patch
@@ -0,0 +1,17 @@
+ pjlib/include/pj/config.h | 3 +++
+ 1 file changed, 3 insertions(+)
+
+diff --git a/pjlib/include/pj/config.h b/pjlib/include/pj/config.h
+index ad0abbb5..6bba5a55 100644
+--- a/pjlib/include/pj/config.h
++++ b/pjlib/include/pj/config.h
+@@ -1003,6 +1003,9 @@
+ #endif
+ 
+ 
++#ifndef PJ_GETHOSTIP_DISABLE_LOCAL_RESOLUTION
++#   define PJ_GETHOSTIP_DISABLE_LOCAL_RESOLUTION	    1
++#endif
+ 
+ /** @} */
+ 
diff --git a/contrib/src/pjproject/fetch_and_patch.bat b/contrib/src/pjproject/fetch_and_patch.bat
index 2082821e81..fafb48d9aa 100644
--- a/contrib/src/pjproject/fetch_and_patch.bat
+++ b/contrib/src/pjproject/fetch_and_patch.bat
@@ -39,6 +39,8 @@ bash -c "%PATCH_CMD% %UNIXPATH%pjproject/ice_config.patch"
 bash -c "%PATCH_CMD% %UNIXPATH%pjproject/fix_first_packet_turn_tcp.patch"
 bash -c "%PATCH_CMD% %UNIXPATH%pjproject/fix_ebusy_turn.patch"
 bash -c "%PATCH_CMD% %UNIXPATH%pjproject/ignore_ipv6_on_transport_check.patch"
+bash -c "%PATCH_CMD% %UNIXPATH%pjproject/fix_turn_connection_failure.patch"
+bash -c "%PATCH_CMD% %UNIXPATH%pjproject/disable_local_resolution.patch"
 
 %APPLY_CMD% %SRC%\pjproject\win32_vs_gnutls.patch
 %APPLY_CMD% %SRC%\pjproject\win_config.patch
diff --git a/contrib/src/pjproject/fix_turn_connection_failure.patch b/contrib/src/pjproject/fix_turn_connection_failure.patch
new file mode 100644
index 0000000000..f42a15bd72
--- /dev/null
+++ b/contrib/src/pjproject/fix_turn_connection_failure.patch
@@ -0,0 +1,16 @@
+ pjnath/src/pjnath/ice_strans.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/pjnath/src/pjnath/ice_strans.c b/pjnath/src/pjnath/ice_strans.c
+index 72f12700..d4790049 100644
+--- a/pjnath/src/pjnath/ice_strans.c
++++ b/pjnath/src/pjnath/ice_strans.c
+@@ -2854,7 +2854,7 @@ static void turn_on_state(pj_turn_sock *turn_sock, pj_turn_state_t old_state,
+ 
+ 	sess_init_update(comp->ice_st);
+ 
+-    } else if ((old_state == PJ_TURN_STATE_RESOLVING || old_state == PJ_TURN_STATE_ALLOCATING) &&
++    } 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;
diff --git a/contrib/src/pjproject/rules.mak b/contrib/src/pjproject/rules.mak
index 42d037d22e..0463f54a6a 100644
--- a/contrib/src/pjproject/rules.mak
+++ b/contrib/src/pjproject/rules.mak
@@ -68,6 +68,8 @@ endif
 	$(APPLY) $(SRC)/pjproject/fix_first_packet_turn_tcp.patch
 	$(APPLY) $(SRC)/pjproject/fix_ebusy_turn.patch
 	$(APPLY) $(SRC)/pjproject/ignore_ipv6_on_transport_check.patch
+	$(APPLY) $(SRC)/pjproject/fix_turn_connection_failure.patch
+	$(APPLY) $(SRC)/pjproject/disable_local_resolution.patch
 	$(UPDATE_AUTOCONFIG)
 	$(MOVE)
 
-- 
GitLab