Search code examples
pythonudpipv6multicast

Sending IPv6 multicast packets through a specific network interface


I am trying to send IPv6 UDP packets to all of the nodes on the local network segment, in python, over Windows.

I have several network interfaces in my computer, and I want to know how to specify the network interface for sending the packets.

I have tried sending the packets to the multicast address ff02::1, using socket.sendto (without binding), but the packets are sent in the wrong network interface.

Any idea how can I specify the network adapter? (I read about BINDTODEVICE, but it won't work on windows, and some methods using bind to broadcast IP address classes, but only for IPv4).

Thanks!


Solution

  • I didn't really like the previous solution, so I kept looking for others.

    The first option that worked for me is to bind the sender socket to the specific network interface address. The network interfaces addresses can be found using netifaces module, and I used this helpful answer to specify the Ethernet address.

    Another option that might work is the IPV6_MULTICAST_IF option-

    #x is the relevant interface index
    sock.setsockopt(socket.IPPROTO_IPV6,socket.IPV6_MULTICAST_IF,x)
    

    In Windows, python 2.7, one should add the line

    socket.IPPROTO_IPV6=41
    

    before this code (since the relevant enum is not well defined).

    Additional information can be found here (Windows) or here (Linux).

    Although it seems like a simpler solution, I didn't completely managed to make it work, and not sure what is the proper way to find the right interface index (on Windows, Linux has several options).