From a8013d76b847908449869bfb4906498744b4d8d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Sun, 16 Aug 2015 14:25:34 -0400
Subject: [PATCH] opendht: bump to v2

WARNING: breaks compatibility with previous versions (RING and DHT)

* OpenDHT uses msgpack library for serialization (added to contrib)
* OpenDHT uses GCM-AES+RSA for encryption (instead of RSA)
* adapts ringaccount to follow these API changes

Issue: #78264
Change-Id: I12744990d51e8f9022007ba59472d74aec89caeb
---
 contrib/src/msgpack/rules.mak              | 27 ++++++++++++++++++++++
 contrib/src/opendht/rules.mak              |  7 ++++--
 src/dring/configurationmanager_interface.h |  2 +-
 src/ringdht/ringaccount.cpp                | 26 +++++++--------------
 src/ringdht/sips_transport_ice.cpp         |  2 +-
 5 files changed, 42 insertions(+), 22 deletions(-)
 create mode 100644 contrib/src/msgpack/rules.mak

diff --git a/contrib/src/msgpack/rules.mak b/contrib/src/msgpack/rules.mak
new file mode 100644
index 0000000000..ee0d901a97
--- /dev/null
+++ b/contrib/src/msgpack/rules.mak
@@ -0,0 +1,27 @@
+# MSGPACK
+MSGPACK_VERSION := 294aa52c3ad8392ea54331d0ed89299f6a32a798
+MSGPACK_URL := https://github.com/msgpack/msgpack-c/archive/$(MSGPACK_VERSION).tar.gz
+
+PKGS += msgpack
+ifeq ($(call need_pkg,"msgpack >= 1.1"),)
+PKGS_FOUND += msgpack
+endif
+
+MSGPACK_CMAKECONF := -DMSGPACK_CXX11=ON \
+                     -DMSGPACK_BUILD_EXAMPLES=OFF
+
+$(TARBALLS)/msgpack-c-$(MSGPACK_VERSION).tar.gz:
+	$(call download,$(MSGPACK_URL))
+
+.sum-msgpack: msgpack-c-$(MSGPACK_VERSION).tar.gz
+	$(warning $@ not implemented)
+	touch $@
+
+msgpack: msgpack-c-$(MSGPACK_VERSION).tar.gz .sum-msgpack
+	$(UNPACK)
+	$(MOVE)
+
+.msgpack: msgpack toolchain.cmake
+	cd $< && $(HOSTVARS) $(CMAKE) . $(MSGPACK_CMAKECONF)
+	cd $< && $(MAKE) install
+	touch $@
diff --git a/contrib/src/opendht/rules.mak b/contrib/src/opendht/rules.mak
index 04dc067c5f..ddc79b5646 100644
--- a/contrib/src/opendht/rules.mak
+++ b/contrib/src/opendht/rules.mak
@@ -1,5 +1,5 @@
 # OPENDHT
-OPENDHT_VERSION := d2be36b615161c2961b89fb4b969cd72aeb2c789
+OPENDHT_VERSION := 27738e5992fdef4ee12df85682df098a7f57f5f9
 OPENDHT_URL := https://github.com/savoirfairelinux/opendht/archive/$(OPENDHT_VERSION).tar.gz
 
 PKGS += opendht
@@ -8,8 +8,11 @@ PKGS_FOUND += opendht
 endif
 
 # Avoid building distro-provided dependencies in case opendht was built manually
+ifneq ($(call need_pkg,"msgpack >= 1.1"),)
+DEPS_opendht += msgpack
+endif
 ifneq ($(call need_pkg,"gnutls >= 3.3.0"),)
-DEPS_opendht = gnutls $(DEPS_gnutls)
+DEPS_opendht += gnutls $(DEPS_gnutls)
 endif
 
 $(TARBALLS)/opendht-$(OPENDHT_VERSION).tar.gz:
diff --git a/src/dring/configurationmanager_interface.h b/src/dring/configurationmanager_interface.h
index d85bfc60ee..5d7d3c6997 100644
--- a/src/dring/configurationmanager_interface.h
+++ b/src/dring/configurationmanager_interface.h
@@ -117,7 +117,7 @@ std::map<std::string, std::string> getIp2IpDetails();
 std::vector<std::map<std::string, std::string>> getCredentials(const std::string& accountID);
 void setCredentials(const std::string& accountID, const std::vector<std::map<std::string, std::string>>& details);
 
-std::string getAddrFromInterfaceName(const std::string& interface);
+std::string getAddrFromInterfaceName(const std::string& iface);
 
 std::vector<std::string> getAllIpInterface();
 std::vector<std::string> getAllIpInterfaceByName();
diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index f169591044..c2692aac2f 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -193,7 +193,7 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
     setCertificateStatus(toUri, tls::TrustStore::Status::ALLOWED);
 
     std::weak_ptr<SIPCall> weak_call = call;
