Easy Method to Verify IP address has been issued - c++

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.

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)

Finding the IP and MAC address of DNS Server and Gateway

Hello I am trying to programmatically(C++) find the Gateway IP and DNS Server IP and their MAC address of a local network. Do you have any suggestions on how to do that? Also is there a way to do it using WinPcap? Code samples would be great.
Since you mentioned winpcap I conclude that you are on Windows. Thus you need WinAPI to retrieve your info.
Take a look at Retrieving Information Using GetNetworkParams article it contains all steps you need to get what you want.
P.S.: There's nothing to do with winpcap though.
Well i did what ihor and Sp. suggested to find the IP addresses and then by using winpcap, crafted an ARP Request to the DNS server to get its MAC and processed the ARP reply.

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