1

in C++ linux application, how can I get the network interface reffering to each IP on my machine? IP contains: static IP and dynamic IP

Note: I can't use the system call getnameinfo

10x

1

2 Answers 2

3

You can use the getifaddrs call; however, note that this only retrieves one address per interface. If that's not sufficient, use the rtnetlink protocol over a netlink socket; libnetlink may make this easier.

Sign up to request clarification or add additional context in comments.

1 Comment

getifaddrs() is implemented (on Linux) using netlink. It's the right interface (and it can return multiple addresses per interface - they just appear in multiple struct ifaddrs).
2

It's quite tricky to do this, I believe you need to have root access. You need to issue an ioctl (something like SIOCGIFCONF) which then returns you a list of all interfaces, and then you can issue further ioctl calls to extract status information, etc.

5 Comments

Neither is root needed, nor is SIOCGIFCONF a timely interface. Avoid.
stracing ip addr shows it using netlink sockets; and, of course, /bin/ip is not setuid :)
@user562374, I did say "I believe", not "it requires", the last time I did this was almost four years ago (but that was for capturing packets off of the interfaces - which did require root - hence the misconception), also, it is a valid mechanism for querying all interfaces on a box, it's not deprecated(?) so what's wrong with it?
SIGCGIFCONF won't return ipv6 addresses - and with the IANA pool depleted this is going to be a very important concern soon
@bdonlan, very true, definitely netlink if you've got ipv6 in your setup (but I'd hazard that most likely the OP's is on a private network, which is ipv4...) I've yet to encounter a private network that is using ipv6 though...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.