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