Properly print the IP number and "localhost" for failed localhost
authorBruce Momjian <[email protected]>
Sat, 18 Dec 2010 16:25:41 +0000 (11:25 -0500)
committerBruce Momjian <[email protected]>
Sat, 18 Dec 2010 16:26:17 +0000 (11:26 -0500)
connections when the server is down, on Win32.

src/interfaces/libpq/fe-connect.c

index 8d9400be0fc7c30f27a7ac949912d79ef7da4be3..bf8beb7b89903c2e5633c93313e7134eb87cd385 100644 (file)
@@ -1030,20 +1030,25 @@ connectFailureMessage(PGconn *conn, int errorno)
        else
            strcpy(host_addr, "???");
 
+       /*
+        *  If the user did not supply an IP address using 'hostaddr', and
+        *  'host' was missing or does not match our lookup, display the
+        *  looked-up IP address.
+        */
        display_host_addr = (conn->pghostaddr == NULL) &&
-                           (conn->pghost != NULL) &&
-                           (strcmp(conn->pghost, host_addr) != 0);
+                           ((conn->pghost == NULL) ||
+                            (strcmp(conn->pghost, host_addr) != 0));
 
        appendPQExpBuffer(&conn->errorMessage,
                          libpq_gettext("could not connect to server: %s\n"
                     "\tIs the server running on host \"%s\"%s%s%s and accepting\n"
                                        "\tTCP/IP connections on port %s?\n"),
                          SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)),
-                         conn->pghostaddr
+                         (conn->pghostaddr && conn->pghostaddr[0] != '\0')
                          ? conn->pghostaddr
-                         : (conn->pghost
+                         : (conn->pghost && conn->pghost[0] != '\0')
                             ? conn->pghost
-                            : "???"),
+                            : DefaultHost,
                          /* display the IP address only if not already output */
                          display_host_addr ? " (" : "",
                          display_host_addr ? host_addr : "",
@@ -1304,7 +1309,7 @@ connectDBStart(PGconn *conn)
        UNIXSOCK_PATH(portstr, portnum, conn->pgunixsocket);
 #else
        /* Without Unix sockets, default to localhost instead */
-       node = "localhost";
+       node = DefaultHost;
        hint.ai_family = AF_UNSPEC;
 #endif   /* HAVE_UNIX_SOCKETS */
    }
@@ -3388,7 +3393,7 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
    /* hostname */
    hostname = url + strlen(LDAP_URL);
    if (*hostname == '/')       /* no hostname? */
-       hostname = "localhost"; /* the default */
+       hostname = DefaultHost; /* the default */
 
    /* dn, "distinguished name" */
    p = strchr(url + strlen(LDAP_URL), '/');