2008-12-18 16:42:39 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2011-01-26 10:38:06 -07:00
|
|
|
"fmt"
|
2016-04-13 21:17:44 -06:00
|
|
|
"internal/testenv"
|
2009-12-15 16:35:38 -07:00
|
|
|
"io"
|
2011-01-26 10:38:06 -07:00
|
|
|
"strings"
|
2009-12-15 16:35:38 -07:00
|
|
|
"testing"
|
2008-12-18 16:42:39 -07:00
|
|
|
)
|
|
|
|
|
2013-08-30 19:28:49 -06:00
|
|
|
func TestResolveGoogle(t *testing.T) {
|
2016-04-13 21:17:44 -06:00
|
|
|
testenv.MustHaveExternalNetwork(t)
|
|
|
|
|
2017-04-14 02:43:42 -06:00
|
|
|
if !supportsIPv4() || !supportsIPv6() || !*testIPv4 || !*testIPv6 {
|
2015-05-12 21:44:45 -06:00
|
|
|
t.Skip("both IPv4 and IPv6 are required")
|
2013-08-30 19:28:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, network := range []string{"tcp", "tcp4", "tcp6"} {
|
|
|
|
addr, err := ResolveTCPAddr(network, "www.google.com:http")
|
|
|
|
if err != nil {
|
2015-05-12 21:44:45 -06:00
|
|
|
t.Error(err)
|
2013-08-30 19:28:49 -06:00
|
|
|
continue
|
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
switch {
|
|
|
|
case network == "tcp" && addr.IP.To4() == nil:
|
|
|
|
fallthrough
|
|
|
|
case network == "tcp4" && addr.IP.To4() == nil:
|
2015-05-12 21:44:45 -06:00
|
|
|
t.Errorf("got %v; want an IPv4 address on %s", addr, network)
|
2015-04-01 07:18:33 -06:00
|
|
|
case network == "tcp6" && (addr.IP.To16() == nil || addr.IP.To4() != nil):
|
2015-05-12 21:44:45 -06:00
|
|
|
t.Errorf("got %v; want an IPv6 address on %s", addr, network)
|
2013-08-30 19:28:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 07:18:33 -06:00
|
|
|
var dialGoogleTests = []struct {
|
|
|
|
dial func(string, string) (Conn, error)
|
|
|
|
unreachableNetwork string
|
|
|
|
networks []string
|
|
|
|
addrs []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
dial: (&Dialer{DualStack: true}).Dial,
|
|
|
|
networks: []string{"tcp", "tcp4", "tcp6"},
|
|
|
|
addrs: []string{"www.google.com:http"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dial: Dial,
|
|
|
|
unreachableNetwork: "tcp6",
|
|
|
|
networks: []string{"tcp", "tcp4"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dial: Dial,
|
|
|
|
unreachableNetwork: "tcp4",
|
|
|
|
networks: []string{"tcp", "tcp6"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
net: implement TCP connection setup with fast failover
This CL adds minimal support of Happy Eyeballs-like TCP connection
setup to Dialer API. Happy Eyeballs and derivation techniques are
described in the following:
- Happy Eyeballs: Success with Dual-Stack Hosts
http://tools.ietf.org/html/rfc6555
- Analysing Dual Stack Behaviour and IPv6 Quality
http://www.potaroo.net/presentations/2012-04-17-dual-stack-quality.pdf
Usually, the techniques consist of three components below.
- DNS query racers, that run A and AAAA queries in parallel or series
- A short list of destination addresses
- TCP SYN racers, that run IPv4 and IPv6 transport in parallel or series
This CL implements only the latter two. The existing DNS query
component gathers together A and AAAA records in series, so we don't
touch it here. This CL just uses extended resolveInternetAddr and makes
it possible to run multiple Dial racers in parallel.
For example, when the given destination is a DNS name and the name has
multiple address family A and AAAA records, and it happens on the TCP
wildcard network "tcp" with DualStack=true like the following:
(&net.Dialer{DualStack: true}).Dial("tcp", "www.example.com:80")
The function will return a first established connection either TCP over
IPv4 or TCP over IPv6, and close the other connection internally.
Fixes #3610.
Fixes #5267.
Benchmark results on freebsd/amd64 virtual machine, tip vs. tip+12416043:
benchmark old ns/op new ns/op delta
BenchmarkTCP4OneShot 50696 52141 +2.85%
BenchmarkTCP4OneShotTimeout 65775 66426 +0.99%
BenchmarkTCP4Persistent 10986 10457 -4.82%
BenchmarkTCP4PersistentTimeout 11207 10445 -6.80%
BenchmarkTCP6OneShot 62009 63718 +2.76%
BenchmarkTCP6OneShotTimeout 78351 79138 +1.00%
BenchmarkTCP6Persistent 14695 14659 -0.24%
BenchmarkTCP6PersistentTimeout 15032 14646 -2.57%
BenchmarkTCP4ConcurrentReadWrite 7215 6217 -13.83%
BenchmarkTCP6ConcurrentReadWrite 7528 7493 -0.46%
benchmark old allocs new allocs delta
BenchmarkTCP4OneShot 36 36 0.00%
BenchmarkTCP4OneShotTimeout 36 36 0.00%
BenchmarkTCP4Persistent 0 0 n/a%
BenchmarkTCP4PersistentTimeout 0 0 n/a%
BenchmarkTCP6OneShot 37 37 0.00%
BenchmarkTCP6OneShotTimeout 37 37 0.00%
BenchmarkTCP6Persistent 0 0 n/a%
BenchmarkTCP6PersistentTimeout 0 0 n/a%
BenchmarkTCP4ConcurrentReadWrite 0 0 n/a%
BenchmarkTCP6ConcurrentReadWrite 0 0 n/a%
benchmark old bytes new bytes delta
BenchmarkTCP4OneShot 2500 2503 0.12%
BenchmarkTCP4OneShotTimeout 2508 2505 -0.12%
BenchmarkTCP4Persistent 0 0 n/a%
BenchmarkTCP4PersistentTimeout 0 0 n/a%
BenchmarkTCP6OneShot 2713 2707 -0.22%
BenchmarkTCP6OneShotTimeout 2722 2720 -0.07%
BenchmarkTCP6Persistent 0 0 n/a%
BenchmarkTCP6PersistentTimeout 0 0 n/a%
BenchmarkTCP4ConcurrentReadWrite 0 0 n/a%
BenchmarkTCP6ConcurrentReadWrite 0 0 n/a%
R=golang-dev, bradfitz, nightlyone, rsc
CC=golang-dev
https://golang.org/cl/12416043
2013-09-11 08:48:53 -06:00
|
|
|
func TestDialGoogle(t *testing.T) {
|
2016-04-13 21:17:44 -06:00
|
|
|
testenv.MustHaveExternalNetwork(t)
|
|
|
|
|
2017-04-14 02:43:42 -06:00
|
|
|
if !supportsIPv4() || !supportsIPv6() || !*testIPv4 || !*testIPv6 {
|
2015-05-12 21:44:45 -06:00
|
|
|
t.Skip("both IPv4 and IPv6 are required")
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2015-04-01 07:18:33 -06:00
|
|
|
var err error
|
|
|
|
dialGoogleTests[1].addrs, dialGoogleTests[2].addrs, err = googleLiteralAddrs()
|
2008-12-18 16:42:39 -07:00
|
|
|
if err != nil {
|
2015-04-01 07:18:33 -06:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
for _, tt := range dialGoogleTests {
|
|
|
|
for _, network := range tt.networks {
|
|
|
|
disableSocketConnect(tt.unreachableNetwork)
|
|
|
|
for _, addr := range tt.addrs {
|
|
|
|
if err := fetchGoogle(tt.dial, network, addr); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enableSocketConnect()
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 08:49:42 -06:00
|
|
|
}
|
|
|
|
|
2015-04-01 07:18:33 -06:00
|
|
|
var (
|
|
|
|
literalAddrs4 = [...]string{
|
|
|
|
"%d.%d.%d.%d:80",
|
|
|
|
"www.google.com:80",
|
|
|
|
"%d.%d.%d.%d:http",
|
|
|
|
"www.google.com:http",
|
|
|
|
"%03d.%03d.%03d.%03d:0080",
|
|
|
|
"[::ffff:%d.%d.%d.%d]:80",
|
|
|
|
"[::ffff:%02x%02x:%02x%02x]:80",
|
|
|
|
"[0:0:0:0:0000:ffff:%d.%d.%d.%d]:80",
|
|
|
|
"[0:0:0:0:000000:ffff:%d.%d.%d.%d]:80",
|
|
|
|
"[0:0:0:0::ffff:%d.%d.%d.%d]:80",
|
|
|
|
}
|
|
|
|
literalAddrs6 = [...]string{
|
|
|
|
"[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:80",
|
|
|
|
"ipv6.google.com:80",
|
|
|
|
"[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:http",
|
|
|
|
"ipv6.google.com:http",
|
2011-04-15 06:20:42 -06:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
)
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2015-04-01 07:18:33 -06:00
|
|
|
func googleLiteralAddrs() (lits4, lits6 []string, err error) {
|
|
|
|
ips, err := LookupIP("www.google.com")
|
2011-01-26 10:38:06 -07:00
|
|
|
if err != nil {
|
2015-04-01 07:18:33 -06:00
|
|
|
return nil, nil, err
|
2011-01-26 10:38:06 -07:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
if len(ips) == 0 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
var ip4, ip6 IP
|
|
|
|
for _, ip := range ips {
|
|
|
|
if ip4 == nil && ip.To4() != nil {
|
|
|
|
ip4 = ip.To4()
|
|
|
|
}
|
|
|
|
if ip6 == nil && ip.To16() != nil && ip.To4() == nil {
|
|
|
|
ip6 = ip.To16()
|
|
|
|
}
|
|
|
|
if ip4 != nil && ip6 != nil {
|
2011-04-20 13:21:59 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
if ip4 != nil {
|
|
|
|
for i, lit4 := range literalAddrs4 {
|
|
|
|
if strings.Contains(lit4, "%") {
|
|
|
|
literalAddrs4[i] = fmt.Sprintf(lit4, ip4[0], ip4[1], ip4[2], ip4[3])
|
|
|
|
}
|
2011-01-26 10:38:06 -07:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
lits4 = literalAddrs4[:]
|
2011-01-26 10:38:06 -07:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
if ip6 != nil {
|
|
|
|
for i, lit6 := range literalAddrs6 {
|
|
|
|
if strings.Contains(lit6, "%") {
|
|
|
|
literalAddrs6[i] = fmt.Sprintf(lit6, ip6[0], ip6[1], ip6[2], ip6[3], ip6[4], ip6[5], ip6[6], ip6[7], ip6[8], ip6[9], ip6[10], ip6[11], ip6[12], ip6[13], ip6[14], ip6[15])
|
2009-05-07 11:31:48 -06:00
|
|
|
}
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
lits6 = literalAddrs6[:]
|
2011-05-02 08:49:42 -06:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
return
|
2011-05-02 08:49:42 -06:00
|
|
|
}
|
|
|
|
|
2015-04-01 07:18:33 -06:00
|
|
|
func fetchGoogle(dial func(string, string) (Conn, error), network, address string) error {
|
|
|
|
c, err := dial(network, address)
|
2011-05-02 08:49:42 -06:00
|
|
|
if err != nil {
|
2015-04-01 07:18:33 -06:00
|
|
|
return err
|
2011-05-02 08:49:42 -06:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
defer c.Close()
|
|
|
|
req := []byte("GET /robots.txt HTTP/1.0\r\nHost: www.google.com\r\n\r\n")
|
|
|
|
if _, err := c.Write(req); err != nil {
|
|
|
|
return err
|
2011-05-02 08:49:42 -06:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
b := make([]byte, 1000)
|
|
|
|
n, err := io.ReadFull(c, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2011-05-02 08:49:42 -06:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
if n < 1000 {
|
|
|
|
return fmt.Errorf("short read from %s:%s->%s", network, c.RemoteAddr(), c.LocalAddr())
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2015-04-01 07:18:33 -06:00
|
|
|
return nil
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|