get IP address of local machine on c++ - 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)

Related

How to Know an IP Address is Local or not on Linux Server

We are developing a Server software on Linux using C/C++, this software will limit the download rate for those requests which are from the Internet, but for those from local machines (intranet) it won't set any limit.
The problem is how to judge an IP address is local or not, is it possible to do it through c/c++ by reading some network number settings (maybe from router?)?
UPDATE
When I say local ip, I mean it is from within the company. For example, suppose the company has three subnets (this company only has a DSL link to the internet), they are 10.123.1.xxx, 172.16.1.xxx and 192.168.1.xxx, then all ip addresses from these three subnets should be considered as local address.
The private address ranges are:
10.0.0.0 - 10.255.255.255 (10/8 prefix)
172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
You might also want to filter out link-local addresses (169.254/16)
You could then parse the ip address in your code(to get the addresses you could use avahi or something similiar and save all the addresses to a file and then parse each address individually)and check it matches these addresses. If it does not then limit its connection
Edit
You could also look into using the getifaddrs function that will list local addresses
If you run a traceroute on the IP address of the machine requesting a connection, you should be able to see whether the route takes you through the "gateway outside the company" (typically your ISP). A simple example in my house would be the Time Warner gateway that my internal router connects to. If the route to the client does not go through the ISP (as you mentioned, you have a DSL link; so the IP address of the DSL endpoint should be known), then it's an internal request. This doesn't require you to know the full map of IP addresses inside the company - you can assume your routers have it figured out.
To get this information you can run a system command from inside your program and parse the response.
To start with, run it from the command line (with a known "internal" and "external" IP address), and look at the difference. If you need further help after that, please update your question with the information you gathered.

get global ip address

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

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>

how to know which installed devices has internet connection in WINPCAP?

i have known that winpcap library enabled a person to obtain advanced information about installed devices...
given all the networking devices found in the computer, how will i know which one of this has an internet connection?!
thanks:)
You typically don't care about this. Normally you just ask the network stack to make a connection, and don't worry about details.
However, each "device" will have its own IP address. You can request the network stack to use this IP address when making a connection.
Now, to figure which devices have an intenet connection, iterate over all of them, obtain their IP address, and try to create a connection from that originating IP to a destination IP on the Internet. Typically you already know such a destination IP, e.g. your companies webserver.
GetAdaptersInfo() will give you a list of all network adapters installed in a form that you can then use in GetIfEntry(). This latter function will tell you the operational status of an adapter, so you can at least tell if the adapter is plugged into a live hub/switch/router.
If you are particularly interested in IP connectivity, you could look to see if a default gateway is configured for that interface. Don't forget you might have to support IPv6 as well as IP4 if you try this.
Correlating the network adapters found by GetAdaptersInfo() with those found by pcap_findalldevs() is left as an exercise for the reader. I don't remember the details but it was fairly obvious.

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'];
?>