From cc33fbf52beecb7276753010170d318ab14ce4f0 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Mon, 29 Jan 2024 08:26:21 +0100 Subject: [PATCH 1/2] Add support for special hostname --- tests/test_udp.py | 16 ++++++++++++++++ uvloop/dns.pyx | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/tests/test_udp.py b/tests/test_udp.py index dd739650..1b7953f2 100644 --- a/tests/test_udp.py +++ b/tests/test_udp.py @@ -378,6 +378,22 @@ def test_udp_sendto_dns(self): s_transport.close() self.loop.run_until_complete(asyncio.sleep(0.01)) + def test_udp_sendto_broadcast(self): + coro = self.loop.create_datagram_endpoint( + asyncio.DatagramProtocol, + local_addr=('127.0.0.1', 0), + family=socket.AF_INET) + + s_transport, server = self.loop.run_until_complete(coro) + + try: + s_transport.sendto(b'aaaa', ('', 80)) + except ValueError as exc: + raise AssertionError('sendto raises {}.'.format(exc)) + + s_transport.close() + self.loop.run_until_complete(asyncio.sleep(0.01)) + def test_send_after_close(self): coro = self.loop.create_datagram_endpoint( asyncio.DatagramProtocol, diff --git a/uvloop/dns.pyx b/uvloop/dns.pyx index 7aad6319..3f3840ed 100644 --- a/uvloop/dns.pyx +++ b/uvloop/dns.pyx @@ -113,6 +113,10 @@ cdef __convert_pyaddr_to_sockaddr(int family, object addr, port = __port_to_int(port, None) + # resolve special hostname to the broadcast address + if host == b'': + host = b'255.255.255.255' + ret.addr_size = sizeof(system.sockaddr_in) err = uv.uv_ip4_addr(host, port, &ret.addr) if err < 0: From 2061cfbf0d17398e30d86df3ddb5c56a84448a72 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Sat, 7 Sep 2024 23:51:38 +0200 Subject: [PATCH 2/2] Resolve special hostname in udp handle --- tests/test_dns.py | 4 ++++ uvloop/dns.pyx | 4 ---- uvloop/handles/udp.pyx | 4 ++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/test_dns.py b/tests/test_dns.py index 66da026b..6f3f7d65 100644 --- a/tests/test_dns.py +++ b/tests/test_dns.py @@ -199,6 +199,10 @@ def test_getaddrinfo_22(self): self._test_getaddrinfo(payload, 80) self._test_getaddrinfo(payload, 80, type=socket.SOCK_STREAM) + def test_getaddrinfo_broadcast(self): + self._test_getaddrinfo('', 80) + self._test_getaddrinfo('', 80, type=socket.SOCK_STREAM) + ###### def test_getnameinfo_1(self): diff --git a/uvloop/dns.pyx b/uvloop/dns.pyx index 2225b5c5..c6be7cbe 100644 --- a/uvloop/dns.pyx +++ b/uvloop/dns.pyx @@ -113,10 +113,6 @@ cdef __convert_pyaddr_to_sockaddr(int family, object addr, port = __port_to_int(port, None) - # resolve special hostname to the broadcast address - if host == b'': - host = b'255.255.255.255' - ret.addr_size = sizeof(system.sockaddr_in) err = uv.uv_ip4_addr(host, port, &ret.addr) if err < 0: diff --git a/uvloop/handles/udp.pyx b/uvloop/handles/udp.pyx index bbe60d56..b39b7add 100644 --- a/uvloop/handles/udp.pyx +++ b/uvloop/handles/udp.pyx @@ -208,6 +208,10 @@ cdef class UDPTransport(UVBaseTransport): if addr is None: saddr = NULL else: + # resolve special hostname to the broadcast address before use + if self._family == uv.AF_INET and addr[0] == '': + addr = (b'255.255.255.255', addr[1]) + try: __convert_pyaddr_to_sockaddr(self._family, addr, &saddr_st)