Reputation: 342
I am trying to figure out from C code in linux if an interface is configured as static or uses dhcp.
I know I can open and parse the /etc/network/interfaces file, but I would prefer something cleaner, like the function getifaddrs() that I use to get the IP address and the mask. Because what if the interface is configured as dhcp in /etc/network/interfaces file but later on the user changes it to static from the command line? I would get a wrong answer.
Is there any way for asking the kernel about the static/dhcp current state of an interface?
Upvotes: 1
Views: 968
Reputation: 2128
DHCP IP address adquisition is usually managed by distribution scripts or network manager services configured by host. At low level they could use dhclient daemon...
In Debian, dhclient daemon creates the file
/run/dhclient.${interface}.pid
so, you could test when the interface is being set by dhclient.
In not fully managed network environment you should also read that file and test if the process ID is still alive.
Upvotes: 1
Reputation:
The kernel (the Linux part of e.g. GNU/Linux) doesn't decide, it doesn't (and shouldn't) care, it just gets told which network addresses go with which interfaces by whatever configuration system the OS is using. OpenWRT's not GNU, it operates differently
Upvotes: 1