diff --git a/raw_socket.nim b/raw_socket.nim index 3bca0be..a70a18d 100644 --- a/raw_socket.nim +++ b/raw_socket.nim @@ -1,12 +1,10 @@ import asyncdispatch from nativesockets import AF_INET, - IPPROTO_IP, IPPROTO_TCP, SOCK_RAW, bindAddr, - htons, - setSockOptInt + htons from posix import setsockopt, SockAddr, SockLen, SocketHandle import network_interface @@ -34,6 +32,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 + IPPROTO_IP {.importc: "IPPROTO_IP", header: "".}: cint 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 @@ -70,6 +69,9 @@ proc setupTcpInjectingSocket*(): AsyncFD = 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) + ## Tell the kernel to not generate an IP header, as we generate it ourselves, + ## see raw(7) man page. + var sockOpt: cint = 1 + if setsockopt(result.SocketHandle, IPPROTO_IP, IP_HDRINCL, addr sockOpt, + sizeof(sockOpt).SockLen) != 0: + raise newException(RawSocketError, "cannot set IP_HDRINCL option")