1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 // A Dialer contains options for connecting to an address.
14 // The zero value for each field is equivalent to dialing
15 // without that option. Dialing with the zero value of Dialer
16 // is therefore equivalent to just calling the Dial function.
18 // Timeout is the maximum amount of time a dial will wait for
19 // a connect to complete. If Deadline is also set, it may fail
22 // The default is no timeout.
24 // With or without a timeout, the operating system may impose
25 // its own earlier timeout. For instance, TCP timeouts are
26 // often around 3 minutes.
29 // Deadline is the absolute point in time after which dials
30 // will fail. If Timeout is set, it may fail earlier.
31 // Zero means no deadline, or dependent on the operating system
32 // as with the Timeout option.
35 // LocalAddr is the local address to use when dialing an
36 // address. The address must be of a compatible type for the
37 // network being dialed.
38 // If nil, a local address is automatically chosen.
42 // Return either now+Timeout or Deadline, whichever comes first.
43 // Or zero, if neither is set.
44 func (d
*Dialer
) deadline() time
.Time
{
48 timeoutDeadline
:= time
.Now().Add(d
.Timeout
)
49 if d
.Deadline
.IsZero() || timeoutDeadline
.Before(d
.Deadline
) {
50 return timeoutDeadline
56 func parseNetwork(net
string) (afnet
string, proto
int, err error
) {
58 if i
< 0 { // no colon
60 case "tcp", "tcp4", "tcp6":
61 case "udp", "udp4", "udp6":
62 case "ip", "ip4", "ip6":
63 case "unix", "unixgram", "unixpacket":
65 return "", 0, UnknownNetworkError(net
)
71 case "ip", "ip4", "ip6":
73 proto
, i
, ok
:= dtoi(protostr
, 0)
74 if !ok || i
!= len(protostr
) {
75 proto
, err
= lookupProtocol(protostr
)
80 return afnet
, proto
, nil
82 return "", 0, UnknownNetworkError(net
)
85 func resolveAddr(op
, net
, addr
string, deadline time
.Time
) (Addr
, error
) {
86 afnet
, _
, err
:= parseNetwork(net
)
88 return nil, &OpError
{op
, net
, nil, err
}
90 if op
== "dial" && addr
== "" {
91 return nil, &OpError
{op
, net
, nil, errMissingAddress
}
94 case "unix", "unixgram", "unixpacket":
95 return ResolveUnixAddr(afnet
, addr
)
97 return resolveInternetAddr(afnet
, addr
, deadline
)
100 // Dial connects to the address on the named network.
102 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
103 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
104 // (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and
107 // For TCP and UDP networks, addresses have the form host:port.
108 // If host is a literal IPv6 address or host name, it must be enclosed
109 // in square brackets as in "[::1]:80", "[ipv6-host]:http" or
110 // "[ipv6-host%zone]:80".
111 // The functions JoinHostPort and SplitHostPort manipulate addresses
115 // Dial("tcp", "12.34.56.78:80")
116 // Dial("tcp", "google.com:http")
117 // Dial("tcp", "[2001:db8::1]:http")
118 // Dial("tcp", "[fe80::1%lo0]:80")
120 // For IP networks, the network must be "ip", "ip4" or "ip6" followed
121 // by a colon and a protocol number or name and the addr must be a
122 // literal IP address.
125 // Dial("ip4:1", "127.0.0.1")
126 // Dial("ip6:ospf", "::1")
128 // For Unix networks, the address must be a file system path.
129 func Dial(network
, address
string) (Conn
, error
) {
131 return d
.Dial(network
, address
)
134 // DialTimeout acts like Dial but takes a timeout.
135 // The timeout includes name resolution, if required.
136 func DialTimeout(network
, address
string, timeout time
.Duration
) (Conn
, error
) {
137 d
:= Dialer
{Timeout
: timeout
}
138 return d
.Dial(network
, address
)
141 // Dial connects to the address on the named network.
143 // See func Dial for a description of the network and address
145 func (d
*Dialer
) Dial(network
, address
string) (Conn
, error
) {
146 return resolveAndDial(network
, address
, d
.LocalAddr
, d
.deadline())
149 func dial(net
, addr
string, la
, ra Addr
, deadline time
.Time
) (c Conn
, err error
) {
150 if la
!= nil && la
.Network() != ra
.Network() {
151 return nil, &OpError
{"dial", net
, ra
, errors
.New("mismatched local addr type " + la
.Network())}
153 switch ra
:= ra
.(type) {
155 la
, _
:= la
.(*TCPAddr
)
156 c
, err
= dialTCP(net
, la
, ra
, deadline
)
158 la
, _
:= la
.(*UDPAddr
)
159 c
, err
= dialUDP(net
, la
, ra
, deadline
)
161 la
, _
:= la
.(*IPAddr
)
162 c
, err
= dialIP(net
, la
, ra
, deadline
)
164 la
, _
:= la
.(*UnixAddr
)
165 c
, err
= dialUnix(net
, la
, ra
, deadline
)
167 err
= &OpError
{"dial", net
+ " " + addr
, ra
, UnknownNetworkError(net
)}
175 type stringAddr
struct {
179 func (a stringAddr
) Network() string { return a
.net
}
180 func (a stringAddr
) String() string { return a
.addr
}
182 // Listen announces on the local network address laddr.
183 // The network net must be a stream-oriented network: "tcp", "tcp4",
184 // "tcp6", "unix" or "unixpacket".
185 // See Dial for the syntax of laddr.
186 func Listen(net
, laddr
string) (Listener
, error
) {
187 la
, err
:= resolveAddr("listen", net
, laddr
, noDeadline
)
191 switch la
:= la
.(type) {
193 return ListenTCP(net
, la
)
195 return ListenUnix(net
, la
)
197 return nil, UnknownNetworkError(net
)
200 // ListenPacket announces on the local network address laddr.
201 // The network net must be a packet-oriented network: "udp", "udp4",
202 // "udp6", "ip", "ip4", "ip6" or "unixgram".
203 // See Dial for the syntax of laddr.
204 func ListenPacket(net
, laddr
string) (PacketConn
, error
) {
205 la
, err
:= resolveAddr("listen", net
, laddr
, noDeadline
)
209 switch la
:= la
.(type) {
211 return ListenUDP(net
, la
)
213 return ListenIP(net
, la
)
215 return ListenUnixgram(net
, la
)
217 return nil, UnknownNetworkError(net
)