Search code examples
c++cserverdhcptftp

How to get the TFTP server address from the DHCP configuration


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!


Solution

  • 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.