How to get the TFTP server address from the DHCP configuration - c++

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.

Related

iOS NAT64 IPv6 environment issue

Now that apple has changed their review environment to an IPv6 only network ive been running into troubles with my application while testing.
The application requires the user to input their server/systems IP address, port (4401) and credentials to gain access to the application. Following the guidelines provided by apple i set up a NAT64 environment to test my application's compatibility. Running iOS 9.3.2 on an iphone 5s.
My work environment has several internal networks. I connected my iMac to the internal server (10.10.50.XX) via Ethernet which also has an external IP which is what i provide to apple. The 50 network does not run behind a proxy, but there are some sites which get filtered, but can access by confirming. I then shared my Ethernet connection through WiFI.
I connected the device and had internet access with the same pages being filtered. The device gets assigned a local-link address of 169.254.XX.XX. From what ive read my device is connecting to the ipv6 only network correctly, as the iphone does not show the ipv6 address only local link.
If I am correctly connected to the shared IPv6 network, the problem I am having is connecting to my server on the 50 network from my application. The application fails when ever i try to connected to the 10.10.50.XX network or the external(understandable because my imac is on the 50 internal network) .
Testing the application using IPv4 with a wireless router that is connected to the 50 network runs perfectly.I think it is an issue with the local link address not being able to see the 50 network or something.
Heres some brief background info on how i connect to the server. The user inputs all the credentials and the IP address and port of the server they want to connect to. The application saves this as a text (utf-8) and uses boost shared_ptr to send it to the cpp connection class which validates the credentials and provides access to the user. This cpp connection class handles the connection, synchronization, logging, requests and response from the server.
Is it possible to somehow get access to the 50 network, using the NAT64 internet sharing option?
If you're passing an IP address straight down to your network lib, then no. When your device is on the IPv6-only side of the NAT64, the server's IPv4 address is useless.
You almost certainly need a DNS name for the server you're trying to reach. The NAT64 relies first on DNS64 to create IPv6 addresses for your application when the server is IPv4-only. These synthesised addresses contain the IPv4 address(es) for the server, giving the NAT64 the information it needs to translate from IPv6 to IPv4.
Updating this to add: as suggested by user102008 in the comment thread attached to this answer, you may also be able to pass the IPv4 string literal though getaddrinfo() (see code listing 10-1 on this page). When you're behind a NAT64, a synthesised IPv6 address should be among the results returned by that call. The NAT64 will translate from this address back to IPv4 to reach the host you specified. In this case, if the system knows the correct prefix to use for the NAT64, a hostname is not necessary.

Listing all Network Connections, then managing one

I'm developing an application in C++ for Windows 7, that sets up a TFTP server to communicate to an embedded device. The device is supposed to be connected directly to the Ethernet port, and it expects a TFTP server on 192.168.1.19 static address.
To simplify user experience, I want the following scenario:
present a list of all network connections registered in the system to the user (same list as in "Control Panel\Network and Internet\Network Connections")
allow to choose one connection
memorize its IPv4 configuration
set IPv4 configuration to 192.168.1.19 and 255.255.255.0
after TFTP job done, restore original IPv4 configuration
The problem I encountered is a mix of the following:
when no Ethernet cable plugged, neither of GetAdaptersInfo, GetInterfaceInfo, GetAdaptersAddresses list the connection I'm interested in;
even if I get that connection, I'm not sure which API to use to set DHCP on/off, static IP, network mask.
I tried using netsh, and it works fine, but I don't want to run it from my app, because I don't want to deal with non-English characters in the connection names.
Suggestions welcome,
Thanks!
===== edit #1
Tried WMI query "Select * From Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'" -- no IP address in the feedback, although I can see it in the Control Panel connection properties.
Queries involving Win32_NetworkConnection don't return anything at all.
I ended up using:
"Select * From Win32_NetworkAdapter Where PhysicalAdapter='True' AND AdapterTypeID=0" query to list adapters/connection names/guid - works pretty good
registry to read configuration via guid
netsh to do configuration via connection name. Yes, I had much fun with locales.
I frankly didn't find a more reliable way to work with disconnected connections. Hope this helps. If anyone has better solutions - I'd be glad to see them!

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.

Linux ioctl -> how to tell if current IP was obtained by dhcp

I'm fiddling with the sockets ioctl's to get the current interfaces setup and I can already get the IP, interface name, netmask and check if the interface is up or down, (I just do IOCTl to SIOCGIFCONF, SIOCGIFNETMASK and SIOCGIFFLAGS).
I am looking for a way to tell if my current IP address was obtained through dhcp or if it was static.
I can check /etc/network/interfaces for everything I want, but I'm looking for a way to do it programmaticly (does this word exist?).
Does anyone have any insight into this?
One more thing, I'm working on Linux (for now).
Cheers
With the wide variety of DHCP clients on Linux -- pump, dhcpcd, dhclient, udhcpc, and quite possibly others that I do not know of -- this isn't possible in a general sense.
However, if you are targeting a specific distribution -- say, "default install of Ubuntu" -- then you can investigate solutions such as Stefan's. Note that all four of the DHCP clients listed here can be installed on Ubuntu and can replace the default DHCP client, dhclient.
If you're running Ubuntu, the leases are stored in /var/lib/dhcp3/dhclient-[interface_name].lease, maybe that's a start.
I don't think its possible to tell via a kernel interface (ioctl) if an IP address was allocated via DHCP as in most distributions DHCP is a userland app that just configures the kernel with data provided by a remote source as if the user had done it manually. In fact, if you look at the ISC dhclient it just passes the data received from the DHCP server to simple shell scripts that do ifconfig, route and various other commands that you could type as a user.
So you'll probably have to look at methods that are specific to your DHCP client and distribution as suggested by Stefan.

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.