From 406bb02515485893859cd8822dd86da2ad649dbd Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Sat, 11 Jul 2020 14:45:18 +0200 Subject: [PATCH] implement setupTcpInjectingSocket --- raw_socket.nim | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/raw_socket.nim b/raw_socket.nim index de7f24d..3bca0be 100644 --- a/raw_socket.nim +++ b/raw_socket.nim @@ -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: "".}: cushort SOL_PACKET {.importc: "SOL_PACKET", header: "".}: cushort ETH_P_ALL {.importc: "ETH_P_ALL", header: "".}: cushort + IP_HDRINCL {.importc: "IP_HDRINCL", header: "".}: cint PACKET_ADD_MEMBERSHIP {.importc: "PACKET_ADD_MEMBERSHIP", header: "".}: cushort PACKET_MR_PROMISC {.importc: "PACKET_MR_PROMISC", header: "".}: 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)