2016-07-22 20:15:38 +02:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2010-12-03 05:34:57 +01:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
import (
|
2016-07-22 20:15:38 +02:00
|
|
|
"context"
|
|
|
|
"internal/nettrace"
|
2018-09-24 23:46:21 +02:00
|
|
|
"syscall"
|
2012-01-13 06:11:45 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-09-06 20:12:46 +02:00
|
|
|
// defaultTCPKeepAlive is a default constant value for TCPKeepAlive times
|
|
|
|
// See golang.org/issue/31510
|
|
|
|
const (
|
|
|
|
defaultTCPKeepAlive = 15 * time.Second
|
|
|
|
)
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// A Dialer contains options for connecting to an address.
|
|
|
|
//
|
|
|
|
// The zero value for each field is equivalent to dialing
|
|
|
|
// without that option. Dialing with the zero value of Dialer
|
|
|
|
// is therefore equivalent to just calling the Dial function.
|
2020-01-03 00:05:27 +01:00
|
|
|
//
|
|
|
|
// It is safe to call Dialer's methods concurrently.
|
2013-07-16 08:54:42 +02:00
|
|
|
type Dialer struct {
|
|
|
|
// Timeout is the maximum amount of time a dial will wait for
|
|
|
|
// a connect to complete. If Deadline is also set, it may fail
|
|
|
|
// earlier.
|
|
|
|
//
|
|
|
|
// The default is no timeout.
|
|
|
|
//
|
2017-09-14 19:11:35 +02:00
|
|
|
// When using TCP and dialing a host name with multiple IP
|
|
|
|
// addresses, the timeout may be divided between them.
|
2015-10-31 01:59:47 +01:00
|
|
|
//
|
2013-07-16 08:54:42 +02:00
|
|
|
// With or without a timeout, the operating system may impose
|
|
|
|
// its own earlier timeout. For instance, TCP timeouts are
|
|
|
|
// often around 3 minutes.
|
|
|
|
Timeout time.Duration
|
|
|
|
|
|
|
|
// Deadline is the absolute point in time after which dials
|
|
|
|
// will fail. If Timeout is set, it may fail earlier.
|
|
|
|
// Zero means no deadline, or dependent on the operating system
|
|
|
|
// as with the Timeout option.
|
|
|
|
Deadline time.Time
|
|
|
|
|
|
|
|
// LocalAddr is the local address to use when dialing an
|
|
|
|
// address. The address must be of a compatible type for the
|
|
|
|
// network being dialed.
|
|
|
|
// If nil, a local address is automatically chosen.
|
|
|
|
LocalAddr Addr
|
2013-11-06 20:49:01 +01:00
|
|
|
|
2019-01-18 20:04:36 +01:00
|
|
|
// DualStack previously enabled RFC 6555 Fast Fallback
|
|
|
|
// support, also known as "Happy Eyeballs", in which IPv4 is
|
|
|
|
// tried soon if IPv6 appears to be misconfigured and
|
|
|
|
// hanging.
|
|
|
|
//
|
|
|
|
// Deprecated: Fast Fallback is enabled by default. To
|
|
|
|
// disable, set FallbackDelay to a negative value.
|
2013-11-06 20:49:01 +01:00
|
|
|
DualStack bool
|
2014-07-19 10:53:52 +02:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// FallbackDelay specifies the length of time to wait before
|
2019-01-18 20:04:36 +01:00
|
|
|
// spawning a RFC 6555 Fast Fallback connection. That is, this
|
|
|
|
// is the amount of time to wait for IPv6 to succeed before
|
|
|
|
// assuming that IPv6 is misconfigured and falling back to
|
|
|
|
// IPv4.
|
|
|
|
//
|
2015-10-31 01:59:47 +01:00
|
|
|
// If zero, a default delay of 300ms is used.
|
2019-01-18 20:04:36 +01:00
|
|
|
// A negative value disables Fast Fallback support.
|
2015-10-31 01:59:47 +01:00
|
|
|
FallbackDelay time.Duration
|
|
|
|
|
2019-09-06 20:12:46 +02:00
|
|
|
// KeepAlive specifies the interval between keep-alive
|
|
|
|
// probes for an active network connection.
|
|
|
|
// If zero, keep-alive probes are sent with a default value
|
|
|
|
// (currently 15 seconds), if supported by the protocol and operating
|
|
|
|
// system. Network protocols or operating systems that do
|
|
|
|
// not support keep-alives ignore this field.
|
|
|
|
// If negative, keep-alive probes are disabled.
|
2014-07-19 10:53:52 +02:00
|
|
|
KeepAlive time.Duration
|
2016-02-03 22:58:02 +01:00
|
|
|
|
2017-01-14 01:05:42 +01:00
|
|
|
// Resolver optionally specifies an alternate resolver to use.
|
|
|
|
Resolver *Resolver
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
// Cancel is an optional channel whose closure indicates that
|
|
|
|
// the dial should be canceled. Not all types of dials support
|
2019-09-06 20:12:46 +02:00
|
|
|
// cancellation.
|
2016-07-22 20:15:38 +02:00
|
|
|
//
|
|
|
|
// Deprecated: Use DialContext instead.
|
2016-02-03 22:58:02 +01:00
|
|
|
Cancel <-chan struct{}
|
2018-09-24 23:46:21 +02:00
|
|
|
|
|
|
|
// If Control is not nil, it is called after creating the network
|
|
|
|
// connection but before actually dialing.
|
|
|
|
//
|
|
|
|
// Network and address parameters passed to Control method are not
|
|
|
|
// necessarily the ones passed to Dial. For example, passing "tcp" to Dial
|
|
|
|
// will cause the Control function to be called with "tcp4" or "tcp6".
|
|
|
|
Control func(network, address string, c syscall.RawConn) error
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 20:04:36 +01:00
|
|
|
func (d *Dialer) dualStack() bool { return d.FallbackDelay >= 0 }
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func minNonzeroTime(a, b time.Time) time.Time {
|
|
|
|
if a.IsZero() {
|
|
|
|
return b
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
if b.IsZero() || a.Before(b) {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// deadline returns the earliest of:
|
|
|
|
// - now+Timeout
|
|
|
|
// - d.Deadline
|
|
|
|
// - the context's deadline
|
|
|
|
// Or zero, if none of Timeout, Deadline, or context's deadline is set.
|
|
|
|
func (d *Dialer) deadline(ctx context.Context, now time.Time) (earliest time.Time) {
|
|
|
|
if d.Timeout != 0 { // including negative, for historical reasons
|
|
|
|
earliest = now.Add(d.Timeout)
|
|
|
|
}
|
|
|
|
if d, ok := ctx.Deadline(); ok {
|
|
|
|
earliest = minNonzeroTime(earliest, d)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
return minNonzeroTime(earliest, d.Deadline)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 01:05:42 +01:00
|
|
|
func (d *Dialer) resolver() *Resolver {
|
|
|
|
if d.Resolver != nil {
|
|
|
|
return d.Resolver
|
|
|
|
}
|
|
|
|
return DefaultResolver
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// partialDeadline returns the deadline to use for a single address,
|
|
|
|
// when multiple addresses are pending.
|
|
|
|
func partialDeadline(now, deadline time.Time, addrsRemaining int) (time.Time, error) {
|
|
|
|
if deadline.IsZero() {
|
|
|
|
return deadline, nil
|
|
|
|
}
|
|
|
|
timeRemaining := deadline.Sub(now)
|
|
|
|
if timeRemaining <= 0 {
|
2020-07-28 07:27:54 +02:00
|
|
|
return time.Time{}, errTimeout
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
|
|
|
// Tentatively allocate equal time to each remaining address.
|
|
|
|
timeout := timeRemaining / time.Duration(addrsRemaining)
|
|
|
|
// If the time per address is too short, steal from the end of the list.
|
|
|
|
const saneMinimum = 2 * time.Second
|
|
|
|
if timeout < saneMinimum {
|
|
|
|
if timeRemaining < saneMinimum {
|
|
|
|
timeout = timeRemaining
|
|
|
|
} else {
|
|
|
|
timeout = saneMinimum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return now.Add(timeout), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialer) fallbackDelay() time.Duration {
|
|
|
|
if d.FallbackDelay > 0 {
|
|
|
|
return d.FallbackDelay
|
|
|
|
} else {
|
|
|
|
return 300 * time.Millisecond
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
func parseNetwork(ctx context.Context, network string, needsProto bool) (afnet string, proto int, err error) {
|
|
|
|
i := last(network, ':')
|
2012-02-01 20:26:59 +01:00
|
|
|
if i < 0 { // no colon
|
2017-09-14 19:11:35 +02:00
|
|
|
switch network {
|
2012-02-01 20:26:59 +01:00
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
case "udp", "udp4", "udp6":
|
2012-12-13 00:13:29 +01:00
|
|
|
case "ip", "ip4", "ip6":
|
2017-09-14 19:11:35 +02:00
|
|
|
if needsProto {
|
|
|
|
return "", 0, UnknownNetworkError(network)
|
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
case "unix", "unixgram", "unixpacket":
|
|
|
|
default:
|
2017-09-14 19:11:35 +02:00
|
|
|
return "", 0, UnknownNetworkError(network)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
return network, 0, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
afnet = network[:i]
|
2012-02-01 20:26:59 +01:00
|
|
|
switch afnet {
|
2011-09-16 17:47:21 +02:00
|
|
|
case "ip", "ip4", "ip6":
|
2017-09-14 19:11:35 +02:00
|
|
|
protostr := network[i+1:]
|
2017-01-14 01:05:42 +01:00
|
|
|
proto, i, ok := dtoi(protostr)
|
2012-02-01 20:26:59 +01:00
|
|
|
if !ok || i != len(protostr) {
|
2016-07-22 20:15:38 +02:00
|
|
|
proto, err = lookupProtocol(ctx, protostr)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return afnet, proto, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
return "", 0, UnknownNetworkError(network)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-14 01:05:42 +01:00
|
|
|
// resolveAddrList resolves addr using hint and returns a list of
|
2016-07-22 20:15:38 +02:00
|
|
|
// addresses. The result contains at least one address when error is
|
|
|
|
// nil.
|
2017-01-14 01:05:42 +01:00
|
|
|
func (r *Resolver) resolveAddrList(ctx context.Context, op, network, addr string, hint Addr) (addrList, error) {
|
2017-09-14 19:11:35 +02:00
|
|
|
afnet, _, err := parseNetwork(ctx, network, true)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2013-11-06 20:49:01 +01:00
|
|
|
return nil, err
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
if op == "dial" && addr == "" {
|
2013-11-06 20:49:01 +01:00
|
|
|
return nil, errMissingAddress
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
switch afnet {
|
|
|
|
case "unix", "unixgram", "unixpacket":
|
2015-10-31 01:59:47 +01:00
|
|
|
addr, err := ResolveUnixAddr(afnet, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
if op == "dial" && hint != nil && addr.Network() != hint.Network() {
|
|
|
|
return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()}
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
return addrList{addr}, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2017-01-14 01:05:42 +01:00
|
|
|
addrs, err := r.internetAddrList(ctx, afnet, addr)
|
2016-07-22 20:15:38 +02:00
|
|
|
if err != nil || op != "dial" || hint == nil {
|
|
|
|
return addrs, err
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
tcp *TCPAddr
|
|
|
|
udp *UDPAddr
|
|
|
|
ip *IPAddr
|
|
|
|
wildcard bool
|
|
|
|
)
|
|
|
|
switch hint := hint.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
tcp = hint
|
|
|
|
wildcard = tcp.isWildcard()
|
|
|
|
case *UDPAddr:
|
|
|
|
udp = hint
|
|
|
|
wildcard = udp.isWildcard()
|
|
|
|
case *IPAddr:
|
|
|
|
ip = hint
|
|
|
|
wildcard = ip.isWildcard()
|
|
|
|
}
|
|
|
|
naddrs := addrs[:0]
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if addr.Network() != hint.Network() {
|
|
|
|
return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()}
|
|
|
|
}
|
|
|
|
switch addr := addr.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(tcp.IP) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
naddrs = append(naddrs, addr)
|
|
|
|
case *UDPAddr:
|
|
|
|
if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(udp.IP) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
naddrs = append(naddrs, addr)
|
|
|
|
case *IPAddr:
|
|
|
|
if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(ip.IP) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
naddrs = append(naddrs, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(naddrs) == 0 {
|
2017-01-14 01:05:42 +01:00
|
|
|
return nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: hint.String()}
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
|
|
|
return naddrs, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// Dial connects to the address on the named network.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
|
|
|
|
// "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
|
2013-07-16 08:54:42 +02:00
|
|
|
// (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and
|
|
|
|
// "unixpacket".
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
2017-09-14 19:11:35 +02:00
|
|
|
// For TCP and UDP networks, the address has the form "host:port".
|
|
|
|
// The host must be a literal IP address, or a host name that can be
|
|
|
|
// resolved to IP addresses.
|
|
|
|
// The port must be a literal port number or a service name.
|
|
|
|
// If the host is a literal IPv6 address it must be enclosed in square
|
|
|
|
// brackets, as in "[2001:db8::1]:80" or "[fe80::1%zone]:80".
|
|
|
|
// The zone specifies the scope of the literal IPv6 address as defined
|
|
|
|
// in RFC 4007.
|
|
|
|
// The functions JoinHostPort and SplitHostPort manipulate a pair of
|
|
|
|
// host and port in this form.
|
|
|
|
// When using TCP, and the host resolves to multiple IP addresses,
|
|
|
|
// Dial will try each IP address in order until one succeeds.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// Examples:
|
2016-07-22 20:15:38 +02:00
|
|
|
// Dial("tcp", "golang.org:http")
|
2017-09-14 19:11:35 +02:00
|
|
|
// Dial("tcp", "192.0.2.1:http")
|
|
|
|
// Dial("tcp", "198.51.100.1:80")
|
|
|
|
// Dial("udp", "[2001:db8::1]:domain")
|
|
|
|
// Dial("udp", "[fe80::1%lo0]:53")
|
2016-02-03 22:58:02 +01:00
|
|
|
// Dial("tcp", ":80")
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
2013-07-16 08:54:42 +02:00
|
|
|
// For IP networks, the network must be "ip", "ip4" or "ip6" followed
|
2017-09-14 19:11:35 +02:00
|
|
|
// by a colon and a literal protocol number or a protocol name, and
|
|
|
|
// the address has the form "host". The host must be a literal IP
|
|
|
|
// address or a literal IPv6 address with zone.
|
|
|
|
// It depends on each operating system how the operating system
|
|
|
|
// behaves with a non-well known protocol number such as "0" or "255".
|
2012-02-01 20:26:59 +01:00
|
|
|
//
|
|
|
|
// Examples:
|
2016-07-22 20:15:38 +02:00
|
|
|
// Dial("ip4:1", "192.0.2.1")
|
|
|
|
// Dial("ip6:ipv6-icmp", "2001:db8::1")
|
2017-09-14 19:11:35 +02:00
|
|
|
// Dial("ip6:58", "fe80::1%lo0")
|
2012-02-01 20:26:59 +01:00
|
|
|
//
|
2017-09-14 19:11:35 +02:00
|
|
|
// For TCP, UDP and IP networks, if the host is empty or a literal
|
|
|
|
// unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for
|
|
|
|
// TCP and UDP, "", "0.0.0.0" or "::" for IP, the local system is
|
|
|
|
// assumed.
|
2017-01-14 01:05:42 +01:00
|
|
|
//
|
2017-09-14 19:11:35 +02:00
|
|
|
// For Unix networks, the address must be a file system path.
|
2013-07-16 08:54:42 +02:00
|
|
|
func Dial(network, address string) (Conn, error) {
|
|
|
|
var d Dialer
|
|
|
|
return d.Dial(network, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialTimeout acts like Dial but takes a timeout.
|
2017-09-14 19:11:35 +02:00
|
|
|
//
|
2013-07-16 08:54:42 +02:00
|
|
|
// The timeout includes name resolution, if required.
|
2017-09-14 19:11:35 +02:00
|
|
|
// When using TCP, and the host in the address parameter resolves to
|
|
|
|
// multiple IP addresses, the timeout is spread over each consecutive
|
|
|
|
// dial, such that each is given an appropriate fraction of the time
|
|
|
|
// to connect.
|
|
|
|
//
|
|
|
|
// See func Dial for a description of the network and address
|
|
|
|
// parameters.
|
2013-07-16 08:54:42 +02:00
|
|
|
func DialTimeout(network, address string, timeout time.Duration) (Conn, error) {
|
|
|
|
d := Dialer{Timeout: timeout}
|
|
|
|
return d.Dial(network, address)
|
|
|
|
}
|
|
|
|
|
2018-09-24 23:46:21 +02:00
|
|
|
// sysDialer contains a Dial's parameters and configuration.
|
|
|
|
type sysDialer struct {
|
2015-10-31 01:59:47 +01:00
|
|
|
Dialer
|
|
|
|
network, address string
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// Dial connects to the address on the named network.
|
|
|
|
//
|
|
|
|
// See func Dial for a description of the network and address
|
|
|
|
// parameters.
|
|
|
|
func (d *Dialer) Dial(network, address string) (Conn, error) {
|
2016-07-22 20:15:38 +02:00
|
|
|
return d.DialContext(context.Background(), network, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext connects to the address on the named network using
|
|
|
|
// the provided context.
|
|
|
|
//
|
|
|
|
// The provided Context must be non-nil. If the context expires before
|
|
|
|
// the connection is complete, an error is returned. Once successfully
|
|
|
|
// connected, any expiration of the context will not affect the
|
|
|
|
// connection.
|
|
|
|
//
|
2017-01-14 01:05:42 +01:00
|
|
|
// When using TCP, and the host in the address parameter resolves to multiple
|
|
|
|
// network addresses, any dial timeout (from d.Timeout or ctx) is spread
|
|
|
|
// over each consecutive dial, such that each is given an appropriate
|
|
|
|
// fraction of the time to connect.
|
|
|
|
// For example, if a host has 4 IP addresses and the timeout is 1 minute,
|
|
|
|
// the connect to each single address will be given 15 seconds to complete
|
|
|
|
// before trying the next one.
|
|
|
|
//
|
2016-07-22 20:15:38 +02:00
|
|
|
// See func Dial for a description of the network and address
|
|
|
|
// parameters.
|
|
|
|
func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error) {
|
|
|
|
if ctx == nil {
|
|
|
|
panic("nil context")
|
|
|
|
}
|
|
|
|
deadline := d.deadline(ctx, time.Now())
|
|
|
|
if !deadline.IsZero() {
|
|
|
|
if d, ok := ctx.Deadline(); !ok || deadline.Before(d) {
|
|
|
|
subCtx, cancel := context.WithDeadline(ctx, deadline)
|
|
|
|
defer cancel()
|
|
|
|
ctx = subCtx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if oldCancel := d.Cancel; oldCancel != nil {
|
|
|
|
subCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-oldCancel:
|
|
|
|
cancel()
|
|
|
|
case <-subCtx.Done():
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ctx = subCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shadow the nettrace (if any) during resolve so Connect events don't fire for DNS lookups.
|
|
|
|
resolveCtx := ctx
|
|
|
|
if trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace); trace != nil {
|
|
|
|
shadow := *trace
|
|
|
|
shadow.ConnectStart = nil
|
|
|
|
shadow.ConnectDone = nil
|
|
|
|
resolveCtx = context.WithValue(resolveCtx, nettrace.TraceKey{}, &shadow)
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:05:42 +01:00
|
|
|
addrs, err := d.resolver().resolveAddrList(resolveCtx, "dial", network, address, d.LocalAddr)
|
2013-11-06 20:49:01 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "dial", Net: network, Source: nil, Addr: nil, Err: err}
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
|
2018-09-24 23:46:21 +02:00
|
|
|
sd := &sysDialer{
|
2016-07-22 20:15:38 +02:00
|
|
|
Dialer: *d,
|
|
|
|
network: network,
|
|
|
|
address: address,
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
|
|
|
|
var primaries, fallbacks addrList
|
2019-01-18 20:04:36 +01:00
|
|
|
if d.dualStack() && network == "tcp" {
|
2015-10-31 01:59:47 +01:00
|
|
|
primaries, fallbacks = addrs.partition(isIPv4)
|
|
|
|
} else {
|
|
|
|
primaries = addrs
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
|
|
|
|
var c Conn
|
2016-07-22 20:15:38 +02:00
|
|
|
if len(fallbacks) > 0 {
|
2018-09-24 23:46:21 +02:00
|
|
|
c, err = sd.dialParallel(ctx, primaries, fallbacks)
|
2015-10-31 01:59:47 +01:00
|
|
|
} else {
|
2018-09-24 23:46:21 +02:00
|
|
|
c, err = sd.dialSerial(ctx, primaries)
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 20:04:36 +01:00
|
|
|
if tc, ok := c.(*TCPConn); ok && d.KeepAlive >= 0 {
|
2016-07-22 20:15:38 +02:00
|
|
|
setKeepAlive(tc.fd, true)
|
2019-01-18 20:04:36 +01:00
|
|
|
ka := d.KeepAlive
|
|
|
|
if d.KeepAlive == 0 {
|
2019-09-06 20:12:46 +02:00
|
|
|
ka = defaultTCPKeepAlive
|
2019-01-18 20:04:36 +01:00
|
|
|
}
|
|
|
|
setKeepAlivePeriod(tc.fd, ka)
|
|
|
|
testHookSetKeepAlive(ka)
|
2014-07-19 10:53:52 +02:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
return c, nil
|
2012-01-13 06:11:45 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// dialParallel races two copies of dialSerial, giving the first a
|
|
|
|
// head start. It returns the first established connection and
|
|
|
|
// closes the others. Otherwise it returns an error from the first
|
|
|
|
// primary address.
|
2018-09-24 23:46:21 +02:00
|
|
|
func (sd *sysDialer) dialParallel(ctx context.Context, primaries, fallbacks addrList) (Conn, error) {
|
2016-07-22 20:15:38 +02:00
|
|
|
if len(fallbacks) == 0 {
|
2018-09-24 23:46:21 +02:00
|
|
|
return sd.dialSerial(ctx, primaries)
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
returned := make(chan struct{})
|
|
|
|
defer close(returned)
|
|
|
|
|
|
|
|
type dialResult struct {
|
|
|
|
Conn
|
|
|
|
error
|
|
|
|
primary bool
|
|
|
|
done bool
|
|
|
|
}
|
|
|
|
results := make(chan dialResult) // unbuffered
|
2015-10-31 01:59:47 +01:00
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
startRacer := func(ctx context.Context, primary bool) {
|
|
|
|
ras := primaries
|
|
|
|
if !primary {
|
|
|
|
ras = fallbacks
|
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
c, err := sd.dialSerial(ctx, ras)
|
2015-10-31 01:59:47 +01:00
|
|
|
select {
|
2016-07-22 20:15:38 +02:00
|
|
|
case results <- dialResult{Conn: c, error: err, primary: primary, done: true}:
|
|
|
|
case <-returned:
|
|
|
|
if c != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
var primary, fallback dialResult
|
|
|
|
|
|
|
|
// Start the main racer.
|
|
|
|
primaryCtx, primaryCancel := context.WithCancel(ctx)
|
|
|
|
defer primaryCancel()
|
|
|
|
go startRacer(primaryCtx, true)
|
|
|
|
|
|
|
|
// Start the timer for the fallback racer.
|
2018-09-24 23:46:21 +02:00
|
|
|
fallbackTimer := time.NewTimer(sd.fallbackDelay())
|
2016-07-22 20:15:38 +02:00
|
|
|
defer fallbackTimer.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-fallbackTimer.C:
|
|
|
|
fallbackCtx, fallbackCancel := context.WithCancel(ctx)
|
|
|
|
defer fallbackCancel()
|
|
|
|
go startRacer(fallbackCtx, false)
|
|
|
|
|
|
|
|
case res := <-results:
|
|
|
|
if res.error == nil {
|
|
|
|
return res.Conn, nil
|
|
|
|
}
|
|
|
|
if res.primary {
|
|
|
|
primary = res
|
|
|
|
} else {
|
|
|
|
fallback = res
|
|
|
|
}
|
|
|
|
if primary.done && fallback.done {
|
|
|
|
return nil, primary.error
|
|
|
|
}
|
|
|
|
if res.primary && fallbackTimer.Stop() {
|
|
|
|
// If we were able to stop the timer, that means it
|
|
|
|
// was running (hadn't yet started the fallback), but
|
|
|
|
// we just got an error on the primary path, so start
|
|
|
|
// the fallback immediately (in 0 nanoseconds).
|
|
|
|
fallbackTimer.Reset(0)
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dialSerial connects to a list of addresses in sequence, returning
|
|
|
|
// either the first successful connection, or the first error.
|
2018-09-24 23:46:21 +02:00
|
|
|
func (sd *sysDialer) dialSerial(ctx context.Context, ras addrList) (Conn, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
var firstErr error // The error from the first address is most relevant.
|
|
|
|
|
|
|
|
for i, ra := range ras {
|
|
|
|
select {
|
2016-07-22 20:15:38 +02:00
|
|
|
case <-ctx.Done():
|
2018-09-24 23:46:21 +02:00
|
|
|
return nil, &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: mapErr(ctx.Err())}
|
2015-10-31 01:59:47 +01:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
dialCtx := ctx
|
2020-01-03 00:05:27 +01:00
|
|
|
if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
|
|
|
|
partialDeadline, err := partialDeadline(time.Now(), deadline, len(ras)-i)
|
|
|
|
if err != nil {
|
|
|
|
// Ran out of time.
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: err}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if partialDeadline.Before(deadline) {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
dialCtx, cancel = context.WithDeadline(ctx, partialDeadline)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
|
2018-09-24 23:46:21 +02:00
|
|
|
c, err := sd.dialSingle(dialCtx, ra)
|
2015-10-31 01:59:47 +01:00
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if firstErr == nil {
|
2018-09-24 23:46:21 +02:00
|
|
|
firstErr = &OpError{Op: "dial", Net: sd.network, Source: nil, Addr: nil, Err: errMissingAddress}
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, firstErr
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// dialSingle attempts to establish and returns a single connection to
|
2016-07-22 20:15:38 +02:00
|
|
|
// the destination address.
|
2018-09-24 23:46:21 +02:00
|
|
|
func (sd *sysDialer) dialSingle(ctx context.Context, ra Addr) (c Conn, err error) {
|
2016-07-22 20:15:38 +02:00
|
|
|
trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace)
|
|
|
|
if trace != nil {
|
|
|
|
raStr := ra.String()
|
|
|
|
if trace.ConnectStart != nil {
|
2018-09-24 23:46:21 +02:00
|
|
|
trace.ConnectStart(sd.network, raStr)
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
|
|
|
if trace.ConnectDone != nil {
|
2018-09-24 23:46:21 +02:00
|
|
|
defer func() { trace.ConnectDone(sd.network, raStr, err) }()
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
la := sd.LocalAddr
|
2013-07-16 08:54:42 +02:00
|
|
|
switch ra := ra.(type) {
|
2011-09-16 17:47:21 +02:00
|
|
|
case *TCPAddr:
|
2013-07-16 08:54:42 +02:00
|
|
|
la, _ := la.(*TCPAddr)
|
2018-09-24 23:46:21 +02:00
|
|
|
c, err = sd.dialTCP(ctx, la, ra)
|
2011-09-16 17:47:21 +02:00
|
|
|
case *UDPAddr:
|
2013-07-16 08:54:42 +02:00
|
|
|
la, _ := la.(*UDPAddr)
|
2018-09-24 23:46:21 +02:00
|
|
|
c, err = sd.dialUDP(ctx, la, ra)
|
2011-09-16 17:47:21 +02:00
|
|
|
case *IPAddr:
|
2013-07-16 08:54:42 +02:00
|
|
|
la, _ := la.(*IPAddr)
|
2018-09-24 23:46:21 +02:00
|
|
|
c, err = sd.dialIP(ctx, la, ra)
|
2012-02-01 20:26:59 +01:00
|
|
|
case *UnixAddr:
|
2013-07-16 08:54:42 +02:00
|
|
|
la, _ := la.(*UnixAddr)
|
2018-09-24 23:46:21 +02:00
|
|
|
c, err = sd.dialUnix(ctx, la, ra)
|
|
|
|
default:
|
|
|
|
return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: &AddrError{Err: "unexpected address type", Addr: sd.address}}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: err} // c is non-nil interface containing nil pointer
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenConfig contains options for listening to an address.
|
|
|
|
type ListenConfig struct {
|
|
|
|
// If Control is not nil, it is called after creating the network
|
|
|
|
// connection but before binding it to the operating system.
|
|
|
|
//
|
|
|
|
// Network and address parameters passed to Control method are not
|
|
|
|
// necessarily the ones passed to Listen. For example, passing "tcp" to
|
|
|
|
// Listen will cause the Control function to be called with "tcp4" or "tcp6".
|
|
|
|
Control func(network, address string, c syscall.RawConn) error
|
2019-09-06 20:12:46 +02:00
|
|
|
|
|
|
|
// KeepAlive specifies the keep-alive period for network
|
|
|
|
// connections accepted by this listener.
|
|
|
|
// If zero, keep-alives are enabled if supported by the protocol
|
|
|
|
// and operating system. Network protocols or operating systems
|
|
|
|
// that do not support keep-alives ignore this field.
|
|
|
|
// If negative, keep-alives are disabled.
|
|
|
|
KeepAlive time.Duration
|
2018-09-24 23:46:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listen announces on the local network address.
|
|
|
|
//
|
|
|
|
// See func Listen for a description of the network and address
|
|
|
|
// parameters.
|
|
|
|
func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (Listener, error) {
|
|
|
|
addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
|
|
|
|
}
|
|
|
|
sl := &sysListener{
|
|
|
|
ListenConfig: *lc,
|
|
|
|
network: network,
|
|
|
|
address: address,
|
|
|
|
}
|
|
|
|
var l Listener
|
|
|
|
la := addrs.first(isIPv4)
|
|
|
|
switch la := la.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
l, err = sl.listenTCP(ctx, la)
|
|
|
|
case *UnixAddr:
|
|
|
|
l, err = sl.listenUnix(ctx, la)
|
|
|
|
default:
|
|
|
|
return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // l is non-nil interface containing nil pointer
|
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacket announces on the local network address.
|
|
|
|
//
|
|
|
|
// See func ListenPacket for a description of the network and address
|
|
|
|
// parameters.
|
|
|
|
func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (PacketConn, error) {
|
|
|
|
addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
|
|
|
|
}
|
|
|
|
sl := &sysListener{
|
|
|
|
ListenConfig: *lc,
|
|
|
|
network: network,
|
|
|
|
address: address,
|
|
|
|
}
|
|
|
|
var c PacketConn
|
|
|
|
la := addrs.first(isIPv4)
|
|
|
|
switch la := la.(type) {
|
|
|
|
case *UDPAddr:
|
|
|
|
c, err = sl.listenUDP(ctx, la)
|
|
|
|
case *IPAddr:
|
|
|
|
c, err = sl.listenIP(ctx, la)
|
|
|
|
case *UnixAddr:
|
|
|
|
c, err = sl.listenUnixgram(ctx, la)
|
2011-09-16 17:47:21 +02:00
|
|
|
default:
|
2018-09-24 23:46:21 +02:00
|
|
|
return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2018-09-24 23:46:21 +02:00
|
|
|
return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // c is non-nil interface containing nil pointer
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
return c, nil
|
2012-01-13 06:11:45 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 23:46:21 +02:00
|
|
|
// sysListener contains a Listen's parameters and configuration.
|
|
|
|
type sysListener struct {
|
|
|
|
ListenConfig
|
|
|
|
network, address string
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// Listen announces on the local network address.
|
|
|
|
//
|
|
|
|
// The network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
|
2017-01-14 01:05:42 +01:00
|
|
|
//
|
2017-09-14 19:11:35 +02:00
|
|
|
// For TCP networks, if the host in the address parameter is empty or
|
|
|
|
// a literal unspecified IP address, Listen listens on all available
|
|
|
|
// unicast and anycast IP addresses of the local system.
|
|
|
|
// To only use IPv4, use network "tcp4".
|
|
|
|
// The address can use a host name, but this is not recommended,
|
|
|
|
// because it will create a listener for at most one of the host's IP
|
|
|
|
// addresses.
|
|
|
|
// If the port in the address parameter is empty or "0", as in
|
|
|
|
// "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
|
|
|
|
// The Addr method of Listener can be used to discover the chosen
|
|
|
|
// port.
|
|
|
|
//
|
|
|
|
// See func Dial for a description of the network and address
|
|
|
|
// parameters.
|
|
|
|
func Listen(network, address string) (Listener, error) {
|
2018-09-24 23:46:21 +02:00
|
|
|
var lc ListenConfig
|
|
|
|
return lc.Listen(context.Background(), network, address)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// ListenPacket announces on the local network address.
|
|
|
|
//
|
|
|
|
// The network must be "udp", "udp4", "udp6", "unixgram", or an IP
|
|
|
|
// transport. The IP transports are "ip", "ip4", or "ip6" followed by
|
|
|
|
// a colon and a literal protocol number or a protocol name, as in
|
|
|
|
// "ip:1" or "ip:icmp".
|
2017-01-14 01:05:42 +01:00
|
|
|
//
|
2017-09-14 19:11:35 +02:00
|
|
|
// For UDP and IP networks, if the host in the address parameter is
|
|
|
|
// empty or a literal unspecified IP address, ListenPacket listens on
|
|
|
|
// all available IP addresses of the local system except multicast IP
|
|
|
|
// addresses.
|
|
|
|
// To only use IPv4, use network "udp4" or "ip4:proto".
|
|
|
|
// The address can use a host name, but this is not recommended,
|
|
|
|
// because it will create a listener for at most one of the host's IP
|
|
|
|
// addresses.
|
|
|
|
// If the port in the address parameter is empty or "0", as in
|
|
|
|
// "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
|
|
|
|
// The LocalAddr method of PacketConn can be used to discover the
|
|
|
|
// chosen port.
|
|
|
|
//
|
|
|
|
// See func Dial for a description of the network and address
|
|
|
|
// parameters.
|
|
|
|
func ListenPacket(network, address string) (PacketConn, error) {
|
2018-09-24 23:46:21 +02:00
|
|
|
var lc ListenConfig
|
|
|
|
return lc.ListenPacket(context.Background(), network, address)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|