close raw sockets on error

This commit is contained in:
Christian Ulrich 2020-10-10 11:38:53 +02:00
parent 6dff0847b3
commit d9ff4d40cc
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 3 additions and 0 deletions

View File

@ -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")