1

Is there a way to get and set IP address on an interface in family agnostic way?

I have found about getifaddrs. Is there something similar but for setting addresses, or I need to use ioclt?

Here is my case: I need to extend an IPv4 application managing interfaces to support IPv6 addresses also. This application uses ioctl for AF_INET. My question is what would be the best approach for me to do add/delete/status for an interface to support IPv6 also - add another ioctl layer for IPv6, or mix netlink for IPv6 with ioctl for IPv4?

Thanks!

Aby Aaa Asdda
  • 67
  • 1
  • 5

1 Answers1

0

I would switch to netlink anyway. The reasons:

  1. netlink allows to manage multiple IP addresses on the same interface w/o so called «aliases» etc.
  2. ioctl interface is obsoleted already for ages
  3. asynchronous status monitoring using netlink is really simple: one should subscribe to a required group on a NETLINK_ROUTE socket and then just poll() + recv().

The ifaddrmsg structure is not family agnostic, one has to specify AF_INET or AF_INET6 as a family in the message, + IFA_ADDRESS and other NLA size depends on the family. But it can be managed easy at the application level.

For C there is a libnl library, for Python — pyroute2.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
svinota
  • 779
  • 8
  • 10