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 "os" | 7 import "os" |
8 | 8 |
| 9 func resolveNetAddr(op, net, addr string) (a Addr, err os.Error) { |
| 10 if addr == "" { |
| 11 return nil, &OpError{op, net, nil, errMissingAddress} |
| 12 } |
| 13 switch net { |
| 14 case "tcp", "tcp4", "tcp6": |
| 15 a, err = ResolveTCPAddr(net, addr) |
| 16 case "udp", "udp4", "udp6": |
| 17 a, err = ResolveUDPAddr(net, addr) |
| 18 case "unix", "unixgram", "unixpacket": |
| 19 a, err = ResolveUnixAddr(net, addr) |
| 20 case "ip", "ip4", "ip6": |
| 21 a, err = ResolveIPAddr(net, addr) |
| 22 default: |
| 23 err = UnknownNetworkError(net) |
| 24 } |
| 25 if err == nil { |
| 26 return |
| 27 } |
| 28 return nil, &OpError{op, net + " " + addr, nil, err} |
| 29 } |
| 30 |
9 // Dial connects to the address addr on the network net. | 31 // Dial connects to the address addr on the network net. |
10 // | 32 // |
11 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), | 33 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), |
12 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" | 34 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" |
13 // (IPv4-only), "ip6" (IPv6-only), "unix" and "unixgram". | 35 // (IPv4-only), "ip6" (IPv6-only), "unix" and "unixgram". |
14 // | 36 // |
15 // For IP networks, addresses have the form host:port. If host is | 37 // For IP networks, addresses have the form host:port. If host is |
16 // a literal IPv6 address, it must be enclosed in square brackets. | 38 // a literal IPv6 address, it must be enclosed in square brackets. |
17 // The functions JoinHostPort and SplitHostPort manipulate· | 39 // The functions JoinHostPort and SplitHostPort manipulate· |
18 // addresses in this form. | 40 // addresses in this form. |
19 // | 41 // |
20 // Examples: | 42 // Examples: |
21 // Dial("tcp", "12.34.56.78:80") | 43 // Dial("tcp", "12.34.56.78:80") |
22 // Dial("tcp", "google.com:80") | 44 // Dial("tcp", "google.com:80") |
23 // Dial("tcp", "[de:ad:be:ef::ca:fe]:80") | 45 // Dial("tcp", "[de:ad:be:ef::ca:fe]:80") |
24 // | 46 // |
25 func Dial(net, addr string) (c Conn, err os.Error) { | 47 func Dial(net, addr string) (c Conn, err os.Error) { |
26 » raddr := addr | 48 » addri, err := resolveNetAddr("dial", net, addr) |
27 » if raddr == "" { | 49 » if err != nil { |
28 » » return nil, &OpError{"dial", net, nil, errMissingAddress} | 50 » » return nil, err |
29 } | 51 } |
30 » switch net { | 52 » switch ra := addri.(type) { |
31 » case "tcp", "tcp4", "tcp6": | 53 » case *TCPAddr: |
32 » » var ra *TCPAddr | 54 » » c, err = DialTCP(net, nil, ra) |
33 » » if ra, err = ResolveTCPAddr(net, raddr); err != nil { | 55 » case *UDPAddr: |
34 » » » goto Error | 56 » » c, err = DialUDP(net, nil, ra) |
35 » » } | 57 » case *UnixAddr: |
36 » » c, err := DialTCP(net, nil, ra) | |
37 » » if err != nil { | |
38 » » » return nil, err | |
39 » » } | |
40 » » return c, nil | |
41 » case "udp", "udp4", "udp6": | |
42 » » var ra *UDPAddr | |
43 » » if ra, err = ResolveUDPAddr(net, raddr); err != nil { | |
44 » » » goto Error | |
45 » » } | |
46 » » c, err := DialUDP(net, nil, ra) | |
47 » » if err != nil { | |
48 » » » return nil, err | |
49 » » } | |
50 » » return c, nil | |
51 » case "unix", "unixgram", "unixpacket": | |
52 » » var ra *UnixAddr | |
53 » » if ra, err = ResolveUnixAddr(net, raddr); err != nil { | |
54 » » » goto Error | |
55 » » } | |
56 c, err = DialUnix(net, nil, ra) | 58 c, err = DialUnix(net, nil, ra) |
57 » » if err != nil { | 59 » case *IPAddr: |
58 » » » return nil, err | 60 » » c, err = DialIP(net, nil, ra) |
59 » » } | 61 » default: |
60 » » return c, nil | 62 » » err = UnknownNetworkError(net) |
61 » case "ip", "ip4", "ip6": | |
62 » » var ra *IPAddr | |
63 » » if ra, err = ResolveIPAddr(net, raddr); err != nil { | |
64 » » » goto Error | |
65 » » } | |
66 » » c, err := DialIP(net, nil, ra) | |
67 » » if err != nil { | |
68 » » » return nil, err | |
69 » » } | |
70 » » return c, nil | |
71 | |
72 } | 63 } |
73 » err = UnknownNetworkError(net) | 64 » if err == nil { |
74 Error: | 65 » » return |
75 » return nil, &OpError{"dial", net + " " + raddr, nil, err} | 66 » } |
| 67 » return nil, &OpError{"dial", net + " " + addr, nil, err} |
76 } | 68 } |
77 | 69 |
78 // Listen announces on the local network address laddr. | 70 // Listen announces on the local network address laddr. |
79 // The network string net must be a stream-oriented | 71 // The network string net must be a stream-oriented |
80 // network: "tcp", "tcp4", "tcp6", or "unix", or "unixpacket". | 72 // network: "tcp", "tcp4", "tcp6", or "unix", or "unixpacket". |
81 func Listen(net, laddr string) (l Listener, err os.Error) { | 73 func Listen(net, laddr string) (l Listener, err os.Error) { |
82 switch net { | 74 switch net { |
83 case "tcp", "tcp4", "tcp6": | 75 case "tcp", "tcp4", "tcp6": |
84 var la *TCPAddr | 76 var la *TCPAddr |
85 if laddr != "" { | 77 if laddr != "" { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 c, err := ListenIP(net, la) | 144 c, err := ListenIP(net, la) |
153 if err != nil { | 145 if err != nil { |
154 return nil, err | 146 return nil, err |
155 } | 147 } |
156 return c, nil | 148 return c, nil |
157 } | 149 } |
158 } | 150 } |
159 | 151 |
160 return nil, UnknownNetworkError(net) | 152 return nil, UnknownNetworkError(net) |
161 } | 153 } |
OLD | NEW |