用Python接收UDPv6多播

2024-06-30 12:53:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法使用Python接收UDPv6包。包来自SLIP接口,使用6LBR,整个运行在Beagleboard上,但事实并非如此。 Tcpdump可以毫无问题地捕获数据包:

10:17:32.220009 IP6 (hlim 64, next-header UDP (17) payload length: 21)
fe80::200:0:0:3.3000 > ff02::1.3000: [udp sum ok] UDP, length 13
0x0000:  6000 0000 0015 1140 fe80 0000 0000 0000  `......@........
0x0010:  0200 0000 0000 0003 ff02 0000 0000 0000  ................
0x0020:  0000 0000 0000 0001 0bb8 0bb8 0015 f9fe  ................
0x0030:  4d65 7373 6167 6520 3537 3236 00         Message.5726.

但是,我无法使用Python接收数据包:

^{pr2}$

另一方面,当我生成本地多播时,同样的代码能够接收数据包。Tcpdump分组输出,可接收:

10:29:37.301406 IP6 (hlim 3, next-header UDP (17) payload length: 30)
fe80::c03a:f9ff:fe3d:9b30.56963 > ff02::1.3000: [udp sum ok] UDP, length 22
    0x0000:  6000 0000 001e 1103 fe80 0000 0000 0000  `...............
    0x0010:  c03a f9ff fe3d 9b30 ff02 0000 0000 0000  .:...=.0........
    0x0020:  0000 0000 0000 0001 de83 0bb8 001e 20ca  ................
    0x0030:  3134 3336 3639 3639 3737 2e33 3030 3532  1436696977.30052
    0x0040:  3164 7570 6100                           1dupa.

为什么?如何使用python捕获第一个包?在

ARM Linux ip配置:

debian@arm:~$ sudo ifconfig
eth0      Link encap:Ethernet  HWaddr c2:3a:f9:3d:9b:30  
          inet addr:192.168.1.206  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::c03a:f9ff:fe3d:9b30/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST DYNAMIC  MTU:1500  Metric:1
          RX packets:261457 errors:0 dropped:0 overruns:0 frame:0
          TX packets:568962 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:28323386 (27.0 MiB)  TX bytes:71483396 (68.1 MiB)

Contiki节点的IP配置:

MAC 00:00:00:00:00:00:00:03 Contiki-2.7-7-g9698ca5 started. 
Node id is set to 3.
nullmac nullrdc, channel check rate 128 Hz, radio channel 26
Tentative link-local IPv6 address fe80:0000:0000:0000:0200:0000:0000:0003
Starting 'Unicast sender example process'
IPv6 addresses: bbbb::200:0:0:3
Sending unicast to ff02::1

Tags: 数据包lengthnexttcpdumpheaderpayloadudpfe80
1条回答
网友
1楼 · 发布于 2024-06-30 12:53:26

你忘了加入多播组。进程绑定到端口3000上的通配符地址。任何到达带有该目标端口号的数据包都将被传递到您的进程,但实际上您并没有要求操作系统将该进程订阅到多播组,因此没有理由让该数据包到达该多播组(除非它是在本地创建的)。在

要为系统订阅多播连衣裙,请执行以下操作:

# Get the interface index for eth0
interface_index = socket.if_nametoindex("eth0")

# Create the JOIN option data
mc_addr = ipaddress.IPv6Address('ff02::1')
join_data = struct.pack('16sI', mc_addr.packed, interface_index)

# And join the group
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, join_data)

相关问题 更多 >