Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1241)

Delta Between Two Patch Sets: src/pkg/net/net.go

Issue 684041: code review 684041: net: implement raw sockets (Closed)
Left Patch Set: code review 684041: net: implement raw sockets Created 15 years ago
Right Patch Set: code review 684041: net: implement raw sockets Created 14 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/net/ipsock.go ('k') | src/pkg/net/parse.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 ethernet sockets 11 // support for raw ethernet sockets
12 12
13 import "os" 13 import "os"
14 14
15 // Addr represents a network end point address. 15 // Addr represents a network end point address.
16 type Addr interface { 16 type Addr interface {
17 Network() string // name of the network 17 Network() string // name of the network
18 String() string // string form of address 18 String() string // string form of address
19 } 19 }
20 20
21 // Conn is a generic stream-oriented network connection. 21 // Conn is a generic stream-oriented network connection.
22 type Conn interface { 22 type Conn interface {
23 // Read reads data from the connection. 23 // Read reads data from the connection.
24 » // Read can be made to time out and return err == os.EAGAIN 24 » // Read can be made to time out and return a net.Error with Timeout() == true
25 // after a fixed time limit; see SetTimeout and SetReadTimeout. 25 // after a fixed time limit; see SetTimeout and SetReadTimeout.
26 Read(b []byte) (n int, err os.Error) 26 Read(b []byte) (n int, err os.Error)
27 27
28 // Write writes data to the connection. 28 // Write writes data to the connection.
29 » // Write can be made to time out and return err == os.EAGAIN 29 » // Write can be made to time out and return a net.Error with Timeout() = = true
30 » // after a fixed time limit; see SetTimeout and SetReadTimeout. 30 » // after a fixed time limit; see SetTimeout and SetWriteTimeout.
31 Write(b []byte) (n int, err os.Error) 31 Write(b []byte) (n int, err os.Error)
32 32
33 // Close closes the connection. 33 // Close closes the connection.
34 // The error returned is an os.Error to satisfy io.Closer;
34 Close() os.Error 35 Close() os.Error
35 36
36 // LocalAddr returns the local network address. 37 // LocalAddr returns the local network address.
37 LocalAddr() Addr 38 LocalAddr() Addr
38 39
39 // RemoteAddr returns the remote network address. 40 // RemoteAddr returns the remote network address.
40 RemoteAddr() Addr 41 RemoteAddr() Addr
41 42
42 // SetTimeout sets the read and write deadlines associated 43 // SetTimeout sets the read and write deadlines associated
43 // with the connection. 44 // with the connection.
44 SetTimeout(nsec int64) os.Error 45 SetTimeout(nsec int64) os.Error
45 46
46 // SetReadTimeout sets the time (in nanoseconds) that 47 // SetReadTimeout sets the time (in nanoseconds) that
47 » // Read will wait for data before returning os.EAGAIN. 48 » // Read will wait for data before returning an error with Timeout() == t rue.
48 // Setting nsec == 0 (the default) disables the deadline. 49 // Setting nsec == 0 (the default) disables the deadline.
49 SetReadTimeout(nsec int64) os.Error 50 SetReadTimeout(nsec int64) os.Error
50 51
51 // SetWriteTimeout sets the time (in nanoseconds) that 52 // SetWriteTimeout sets the time (in nanoseconds) that
52 » // Write will wait to send its data before returning os.EAGAIN. 53 » // Write will wait to send its data before returning an error with Timeo ut() == true.
53 // Setting nsec == 0 (the default) disables the deadline. 54 // Setting nsec == 0 (the default) disables the deadline.
54 // Even if write times out, it may return n > 0, indicating that 55 // Even if write times out, it may return n > 0, indicating that
55 // some of the data was successfully written. 56 // some of the data was successfully written.
56 SetWriteTimeout(nsec int64) os.Error 57 SetWriteTimeout(nsec int64) os.Error
58 }
59
60 // An Error represents a network error.
61 type Error interface {
62 os.Error
63 Timeout() bool // Is the error a timeout?
64 Temporary() bool // Is the error temporary?
57 } 65 }
58 66
59 // PacketConn is a generic packet-oriented network connection. 67 // PacketConn is a generic packet-oriented network connection.
60 type PacketConn interface { 68 type PacketConn interface {
61 // ReadFrom reads a packet from the connection, 69 // ReadFrom reads a packet from the connection,
62 // copying the payload into b. It returns the number of 70 // copying the payload into b. It returns the number of
63 // bytes copied into b and the return address that 71 // bytes copied into b and the return address that
64 // was on the packet. 72 // was on the packet.
65 » // ReadFrom can be made to time out and return err == os.EAGAIN 73 » // ReadFrom can be made to time out and return
66 » // after a fixed time limit; see SetTimeout and SetReadTimeout. 74 » // an error with Timeout() == true after a fixed time limit;
75 » // see SetTimeout and SetReadTimeout.
67 ReadFrom(b []byte) (n int, addr Addr, err os.Error) 76 ReadFrom(b []byte) (n int, addr Addr, err os.Error)
68 77
69 // WriteTo writes a packet with payload b to addr. 78 // WriteTo writes a packet with payload b to addr.
70 » // WriteTo can be made to time out and return err == os.EAGAIN 79 » // WriteTo can be made to time out and return
71 » // after a fixed time limit; see SetTimeout and SetWriteTimeout. 80 » // an error with Timeout() == true after a fixed time limit;
81 » // see SetTimeout and SetWriteTimeout.
72 // On packet-oriented connections, write timeouts are rare. 82 // On packet-oriented connections, write timeouts are rare.
73 WriteTo(b []byte, addr Addr) (n int, err os.Error) 83 WriteTo(b []byte, addr Addr) (n int, err os.Error)
74 84
75 // Close closes the connection. 85 // Close closes the connection.
86 // The error returned is an os.Error to satisfy io.Closer;
76 Close() os.Error 87 Close() os.Error
77 88
78 // LocalAddr returns the local network address. 89 // LocalAddr returns the local network address.
79 LocalAddr() Addr 90 LocalAddr() Addr
80 91
81 // SetTimeout sets the read and write deadlines associated 92 // SetTimeout sets the read and write deadlines associated
82 // with the connection. 93 // with the connection.
83 SetTimeout(nsec int64) os.Error 94 SetTimeout(nsec int64) os.Error
84 95
85 // SetReadTimeout sets the time (in nanoseconds) that 96 // SetReadTimeout sets the time (in nanoseconds) that
86 » // Read will wait for data before returning os.EAGAIN. 97 » // Read will wait for data before returning an error with Timeout() == t rue.
87 // Setting nsec == 0 (the default) disables the deadline. 98 // Setting nsec == 0 (the default) disables the deadline.
88 SetReadTimeout(nsec int64) os.Error 99 SetReadTimeout(nsec int64) os.Error
89 100
90 // SetWriteTimeout sets the time (in nanoseconds) that 101 // SetWriteTimeout sets the time (in nanoseconds) that
91 » // Write will wait to send its data before returning os.EAGAIN. 102 » // Write will wait to send its data before returning an error with Timeo ut() == true.
92 // Setting nsec == 0 (the default) disables the deadline. 103 // Setting nsec == 0 (the default) disables the deadline.
93 // Even if write times out, it may return n > 0, indicating that 104 // Even if write times out, it may return n > 0, indicating that
94 // some of the data was successfully written. 105 // some of the data was successfully written.
95 SetWriteTimeout(nsec int64) os.Error 106 SetWriteTimeout(nsec int64) os.Error
96 } 107 }
97 108
98 // A Listener is a generic network listener for stream-oriented protocols. 109 // A Listener is a generic network listener for stream-oriented protocols.
99 // Accept waits for the next connection and Close closes the connection.
100 type Listener interface { 110 type Listener interface {
111 // Accept waits for and returns the next connection to the listener.
101 Accept() (c Conn, err os.Error) 112 Accept() (c Conn, err os.Error)
113
114 // Close closes the listener.
115 // The error returned is an os.Error to satisfy io.Closer;
102 Close() os.Error 116 Close() os.Error
103 » Addr() Addr // Listener's network address 117
118 » // Addr returns the listener's network address.
119 » Addr() Addr
104 } 120 }
105 121
106 // Dial connects to the remote address raddr on the network net. 122 // Dial connects to the remote address raddr on the network net.
107 // If the string laddr is not empty, it is used as the local address 123 // If the string laddr is not empty, it is used as the local address
108 // for the connection. 124 // for the connection.
109 // 125 //
110 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), 126 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
111 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ipv4" 127 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
112 // (IPv4-only) and "ipv6" IPv6-only). 128 // (IPv4-only) and "ip6" IPv6-only).
113 // 129 //
114 // For IP networks, addresses have the form host:port. If host is 130 // For IP networks, addresses have the form host:port. If host is
115 // a literal IPv6 address, it must be enclosed in square brackets. 131 // a literal IPv6 address, it must be enclosed in square brackets.
116 // 132 //
117 // Examples: 133 // Examples:
118 // Dial("tcp", "", "12.34.56.78:80") 134 // Dial("tcp", "", "12.34.56.78:80")
119 // Dial("tcp", "", "google.com:80") 135 // Dial("tcp", "", "google.com:80")
120 // Dial("tcp", "", "[de:ad:be:ef::ca:fe]:80") 136 // Dial("tcp", "", "[de:ad:be:ef::ca:fe]:80")
121 // Dial("tcp", "127.0.0.1:123", "127.0.0.1:88") 137 // Dial("tcp", "127.0.0.1:123", "127.0.0.1:88")
122 // 138 //
123 func Dial(net, laddr, raddr string) (c Conn, err os.Error) { 139 func Dial(net, laddr, raddr string) (c Conn, err os.Error) {
124 » switch net { 140 » switch prefixBefore(net, ':') {
125 case "tcp", "tcp4", "tcp6": 141 case "tcp", "tcp4", "tcp6":
126 var la, ra *TCPAddr 142 var la, ra *TCPAddr
127 if laddr != "" { 143 if laddr != "" {
128 if la, err = ResolveTCPAddr(laddr); err != nil { 144 if la, err = ResolveTCPAddr(laddr); err != nil {
129 goto Error 145 goto Error
130 } 146 }
131 } 147 }
132 if raddr != "" { 148 if raddr != "" {
133 if ra, err = ResolveTCPAddr(raddr); err != nil { 149 if ra, err = ResolveTCPAddr(raddr); err != nil {
134 goto Error 150 goto Error
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 if laddr != "" { 182 if laddr != "" {
167 if la, err = ResolveUnixAddr(net, laddr); err != nil { 183 if la, err = ResolveUnixAddr(net, laddr); err != nil {
168 goto Error 184 goto Error
169 } 185 }
170 } 186 }
171 c, err = DialUnix(net, la, ra) 187 c, err = DialUnix(net, la, ra)
172 if err != nil { 188 if err != nil {
173 return nil, err 189 return nil, err
174 } 190 }
175 return c, nil 191 return c, nil
176 » case "ip", "ipv4", "ipv6": 192 » case "ip", "ip4", "ip6":
177 var la, ra *IPAddr 193 var la, ra *IPAddr
178 if laddr != "" { 194 if laddr != "" {
179 if la, err = ResolveIPAddr(laddr); err != nil { 195 if la, err = ResolveIPAddr(laddr); err != nil {
180 goto Error 196 goto Error
181 } 197 }
182 } 198 }
183 if raddr != "" { 199 if raddr != "" {
184 if ra, err = ResolveIPAddr(raddr); err != nil { 200 if ra, err = ResolveIPAddr(raddr); err != nil {
185 goto Error 201 goto Error
186 } 202 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 243 }
228 return l, nil 244 return l, nil
229 } 245 }
230 return nil, UnknownNetworkError(net) 246 return nil, UnknownNetworkError(net)
231 } 247 }
232 248
233 // ListenPacket announces on the local network address laddr. 249 // ListenPacket announces on the local network address laddr.
234 // The network string net must be a packet-oriented network: 250 // The network string net must be a packet-oriented network:
235 // "udp", "udp4", "udp6", or "unixgram". 251 // "udp", "udp4", "udp6", or "unixgram".
236 func ListenPacket(net, laddr string) (c PacketConn, err os.Error) { 252 func ListenPacket(net, laddr string) (c PacketConn, err os.Error) {
237 » switch net { 253 » switch prefixBefore(net, ':') {
238 case "udp", "udp4", "udp6": 254 case "udp", "udp4", "udp6":
239 var la *UDPAddr 255 var la *UDPAddr
240 if laddr != "" { 256 if laddr != "" {
241 if la, err = ResolveUDPAddr(laddr); err != nil { 257 if la, err = ResolveUDPAddr(laddr); err != nil {
242 return nil, err 258 return nil, err
243 } 259 }
244 } 260 }
245 c, err := ListenUDP(net, la) 261 c, err := ListenUDP(net, la)
246 if err != nil { 262 if err != nil {
247 return nil, err 263 return nil, err
248 } 264 }
249 return c, nil 265 return c, nil
250 case "unixgram": 266 case "unixgram":
251 var la *UnixAddr 267 var la *UnixAddr
252 if laddr != "" { 268 if laddr != "" {
253 if la, err = ResolveUnixAddr(net, laddr); err != nil { 269 if la, err = ResolveUnixAddr(net, laddr); err != nil {
254 return nil, err 270 return nil, err
255 } 271 }
256 } 272 }
257 c, err := DialUnix(net, la, nil) 273 c, err := DialUnix(net, la, nil)
258 if err != nil { 274 if err != nil {
259 return nil, err 275 return nil, err
260 } 276 }
261 return c, nil 277 return c, nil
262 » case "ip", "ipv4", "ipv6": 278 » case "ip", "ip4", "ip6":
263 var la *IPAddr 279 var la *IPAddr
264 if laddr != "" { 280 if laddr != "" {
265 if la, err = ResolveIPAddr(laddr); err != nil { 281 if la, err = ResolveIPAddr(laddr); err != nil {
266 return nil, err 282 return nil, err
267 } 283 }
268 } 284 }
269 c, err := ListenIP(net, la) 285 c, err := ListenIP(net, la)
270 if err != nil { 286 if err != nil {
271 return nil, err 287 return nil, err
272 } 288 }
(...skipping 16 matching lines...) Expand all
289 if e.Net != "" { 305 if e.Net != "" {
290 s += " " + e.Net 306 s += " " + e.Net
291 } 307 }
292 if e.Addr != nil { 308 if e.Addr != nil {
293 s += " " + e.Addr.String() 309 s += " " + e.Addr.String()
294 } 310 }
295 s += ": " + e.Error.String() 311 s += ": " + e.Error.String()
296 return s 312 return s
297 } 313 }
298 314
315 type temporary interface {
316 Temporary() bool
317 }
318
319 func (e *OpError) Temporary() bool {
320 t, ok := e.Error.(temporary)
321 return ok && t.Temporary()
322 }
323
324 type timeout interface {
325 Timeout() bool
326 }
327
328 func (e *OpError) Timeout() bool {
329 t, ok := e.Error.(timeout)
330 return ok && t.Timeout()
331 }
332
299 type AddrError struct { 333 type AddrError struct {
300 Error string 334 Error string
301 Addr string 335 Addr string
302 } 336 }
303 337
304 func (e *AddrError) String() string { 338 func (e *AddrError) String() string {
305 s := e.Error 339 s := e.Error
306 if e.Addr != "" { 340 if e.Addr != "" {
307 s += " " + e.Addr 341 s += " " + e.Addr
308 } 342 }
309 return s 343 return s
310 } 344 }
311 345
346 func (e *AddrError) Temporary() bool {
347 return false
348 }
349
350 func (e *AddrError) Timeout() bool {
351 return false
352 }
353
312 type UnknownNetworkError string 354 type UnknownNetworkError string
313 355
314 func (e UnknownNetworkError) String() string { return "unknown network " + strin g(e) } 356 func (e UnknownNetworkError) String() string { return "unknown network " + stri ng(e) }
357 func (e UnknownNetworkError) Temporary() bool { return false }
358 func (e UnknownNetworkError) Timeout() bool { return false }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b