From: Jim Jagielski Date: Wed, 20 Apr 2005 20:40:46 +0000 (+0000) Subject: APRized ap_get_local_host() X-Git-Tag: 2.1.5~164 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=038b44739bf8a32eb74c67d591a9e393ff73d420;p=apache APRized ap_get_local_host() git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@162066 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/server/util.c b/server/util.c index 4e052226c7..6adb1e2169 100644 --- a/server/util.c +++ b/server/util.c @@ -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); } }