How can I get the IPv4 address of an interface on Linux from C code?
For example, I'd like to get the IP address (if any) assigned to eth0.
How can I get the IPv4 address of an interface on Linux from C code?
For example, I'd like to get the IP address (if any) assigned to eth0.
In addition to the ioctl() method Filip demonstrated you can use getifaddrs(). There is an example program at the bottom of the man page.
My 2 cents: the same code works even if iOS:
I simply removed the test against wlan0 to see data. ps You can remove "family"
If you don't mind the binary size, you can use iproute2 as library.
iproute2-as-lib
Pros:
Cons:
Try this:
The code sample is taken from here.
If you're looking for an address (IPv4) of the specific interface say wlan0 then try this code which uses getifaddrs():
You can replace wlan0 with eth0 for ethernet and lo for local loopback.
The structure and detailed explanations of the data structures used could be found here.
To know more about linked list in C this page will be a good starting point.