getnameinfo - socket address structure to hostname and service name
If a link-layer address is passed to
getnameinfo (,);
its ASCII representation will be stored in
Fa host .
The string pointed to by
Fa serv
will be set to the empty string if non-NULL;
Fa flags
will always be ignored.
This is intended as a replacement for the legacy
link_ntoa3
function.
The sockaddr structure Fa sa should point to either a sockaddr_in sockaddr_in6 or sockaddr_dl structure (for IPv4, IPv6 or link-layer respectively) that is Fa salen bytes long.
The host and service names associated with Fa sa are stored in Fa host and Fa serv which have length parameters Fa hostlen and Fa servlen . The maximum value for Fa hostlen is NI_MAXHOST and the maximum value for Fa servlen is NI_MAXSERV as defined by Aq Pa netdb.h . If a length parameter is zero, no string will be stored. Otherwise, enough space must be provided to store the host name or service string plus a byte for the NUL terminator.
The Fa flags argument is formed by OR 'ing the following values:
This implementation allows numeric IPv6 address notation with scope identifier, as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt. IPv6 link-local address will appear as a string like ``fe80::1%ne0 '' Refer to getaddrinfo(3) for more information.
struct sockaddr *sa; /* input */
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
errx(1, "could not get numeric hostname");
/*NOTREACHED*/
}
printf("host=%s, serv=%s\n", hbuf, sbuf);
The following version checks if the socket address has a reverse address mapping:
struct sockaddr *sa; /* input */
char hbuf[NI_MAXHOST];
if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
NI_NAMEREQD)) {
errx(1, "could not resolve hostname");
/*NOTREACHED*/
}
printf("host=%s\n", hbuf);
1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
and trick the caller of
getnameinfo ();
into believing that
Fa sa
is
10.1.1.1
when it is actually
127.0.0.1
To prevent such attacks, the use of
NI_NAMEREQD
is recommended when the result of
getnameinfo ();
is used
for access control purposes:
struct sockaddr *sa;
socklen_t salen;
char addr[NI_MAXHOST];
struct addrinfo hints, *res;
int error;
error = getnameinfo(sa, salen, addr, sizeof(addr),
NULL, 0, NI_NAMEREQD);
if (error == 0) {
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
hints.ai_flags = AI_NUMERICHOST;
if (getaddrinfo(addr, "0", &hints, &res) == 0) {
/* malicious PTR record */
freeaddrinfo(res);
printf("bogus PTR record\n");
return -1;
}
/* addr is FQDN as a result of PTR lookup */
} else {
/* addr is numeric string */
error = getnameinfo(sa, salen, addr, sizeof(addr),
NULL, 0, NI_NUMERICHOST);
}
|
Закладки на сайте Проследить за страницей |
Created 1996-2025 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |