Skip to content

Commit

Permalink
Do not connect UDP sockets when broadcast is allowed
Browse files Browse the repository at this point in the history
This fixes issue python#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). Cound that be an
issue for connected sockets?
  • Loading branch information
Vincent Michel committed Feb 16, 2017
1 parent d84a8cb commit 1a239a8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
3 changes: 2 additions & 1 deletion asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,8 @@ def create_datagram_endpoint(self, protocol_factory,
if local_addr:
sock.bind(local_address)
if remote_addr:
yield from self.sock_connect(sock, remote_address)
if not allow_broadcast:
yield from self.sock_connect(sock, remote_address)
r_addr = remote_address
except OSError as exc:
if sock is not None:
Expand Down
18 changes: 7 additions & 11 deletions asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,9 +1083,11 @@ def sendto(self, data, addr=None):
if not data:
return

if self._address and addr not in (None, self._address):
raise ValueError('Invalid address: must be None or %s' %
(self._address,))
if self._address:
if addr not in (None, self._address):
raise ValueError(
'Invalid address: must be None or %s' % (self._address,))
addr = self._address

if self._conn_lost and self._address:
if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
Expand All @@ -1096,10 +1098,7 @@ def sendto(self, data, addr=None):
if not self._buffer:
# Attempt to send it right away first.
try:
if self._address:
self._sock.send(data)
else:
self._sock.sendto(data, addr)
self._sock.sendto(data, addr)
return
except (BlockingIOError, InterruptedError):
self._loop._add_writer(self._sock_fd, self._sendto_ready)
Expand All @@ -1119,10 +1118,7 @@ def _sendto_ready(self):
while self._buffer:
data, addr = self._buffer.popleft()
try:
if self._address:
self._sock.send(data)
else:
self._sock.sendto(data, addr)
self._sock.sendto(data, addr)
except (BlockingIOError, InterruptedError):
self._buffer.appendleft((data, addr)) # Try again later.
break
Expand Down

0 comments on commit 1a239a8

Please sign in to comment.