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 #include <sys/cdefs.h>
21 __RCSID("$NetBSD: canon-host.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
22
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include "canon-host.h"
29
30 #include "getaddrinfo.h"
31 #include "strdup.h"
32
33 /* Store the last error for the single-threaded version of this function. */
34 static int last_cherror;
35
36 /* Single-threaded of wrapper for canon_host_r. After a NULL return, error
37 messages may be retrieved via ch_strerror(). */
38 char *
canon_host(const char * host)39 canon_host (const char *host)
40 {
41 return canon_host_r (host, &last_cherror);
42 }
43
44 /* Return a malloc'd string containing the canonical hostname associated with
45 HOST, or NULL if a canonical name cannot be determined. On NULL return,
46 if CHERROR is not NULL, set *CHERROR to an error code as returned by
47 getaddrinfo(). Use ch_strerror_r() or gai_strerror() to convert a *CHERROR
48 value to a string suitable for error messages.
49
50 WARNINGS
51 HOST must be a string representation of a resolvable name for this host.
52 Strings containing an IP address in dotted decimal notation will be
53 returned as-is, without further resolution.
54
55 The use of the word "canonical" in this context is unfortunate but
56 entrenched. The value returned by this function will be the end result
57 of the resolution of any CNAME chains in the DNS. There may only be one
58 such value for any given hostname, though the actual IP address
59 referenced by this value and the device using that IP address may each
60 actually have any number of such "canonical" hostnames. See the POSIX
61 getaddrinfo spec <http://www.opengroup.org/susv3xsh/getaddrinfo.html">,
62 RFC 1034 <http://www.faqs.org/rfcs/rfc1034.html>, & RFC 2181
63 <http://www.faqs.org/rfcs/rfc2181.html> for more on what this confusing
64 term really refers to. */
65 char *
canon_host_r(char const * host,int * cherror)66 canon_host_r (char const *host, int *cherror)
67 {
68 char *retval = NULL;
69 static struct addrinfo hints;
70 struct addrinfo *res = NULL;
71 int status;
72
73 hints.ai_flags = AI_CANONNAME;
74 status = getaddrinfo (host, NULL, &hints, &res);
75 if (!status)
76 {
77 retval = strdup (res->ai_canonname);
78 if (!retval && cherror)
79 *cherror = EAI_MEMORY;
80 freeaddrinfo (res);
81 }
82 else if (cherror)
83 *cherror = status;
84
85 return retval;
86 }
87
88 /* Return a string describing the last error encountered by canon_host. */
89 const char *
ch_strerror(void)90 ch_strerror (void)
91 {
92 return gai_strerror (last_cherror);
93 }
94