@@ -46,6 +46,7 @@ class DomainAddress(NamedTuple):
4646
4747
4848Address : TypeAlias = tuple [str , int ] | UDPv4Address | UDPv6Address | DomainAddress
49+ SocketOption = tuple [int , int , int ]
4950
5051
5152class 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 """
0 commit comments