7

我在 localhost 上使用多播 UDP 来实现在单台机器上运行的协作程序的松散集合。以下代码在 Mac OSX、Windows 和 linux 上运行良好。缺陷是代码也会在 localhost 网络之外接收 UDP 数据包。例如,sendSock.sendto(pkt, ('192.168.0.25', 1600))当从我的网络上的另一个盒子发送时,我的测试机器接收到。

import platform, time, socket, select

addr = ("239.255.2.9", 1600)

sendSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sendSock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 24)
sendSock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, 
    socket.inet_aton("127.0.0.1"))

recvSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
recvSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
if hasattr(socket, 'SO_REUSEPORT'):
    recvSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, True)

recvSock.bind(("0.0.0.0", addr[1]))
status = recvSock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, 
    socket.inet_aton(addr[0]) + socket.inet_aton("127.0.0.1"));

while 1:
    pkt = "Hello host: {1} time: {0}".format(time.ctime(), platform.node())
    print "SEND to: {0} data: {1}".format(addr, pkt)
    r = sendSock.sendto(pkt, addr)

    while select.select([recvSock], [], [], 0)[0]:
        data, fromAddr = recvSock.recvfrom(1024)
        print "RECV from: {0} data: {1}".format(fromAddr, data)

    time.sleep(2)

我尝试过recvSock.bind(("127.0.0.1", addr[1])),但这会阻止套接字接收任何多播流量。是否有适当的方法将 recvSock 配置为仅接受来自 127/24 网络的多播数据包,或者我是否需要测试每个接收到的数据包的地址?

4

4 回答 4

5

与此处其他答案中所述相反,IPv4 支持基于 TTL 的多播范围,如下所示:

0: node-local (not forwarded outside the current host)
1: link-local (not forwarded outside the current subnet)
< 32: site-local
< 64: region-local
< 128: continent-local
< 255: global

(它还支持管理范围的多播。)

资料来源:WR Stevens,Unix Network Programming,第2,Vol I,第 19.2 节,并进行了更正以匹配RFC 2365

于 2013-02-20T21:50:07.060 回答
2

不幸的是,多播 IP 没有任何此类“按子网过滤”功能 - 因此,除非您想使用 IPTables(在 Linux 上)或系统/网络的等效“防火墙”软件/硬件来尝试“放下”地板”每个您不喜欢的多播数据包,我认为您必须在应用程序级别执行此操作(fromAddr例如,在您的内部循环中进行测试)。来自其他主机的 IP 流量是否会降低您的性能...?

于 2010-05-21T02:40:58.673 回答
1

如果您的主机支持 IPv6,您可以使用多播地址的范围组件(这是多播前缀 FF0x 中的“x”:)通过指定范围 1(例如,仅将 IPv6 多播地址 FF01::107 用于本地主机上的“名称服务服务器”)。不幸的是,IPv4 多播机制没有明确的范围,定义管理范围的 IPv4 多播范围的 RFC 2365 (http://tools.ietf.org/html/rfc2365) 没有定义节点本地范围地址,只有一个链接本地范围范围。

于 2011-03-17T14:38:11.420 回答
0

您可以connect()在多播套接字上使用 127.0.0.1,然后 IP 堆栈可以为您过滤。

更新了源代码以演示

您可以在一台主机上多次运行此脚本并查看分发的多播数据包:

#!/usr/bin/python

import platform, time, socket, select

addr = ("239.255.2.9", 1600)

sendSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sendSock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 24)
sendSock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF,
    socket.inet_aton("127.0.0.1"))

recvSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
recvSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
if hasattr(socket, 'SO_REUSEPORT'):
    recvSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, True)

recvSock.bind(("0.0.0.0", addr[1]))
status = recvSock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
    socket.inet_aton(addr[0]) + socket.inet_aton("127.0.0.1"));

sendSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
if hasattr(socket, 'SO_REUSEPORT'):
    sendSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, True)
sendSock.bind(("127.0.0.1", addr[1]));
recvSock.connect(("127.0.0.1", addr[1]));

while 1:
    pkt = "Hello host: {1} time: {0}".format(time.ctime(), platform.node())
    print "SEND to: {0} data: {1}".format(addr, pkt)
    r = sendSock.sendto(pkt, addr)

    while select.select([recvSock], [], [], 0)[0]:
        data, fromAddr = recvSock.recvfrom(1024)
        print "RECV from: {0} data: {1}".format(fromAddr, data)

    time.sleep(2)

从不同的接口发起数据包:

#!/usr/bin/python

import platform, time, socket, select

sendSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

pkt = "Hello from network.";
sendSock.sendto(pkt, ('10.65.42.129', 1600))

我在 Windows XP 上的 Cygwin 上运行了所有三个,并验证了结果是否符合要求。

示例输出

SEND to: ('239.255.2.9', 1600) data: Hello host: NYCMDV8X5R1 time: Thu Feb 21 13:15:15 2013
RECV from: ('127.0.0.1', 1600) data: Hello host: NYCMDV8X5R1 time: Thu Feb 21 13:15:14 2013
RECV from: ('127.0.0.1', 1600) data: Hello host: NYCMDV8X5R1 time: Thu Feb 21 13:15:15 2013
SEND to: ('239.255.2.9', 1600) data: Hello host: NYCMDV8X5R1 time: Thu Feb 21 13:15:17 2013
RECV from: ('127.0.0.1', 1600) data: Hello host: NYCMDV8X5R1 time: Thu Feb 21 13:15:16 2013
RECV from: ('127.0.0.1', 1600) data: Hello host: NYCMDV8X5R1 time: Thu Feb 21 13:15:17 2013

以前输出会显示外部数据包,例如:

SEND to: ('239.255.2.9', 1600) data: Hello host: NYCMDV8X5R1 time: Thu Feb 21 13:07:05 2013
RECV from: ('10.65.42.129', 4711) data: Hello from network.
RECV from: ('127.0.0.1', 4710) data: Hello host: NYCMDV8X5R1 time: Thu Feb 21 13:07:05 2013
SEND to: ('239.255.2.9', 1600) data: Hello host: NYCMDV8X5R1 time: Thu Feb 21 13:07:11 2013
RECV from: ('10.65.42.129', 4712) data: Hello from network.
于 2010-05-22T02:50:57.893 回答