Skip to content

Instantly share code, notes, and snippets.

@ankurparihar
Created March 3, 2019 04:20
Show Gist options
  • Select an option

  • Save ankurparihar/e6eefa4471cdf7ffa5b645d40b400ffd to your computer and use it in GitHub Desktop.

Select an option

Save ankurparihar/e6eefa4471cdf7ffa5b645d40b400ffd to your computer and use it in GitHub Desktop.

Revisions

  1. ankurparihar created this gist Mar 3, 2019.
    23 changes: 23 additions & 0 deletions hostname.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    /*----- Find host name from IP address -----*/

    #include <stdio.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>

    int main(int argc, char* argv[]){
    struct hostent* hent; // host structure
    struct in_addr ip; // ip structure
    const char *ipstr = argv[1];
    // "172.217.166.228" => www.google.com (domain name)
    inet_aton(ipstr, &ip);
    hent = gethostbyaddr((const void *)&ip, sizeof(ip), AF_INET);
    if(hent){
    printf("%s\n", hent->h_name);
    }
    else{
    printf("Something went wrong...\n");
    }
    return 0;
    }