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.
Related
I need to find out the address of the TFTP server that is specified in the DHCP configuration.
When the PC is started on, the computer receives the IP address via DHCP, then downloads the image from the PXE server. During the download of the distribution, I need to run a utility that accesses the Database to the server from which this image was downloaded (where the TFTP server is running).
In theory, it would be possible to register the necessary address of the TFTP server in the downloadable image of the distribution. But the bottom line is that such a scheme exists in various subnets. And specifying its TFTP server for each subnet is an irrational approach. It would be more convenient to get the address of the TFTP server from the DHCP server, which is listed there as next-server
I found something similar at Busybox.
Is it possible to implement something like this in C/C++ and how can it be done? I don't have any ideas.
Some example of the code of contacting the DHCP server to get an address or parameter. Open a socket and make a request or something like that... At least I haven't found any similar examples on the Internet.
P.S. I will put a dislike and send it to read man, than I will answer the question constructively. Nice!
If you are using the ISC dhcp-client then
man dhclient-script
If you are using some other dhcp client then it might have support for calling scripts too. Or it might store the dhcp lease somewhere in a parseable format, like /var/lib/dhcp/dhclient.leases. You can extract the dhcp options from that.
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?
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.
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>
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.