• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 764
  • Last Modified:

DNS of a IPaddress

How can i get the DNS name of a remote IP address?
0
aishwaria
Asked:
aishwaria
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Pete LongTechnical ConsultantCommented:
Hi aishwaria,
you need to query the whois database to get that info
Im dont know how to do that programatically but heres a php script that does the same
http://www.weberdev.com/get_example.php3/460

PeteL
0
 
Pete LongTechnical ConsultantCommented:
0
 
aishwariaAuthor Commented:
I need the C code which gets the DNS name.
0
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 
Kent OlsenData Warehouse Architect / DBACommented:
Hi aishwaria,

There is quite a bit of coding involved to do what you want to do.  I suggest you follow Pete's links, or come up with another way to do this.

If you're on a *nix system you can always do this with a shell script.  In fact, even in a Windows world using the system tools to do the nslookup and writing a C program to examine the results might be the easiest.

nslookup <ip-address> | YourProgramToCheckAnswer


main ()
{
  /*  read stdin until end-of-file and examine the returned records  */
}



Good Luck,
Kent
0
 
Pete LongTechnical ConsultantCommented:
well I hope so, its probably the only chance I'll ever have of points in this TA LOL :)
0
 
ravenscr98Commented:
aishwaria:

Look at the "gethostbyaddr" and "gethostbyname" functions.  These functions query DNS for a given host name or IP address.  It sounds like you have a numeric IP address and want the corresponding hostname.  You will need to use "gethostbyaddr" and your program will have three steps.

1.  Fill in an in_addr structure with the IP address (or a in6_addr structure for IPv6).  You will need to use either "inet_aton" or "inet_pton" to convert the dotted string version, such as "1.23.34.45",  of the IP adddress and store it in the in_addr structure.  IPv6 must use the "inet_pton" function to convert and store the IPv6 address string.

2. Call "gethostbyaddr" function with the address of the in_addr or in6_addr structure as the first argument.  If the function succeeds, it will return the address of a hostent structure.

3. The h_name field of the hostent structure has the official hostname for the IP address.  The h_aliases field is a pointer to NULL terminated array of string pointers that list all aliases for the host.

Do a man on the functions to get more details.

Also, see "UNIX Network Programming," Volume 1 by W. Richard Stevens for a more complete discussion.  He includes a nice example that uses gethostbyname to get the IP addresses and aliases for a given hostname.

- rob r.
 
0
 
ravenscr98Commented:
aishwaria:

Having posted my previous solution, I failed to mention that POSIX is now marking "gethostbyaddr" and "gethostbyname" as obsolescent.  The preferred solution is to use the "getaddrinfo" function.  You pass the dotted IP address string as the hostname argument and set the service argument to NULL.  You also have to fill in a hints structure.  The fourth argument is used to return a linked list of addrinfo structures.  You will find the hostname in the ai_canonname field of the addrinfo structure.
0
 
aishwariaAuthor Commented:
When i passed the the ip address 10.185.3.50 i got 10.185.3.96 as result.
I used getaddrinfo.
What is the mistake i had committed.
0
 
aishwariaAuthor Commented:
Sorry my result was,10.185.3.50.
I had typed it wrong
0
 
aishwariaAuthor Commented:
Hi rob

I am able to get the host name by using the getaddrinfo.
Thanks for the help.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now