Windows不会从所有接口接收多播IPv6数据包 [英] Windows doesn't receive multicast IPv6 packets from all interfaces

查看:192
本文介绍了Windows不会从所有接口接收多播IPv6数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此python 2.7代码在Windows上接收IPv6组播数据包(发送到ff02 :: 1地址)-

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

我从通过以太网连接到我的计算机的另一台计算机发送数据包;另外,我的电脑还具有WiFi接口.尽管在嗅探与Wireshark的以太网连接时可以看到相关的数据包,但是此代码未接收到数据包.

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.

但是,当我禁用WiFi 网卡时,将接收到数据包. 这使我认为启用WiFi接口后,代码仅侦听来自该接口的数据包.

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?

谢谢!

推荐答案

解决了:)

因此,显然IPv6 监听所有接口的多播.语法

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

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

是错误的.根据 ,mreq由组ID和接口ID组成,其中0是默认接口(在我的情况下为WiFi).为了侦听来自其他接口的多播,应指定网络接口索引.

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.

网络接口索引是运行ipconfig时在ipv6地址中%之后出现的数字,也可以在cmd中运行路由打印"时找到.

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.

我使用以下代码在python上找到了它:

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

然后-

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

这篇关于Windows不会从所有接口接收多播IPv6数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