classification
Title: SMTPServer of smptd does not support binding to an IPv6 address
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: giampaolo.rodola, hynek, jcea, jesstess, python-dev, r.david.murray, vsergeev, zvyn
Priority: normal Keywords: patch

Created on 2012-05-08 21:15 by vsergeev, last changed 2014-06-11 20:36 by r.david.murray. This issue is now closed.

Files
File name Uploaded Description Edit
smtpd.patch zvyn, 2014-03-05 16:58 review
smtpd_060914.patch zvyn, 2014-06-09 13:51 review
smtpd_061014.patch zvyn, 2014-06-10 16:32 review
Messages (17)
msg160226 - (view) Author: Ivan Sergeev (vsergeev) Date: 2012-05-08 21:15
The SMTPServer class of the smtpd module creates a server socket with the IPv4 socket.AF_INET address family hardcoded, and this prevents it from later binding to an IPv6 local address.

This occurs on line 282 of smtpd.py for the Python 2.7 branch:
http://hg.python.org/cpython/file/5319a4bf72e7/Lib/smtpd.py#l282

And on line 435 of smtpd for the Python 3.2 branch ( Lib/smtpd.py:435 ):
http://hg.python.org/cpython/file/d937b527b76e/Lib/smtpd.py#l435

One IPv4/IPv6 agnostic solution is to look up provided local address with getaddrinfo(), and use one of the result's address family, socket type and address tuple for create_socket() and bind() at those lines:

...
try:
    gai_results = socket.getaddrinfo(localaddr[0], localaddr[1])
    self.create_socket(gai_results[0][0], gai_results[0][1])
    # try to re-use a server port if possible
    self.set_reuse_addr()
    self.bind(gai_results[0][4])
    self.listen(5)
...
msg160233 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2012-05-08 23:47
Agreed. The only problem I see is that unit tests rely on a mock socket object and should be rewritten by using an actual socket.
msg160235 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-05-08 23:58
I don't think it is necessary to rewrite the existing tests, just add some that test the socket functionality.
msg160285 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2012-05-09 12:21
Unfortunately unit tests overwrite the original smtpd.socket module object with test.mock_socket [1] and the latter one doesn't expose socket.getaddrinfo().

[1] http://hg.python.org/cpython/file/d937b527b76e/Lib/test/test_smtpd.py#l54
msg160290 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-05-09 13:44
Well, that should be fixed anyway (a cleanup added that restores the original value).  Then a new TestCase can test the socket stuff.
msg212761 - (view) Author: Milan Oberkirch (zvyn) * Date: 2014-03-05 14:15
I was going to work on #3461 where IPv6-tests are missing for smtplib and stumbled over this bug. I would be willing to work on this, since it's quiet clear what needs to be done to me: implement what (vsergeev) suggested and write tests (which includes fixing design flaws in current ones).

It may be a good idea to teach mouckup_socket some IPv6, since it's needed for test_smtpd and test_smtplib, but IMHO that can be done as a extra task / will be easy after doing the above.
msg212764 - (view) Author: Milan Oberkirch (zvyn) * Date: 2014-03-05 16:58
The cleaning up of smtpd.socket was already implemented, so there was nothing to do there.

What I did:
- Write two TestCases to check if the IP version is chosen depending on the host-parameter
- Testing, that everything still works with an IPv6 address by inheriting from SMTPDChannelTest and overriding setUp with an IPv6-Server
msg215953 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-04-11 20:34
I added some review comments.  Since this is a new feature, the patch also needs a 'versionchanged' that indicates that ipv6 support was added.
msg220091 - (view) Author: Milan Oberkirch (zvyn) * Date: 2014-06-09 13:51
I applied your suggestions.
msg220128 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-09 23:23
When I run the modified test suite on a machine regrtest tells me that the test modified the environment, specifically the asyncore.socket_map.  Presumably there is some missing cleanup logic.
msg220172 - (view) Author: Milan Oberkirch (zvyn) * Date: 2014-06-10 16:32
Fixed it.
msg220291 - (view) Author: Roundup Robot (python-dev) Date: 2014-06-11 17:49
New changeset 1efbc86a200a by R David Murray in branch 'default':
#14758: add IPv6 support to smtpd.
http://hg.python.org/cpython/rev/1efbc86a200a
msg220292 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-11 17:52
Thanks, Milan.  I had to fix a couple things: you had left the "refactored" methods on the SMTPDServerTest, and somehow your new TestFamilyDetection class got indented under SMTPDServerTest in the new version of the patch.  (I also had to update it to compensate for the decode_data patch, which copy-and-pasted the DummyServer calling bugs you fixed in the other tests...)
msg220300 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-11 18:53
Hmm.  Looks like the IPv6 support is making the FreeBSD and and OSX buildbots unhappy :(.
msg220302 - (view) Author: Roundup Robot (python-dev) Date: 2014-06-11 19:18
New changeset d8e0fca7cbe3 by R David Murray in branch 'default':
#14758: Need to specify the desired socket type in the getaddrinfo call.
http://hg.python.org/cpython/rev/d8e0fca7cbe3
msg220305 - (view) Author: Roundup Robot (python-dev) Date: 2014-06-11 20:21
New changeset 9b0d58b0c712 by R David Murray in branch 'default':
#14758: Fix the fix (fix getaddrinfo in mock_socket)
http://hg.python.org/cpython/rev/9b0d58b0c712
msg220308 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-11 20:36
OK, I think this is fixed.
History
Date User Action Args
2014-06-11 20:36:52r.david.murraysetstatus: open -> closed

messages: + msg220308
2014-06-11 20:21:37python-devsetmessages: + msg220305
2014-06-11 19:18:41python-devsetmessages: + msg220302
2014-06-11 18:53:29r.david.murraysetstatus: closed -> open

messages: + msg220300
2014-06-11 17:52:54r.david.murraysetstatus: open -> closed
resolution: fixed
messages: + msg220292

stage: patch review -> resolved
2014-06-11 17:49:13python-devsetnosy: + python-dev
messages: + msg220291
2014-06-10 16:32:59zvynsetfiles: + smtpd_061014.patch

messages: + msg220172
2014-06-09 23:23:00r.david.murraysetmessages: + msg220128
2014-06-09 13:53:14zvynsetnosy: + jesstess
2014-06-09 13:51:19zvynsetfiles: + smtpd_060914.patch

messages: + msg220091
2014-04-11 20:34:08r.david.murraysetstage: needs patch -> patch review
messages: + msg215953
versions: + Python 3.5, - Python 3.3
2014-03-05 16:58:48zvynsetfiles: + smtpd.patch
keywords: + patch
messages: + msg212764
2014-03-05 14:15:22zvynsetnosy: + zvyn
messages: + msg212761
2012-05-09 13:44:19r.david.murraysetmessages: + msg160290
2012-05-09 13:12:02jceasetnosy: + jcea
2012-05-09 12:21:05giampaolo.rodolasetmessages: + msg160285
2012-05-09 08:18:26hyneksetnosy: + hynek
2012-05-08 23:58:35r.david.murraysetmessages: + msg160235
2012-05-08 23:47:33giampaolo.rodolasetmessages: + msg160233
2012-05-08 22:30:09pitrousetnosy: + giampaolo.rodola, r.david.murray
stage: needs patch
type: behavior -> enhancement

versions: - Python 2.6, Python 3.1, Python 2.7, Python 3.2, Python 3.4
2012-05-08 21:15:49vsergeevcreate