1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*
* Copyright 2010 Oracle. All rights reserved.
*
* This file is part of nfs-utils.
*
* nfs-utils is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* nfs-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with nfs-utils. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include "sockaddr.h"
#include "exportfs.h"
/**
* host_ntop - generate presentation address given a sockaddr
* @sap: pointer to socket address
* @buf: working storage
* @buflen: size of @buf in bytes
*
* Returns a pointer to a @buf.
*/
#ifdef HAVE_GETNAMEINFO
char *
host_ntop(const struct sockaddr *sap, char *buf, const size_t buflen)
{
socklen_t salen = nfs_sockaddr_length(sap);
int error;
memset(buf, 0, buflen);
if (salen == 0) {
(void)strncpy(buf, "bad family", buflen - 1);
return buf;
}
error = getnameinfo(sap, salen, buf, (socklen_t)buflen,
NULL, 0, NI_NUMERICHOST);
if (error != 0) {
buf[0] = '\0';
(void)strncpy(buf, "bad address", buflen - 1);
}
return buf;
}
#else /* !HAVE_GETNAMEINFO */
char *
host_ntop(const struct sockaddr *sap, char *buf, const size_t buflen)
{
const struct sockaddr_in *sin = (const struct sockaddr_in *)(char *)sap;
memset(buf, 0, buflen);
if (sin->sin_family != AF_INET) {
(void)strncpy(buf, "bad family", buflen - 1);
return buf;
}
if (inet_ntop(AF_INET, &sin->sin_addr.s_addr, buf, buflen) != NULL)
return buf;
buf[0] = '\0';
(void)strncpy(buf, "bad address", buflen - 1);
return buf;
}
#endif /* !HAVE_GETNAMEINFO */
/**
* host_pton - return addrinfo for a given presentation address
* @paddr: pointer to a '\0'-terminated ASCII string containing an
* IP presentation address
*
* Returns address info structure, or NULL if an error occurs. Caller
* must free the returned structure with freeaddrinfo(3).
*/
__attribute__((__malloc__))
struct addrinfo *
host_pton(const char *paddr)
{
struct addrinfo *ai = NULL;
struct addrinfo hint = {
/* don't return duplicates */
.ai_protocol = (int)IPPROTO_UDP,
.ai_flags = AI_NUMERICHOST,
.ai_family = AF_UNSPEC,
};
struct sockaddr_in sin;
int error, inet4;
/*
* Although getaddrinfo(3) is easier to use and supports
* IPv6, it recognizes incomplete addresses like "10.4"
* as valid AF_INET addresses. It also accepts presentation
* addresses that end with a blank.
*
* inet_pton(3) is much stricter. Use it to be certain we
* have a real AF_INET presentation address, before invoking
* getaddrinfo(3) to generate the full addrinfo list.
*/
if (paddr == NULL) {
xlog(D_GENERAL, "%s: passed a NULL presentation address",
__func__);
return NULL;
}
inet4 = 1;
if (inet_pton(AF_INET, paddr, &sin.sin_addr) == 0)
inet4 = 0;
error = getaddrinfo(paddr, NULL, &hint, &ai);
switch (error) {
case 0:
if (!inet4 && ai->ai_addr->sa_family == AF_INET) {
xlog(D_GENERAL, "%s: failed to convert %s",
__func__, paddr);
nfs_freeaddrinfo(ai);
break;
}
return ai;
case EAI_NONAME:
break;
case EAI_SYSTEM:
xlog(L_WARNING, "%s: failed to convert %s: (%d) %m",
__func__, paddr, errno);
break;
default:
xlog(L_WARNING, "%s: failed to convert %s: %s",
__func__, paddr, gai_strerror(error));
break;
}
return NULL;
}
/**
* host_addrinfo - return addrinfo for a given hostname
* @hostname: pointer to a '\0'-terminated ASCII string containing a hostname
*
* Returns address info structure with ai_canonname filled in, or NULL
* if no information is available for @hostname. Caller must free the
* returned structure with freeaddrinfo(3).
*/
__attribute__((__malloc__))
struct addrinfo *
host_addrinfo(const char *hostname)
{
struct addrinfo *ai = NULL;
struct addrinfo hint = {
#ifdef IPV6_SUPPORTED
.ai_family = AF_UNSPEC,
#else
.ai_family = AF_INET,
#endif
/* don't return duplicates */
.ai_protocol = (int)IPPROTO_UDP,
.ai_flags = AI_CANONNAME,
};
int error;
error = getaddrinfo(hostname, NULL, &hint, &ai);
switch (error) {
case 0:
return ai;
case EAI_SYSTEM:
xlog(D_PARSE, "%s: failed to resolve %s: (%d) %m",
__func__, hostname, errno);
break;
default:
xlog(D_PARSE, "%s: failed to resolve %s: %s",
__func__, hostname, gai_strerror(error));
break;
}
return NULL;
}
/**
* host_canonname - return canonical hostname bound to an address
* @sap: pointer to socket address to look up
*
* Discover the canonical hostname associated with the given socket
* address. The host's reverse mapping is verified in the process.
*
* Returns a '\0'-terminated ASCII string containing a hostname, or
* NULL if no hostname can be found for @sap. Caller must free
* the string.
*/
#ifdef HAVE_GETNAMEINFO
__attribute__((__malloc__))
char *
host_canonname(const struct sockaddr *sap)
{
socklen_t salen = nfs_sockaddr_length(sap);
char buf[NI_MAXHOST];
int error;
if (salen == 0) {
xlog(D_GENERAL, "%s: unsupported address family %d",
__func__, sap->sa_family);
return NULL;
}
memset(buf, 0, sizeof(buf));
error = getnameinfo(sap, salen, buf, (socklen_t)sizeof(buf),
NULL, 0, NI_NAMEREQD);
switch (error) {
case 0:
break;
case EAI_SYSTEM:
xlog(D_GENERAL, "%s: getnameinfo(3) failed: (%d) %m",
__func__, errno);
return NULL;
default:
(void)getnameinfo(sap, salen, buf, (socklen_t)sizeof(buf),
NULL, 0, NI_NUMERICHOST);
xlog(D_PARSE, "%s: failed to resolve %s: %s",
__func__, buf, gai_strerror(error));
return NULL;
}
return strdup(buf);
}
#else /* !HAVE_GETNAMEINFO */
__attribute__((__malloc__))
char *
host_canonname(const struct sockaddr *sap)
{
const struct sockaddr_in *sin = (const struct sockaddr_in *)(char *)sap;
const struct in_addr *addr = &sin->sin_addr;
struct hostent *hp;
if (sap->sa_family != AF_INET)
return NULL;
hp = gethostbyaddr(addr, (socklen_t)sizeof(addr), AF_INET);
if (hp == NULL)
return NULL;
return strdup(hp->h_name);
}
#endif /* !HAVE_GETNAMEINFO */
/**
* host_reliable_addrinfo - return addrinfo for a given address
* @sap: pointer to socket address to look up
*
* Reverse and forward lookups are performed to ensure the address has
* matching forward and reverse mappings.
*
* Returns addrinfo structure with just the provided address. If there
* is a problem with resolution or the resolved records don't match up
* properly then returns NULL.
*
* Caller must free the returned structure with freeaddrinfo(3).
*/
__attribute__((__malloc__))
struct addrinfo *
host_reliable_addrinfo(const struct sockaddr *sap)
{
struct addrinfo *ai, *a;
char *hostname;
ai = NULL;
hostname = host_canonname(sap);
if (hostname == NULL)
goto out;
ai = host_addrinfo(hostname);
free(hostname);
if (!ai)
goto out;
/* make sure there's a matching address in the list */
for (a = ai; a; a = a->ai_next)
if (nfs_compare_sockaddr(a->ai_addr, sap))
break;
nfs_freeaddrinfo(ai);
ai = NULL;
if (!a)
goto out;
/* get addrinfo with just the original address */
ai = host_numeric_addrinfo(sap);
out:
return ai;
}
/**
* host_numeric_addrinfo - return addrinfo without doing DNS queries
* @sap: pointer to socket address
*
* Returns address info structure, or NULL if an error occurred.
* Caller must free the returned structure with freeaddrinfo(3).
*/
#ifdef HAVE_GETNAMEINFO
__attribute__((__malloc__))
struct addrinfo *
host_numeric_addrinfo(const struct sockaddr *sap)
{
socklen_t salen = nfs_sockaddr_length(sap);
char buf[INET6_ADDRSTRLEN];
int error;
if (salen == 0) {
xlog(D_GENERAL, "%s: unsupported address family %d",
__func__, sap->sa_family);
return NULL;
}
memset(buf, 0, sizeof(buf));
error = getnameinfo(sap, salen, buf, (socklen_t)sizeof(buf),
NULL, 0, NI_NUMERICHOST);
switch (error) {
case 0:
break;
case EAI_SYSTEM:
xlog(D_GENERAL, "%s: getnameinfo(3) failed: (%d) %m",
__func__, errno);
return NULL;
default:
xlog(D_GENERAL, "%s: getnameinfo(3) failed: %s",
__func__, gai_strerror(error));
return NULL;
}
return host_pton(buf);
}
#else /* !HAVE_GETNAMEINFO */
__attribute__((__malloc__))
struct addrinfo *
host_numeric_addrinfo(const struct sockaddr *sap)
{
const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
const struct in_addr *addr = &sin->sin_addr;
char buf[INET_ADDRSTRLEN];
if (sap->sa_family != AF_INET)
return NULL;
memset(buf, 0, sizeof(buf));
if (inet_ntop(AF_INET, (char *)addr, buf,
(socklen_t)sizeof(buf)) == NULL)
return NULL;
return host_pton(buf);
}
#endif /* !HAVE_GETNAMEINFO */
|