<stdin> |

My Thoughts, Trials and Adventures

DNS resolution in C

Posted at — Sep 8, 2020 | Last Modified on — May 11, 2023


DNS resolution is the process of converting Domain name (example.com) into an IP address, Reverse DNS is the opposite of it, IP -> Domain

this article is a code walkthrough of a simple C program to resolve domain names into their respective IP address(s).


the code explained here can be found in my NetworkProgramming GitHub repository.

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]){

    printf("resolving host: %s\n", argv[1]);

    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));

    hints.ai_flags = AI_ALL;

    struct addrinfo *peer;

    if ( getaddrinfo(argv[1], 0, &hints, &peer) != 0 ){
        printf("getaddrinfo error!\n");
        return 1;

    struct addrinfo *address = peer;

        char addr_buf[100];

        getnameinfo(address->ai_addr, address->ai_addrlen, addr_buf, sizeof(addr_buf),
                                    0, 0, NI_NUMERICHOST);

        printf("%s\n", addr_buf);
    } while ((address = address->ai_next));
    return 0;


  1. we start by first printing arguments to screen, the program expects two arguments, a domain name and a port/protocol.

  2. a struct of addrinfo is declared hints. this struct fields need to be set as per our requirement. in the next line, we set hints.ai_flags = AI_ALL;. this means that we want bot IPV4 and IPV6 addresses. the result will be in the form of a linked list that can be iterated.

  3. we then declare another addrinfo struct called peer. this holds the results of getnameinfo()


       int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
                       char *host, socklen_t hostlen,
                       char *serv, socklen_t servlen, int flags);

  1. the address(s) will be stored in a linked, there can be iterated using do while loop as shown above in code.


compile the program using gcc

gcc lookup.c -o lookup

run the program:

~/Documents/Projects/network_programming/dns(master*) ยป ./lookup example.com http                              jojo@synk
resolving host: example.com

the above output contains multiple duplicates, this can be fixed by adding another field to hints

hints.ai_socktype = SOCK_STREAM;


  • resolving domain names to IP address.
  • usage of getnameinfo()