Russ Cox | e8a0223 | 2008-09-16 13:42:47 -0700 | [diff] [blame] | 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. |
| 4 | |
Nigel Tao | 6a186d3 | 2011-04-20 09:57:05 +1000 | [diff] [blame] | 5 | // Package net provides a portable interface to Unix networks sockets, |
| 6 | // including TCP/IP, UDP, domain name resolution, and Unix domain sockets. |
Russ Cox | e8a0223 | 2008-09-16 13:42:47 -0700 | [diff] [blame] | 7 | package net |
| 8 | |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 9 | // TODO(rsc): |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 10 | // support for raw ethernet sockets |
Russ Cox | e8a0223 | 2008-09-16 13:42:47 -0700 | [diff] [blame] | 11 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 12 | import "errors" |
Russ Cox | 97bc222 | 2009-05-07 17:36:29 -0700 | [diff] [blame] | 13 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 14 | // Addr represents a network end point address. |
| 15 | type Addr interface { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 16 | Network() string // name of the network |
| 17 | String() string // string form of address |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | // Conn is a generic stream-oriented network connection. |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 21 | type Conn interface { |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 22 | // Read reads data from the connection. |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 23 | // Read can be made to time out and return a net.Error with Timeout() == true |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 24 | // after a fixed time limit; see SetTimeout and SetReadTimeout. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 25 | Read(b []byte) (n int, err error) |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 26 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 27 | // Write writes data to the connection. |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 28 | // Write can be made to time out and return a net.Error with Timeout() == true |
| 29 | // after a fixed time limit; see SetTimeout and SetWriteTimeout. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 30 | Write(b []byte) (n int, err error) |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 31 | |
| 32 | // Close closes the connection. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 33 | Close() error |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 34 | |
Rob Pike | efc4088 | 2009-06-19 16:03:59 -0700 | [diff] [blame] | 35 | // LocalAddr returns the local network address. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 36 | LocalAddr() Addr |
Rob Pike | efc4088 | 2009-06-19 16:03:59 -0700 | [diff] [blame] | 37 | |
| 38 | // RemoteAddr returns the remote network address. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 39 | RemoteAddr() Addr |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 40 | |
| 41 | // SetTimeout sets the read and write deadlines associated |
| 42 | // with the connection. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 43 | SetTimeout(nsec int64) error |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 44 | |
| 45 | // SetReadTimeout sets the time (in nanoseconds) that |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 46 | // Read will wait for data before returning an error with Timeout() == true. |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 47 | // Setting nsec == 0 (the default) disables the deadline. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 48 | SetReadTimeout(nsec int64) error |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 49 | |
| 50 | // SetWriteTimeout sets the time (in nanoseconds) that |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 51 | // Write will wait to send its data before returning an error with Timeout() == true. |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 52 | // Setting nsec == 0 (the default) disables the deadline. |
| 53 | // Even if write times out, it may return n > 0, indicating that |
| 54 | // some of the data was successfully written. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 55 | SetWriteTimeout(nsec int64) error |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 58 | // An Error represents a network error. |
| 59 | type Error interface { |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 60 | error |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 61 | Timeout() bool // Is the error a timeout? |
| 62 | Temporary() bool // Is the error temporary? |
| 63 | } |
| 64 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 65 | // PacketConn is a generic packet-oriented network connection. |
| 66 | type PacketConn interface { |
| 67 | // ReadFrom reads a packet from the connection, |
| 68 | // copying the payload into b. It returns the number of |
| 69 | // bytes copied into b and the return address that |
| 70 | // was on the packet. |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 71 | // ReadFrom can be made to time out and return |
| 72 | // an error with Timeout() == true after a fixed time limit; |
| 73 | // see SetTimeout and SetReadTimeout. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 74 | ReadFrom(b []byte) (n int, addr Addr, err error) |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 75 | |
| 76 | // WriteTo writes a packet with payload b to addr. |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 77 | // WriteTo can be made to time out and return |
| 78 | // an error with Timeout() == true after a fixed time limit; |
| 79 | // see SetTimeout and SetWriteTimeout. |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 80 | // On packet-oriented connections, write timeouts are rare. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 81 | WriteTo(b []byte, addr Addr) (n int, err error) |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 82 | |
| 83 | // Close closes the connection. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 84 | Close() error |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 85 | |
| 86 | // LocalAddr returns the local network address. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 87 | LocalAddr() Addr |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 88 | |
| 89 | // SetTimeout sets the read and write deadlines associated |
| 90 | // with the connection. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 91 | SetTimeout(nsec int64) error |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 92 | |
| 93 | // SetReadTimeout sets the time (in nanoseconds) that |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 94 | // Read will wait for data before returning an error with Timeout() == true. |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 95 | // Setting nsec == 0 (the default) disables the deadline. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 96 | SetReadTimeout(nsec int64) error |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 97 | |
| 98 | // SetWriteTimeout sets the time (in nanoseconds) that |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 99 | // Write will wait to send its data before returning an error with Timeout() == true. |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 100 | // Setting nsec == 0 (the default) disables the deadline. |
| 101 | // Even if write times out, it may return n > 0, indicating that |
| 102 | // some of the data was successfully written. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 103 | SetWriteTimeout(nsec int64) error |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // A Listener is a generic network listener for stream-oriented protocols. |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 107 | type Listener interface { |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 108 | // Accept waits for and returns the next connection to the listener. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 109 | Accept() (c Conn, err error) |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 110 | |
| 111 | // Close closes the listener. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 112 | Close() error |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 113 | |
| 114 | // Addr returns the listener's network address. |
| 115 | Addr() Addr |
Russ Cox | 5d2ee9d | 2009-06-17 21:44:26 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 118 | var errMissingAddress = errors.New("missing address") |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 119 | |
| 120 | type OpError struct { |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 121 | Op string |
| 122 | Net string |
| 123 | Addr Addr |
| 124 | Err error |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 127 | func (e *OpError) Error() string { |
Andrew Gerrand | fc4ba15 | 2010-07-27 17:22:22 +1000 | [diff] [blame] | 128 | if e == nil { |
| 129 | return "<nil>" |
| 130 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 131 | s := e.Op |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 132 | if e.Net != "" { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 133 | s += " " + e.Net |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 134 | } |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 135 | if e.Addr != nil { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 136 | s += " " + e.Addr.String() |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 137 | } |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 138 | s += ": " + e.Err.Error() |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 139 | return s |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 142 | type temporary interface { |
| 143 | Temporary() bool |
| 144 | } |
| 145 | |
| 146 | func (e *OpError) Temporary() bool { |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 147 | t, ok := e.Err.(temporary) |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 148 | return ok && t.Temporary() |
| 149 | } |
| 150 | |
| 151 | type timeout interface { |
| 152 | Timeout() bool |
| 153 | } |
| 154 | |
| 155 | func (e *OpError) Timeout() bool { |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 156 | t, ok := e.Err.(timeout) |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 157 | return ok && t.Timeout() |
| 158 | } |
| 159 | |
Brad Fitzpatrick | 01507b9 | 2011-12-20 14:32:33 -0800 | [diff] [blame^] | 160 | type timeoutError struct{} |
| 161 | |
| 162 | func (e *timeoutError) Error() string { return "i/o timeout" } |
| 163 | func (e *timeoutError) Timeout() bool { return true } |
| 164 | func (e *timeoutError) Temporary() bool { return true } |
| 165 | |
| 166 | var errTimeout error = &timeoutError{} |
| 167 | |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 168 | type AddrError struct { |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 169 | Err string |
| 170 | Addr string |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 173 | func (e *AddrError) Error() string { |
Andrew Gerrand | fc4ba15 | 2010-07-27 17:22:22 +1000 | [diff] [blame] | 174 | if e == nil { |
| 175 | return "<nil>" |
| 176 | } |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 177 | s := e.Err |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 178 | if e.Addr != "" { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 179 | s += " " + e.Addr |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 180 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 181 | return s |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 184 | func (e *AddrError) Temporary() bool { |
| 185 | return false |
| 186 | } |
| 187 | |
| 188 | func (e *AddrError) Timeout() bool { |
| 189 | return false |
| 190 | } |
| 191 | |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 192 | type UnknownNetworkError string |
Robert Griesemer | 5d37705 | 2009-11-04 23:16:46 -0800 | [diff] [blame] | 193 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 194 | func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) } |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 195 | func (e UnknownNetworkError) Temporary() bool { return false } |
| 196 | func (e UnknownNetworkError) Timeout() bool { return false } |