gethostbyaddr: Get host information for IP address

The gethostbyaddr socket function returns information about a host specified by an Internet Protocol (IP) address.

Last updated

Added for PUT00.

Format

#include  <netdb.h>
struct hostent *gethostbyaddr(char *addr,
                              int addrlen,
                              int domain);
addr
A pointer to an IP address in network byte order.
addrlen
The size of the Internet address in bytes.
domain
The address domain supported (AF_INET).

Normal return

This function returns a pointer to a hostent structure for the host name specified on the call. The netdb.h header file defines the hostent structure, which contains the following elements:
h_name
Official name of the host.
h_aliases
Zero-terminated array of alternative names for the host.
h_addrtype
Type of address being returned, always set to AF_INET.
h_length
Length of the address in bytes.
h_addr
Pointer to the network address of the host in network byte order.
Note: Subsequent gethostbyaddr calls overwrite the data in the hostent structure.

Error return

A NULL pointer indicates an error. The value of h_errno indicates the specific error.
HOST_NOT_FOUND
The host name specified by the addr parameter was not found.
TRY_AGAIN
The local server did not receive a response from an authorized server. Try again later.
NO_RECOVERY
An irrecoverable error has occurred.
NO_DATA
The host name is a valid name, but there is no corresponding IP address.

Programming considerations

The gethostbyaddr function tries to resolve the host Internet address through a name server if one is present.

Examples

The following example obtains the host name associated with a given IP address.
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
⋮
struct hostent *h;
struct sockaddr_in sin;
char domain[512];
sin.sin_addr.s_addr=gethostid();
h = gethostbyaddr((char *)&sin.sin_addr.s_addr,
sizeof(struct in_addr), AF_INET);
if (h!=(struct hostent *)0)
{
   strcpy(domain,h->h_name);
   printf("gethostbyaddr was successful\n");
}
else
   printf("gethostbyaddr failed\n");

Related information

gethostname: Return host name.

See z/TPF Transmission Control Protocol/Internet Protocol for information about z/TPF TCP/IP support.