我正在开发一个软件定义的网络研究项目,我需要的是制作一个简单的UDP服务器,将数据标记放入UDP数据包的目标选项字段(IPv6)中。我期待sendmsg()recvmsg()命令,或者使用setsockopt()和getsockopt()。因此,Python 2.7没有sendmsg()或recvmsg(),虽然我可以通过setsockopt()将正确的标签加载到数据包中(我在Wireshark中看到它),但getsockopt()命令只返回0,即使标题在那里。IPv6目标选项标题
#Python 2.7 client
#This code does put the dest opts header onto the packet correctly
#dst_header is a packed binary string (construction details irrelevant--
# it appears correctly formatted and parsed in Wireshark)
addr = ("::", 5000, 0, 0)
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_DSTOPTS, dst_header)
s.sendto('This is my message ', addr)
#Python 2.7 server
addr = ("::", 5000, 0, 0)
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVDSTOPTS, 1)
s.bind(addr)
data, remote_address = s.recvfrom(MAX)
header_data = s.getsockopt(socket.IPPROTO_IPV6, socket.IPPROTO_DSTOPTS, 1024)
我也试过这个在Python 3.4,里面确实有SENDMSG()和recvmsg(),但我只是得到一个错误信息 “OSERROR:[错误22]:无效的参数”,即使我”中号传递给它(显然)正确类型:
s.sendmsg(["This is my message"], (socket.IPPROTO_IPV6, socket.IPV6_DSTOPTS, dst_header), 0, addr) #dst_header is same string as for 2.7 version
它看起来像SENDMSG()和recvmsg()是通过UNIX文件描述符,这是不是我想要做的使用率的99%。任何人有任何想法?我认为这只是一个四五行没有特别的计划,但我很难过。