]> granicus.if.org Git - icinga2/commitdiff
Split Utility::GetHostName into two functions.
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 27 May 2014 08:20:33 +0000 (10:20 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 27 May 2014 08:20:33 +0000 (10:20 +0200)
Fixes #6312

lib/base/utility.cpp
lib/base/utility.hpp

index 1a741094b1b46e3b78a4db43ac3b1414ddb5b6eb..f6d1d51bdd97dc8084704cb93b1ab2785fc09aa5 100644 (file)
@@ -978,19 +978,25 @@ int Utility::CompareVersion(const String& v1, const String& v2)
        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
  * we're running on.
  *
  * @returns The FQDN.
  */
-String Utility::GetHostName(void)
+String Utility::GetFQDN(void)
 {
-       char name[255];
-
-       if (gethostname(name, sizeof(name)) < 0) {
-               return "localhost";
-       }
+       String hostname = GetHostName();
 
        addrinfo hints;
        memset(&hints, 0, sizeof(hints));
@@ -999,18 +1005,20 @@ String Utility::GetHostName(void)
        hints.ai_flags = AI_CANONNAME;
 
        addrinfo *result;
-       int rc = getaddrinfo(name, NULL, &hints, &result);
+       int rc = getaddrinfo(hostname.CStr(), NULL, &hints, &result);
 
        if (rc < 0)
                result = NULL;
 
        String canonicalName;
 
-       if (result && strcmp(result->ai_canonname, "localhost") != 0) {
-               canonicalName = result->ai_canonname;
+       if (result) {
+               if (strcmp(result->ai_canonname, "localhost") != 0)
+                       canonicalName = result->ai_canonname;
+
                freeaddrinfo(result);
        } else {
-               canonicalName = name;
+               canonicalName = hostname;
        }
 
        return canonicalName;
index 2db5983132f3768521333ac122a7e1211cd275c5..bf4f0498df6c80cae2cbf21ebabbec2676f40bdd 100644 (file)
@@ -120,6 +120,7 @@ public:
        static int Random(void);
 
        static String GetHostName(void);
+       static String GetFQDN(void);
 
        static tm LocalTime(time_t ts);