views:

150

answers:

2

I am trying to create a BSD socket to listen for messages from a specific IPv6 multicast address. I currently have no problem creating the socket listening on the correct address 0::0.

The problem is that I am running on a small embedded linux server with multiple NICs; here the ipv6mr_interface field of the ipv6_mreq is important. By trial and error, I have determined that 0, 1 and 3 do not work, but 2, does (it gives me all IPv6 multicast messages to my address, ff05::3, arriving on the correct interface).

My question is: How do I correlate, in linux, my interface, whos' address I know, to the correct interface number?

A: 

To summarize my comments on the original answer:

  1. Stevens (UNIX Network Programming Vol. 1) gives a solution for mapping device name to interface number. This involves querying the OS routing table for the interface number over a raw socket. In the Second Edition this is in section 17.6 and in the Third Edition this is in section 18.6.
  2. You can also use the proc filesystem to gather the same information in option one. Of particular interest is the /proc/net/dev_mcast file. This file gives a mapping between interface name and interface number; mapping from IP address to interface name is presumably just as easy.

Presumably option 1 is the most robust since the proc filesystem can change how it's organized, what information is available and how it's formatted. I am not personally familiar with how often the Linux Kernel changes these attributes, so, someone with more information about this is welcome to comment or edit this post.

MGoDave
+1  A: 

Hi,

maybe you can have a look to the if_nametoindex(3) function (seen in /usr/include/net/in.h).

Sincerely, Rémi

Rémi
+1. One can also use the SIOCGIFINDEX ioctl.
Teddy
For debugging and command line access, you can also use "ip link show". The integer before the colon is the ifindex.
David Joyner