Find DNS names given Subnet Mask and Network Address

Posted on 2009-04-11
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?

Question by:antiderivativeJill
  • 2
  • 2
LVL 57

Expert Comment

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"  the d.c.b.a is the IP address backwards.

So if you knew the IP address was you would look up

Author Comment

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\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          

     ip_addr_long_nl++;                      //increments to     

     ip_addr_long = htonl(ip_addr_long_nl);  //converts back to  

     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)); 



        // Display new line





   return 0;


Open in new window

LVL 57

Accepted Solution

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).


Author Closing Comment

Comment Utility
Thanks! :) Here's my solution based your and my own suggestions.
* 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

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\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          
      temp++;                            //increments to    
      subnetStartLong = htonl(temp);   //converts back to  

      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);
   return 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 ( 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 (, 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 ( 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