2010-06-21 20:52:30 -06:00
|
|
|
// Copyright 2010 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
|
|
|
|
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A DialOption modifies a DialOpt call.
|
|
|
|
type DialOption interface {
|
|
|
|
dialOption()
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// TCP is a dial option to dial with TCP (over IPv4 or IPv6).
|
|
|
|
TCP = Network("tcp")
|
|
|
|
|
|
|
|
// UDP is a dial option to dial with UDP (over IPv4 or IPv6).
|
|
|
|
UDP = Network("udp")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Network returns a DialOption to dial using the given network.
|
|
|
|
//
|
|
|
|
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
|
|
|
|
// "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
|
|
|
|
// (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and
|
|
|
|
// "unixpacket".
|
|
|
|
//
|
|
|
|
// For IP networks, net must be "ip", "ip4" or "ip6" followed
|
|
|
|
// by a colon and a protocol number or name, such as
|
|
|
|
// "ipv4:1" or "ip6:ospf".
|
|
|
|
func Network(net string) DialOption {
|
|
|
|
return dialNetwork(net)
|
|
|
|
}
|
|
|
|
|
|
|
|
type dialNetwork string
|
|
|
|
|
|
|
|
func (dialNetwork) dialOption() {}
|
|
|
|
|
|
|
|
// Deadline returns a DialOption to fail a dial that doesn't
|
|
|
|
// complete before t.
|
|
|
|
func Deadline(t time.Time) DialOption {
|
|
|
|
return dialDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns a DialOption to fail a dial that doesn't
|
|
|
|
// complete within the provided duration.
|
|
|
|
func Timeout(d time.Duration) DialOption {
|
|
|
|
return dialDeadline(time.Now().Add(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
type dialDeadline time.Time
|
|
|
|
|
|
|
|
func (dialDeadline) dialOption() {}
|
|
|
|
|
|
|
|
type tcpFastOpen struct{}
|
|
|
|
|
|
|
|
func (tcpFastOpen) dialOption() {}
|
|
|
|
|
|
|
|
// TODO(bradfitz): implement this (golang.org/issue/4842) and unexport this.
|
|
|
|
//
|
|
|
|
// TCPFastTimeout returns an option to use TCP Fast Open (TFO) when
|
|
|
|
// doing this dial. It is only valid for use with TCP connections.
|
|
|
|
// Data sent over a TFO connection may be processed by the peer
|
|
|
|
// multiple times, so should be used with caution.
|
|
|
|
func todo_TCPFastTimeout() DialOption {
|
|
|
|
return tcpFastOpen{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type localAddrOption struct {
|
|
|
|
la Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (localAddrOption) dialOption() {}
|
|
|
|
|
|
|
|
// LocalAddress returns a dial option to perform a dial with the
|
|
|
|
// provided local address. The address must be of a compatible type
|
|
|
|
// for the network being dialed.
|
|
|
|
func LocalAddress(addr Addr) DialOption {
|
|
|
|
return localAddrOption{addr}
|
|
|
|
}
|
2011-12-20 14:17:39 -07:00
|
|
|
|
2013-02-08 05:53:10 -07:00
|
|
|
func parseNetwork(net string) (afnet string, proto int, err error) {
|
2012-01-21 05:51:53 -07:00
|
|
|
i := last(net, ':')
|
|
|
|
if i < 0 { // no colon
|
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
case "udp", "udp4", "udp6":
|
2012-11-27 14:36:05 -07:00
|
|
|
case "ip", "ip4", "ip6":
|
2012-01-21 05:51:53 -07:00
|
|
|
case "unix", "unixgram", "unixpacket":
|
|
|
|
default:
|
|
|
|
return "", 0, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
return net, 0, nil
|
2011-06-29 22:56:13 -06:00
|
|
|
}
|
2012-01-21 05:51:53 -07:00
|
|
|
afnet = net[:i]
|
|
|
|
switch afnet {
|
2011-06-29 22:56:13 -06:00
|
|
|
case "ip", "ip4", "ip6":
|
2012-01-21 05:51:53 -07:00
|
|
|
protostr := net[i+1:]
|
|
|
|
proto, i, ok := dtoi(protostr, 0)
|
|
|
|
if !ok || i != len(protostr) {
|
|
|
|
proto, err = lookupProtocol(protostr)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return afnet, proto, nil
|
2011-06-29 22:56:13 -06:00
|
|
|
}
|
2012-01-21 05:51:53 -07:00
|
|
|
return "", 0, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
|
2013-02-08 05:53:10 -07:00
|
|
|
func resolveAddr(op, net, addr string, deadline time.Time) (Addr, error) {
|
|
|
|
afnet, _, err := parseNetwork(net)
|
2011-06-29 22:56:13 -06:00
|
|
|
if err != nil {
|
2013-02-08 05:53:10 -07:00
|
|
|
return nil, &OpError{op, net, nil, err}
|
2012-01-21 05:51:53 -07:00
|
|
|
}
|
|
|
|
if op == "dial" && addr == "" {
|
2013-02-08 05:53:10 -07:00
|
|
|
return nil, &OpError{op, net, nil, errMissingAddress}
|
2012-11-08 09:35:16 -07:00
|
|
|
}
|
2012-01-21 05:51:53 -07:00
|
|
|
switch afnet {
|
|
|
|
case "unix", "unixgram", "unixpacket":
|
2012-11-08 09:35:16 -07:00
|
|
|
return ResolveUnixAddr(afnet, addr)
|
2011-06-29 22:56:13 -06:00
|
|
|
}
|
2013-02-08 05:53:10 -07:00
|
|
|
return resolveInternetAddr(afnet, addr, deadline)
|
2011-06-29 22:56:13 -06:00
|
|
|
}
|
|
|
|
|
2011-03-28 21:28:42 -06:00
|
|
|
// Dial connects to the address addr on the network net.
|
2010-06-21 20:52:30 -06:00
|
|
|
//
|
|
|
|
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
|
|
|
|
// "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
|
2013-02-08 16:18:32 -07:00
|
|
|
// (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and
|
|
|
|
// "unixpacket".
|
2010-06-21 20:52:30 -06:00
|
|
|
//
|
2012-01-21 05:51:53 -07:00
|
|
|
// For TCP and UDP networks, addresses have the form host:port.
|
|
|
|
// If host is a literal IPv6 address, it must be enclosed
|
|
|
|
// in square brackets. The functions JoinHostPort and SplitHostPort
|
|
|
|
// manipulate addresses in this form.
|
2010-06-21 20:52:30 -06:00
|
|
|
//
|
|
|
|
// Examples:
|
2011-03-28 21:28:42 -06:00
|
|
|
// Dial("tcp", "12.34.56.78:80")
|
|
|
|
// Dial("tcp", "google.com:80")
|
|
|
|
// Dial("tcp", "[de:ad:be:ef::ca:fe]:80")
|
2010-06-21 20:52:30 -06:00
|
|
|
//
|
2012-09-20 16:13:36 -06:00
|
|
|
// For IP networks, net must be "ip", "ip4" or "ip6" followed
|
2012-01-21 05:51:53 -07:00
|
|
|
// by a colon and a protocol number or name.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
// Dial("ip4:1", "127.0.0.1")
|
|
|
|
// Dial("ip6:ospf", "::1")
|
|
|
|
//
|
2011-12-20 14:17:39 -07:00
|
|
|
func Dial(net, addr string) (Conn, error) {
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
return DialOpt(addr, dialNetwork(net))
|
|
|
|
}
|
|
|
|
|
|
|
|
func netFromOptions(opts []DialOption) string {
|
|
|
|
for _, opt := range opts {
|
|
|
|
if p, ok := opt.(dialNetwork); ok {
|
|
|
|
return string(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
|
|
|
|
func deadlineFromOptions(opts []DialOption) time.Time {
|
|
|
|
for _, opt := range opts {
|
|
|
|
if d, ok := opt.(dialDeadline); ok {
|
|
|
|
return time.Time(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return noDeadline
|
|
|
|
}
|
|
|
|
|
|
|
|
var noLocalAddr Addr // nil
|
|
|
|
|
|
|
|
func localAddrFromOptions(opts []DialOption) Addr {
|
|
|
|
for _, opt := range opts {
|
|
|
|
if o, ok := opt.(localAddrOption); ok {
|
|
|
|
return o.la
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return noLocalAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialOpt dials addr using the provided options.
|
|
|
|
// If no options are provided, DialOpt(addr) is equivalent
|
|
|
|
// to Dial("tcp", addr). See Dial for the syntax of addr.
|
|
|
|
func DialOpt(addr string, opts ...DialOption) (Conn, error) {
|
|
|
|
net := netFromOptions(opts)
|
|
|
|
deadline := deadlineFromOptions(opts)
|
|
|
|
la := localAddrFromOptions(opts)
|
|
|
|
ra, err := resolveAddr("dial", net, addr, deadline)
|
2011-06-29 22:56:13 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-03-28 21:28:42 -06:00
|
|
|
}
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
return dial(net, addr, la, ra, deadline)
|
2011-12-20 14:17:39 -07:00
|
|
|
}
|
|
|
|
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
func dial(net, addr string, la, ra Addr, deadline time.Time) (c Conn, err error) {
|
|
|
|
if la != nil && la.Network() != ra.Network() {
|
|
|
|
return nil, &OpError{"dial", net, ra, errors.New("mismatched local addr type " + la.Network())}
|
|
|
|
}
|
2013-02-08 05:53:10 -07:00
|
|
|
switch ra := ra.(type) {
|
2011-06-29 22:56:13 -06:00
|
|
|
case *TCPAddr:
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
la, _ := la.(*TCPAddr)
|
|
|
|
c, err = dialTCP(net, la, ra, deadline)
|
2011-06-29 22:56:13 -06:00
|
|
|
case *UDPAddr:
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
la, _ := la.(*UDPAddr)
|
|
|
|
c, err = dialUDP(net, la, ra, deadline)
|
2011-06-29 22:56:13 -06:00
|
|
|
case *IPAddr:
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
la, _ := la.(*IPAddr)
|
|
|
|
c, err = dialIP(net, la, ra, deadline)
|
2012-01-21 05:51:53 -07:00
|
|
|
case *UnixAddr:
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
la, _ := la.(*UnixAddr)
|
|
|
|
c, err = dialUnix(net, la, ra, deadline)
|
2011-06-29 22:56:13 -06:00
|
|
|
default:
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
err = &OpError{"dial", net + " " + addr, ra, UnknownNetworkError(net)}
|
2011-06-29 22:56:13 -06:00
|
|
|
}
|
|
|
|
if err != nil {
|
2011-10-18 12:51:40 -06:00
|
|
|
return nil, err
|
2010-06-21 20:52:30 -06:00
|
|
|
}
|
2011-06-29 22:56:13 -06:00
|
|
|
return
|
2010-06-21 20:52:30 -06:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:17:39 -07:00
|
|
|
// DialTimeout acts like Dial but takes a timeout.
|
|
|
|
// The timeout includes name resolution, if required.
|
|
|
|
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error) {
|
2013-01-10 18:42:09 -07:00
|
|
|
return dialTimeout(net, addr, timeout)
|
2012-11-08 09:35:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// dialTimeoutRace is the old implementation of DialTimeout, still used
|
|
|
|
// on operating systems where the deadline hasn't been pushed down
|
|
|
|
// into the pollserver.
|
2013-01-10 18:42:09 -07:00
|
|
|
// TODO: fix this on plan9.
|
2012-11-08 09:35:16 -07:00
|
|
|
func dialTimeoutRace(net, addr string, timeout time.Duration) (Conn, error) {
|
2011-12-20 14:17:39 -07:00
|
|
|
t := time.NewTimer(timeout)
|
|
|
|
defer t.Stop()
|
|
|
|
type pair struct {
|
|
|
|
Conn
|
|
|
|
error
|
|
|
|
}
|
|
|
|
ch := make(chan pair, 1)
|
|
|
|
resolvedAddr := make(chan Addr, 1)
|
|
|
|
go func() {
|
2013-02-08 05:53:10 -07:00
|
|
|
ra, err := resolveAddr("dial", net, addr, noDeadline)
|
2011-12-20 14:17:39 -07:00
|
|
|
if err != nil {
|
|
|
|
ch <- pair{nil, err}
|
|
|
|
return
|
|
|
|
}
|
2013-02-08 05:53:10 -07:00
|
|
|
resolvedAddr <- ra // in case we need it for OpError
|
net: add DialOpt, the extensible Dial w/ options dialer
Add DialOpt. So we have:
func Dial(net, addr string) (Conn, error)
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)
func DialOpt(addr string, opts ...DialOption) (Conn, error)
DialTimeout (and Dial) are regrettable in retrospect. Maybe
in a future Go we'll be back down to one Dial, with DialOpt
becoming Dial.
DialOpt looks like:
c, err := net.DialOpt("google.com:80") // tcp is default
c, err := net.DialOpt("google.com:80", net.Timeout(30 * time.Second))
c, err := net.DialOpt("google.com:80", net.TCPFastOpen())
c, err := net.DialOpt("google.com:80", net.LocalAddr(..))
c, err := net.DialOpt("google.com:53", net.Network("udp6"))
And then: (clustered in godoc)
type DialOption interface { /* private only */ }
func Deadline(time.Time) DialOption
func LocalAddr(Addr) DialOption
func Network(string) DialOption
func TCPFastOpen() DialOption
func Timeout(time.Duration) DialOption
I'm pretty confident we could add Happy Eyeballs to this too.
Fixes #3097
Update #3610
Update #4842
R=golang-dev, r, dave, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7365049
2013-02-27 12:59:36 -07:00
|
|
|
c, err := dial(net, addr, noLocalAddr, ra, noDeadline)
|
2011-12-20 14:17:39 -07:00
|
|
|
ch <- pair{c, err}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
// Try to use the real Addr in our OpError, if we resolved it
|
|
|
|
// before the timeout. Otherwise we just use stringAddr.
|
2013-02-08 05:53:10 -07:00
|
|
|
var ra Addr
|
2011-12-20 14:17:39 -07:00
|
|
|
select {
|
|
|
|
case a := <-resolvedAddr:
|
2013-02-08 05:53:10 -07:00
|
|
|
ra = a
|
2011-12-20 14:17:39 -07:00
|
|
|
default:
|
2013-02-08 05:53:10 -07:00
|
|
|
ra = &stringAddr{net, addr}
|
2011-12-20 14:17:39 -07:00
|
|
|
}
|
|
|
|
err := &OpError{
|
|
|
|
Op: "dial",
|
|
|
|
Net: net,
|
2013-02-08 05:53:10 -07:00
|
|
|
Addr: ra,
|
2011-12-20 14:17:39 -07:00
|
|
|
Err: &timeoutError{},
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
case p := <-ch:
|
|
|
|
return p.Conn, p.error
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
type stringAddr struct {
|
|
|
|
net, addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a stringAddr) Network() string { return a.net }
|
|
|
|
func (a stringAddr) String() string { return a.addr }
|
|
|
|
|
2010-06-21 20:52:30 -06:00
|
|
|
// Listen announces on the local network address laddr.
|
2012-01-21 05:51:53 -07:00
|
|
|
// The network string net must be a stream-oriented network:
|
2012-07-19 17:32:25 -06:00
|
|
|
// "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
|
2012-01-21 05:51:53 -07:00
|
|
|
func Listen(net, laddr string) (Listener, error) {
|
2013-02-08 05:53:10 -07:00
|
|
|
la, err := resolveAddr("listen", net, laddr, noDeadline)
|
2012-01-21 05:51:53 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-02-08 05:53:10 -07:00
|
|
|
switch la := la.(type) {
|
|
|
|
case *TCPAddr:
|
2012-02-14 09:59:18 -07:00
|
|
|
return ListenTCP(net, la)
|
2013-02-08 05:53:10 -07:00
|
|
|
case *UnixAddr:
|
2012-01-21 05:51:53 -07:00
|
|
|
return ListenUnix(net, la)
|
2010-06-21 20:52:30 -06:00
|
|
|
}
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacket announces on the local network address laddr.
|
|
|
|
// The network string net must be a packet-oriented network:
|
2012-01-21 05:51:53 -07:00
|
|
|
// "udp", "udp4", "udp6", "ip", "ip4", "ip6" or "unixgram".
|
2012-11-27 14:36:05 -07:00
|
|
|
func ListenPacket(net, laddr string) (PacketConn, error) {
|
2013-02-08 05:53:10 -07:00
|
|
|
la, err := resolveAddr("listen", net, laddr, noDeadline)
|
2012-01-21 05:51:53 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-02-08 05:53:10 -07:00
|
|
|
switch la := la.(type) {
|
|
|
|
case *UDPAddr:
|
2012-01-21 05:51:53 -07:00
|
|
|
return ListenUDP(net, la)
|
2013-02-08 05:53:10 -07:00
|
|
|
case *IPAddr:
|
2012-01-21 05:51:53 -07:00
|
|
|
return ListenIP(net, la)
|
2013-02-08 05:53:10 -07:00
|
|
|
case *UnixAddr:
|
2012-12-15 19:51:47 -07:00
|
|
|
return ListenUnixgram(net, la)
|
2011-02-16 13:03:47 -07:00
|
|
|
}
|
2010-06-21 20:52:30 -06:00
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|