diff --git a/src/ip_utils.cpp b/src/ip_utils.cpp index 4b2fd646de3f8d22a6b2f1dfba2572fa1df8ea9f..7524d7d014b109b7d54401418271eaaf67b1a323 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 8b9364e2c7fee380068031170e8a2f377834d39c..01b0fed6903cae2471c459a6c1daad836a2f70e9 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).