classification
Title: socket.inet_pton raised when pass an IPv6 address like "[::]" to it
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jeffrey.Kintscher, christian.heimes, seahoh
Priority: normal Keywords:

Created on 2020-06-30 14:06 by seahoh, last changed 2020-08-11 09:09 by wyz23x2.

Messages (8)
msg372694 - (view) Author: Wator Sead (seahoh) Date: 2020-06-30 14:06
3.6:

>>> import socket
>>> socket.inet_pton(socket.AF_INET6,'[::]')
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'


3.7 and above:

>>> import socket
>>> socket.inet_pton(socket.AF_INET6,'[::]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: illegal IP address string passed to inet_pton


Both:

>>> import socket
>>> addr = '[::1]', 888
>>> ls = socket.socket(socket.AF_INET6)
>>> cs = socket.socket(socket.AF_INET6)
>>> ls.bind(addr)  # no raise
>>> ls.listen(1)
>>> cs.connect(addr)  # no raise
msg372723 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2020-06-30 21:21
I can reproduce the problem with 3.6 and even Python versions as old as 3.4:

Python 3.4.10rc1+ (default, Jun 30 2020, 23:15:25) 
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.inet_pton(socket.AF_INET6,'[::]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: illegal IP address string passed to inet_pton

Python is just mapping inet_pton(3) return value 0 to an exception. It might be possible that your libc has changed behavior.
msg372725 - (view) Author: Wator Sead (seahoh) Date: 2020-07-01 00:35
I did not build it, just download the Windows pre-built releases.

Can make a consistent behave via constraints it? socket.socket accepted it, but socket.inet_pton did not.
msg372732 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2020-07-01 06:12
In your example you are binding to [::1]. That's suppose to work. Binding or connecting to [::] does not work for me on Linux:


>>> addr = '[::]', 8888
>>> s = socket.socket(socket.AF_INET6)
>>> s.bind(addr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known
>>> s.connect(addr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known
msg372750 - (view) Author: Wator Sead (seahoh) Date: 2020-07-01 12:18
[::] can be bound, but the resoult is [::1], you must use this address to connect. Excuse me, are you a developer of the Python?

>>> import socket
>>> ls = socket.socket(socket.AF_INET6)
>>> cs = socket.socket(socket.AF_INET6)
>>> ls.bind(('[::]', 888))  # no raise
>>> ls.listen(1)
>>> cs.connect(('[::1]', 888))  # no raise
msg372751 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2020-07-01 12:29
The behavior of sockets depends on platform and implementation details of OS and libc. Binding to ('[::]', 888) does not work for me on Linux. It might work on other problems. The majority of functions and methods in the socket module are thin wrappers around low-level OS features. 

>>> import socket
>>> import sys
>>> sys.version
'3.8.3 (default, May 29 2020, 00:00:00) \n[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)]'
>>> sys.platform
'linux'
>>> s = socket.socket(socket.AF_INET6)
>>> s.bind(('[::]', 888))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known
msg372753 - (view) Author: Wator Sead (seahoh) Date: 2020-07-01 12:56
Can you try bind "::".

My ask is "Can make a consistent behave via constraints it?".
msg372897 - (view) Author: Wator Sead (seahoh) Date: 2020-07-03 00:26
The point is square brackets, not the address, they (socket.inet_pton and socket.socket) behave different. Can make it not be accepted, any conditions?
History
Date User Action Args
2020-08-11 09:09:06wyz23x2setversions: + Python 3.7, Python 3.8, Python 3.9
2020-08-07 05:48:10Jeffrey.Kintschersetnosy: + Jeffrey.Kintscher
2020-07-03 00:26:09seahohsetmessages: + msg372897
versions: - Python 3.7, Python 3.8
2020-07-01 12:56:11seahohsetmessages: + msg372753
2020-07-01 12:29:58christian.heimessetmessages: + msg372751
2020-07-01 12:18:25seahohsetmessages: + msg372750
2020-07-01 06:12:33christian.heimessetmessages: + msg372732
2020-07-01 00:35:17seahohsetmessages: + msg372725
2020-06-30 21:21:06christian.heimessetnosy: + christian.heimes
messages: + msg372723
2020-06-30 14:06:11seahohcreate