gtpc1m3oTransmission Control Protocol/Internet Protocol

gethostbyaddr -- Get Host Information for IP Address

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

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:

Element
Description

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.

Note:
Unless otherwise stated in the description, the following error codes can be returned for either TCP/IP offload support or TCP/IP native stack support.

Value
Description

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 <types.h>
#include <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.