#include <sys/socket.h> #include <netdb.h>
int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
Since glibc 2.22: _POSIX_C_SOURCE >= 201112L
Glibc 2.21 and earlier: _POSIX_C_SOURCE
- If set, then an error is returned if the hostname cannot be determined.
- If set, then the service is datagram (UDP) based rather than stream (TCP) based. This is required for the few ports (512–514) that have different services for UDP and TCP.
- If set, return only the hostname part of the fully qualified domain name for local hosts.
- If set, then the numeric form of the hostname is returned. (When not set, this will still happen in case the node's name cannot be determined.)
- If set, then the numeric form of the service address is returned. (When not set, this will still happen in case the service's name cannot be determined.)
- If this flag is used, then the name found in the lookup process is converted from IDN format to the locale's encoding if necessary. ASCII-only names are not affected by the conversion, which makes this flag usable in existing programs and environments.
- NI_IDN_ALLOW_UNASSIGNED, NI_IDN_USE_STD3_ASCII_RULES
- Setting these flags will enable the IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3 conforming hostname) flags respectively to be used in the IDNA handling.
- The name could not be resolved at this time. Try again later.
- The flags argument has an invalid value.
- A nonrecoverable error occurred.
- The address family was not recognized, or the address length was invalid for the specified family.
- Out of memory.
- The name does not resolve for the supplied arguments. NI_NAMEREQD is set and the host's name cannot be located, or neither hostname nor service name were requested.
- The buffer pointed to by host or serv was too small.
- A system error occurred. The error code can be found in errno.
|getnameinfo ()||Thread safety||MT-Safe env locale|
#define NI_MAXHOST 1025 #define NI_MAXSERV 32
Since glibc 2.8, these definitions are exposed only if suitable feature test macros are defined, namely: _GNU_SOURCE, _DEFAULT_SOURCE (since glibc 2.19), or (in glibc versions up to and including 2.19) _BSD_SOURCE or _SVID_SOURCE. The former is the constant MAXDNAME in recent versions of BIND's <arpa/nameser.h> header file. The latter is a guess based on the services listed in the current Assigned Numbers RFC. Before glibc version 2.2, the hostlen and servlen arguments were typed as size_t.
struct sockaddr *addr; /* input */ socklen_t addrlen; /* input */ char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0) printf("host=%s, serv=%s\n", hbuf, sbuf);
The following version checks if the socket address has a reverse address mapping.
struct sockaddr *addr; /* input */ socklen_t addrlen; /* input */ char hbuf[NI_MAXHOST]; if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), NULL, 0, NI_NAMEREQD)) printf("could not resolve hostname"); else printf("host=%s\n", hbuf);
An example program using getnameinfo() can be found in getaddrinfo(3).