Windows doesn't receive multicast IPv6 packets from all interfaces

邮差的信 提交于 2019-12-03 20:28:10

问题


I am trying to receive IPv6 multicast packets (sent to the ff02::1 address) on Windows using this python 2.7 code-

import socket
import win_inet_pton
import struct

socket.IPPROTO_IPV6=41  #because using python 2.7 on wondows

PORT = 1234
UDP_BROADCAST_IPv6 = "ff02::1"

sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

sock.bind(("",PORT)) # not working with "::" either

# Join multicast group
addrinfo = socket.getaddrinfo(UDP_BROADCAST_IPv6, None)[0]
group = socket.inet_pton(addrinfo[0], addrinfo[4][0])
mreq = group + struct.pack('@I', 0)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)

while True:
    msg=sock.recv(1024)
    print msg

I send packets from another computer that is connected to my computer via Ethernet; in addition, my computer also has a WiFi interface. Although I'm able to see the relevant packets when sniffing the Ethernet connection with Wireshark, the packets are not received by this code.

However, when I disable the WiFi network card, the packets are received. This makes me think that while the WiFi interface is enabled the code listens only to packets from that interface.

I read that binding to "" should enable receiving packets from all network interfaces, but for some reason it doesn't work for me.

Does anyone have any idea to something that I have forgotten to do? or a different way to solve this?

Thanks!


回答1:


Solved it :)

So apparently IPv6 doesn't listen to multicast from all interfaces. This syntax

mreq = group + struct.pack('@I', 0)

was wrong. According to this, mreq is composed of the group id and the interface id, where 0 is the default interface (in my case- WiFi). In order to listen to multicast from other interfaces, the network interface index should be specified.

The network interface index is the number thet appears after the % in the ipv6 address when running ipconfig, and can also be found running "route print" in cmd.

I used this code to find it on python:

import netifaces as ni
import _winreg as wr # use "winreg" in python3

def get_ethernet_ipv6_ifindex():
    x=ni.interfaces()
    con_names=get_connection_name_from_guid(x)
    ethernet_index= con_names.index('Ethernet')

    addresses= ni.ifaddresses(x[ethernet_index])
    brod_addr=addresses[socket.AF_INET6][-1]["broadcast"]

    return int(brod_addr[brod_addr.find("%")+1:])

"""
Taken from the very helpful https://stackoverflow.com/questions/29913516/how-to-get-meaningful-network-interface-names-instead-of-guids-with-netifaces-un
"""
def get_connection_name_from_guid(iface_guids):
    iface_names = ['(unknown)' for i in range(len(iface_guids))]
    reg = wr.ConnectRegistry(None, wr.HKEY_LOCAL_MACHINE)
    reg_key = wr.OpenKey(reg, r'SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}')
    for i in range(len(iface_guids)):
        try:
            reg_subkey = wr.OpenKey(reg_key, iface_guids[i] + r'\Connection')
            iface_names[i] = wr.QueryValueEx(reg_subkey, 'Name')[0]
        except WindowsError:
            pass
    return iface_names

And then-

mreq = group + struct.pack('@I', get_ethernet_ipv6_ifindex())


来源:https://stackoverflow.com/questions/47280816/windows-doesnt-receive-multicast-ipv6-packets-from-all-interfaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!