Solved

Find DNS names given Subnet Mask and Network Address

Posted on 2009-04-11
4
1,007 Views
Last Modified: 2013-12-23
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?

0
Comment
Question by:antiderivativeJill
  • 2
  • 2
4 Comments
 
LVL 57

Expert Comment

by:giltjr
Comment Utility
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.
0
 

Author Comment

by:antiderivativeJill
Comment Utility
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

0
 
LVL 57

Accepted Solution

by:
giltjr earned 500 total points
Comment Utility
-->  hostPtr = gethostbyaddr(addrPtr, 4, AF_INET);

This command gets the host name by looking up the IP address using DNS services.  The DNS server only know the host name based on IP address if there is a PTR record defined that that IP address.  Now, if all of the hosts you are looking up are within your network and you always setup PTR records, then you are fine.  However if you are looking up IP addresses on the Internet, you may be out of luck as a LOT of ISP's do not delegate the PTR records to the customers they assign the address ranges to.  In most cases the ISP owns the IP address range and so they must delegate it to the customer they assign it to.

O.K., you have a address and the subnet mask.  You know how to compute the  IP subnet based on that.

Since you have the IP subnet mask, you also should know how many hosts are in that subnet.   If you know the subnet, and the number of hosts, you can then figure out the 1st valid IP address (subnet+1) and the last valid IP address (subnet+numhost-1).



0
 

Author Closing Comment

by:antiderivativeJill
Comment Utility
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]
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Lets look at the default installation and configuration of FreeProxy 4.10 REQUIREMENTS 1. FreeProxy 4.10 Application - Can be downloaded here (http://www.handcraftedsoftware.org/index.php?page=download) 2. Ensure that you disable the windows fi…
This is an article about my experiences with remote access to my clients (so that I may serve them) and eventually to my home office system via Radmin Remote Control. I have been using remote access for over 10 years and have been improving my metho…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now