Dylan Klomparens
Dylan Klomparens

Reputation: 2932

Finding the correct "network interface" number for IPv6

I am trying to use Boost for some IPv6 and multicast network communication. I need to construct an IPv6 multicast socket that uses a specific network interface index.

I was able to find the correct multicast option to set the network interface index in boost/asio/ip/detail/socket_option.hpp: explicit multicast_request(const boost::asio::ip::address_v6& multicast_address, unsigned long network_interface = 0)

The problem is, I don't know how to find the correct value for the "network_interface" parameter. Is there a way to get the network_interface value using a local IPv6 address that I can provide? I looked in the documentation and examples, but couldn't find anything.

-- Dylan

Upvotes: 3

Views: 2220

Answers (2)

Steve-o
Steve-o

Reputation: 12866

Each platform provides APIs to enumerate the network interfaces, e.g. getifaddrs for many Unixes and GetAdaptersAddresses for Windows. Note on Windows there is a separate numerical space for IPv4 and IPv6 adapters which makes the API call if_nametoindex quite confusing.

You may wish to inspect the methods I employed in OpenPGM for portability, considering Windows doesn't really have useful adapter names:

http://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/getifaddrs.c

http://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/nametoindex.c

http://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/indextoaddr.c

http://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/indextoname.c

Upvotes: 7

eater
eater

Reputation: 2817

I don't think there's a platform-independent way to figure this out, just as there is no portable solution to enumerating the local addresses.

On Linux, you can find what you want in the second column of /proc/net/if_inet6, which is also available more robustly through the rtnetlink(7) interface.

Upvotes: 2

Related Questions