Skip to content

Commit 63deaa5

Browse files
vxgmichelmiss-islington
authored andcommitted
bpo-31922: Do not connect UDP sockets when broadcast is allowed (GH-423)
*Moved from python/asyncio#493.* This PR fixes issue python/asyncio#480, as explained in [this comment](python/asyncio#480 (comment)). The `_SelectorDatagramTransport.sendto` method has to be modified ~~so `_sock.sendto` is used in all cases (because it is tricky to reliably tell if the socket is connected or not). Could that be an issue for connected sockets?~~ *EDIT* ... so `_sock.send` is used only if `_sock` is connected. It also protects `socket.getsockname` against `OSError` in `_SelectorTransport`. This might happen on Windows if the socket is not connected (e.g. for UDP broadcasting). https://bugs.python.org/issue31922
1 parent 91cc01f commit 63deaa5

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

Lib/asyncio/base_events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,8 @@ async def create_datagram_endpoint(self, protocol_factory,
13061306
if local_addr:
13071307
sock.bind(local_address)
13081308
if remote_addr:
1309-
await self.sock_connect(sock, remote_address)
1309+
if not allow_broadcast:
1310+
await self.sock_connect(sock, remote_address)
13101311
r_addr = remote_address
13111312
except OSError as exc:
13121313
if sock is not None:

Lib/asyncio/selector_events.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,10 @@ class _SelectorTransport(transports._FlowControlMixin,
587587
def __init__(self, loop, sock, protocol, extra=None, server=None):
588588
super().__init__(extra, loop)
589589
self._extra['socket'] = sock
590-
self._extra['sockname'] = sock.getsockname()
590+
try:
591+
self._extra['sockname'] = sock.getsockname()
592+
except OSError:
593+
self._extra['sockname'] = None
591594
if 'peername' not in self._extra:
592595
try:
593596
self._extra['peername'] = sock.getpeername()
@@ -976,9 +979,11 @@ def sendto(self, data, addr=None):
976979
if not data:
977980
return
978981

979-
if self._address and addr not in (None, self._address):
980-
raise ValueError(
981-
f'Invalid address: must be None or {self._address}')
982+
if self._address:
983+
if addr not in (None, self._address):
984+
raise ValueError(
985+
f'Invalid address: must be None or {self._address}')
986+
addr = self._address
982987

983988
if self._conn_lost and self._address:
984989
if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
@@ -989,7 +994,7 @@ def sendto(self, data, addr=None):
989994
if not self._buffer:
990995
# Attempt to send it right away first.
991996
try:
992-
if self._address:
997+
if self._extra['peername']:
993998
self._sock.send(data)
994999
else:
9951000
self._sock.sendto(data, addr)
@@ -1012,7 +1017,7 @@ def _sendto_ready(self):
10121017
while self._buffer:
10131018
data, addr = self._buffer.popleft()
10141019
try:
1015-
if self._address:
1020+
if self._extra['peername']:
10161021
self._sock.send(data)
10171022
else:
10181023
self._sock.sendto(data, addr)

Lib/test/test_asyncio/test_base_events.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,23 @@ def test_create_datagram_endpoint_connect_err(self):
15861586
self.assertRaises(
15871587
OSError, self.loop.run_until_complete, coro)
15881588

1589+
def test_create_datagram_endpoint_allow_broadcast(self):
1590+
protocol = MyDatagramProto(create_future=True, loop=self.loop)
1591+
self.loop.sock_connect = sock_connect = mock.Mock()
1592+
sock_connect.return_value = []
1593+
1594+
coro = self.loop.create_datagram_endpoint(
1595+
lambda: protocol,
1596+
remote_addr=('127.0.0.1', 0),
1597+
allow_broadcast=True)
1598+
1599+
transport, _ = self.loop.run_until_complete(coro)
1600+
self.assertFalse(sock_connect.called)
1601+
1602+
transport.close()
1603+
self.loop.run_until_complete(protocol.done)
1604+
self.assertEqual('CLOSED', protocol.state)
1605+
15891606
@patch_socket
15901607
def test_create_datagram_endpoint_socket_err(self, m_socket):
15911608
m_socket.getaddrinfo = socket.getaddrinfo

Lib/test/test_asyncio/test_selector_events.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ def setUp(self):
10651065
self.sock.fileno.return_value = 7
10661066

10671067
def datagram_transport(self, address=None):
1068+
self.sock.getpeername.side_effect = None if address else OSError
10681069
transport = _SelectorDatagramTransport(self.loop, self.sock,
10691070
self.protocol,
10701071
address=address)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:meth:`asyncio.AbstractEventLoop.create_datagram_endpoint`:
2+
Do not connect UDP socket when broadcast is allowed.
3+
This allows to receive replies after a UDP broadcast.

0 commit comments

Comments
 (0)