186d7f5d3SJohn Marino /* Host name canonicalization
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino Copyright (C) 2005 Free Software Foundation, Inc.
486d7f5d3SJohn Marino
586d7f5d3SJohn Marino Written by Derek Price <derek@ximbiot.com>.
686d7f5d3SJohn Marino
786d7f5d3SJohn Marino This program is free software; you can redistribute it and/or
886d7f5d3SJohn Marino modify it under the terms of the GNU General Public License as
986d7f5d3SJohn Marino published by the Free Software Foundation; either version 2, or (at
1086d7f5d3SJohn Marino your option) any later version.
1186d7f5d3SJohn Marino
1286d7f5d3SJohn Marino This program is distributed in the hope that it will be useful, but
1386d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of
1486d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1586d7f5d3SJohn Marino General Public License for more details.
1686d7f5d3SJohn Marino
1786d7f5d3SJohn Marino You should have received a copy of the GNU General Public License
1886d7f5d3SJohn Marino along with this program; if not, write to the Free Software Foundation,
1986d7f5d3SJohn Marino Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
2086d7f5d3SJohn Marino
2186d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
2286d7f5d3SJohn Marino # include <config.h>
2386d7f5d3SJohn Marino #endif
2486d7f5d3SJohn Marino
2586d7f5d3SJohn Marino #include "canon-host.h"
2686d7f5d3SJohn Marino
2786d7f5d3SJohn Marino #include "getaddrinfo.h"
2886d7f5d3SJohn Marino #include "strdup.h"
2986d7f5d3SJohn Marino
3086d7f5d3SJohn Marino /* Store the last error for the single-threaded version of this function. */
3186d7f5d3SJohn Marino static int last_cherror;
3286d7f5d3SJohn Marino
3386d7f5d3SJohn Marino /* Single-threaded of wrapper for canon_host_r. After a NULL return, error
3486d7f5d3SJohn Marino messages may be retrieved via ch_strerror(). */
3586d7f5d3SJohn Marino char *
canon_host(const char * host)3686d7f5d3SJohn Marino canon_host (const char *host)
3786d7f5d3SJohn Marino {
3886d7f5d3SJohn Marino return canon_host_r (host, &last_cherror);
3986d7f5d3SJohn Marino }
4086d7f5d3SJohn Marino
4186d7f5d3SJohn Marino /* Return a malloc'd string containing the canonical hostname associated with
4286d7f5d3SJohn Marino HOST, or NULL if a canonical name cannot be determined. On NULL return,
4386d7f5d3SJohn Marino if CHERROR is not NULL, set *CHERROR to an error code as returned by
4486d7f5d3SJohn Marino getaddrinfo(). Use ch_strerror_r() or gai_strerror() to convert a *CHERROR
4586d7f5d3SJohn Marino value to a string suitable for error messages.
4686d7f5d3SJohn Marino
4786d7f5d3SJohn Marino WARNINGS
4886d7f5d3SJohn Marino HOST must be a string representation of a resolvable name for this host.
4986d7f5d3SJohn Marino Strings containing an IP address in dotted decimal notation will be
5086d7f5d3SJohn Marino returned as-is, without further resolution.
5186d7f5d3SJohn Marino
5286d7f5d3SJohn Marino The use of the word "canonical" in this context is unfortunate but
5386d7f5d3SJohn Marino entrenched. The value returned by this function will be the end result
5486d7f5d3SJohn Marino of the resolution of any CNAME chains in the DNS. There may only be one
5586d7f5d3SJohn Marino such value for any given hostname, though the actual IP address
5686d7f5d3SJohn Marino referenced by this value and the device using that IP address may each
5786d7f5d3SJohn Marino actually have any number of such "canonical" hostnames. See the POSIX
5886d7f5d3SJohn Marino getaddrinfo spec <http://www.opengroup.org/susv3xsh/getaddrinfo.html">,
5986d7f5d3SJohn Marino RFC 1034 <http://www.faqs.org/rfcs/rfc1034.html>, & RFC 2181
6086d7f5d3SJohn Marino <http://www.faqs.org/rfcs/rfc2181.html> for more on what this confusing
6186d7f5d3SJohn Marino term really refers to. */
6286d7f5d3SJohn Marino char *
canon_host_r(char const * host,int * cherror)6386d7f5d3SJohn Marino canon_host_r (char const *host, int *cherror)
6486d7f5d3SJohn Marino {
6586d7f5d3SJohn Marino char *retval = NULL;
6686d7f5d3SJohn Marino static struct addrinfo hints;
6786d7f5d3SJohn Marino struct addrinfo *res = NULL;
6886d7f5d3SJohn Marino int status;
6986d7f5d3SJohn Marino
7086d7f5d3SJohn Marino hints.ai_flags = AI_CANONNAME;
7186d7f5d3SJohn Marino status = getaddrinfo (host, NULL, &hints, &res);
7286d7f5d3SJohn Marino if (!status)
7386d7f5d3SJohn Marino {
7486d7f5d3SJohn Marino retval = strdup (res->ai_canonname);
7586d7f5d3SJohn Marino if (!retval && cherror)
7686d7f5d3SJohn Marino *cherror = EAI_MEMORY;
7786d7f5d3SJohn Marino freeaddrinfo (res);
7886d7f5d3SJohn Marino }
7986d7f5d3SJohn Marino else if (cherror)
8086d7f5d3SJohn Marino *cherror = status;
8186d7f5d3SJohn Marino
8286d7f5d3SJohn Marino return retval;
8386d7f5d3SJohn Marino }
8486d7f5d3SJohn Marino
8586d7f5d3SJohn Marino /* Return a string describing the last error encountered by canon_host. */
8686d7f5d3SJohn Marino const char *
ch_strerror(void)8786d7f5d3SJohn Marino ch_strerror (void)
8886d7f5d3SJohn Marino {
8986d7f5d3SJohn Marino return gai_strerror (last_cherror);
9086d7f5d3SJohn Marino }
91