问题
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!
回答1:
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 :)
回答2:
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).
来源:https://stackoverflow.com/questions/47077323/sending-ipv6-multicast-packets-through-a-specific-network-interface