* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / libgo / go / net / net.go
blob72b2b646c48f8214c67b2a9f4cab7abe5743e44b
1 // Copyright 2009 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 /*
6 Package net provides a portable interface for network I/O, including
7 TCP/IP, UDP, domain name resolution, and Unix domain sockets.
9 Although the package provides access to low-level networking
10 primitives, most clients will need only the basic interface provided
11 by the Dial, Listen, and Accept functions and the associated
12 Conn and Listener interfaces. The crypto/tls package uses
13 the same interfaces and similar Dial and Listen functions.
15 The Dial function connects to a server:
17 conn, err := net.Dial("tcp", "google.com:80")
18 if err != nil {
19 // handle error
21 fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
22 status, err := bufio.NewReader(conn).ReadString('\n')
23 // ...
25 The Listen function creates servers:
27 ln, err := net.Listen("tcp", ":8080")
28 if err != nil {
29 // handle error
31 for {
32 conn, err := ln.Accept()
33 if err != nil {
34 // handle error
35 continue
37 go handleConnection(conn)
40 package net
42 // TODO(rsc):
43 // support for raw ethernet sockets
45 import (
46 "errors"
47 "io"
48 "os"
49 "sync"
50 "syscall"
51 "time"
54 // Addr represents a network end point address.
55 type Addr interface {
56 Network() string // name of the network
57 String() string // string form of address
60 // Conn is a generic stream-oriented network connection.
62 // Multiple goroutines may invoke methods on a Conn simultaneously.
63 type Conn interface {
64 // Read reads data from the connection.
65 // Read can be made to time out and return a Error with Timeout() == true
66 // after a fixed time limit; see SetDeadline and SetReadDeadline.
67 Read(b []byte) (n int, err error)
69 // Write writes data to the connection.
70 // Write can be made to time out and return a Error with Timeout() == true
71 // after a fixed time limit; see SetDeadline and SetWriteDeadline.
72 Write(b []byte) (n int, err error)
74 // Close closes the connection.
75 // Any blocked Read or Write operations will be unblocked and return errors.
76 Close() error
78 // LocalAddr returns the local network address.
79 LocalAddr() Addr
81 // RemoteAddr returns the remote network address.
82 RemoteAddr() Addr
84 // SetDeadline sets the read and write deadlines associated
85 // with the connection. It is equivalent to calling both
86 // SetReadDeadline and SetWriteDeadline.
88 // A deadline is an absolute time after which I/O operations
89 // fail with a timeout (see type Error) instead of
90 // blocking. The deadline applies to all future I/O, not just
91 // the immediately following call to Read or Write.
93 // An idle timeout can be implemented by repeatedly extending
94 // the deadline after successful Read or Write calls.
96 // A zero value for t means I/O operations will not time out.
97 SetDeadline(t time.Time) error
99 // SetReadDeadline sets the deadline for future Read calls.
100 // A zero value for t means Read will not time out.
101 SetReadDeadline(t time.Time) error
103 // SetWriteDeadline sets the deadline for future Write calls.
104 // Even if write times out, it may return n > 0, indicating that
105 // some of the data was successfully written.
106 // A zero value for t means Write will not time out.
107 SetWriteDeadline(t time.Time) error
110 type conn struct {
111 fd *netFD
114 func (c *conn) ok() bool { return c != nil && c.fd != nil }
116 // Implementation of the Conn interface.
118 // Read implements the Conn Read method.
119 func (c *conn) Read(b []byte) (int, error) {
120 if !c.ok() {
121 return 0, syscall.EINVAL
123 return c.fd.Read(b)
126 // Write implements the Conn Write method.
127 func (c *conn) Write(b []byte) (int, error) {
128 if !c.ok() {
129 return 0, syscall.EINVAL
131 return c.fd.Write(b)
134 // Close closes the connection.
135 func (c *conn) Close() error {
136 if !c.ok() {
137 return syscall.EINVAL
139 return c.fd.Close()
142 // LocalAddr returns the local network address.
143 func (c *conn) LocalAddr() Addr {
144 if !c.ok() {
145 return nil
147 return c.fd.laddr
150 // RemoteAddr returns the remote network address.
151 func (c *conn) RemoteAddr() Addr {
152 if !c.ok() {
153 return nil
155 return c.fd.raddr
158 // SetDeadline implements the Conn SetDeadline method.
159 func (c *conn) SetDeadline(t time.Time) error {
160 if !c.ok() {
161 return syscall.EINVAL
163 return setDeadline(c.fd, t)
166 // SetReadDeadline implements the Conn SetReadDeadline method.
167 func (c *conn) SetReadDeadline(t time.Time) error {
168 if !c.ok() {
169 return syscall.EINVAL
171 return setReadDeadline(c.fd, t)
174 // SetWriteDeadline implements the Conn SetWriteDeadline method.
175 func (c *conn) SetWriteDeadline(t time.Time) error {
176 if !c.ok() {
177 return syscall.EINVAL
179 return setWriteDeadline(c.fd, t)
182 // SetReadBuffer sets the size of the operating system's
183 // receive buffer associated with the connection.
184 func (c *conn) SetReadBuffer(bytes int) error {
185 if !c.ok() {
186 return syscall.EINVAL
188 return setReadBuffer(c.fd, bytes)
191 // SetWriteBuffer sets the size of the operating system's
192 // transmit buffer associated with the connection.
193 func (c *conn) SetWriteBuffer(bytes int) error {
194 if !c.ok() {
195 return syscall.EINVAL
197 return setWriteBuffer(c.fd, bytes)
200 // File sets the underlying os.File to blocking mode and returns a copy.
201 // It is the caller's responsibility to close f when finished.
202 // Closing c does not affect f, and closing f does not affect c.
204 // The returned os.File's file descriptor is different from the connection's.
205 // Attempting to change properties of the original using this duplicate
206 // may or may not have the desired effect.
207 func (c *conn) File() (f *os.File, err error) { return c.fd.dup() }
209 // An Error represents a network error.
210 type Error interface {
211 error
212 Timeout() bool // Is the error a timeout?
213 Temporary() bool // Is the error temporary?
216 // PacketConn is a generic packet-oriented network connection.
218 // Multiple goroutines may invoke methods on a PacketConn simultaneously.
219 type PacketConn interface {
220 // ReadFrom reads a packet from the connection,
221 // copying the payload into b. It returns the number of
222 // bytes copied into b and the return address that
223 // was on the packet.
224 // ReadFrom can be made to time out and return
225 // an error with Timeout() == true after a fixed time limit;
226 // see SetDeadline and SetReadDeadline.
227 ReadFrom(b []byte) (n int, addr Addr, err error)
229 // WriteTo writes a packet with payload b to addr.
230 // WriteTo can be made to time out and return
231 // an error with Timeout() == true after a fixed time limit;
232 // see SetDeadline and SetWriteDeadline.
233 // On packet-oriented connections, write timeouts are rare.
234 WriteTo(b []byte, addr Addr) (n int, err error)
236 // Close closes the connection.
237 // Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
238 Close() error
240 // LocalAddr returns the local network address.
241 LocalAddr() Addr
243 // SetDeadline sets the read and write deadlines associated
244 // with the connection.
245 SetDeadline(t time.Time) error
247 // SetReadDeadline sets the deadline for future Read calls.
248 // If the deadline is reached, Read will fail with a timeout
249 // (see type Error) instead of blocking.
250 // A zero value for t means Read will not time out.
251 SetReadDeadline(t time.Time) error
253 // SetWriteDeadline sets the deadline for future Write calls.
254 // If the deadline is reached, Write will fail with a timeout
255 // (see type Error) instead of blocking.
256 // A zero value for t means Write will not time out.
257 // Even if write times out, it may return n > 0, indicating that
258 // some of the data was successfully written.
259 SetWriteDeadline(t time.Time) error
262 // A Listener is a generic network listener for stream-oriented protocols.
264 // Multiple goroutines may invoke methods on a Listener simultaneously.
265 type Listener interface {
266 // Accept waits for and returns the next connection to the listener.
267 Accept() (c Conn, err error)
269 // Close closes the listener.
270 // Any blocked Accept operations will be unblocked and return errors.
271 Close() error
273 // Addr returns the listener's network address.
274 Addr() Addr
277 var errMissingAddress = errors.New("missing address")
279 // OpError is the error type usually returned by functions in the net
280 // package. It describes the operation, network type, and address of
281 // an error.
282 type OpError struct {
283 // Op is the operation which caused the error, such as
284 // "read" or "write".
285 Op string
287 // Net is the network type on which this error occurred,
288 // such as "tcp" or "udp6".
289 Net string
291 // Addr is the network address on which this error occurred.
292 Addr Addr
294 // Err is the error that occurred during the operation.
295 Err error
298 func (e *OpError) Error() string {
299 if e == nil {
300 return "<nil>"
302 s := e.Op
303 if e.Net != "" {
304 s += " " + e.Net
306 if e.Addr != nil {
307 s += " " + e.Addr.String()
309 s += ": " + e.Err.Error()
310 return s
313 type temporary interface {
314 Temporary() bool
317 func (e *OpError) Temporary() bool {
318 t, ok := e.Err.(temporary)
319 return ok && t.Temporary()
322 var noDeadline = time.Time{}
324 type timeout interface {
325 Timeout() bool
328 func (e *OpError) Timeout() bool {
329 t, ok := e.Err.(timeout)
330 return ok && t.Timeout()
333 type timeoutError struct{}
335 func (e *timeoutError) Error() string { return "i/o timeout" }
336 func (e *timeoutError) Timeout() bool { return true }
337 func (e *timeoutError) Temporary() bool { return true }
339 var errTimeout error = &timeoutError{}
341 var errClosing = errors.New("use of closed network connection")
343 type AddrError struct {
344 Err string
345 Addr string
348 func (e *AddrError) Error() string {
349 if e == nil {
350 return "<nil>"
352 s := e.Err
353 if e.Addr != "" {
354 s += " " + e.Addr
356 return s
359 func (e *AddrError) Temporary() bool {
360 return false
363 func (e *AddrError) Timeout() bool {
364 return false
367 type UnknownNetworkError string
369 func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) }
370 func (e UnknownNetworkError) Temporary() bool { return false }
371 func (e UnknownNetworkError) Timeout() bool { return false }
373 // DNSConfigError represents an error reading the machine's DNS configuration.
374 type DNSConfigError struct {
375 Err error
378 func (e *DNSConfigError) Error() string {
379 return "error reading DNS config: " + e.Err.Error()
382 func (e *DNSConfigError) Timeout() bool { return false }
383 func (e *DNSConfigError) Temporary() bool { return false }
385 type writerOnly struct {
386 io.Writer
389 // Fallback implementation of io.ReaderFrom's ReadFrom, when sendfile isn't
390 // applicable.
391 func genericReadFrom(w io.Writer, r io.Reader) (n int64, err error) {
392 // Use wrapper to hide existing r.ReadFrom from io.Copy.
393 return io.Copy(writerOnly{w}, r)
396 // deadline is an atomically-accessed number of nanoseconds since 1970
397 // or 0, if no deadline is set.
398 type deadline struct {
399 sync.Mutex
400 val int64
403 func (d *deadline) expired() bool {
404 t := d.value()
405 return t > 0 && time.Now().UnixNano() >= t
408 func (d *deadline) value() (v int64) {
409 d.Lock()
410 v = d.val
411 d.Unlock()
412 return
415 func (d *deadline) set(v int64) {
416 d.Lock()
417 d.val = v
418 d.Unlock()
421 func (d *deadline) setTime(t time.Time) {
422 if t.IsZero() {
423 d.set(0)
424 } else {
425 d.set(t.UnixNano())