From e6127d1212ab0f2256e340236cee44073f556631 Mon Sep 17 00:00:00 2001
From: atraczyk <andreastraczyk@gmail.com>
Date: Tue, 13 Jun 2017 14:27:35 -0400
Subject: [PATCH] ip_utils: add GetDeviceName signal

Add signal to retrieve the device name on platforms where it must be
acquired client-side.

createRingDevice now gets the device name from getDeviceName, which
uses the GetDeviceName signal on Android, iOS, and UWP builds, and
otherwise getHostname for the GNU/Linux, win32, and MacOSX builds.

Change-Id: I81013afdd5d51f3cbd518dca22a4cfdc03f94c59
---
 src/client/ring_signal.cpp                 |  1 +
 src/dring/configurationmanager_interface.h |  4 ++++
 src/ip_utils.cpp                           | 17 +++++++++++++++++
 src/ip_utils.h                             |  2 ++
 src/ringdht/ringaccount.cpp                |  4 ++--
 5 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/src/client/ring_signal.cpp b/src/client/ring_signal.cpp
index b29ab291f6..f3234a9daa 100644
--- a/src/client/ring_signal.cpp
+++ b/src/client/ring_signal.cpp
@@ -78,6 +78,7 @@ getSignalHandlers()
 #endif
 #if defined(__ANDROID__) || (defined(TARGET_OS_IOS) && TARGET_OS_IOS) || defined(RING_UWP)
         exported_callback<DRing::ConfigurationSignal::GetAppDataPath>(),
+        exported_callback<DRing::ConfigurationSignal::GetDeviceName>(),
 #endif
 
         /* Debug */
diff --git a/src/dring/configurationmanager_interface.h b/src/dring/configurationmanager_interface.h
index 135aa7ea0a..013c1806bd 100644
--- a/src/dring/configurationmanager_interface.h
+++ b/src/dring/configurationmanager_interface.h
@@ -294,6 +294,10 @@ struct ConfigurationSignal {
                 constexpr static const char* name = "GetAppDataPath";
                 using cb_type = void(const std::string& name, std::vector<std::string>* /* path_ret */);
         };
+        struct GetDeviceName {
+            constexpr static const char* name = "GetDeviceName";
+            using cb_type = void(std::vector<std::string>* /* path_ret */);
+        };
 #endif
 };
 
diff --git a/src/ip_utils.cpp b/src/ip_utils.cpp
index 0a27713645..c4d6d29cb9 100644
--- a/src/ip_utils.cpp
+++ b/src/ip_utils.cpp
@@ -30,6 +30,10 @@
 #include <unistd.h>
 #include <limits.h>
 
+#if defined(__ANDROID__) || defined(RING_UWP) || (defined(TARGET_OS_IOS) && TARGET_OS_IOS)
+#include "client/ring_signal.h"
+#endif
+
 #ifdef _WIN32
 #define InetPtonA inet_pton
 WINSOCK_API_LINKAGE INT WSAAPI InetPtonA(INT Family, LPCSTR pStringBuf, PVOID pAddr);
@@ -55,6 +59,19 @@ ip_utils::getHostname()
     return hostname;
 }
 
+std::string
+ip_utils::getDeviceName()
+{
+#if defined(__ANDROID__) || defined(RING_UWP) || (defined(TARGET_OS_IOS) && TARGET_OS_IOS)
+    std::vector<std::string> deviceNames;
+    emitSignal<DRing::ConfigurationSignal::GetDeviceName>(&deviceNames);
+    if (not deviceNames.empty()) {
+        return deviceNames[0];
+    }
+#endif
+    return getHostname();
+}
+
 std::vector<IpAddr>
 ip_utils::getAddrList(const std::string &name, pj_uint16_t family)
 {
diff --git a/src/ip_utils.h b/src/ip_utils.h
index f20f91194a..e70b4363c8 100644
--- a/src/ip_utils.h
+++ b/src/ip_utils.h
@@ -237,6 +237,8 @@ static const char *const DEFAULT_INTERFACE = "default";
 
 std::string getHostname();
 
+std::string getDeviceName();
+
 /**
  * Return the generic "any host" IP address of the specified family.
  * If family is unspecified, default to pj_AF_INET6() (IPv6).
diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index 21c6b0fb1c..7b4ccdfdb0 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -753,7 +753,7 @@ void RingAccount::unserialize(const YAML::Node &node)
         try {
             parseValue(node, DRing::Account::VolatileProperties::REGISTERED_NAME, registeredName_);
         } catch (const std::exception& e) {
-            RING_WARN("can't read device name: %s", e.what());
+            RING_WARN("can't read registered name: %s", e.what());
         }
     }
 
@@ -810,7 +810,7 @@ RingAccount::createRingDevice(const dht::crypto::Identity& id)
     accountTrust_ = dht::crypto::TrustList{};
     accountTrust_.add(*id.second);
     ringDeviceId_ = dev_id.first->getPublicKey().getId().toString();
-    ringDeviceName_ = ip_utils::getHostname();
+    ringDeviceName_ = ip_utils::getDeviceName();
     if (ringDeviceName_.empty())
         ringDeviceName_ = ringDeviceId_.substr(8);
 
-- 
GitLab