* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / libgo / go / net / dial.go
blobb18d283626c71203d4884422756ef96dcd84d17e
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.
5 package net
7 import (
8 "errors"
9 "time"
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.
17 type Dialer struct {
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
20 // earlier.
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.
27 Timeout time.Duration
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.
33 Deadline time.Time
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.
39 LocalAddr Addr
42 // Return either now+Timeout or Deadline, whichever comes first.
43 // Or zero, if neither is set.
44 func (d *Dialer) deadline() time.Time {
45 if d.Timeout == 0 {
46 return d.Deadline
48 timeoutDeadline := time.Now().Add(d.Timeout)
49 if d.Deadline.IsZero() || timeoutDeadline.Before(d.Deadline) {
50 return timeoutDeadline
51 } else {
52 return d.Deadline
56 func parseNetwork(net string) (afnet string, proto int, err error) {
57 i := last(net, ':')
58 if i < 0 { // no colon
59 switch net {
60 case "tcp", "tcp4", "tcp6":
61 case "udp", "udp4", "udp6":
62 case "ip", "ip4", "ip6":
63 case "unix", "unixgram", "unixpacket":
64 default:
65 return "", 0, UnknownNetworkError(net)
67 return net, 0, nil
69 afnet = net[:i]
70 switch afnet {
71 case "ip", "ip4", "ip6":
72 protostr := net[i+1:]
73 proto, i, ok := dtoi(protostr, 0)
74 if !ok || i != len(protostr) {
75 proto, err = lookupProtocol(protostr)
76 if err != nil {
77 return "", 0, err
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)
87 if err != nil {
88 return nil, &OpError{op, net, nil, err}
90 if op == "dial" && addr == "" {
91 return nil, &OpError{op, net, nil, errMissingAddress}
93 switch afnet {
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
105 // "unixpacket".
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
112 // in this form.
114 // Examples:
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.
124 // Examples:
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) {
130 var d Dialer
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
144 // parameters.
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) {
154 case *TCPAddr:
155 la, _ := la.(*TCPAddr)
156 c, err = dialTCP(net, la, ra, deadline)
157 case *UDPAddr:
158 la, _ := la.(*UDPAddr)
159 c, err = dialUDP(net, la, ra, deadline)
160 case *IPAddr:
161 la, _ := la.(*IPAddr)
162 c, err = dialIP(net, la, ra, deadline)
163 case *UnixAddr:
164 la, _ := la.(*UnixAddr)
165 c, err = dialUnix(net, la, ra, deadline)
166 default:
167 err = &OpError{"dial", net + " " + addr, ra, UnknownNetworkError(net)}
169 if err != nil {
170 return nil, err
172 return
175 type stringAddr struct {
176 net, addr string
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)
188 if err != nil {
189 return nil, err
191 switch la := la.(type) {
192 case *TCPAddr:
193 return ListenTCP(net, la)
194 case *UnixAddr:
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)
206 if err != nil {
207 return nil, err
209 switch la := la.(type) {
210 case *UDPAddr:
211 return ListenUDP(net, la)
212 case *IPAddr:
213 return ListenIP(net, la)
214 case *UnixAddr:
215 return ListenUnixgram(net, la)
217 return nil, UnknownNetworkError(net)