Skip to content

Commit f60d0db

Browse files
authored
Merge pull request #1357 from egbertbouman/sockopts
Disable IPv4 on UDPv6Endpoint
2 parents 4447c1e + 692b6ab commit f60d0db

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

ipv8/messaging/interfaces/udp/endpoint.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class DomainAddress(NamedTuple):
4646

4747

4848
Address: TypeAlias = tuple[str, int] | UDPv4Address | UDPv6Address | DomainAddress
49+
SocketOption = tuple[int, int, int]
4950

5051

5152
class UDPEndpoint(Endpoint, asyncio.DatagramProtocol):
@@ -55,7 +56,7 @@ class UDPEndpoint(Endpoint, asyncio.DatagramProtocol):
5556

5657
SOCKET_FAMILY = socket.AF_INET
5758

58-
def __init__(self, port: int = 0, ip: str = "0.0.0.0") -> None:
59+
def __init__(self, port: int = 0, ip: str = "0.0.0.0", sockopts: list[SocketOption] | None = None) -> None:
5960
"""
6061
Create a new UDP endpoint that will attempt to bind on the given ip and ATTEMPT to claim the given port.
6162
@@ -66,6 +67,7 @@ def __init__(self, port: int = 0, ip: str = "0.0.0.0") -> None:
6667
self._port = port
6768
self._ip = ip
6869
self._running = False
70+
self._sockopts = [(socket.SOL_SOCKET, socket.SO_RCVBUF, 870400)] if sockopts is None else sockopts
6971

7072
# The transport object passed on by Asyncio
7173
self._transport: DatagramTransport | None = None
@@ -121,7 +123,8 @@ async def open(self) -> bool:
121123
# It is recommended that this endpoint is opened at port = 0,
122124
# such that the OS handles the port assignment
123125
s = socket.socket(self.SOCKET_FAMILY, socket.SOCK_DGRAM)
124-
s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 870400)
126+
for level, optname, value in self._sockopts:
127+
s.setsockopt(level, optname, value)
125128
s.bind((self._ip, self._port))
126129
s.setblocking(False)
127130
self._port = s.getsockname()[1]
@@ -189,7 +192,8 @@ def __init__(self, port: int = 0, ip: str = "::") -> None:
189192
"""
190193
Create new UDP endpoint over IPv6.
191194
"""
192-
super().__init__(port, ip)
195+
super().__init__(port, ip, [(socket.SOL_SOCKET, socket.SO_RCVBUF, 870400),
196+
(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)])
193197

194198
def datagram_received(self, datagram: bytes, addr: Address) -> None:
195199
"""

ipv8/test/test_taskmanager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import threading
4+
import time
45
from asyncio import AbstractEventLoop, CancelledError, Future, ensure_future, get_running_loop, sleep
56
from contextlib import suppress
67
from typing import Any
@@ -282,7 +283,7 @@ async def test_register_executor_task_anon(self) -> None:
282283
"""
283284
test = lambda: None
284285

285-
_ = self.tm.register_executor_task("test", test)
286+
_ = self.tm.register_executor_task("test", time.sleep, 0.1)
286287
self.assertEqual(1, len(self.tm.get_tasks()))
287288

288289
with self.assertRaises(RuntimeError):

0 commit comments

Comments
 (0)