Note
gethostbyaddr is no longer recommended for use as of Windows Sockets 2. Instead, use getnameinfo.
The gethostbyaddr function retrieves the host information corresponding to a network address.
hostent* gethostbyaddr(
const char *addr,
int len,
int type
)
addr
Type: char *
[in]
A pointer to an address in network byte order.
len
Type: int
[in]
The length, in bytes, of the address.
type
Type: int
[in]
The type of the address, such as the AF_INET address family type (used with TCP, UDP, and other associated Internet protocols). Possible values for the address family are defined in the Winsock2.h header file.
On the Microsoft Windows Software Development Kit (SDK) released for Windows Vista and later, the organization of header files has changed and the possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is automatically included in Winsock2.h, and should never be used directly.
Note that the values for the AF_ address family and PF_ protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.
The table below lists the possible values for address family that are supported.
Type: hostent *
If no error occurs, gethostbyaddr returns a pointer to the hostent structure. Otherwise, it returns a null pointer, and a specific error code can be retrieved by calling WSAGetLastError.
| Return code | Description |
|---|---|
| WSANOTINITIALISED | A successful WSAStartup call must occur before using this function. |
| WSAEINVAL | An invalid argument was supplied. This error is returned if AF_INET6 was specified in the type parameter and the len parameter was not set equal to the size of an IPv6 address. |
| WSAENETDOWN | The network subsystem has failed. |
| WSAHOST_NOT_FOUND | Authoritative answer host not found. |
| WSATRY_AGAIN | Nonauthoritative host not found, or server failed. |
| WSANO_RECOVERY | A nonrecoverable error occurred. |
| WSANO_DATA | Valid name, no data record of requested type. |
| WSAEINPROGRESS | A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. |
| WSAEAFNOSUPPORT | The type specified is not supported by the Windows Sockets implementation. |
| WSAEFAULT | The addr parameter is not a valid part of the user address space, or the len parameter is too small. |
| WSAEINTR | A blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall. |
The gethostbyaddr function returns a pointer to the hostent structure that contains the name and address corresponding to the given network address.
The memory for the hostent structure returned by the gethostbyaddr function is allocated internally by the Winsock DLL from thread local storage. Only a single hostent structure is allocated and used, no matter how many times the gethostbyaddr or gethostbyname functions are called on the thread. The returned hostent structure must be copied to an application buffer if additional calls are to be made to the gethostbyaddr or gethostbyname functions on the same thread. Otherwise, the return value will be overwritten by subsequent gethostbyaddr or gethostbyname calls on the same thread. The internal memory allocated for the returned hostent structure is released by the Winsock DLL when the thread exits.
An application should not try to release the memory used by the returned hostent structure. The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other function calls to gethostbyaddr or gethostbyname.
Although gethostbyaddr no longer recommended for use as of Windows Sockets 2 and the getnameinfo function should be used, gethostbyaddr is capable of returning a NetBIOS name; getnameinfo is not. Developers requiring NetBIOS name resolution may need to use gethostbyaddr until their applications are completely independent of NetBIOS names.
Note
The capability to perform reverse lookups using the gethostbyaddr function is convenient, but such lookups are considered inherently unreliable, and should be used only as a hint.
The following example demonstrates the use of the gethostbyaddr function.
Windows Phone 8: This API is supported.
Header: Declared in winsock2.h.
Library: Use ws2_32.lib.