2009-11-01 12:15:34 -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.
|
|
|
|
|
|
|
|
// IP sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2011-05-16 15:03:06 -06:00
|
|
|
var supportsIPv6, supportsIPv4map = probeIPv6Stack()
|
|
|
|
|
undo CL 4557058 / b4c2ffae7034
Using the getaddrinfo order is only okay if we
are smart enough to try multiple addresses in Dial.
Since the code does not do that, we must make
the right first choice, regardless of what getaddrinfo
does, and more often that not that means using the
IPv4 address, even on IPv6 systems. With the CL
applied, gotest fails in package net on OS X.
helix.cam=; gotest
...
--- FAIL: net.TestDialGoogleIPv4 (1.05 seconds)
-- 74.125.226.179:80 --
-- www.google.com:80 --
Dial("tcp", "", "www.google.com:80") = _, dial tcp [2001:4860:800f::69]:80: address family not supported by protocol family
-- 74.125.226.179:http --
-- www.google.com:http --
Dial("tcp", "", "www.google.com:http") = _, dial tcp [2001:4860:800f::69]:80: address family not supported by protocol family
-- 074.125.226.179:0080 --
-- [::ffff:74.125.226.179]:80 --
-- [::ffff:4a7d:e2b3]:80 --
-- [0:0:0:0:0000:ffff:74.125.226.179]:80 --
-- [0:0:0:0:000000:ffff:74.125.226.179]:80 --
-- [0:0:0:0:0:ffff::74.125.226.179]:80 --
FAIL
gotest: "./6.out" failed: exit status 1
««« original CL description
net: name-based destination address selection
getaddrinfo() orders the addresses according to RFC 3484.
This means when IPv6 is working on a host we get results like:
[]string = {"2001:4810::110", "66.117.47.214"}
and when it's not working we get:
[]string = {"66.117.47.214", "2001:4810::110"}
thus can drop firstFavoriteAddr.
This also means /etc/gai.conf works on relevant systems.
R=rsc, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/4557058
»»»
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/4532101
2011-06-01 13:49:57 -06:00
|
|
|
func firstFavoriteAddr(filter func(IP) IP, addrs []string) (addr IP) {
|
2011-11-13 20:57:19 -07:00
|
|
|
if filter == nil {
|
undo CL 4557058 / b4c2ffae7034
Using the getaddrinfo order is only okay if we
are smart enough to try multiple addresses in Dial.
Since the code does not do that, we must make
the right first choice, regardless of what getaddrinfo
does, and more often that not that means using the
IPv4 address, even on IPv6 systems. With the CL
applied, gotest fails in package net on OS X.
helix.cam=; gotest
...
--- FAIL: net.TestDialGoogleIPv4 (1.05 seconds)
-- 74.125.226.179:80 --
-- www.google.com:80 --
Dial("tcp", "", "www.google.com:80") = _, dial tcp [2001:4860:800f::69]:80: address family not supported by protocol family
-- 74.125.226.179:http --
-- www.google.com:http --
Dial("tcp", "", "www.google.com:http") = _, dial tcp [2001:4860:800f::69]:80: address family not supported by protocol family
-- 074.125.226.179:0080 --
-- [::ffff:74.125.226.179]:80 --
-- [::ffff:4a7d:e2b3]:80 --
-- [0:0:0:0:0000:ffff:74.125.226.179]:80 --
-- [0:0:0:0:000000:ffff:74.125.226.179]:80 --
-- [0:0:0:0:0:ffff::74.125.226.179]:80 --
FAIL
gotest: "./6.out" failed: exit status 1
««« original CL description
net: name-based destination address selection
getaddrinfo() orders the addresses according to RFC 3484.
This means when IPv6 is working on a host we get results like:
[]string = {"2001:4810::110", "66.117.47.214"}
and when it's not working we get:
[]string = {"66.117.47.214", "2001:4810::110"}
thus can drop firstFavoriteAddr.
This also means /etc/gai.conf works on relevant systems.
R=rsc, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/4557058
»»»
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/4532101
2011-06-01 13:49:57 -06:00
|
|
|
// We'll take any IP address, but since the dialing code
|
|
|
|
// does not yet try multiple addresses, prefer to use
|
|
|
|
// an IPv4 address if possible. This is especially relevant
|
|
|
|
// if localhost resolves to [ipv6-localhost, ipv4-localhost].
|
|
|
|
// Too much code assumes localhost == ipv4-localhost.
|
|
|
|
addr = firstSupportedAddr(ipv4only, addrs)
|
|
|
|
if addr == nil {
|
|
|
|
addr = firstSupportedAddr(anyaddr, addrs)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addr = firstSupportedAddr(filter, addrs)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-04-20 13:21:59 -06:00
|
|
|
func firstSupportedAddr(filter func(IP) IP, addrs []string) IP {
|
2011-02-16 13:05:48 -07:00
|
|
|
for _, s := range addrs {
|
2011-04-20 13:21:59 -06:00
|
|
|
if addr := filter(ParseIP(s)); addr != nil {
|
|
|
|
return addr
|
2011-02-16 13:05:48 -07:00
|
|
|
}
|
|
|
|
}
|
2011-04-20 13:21:59 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-05-16 15:03:06 -06:00
|
|
|
func anyaddr(x IP) IP {
|
|
|
|
if x4 := x.To4(); x4 != nil {
|
|
|
|
return x4
|
|
|
|
}
|
|
|
|
if supportsIPv6 {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-04-20 13:21:59 -06:00
|
|
|
func ipv4only(x IP) IP { return x.To4() }
|
|
|
|
|
|
|
|
func ipv6only(x IP) IP {
|
|
|
|
// Only return addresses that we can use
|
|
|
|
// with the kernel's IPv6 addressing modes.
|
2011-05-16 15:03:06 -06:00
|
|
|
if len(x) == IPv6len && x.To4() == nil && supportsIPv6 {
|
|
|
|
return x
|
2011-04-20 13:21:59 -06:00
|
|
|
}
|
2011-05-16 15:03:06 -06:00
|
|
|
return nil
|
2011-02-16 13:05:48 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
type InvalidAddrError string
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (e InvalidAddrError) Error() string { return string(e) }
|
2010-04-26 23:15:25 -06:00
|
|
|
func (e InvalidAddrError) Timeout() bool { return false }
|
|
|
|
func (e InvalidAddrError) Temporary() bool { return false }
|
|
|
|
|
2011-03-28 21:28:42 -06:00
|
|
|
// SplitHostPort splits a network address of the form
|
|
|
|
// "host:port" or "[host]:port" into host and port.
|
|
|
|
// The latter form must be used when host contains a colon.
|
2011-11-01 20:05:34 -06:00
|
|
|
func SplitHostPort(hostport string) (host, port string, err error) {
|
2009-11-01 12:15:34 -07:00
|
|
|
// The port starts after the last colon.
|
2009-12-15 16:35:38 -07:00
|
|
|
i := last(hostport, ':')
|
2009-11-01 12:15:34 -07:00
|
|
|
if i < 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = &AddrError{"missing port in address", hostport}
|
|
|
|
return
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
host, port = hostport[0:i], hostport[i+1:]
|
2009-11-01 12:15:34 -07:00
|
|
|
|
|
|
|
// Can put brackets around host ...
|
|
|
|
if len(host) > 0 && host[0] == '[' && host[len(host)-1] == ']' {
|
2009-11-09 13:07:39 -07:00
|
|
|
host = host[1 : len(host)-1]
|
2009-11-01 12:15:34 -07:00
|
|
|
} else {
|
|
|
|
// ... but if there are no brackets, no colons.
|
|
|
|
if byteIndex(host, ':') >= 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = &AddrError{"too many colons in address", hostport}
|
|
|
|
return
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2011-03-28 21:28:42 -06:00
|
|
|
// JoinHostPort combines host and port into a network address
|
|
|
|
// of the form "host:port" or, if host contains a colon, "[host]:port".
|
|
|
|
func JoinHostPort(host, port string) string {
|
2009-11-01 12:15:34 -07:00
|
|
|
// If host has colons, have to bracket it.
|
|
|
|
if byteIndex(host, ':') >= 0 {
|
2009-11-09 22:23:52 -07:00
|
|
|
return "[" + host + "]:" + port
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return host + ":" + port
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert "host:port" into IP address and port.
|
2011-11-01 20:05:34 -06:00
|
|
|
func hostPortToIP(net, hostport string) (ip IP, iport int, err error) {
|
2011-06-17 04:07:13 -06:00
|
|
|
var (
|
|
|
|
addr IP
|
|
|
|
p, i int
|
|
|
|
ok bool
|
|
|
|
)
|
2011-03-28 21:28:42 -06:00
|
|
|
host, port, err := SplitHostPort(hostport)
|
2009-11-01 12:15:34 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
if host != "" {
|
|
|
|
// Try as an IP address.
|
2009-12-15 16:35:38 -07:00
|
|
|
addr = ParseIP(host)
|
2009-11-01 12:15:34 -07:00
|
|
|
if addr == nil {
|
2011-11-13 20:57:19 -07:00
|
|
|
var filter func(IP) IP
|
2011-05-16 15:03:06 -06:00
|
|
|
if net != "" && net[len(net)-1] == '4' {
|
2011-04-20 13:21:59 -06:00
|
|
|
filter = ipv4only
|
2011-05-16 15:03:06 -06:00
|
|
|
}
|
|
|
|
if net != "" && net[len(net)-1] == '6' {
|
2011-04-20 13:21:59 -06:00
|
|
|
filter = ipv6only
|
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
// Not an IP address. Try as a DNS name.
|
2011-03-28 21:28:42 -06:00
|
|
|
addrs, err1 := LookupHost(host)
|
2009-11-02 19:37:30 -07:00
|
|
|
if err1 != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = err1
|
|
|
|
goto Error
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
undo CL 4557058 / b4c2ffae7034
Using the getaddrinfo order is only okay if we
are smart enough to try multiple addresses in Dial.
Since the code does not do that, we must make
the right first choice, regardless of what getaddrinfo
does, and more often that not that means using the
IPv4 address, even on IPv6 systems. With the CL
applied, gotest fails in package net on OS X.
helix.cam=; gotest
...
--- FAIL: net.TestDialGoogleIPv4 (1.05 seconds)
-- 74.125.226.179:80 --
-- www.google.com:80 --
Dial("tcp", "", "www.google.com:80") = _, dial tcp [2001:4860:800f::69]:80: address family not supported by protocol family
-- 74.125.226.179:http --
-- www.google.com:http --
Dial("tcp", "", "www.google.com:http") = _, dial tcp [2001:4860:800f::69]:80: address family not supported by protocol family
-- 074.125.226.179:0080 --
-- [::ffff:74.125.226.179]:80 --
-- [::ffff:4a7d:e2b3]:80 --
-- [0:0:0:0:0000:ffff:74.125.226.179]:80 --
-- [0:0:0:0:000000:ffff:74.125.226.179]:80 --
-- [0:0:0:0:0:ffff::74.125.226.179]:80 --
FAIL
gotest: "./6.out" failed: exit status 1
««« original CL description
net: name-based destination address selection
getaddrinfo() orders the addresses according to RFC 3484.
This means when IPv6 is working on a host we get results like:
[]string = {"2001:4810::110", "66.117.47.214"}
and when it's not working we get:
[]string = {"66.117.47.214", "2001:4810::110"}
thus can drop firstFavoriteAddr.
This also means /etc/gai.conf works on relevant systems.
R=rsc, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/4557058
»»»
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/4532101
2011-06-01 13:49:57 -06:00
|
|
|
addr = firstFavoriteAddr(filter, addrs)
|
2009-11-02 19:37:30 -07:00
|
|
|
if addr == nil {
|
|
|
|
// should not happen
|
2011-05-16 15:03:06 -06:00
|
|
|
err = &AddrError{"LookupHost returned no suitable address", addrs[0]}
|
2009-12-15 16:35:38 -07:00
|
|
|
goto Error
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-17 04:07:13 -06:00
|
|
|
p, i, ok = dtoi(port, 0)
|
2009-11-01 12:15:34 -07:00
|
|
|
if !ok || i != len(port) {
|
2009-12-15 16:35:38 -07:00
|
|
|
p, err = LookupPort(net, port)
|
2009-11-01 12:15:34 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if p < 0 || p > 0xFFFF {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = &AddrError{"invalid port", port}
|
|
|
|
goto Error
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
return addr, p, nil
|
2009-11-01 12:15:34 -07:00
|
|
|
|
|
|
|
Error:
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil, 0, err
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|