Windows doesn't receive multicast IPv6 packets from all interfaces

前端 未结 1 2047
梦如初夏
梦如初夏 2021-01-03 13:55

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          


        
相关标签:
1条回答
  • 2021-01-03 14:34

    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())
    
    0 讨论(0)
提交回复
热议问题