Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a way in C++ (Windows) to get the URL of a website if I have the IP address? Thank you for any help or direction.
Posted

While you might be able to get the URL of a website from an IP address, you can't be sure that you've got the right one.

Multiple websites can be hosted on a single IP address, so the best you can hope for is that a reverse DNS lookup will give you the name of a website - it might just give you a server name.

Example: dig www.google.com gives me a CNAME with 6 IP addresses:
www.google.com.         86399   IN      CNAME   www.l.google.com.
www.l.google.com.       299     IN      A       209.85.146.105
www.l.google.com.       299     IN      A       209.85.146.99
www.l.google.com.       299     IN      A       209.85.146.104
www.l.google.com.       299     IN      A       209.85.146.106
www.l.google.com.       299     IN      A       209.85.146.147
www.l.google.com.       299     IN      A       209.85.146.103


Reverse DNS lookup on the first of these addresses (dig -x 209.85.146.105) gives me a different name:
105.146.85.209.in-addr.arpa. 86400 IN   PTR     bru01s01-in-f105.1e100.net.


- which doesn't say anything useful about any websites.
 
Share this answer
 
Comments
Member 7766180 16-Sep-11 12:33pm    
You right. Thank you this is very informative and a little wackey! You would think that it would be more black and white and that you would be able to cross reference it!
You can't.
The relation between URL and IP is not 1/1, and URLs are more abstract concept than IPs: They are tailored to different purposes: The URL tells WHAT page you want, the IP tells WHERE it can be found.

The mapping between IP names and IP addresses id managed by a hierarchical redundant distributed database called "Domain Name System", whose API are represented by the get_host_by_name and get_name_by_host functions.

Whatever the DNS returns to a call or the other does not depend on you, ant it is not required by the DNS standard for that calls to represent an involution (hence get_name_by_host(get_host_by_name(name)) != name),

Basically you question sounds like "Is there a way to get the name of Smith?", in a world that has many even unrelated people having "Smith" as a surname.
 
Share this answer
 
v2
Comments
Member 7766180 16-Sep-11 12:34pm    
Thank you. I can see I'm barking up the wrong tree of many! Very informative!
uh, that doesn't get a URL, it gets the IP address (binary) as a standard IP address as a string of characters with the dots between the numbers.

Oh, BTW, the sockAddr.sin_addr.s_addr field might be real useful to you in the other question you asked about comparing addresses.
 
Share this answer
 
Comments
Member 7766180 16-Sep-11 0:24am    
Your right! I was so excited that it did what I needed that I didn't see it!
So how does one get an URL from an IP?
GOT IT!
C++
HOSTENT *pHostEnt;
			int  **ppaddr;
			SOCKADDR_IN sockAddr;
			char* addr;
			pHostEnt = gethostbyname( "www.codeproject.com");
			ppaddr = (int**)pHostEnt->h_addr_list;
			sockAddr.sin_addr.s_addr = **ppaddr;
			addr = inet_ntoa(sockAddr.sin_addr);  //this is your ip address
			printf("\n   Code %s\n",addr);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900