Solved

Find DNS names given Subnet Mask and Network Address

Posted on 2009-04-11
4
1,020 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
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 500 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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to simulate latency? 5 40
Connecting Servers to L2 OR L3 Switch 6 47
BGP recommended setup with failover 2 49
VPN Server config in Modem 5 31
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
When it comes to security, there are always trade-offs between security and convenience/ease of administration. This article examines some of the main pros and cons of using key authentication vs password authentication for hosting an SFTP server.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

830 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