2 * Copyright 2009 Oracle. All rights reserved.
4 * This file is part of nfs-utils.
6 * nfs-utils is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * nfs-utils is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with nfs-utils. If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
29 #include <sys/socket.h>
41 * Look up the hostname; report exceptional errors. Caller must
42 * call freeaddrinfo(3) if a valid addrinfo is returned.
45 static struct addrinfo *
46 get_addrinfo(const char *hostname, const struct addrinfo *hint)
48 struct addrinfo *ai = NULL;
51 error = getaddrinfo(hostname, NULL, hint, &ai);
58 xlog(D_GENERAL, "%s: failed to resolve host %s: %s",
59 __func__, hostname, gai_strerror(error));
66 * statd_matchhostname - check if two hostnames are equivalent
67 * @hostname1: C string containing hostname
68 * @hostname2: C string containing hostname
70 * Returns true if the hostnames are the same, the hostnames resolve
71 * to the same canonical name, or the hostnames resolve to at least
72 * one address that is the same. False is returned if the hostnames
73 * do not match in any of these ways, if either hostname contains
74 * wildcard characters, if either hostname is a netgroup name, or
78 statd_matchhostname(const char *hostname1, const char *hostname2)
80 struct addrinfo *ai1, *ai2, *results1 = NULL, *results2 = NULL;
81 struct addrinfo hint = {
82 .ai_family = AF_UNSPEC,
83 .ai_flags = AI_CANONNAME,
84 .ai_protocol = (int)IPPROTO_UDP,
88 if (strcasecmp(hostname1, hostname2) == 0) {
93 results1 = get_addrinfo(hostname1, &hint);
96 results2 = get_addrinfo(hostname2, &hint);
100 if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
105 for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
106 for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
107 if (nfs_compare_sockaddr(ai1->ai_addr, ai2->ai_addr)) {
113 freeaddrinfo(results2);
114 freeaddrinfo(results1);
116 xlog(D_CALL, "%s: hostnames %s", __func__,
117 (result ? "matched" : "did not match"));