-    manager.addTask([=] {
+    runOnMainThread([=] {
         static std::uniform_int_distribution<dht::Value::Id> udist;
         auto call = weak_call.lock();
 
@@ -212,7 +212,6 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
 
         /* Next step: sent the ICE data to peer through DHT */
         const dht::Value::Id callvid  = udist(shared_this->rand_);
-        const dht::Value::Id replyvid = callvid + 1;
         const auto toH = dht::InfoHash(toUri);
         const auto callkey = dht::InfoHash::get("callto:" + toUri);
 
@@ -220,8 +219,7 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
         shared_this->dht_.putEncrypted(
             callkey, toH,
             dht::Value {
-                dht::IceCandidates(ice->getLocalAttributesAndCandidates()),
-                callvid
+                dht::IceCandidates(callvid, ice->getLocalAttributesAndCandidates())
             },
             [=](bool ok) { // Put complete callback
                 if (!ok) {
@@ -237,7 +235,7 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
         auto listenKey = shared_this->dht_.listen<dht::IceCandidates>(
             callkey,
             [=] (dht::IceCandidates&& msg) {
-                if (msg.id != replyvid)
+                if (msg.id != callvid or msg.from != toH)
                     return true;
                 RING_WARN("ICE request replied from DHT peer %s\n%s", toH.toString().c_str(),
                           std::string(msg.ice_data.cbegin(), msg.ice_data.cend()).c_str());
@@ -254,7 +252,6 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
             std::move(listenKey),
             callkey, toH
         });
-        return false;
     });
 
     return call;
@@ -811,7 +808,7 @@ RingAccount::doRegister_()
                     this_.findCertificate(
                         msg.from,
                         [shared, msg](const std::shared_ptr<dht::crypto::Certificate> cert) mutable {
-                            if (!cert) {
+                            if (!cert or cert->getId() != msg.from) {
                                 RING_WARN("Can't find certificate of %s for incoming call.",
                                           msg.from.toString().c_str());
                                 return;
@@ -820,7 +817,7 @@ RingAccount::doRegister_()
                             tls::CertificateStore::instance().pinCertificate(cert);
 
                             auto& this_ = *shared;
-                            if (!this_.trust_.isTrusted(*cert) or cert->getId() != msg.from) {
+                            if (!this_.trust_.isTrusted(*cert)) {
                                 RING_WARN("Discarding incoming DHT call from untrusted peer %s.",
                                           msg.from.toString().c_str());
                                 return;
@@ -882,30 +879,23 @@ void
 RingAccount::incomingCall(dht::IceCandidates&& msg)
 {
     auto from = msg.from.toString();
-    auto reply_vid = msg.id+1;
-    RING_WARN("ICE incoming from DHT peer %s\n%s", from.c_str(),
+    RING_WARN("ICE incoming (id %lu) from DHT peer %s\n%s", msg.id, from.c_str(),
               std::string(msg.ice_data.cbegin(), msg.ice_data.cend()).c_str());
     auto call = Manager::instance().callFactory.newCall<SIPCall, RingAccount>(*this, Manager::instance().getNewCallID(), Call::CallType::INCOMING);
     auto ice = createIceTransport(("sip:"+call->getCallId()).c_str(), ICE_COMPONENTS, false, getIceOptions());
 
     std::weak_ptr<SIPCall> weak_call = call;
-    auto shared = std::static_pointer_cast<RingAccount>(shared_from_this());
     dht_.putEncrypted(
         callKey_,
         msg.from,
-        dht::Value {
-            dht::IceCandidates(ice->getLocalAttributesAndCandidates()),
-            reply_vid
-        },
-        [weak_call,shared,reply_vid](bool ok) {
-            auto& this_ = *shared.get();
+        { dht::IceCandidates(msg.id, ice->getLocalAttributesAndCandidates()) },
+        [weak_call](bool ok) {
             if (!ok) {
                 RING_WARN("Can't put ICE descriptor reply on DHT");
                 if (auto call = weak_call.lock())
                     call->onFailure();
             } else
                 RING_DBG("Successfully put ICE descriptor reply on DHT");
-            this_.dht_.cancelPut(this_.callKey_, reply_vid);
         }
     );
     ice->start(msg.ice_data);
diff --git a/src/ringdht/sips_transport_ice.cpp b/src/ringdht/sips_transport_ice.cpp
index 95e86d69c2..a738b94ffa 100644
--- a/src/ringdht/sips_transport_ice.cpp
+++ b/src/ringdht/sips_transport_ice.cpp
@@ -393,7 +393,7 @@ SipsIceTransport::certGetInfo(pj_pool_t* pool, pj_ssl_cert_info* ci,
 
     pj_assert(pool && ci && crt_raw);
 
-    dht::crypto::Certificate crt(Blob(crt_raw[0].data, crt_raw[0].data + crt_raw[0].size));
+    dht::crypto::Certificate crt(crt_raw[0].data, crt_raw[0].size);
 
     /* Get issuer */
     gnutls_x509_crt_get_issuer_dn(crt.cert, buf, &bufsize);
-- 
GitLab