Skip to content

getaddrinfo: inconsistent handling of port=None #75381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
njsmith opened this issue Aug 14, 2017 · 2 comments
Closed

getaddrinfo: inconsistent handling of port=None #75381

njsmith opened this issue Aug 14, 2017 · 2 comments
Labels
3.7 (EOL) end of life extension-modules C modules in the Modules dir

Comments

@njsmith
Copy link
Contributor

njsmith commented Aug 14, 2017

BPO 31198
Nosy @giampaolo, @njsmith

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-08-15.02:11:14.396>
created_at = <Date 2017-08-14.07:35:10.690>
labels = ['extension-modules', '3.7', 'invalid']
title = 'getaddrinfo: inconsistent handling of port=None'
updated_at = <Date 2017-08-15.02:11:14.395>
user = 'https://github.com/njsmith'

bugs.python.org fields:

activity = <Date 2017-08-15.02:11:14.395>
actor = 'njs'
assignee = 'none'
closed = True
closed_date = <Date 2017-08-15.02:11:14.396>
closer = 'njs'
components = ['Extension Modules']
creation = <Date 2017-08-14.07:35:10.690>
creator = 'njs'
dependencies = []
files = []
hgrepos = []
issue_num = 31198
keywords = []
message_count = 2.0
messages = ['300236', '300281']
nosy_count = 2.0
nosy_names = ['giampaolo.rodola', 'njs']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue31198'
versions = ['Python 3.7']

@njsmith
Copy link
Contributor Author

njsmith commented Aug 14, 2017

socket.getaddrinfo accepts None as a port argument, and translates it into 0. This is handy, because bind() understands 0 to mean "pick a port for me", and if you want bind to pick a port for you and port=None is a slightly more obvious way to say that then port=0.

For example:

>>> socket.getaddrinfo("127.0.0.1", None)
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('127.0.0.1', 0))]

socket.getaddrinfo also accepts None as a host name; this is necessary because the underlying getaddrinfo(3) call has special handling for host=NULL, and we need some way to access it:

>>> socket.getaddrinfo(None, 0)
[(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_RAW: 3>, 0, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('127.0.0.1', 0))]

However, even though both of these features are supported separately... if you try to use them *together*, then socket.getaddrinfo errors out:

>>> socket.getaddrinfo(None, None)
socket.gaierror: [Errno -2] Name or service not known

I expected that last call to be equivalent to socket.getaddrinfo(None, 0).

@njsmith njsmith added 3.7 (EOL) end of life extension-modules C modules in the Modules dir labels Aug 14, 2017
@njsmith
Copy link
Contributor Author

njsmith commented Aug 15, 2017

Ugh, apparently this weird behavior is actually mandated by the RFC :-(.

RFC 3493:

The nodename and servname arguments are either null pointers or
pointers to null-terminated strings. One or both of these two
arguments must be a non-null pointer.

So... never mind!

@njsmith njsmith closed this as completed Aug 15, 2017
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life extension-modules C modules in the Modules dir
Projects
None yet
Development

No branches or pull requests

1 participant