Split Utility::GetHostName into two functions.

Fixes #6312
This commit is contained in:
Gunnar Beutner 2014-05-27 10:20:33 +02:00
parent 2cac149903
commit 7f5d8a36b4
2 changed files with 19 additions and 10 deletions

View File

@ -978,19 +978,25 @@ int Utility::CompareVersion(const String& v1, const String& v2)
return 0; return 0;
} }
String Utility::GetHostName(void)
{
char name[255];
if (gethostname(name, sizeof(name)) < 0)
return "localhost";
return name;
}
/** /**
* Returns the fully-qualified domain name for the host * Returns the fully-qualified domain name for the host
* we're running on. * we're running on.
* *
* @returns The FQDN. * @returns The FQDN.
*/ */
String Utility::GetHostName(void) String Utility::GetFQDN(void)
{ {
char name[255]; String hostname = GetHostName();
if (gethostname(name, sizeof(name)) < 0) {
return "localhost";
}
addrinfo hints; addrinfo hints;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
@ -999,18 +1005,20 @@ String Utility::GetHostName(void)
hints.ai_flags = AI_CANONNAME; hints.ai_flags = AI_CANONNAME;
addrinfo *result; addrinfo *result;
int rc = getaddrinfo(name, NULL, &hints, &result); int rc = getaddrinfo(hostname.CStr(), NULL, &hints, &result);
if (rc < 0) if (rc < 0)
result = NULL; result = NULL;
String canonicalName; String canonicalName;
if (result && strcmp(result->ai_canonname, "localhost") != 0) { if (result) {
canonicalName = result->ai_canonname; if (strcmp(result->ai_canonname, "localhost") != 0)
canonicalName = result->ai_canonname;
freeaddrinfo(result); freeaddrinfo(result);
} else { } else {
canonicalName = name; canonicalName = hostname;
} }
return canonicalName; return canonicalName;

View File

@ -120,6 +120,7 @@ public:
static int Random(void); static int Random(void);
static String GetHostName(void); static String GetHostName(void);
static String GetFQDN(void);
static tm LocalTime(time_t ts); static tm LocalTime(time_t ts);