hari
hari

Reputation: 9733

FreeBSD: network interface address: dhcp or static

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?

Upvotes: 3

Views: 1280

Answers (3)

sonicpond
sonicpond

Reputation: 21

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 

Upvotes: 0

Roland Smith
Roland Smith

Reputation: 43495

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.

Upvotes: 2

Perry
Perry

Reputation: 4487

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.

Upvotes: 2

Related Questions