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.
|
|
|
|
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
// Internet protocol family sockets
|
2009-11-01 12:15:34 -07:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
2012-11-08 09:35:16 -07:00
|
|
|
import "time"
|
2012-11-06 22:58:20 -07:00
|
|
|
|
2012-11-08 10:09:09 -07:00
|
|
|
var supportsIPv6, supportsIPv4map bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sysInit()
|
|
|
|
supportsIPv6, supportsIPv4map = probeIPv6Stack()
|
|
|
|
}
|
2011-05-16 15:03:06 -06: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
|
|
|
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) {
|
2012-11-27 14:36:05 -07:00
|
|
|
host, port, _, err = splitHostPort(hostport)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitHostPort(hostport string) (host, port, zone 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
|
|
|
}
|
2012-11-27 14:36:05 -07:00
|
|
|
host, port = hostport[: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
|
|
|
}
|
|
|
|
|
2012-11-27 14:36:05 -07:00
|
|
|
func resolveInternetAddr(net, addr string, deadline time.Time) (Addr, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
host, port, zone string
|
|
|
|
portnum int
|
|
|
|
)
|
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
|
|
|
|
if addr != "" {
|
|
|
|
if host, port, zone, err = splitHostPort(addr); err != nil {
|
|
|
|
return nil, err
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2012-11-27 14:36:05 -07:00
|
|
|
if portnum, err = parsePort(net, port); err != nil {
|
|
|
|
return nil, err
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2012-11-27 14:36:05 -07:00
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
if addr != "" {
|
|
|
|
host = addr
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2012-11-27 14:36:05 -07:00
|
|
|
inetaddr := func(net string, ip IP, port int, zone string) Addr {
|
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
return &TCPAddr{IP: ip, Port: port, Zone: zone}
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
return &UDPAddr{IP: ip, Port: port, Zone: zone}
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
return &IPAddr{IP: ip, Zone: zone}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if host == "" {
|
|
|
|
return inetaddr(net, nil, portnum, zone), nil
|
|
|
|
}
|
|
|
|
// Try as an IP address.
|
|
|
|
if ip := ParseIP(host); ip != nil {
|
|
|
|
return inetaddr(net, ip, portnum, zone), nil
|
|
|
|
}
|
|
|
|
var filter func(IP) IP
|
|
|
|
if net != "" && net[len(net)-1] == '4' {
|
|
|
|
filter = ipv4only
|
|
|
|
}
|
|
|
|
if net != "" && net[len(net)-1] == '6' {
|
|
|
|
filter = ipv6only
|
|
|
|
}
|
|
|
|
// Try as a DNS name.
|
|
|
|
addrs, err := lookupHostDeadline(host, deadline)
|
2012-05-28 15:12:06 -06:00
|
|
|
if err != nil {
|
2012-11-27 14:36:05 -07:00
|
|
|
return nil, err
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2012-11-27 14:36:05 -07:00
|
|
|
ip := firstFavoriteAddr(filter, addrs)
|
|
|
|
if ip == nil {
|
|
|
|
// should not happen
|
|
|
|
return nil, &AddrError{"LookupHost returned no suitable address", addrs[0]}
|
|
|
|
}
|
|
|
|
return inetaddr(net, ip, portnum, zone), nil
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
|
|
|
|
func zoneToString(zone int) string {
|
|
|
|
if zone == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if ifi, err := InterfaceByIndex(zone); err == nil {
|
|
|
|
return ifi.Name
|
|
|
|
}
|
|
|
|
return itod(uint(zone))
|
|
|
|
}
|
|
|
|
|
|
|
|
func zoneToInt(zone string) int {
|
|
|
|
if zone == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if ifi, err := InterfaceByName(zone); err == nil {
|
|
|
|
return ifi.Index
|
|
|
|
}
|
|
|
|
n, _, _ := dtoi(zone, 0)
|
|
|
|
return n
|
|
|
|
}
|