]> Cypherpunks repositories - gostls13.git/commitdiff
net: Make Listen(":port") use IPv6 when IPv4 is not supported.
authorPaul Marks <pmarks@google.com>
Sat, 5 Sep 2015 01:33:35 +0000 (18:33 -0700)
committerPaul Marks <pmarks@google.com>
Sat, 5 Sep 2015 02:31:22 +0000 (02:31 +0000)
When running an experimental kernel with IPv4 disabled, Listen(":port")
currently tries to create an AF_INET socket, and fails.  Instead, it
should see !supportsIPv4, and use an AF_INET6 socket.

This sort of environment is quite esoteric at the moment, but I can
force the tests to fail on regular Linux using the following tweaks:

- net/net.go: supportsIPv4, supportsIPv6, supportsIPv4map = false, true, false
- net/sockopt_linux.go: ipv6only=true
- net/ipsock_posix.go: Revert this fix
- ./make.bash && ../bin/go test net

Also, make the arrows in server_test.go point to the left, because
server<-client is easier to read.

Fixes #12510

Change-Id: I0cc3b6b08d5e6908d2fbf8594f652ba19815aa4b
Reviewed-on: https://go-review.googlesource.com/14334
Run-TryBot: Paul Marks <pmarks@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/ipsock_posix.go
src/net/platform_test.go
src/net/server_test.go

index 83eaf855b4c2d07e125463966f7e792be826e119..4419aaf8a06d70483c69a33aa744bed58ec0e5d6 100644 (file)
@@ -101,10 +101,11 @@ func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
 //
 //     1. A wild-wild listen, "tcp" + ""
 //     If the platform supports both IPv6 and IPv6 IPv4-mapping
-//     capabilities, we assume that the user want to listen on
-//     both IPv4 and IPv6 wildcard address over an AF_INET6
-//     socket with IPV6_V6ONLY=0.  Otherwise we prefer an IPv4
-//     wildcard address listen over an AF_INET socket.
+//     capabilities, or does not support IPv4, we assume that
+//     the user wants to listen on both IPv4 and IPv6 wildcard
+//     addresses over an AF_INET6 socket with IPV6_V6ONLY=0.
+//     Otherwise we prefer an IPv4 wildcard address listen over
+//     an AF_INET socket.
 //
 //     2. A wild-ipv4wild listen, "tcp" + "0.0.0.0"
 //     Same as 1.
@@ -137,7 +138,7 @@ func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family
        }
 
        if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
-               if supportsIPv4map {
+               if supportsIPv4map || !supportsIPv4 {
                        return syscall.AF_INET6, false
                }
                if laddr == nil {
index d6248520f33b6910e0d11603c9d67e3a2b248b0a..c9415d1038c1ea58add93cb8f311b90fbc592817 100644 (file)
@@ -134,7 +134,7 @@ func testableListenArgs(network, address, client string) bool {
 
        // Test functionality of IPv4 communication using AF_INET6
        // sockets.
-       if !supportsIPv4map && (network == "tcp" || network == "udp" || network == "ip") && wildcard {
+       if !supportsIPv4map && supportsIPv4 && (network == "tcp" || network == "udp" || network == "ip") && wildcard {
                // At this point, we prefer IPv4 when ip is nil.
                // See favoriteAddrFamily for further information.
                if ip.To16() != nil && ip.To4() == nil && cip.To4() != nil { // a pair of IPv6 server and IPv4 client
index fe0006b11fb0370b8de9ee10288e7ebb55118aca..2e998e23a8a0aba1bb3f3687d7fc0d9bfa6235db 100644 (file)
@@ -55,7 +55,7 @@ func TestTCPServer(t *testing.T) {
 
        for i, tt := range tcpServerTests {
                if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
-                       t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"->"+tt.taddr)
+                       t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"<-"+tt.taddr)
                        continue
                }
 
@@ -251,7 +251,7 @@ var udpServerTests = []struct {
 func TestUDPServer(t *testing.T) {
        for i, tt := range udpServerTests {
                if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
-                       t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"->"+tt.taddr)
+                       t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"<-"+tt.taddr)
                        continue
                }
 
@@ -329,7 +329,7 @@ var unixgramServerTests = []struct {
 func TestUnixgramServer(t *testing.T) {
        for i, tt := range unixgramServerTests {
                if !testableListenArgs("unixgram", tt.saddr, "") {
-                       t.Logf("skipping %s test", "unixgram "+tt.saddr+"->"+tt.caddr)
+                       t.Logf("skipping %s test", "unixgram "+tt.saddr+"<-"+tt.caddr)
                        continue
                }