Link to home
Start Free TrialLog in
Avatar of antiderivativeJill
antiderivativeJillFlag for United States of America

asked on

Find DNS names given Subnet Mask and Network Address

If I have a subnet mask and a network address, how do I find the DNS names assigned to the hosts on this subnet using c in unix?

Avatar of giltjr
giltjr
Flag of United States of America image

You might not be able to.  In order to find a host name based on the IP address there needs to be a PTR record for the address setup.

What you would need to do is do a DNS query for the the host "name" d.c.b.a.in-addr.arpa  the d.c.b.a is the IP address backwards.

So if you knew the IP address was 10.23.4.15 you would look up 15.4.23.10.in-addr.arpa.
Avatar of antiderivativeJill

ASKER

I know it's possible to complete this task using the networking API but everything about this API is highly confusing to me. Do you know of any place I can go where I can find out about unix network programming syntax? I know all hosts on a subnet have the same network address and subnet mask values.

I know how to use the subnet mask to obtain the network address from a given IP address. First, convert the subnet mask and IP address to binary and perform an AND operation on them to obtain the network address.

Given this, how can I find the IP Address given the subnet mask and network address???

To giltjr:  What do you mean when you say PTR; do you mean pointer? I'm pretty sure I don't need a dns query because what I'm doing shouldn't involve sockets.

Here's an example of how I can find the DNS given the subnet id.

Given the subnet mask and a network address in dotted-decimal notation, I'd like to find all the DNS names assigned to all hosts on that subnet.



#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;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of giltjr
giltjr
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks! :) Here's my solution based your and my own suggestions.
[code]
/********************************************************************
* Author: Jillian
* 2. Given a subnet mask and a network address in dotted decimal
* notation, find the DNS names assigned to all hosts on that
* particular subnet. The executable of your program must accept
* the subnet mask and the network address as command line arguments.
* The program should output the IP address and the corresponding DNS
* names for all the hosts. For example, if the name of your executable
* is listIPDNS, one should be able to run it as:
*       listIPDNS 255.255.255.0 165.95.11.15
********************************************************************/
#include
#include
#include
#include
#include
#include
#include

int main(int argcount, char* argvector[])
{
   char* inputAddress;
   char* inputMask;

   u_long inputAddressLong;
   u_long inputMaskLong;
   u_long subnetStartLong;
   u_long numHosts;
   int i;
   struct in_addr* inAddress;
   unsigned long temp;
   struct hostent *hostptr;
      
   if(argcount <= 1)
   {
      printf("\n   Example: listIPDNS 255.255.255.0 165.95.11.15\n\n");
      return 0;
   }
      
   inputMask = argvector[1];
   inputAddress = argvector[2];      
   inputAddressLong = inet_addr(inputAddress);
   inputMaskLong = inet_addr(inputMask);
   numHosts = htonl(inputMaskLong);
   subnetStartLong = inputAddressLong & inputMaskLong;
      
   for(i = 0; i < 32; i++)
      numHosts = numHosts ^ (1UL << i);
      
   i = 0;
   while(i <  numHosts)
   {
      //convert to long and increment ip address
      temp = htonl(subnetStartLong);  //outputs 0.10.95.165          
      temp++;                            //increments to 1.10.95.165    
      subnetStartLong = htonl(temp);   //converts back to 165.95.10.1  

      inAddress = (struct in_addr *) &subnetStartLong;
      
      hostptr = gethostbyaddr(inAddress, 4, AF_INET);
      if( hostptr != NULL)
      {
         // display 'ip: hostname'
       printf("%s", inet_ntoa(*inAddress));
       printf(": %s",  hostptr->h_name);
       printf("\n");
      }
      i++;
   }
      
   return 0;
}


[/code]