Manual Pages for UNIX Darwin command on man getnameinfo
MyWebUniversity

Manual Pages for UNIX Darwin command on man getnameinfo

GETNAMEINFO(3) BSD Library Functions Manual GETNAMEINFO(3)

NAME

ggeettnnaammeeiinnffoo - socket address structure to hostname and service name

SYNOPSIS

##iinncclluuddee <>

##iinncclluuddee <>

##iinncclluuddee <>

int ggeettnnaammeeiinnffoo(const struct sockaddr *sa, socklent salen, char *host, sizet hostlen, char *serv, sizet servlen, int flags);

DESCRIPTION

The ggeettnnaammeeiinnffoo() function is used to convert a sockaddr structure to a

pair of host name and service strings. It is a replacement for and pro-

vides more flexibility than the gethostbyaddr(3) and getservbyport(3) functions and is the converse of the getaddrinfo(3) function. The sockaddr structure sa should point to either a sockaddrin or sockaddrin6 structure (for IPv4 or IPv6 respectively) that is salen bytes long. The host and service names associated with sa are stored in host and serv which have length parameters hostlen and servlen. The maximum value for hostlen is NIMAXHOST and the maximum value for servlen is NIMAXSERV, as defined by . 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 flags argument is formed by OORR'ing the following values: NINOFQDN A fully qualified domain name is not required for local hosts. The local part of the fully qualified domain name is returned instead. NINUMERICHOST Return the address in numeric form, as if calling inetntop(3), instead of a host name.

NINAMEREQD A name is required. If the host name cannot be found

in DNS and this flag is set, a non-zero error code is

returned. If the host name is not found and the flag is not set, the address is returned in numeric form.

NINUMERICSERV The service name is returned as a digit string repre-

senting the port number.

NIDGRAM Specifies that the service being looked up is a data-

gram service, and causes getservbyport(3) to be called with a second argument of ``udp'' instead of its default of ``tcp''. This is required for the few ports

(512-514) that have different services for UDP and TCP.

This implementation allows numeric IPv6 address notation with scope iden-

tifier, 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.

RETURN VALUES

ggeettnnaammeeiinnffoo() returns zero on success or one of the error codes listed in gaistrerror(3) if an error occurs. EEXXAAMMPPLLEESS The following code tries to get a numeric host name, and service name, for a given socket address. Observe that there is no hardcoded reference to a particular address family. struct sockaddr *sa; /* input */ char hbuf[NIMAXHOST], sbuf[NIMAXSERV];

if (getnameinfo(sa, sa->salen, hbuf, sizeof(hbuf), sbuf,

sizeof(sbuf), NINUMERICHOST | NINUMERICSERV)) { 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[NIMAXHOST];

if (getnameinfo(sa, sa->salen, hbuf, sizeof(hbuf), NULL, 0,

NINAMEREQD)) {

errx(1, "could not resolve hostname"); /*NOTREACHED*/ }

printf("host=%s\n", hbuf);

SEE ALSO

gaistrerror(3), getaddrinfo(3), gethostbyaddr(3), getservbyport(3), inetntop(3), resolver(3), hosts(5), resolv.conf(5), services(5), hostname(7), named(8) R. Gilligan, S. Thomson, J. Bound, and W. Stevens, Basic Socket Interface Extensions for IPv6, RFC 2553, March 1999. S. Deering, B. Haberman, T. Jinmei, E. Nordmark, and B. Zill, IPv6 Scoped

Address Architecture, internet draft, draft-ietf-ipv6-scoping-

arch-02.txt, work in progress material.

Craig Metz, "Protocol Independence Using the Sockets API", Proceedings of the FREENIX track: 2000 USENIX annual technical conference, June 2000. STANDARDS

The ggeettnnaammeeiinnffoo() function is defined by the IEEE Std 1003.1g-2000

(``POSIX.1'') draft specification and documented in RRFFCC 22555533, ``Basic Socket Interface Extensions for IPv6''. CCAAVVEEAATTSS

ggeettnnaammeeiinnffoo() can return both numeric and FQDN forms of the address spec-

ified in sa. There is no return value that indicates whether the string

returned in host is a result of binary to numeric-text translation (like

inetntop(3)), or is the result of a DNS reverse lookup. Because of this, malicious parties could set up a PTR record as follows:

1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1

and trick the caller of ggeettnnaammeeiinnffoo() into believing that sa is 10.1.1.1 when it is actually 127.0.0.1.

To prevent such attacks, the use of NINAMEREQD is recommended when the

result of ggeettnnaammeeiinnffoo() is used for access control purposes: struct sockaddr *sa; socklent salen; char addr[NIMAXHOST]; struct addrinfo hints, *res; int error;

error = getnameinfo(sa, salen, addr, sizeof(addr),

NULL, 0, NINAMEREQD);

if (error == 0) { memset(&hints, 0, sizeof(hints)); hints.aisocktype = SOCKDGRAM; /*dummy*/ hints.aiflags = AINUMERICHOST; 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, NINUMERICHOST); }

BUGS

The implementation of ggeettnnaammeeiinnffoo() is not thread-safe.

BSD December 20, 2004 BSD




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™