OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 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 // The net package provides a portable interface to Unix | 5 // The net package provides a portable interface to Unix |
6 // networks sockets, including TCP/IP, UDP, domain name | 6 // networks sockets, including TCP/IP, UDP, domain name |
7 // resolution, and Unix domain sockets. | 7 // resolution, and Unix domain sockets. |
8 package net | 8 package net |
9 | 9 |
10 // TODO(rsc): | 10 // TODO(rsc): |
11 // support for raw IP sockets | |
12 // support for raw ethernet sockets | 11 // support for raw ethernet sockets |
13 | 12 |
14 import "os" | 13 import "os" |
15 | 14 |
16 // Addr represents a network end point address. | 15 // Addr represents a network end point address. |
17 type Addr interface { | 16 type Addr interface { |
18 Network() string // name of the network | 17 Network() string // name of the network |
19 String() string // string form of address | 18 String() string // string form of address |
20 } | 19 } |
21 | 20 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 Accept() (c Conn, err os.Error) | 101 Accept() (c Conn, err os.Error) |
103 Close() os.Error | 102 Close() os.Error |
104 Addr() Addr // Listener's network address | 103 Addr() Addr // Listener's network address |
105 } | 104 } |
106 | 105 |
107 // Dial connects to the remote address raddr on the network net. | 106 // Dial connects to the remote address raddr on the network net. |
108 // If the string laddr is not empty, it is used as the local address | 107 // If the string laddr is not empty, it is used as the local address |
109 // for the connection. | 108 // for the connection. |
110 // | 109 // |
111 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), | 110 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), |
112 // "udp", "udp4" (IPv4-only), and "udp6" (IPv6-only). | 111 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" |
| 112 // (IPv4-only) and "ip6" IPv6-only). |
113 // | 113 // |
114 // For IP networks, addresses have the form host:port. If host is | 114 // For IP networks, addresses have the form host:port. If host is |
115 // a literal IPv6 address, it must be enclosed in square brackets. | 115 // a literal IPv6 address, it must be enclosed in square brackets. |
116 // | 116 // |
117 // Examples: | 117 // Examples: |
118 // Dial("tcp", "", "12.34.56.78:80") | 118 // Dial("tcp", "", "12.34.56.78:80") |
119 // Dial("tcp", "", "google.com:80") | 119 // Dial("tcp", "", "google.com:80") |
120 // Dial("tcp", "", "[de:ad:be:ef::ca:fe]:80") | 120 // Dial("tcp", "", "[de:ad:be:ef::ca:fe]:80") |
121 // Dial("tcp", "127.0.0.1:123", "127.0.0.1:88") | 121 // Dial("tcp", "127.0.0.1:123", "127.0.0.1:88") |
122 // | 122 // |
123 func Dial(net, laddr, raddr string) (c Conn, err os.Error) { | 123 func Dial(net, laddr, raddr string) (c Conn, err os.Error) { |
124 » switch net { | 124 » switch prefixBefore(net, ':') { |
125 case "tcp", "tcp4", "tcp6": | 125 case "tcp", "tcp4", "tcp6": |
126 var la, ra *TCPAddr | 126 var la, ra *TCPAddr |
127 if laddr != "" { | 127 if laddr != "" { |
128 if la, err = ResolveTCPAddr(laddr); err != nil { | 128 if la, err = ResolveTCPAddr(laddr); err != nil { |
129 goto Error | 129 goto Error |
130 } | 130 } |
131 } | 131 } |
132 if raddr != "" { | 132 if raddr != "" { |
133 if ra, err = ResolveTCPAddr(raddr); err != nil { | 133 if ra, err = ResolveTCPAddr(raddr); err != nil { |
134 goto Error | 134 goto Error |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 if laddr != "" { | 166 if laddr != "" { |
167 if la, err = ResolveUnixAddr(net, laddr); err != nil { | 167 if la, err = ResolveUnixAddr(net, laddr); err != nil { |
168 goto Error | 168 goto Error |
169 } | 169 } |
170 } | 170 } |
171 c, err = DialUnix(net, la, ra) | 171 c, err = DialUnix(net, la, ra) |
172 if err != nil { | 172 if err != nil { |
173 return nil, err | 173 return nil, err |
174 } | 174 } |
175 return c, nil | 175 return c, nil |
| 176 case "ip", "ip4", "ip6": |
| 177 var la, ra *IPAddr |
| 178 if laddr != "" { |
| 179 if la, err = ResolveIPAddr(laddr); err != nil { |
| 180 goto Error |
| 181 } |
| 182 } |
| 183 if raddr != "" { |
| 184 if ra, err = ResolveIPAddr(raddr); err != nil { |
| 185 goto Error |
| 186 } |
| 187 } |
| 188 c, err := DialIP(net, la, ra) |
| 189 if err != nil { |
| 190 return nil, err |
| 191 } |
| 192 return c, nil |
| 193 |
176 } | 194 } |
177 err = UnknownNetworkError(net) | 195 err = UnknownNetworkError(net) |
178 Error: | 196 Error: |
179 return nil, &OpError{"dial", net + " " + raddr, nil, err} | 197 return nil, &OpError{"dial", net + " " + raddr, nil, err} |
180 } | 198 } |
181 | 199 |
182 // Listen announces on the local network address laddr. | 200 // Listen announces on the local network address laddr. |
183 // The network string net must be a stream-oriented | 201 // The network string net must be a stream-oriented |
184 // network: "tcp", "tcp4", "tcp6", or "unix". | 202 // network: "tcp", "tcp4", "tcp6", or "unix". |
185 func Listen(net, laddr string) (l Listener, err os.Error) { | 203 func Listen(net, laddr string) (l Listener, err os.Error) { |
(...skipping 23 matching lines...) Expand all Loading... |
209 } | 227 } |
210 return l, nil | 228 return l, nil |
211 } | 229 } |
212 return nil, UnknownNetworkError(net) | 230 return nil, UnknownNetworkError(net) |
213 } | 231 } |
214 | 232 |
215 // ListenPacket announces on the local network address laddr. | 233 // ListenPacket announces on the local network address laddr. |
216 // The network string net must be a packet-oriented network: | 234 // The network string net must be a packet-oriented network: |
217 // "udp", "udp4", "udp6", or "unixgram". | 235 // "udp", "udp4", "udp6", or "unixgram". |
218 func ListenPacket(net, laddr string) (c PacketConn, err os.Error) { | 236 func ListenPacket(net, laddr string) (c PacketConn, err os.Error) { |
219 » switch net { | 237 » switch prefixBefore(net, ':') { |
220 case "udp", "udp4", "udp6": | 238 case "udp", "udp4", "udp6": |
221 var la *UDPAddr | 239 var la *UDPAddr |
222 if laddr != "" { | 240 if laddr != "" { |
223 if la, err = ResolveUDPAddr(laddr); err != nil { | 241 if la, err = ResolveUDPAddr(laddr); err != nil { |
224 return nil, err | 242 return nil, err |
225 } | 243 } |
226 } | 244 } |
227 c, err := ListenUDP(net, la) | 245 c, err := ListenUDP(net, la) |
228 if err != nil { | 246 if err != nil { |
229 return nil, err | 247 return nil, err |
230 } | 248 } |
231 return c, nil | 249 return c, nil |
232 case "unixgram": | 250 case "unixgram": |
233 var la *UnixAddr | 251 var la *UnixAddr |
234 if laddr != "" { | 252 if laddr != "" { |
235 if la, err = ResolveUnixAddr(net, laddr); err != nil { | 253 if la, err = ResolveUnixAddr(net, laddr); err != nil { |
236 return nil, err | 254 return nil, err |
237 } | 255 } |
238 } | 256 } |
239 c, err := DialUnix(net, la, nil) | 257 c, err := DialUnix(net, la, nil) |
240 if err != nil { | 258 if err != nil { |
241 return nil, err | 259 return nil, err |
242 } | 260 } |
243 return c, nil | 261 return c, nil |
| 262 case "ip", "ip4", "ip6": |
| 263 var la *IPAddr |
| 264 if laddr != "" { |
| 265 if la, err = ResolveIPAddr(laddr); err != nil { |
| 266 return nil, err |
| 267 } |
| 268 } |
| 269 c, err := ListenIP(net, la) |
| 270 if err != nil { |
| 271 return nil, err |
| 272 } |
| 273 return c, nil |
244 } | 274 } |
245 return nil, UnknownNetworkError(net) | 275 return nil, UnknownNetworkError(net) |
246 } | 276 } |
247 | 277 |
248 var errMissingAddress = os.ErrorString("missing address") | 278 var errMissingAddress = os.ErrorString("missing address") |
249 | 279 |
250 type OpError struct { | 280 type OpError struct { |
251 Op string | 281 Op string |
252 Net string | 282 Net string |
253 Addr Addr | 283 Addr Addr |
(...skipping 21 matching lines...) Expand all Loading... |
275 s := e.Error | 305 s := e.Error |
276 if e.Addr != "" { | 306 if e.Addr != "" { |
277 s += " " + e.Addr | 307 s += " " + e.Addr |
278 } | 308 } |
279 return s | 309 return s |
280 } | 310 } |
281 | 311 |
282 type UnknownNetworkError string | 312 type UnknownNetworkError string |
283 | 313 |
284 func (e UnknownNetworkError) String() string { return "unknown network " + strin
g(e) } | 314 func (e UnknownNetworkError) String() string { return "unknown network " + strin
g(e) } |
OLD | NEW |