X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fcanon-host.c;h=e2bd1a773b0cebf8d2232e49322dd13e25d3f38e;hb=5fa5d3d767b29cf1d757609afbb56459b09743fa;hp=9d968a0359bf98f2dcc7dbaa0575ec930f86f971;hpb=968fbdf8796db1bd5120865d8d2905bab6bd17fb;p=gnulib.git diff --git a/lib/canon-host.c b/lib/canon-host.c index 9d968a035..e2bd1a773 100644 --- a/lib/canon-host.c +++ b/lib/canon-host.c @@ -1,8 +1,8 @@ /* Host name canonicalization - Copyright (C) 1995, 1999, 2000 Free Software Foundation, Inc. + Copyright (C) 2005 Free Software Foundation, Inc. - Written by Miles Bader + Written by Derek Price . This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -16,96 +16,75 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifdef HAVE_CONFIG_H # include #endif -#include -#ifdef HAVE_UNISTD_H -# include -#endif -#ifdef HAVE_STRING_H -# include -#endif -#ifdef HAVE_NETDB_H -# include -#endif -#ifdef HAVE_SYS_SOCKET_H -# include -#endif +#include "canon-host.h" -#ifdef HAVE_NETINET_IN_H -# include -#endif -#ifdef HAVE_ARPA_INET_H -# include -#endif +#include "getaddrinfo.h" +#include "strdup.h" -char *strdup (); -void free (); +/* Store the last error for the single-threaded version of this function. */ +static int last_cherror; -/* Returns the canonical hostname associated with HOST (allocated in a static - buffer), or 0 if it can't be determined. */ +/* Single-threaded of wrapper for canon_host_r. After a NULL return, error + messages may be retrieved via ch_strerror(). */ char * canon_host (const char *host) { -#ifdef HAVE_GETHOSTBYNAME - struct hostent *he = gethostbyname (host); + return canon_host_r (host, &last_cherror); +} + +/* Return a malloc'd string containing the canonical hostname associated with + HOST, or NULL if a canonical name cannot be determined. On NULL return, + if CHERROR is not NULL, set *CHERROR to an error code as returned by + getaddrinfo(). Use ch_strerror_r() or gai_strerror() to convert a *CHERROR + value to a string suitable for error messages. - if (he) + WARNINGS + HOST must be a string representation of a resolvable name for this host. + Strings containing an IP address in dotted decimal notation will be + returned as-is, without further resolution. + + The use of the word "canonical" in this context is unfortunate but + entrenched. The value returned by this function will be the end result + of the resolution of any CNAME chains in the DNS. There may only be one + such value for any given hostname, though the actual IP address + referenced by this value and the device using that IP address may each + actually have any number of such "canonical" hostnames. See the POSIX + getaddrinfo spec , + RFC 1034 , & RFC 2181 + for more on what this confusing + term really refers to. */ +char * +canon_host_r (char const *host, int *cherror) +{ + char *retval = NULL; + static struct addrinfo hints; + struct addrinfo *res = NULL; + int status; + + hints.ai_flags = AI_CANONNAME; + status = getaddrinfo (host, NULL, &hints, &res); + if (!status) { -# ifdef HAVE_GETHOSTBYADDR - char *addr = 0; - - /* Try and get an ascii version of the numeric host address. */ - switch (he->h_addrtype) - { -# ifdef HAVE_INET_NTOA - case AF_INET: - addr = inet_ntoa (*(struct in_addr *) he->h_addr); - break; -# endif /* HAVE_INET_NTOA */ - } - - if (addr && strcmp (he->h_name, addr) == 0) - { - /* gethostbyname has returned a string representation of the IP - address, for example, "127.0.0.1". So now, look up the host - name via the address. Although it may seem reasonable to look - up the host name via the address, we must not pass `he->h_addr' - directly to gethostbyaddr because on some systems he->h_addr - is located in a static library buffer that is reused in the - gethostbyaddr call. Make a copy and use that instead. */ - char *h_addr_copy = strdup (he->h_addr); - if (h_addr_copy == NULL) - he = NULL; - else - { - he = gethostbyaddr (h_addr_copy, he->h_length, he->h_addrtype); - free (h_addr_copy); - } - } -# endif /* HAVE_GETHOSTBYADDR */ - - if (he) - return (char *) (he->h_name); + retval = strdup (res->ai_canonname); + if (!retval && cherror) + *cherror = EAI_MEMORY; + freeaddrinfo (res); } -#endif /* HAVE_GETHOSTBYNAME */ - return 0; + else if (cherror) + *cherror = status; + + return retval; } -#ifdef TEST_CANON_HOST -int -main (int argc, char **argv) +/* Return a string describing the last error encountered by canon_host. */ +const char * +ch_strerror (void) { - int i; - for (i = 1; i < argc; i++) - { - char *s = canon_host (argv[i]); - printf ("%s: %s\n", argv[i], (s ? s : "")); - } - exit (0); + return gai_strerror (last_cherror); } -#endif /* TEST_CANON_HOST */