Skip to content

Conversation

@jpbede
Copy link
Contributor

@jpbede jpbede commented Jan 29, 2024

Currently sendto would fail if you use the special hostname <broadcast>, which is a valid use according to the Python docs. This PR proposes to to "resolve" it to the broadcast address before passing it to uv_ip4_addr.

This resolves the issue #540

Copy link
Member

@fantix fantix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! This is almost a good one, except that __convert_pyaddr_to_sockaddr also affected getaddrinfo():

>>> import uvloop; loop=uvloop.new_event_loop(); loop.run_until_complete(loop.getaddrinfo('<broadcast>', 0, type=socket.SOCK_DGRAM))
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('255.255.255.255', 0))]

while CPython won't return 255.255.255.255:

import socket
>>> socket.getaddrinfo('<broadcast>', 0, type=socket.SOCK_DGRAM)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/socket.py", line 964, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno -2] Name or service not known

Or asyncio:

>>> import asyncio
>>> loop=asyncio.new_event_loop()
>>> loop.run_until_complete(loop.getaddrinfo('<broadcast>',0,type=socket.SOCK_DGRAM))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 901, in getaddrinfo
    return await self.run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/socket.py", line 964, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno -2] Name or service not known

@jpbede
Copy link
Contributor Author

jpbede commented Sep 7, 2024

Thanks for the review. I moved the "resolving" of <broadcast> to the UDP handle and added a test case for getaddrinfo

Copy link
Member

@fantix fantix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@rlippmann
Copy link

Any chance of this being merged?

@fantix fantix merged commit 3060ceb into MagicStack:master Jan 12, 2026
43 of 51 checks passed
@rlippmann
Copy link

I believe this closes this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants