Created
March 3, 2019 04:20
-
-
Save ankurparihar/e6eefa4471cdf7ffa5b645d40b400ffd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*----- 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment