saghul / pycares

Python interface for c-ares
https://pypi.org/project/pycares/
MIT License
163 stars 74 forks source link

getnameinfo() is broken on IPv6 link-local addresses #121

Closed socketpair closed 4 years ago

socketpair commented 4 years ago

https://pycares.readthedocs.io/en/latest/channel.html#pycares.Channel.getnameinfo

Function signature is completely wrong. instead of two arguments (name and port) you have to use one argument -- tuple. For ipv4 it would be 2-element tuple (ip and port). For IPv6 it would be 4-element tuple, where the last element is a scope-id. It is essential for Link-local IPv6.

See Python's native socket.getnameinfo().

In [16]: socket.getnameinfo(('fe80::841e:8fff:fea4:4f19', 456, 0, 3), socket.NI_NUMERICHOST | socket.NI_NUMERICSERV)
Out[16]: ('fe80::841e:8fff:fea4:4f19%wlp2s0', '456')

note wlp2s0. It is interface number 3 on my PC.

saghul commented 4 years ago

Looks like the bug is two fold: the documentation is wrong, because we do take a tuple, and the code is wrong because we only look at the first 2 elements.

saghul commented 4 years ago

Would you be able to provide a patch?

saghul commented 4 years ago

cheers!