DNS of a IPaddress

Posted on 2004-04-02
Last Modified: 2010-04-15
How can i get the DNS name of a remote IP address?
Question by:aishwaria
  • 4
  • 3
  • 2
  • +1
LVL 57

Expert Comment

by:Pete Long
ID: 10740411
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

LVL 57

Expert Comment

by:Pete Long
ID: 10740446

Author Comment

ID: 10740507
I need the C code which gets the DNS name.
LVL 45

Expert Comment

ID: 10740835
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,
LVL 57

Expert Comment

by:Pete Long
ID: 10741389
well I hope so, its probably the only chance I'll ever have of points in this TA LOL :)
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.


Expert Comment

ID: 10746595

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 "",  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.

Accepted Solution

ravenscr98 earned 25 total points
ID: 10746701

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.

Author Comment

ID: 10746806
When i passed the the ip address i got as result.
I used getaddrinfo.
What is the mistake i had committed.

Author Comment

ID: 10746808
Sorry my result was,
I had typed it wrong

Author Comment

ID: 10747066
Hi rob

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

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 175
stack 22 159
Coverting 24 hour time to 12 hour in C++ 15 168
c++ substatte a varabe for a string in a LPCTSTR statment 8 83
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

911 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

18 Experts available now in Live!

Get 1:1 Help Now