2010-12-03 05:34:57 +01: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
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
import (
|
2013-07-16 08:54:42 +02:00
|
|
|
"errors"
|
2012-01-13 06:11:45 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
// DualStack allows a single dial to attempt to establish
|
|
|
|
// multiple IPv4 and IPv6 connections and to return the first
|
|
|
|
// established connection when the network is "tcp" and the
|
|
|
|
// destination is a host name that has multiple address family
|
|
|
|
// DNS records.
|
|
|
|
DualStack bool
|
2014-07-19 10:53:52 +02:00
|
|
|
|
|
|
|
// KeepAlive specifies the keep-alive period for an active
|
|
|
|
// network connection.
|
|
|
|
// If zero, keep-alives are not enabled. Network protocols
|
|
|
|
// that do not support keep-alives ignore this field.
|
|
|
|
KeepAlive time.Duration
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return either now+Timeout or Deadline, whichever comes first.
|
|
|
|
// Or zero, if neither is set.
|
|
|
|
func (d *Dialer) deadline() time.Time {
|
|
|
|
if d.Timeout == 0 {
|
|
|
|
return d.Deadline
|
|
|
|
}
|
|
|
|
timeoutDeadline := time.Now().Add(d.Timeout)
|
|
|
|
if d.Deadline.IsZero() || timeoutDeadline.Before(d.Deadline) {
|
|
|
|
return timeoutDeadline
|
|
|
|
} else {
|
|
|
|
return d.Deadline
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNetwork(net string) (afnet string, proto int, err error) {
|
2012-02-01 20:26:59 +01:00
|
|
|
i := last(net, ':')
|
|
|
|
if i < 0 { // no colon
|
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
case "udp", "udp4", "udp6":
|
2012-12-13 00:13:29 +01:00
|
|
|
case "ip", "ip4", "ip6":
|
2012-02-01 20:26:59 +01:00
|
|
|
case "unix", "unixgram", "unixpacket":
|
|
|
|
default:
|
|
|
|
return "", 0, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
return net, 0, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
afnet = net[:i]
|
|
|
|
switch afnet {
|
2011-09-16 17:47:21 +02:00
|
|
|
case "ip", "ip4", "ip6":
|
2012-02-01 20:26:59 +01: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-09-16 17:47:21 +02:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
return "", 0, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func resolveAddr(op, net, addr string, deadline time.Time) (netaddr, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
afnet, _, err := parseNetwork(net)
|
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":
|
2012-11-21 08:03:38 +01:00
|
|
|
return ResolveUnixAddr(afnet, addr)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
return resolveInternetAddr(afnet, addr, deadline)
|
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
|
|
|
//
|
2012-02-01 20:26:59 +01:00
|
|
|
// For TCP and UDP networks, addresses have the form host:port.
|
2015-01-15 01:27:56 +01:00
|
|
|
// If host is a literal IPv6 address it must be enclosed
|
|
|
|
// in square brackets as in "[::1]:80" or "[ipv6-host%zone]:80".
|
2013-07-16 08:54:42 +02:00
|
|
|
// The functions JoinHostPort and SplitHostPort manipulate addresses
|
|
|
|
// in this form.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// Examples:
|
2011-03-30 17:33:16 +02:00
|
|
|
// Dial("tcp", "12.34.56.78:80")
|
2013-07-16 08:54:42 +02:00
|
|
|
// Dial("tcp", "google.com:http")
|
|
|
|
// Dial("tcp", "[2001:db8::1]:http")
|
|
|
|
// Dial("tcp", "[fe80::1%lo0]: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
|
|
|
|
// by a colon and a protocol number or name and the addr must be a
|
|
|
|
// literal IP address.
|
2012-02-01 20:26:59 +01:00
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
// Dial("ip4:1", "127.0.0.1")
|
|
|
|
// Dial("ip6:ospf", "::1")
|
|
|
|
//
|
2013-07-16 08:54:42 +02:00
|
|
|
// For Unix networks, the address must be a file system path.
|
|
|
|
func Dial(network, address string) (Conn, error) {
|
|
|
|
var d Dialer
|
|
|
|
return d.Dial(network, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialTimeout acts like Dial but takes a timeout.
|
|
|
|
// The timeout includes name resolution, if required.
|
|
|
|
func DialTimeout(network, address string, timeout time.Duration) (Conn, error) {
|
|
|
|
d := Dialer{Timeout: timeout}
|
|
|
|
return d.Dial(network, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2013-11-06 20:49:01 +01:00
|
|
|
ra, err := resolveAddr("dial", network, address, d.deadline())
|
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "dial", Net: network, Addr: nil, Err: err}
|
|
|
|
}
|
|
|
|
dialer := func(deadline time.Time) (Conn, error) {
|
|
|
|
return dialSingle(network, address, d.LocalAddr, ra.toAddr(), deadline)
|
|
|
|
}
|
|
|
|
if ras, ok := ra.(addrList); ok && d.DualStack && network == "tcp" {
|
|
|
|
dialer = func(deadline time.Time) (Conn, error) {
|
|
|
|
return dialMulti(network, address, d.LocalAddr, ras, deadline)
|
|
|
|
}
|
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
c, err := dial(network, ra.toAddr(), dialer, d.deadline())
|
|
|
|
if d.KeepAlive > 0 && err == nil {
|
|
|
|
if tc, ok := c.(*TCPConn); ok {
|
|
|
|
tc.SetKeepAlive(true)
|
|
|
|
tc.SetKeepAlivePeriod(d.KeepAlive)
|
|
|
|
testHookSetKeepAlive()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c, err
|
2012-01-13 06:11:45 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
var testHookSetKeepAlive = func() {} // changed by dial_test.go
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// dialMulti attempts to establish connections to each destination of
|
|
|
|
// the list of addresses. It will return the first established
|
|
|
|
// connection and close the other connections. Otherwise it returns
|
|
|
|
// error on the last attempt.
|
|
|
|
func dialMulti(net, addr string, la Addr, ras addrList, deadline time.Time) (Conn, error) {
|
|
|
|
type racer struct {
|
|
|
|
Conn
|
|
|
|
error
|
|
|
|
}
|
|
|
|
// Sig controls the flow of dial results on lane. It passes a
|
|
|
|
// token to the next racer and also indicates the end of flow
|
|
|
|
// by using closed channel.
|
|
|
|
sig := make(chan bool, 1)
|
|
|
|
lane := make(chan racer, 1)
|
|
|
|
for _, ra := range ras {
|
|
|
|
go func(ra Addr) {
|
|
|
|
c, err := dialSingle(net, addr, la, ra, deadline)
|
|
|
|
if _, ok := <-sig; ok {
|
2014-06-05 01:15:33 +02:00
|
|
|
lane <- racer{c, err}
|
2013-11-06 20:49:01 +01:00
|
|
|
} else if err == nil {
|
|
|
|
// We have to return the resources
|
|
|
|
// that belong to the other
|
|
|
|
// connections here for avoiding
|
|
|
|
// unnecessary resource starvation.
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}(ra.toAddr())
|
|
|
|
}
|
|
|
|
defer close(sig)
|
|
|
|
lastErr := errTimeout
|
|
|
|
nracers := len(ras)
|
|
|
|
for nracers > 0 {
|
|
|
|
sig <- true
|
2015-01-15 01:27:56 +01:00
|
|
|
racer := <-lane
|
|
|
|
if racer.error == nil {
|
|
|
|
return racer.Conn, nil
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
lastErr = racer.error
|
|
|
|
nracers--
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2014-06-05 01:15:33 +02:00
|
|
|
return nil, lastErr
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// dialSingle attempts to establish and returns a single connection to
|
|
|
|
// the destination address.
|
|
|
|
func dialSingle(net, addr string, la, ra Addr, deadline time.Time) (c Conn, err error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
if la != nil && la.Network() != ra.Network() {
|
2013-11-06 20:49:01 +01:00
|
|
|
return nil, &OpError{Op: "dial", Net: net, Addr: ra, Err: errors.New("mismatched local address type " + la.Network())}
|
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)
|
|
|
|
c, err = dialTCP(net, la, ra, deadline)
|
2011-09-16 17:47:21 +02:00
|
|
|
case *UDPAddr:
|
2013-07-16 08:54:42 +02:00
|
|
|
la, _ := la.(*UDPAddr)
|
|
|
|
c, err = dialUDP(net, la, ra, deadline)
|
2011-09-16 17:47:21 +02:00
|
|
|
case *IPAddr:
|
2013-07-16 08:54:42 +02:00
|
|
|
la, _ := la.(*IPAddr)
|
|
|
|
c, err = dialIP(net, la, ra, deadline)
|
2012-02-01 20:26:59 +01:00
|
|
|
case *UnixAddr:
|
2013-07-16 08:54:42 +02:00
|
|
|
la, _ := la.(*UnixAddr)
|
|
|
|
c, err = dialUnix(net, la, ra, deadline)
|
2011-09-16 17:47:21 +02:00
|
|
|
default:
|
2013-11-06 20:49:01 +01:00
|
|
|
return nil, &OpError{Op: "dial", Net: net, Addr: ra, Err: &AddrError{Err: "unexpected address type", Addr: addr}}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2013-11-06 20:49:01 +01:00
|
|
|
return nil, 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
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Listen announces on the local network address laddr.
|
2013-07-16 08:54:42 +02:00
|
|
|
// The network net must be a stream-oriented network: "tcp", "tcp4",
|
|
|
|
// "tcp6", "unix" or "unixpacket".
|
|
|
|
// See Dial for the syntax of laddr.
|
2012-02-01 20:26:59 +01:00
|
|
|
func Listen(net, laddr string) (Listener, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
la, err := resolveAddr("listen", net, laddr, noDeadline)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err != nil {
|
2013-11-06 20:49:01 +01:00
|
|
|
return nil, &OpError{Op: "listen", Net: net, Addr: nil, Err: err}
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
var l Listener
|
|
|
|
switch la := la.toAddr().(type) {
|
2013-07-16 08:54:42 +02:00
|
|
|
case *TCPAddr:
|
2013-11-06 20:49:01 +01:00
|
|
|
l, err = ListenTCP(net, la)
|
2013-07-16 08:54:42 +02:00
|
|
|
case *UnixAddr:
|
2013-11-06 20:49:01 +01:00
|
|
|
l, err = ListenUnix(net, la)
|
|
|
|
default:
|
|
|
|
return nil, &OpError{Op: "listen", Net: net, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: laddr}}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err // l is non-nil interface containing nil pointer
|
|
|
|
}
|
|
|
|
return l, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacket announces on the local network address laddr.
|
2013-07-16 08:54:42 +02:00
|
|
|
// The network net must be a packet-oriented network: "udp", "udp4",
|
|
|
|
// "udp6", "ip", "ip4", "ip6" or "unixgram".
|
|
|
|
// See Dial for the syntax of laddr.
|
2012-12-13 00:13:29 +01:00
|
|
|
func ListenPacket(net, laddr string) (PacketConn, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
la, err := resolveAddr("listen", net, laddr, noDeadline)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err != nil {
|
2013-11-06 20:49:01 +01:00
|
|
|
return nil, &OpError{Op: "listen", Net: net, Addr: nil, Err: err}
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
var l PacketConn
|
|
|
|
switch la := la.toAddr().(type) {
|
2013-07-16 08:54:42 +02:00
|
|
|
case *UDPAddr:
|
2013-11-06 20:49:01 +01:00
|
|
|
l, err = ListenUDP(net, la)
|
2013-07-16 08:54:42 +02:00
|
|
|
case *IPAddr:
|
2013-11-06 20:49:01 +01:00
|
|
|
l, err = ListenIP(net, la)
|
2013-07-16 08:54:42 +02:00
|
|
|
case *UnixAddr:
|
2013-11-06 20:49:01 +01:00
|
|
|
l, err = ListenUnixgram(net, la)
|
|
|
|
default:
|
|
|
|
return nil, &OpError{Op: "listen", Net: net, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: laddr}}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err // l is non-nil interface containing nil pointer
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
return l, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|