Title: socket.getsockname() should return correct addresses when binding on ipaddr_any
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: amak, irmen, santa4nt, zyasoft
Priority: Keywords: patch

Created on 2013-08-26.15:50:22 by irmen, last changed 2014-06-25.16:04:09 by zyasoft.

File name Uploaded Description Edit Remove
issue2078.patch santa4nt, 2014-05-22.00:37:35 AF_INET6 socket address should be formed in 4-tuple.
issue2078.patch santa4nt, 2014-05-22.01:14:17 Force Inet4Address when dealing if "any" (local) address.
msg8090 (view) Author: Irmen de Jong (irmen) Date: 2013-08-26.15:50:22
When creating an ipv4 socket and binding it on ipaddr_any, getsockname returns an ipv6 address:

>>> s=socket(AF_INET, SOCK_STREAM)
>>> s.bind(("",0))
>>> s.listen(1)
>>> s.getsockname()
(u'0:0:0:0:0:0:0:0', 58526)

Expected behavior: return the ipv4 address "" instead.
msg8105 (view) Author: Alan Kennedy (amak) Date: 2013-09-07.13:34:06
Hi Irmen.

I am not able to reproduce this on my local setup.

What version of jython is this happening on?
What java version?
What operating system?
msg8109 (view) Author: Irmen de Jong (irmen) Date: 2013-09-08.13:30:52
Happens with jython trunk (2.7b1+).
Happens on Windows 7 (java 1.7) and OS X 10.8.4 (java 1.6).
Both machines have a functional IPV6 (and IPV4) network address assigned.
msg8472 (view) Author: Jim Baker (zyasoft) Date: 2014-05-21.20:55:21
Same result with the  new socket-reboot code in beta 3, but this is not surprising since this part is unchanged (the internal function _socket._get_jsockaddr)

Target beta 4
msg8479 (view) Author: Jim Baker (zyasoft) Date: 2014-05-21.21:29:10
Also need to fix the nearly identical issue (from an implementation perspective) as reported in #2079:

from socket import *
assert len(s.getsockname()) == 4
msg8521 (view) Author: Santoso Wijaya (santa4nt) Date: 2014-05-22.00:35:58
Netty4 is strange... you can bind its ServerBootstrap with an Inet4Address, but when you query the Channel it returns for its local address, you get an Inet6Address!

Here's a print-peppered _realsocket.listen() method:

def listen(self, backlog):
    # ...
    print 'Binding to {:s} with type {:s}'.format(repr(self.bind_addr.getAddress()), type(self.bind_addr.getAddress()))
    future = b.bind(self.bind_addr.getAddress(), self.bind_addr.getPort())

    # ... =
    localaddr =
    print 'The channel is bound to {:s} with type {:s}'.format(repr(localaddr.getAddress()), type(localaddr.getAddress()))

    # ...

And what you get when you execute that modified method:

Binding to / with type <type ''>
The channel is bound to /0:0:0:0:0:0:0:0 with type <type ''>

And that's why you get an IPv6 '0' address from getsockname() after binding it with IPv4 '0' address.
msg8522 (view) Author: Santoso Wijaya (santa4nt) Date: 2014-05-22.00:37:34
As for @zyasoft test case outputting the correct number of tuple elements for AF_INET6 socket address, here's a simple patch to address just that.

The original issue still remains since I don't know how to work around that Netty 4 quirk yet.
msg8535 (view) Author: Santoso Wijaya (santa4nt) Date: 2014-05-22.01:14:17
How about this hack-ish workaround?
msg8677 (view) Author: Santoso Wijaya (santa4nt) Date: 2014-06-18.18:25:17
Merged in this commit:
Date User Action Args
2014-06-25 16:04:09zyasoftsetstatus: pending -> closed
2014-06-18 19:28:01zyasoftsetstatus: open -> pending
resolution: accepted -> fixed
2014-06-18 18:25:17santa4ntsetmessages: + msg8677
2014-05-22 01:14:17santa4ntsetfiles: + issue2078.patch
messages: + msg8535
2014-05-22 00:37:35santa4ntsetfiles: + issue2078.patch
keywords: + patch
messages: + msg8522
2014-05-22 00:35:59santa4ntsetmessages: + msg8521
2014-05-21 22:51:23santa4ntsettype: behaviour
2014-05-21 22:51:19santa4ntsetnosy: + santa4nt
2014-05-21 21:30:01zyasoftsettitle: socket.getsockname() returns ipv6 name for ipv4 family when binding on ipaddr_any -> socket.getsockname() should return correct addresses when binding on ipaddr_any
2014-05-21 21:29:10zyasoftsetmessages: + msg8479
2014-05-21 20:55:21zyasoftsetassignee: amak -> zyasoft
resolution: accepted
messages: + msg8472
nosy: + zyasoft
2013-09-08 13:30:52irmensetmessages: + msg8109
2013-09-07 13:34:07amaksetassignee: amak
messages: + msg8105
nosy: + amak
2013-08-26 16:01:06irmensetcomponents: + Core
versions: + Jython 2.7
2013-08-26 15:50:22irmencreate