From 616164fa54c982b10033e89ddd7442aab12e88ac Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Tue, 23 Jul 2019 18:06:00 -0400
Subject: [PATCH] sip: unuse default sip port and do implicit port binding

- solve the registration failure issue in android due to the possible
  occupied Default sip port (Address already in use)

Change-Id: I4794a4a1f6d3a0128aea2c21d18868de23cbbcee
---
 src/sip/sip_utils.h      | 1 +
 src/sip/sipaccount.cpp   | 4 ++++
 src/sip/sipaccount.h     | 4 ++--
 src/sip/siptransport.cpp | 1 -
 4 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/sip/sip_utils.h b/src/sip/sip_utils.h
index 9645ad0b18..2fb5a28182 100644
--- a/src/sip/sip_utils.h
+++ b/src/sip/sip_utils.h
@@ -44,6 +44,7 @@ namespace jami { namespace sip_utils {
 
 static constexpr int DEFAULT_SIP_PORT {5060};
 static constexpr int DEFAULT_SIP_TLS_PORT {5061};
+static constexpr int DEFAULT_AUTO_SELECT_PORT {0};
 
 enum class KeyExchangeProtocol { NONE, SDES };
 
diff --git a/src/sip/sipaccount.cpp b/src/sip/sipaccount.cpp
index 62debcd1df..4ab8a078a9 100644
--- a/src/sip/sipaccount.cpp
+++ b/src/sip/sipaccount.cpp
@@ -614,6 +614,10 @@ void SIPAccount::setAccountDetails(const std::map<std::string, std::string> &det
         v.push_back(map);
         setCredentials(v);
     }
+    if (isIP2IP()) {
+        localPort_ = (localPort_ == sip_utils::DEFAULT_AUTO_SELECT_PORT) ? sip_utils::DEFAULT_SIP_PORT : localPort_;
+        tlsListenerPort_ = (tlsListenerPort_ == sip_utils::DEFAULT_AUTO_SELECT_PORT) ? sip_utils::DEFAULT_SIP_TLS_PORT : tlsListenerPort_;
+    }
 }
 
 std::map<std::string, std::string>
diff --git a/src/sip/sipaccount.h b/src/sip/sipaccount.h
index 4375f59eec..fae3b6b459 100644
--- a/src/sip/sipaccount.h
+++ b/src/sip/sipaccount.h
@@ -702,7 +702,7 @@ class SIPAccount : public SIPAccountBase {
         /**
          * Local port to whih this account is bound
          */
-        pj_uint16_t localPort_ {sip_utils::DEFAULT_SIP_PORT};
+        pj_uint16_t localPort_ {sip_utils::DEFAULT_AUTO_SELECT_PORT};
 
         /**
          * Potential ip addresss on which this account is bound
@@ -712,7 +712,7 @@ class SIPAccount : public SIPAccountBase {
         /**
          * The TLS listener port
          */
-        pj_uint16_t tlsListenerPort_ {sip_utils::DEFAULT_SIP_TLS_PORT};
+        pj_uint16_t tlsListenerPort_ {sip_utils::DEFAULT_AUTO_SELECT_PORT};
 
         /**
          * Send Request Callback
diff --git a/src/sip/siptransport.cpp b/src/sip/siptransport.cpp
index 23707ec4dd..dd457639fb 100644
--- a/src/sip/siptransport.cpp
+++ b/src/sip/siptransport.cpp
@@ -329,7 +329,6 @@ SipTransportBroker::getUdpTransport(const IpAddr& ipAddress)
 std::shared_ptr<SipTransport>
 SipTransportBroker::createUdpTransport(const IpAddr& ipAddress)
 {
-    RETURN_IF_FAIL(ipAddress.getPort() != 0, nullptr, "Could not determine port for this transport");
     RETURN_IF_FAIL(ipAddress, nullptr, "Could not determine IP address for this transport");
 
     pjsip_udp_transport_cfg pj_cfg;
-- 
GitLab