tags:

views:

494

answers:

3

Hello all,

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

A: 

If you're running Ubuntu, the leases are stored in /var/lib/dhcp3/dhclient-[interface_name].lease, maybe that's a start.

Stefan Mai
+3  A: 

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.

ephemient
A: 

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.

Jason