implement setupTcpInjectingSocket

This commit is contained in:
Christian Ulrich 2020-07-11 14:45:18 +02:00
parent 466d34963b
commit 406bb02515
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 16 additions and 1 deletions

View File

@ -1,5 +1,12 @@
import asyncdispatch
from nativesockets import SOCK_RAW, bindAddr, htons
from nativesockets import
AF_INET,
IPPROTO_IP,
IPPROTO_TCP,
SOCK_RAW,
bindAddr,
htons,
setSockOptInt
from posix import setsockopt, SockAddr, SockLen, SocketHandle
import network_interface
@ -27,6 +34,7 @@ var
AF_PACKET {.importc: "AF_PACKET", header: "<sys/socket.h>".}: cushort
SOL_PACKET {.importc: "SOL_PACKET", header: "<sys/socket.h>".}: cushort
ETH_P_ALL {.importc: "ETH_P_ALL", header: "<linux/if_ether.h>".}: cushort
IP_HDRINCL {.importc: "IP_HDRINCL", header: "<netinet/in.h>".}: cint
PACKET_ADD_MEMBERSHIP {.importc: "PACKET_ADD_MEMBERSHIP", header: "<linux/if_packet.h>".}: cushort
PACKET_MR_PROMISC {.importc: "PACKET_MR_PROMISC", header: "<linux/if_packet.h>".}: cushort
@ -57,4 +65,11 @@ proc setupEthernetCapturingSocket*(iface: NetworkInterface): AsyncFD =
sizeof(req).SockLen) != 0:
raise newException(RawSocketError, "cannot enable promiscuous mode")
proc setupTcpInjectingSocket*(): AsyncFD =
# FIXME: would bindAddr be beneficial?
result = createAsyncNativeSocket(AF_INET.cint, SOCK_RAW.cint,
IPPROTO_TCP.cint)
# Tell the kernel to not generate an IP header, as we generate it ourselves,
# see raw(7) man page.
setSockOptInt(result.SocketHandle, IPPROTO_IP.int, IP_HDRINCL, 1)