Question

How can I programmingly (C) know if given interface has static address or one provided by dhcp? I can look at /var/db/dhclient.leases.<interface_name>. Any better/cleaner way? any ioctl I can use?

Was it helpful?

Solution

Read /etc/rc.conf. Look for lines starting with ifconfig and see which of those contain the text DHCP.

This will not catch interfaces that were re-configured by running dhclient manually. Parsing the leases file would work better in that respect, assuming its permessions allow your program to read it.

OTHER TIPS

The short answer is no, there isn't. The dhcp client sets the interface address etc. using the same mechanisms that ifconfig uses. There is no special flag or other indication that it leaves to tell you that the interface was dynamically configured.

I'd check the config files in /etc/ rather than the leases, but yes, you're going to have to do something hackish to find out the information.

in addition to roland and perry's astute advice, i would add this: if rc.conf is NOT configured to use dhcp, thats doesn't mean it wasn't called from the command line.

if you see that it's running, there's a good chance that's where the interface is getting its address.

pgrep dhclient 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top