Maps addresses to names in a protocol-independent way. Format #include <socket.h> #include <netdb.h> int getnameinfo ( const struct sockaddr *sa, size_t salen, char *node, size_t nodelen, char *service, size_t servicelen, int flags );
1 – Arguments
sa Points either to a sockaddr_in structure (for IPv4) or to a sockaddr_in6 structure (for IPv6) that holds the IP address and port number. salen Specifies the length of either the sockaddr_in structure or the sockaddr_in6 structure. node Points to a buffer in which to receive the null-terminated network node name or alias corresponding to the address contained in the sa. A NULL pointer instructs getnameinfo() to not return a node name. The node argument and service argument must not both be zero. nodelen Specifies the length of the node buffer. A value of zero instructs getnameinfo() to not return a node name. service Points to a buffer in which to receive the null-terminated network service name associated with the port number contained in sa. A NULL pointer instructs getnameinfo() to not return a service name. The node argument and service argument must not both be 0. servicelen Specifies the length of the service buffer. A value of zero instructs getnameinfo() to not return a service name. flags Specifies changes to the default actions of getnameinfo(). By default, getnameinfo() searches for the fully-qualified domain name of the node in the hosts database and returns it. See Flags for a list of flags and their meanings.
2 – Description
This function looks up an IP address and port number in a sockaddr structure specified by sa and returns node name and service name text strings in the buffers pointed to by the node and service arguments, respectively. If the node name is not found, getnameinfo() returns the numeric form of the node address, regardless of the value of the flags argument. If the service name is not found, getnameinfo() returns the numeric form of the service address (port number) regardless of the value of the flags argument. The application must provide buffers large enough to hold the fully-qualified domain name and the service name, including the terminating null characters. Flags describes the flag bits and, if set, their meanings. The flags are defined in the NETDB.H header file. Table 4-2 getnameinfo() Flags Flag Value Description NI_DGRAM Specifies that the service is a datagram service (SOCK_DGRAM). The default assumes a stream service (SOCK_STREAM). This is required for the few ports (512-514) that have different services for UDP and TCP. NI_NAMEREQD Returns an error if the host name cannot be located in the hosts database. NI_NOFQDN Searches the hosts database and returns the node name portion of the fully-qualified domain name for local hosts. NI_NUMERICHOST Returns the numeric form of the host's address instead of its name. Resolution of the host name is not performed. NI_NUMERICSERV Returns the numeric form (port number) of the service address instead of its name. The host name is not resolved.
3 – Return Values
0 Indicates success. x Indicates an error occurred. The value of errno indicates the error.
4 – Errors
EAI_AGAIN The name could not be resolved at this time. Future attempts may succeed. EAI_BADFLAGS The flags argument had an invalid value. EAI_FAIL A nonrecoverable error occurred when attempting to resolve the name. EAI_FAMILY The address family was not recognized. EAI_MEMORY There was a memory allocation failure when trying to allocate storage for the return value. EAI_NONAME The name does not resolve for the supplied parameters. Neither the node name nor the service name were supplied. At least one of these must be supplied. EAI_SYSTEM A system error occurred; the error code can be found in errno.