Skip to content

Commit

Permalink
Merge pull request #1273 from qstokkink/fix_unclosed_socket
Browse files Browse the repository at this point in the history
Fixed possibly unclosed sockets for LAN discovery
  • Loading branch information
qstokkink authored Feb 13, 2024
2 parents 3fa32c6 + fe602b9 commit 1a126ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
18 changes: 18 additions & 0 deletions ipv8/messaging/interfaces/lan_addresses/any_os/testnet1.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,40 @@ def get_addresses(self) -> set:
"""
interface_specifications = []

s = None
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("192.0.2.0", 80))
local_ip = s.getsockname()[0]
s.close()
s = None
interface_specifications.append(local_ip)
except OSError:
self.on_exception()
finally:
if s is not None:
try:
s.close()
s = None
except OSError:
self.on_exception()

s = None
try:
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.connect(("::ffff:0:192.0.2.0", 80))
local_ip = s.getsockname()[0]
s.close()
s = None
interface_specifications.append(local_ip)
except OSError:
self.on_exception()
finally:
if s is not None:
try:
s.close()
s = None
except OSError:
self.on_exception()

return set(interface_specifications)
11 changes: 10 additions & 1 deletion ipv8/messaging/interfaces/lan_addresses/unix/ioctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ def ioctl(__fd: int, __request: int, __arg: bytes, __mutate_flag: bool = ...) ->
"""
Stub for the ioctl call's types.
"""
...
else:
from fcntl import ioctl

Expand All @@ -31,6 +30,7 @@ def get_addresses(self) -> set:
"""
out_addresses = []

s = None
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for ifspec in socket.if_nameindex():
Expand All @@ -39,7 +39,16 @@ def get_addresses(self) -> set:
family, = struct.unpack(FMT_FAMILY, ifreq[16:18])
if family == socket.AF_INET:
out_addresses.append(socket.inet_ntop(socket.AF_INET, ifreq[20:24]))
s.close()
s = None
except OSError:
self.on_exception()
finally:
if s is not None:
try:
s.close()
s = None
except OSError:
self.on_exception()

return set(out_addresses)

0 comments on commit 1a126ff

Please sign in to comment.