Network.Socket 中提供了原始套接字类型,但在绑定套接字附近有一条注释“当前仅支持 Unix 域套接字和 Internet 系列”。如何在 haskell 中使用原始套接字?
作为工作 Python 代码,我想要实现的目标是:
import binascii
import socket
# Create raw socket.
ethType = b'FFFF' # 2 bytes, has to be >= 0x0600. FFFF is "unavailable" by IEEE.
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
sock.bind( ('lo', int(ethType,16)) )
# Create packet.
srcMac = b'000000000000' # 6 bytes
destMac = b'000000000000' # 6 bytes
header = binascii.a2b_hex( destMac + srcMac + ethType ) # 14 bytes
message = b'Hello, World!'
sock.send(header + message)
# Receive such packets
while True: print (sock.recv(65535))
EDIT1:在 Haskell 中,我sock <- socket AF_PACKET Raw 0xFFFF
用来创建一个套接字,但bindSocket
需要 aSockAddr
作为参数,可用的构造函数是
SockAddrInet PortNumber HostAddress
SockAddrInet6 PortNumber FlowInfo HostAddress6 ScopeID
SockAddrUnix String
但这些似乎都不对。
EDIT2:感谢 Yuras 的评论,我收到了工作包:
import Network.Socket
sock <- socket AF_PACKET Raw 0xFFFF
recv sock 0xFFFF
EDIT3:尝试从套接字发送数据包而不绑定它会导致异常:
sock <- socket AF_PACKET Raw 0xFFFF
send sock "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\255\255"
*** Exception: send: does not exist (No such device or address)
这是有道理的,因为内核对于实际传输数据包的接口没有任何线索。有没有办法将(原始)套接字绑定到 Haskell 中的接口?