The gethostbyaddr(3) Function
There are times where you have an Internet address, but you need to report the hostname instead of the IP number. A server might want to log the hostname of the client that has contacted it, instead of the IP number alone. The function synopsis for gethostbyaddr(3) is as follows:
#include <sys/socket.h> /* for AF_INET */
struct hostent *gethostbyaddr(
const char *addr, /* Input address */
int len, /* Address length */
int type); /* Address type */
The gethostbyaddr(3) function accepts three input arguments. They
- The input address (addr) to be converted into a hostname. For address type AF_INET, this is the pointer to the sin_addr member of the address structure.
- The length of the input address (len). For type AF_INET, this will be the value 4 (4 bytes). For type AF_INET6, this value will be 16.
- The type of the input address (type), which is the value AF_INET or AF_INET6.
Notice that the first argument is a character pointer, allowing it to potentially accept many forms of addresses. You will need to cast your address pointer to (char *) to satisfy the compiler. The second argument indicates the length of the supplied address.
The third argument is the type of the address being passed. It is AF_INET for an IPv4 Internet address, or in the future, it will be the value AF_INET6 for an IPv6 format address.