OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package net | 5 package net |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | 8 "errors" |
9 "time" | 9 "time" |
10 ) | 10 ) |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 162 } |
163 | 163 |
164 // Dial connects to the address addr on the network net. | 164 // Dial connects to the address addr on the network net. |
165 // | 165 // |
166 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), | 166 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), |
167 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" | 167 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" |
168 // (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and | 168 // (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and |
169 // "unixpacket". | 169 // "unixpacket". |
170 // | 170 // |
171 // For TCP and UDP networks, addresses have the form host:port. | 171 // For TCP and UDP networks, addresses have the form host:port. |
172 // If host is a literal IPv6 address, it must be enclosed | 172 // If host is a literal IPv6 address or host name, it must be enclosed |
173 // in square brackets. The functions JoinHostPort and SplitHostPort | 173 // in square brackets as in "[::1]:80", "[ipv6-host]:http" or |
174 // manipulate addresses in this form. | 174 // "[ipv6-host%zone]:80". |
| 175 // The functions JoinHostPort and SplitHostPort manipulate addresses |
| 176 // in this form. |
175 // | 177 // |
176 // Examples: | 178 // Examples: |
177 // Dial("tcp", "12.34.56.78:80") | 179 // Dial("tcp", "12.34.56.78:80") |
178 //» Dial("tcp", "google.com:80") | 180 //» Dial("tcp", "google.com:http") |
179 //» Dial("tcp", "[de:ad:be:ef::ca:fe]:80") | 181 //» Dial("tcp", "[2001:db8::1]:http") |
| 182 //» Dial("tcp", "[fe80::1%lo0]:80") |
180 // | 183 // |
181 // For IP networks, net must be "ip", "ip4" or "ip6" followed | 184 // For IP networks, the net must be "ip", "ip4" or "ip6" followed by a |
182 // by a colon and a protocol number or name. | 185 // colon and a protocol number or name and the addr must be a literal |
| 186 // IP address. |
183 // | 187 // |
184 // Examples: | 188 // Examples: |
185 // Dial("ip4:1", "127.0.0.1") | 189 // Dial("ip4:1", "127.0.0.1") |
186 // Dial("ip6:ospf", "::1") | 190 // Dial("ip6:ospf", "::1") |
187 // | 191 // |
| 192 // For Unix networks, the addr must be a file system path. |
188 func Dial(net, addr string) (Conn, error) { | 193 func Dial(net, addr string) (Conn, error) { |
189 return DialOpt(addr, dialNetwork(net)) | 194 return DialOpt(addr, dialNetwork(net)) |
190 } | 195 } |
191 | 196 |
192 // DialOpt dials addr using the provided options. | 197 // DialOpt dials addr using the provided options. |
193 // If no options are provided, DialOpt(addr) is equivalent | 198 // If no options are provided, DialOpt(addr) is equivalent |
194 // to Dial("tcp", addr). See Dial for the syntax of addr. | 199 // to Dial("tcp", addr). See Dial for the syntax of addr. |
195 func DialOpt(addr string, opts ...DialOption) (Conn, error) { | 200 func DialOpt(addr string, opts ...DialOption) (Conn, error) { |
196 var o dialOpts | 201 var o dialOpts |
197 for _, opt := range opts { | 202 for _, opt := range opts { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 } | 288 } |
284 | 289 |
285 type stringAddr struct { | 290 type stringAddr struct { |
286 net, addr string | 291 net, addr string |
287 } | 292 } |
288 | 293 |
289 func (a stringAddr) Network() string { return a.net } | 294 func (a stringAddr) Network() string { return a.net } |
290 func (a stringAddr) String() string { return a.addr } | 295 func (a stringAddr) String() string { return a.addr } |
291 | 296 |
292 // Listen announces on the local network address laddr. | 297 // Listen announces on the local network address laddr. |
293 // The network string net must be a stream-oriented network: | 298 // The network net must be a stream-oriented network: "tcp", "tcp4", |
294 // "tcp", "tcp4", "tcp6", "unix" or "unixpacket". | 299 // "tcp6", "unix" or "unixpacket". |
| 300 // See Dial for the syntax of laddr. |
295 func Listen(net, laddr string) (Listener, error) { | 301 func Listen(net, laddr string) (Listener, error) { |
296 la, err := resolveAddr("listen", net, laddr, noDeadline) | 302 la, err := resolveAddr("listen", net, laddr, noDeadline) |
297 if err != nil { | 303 if err != nil { |
298 return nil, err | 304 return nil, err |
299 } | 305 } |
300 switch la := la.(type) { | 306 switch la := la.(type) { |
301 case *TCPAddr: | 307 case *TCPAddr: |
302 return ListenTCP(net, la) | 308 return ListenTCP(net, la) |
303 case *UnixAddr: | 309 case *UnixAddr: |
304 return ListenUnix(net, la) | 310 return ListenUnix(net, la) |
305 } | 311 } |
306 return nil, UnknownNetworkError(net) | 312 return nil, UnknownNetworkError(net) |
307 } | 313 } |
308 | 314 |
309 // ListenPacket announces on the local network address laddr. | 315 // ListenPacket announces on the local network address laddr. |
310 // The network string net must be a packet-oriented network: | 316 // The network net must be a packet-oriented network: "udp", "udp4", |
311 // "udp", "udp4", "udp6", "ip", "ip4", "ip6" or "unixgram". | 317 // "udp6", "ip", "ip4", "ip6" or "unixgram". |
| 318 // See Dial for the syntax of laddr. |
312 func ListenPacket(net, laddr string) (PacketConn, error) { | 319 func ListenPacket(net, laddr string) (PacketConn, error) { |
313 la, err := resolveAddr("listen", net, laddr, noDeadline) | 320 la, err := resolveAddr("listen", net, laddr, noDeadline) |
314 if err != nil { | 321 if err != nil { |
315 return nil, err | 322 return nil, err |
316 } | 323 } |
317 switch la := la.(type) { | 324 switch la := la.(type) { |
318 case *UDPAddr: | 325 case *UDPAddr: |
319 return ListenUDP(net, la) | 326 return ListenUDP(net, la) |
320 case *IPAddr: | 327 case *IPAddr: |
321 return ListenIP(net, la) | 328 return ListenIP(net, la) |
322 case *UnixAddr: | 329 case *UnixAddr: |
323 return ListenUnixgram(net, la) | 330 return ListenUnixgram(net, la) |
324 } | 331 } |
325 return nil, UnknownNetworkError(net) | 332 return nil, UnknownNetworkError(net) |
326 } | 333 } |
OLD | NEW |