5

如何获得一个套接字来接收发往 IPv6 子网路由器任播地址的数据包?

我无法找到有关如何执行此操作的任何信息。

绝望中,我尝试使用 socket.setsockopt ,就像加入多播组一样:

# 7 is the interface number 
s = socket(AF_INET6, SOCK_DGRAM)
packed_iface_num = struct.pack("I", 7) 
group = inet_pton(AF_INET6, 'fd36:d00d:d00d:47cb::') + packed_iface_num

# socket.error: (22, 'Invalid argument')
s.setsockopt(IPPROTO_IPV6, IPV6_JOIN_GROUP,  group)

并且还使用绑定

# socket.error: (99, 'Cannot assign requested address')
s.bind(('fd36:773e:6b4c:47cb::', 9876))

正如预期的那样,这些都不起作用。有没有办法做到这一点?

4

2 回答 2

2

而不是IPV6_JOIN_GROUP,尝试传递IPV6_JOIN_ANYCAST给您的s.setsockopt()代码。不幸的是,Pythonsocket模块没有定义它,但您应该能够传递等效的整数。在 LinuxIPV6_JOIN_ANYCAST中是27IPV6_LEAVE_ANYCAST28. (在 /usr/include/linux/in6.h 中定义)

我能找到的最好的文档来自描述 Linux 内核任播补丁的 lkml 电子邮件

加入和离开任播组的应用程序接口是 2 个新的 setsockopt() 调用:IPV6_JOIN_ANYCAST 和 IPV6_LEAVE_ANYCAST。参数与相应的多播操作相同。

愿舞蹈卡梅与你同在!

于 2009-03-03T14:11:59.357 回答
0

IPV6_JOIN_ANYCASTIPV6_LEAVE_ANYCASTsocket 选项是非标准的 Linux 主义。

If you'd like your code to be portable, then you should probably do it the standard way, i.e. assign the subnet routers anycast address to the appropriate interface, then bind your socket to the wildcard address and discard everything not sent to the subnet router anycast address. Remember, you're not supposed to send packets with the anycast address in the source field, and you can't open a read-only socket in the standard sockets API.

Assigning an interface address should be a privileged operation on any reasonable operating system, and that's the part that isn't going to be standard whatever you do. If you must do that programmatically, then it will mean (on BSD for example) using something like the SIOCAIFADDR_IN6 code and the ioctl() system call. Make sure to set the IN6_IFF_ANYCAST flag in the ifra_flags field of the interface alias request structure.

于 2009-05-27T23:00:50.967 回答