get global ip address - c++

How can I get ( in C++ ) the global IP address of my computer(windows XP)?

You can't.
You can determine the IP addresses on the various interfaces, and there may be more than one. These could be local area network IPs (10.0.0.0/8, 192.168.0.0/16, etc.), or they might be internet routable.
You seem to be asking "if I have 192.168.0.3, how do I get my Internet IP?" There is not function call to do this: such an IP might exist, it might not exist, there might even be more than one.
The closest you can get is to have a known computer on the Internet tell you: connect to some other machine, and ask them to send back what they think your IP address is. There are a few websites out there for this, some might even have APIs to do this.
I feel like some home-routers might be able to tell you through uPnP too, but again, this will not cover all possible cases.

On windows you need to get the local host name and then pass that to the gethostbyname function in winsock2 which returns the associated IP addresses.
Example: http://tangentsoft.net/wskfaq/examples/ipaddr.html
A stack overflow answer for Linux:
Get the IP address of the machine

You will have to make a request to a website like http://whatismyipaddress.com/ and extract the string in it that shows your IP.

Use this plain text IP url: get ip

Related

get IP address of local machine on c++

I want to retrieve IP address of my computer (same as I get on http://www.whatsmyip.org/)
I have a win32 project.
This is the code that I am using, as I didnt find any tutorial on this, I could get following info, but not the IP address which I saw for my computer on the whatsmyip.org :(
The IP I got on whatsmyip.org starts with 116.x.x.x
Your code gets adapter addresses, which are local. If you want your Internet address, you need to use the Internet, not your local network. You need to replicate the functionality of asking an external site what IP it sees you connecting from. See here for some suggestions for how to do that.
Retrieving http://icanhazip.com will do it. You can use whatever HTTP library you like.
The IP which assigned to your machine is not necessarily the IP that you see outside of your local network (e.g. in whatsmyip.org).
Your machine is not directly connected to the Internet with a valid and static IP. Maybe you are behind a NAT. So you can not determine your valid IP over Internet by listing your local assigned IPs in many situations.
To findout what IP address you have in Internet, you can do two ways. Ask from someone over Internet (for example, using whatsmyip.org). Or, query your local network recursivly (which is not easy task)

How to get my computer external IP address?

Lets say my IP currently is: 123.123.123.123
How can I get that string programmatically?
Note: I don't want to get this IP: 192.168.0.10, I want that IP which others can use to connect into my computer via HTTP or anything.
NOTE: I dont want to open some web-page such as ip4.me to get the address, I want to get it with just C++.
I tried to google but every suggestion was "load a page and read the IP it tells you". Is that really the only way?!
You can fetch it from http://api.externalip.net/ip/ or some similar services, but I'm not sure how reliable these sites are, in means of availability
Any other way would be extremely complicated, as in general, no network equipment has api to tell external IP, and even if it had, you can not tell is there a simple xDSL router in front of you or Cisco ASA nating outbound traffic
My recommendation is to send a packet with the record route option.
If you know your upstream gateway, you should be able to find a ping command that allows you to set record route, and then either stores that data in an array or something you can regex.
Your WAN IP should be either record 0 or record 1, I believe.
What network library are you using?

Easy Method to Verify IP address has been issued

I'm writing a C++ program on a linux box (DHCP client) that depends on the connectivity of the network. I need to pragmatically verify that my system has a IP address. I know this is a general and open ended question, so any quick and dirty solution will work for me, but ideally I would like to check/read a system file to ensure the DHCP client has received an IP address from the DHCP sever.
Thanks in advance.
Just try any operation that requires an IP address and that should work if there is one. A DNS lookup comes to mind.
Maybe have a look at the source of say ifconfig, since that gets the ip address if one is assigned. A quick strace suggests that it might be an ioctl such as
ioctl(4, SIOCGIFADDR, {ifr_name="eth0", ifr_addr={AF_INET, inet_addr("<my ip>")}})
and a grep of /proc/net for my IP address suggests that if you know how to parse /proc/net/fib_trie you might be able to get it from there.

How to get IP address of your application server

I need to be able to find the IP address of the server the page is currently executing on. I have some code that calls a third party site and has to pass a specific key that changes depending on which server it is on. Is there a CGI variable or some way in ColdFusion to determine what the IP address is of the host server?
Like the other commenters have outlined if you need the external IP as the third party site sees it then you probably should use the external approaches they recommend.
However if the third party is giving you access in some form that is based on an actual IP as the Server sees itself and not the IP as they see it you can use
<cfset cName = CreateObject("java", "java.net.InetAddress").getLocalHost().getHostAddress()>
<cfdump var="#cName#">
There are two reasons why a program can't query the host it's running on and see what its IP is:
It might have multiple ips, and short of looking through all sorts of kernel data structures you're unlikely to know which one is going to be used for a given outgoing connection.
It might connect to the outside world though a NAT firewall or some sort of proxy so that the outside world will see a different IP than any of the ones configured on your box.
Actually, there might be more than those two, but those are ones that have occurred to me.
Because of that, the simplest way is to connect to another box somewhere outside of your corporate network and see what IP it thinks you have. I use a two line CGI script running on my colo box to detect what IP my home server currently has (so I can detect when the cable company changes it).
You can use CGI.LOCAL_ADDR to determine the IP-address of your server (CFML equivalent to PHP's $_SERVER["SERVER_ADDR"]). It works on IIS and Apache using ColdFusion or Railo, given you are not behind a Proxy, not natting your server IP and having only one IP assigned to your server (not sure which IP would be shown, if there are more than one).
I would use hostip.info API
Your IP in XML: http://api.hostip.info/get_xml.php
Your IP in HTML: http://api.hostip.info/get_html.php
The safest way will be to use a service like WhatIsMyIP. If the server is behind a NAT, then the OS has no knowledge of the external IP address.
There are many questions in SO regarding this, see here for example.
<cfhttp url="http://www.whatismyip.com/" result="myresult" resolveurl="yes">
<cfoutput>#myresult.filecontent#</cfoutput>

Get my WAN IP address

How can i go about programaticaly getting the IP address of my network as seen from the Internet? Its obviously a property that my router has access to when it connects to the ISP. Is there any way to get this info from a router using a standard protocol. My only other option is to either find a WS which returns my IP address (suprisingly difficult to do), or just go to something like whatismyip.com and strip out all the HTML (very dirty and susceptable to change). Is there any other way???
Don't scrape whatismyip.com, see here for how you can call their API which just returns your address.
If you don't use this, you have to write something like it yourself, i.e. a host beyond your router which can report back your apparent address.
Note that webserver might not see your real WAN IP address because:
your ISP might be transparently
proxying HTTP traffic, and the server would see the IP of the
proxy. In that case, you'd typically
need to look for and parse a
X-Forwarded-For header.
or, as Olaf noted, there may be another NAT router between you and the wide open Internet, in other words, the WAN address of your router is on a private network. The best you'd get from a service like whatismyip.com is the IP of the outermost NAT router.
If your router supports snmp you could use that to ask it about it's external ip. A small example is found here:
http://www.rohitab.com/discuss/index.php?showtopic=31901
I've voted up Paul Dixons answer because it seemed complete, but there's one more aspect to this:
Your ISP might provide private addresses for you - this does happen with some ISPs. Depending on what you expect you might need a routable address that you don't have
The proxy information that Paul mentions (HTTP-Header X-Forwarded-For) might be a non-routable address if you yourself have a proxy
based on mixing all these aspects (getting a nonroutable address from your ISP and having a proxy yourself) you might get bizarre results.
These aspects are not the typical day to day situation, but depending on your needs you might want to take these into account.
I don't see a language specifacation but I did it here in python: Finding a public facing IP address in Python?Basicly there is no way of doing it without relying on an external server, in this case I use http://www.whatismyip.com/automation/n09230945.asp which only provide the ip address.
Alternatively you could use your own script which in PHP would look like:
<?php
echo $_SERVER['REMOTE_ADDR'];
?>