Skip to content

Instantly share code, notes, and snippets.

@ankurparihar
Created March 3, 2019 04:20
Show Gist options
  • Save ankurparihar/e6eefa4471cdf7ffa5b645d40b400ffd to your computer and use it in GitHub Desktop.
Save ankurparihar/e6eefa4471cdf7ffa5b645d40b400ffd to your computer and use it in GitHub Desktop.
/*----- 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