Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit 913d7cb

Browse files
author
Vincent Michel
committed
Do not connect UDP sockets when broadcast is allowed
This fixes issue #480. The _SelectorDatagramTransport.sendto method has to be modified so _sock.sendto is used in all cases (since there is no proper way to tell if the socket is connected or not). Could that be an issue for connected sockets?
1 parent d84a8cb commit 913d7cb

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

asyncio/base_events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ def create_datagram_endpoint(self, protocol_factory,
916916
if local_addr:
917917
sock.bind(local_address)
918918
if remote_addr:
919-
yield from self.sock_connect(sock, remote_address)
919+
if not allow_broadcast:
920+
yield from self.sock_connect(sock, remote_address)
920921
r_addr = remote_address
921922
except OSError as exc:
922923
if sock is not None:

asyncio/selector_events.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,9 +1083,11 @@ def sendto(self, data, addr=None):
10831083
if not data:
10841084
return
10851085

1086-
if self._address and addr not in (None, self._address):
1087-
raise ValueError('Invalid address: must be None or %s' %
1088-
(self._address,))
1086+
if self._address:
1087+
if addr not in (None, self._address):
1088+
raise ValueError(
1089+
'Invalid address: must be None or %s' % (self._address,))
1090+
addr = self._address
10891091

10901092
if self._conn_lost and self._address:
10911093
if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
@@ -1096,10 +1098,7 @@ def sendto(self, data, addr=None):
10961098
if not self._buffer:
10971099
# Attempt to send it right away first.
10981100
try:
1099-
if self._address:
1100-
self._sock.send(data)
1101-
else:
1102-
self._sock.sendto(data, addr)
1101+
self._sock.sendto(data, addr)
11031102
return
11041103
except (BlockingIOError, InterruptedError):
11051104
self._loop._add_writer(self._sock_fd, self._sendto_ready)
@@ -1119,10 +1118,7 @@ def _sendto_ready(self):
11191118
while self._buffer:
11201119
data, addr = self._buffer.popleft()
11211120
try:
1122-
if self._address:
1123-
self._sock.send(data)
1124-
else:
1125-
self._sock.sendto(data, addr)
1121+
self._sock.sendto(data, addr)
11261122
except (BlockingIOError, InterruptedError):
11271123
self._buffer.appendleft((data, addr)) # Try again later.
11281124
break

0 commit comments

Comments
 (0)