• C

DNS of a IPaddress

How can i get the DNS name of a remote IP address?
aishwariaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ravenscr98Connect With a Mentor Commented:
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
 
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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
aishwariaAuthor Commented:
I need the C code which gets the DNS name.
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.