gethostbyname() function on Linux. Explained with a C example.


DNS lookups translate a hostname (such as www.cspsprotocol.com) into an IP address (such as 19.45.6.3).

The gethostbyname() Linux system call provides a method for performing the lookup programmatically. An example of a C program will be used in this tutorial to demonstrate how the system call works.

Why do we need to resolve a host?

A computer network connects to each other over IP protocol. In the network, each node has an address to communicate with other nodes. The address could be a type of IPv4 or IPv6. An IPv4 address is a string consists four bytes separated by dots(.).

For example, 192.168.1.3 is a type of IPv4 address, the IPv6 address is separated by the a “::” for example, fe80::a883:444a:b088:cb4c.

So it’s obvious that the hostname should be resolved to the actual IP address before starting any communication. The same happens when you browse a website.

The first request from your laptop is a DNS lookup. Then the web page request goes to the hosting server using the resolved IP address.

What is gethostbyname() function?

The gethostbyname() is declared in the file netdb.h. The prototype of the function is as follows.

struct hostent *gethostbyname(const char *hostname);
  • Return Value – It returns the pointer of the hostent structure type. The structure has information of the translated address. If the system call cannot resolve the given hostname, it returns a NULL value, and errno is set to the actual error code.
  • Argument – It takes the hostname string as an argument.
  • Errors – In case of failures, it could return any one of the errors.
    HOST_NOT_FOUND, NO_DATA, NO_RECOVERY, and TRY_AGAIN.

Where gethostbyname() look for translation?

On Linux, the lookup query scans the /etc/hosts file on the machine. If it has the host entry, the corresponding Ip address is returned; else request is sent to the gateway configured.

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

C Program for DNS lookup using the gethostbyname() function.

 include <stdio.h>
 include <string.h>
 include <stdlib.h>
 include <sys/socket.h>
 include <errno.h>
 include <netdb.h>
 include <arpa/inet.h> 

int resolveHost(char * hostname , char* ip);
 int main(int argc , char *argv[])
 {
         if(argc <2)
         {
                 printf("No hostname is given to resolve");
                 exit(1);
         }
 char *hostname = argv[1];     
char ip[100];     resolveHost(hostname,ip);     
printf("%s resolved to %s" , hostname , ip);     
 }

int resolveHost(char * hostname , char* ip)
 {
         struct hostent *hent;
         struct in_addr **addr_list;
         int i;
 if ( (hent = gethostbyname( hostname ) ) == NULL)     
{             
herror("gethostbyname error");             
return 1;     }     
addr_list = (struct in_addr **) hent->h_addr_list;     
for(i = 0; addr_list[i] != NULL; i++)     
{             
strcpy(ip , inet_ntoa(*addr_list[i]));             
return 0;     
}     return 1;
 }

Output-> Compile and Run with GCC Linux compiler.

# gcc -o getHostByName getHostaddr.c
# ./getHostByName localhost
 localhost resolved to 127.0.0.1
# ./getHostByName www.google.com
 www.google.com resolved to 142.250.194.196