xref: /dflybsd-src/contrib/cvs-1.12/lib/canon-host.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /* Host name canonicalization
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino    Copyright (C) 2005 Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino 
5*86d7f5d3SJohn Marino    Written by Derek Price <derek@ximbiot.com>.
6*86d7f5d3SJohn Marino 
7*86d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or
8*86d7f5d3SJohn Marino    modify it under the terms of the GNU General Public License as
9*86d7f5d3SJohn Marino    published by the Free Software Foundation; either version 2, or (at
10*86d7f5d3SJohn Marino    your option) any later version.
11*86d7f5d3SJohn Marino 
12*86d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful, but
13*86d7f5d3SJohn Marino    WITHOUT ANY WARRANTY; without even the implied warranty of
14*86d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*86d7f5d3SJohn Marino    General Public License for more details.
16*86d7f5d3SJohn Marino 
17*86d7f5d3SJohn Marino    You should have received a copy of the GNU General Public License
18*86d7f5d3SJohn Marino    along with this program; if not, write to the Free Software Foundation,
19*86d7f5d3SJohn Marino    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20*86d7f5d3SJohn Marino 
21*86d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
22*86d7f5d3SJohn Marino # include <config.h>
23*86d7f5d3SJohn Marino #endif
24*86d7f5d3SJohn Marino 
25*86d7f5d3SJohn Marino #include "canon-host.h"
26*86d7f5d3SJohn Marino 
27*86d7f5d3SJohn Marino #include "getaddrinfo.h"
28*86d7f5d3SJohn Marino #include "strdup.h"
29*86d7f5d3SJohn Marino 
30*86d7f5d3SJohn Marino /* Store the last error for the single-threaded version of this function.  */
31*86d7f5d3SJohn Marino static int last_cherror;
32*86d7f5d3SJohn Marino 
33*86d7f5d3SJohn Marino /* Single-threaded of wrapper for canon_host_r.  After a NULL return, error
34*86d7f5d3SJohn Marino    messages may be retrieved via ch_strerror().  */
35*86d7f5d3SJohn Marino char *
canon_host(const char * host)36*86d7f5d3SJohn Marino canon_host (const char *host)
37*86d7f5d3SJohn Marino {
38*86d7f5d3SJohn Marino   return canon_host_r (host, &last_cherror);
39*86d7f5d3SJohn Marino }
40*86d7f5d3SJohn Marino 
41*86d7f5d3SJohn Marino /* Return a malloc'd string containing the canonical hostname associated with
42*86d7f5d3SJohn Marino    HOST, or NULL if a canonical name cannot be determined.  On NULL return,
43*86d7f5d3SJohn Marino    if CHERROR is not NULL, set *CHERROR to an error code as returned by
44*86d7f5d3SJohn Marino    getaddrinfo().  Use ch_strerror_r() or gai_strerror() to convert a *CHERROR
45*86d7f5d3SJohn Marino    value to a string suitable for error messages.
46*86d7f5d3SJohn Marino 
47*86d7f5d3SJohn Marino    WARNINGS
48*86d7f5d3SJohn Marino      HOST must be a string representation of a resolvable name for this host.
49*86d7f5d3SJohn Marino      Strings containing an IP address in dotted decimal notation will be
50*86d7f5d3SJohn Marino      returned as-is, without further resolution.
51*86d7f5d3SJohn Marino 
52*86d7f5d3SJohn Marino      The use of the word "canonical" in this context is unfortunate but
53*86d7f5d3SJohn Marino      entrenched.  The value returned by this function will be the end result
54*86d7f5d3SJohn Marino      of the resolution of any CNAME chains in the DNS.  There may only be one
55*86d7f5d3SJohn Marino      such value for any given hostname, though the actual IP address
56*86d7f5d3SJohn Marino      referenced by this value and the device using that IP address may each
57*86d7f5d3SJohn Marino      actually have any number of such "canonical" hostnames.  See the POSIX
58*86d7f5d3SJohn Marino      getaddrinfo spec <http://www.opengroup.org/susv3xsh/getaddrinfo.html">,
59*86d7f5d3SJohn Marino      RFC 1034 <http://www.faqs.org/rfcs/rfc1034.html>, & RFC 2181
60*86d7f5d3SJohn Marino      <http://www.faqs.org/rfcs/rfc2181.html> for more on what this confusing
61*86d7f5d3SJohn Marino      term really refers to. */
62*86d7f5d3SJohn Marino char *
canon_host_r(char const * host,int * cherror)63*86d7f5d3SJohn Marino canon_host_r (char const *host, int *cherror)
64*86d7f5d3SJohn Marino {
65*86d7f5d3SJohn Marino   char *retval = NULL;
66*86d7f5d3SJohn Marino   static struct addrinfo hints;
67*86d7f5d3SJohn Marino   struct addrinfo *res = NULL;
68*86d7f5d3SJohn Marino   int status;
69*86d7f5d3SJohn Marino 
70*86d7f5d3SJohn Marino   hints.ai_flags = AI_CANONNAME;
71*86d7f5d3SJohn Marino   status = getaddrinfo (host, NULL, &hints, &res);
72*86d7f5d3SJohn Marino   if (!status)
73*86d7f5d3SJohn Marino     {
74*86d7f5d3SJohn Marino       retval = strdup (res->ai_canonname);
75*86d7f5d3SJohn Marino       if (!retval && cherror)
76*86d7f5d3SJohn Marino 	*cherror = EAI_MEMORY;
77*86d7f5d3SJohn Marino       freeaddrinfo (res);
78*86d7f5d3SJohn Marino     }
79*86d7f5d3SJohn Marino   else if (cherror)
80*86d7f5d3SJohn Marino     *cherror = status;
81*86d7f5d3SJohn Marino 
82*86d7f5d3SJohn Marino   return retval;
83*86d7f5d3SJohn Marino }
84*86d7f5d3SJohn Marino 
85*86d7f5d3SJohn Marino /* Return a string describing the last error encountered by canon_host.  */
86*86d7f5d3SJohn Marino const char *
ch_strerror(void)87*86d7f5d3SJohn Marino ch_strerror (void)
88*86d7f5d3SJohn Marino {
89*86d7f5d3SJohn Marino   return gai_strerror (last_cherror);
90*86d7f5d3SJohn Marino }
91