In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <stddef.h>
int main(int argcount, char* argvector[])
{
if(argcount <= 1)
{
printf("\n\nThe usage for this program is \"listDNS (target subnet id)\".\n");
printf("\n Example: listDNS 165.95.5.0\n\n");
return 0;
}
struct hostent *hostPtr;
char* ip_addr_dot;
char ip_addr_tmp[16];
struct in_addr *addrPtr;
u_long ip_addr_long;
u_long ip_addr_long_nl;
//parse argument (ip address) to ip_addr_dot
ip_addr_dot = argvector[1];
//determine last octet in ip address
strcpy(ip_addr_tmp, ip_addr_dot );
const char delimiters[] = ".";
char *token;
token = strtok( ip_addr_tmp, delimiters );
token = strtok( NULL, delimiters );
token = strtok( NULL, delimiters );
token = strtok( NULL, delimiters );
if( token == NULL )
{
printf("Error: the ip address you have provided is not valid!\n");
return 0;
}
//convert last octet into an integer
int lastOctet = atoi(token);
//convert ip address string into long integer
ip_addr_long = inet_addr(ip_addr_dot);
while( lastOctet < 255 )
{
//convert to long and increment ip address
ip_addr_long_nl = htonl(ip_addr_long); //outputs 0.10.95.165
ip_addr_long_nl++; //increments to 1.10.95.165
ip_addr_long = htonl(ip_addr_long_nl); //converts back to 165.95.10.1
addrPtr = (struct in_addr *) &ip_addr_long;
hostPtr = gethostbyaddr(addrPtr, 4, AF_INET);
if( hostPtr != NULL )
{
// Display IP addresses
while(*(hostPtr->h_addr_list) != NULL)
{
addrPtr = (struct in_addr *) *hostPtr->h_addr_list;
printf("%s", inet_ntoa(*addrPtr));
hostPtr->h_addr_list ++;
}
// Display Hostname
printf(": %s", hostPtr->h_name);
while(*(hostPtr->h_aliases) != NULL)
{
printf(", %s", *(hostPtr->h_aliases));
hostPtr->h_aliases++;
}
// Display new line
printf("\n");
}
lastOctet++;
}
return 0;
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Join the community of 500,000 technology professionals and ask your questions.