Commit 4c9204a5 authored by Jim Jagielski's avatar Jim Jagielski
Browse files

APRized ap_get_local_host()


git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@162066 13f79535-47bb-0310-9956-ffa450edef68
parent f4c3e43b
Loading
Loading
Loading
Loading
+11 −41
Original line number Diff line number Diff line
@@ -1977,24 +1977,6 @@ AP_DECLARE(void) ap_str_tolower(char *str)
    }
}

static char *find_fqdn(apr_pool_t *a, struct hostent *p)
{
    int x;

    if (!strchr(p->h_name, '.')) {
        if (p->h_aliases) {
            for (x = 0; p->h_aliases[x]; ++x) {
                if (strchr(p->h_aliases[x], '.') &&
                    (!strncasecmp(p->h_aliases[x], p->h_name,
                                  strlen(p->h_name))))
                    return apr_pstrdup(a, p->h_aliases[x]);
            }
        }
        return NULL;
    }
    return apr_pstrdup(a, (void *) p->h_name);
}

char *ap_get_local_host(apr_pool_t *a)
{
#ifndef MAXHOSTNAMELEN
@@ -2002,34 +1984,22 @@ char *ap_get_local_host(apr_pool_t *a)
#endif
    char str[MAXHOSTNAMELEN + 1];
    char *server_hostname = NULL;
    struct hostent *p;
    apr_sockaddr_t *sockaddr;

#ifdef BEOS_R5
    if (gethostname(str, sizeof(str) - 1) == 0)
#else
    if (gethostname(str, sizeof(str) - 1) != 0)
#endif
    {
    if (apr_gethostname(str, sizeof(str) - 1, a) != APR_SUCCESS) {
        ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
                     "%s: gethostname() failed to determine ServerName",
                     "%s: apr_gethostname() failed to determine ServerName",
                     ap_server_argv0);
    }
    else 
    {
    } else {
        str[sizeof(str) - 1] = '\0';
        /* TODO: Screaming for APR-ization */
        if ((!(p = gethostbyname(str))) 
            || (!(server_hostname = find_fqdn(a, p)))) {
            /* Recovery - return the default servername by IP: */
            if (p && p->h_addr_list[0]) {
                apr_snprintf(str, sizeof(str), "%pA", p->h_addr_list[0]);
                server_hostname = apr_pstrdup(a, str);
                /* We will drop through to report the IP-named server */
            }
        }
        else {
            /* Since we found a fdqn, return it with no logged message. */
        if (apr_sockaddr_info_get(&sockaddr, str, AF_INET, 0, 0, a) == APR_SUCCESS) {
            server_hostname = apr_pstrdup(a, sockaddr->hostname);
            return server_hostname;
        } else {
            ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
                         "%s: apr_sockaddr_info_get() failed for hostname '%s'",
                         ap_server_argv0, str);
            server_hostname = apr_pstrdup(a, str);
        }
    }