From d9ff4d40cced5199edde237ed67e3afa8172479e Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Sat, 10 Oct 2020 11:38:53 +0200 Subject: [PATCH] close raw sockets on error --- raw_socket.nim | 3 +++ 1 file changed, 3 insertions(+) diff --git a/raw_socket.nim b/raw_socket.nim index a70a18d..8286838 100644 --- a/raw_socket.nim +++ b/raw_socket.nim @@ -53,6 +53,7 @@ proc setupEthernetCapturingSocket*(iface: NetworkInterface): AsyncFD = if bindAddr(result.SocketHandle, cast[ptr SockAddr](addr sa), sizeof(Sockaddr_ll).SockLen) != 0: + closeSocket(result) raise newException(RawSocketError, "cannot bind to interface") # Enable promiscuous mode, see netdevice(7) man page @@ -62,6 +63,7 @@ proc setupEthernetCapturingSocket*(iface: NetworkInterface): AsyncFD = PACKET_ADD_MEMBERSHIP.cint, addr req, sizeof(req).SockLen) != 0: + closeSocket(result) raise newException(RawSocketError, "cannot enable promiscuous mode") proc setupTcpInjectingSocket*(): AsyncFD = @@ -74,4 +76,5 @@ proc setupTcpInjectingSocket*(): AsyncFD = var sockOpt: cint = 1 if setsockopt(result.SocketHandle, IPPROTO_IP, IP_HDRINCL, addr sockOpt, sizeof(sockOpt).SockLen) != 0: + closeSocket(result) raise newException(RawSocketError, "cannot set IP_HDRINCL option")