[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Find DNS names given Subnet Mask and Network Address

Posted on 2009-04-11
4
Medium Priority
?
1,051 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 57

Expert Comment

by:giltjr
ID: 24124135
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
ID: 24124469
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 2000 total points
ID: 24125390
-->  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
ID: 31570299
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
This article explains the fundamentals of industrial networking which ultimately is the backbone network which is providing communications for process devices like robots and other not so interesting stuff.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Suggested Courses

649 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