From c29ca0269bdfd714751b0f2514bf3e819e47cb70 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Thu, 29 Dec 2016 16:56:14 -0500
Subject: [PATCH] ip_utils: add getHostname()

Add method to retreive the hostname,
to fill the default device name.

Change-Id: I5aafe8d0413caa13c4fdf720f6f3ce8b0577d238
---
 src/ip_utils.cpp | 27 +++++++++++++++++++++++----
 src/ip_utils.h   |  2 ++
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/ip_utils.cpp b/src/ip_utils.cpp
index 4b2fd646de..7524d7d014 100644
--- a/src/ip_utils.cpp
+++ b/src/ip_utils.cpp
@@ -28,14 +28,33 @@
 
 #include <sys/types.h>
 #include <unistd.h>
+#include <limits.h>
 
 #ifdef _WIN32
 #define InetPtonA inet_pton
 WINSOCK_API_LINKAGE INT WSAAPI InetPtonA(INT Family, LPCSTR pStringBuf, PVOID pAddr);
 #endif
 
+#ifndef HOST_NAME_MAX
+#ifdef MAX_COMPUTERNAME_LENGTH
+#define HOST_NAME_MAX MAX_COMPUTERNAME_LENGTH
+#else
+// Max 255 chars as per RFC 1035
+#define HOST_NAME_MAX 255
+#endif
+#endif
+
 namespace ring {
 
+std::string
+ip_utils::getHostname()
+{
+    char hostname[HOST_NAME_MAX];
+    if (gethostname(hostname, HOST_NAME_MAX))
+        return {};
+    return hostname;
+}
+
 std::vector<IpAddr>
 ip_utils::getAddrList(const std::string &name, pj_uint16_t family)
 {
@@ -247,10 +266,10 @@ ip_utils::getLocalNameservers()
 {
     std::vector<IpAddr> res;
 #if defined __ANDROID__ || defined _WIN32 || TARGET_OS_IPHONE
-#ifdef _MSC_VER
-#pragma message (__FILE__ "(" STR2(__LINE__) ") : -NOTE- " "Not implemented")
-#else
-#warning "Not implemented"
+#ifdef _MSC_VER
+#pragma message (__FILE__ "(" STR2(__LINE__) ") : -NOTE- " "Not implemented")
+#else
+#warning "Not implemented"
 #endif
 #else
     if (not (_res.options & RES_INIT))
diff --git a/src/ip_utils.h b/src/ip_utils.h
index 8b9364e2c7..01b0fed690 100644
--- a/src/ip_utils.h
+++ b/src/ip_utils.h
@@ -235,6 +235,8 @@ namespace ip_utils {
 
 static const char *const DEFAULT_INTERFACE = "default";
 
+std::string getHostname();
+
 /**
  * Return the generic "any host" IP address of the specified family.
  * If family is unspecified, default to pj_AF_INET6() (IPv6).
-- 
GitLab