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>
36 #include <arpa/inet.h>
43 * statd_present_address - convert sockaddr to presentation address
44 * @sap: pointer to socket address to convert
45 * @buf: pointer to buffer to fill in
46 * @buflen: length of buffer
48 * Convert the passed-in sockaddr-style address to presentation format.
49 * The presentation format address is placed in @buf and is
52 * Returns true if successful; otherwise false.
54 * getnameinfo(3) is preferred, since it can parse IPv6 scope IDs.
55 * An alternate version of statd_present_address() is available to
56 * handle older glibcs that do not have getnameinfo(3).
58 #ifdef HAVE_GETNAMEINFO
60 statd_present_address(const struct sockaddr *sap, char *buf, const size_t buflen)
65 salen = nfs_sockaddr_length(sap);
67 xlog(D_GENERAL, "%s: unsupported address family",
72 error = getnameinfo(sap, salen, buf, (socklen_t)buflen,
73 NULL, 0, NI_NUMERICHOST);
75 xlog(D_GENERAL, "%s: getnameinfo(3): %s",
76 __func__, gai_strerror(error));
81 #else /* !HAVE_GETNAMEINFO */
83 statd_present_address(const struct sockaddr *sap, char *buf, const size_t buflen)
85 const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
87 if (sin->sin_family != AF_INET) {
88 xlog(D_GENERAL, "%s: unsupported address family", __func__);
92 /* ensure '\0' termination */
93 memset(buf, 0, buflen);
95 if (inet_ntop(AF_INET, (char *)&sin->sin_addr,
96 buf, (socklen_t)buflen) == NULL) {
97 xlog(D_GENERAL, "%s: inet_ntop(3): %m", __func__);
102 #endif /* !HAVE_GETNAMEINFO */
105 * Look up the hostname; report exceptional errors. Caller must
106 * call freeaddrinfo(3) if a valid addrinfo is returned.
109 static struct addrinfo *
110 get_addrinfo(const char *hostname, const struct addrinfo *hint)
112 struct addrinfo *ai = NULL;
115 error = getaddrinfo(hostname, NULL, hint, &ai);
122 xlog(D_GENERAL, "%s: failed to resolve host %s: %s",
123 __func__, hostname, gai_strerror(error));
130 * statd_matchhostname - check if two hostnames are equivalent
131 * @hostname1: C string containing hostname
132 * @hostname2: C string containing hostname
134 * Returns true if the hostnames are the same, the hostnames resolve
135 * to the same canonical name, or the hostnames resolve to at least
136 * one address that is the same. False is returned if the hostnames
137 * do not match in any of these ways, if either hostname contains
138 * wildcard characters, if either hostname is a netgroup name, or
139 * if an error occurs.
142 statd_matchhostname(const char *hostname1, const char *hostname2)
144 struct addrinfo *ai1, *ai2, *results1 = NULL, *results2 = NULL;
145 struct addrinfo hint = {
146 .ai_family = AF_UNSPEC,
147 .ai_flags = AI_CANONNAME,
148 .ai_protocol = (int)IPPROTO_UDP,
150 _Bool result = false;
152 if (strcasecmp(hostname1, hostname2) == 0) {
157 results1 = get_addrinfo(hostname1, &hint);
158 if (results1 == NULL)
160 results2 = get_addrinfo(hostname2, &hint);
161 if (results2 == NULL)
164 if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
169 for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
170 for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
171 if (nfs_compare_sockaddr(ai1->ai_addr, ai2->ai_addr)) {
177 freeaddrinfo(results2);
178 freeaddrinfo(results1);
180 xlog(D_CALL, "%s: hostnames %s", __func__,
181 (result ? "matched" : "did not match"));