1 /* Host name canonicalization
3 Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
5 Written by Derek Price <derek@ximbiot.com>.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "canon-host.h"
26 #include "getaddrinfo.h"
28 /* Store the last error for the single-threaded version of this function. */
29 static int last_cherror;
31 /* Single-threaded of wrapper for canon_host_r. After a NULL return, error
32 messages may be retrieved via ch_strerror(). */
34 canon_host (const char *host)
36 return canon_host_r (host, &last_cherror);
39 /* Return a malloc'd string containing the canonical hostname associated with
40 HOST, or NULL if a canonical name cannot be determined. On NULL return,
41 if CHERROR is not NULL, set *CHERROR to an error code as returned by
42 getaddrinfo(). Use ch_strerror_r() or gai_strerror() to convert a *CHERROR
43 value to a string suitable for error messages.
46 HOST must be a string representation of a resolvable name for this host.
47 Strings containing an IP address in dotted decimal notation will be
48 returned as-is, without further resolution.
50 The use of the word "canonical" in this context is unfortunate but
51 entrenched. The value returned by this function will be the end result
52 of the resolution of any CNAME chains in the DNS. There may only be one
53 such value for any given hostname, though the actual IP address
54 referenced by this value and the device using that IP address may each
55 actually have any number of such "canonical" hostnames. See the POSIX
56 getaddrinfo spec <http://www.opengroup.org/susv3xsh/getaddrinfo.html">,
57 RFC 1034 <http://www.faqs.org/rfcs/rfc1034.html>, & RFC 2181
58 <http://www.faqs.org/rfcs/rfc2181.html> for more on what this confusing
59 term really refers to. */
61 canon_host_r (char const *host, int *cherror)
64 static struct addrinfo hints;
65 struct addrinfo *res = NULL;
68 hints.ai_flags = AI_CANONNAME;
69 status = getaddrinfo (host, NULL, &hints, &res);
72 /* http://lists.gnu.org/archive/html/bug-coreutils/2006-09/msg00300.html
73 says Darwin 7.9.0 getaddrinfo returns 0 but sets
74 res->ai_canonname to NULL. */
75 retval = strdup (res->ai_canonname ? res->ai_canonname : host);
76 if (!retval && cherror)
77 *cherror = EAI_MEMORY;
86 /* Return a string describing the last error encountered by canon_host. */
90 return gai_strerror (last_cherror);