#include <sys.socket.h>
#include <netdb.h>
Kernighan-Ritchie-C:
int getnameinfo (sa, salen, host, hostlen, serv, servlen, flags);
const struct sockaddr *sa;
socklen_t salen;
char *host;
size_t hostlen;
char *serv;
size_t servlen;
int flags;
ANSI-C:
int getnameinfo (const struct sockaddr* sa, socklen_t salen, char* host,
size_t hostlen, char* serv, size_t servlen, int flags);
Description
The getnameinfo() function returns the name assigned to the IP address and port number specified in the call as a text string. The values are determined using either the DNS service or system-specific tables.
The sa parameter is a pointer to a sockaddr_in structure (for IPv4) or a sockaddr_in6 structure (for IPv6), which contains the IP address and port number.
salen indicates the length of these structures.
If getnameinfo() is executed successfully, host is a pointer to the socket host name which corresponds to the specified IP address. The socket host name is terminated with the null byte, and its length corresponds to the value of hostlen. The same applies to the service name which corresponds to the specified port number. This is the service name to which the pointer serv points, and its length (including the null byte) corresponds to the value of servlen.
If the value 0 is specified for hostlen or servlen when getnameinfo() is called, this indicates that no name is to be returned in the corresponding host parameter or no service name or port number is to be returned in the serv parameter respectively.
However, a sufficiently large buffer, which can accommodate the host and service names including the null byte, must be made available for the desired information.
Specification of the maximum lengths for DNS and service names in <netdb.h>:
|
|
|
|
The flags parameter changes how getnameinfo() is executed. Normally, the fully-qualified domain name of the host is determined from the DNS and returned. Depending on the value of flags, a distinction is made between the following cases:
If the flags bit NI_NOFQDN is set, only the host name part of an FQDN is returned.
The LWRESD resolver which is common to the SOCKETS(BS2000), BCAM and POSIX sockets is used to access the DNS. For more details, see the manual “BCAM Volume 1/2”.
If the flags bit NI_NUMERICHOST is set, or it is impossible to determine the host name in the DNS or using local information, the numeric host name is returned in printable format after address conversion.
If the flags bit NI_NAMEREQD is set, an error is reported if the host name cannot be determined in the DNS.
If the flags bit NI_NAMEREQD is set in combination with NI_NOFQDN, the bit has no effect.If the flags bit NI_NUMERICSERV is set, the port number is returned in printable format instead of the service name.
If the flags bit NI_DGRAM is set, the service name for the udp protocol is returned. If NI_DGRAM is not specified, the service name for the tcp protocol is always returned.
Return value
0:
If successful.
>
0:
If an error occurs
As thread savety is required for the DNS Resolver, errnos cannot be set.
If the return value > 0, it corresponds to the value of an EAI_xxx error code as defined in <netdb.h>.
<0:
If an error occurs
An error has occurred, which prevents execution of the function. Therefore errno is set.