2
votes

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!

3

3 Answers

1
votes

This is a problem with your system configuration. The operating system needs to be configured with the proper IPv6 routes to ensure the packets go out on the correct interface. It is not up to the application to decide, similar to how it is not the application's job to assign an IP address to the network interface - it's all the responsibility of the OS.

Here is an answer that explains how this is done under Linux. Feel free to add a comment with a link for how it's done on Windows if anyone knows.

0
votes

According the answer to this question- multicasting with ff12::1 sometimes works better than multicasting with ff02::1. I tried it and it worked- the packets were sent through the Ethernet network interface (as I wanted), and not in the WiFi as they were before.

However, I don't know why is it working, and I couldn't find any reference for it in the IPv6 RFC or anywhere else in the internet. Explanations will be welcomed :)

0
votes

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).