From 5cc152c0395e9d4bf29b53b7d4fb4a1cebcb3513 Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Fri, 17 Jul 2020 00:09:09 +0200 Subject: [PATCH] nativesockets.IPPROTO_IP is wrong here; use setsockopt instead of setSockOptInt --- raw_socket.nim | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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")