getnameinfo(3)
- NetBSD Manual Pages
GETNAMEINFO(3) NetBSD Library Functions Manual GETNAMEINFO(3)
NAME
getnameinfo -- socket address structure to hostname and service name
SYNOPSIS
#include <netdb.h>
int
getnameinfo(const struct sockaddr * restrict sa, socklen_t salen,
char * restrict host, socklen_t hostlen, char * restrict serv,
socklen_t servlen, int flags);
DESCRIPTION
The getnameinfo() 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 a sockaddr_in (for IPv4),
sockaddr_in6 (for IPv6), sockaddr_atalk (for AppleTalk), sockaddr_link
(for link layer), or sockaddr_local (for local/unix) structures that are
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 NI_MAXHOST and the maximum value for servlen is NI_MAXSERV, as
defined by <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 flags argument is formed by or'ing the following values:
NI_NOFQDN A fully qualified domain name is not required for local
hosts. The local part of the fully qualified domain name
is returned instead.
NI_NUMERICHOST
Return the address in numeric form, as if calling
inet_ntop(3), instead of a host name.
NI_NAMEREQD 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.
NI_NUMERICSCOPE
For IPv6 addresses the numeric form of the IPv6 scope iden-
tifier is returned. This flag is ignored for non-IPv6
addresses.
NI_NUMERICSERV
The service name is returned as a digit string representing
the port number.
NI_DGRAM Specifies that the service being looked up is a datagram
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 dif-
ferent 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
getnameinfo() returns zero on success or one of the error codes listed in
gai_strerror(3) if an error occurs.
EXAMPLES
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[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);
SEE ALSO
gai_strerror(3), getaddrinfo(3), gethostbyaddr(3), getservbyport(3),
inet_ntop(3), resolver(3), hosts(5), resolv.conf(5), services(5),
hostname(7)<