I've been working through a small tutorial on how to build a basic packet sniffer for Linux. I got everything working, and I now want to add IP-to-host mapping.
Everything was working before I added this function:
void IPtoHostname(char *ipaddress, char *hostname){
struct hostent *host;
in_addr_t ip = inet_addr(ipaddress);
if (!hostname){
puts("Can't allocate memory...");
exit(-1);
}
host = gethostbyaddr((char *)&ip, 32, AF_INET);
hostname = strdup(host->h_name);
}
This basically takes a string IP address ("192.168.28.18") ipaddress and fills in that IP's hostname ("who.cares.com") into hostname.
=================================== EDIT ================================
Apparently, this appears to be an issue with my understanding gethostbyaddr() (I apologize for the misnomeric question title and the nasty little edit), as after rewriting the function to:
void IPtoHostname(char *ipaddress, char **hostname){
struct hostent *host;
in_addr_t ip = inet_addr(ipaddress);
host = gethostbyaddr((char *)&ip, 32, AF_INET);
if (!host){
puts("Packet damage?");
return;
}
*hostname = strdup(host->h_name);
}
It prints out packet damage. Every single time.