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 | |
Brad Fitzpatrick | 008e64d | 2012-02-17 13:07:06 +1100 | [diff] [blame] | 5 | /* |
| 6 | Package net provides a portable interface for network I/O, including |
| 7 | TCP/IP, UDP, domain name resolution, and Unix domain sockets. |
| 8 | |
| 9 | Although the package provides access to low-level networking |
Brad Fitzpatrick | 8729d15 | 2012-02-21 11:11:18 +1100 | [diff] [blame] | 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. |
Brad Fitzpatrick | 008e64d | 2012-02-17 13:07:06 +1100 | [diff] [blame] | 14 | |
| 15 | The Dial function connects to a server: |
| 16 | |
Mikio Hara | 495e3c6 | 2016-05-17 12:20:16 +0900 | [diff] [blame] | 17 | conn, err := net.Dial("tcp", "golang.org:80") |
Brad Fitzpatrick | 008e64d | 2012-02-17 13:07:06 +1100 | [diff] [blame] | 18 | if err != nil { |
| 19 | // handle error |
| 20 | } |
| 21 | fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") |
| 22 | status, err := bufio.NewReader(conn).ReadString('\n') |
| 23 | // ... |
| 24 | |
| 25 | The Listen function creates servers: |
| 26 | |
| 27 | ln, err := net.Listen("tcp", ":8080") |
| 28 | if err != nil { |
| 29 | // handle error |
| 30 | } |
| 31 | for { |
| 32 | conn, err := ln.Accept() |
| 33 | if err != nil { |
| 34 | // handle error |
Brad Fitzpatrick | 008e64d | 2012-02-17 13:07:06 +1100 | [diff] [blame] | 35 | } |
| 36 | go handleConnection(conn) |
| 37 | } |
Russ Cox | b711f5a | 2015-08-18 23:19:41 -0400 | [diff] [blame] | 38 | |
| 39 | Name Resolution |
| 40 | |
| 41 | The method for resolving domain names, whether indirectly with functions like Dial |
| 42 | or directly with functions like LookupHost and LookupAddr, varies by operating system. |
| 43 | |
| 44 | On Unix systems, the resolver has two options for resolving names. |
| 45 | It can use a pure Go resolver that sends DNS requests directly to the servers |
| 46 | listed in /etc/resolv.conf, or it can use a cgo-based resolver that calls C |
| 47 | library routines such as getaddrinfo and getnameinfo. |
| 48 | |
| 49 | By default the pure Go resolver is used, because a blocked DNS request consumes |
| 50 | only a goroutine, while a blocked C call consumes an operating system thread. |
| 51 | When cgo is available, the cgo-based resolver is used instead under a variety of |
| 52 | conditions: on systems that do not let programs make direct DNS requests (OS X), |
| 53 | when the LOCALDOMAIN environment variable is present (even if empty), |
| 54 | when the RES_OPTIONS or HOSTALIASES environment variable is non-empty, |
| 55 | when the ASR_CONFIG environment variable is non-empty (OpenBSD only), |
| 56 | when /etc/resolv.conf or /etc/nsswitch.conf specify the use of features that the |
| 57 | Go resolver does not implement, and when the name being looked up ends in .local |
| 58 | or is an mDNS name. |
| 59 | |
| 60 | The resolver decision can be overridden by setting the netdns value of the |
| 61 | GODEBUG environment variable (see package runtime) to go or cgo, as in: |
| 62 | |
| 63 | export GODEBUG=netdns=go # force pure Go resolver |
| 64 | export GODEBUG=netdns=cgo # force cgo resolver |
| 65 | |
| 66 | The decision can also be forced while building the Go source tree |
| 67 | by setting the netgo or netcgo build tag. |
| 68 | |
| 69 | A numeric netdns setting, as in GODEBUG=netdns=1, causes the resolver |
| 70 | to print debugging information about its decisions. |
| 71 | To force a particular resolver while also printing debugging information, |
| 72 | join the two settings by a plus sign, as in GODEBUG=netdns=go+1. |
| 73 | |
| 74 | On Plan 9, the resolver always accesses /net/cs and /net/dns. |
| 75 | |
| 76 | On Windows, the resolver always uses C library functions, such as GetAddrInfo and DnsQuery. |
| 77 | |
Brad Fitzpatrick | 008e64d | 2012-02-17 13:07:06 +1100 | [diff] [blame] | 78 | */ |
Russ Cox | e8a0223 | 2008-09-16 13:42:47 -0700 | [diff] [blame] | 79 | package net |
| 80 | |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 81 | import ( |
Brad Fitzpatrick | b6b4004 | 2016-04-14 17:47:25 -0700 | [diff] [blame] | 82 | "context" |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 83 | "errors" |
Anthony Martin | 253ed02 | 2012-11-30 11:41:50 -0800 | [diff] [blame] | 84 | "io" |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 85 | "os" |
| 86 | "syscall" |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 87 | "time" |
| 88 | ) |
Russ Cox | 97bc222 | 2009-05-07 17:36:29 -0700 | [diff] [blame] | 89 | |
Brad Fitzpatrick | b615ad8 | 2015-06-25 12:52:54 +0200 | [diff] [blame] | 90 | // netGo and netCgo contain the state of the build tags used |
| 91 | // to build this binary, and whether cgo is available. |
| 92 | // conf.go mirrors these into conf for easier testing. |
| 93 | var ( |
| 94 | netGo bool // set true in cgo_stub.go for build tag "netgo" (or no cgo) |
| 95 | netCgo bool // set true in conf_netcgo.go for build tag "netcgo" |
| 96 | ) |
| 97 | |
Mikio Hara | 3a9024b | 2015-04-01 22:46:12 +0900 | [diff] [blame] | 98 | func init() { |
| 99 | sysInit() |
| 100 | supportsIPv4 = probeIPv4Stack() |
| 101 | supportsIPv6, supportsIPv4map = probeIPv6Stack() |
| 102 | } |
| 103 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 104 | // Addr represents a network end point address. |
| 105 | type Addr interface { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 106 | Network() string // name of the network |
| 107 | String() string // string form of address |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Conn is a generic stream-oriented network connection. |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 111 | // |
| 112 | // Multiple goroutines may invoke methods on a Conn simultaneously. |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 113 | type Conn interface { |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 114 | // Read reads data from the connection. |
Mikio Hara | 3d400db | 2012-01-29 19:11:05 +0900 | [diff] [blame] | 115 | // Read can be made to time out and return a Error with Timeout() == true |
Mikio Hara | 2356e43 | 2012-01-19 12:23:30 +0900 | [diff] [blame] | 116 | // after a fixed time limit; see SetDeadline and SetReadDeadline. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 117 | Read(b []byte) (n int, err error) |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 118 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 119 | // Write writes data to the connection. |
Mikio Hara | 3d400db | 2012-01-29 19:11:05 +0900 | [diff] [blame] | 120 | // Write can be made to time out and return a Error with Timeout() == true |
Mikio Hara | 2356e43 | 2012-01-19 12:23:30 +0900 | [diff] [blame] | 121 | // after a fixed time limit; see SetDeadline and SetWriteDeadline. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 122 | Write(b []byte) (n int, err error) |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 123 | |
| 124 | // Close closes the connection. |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 125 | // Any blocked Read or Write operations will be unblocked and return errors. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 126 | Close() error |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 127 | |
Rob Pike | efc4088 | 2009-06-19 16:03:59 -0700 | [diff] [blame] | 128 | // LocalAddr returns the local network address. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 129 | LocalAddr() Addr |
Rob Pike | efc4088 | 2009-06-19 16:03:59 -0700 | [diff] [blame] | 130 | |
| 131 | // RemoteAddr returns the remote network address. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 132 | RemoteAddr() Addr |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 133 | |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 134 | // SetDeadline sets the read and write deadlines associated |
Brad Fitzpatrick | 8729d15 | 2012-02-21 11:11:18 +1100 | [diff] [blame] | 135 | // with the connection. It is equivalent to calling both |
| 136 | // SetReadDeadline and SetWriteDeadline. |
| 137 | // |
| 138 | // A deadline is an absolute time after which I/O operations |
| 139 | // fail with a timeout (see type Error) instead of |
| 140 | // blocking. The deadline applies to all future I/O, not just |
| 141 | // the immediately following call to Read or Write. |
| 142 | // |
| 143 | // An idle timeout can be implemented by repeatedly extending |
| 144 | // the deadline after successful Read or Write calls. |
| 145 | // |
| 146 | // A zero value for t means I/O operations will not time out. |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 147 | SetDeadline(t time.Time) error |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 148 | |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 149 | // SetReadDeadline sets the deadline for future Read calls. |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 150 | // A zero value for t means Read will not time out. |
| 151 | SetReadDeadline(t time.Time) error |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 152 | |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 153 | // SetWriteDeadline sets the deadline for future Write calls. |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 154 | // Even if write times out, it may return n > 0, indicating that |
| 155 | // some of the data was successfully written. |
Brad Fitzpatrick | 8729d15 | 2012-02-21 11:11:18 +1100 | [diff] [blame] | 156 | // A zero value for t means Write will not time out. |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 157 | SetWriteDeadline(t time.Time) error |
Russ Cox | cc1d4b7 | 2009-05-13 18:03:41 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 160 | type conn struct { |
| 161 | fd *netFD |
| 162 | } |
| 163 | |
| 164 | func (c *conn) ok() bool { return c != nil && c.fd != nil } |
| 165 | |
| 166 | // Implementation of the Conn interface. |
| 167 | |
| 168 | // Read implements the Conn Read method. |
| 169 | func (c *conn) Read(b []byte) (int, error) { |
| 170 | if !c.ok() { |
| 171 | return 0, syscall.EINVAL |
| 172 | } |
Mikio Hara | ec11444 | 2015-04-16 23:10:56 +0900 | [diff] [blame] | 173 | n, err := c.fd.Read(b) |
| 174 | if err != nil && err != io.EOF { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 175 | err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} |
Mikio Hara | ec11444 | 2015-04-16 23:10:56 +0900 | [diff] [blame] | 176 | } |
| 177 | return n, err |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Write implements the Conn Write method. |
| 181 | func (c *conn) Write(b []byte) (int, error) { |
| 182 | if !c.ok() { |
| 183 | return 0, syscall.EINVAL |
| 184 | } |
Mikio Hara | 11b5f98 | 2015-04-16 11:26:44 +0900 | [diff] [blame] | 185 | n, err := c.fd.Write(b) |
| 186 | if err != nil { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 187 | err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} |
Mikio Hara | 11b5f98 | 2015-04-16 11:26:44 +0900 | [diff] [blame] | 188 | } |
| 189 | return n, err |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Close closes the connection. |
| 193 | func (c *conn) Close() error { |
| 194 | if !c.ok() { |
| 195 | return syscall.EINVAL |
| 196 | } |
Mikio Hara | 310db63 | 2015-04-17 12:24:42 +0900 | [diff] [blame] | 197 | err := c.fd.Close() |
| 198 | if err != nil { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 199 | err = &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} |
Mikio Hara | 310db63 | 2015-04-17 12:24:42 +0900 | [diff] [blame] | 200 | } |
| 201 | return err |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // LocalAddr returns the local network address. |
Shenghou Ma | 7e43aee | 2015-02-03 12:59:40 -0500 | [diff] [blame] | 205 | // The Addr returned is shared by all invocations of LocalAddr, so |
| 206 | // do not modify it. |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 207 | func (c *conn) LocalAddr() Addr { |
| 208 | if !c.ok() { |
| 209 | return nil |
| 210 | } |
| 211 | return c.fd.laddr |
| 212 | } |
| 213 | |
| 214 | // RemoteAddr returns the remote network address. |
Shenghou Ma | 7e43aee | 2015-02-03 12:59:40 -0500 | [diff] [blame] | 215 | // The Addr returned is shared by all invocations of RemoteAddr, so |
| 216 | // do not modify it. |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 217 | func (c *conn) RemoteAddr() Addr { |
| 218 | if !c.ok() { |
| 219 | return nil |
| 220 | } |
| 221 | return c.fd.raddr |
| 222 | } |
| 223 | |
| 224 | // SetDeadline implements the Conn SetDeadline method. |
| 225 | func (c *conn) SetDeadline(t time.Time) error { |
| 226 | if !c.ok() { |
| 227 | return syscall.EINVAL |
| 228 | } |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 229 | if err := c.fd.setDeadline(t); err != nil { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 230 | return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 231 | } |
| 232 | return nil |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // SetReadDeadline implements the Conn SetReadDeadline method. |
| 236 | func (c *conn) SetReadDeadline(t time.Time) error { |
| 237 | if !c.ok() { |
| 238 | return syscall.EINVAL |
| 239 | } |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 240 | if err := c.fd.setReadDeadline(t); err != nil { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 241 | return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 242 | } |
| 243 | return nil |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // SetWriteDeadline implements the Conn SetWriteDeadline method. |
| 247 | func (c *conn) SetWriteDeadline(t time.Time) error { |
| 248 | if !c.ok() { |
| 249 | return syscall.EINVAL |
| 250 | } |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 251 | if err := c.fd.setWriteDeadline(t); err != nil { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 252 | return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 253 | } |
| 254 | return nil |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // SetReadBuffer sets the size of the operating system's |
| 258 | // receive buffer associated with the connection. |
| 259 | func (c *conn) SetReadBuffer(bytes int) error { |
| 260 | if !c.ok() { |
| 261 | return syscall.EINVAL |
| 262 | } |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 263 | if err := setReadBuffer(c.fd, bytes); err != nil { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 264 | return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 265 | } |
| 266 | return nil |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // SetWriteBuffer sets the size of the operating system's |
| 270 | // transmit buffer associated with the connection. |
| 271 | func (c *conn) SetWriteBuffer(bytes int) error { |
| 272 | if !c.ok() { |
| 273 | return syscall.EINVAL |
| 274 | } |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 275 | if err := setWriteBuffer(c.fd, bytes); err != nil { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 276 | return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} |
Mikio Hara | 2173a27 | 2015-04-19 19:01:49 +0900 | [diff] [blame] | 277 | } |
| 278 | return nil |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 279 | } |
| 280 | |
Rick Arnold | 5416e6e | 2012-12-05 23:31:35 -0500 | [diff] [blame] | 281 | // File sets the underlying os.File to blocking mode and returns a copy. |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 282 | // It is the caller's responsibility to close f when finished. |
| 283 | // Closing c does not affect f, and closing f does not affect c. |
Rick Arnold | 5416e6e | 2012-12-05 23:31:35 -0500 | [diff] [blame] | 284 | // |
| 285 | // The returned os.File's file descriptor is different from the connection's. |
| 286 | // Attempting to change properties of the original using this duplicate |
| 287 | // may or may not have the desired effect. |
Mikio Hara | 8851113 | 2015-04-18 16:53:55 +0900 | [diff] [blame] | 288 | func (c *conn) File() (f *os.File, err error) { |
| 289 | f, err = c.fd.dup() |
| 290 | if err != nil { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 291 | err = &OpError{Op: "file", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} |
Mikio Hara | 8851113 | 2015-04-18 16:53:55 +0900 | [diff] [blame] | 292 | } |
| 293 | return |
| 294 | } |
Mikio Hara | 306afc7 | 2012-11-13 16:18:37 +0900 | [diff] [blame] | 295 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 296 | // PacketConn is a generic packet-oriented network connection. |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 297 | // |
| 298 | // Multiple goroutines may invoke methods on a PacketConn simultaneously. |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 299 | type PacketConn interface { |
| 300 | // ReadFrom reads a packet from the connection, |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 301 | // copying the payload into b. It returns the number of |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 302 | // bytes copied into b and the return address that |
| 303 | // was on the packet. |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 304 | // ReadFrom can be made to time out and return |
| 305 | // an error with Timeout() == true after a fixed time limit; |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 306 | // see SetDeadline and SetReadDeadline. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 307 | ReadFrom(b []byte) (n int, addr Addr, err error) |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 308 | |
| 309 | // WriteTo writes a packet with payload b to addr. |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 310 | // WriteTo can be made to time out and return |
| 311 | // an error with Timeout() == true after a fixed time limit; |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 312 | // see SetDeadline and SetWriteDeadline. |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 313 | // On packet-oriented connections, write timeouts are rare. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 314 | WriteTo(b []byte, addr Addr) (n int, err error) |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 315 | |
| 316 | // Close closes the connection. |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 317 | // Any blocked ReadFrom or WriteTo operations will be unblocked and return errors. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 318 | Close() error |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 319 | |
| 320 | // LocalAddr returns the local network address. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 321 | LocalAddr() Addr |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 322 | |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 323 | // SetDeadline sets the read and write deadlines associated |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 324 | // with the connection. |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 325 | SetDeadline(t time.Time) error |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 326 | |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 327 | // SetReadDeadline sets the deadline for future Read calls. |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 328 | // If the deadline is reached, Read will fail with a timeout |
| 329 | // (see type Error) instead of blocking. |
| 330 | // A zero value for t means Read will not time out. |
| 331 | SetReadDeadline(t time.Time) error |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 332 | |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 333 | // SetWriteDeadline sets the deadline for future Write calls. |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 334 | // If the deadline is reached, Write will fail with a timeout |
| 335 | // (see type Error) instead of blocking. |
| 336 | // A zero value for t means Write will not time out. |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 337 | // Even if write times out, it may return n > 0, indicating that |
| 338 | // some of the data was successfully written. |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 339 | SetWriteDeadline(t time.Time) error |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Mikio Hara | a0a45bb | 2013-07-24 08:43:08 +0900 | [diff] [blame] | 342 | var listenerBacklog = maxListenerBacklog() |
| 343 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 344 | // A Listener is a generic network listener for stream-oriented protocols. |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 345 | // |
| 346 | // Multiple goroutines may invoke methods on a Listener simultaneously. |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 347 | type Listener interface { |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 348 | // Accept waits for and returns the next connection to the listener. |
Brad Fitzpatrick | 19d262f | 2015-09-14 20:58:21 -0700 | [diff] [blame] | 349 | Accept() (Conn, error) |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 350 | |
| 351 | // Close closes the listener. |
Russ Cox | babbf94 | 2012-03-07 14:55:09 -0500 | [diff] [blame] | 352 | // Any blocked Accept operations will be unblocked and return errors. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 353 | Close() error |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 354 | |
| 355 | // Addr returns the listener's network address. |
| 356 | Addr() Addr |
Russ Cox | 5d2ee9d | 2009-06-17 21:44:26 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 359 | // An Error represents a network error. |
| 360 | type Error interface { |
| 361 | error |
| 362 | Timeout() bool // Is the error a timeout? |
| 363 | Temporary() bool // Is the error temporary? |
| 364 | } |
| 365 | |
Mikio Hara | a2a3514 | 2014-04-08 06:14:49 +0900 | [diff] [blame] | 366 | // Various errors contained in OpError. |
| 367 | var ( |
Mikio Hara | 790053b | 2016-03-15 10:00:12 +0900 | [diff] [blame] | 368 | // For connection setup operations. |
| 369 | errNoSuitableAddress = errors.New("no suitable address found") |
| 370 | |
Mikio Hara | a2a3514 | 2014-04-08 06:14:49 +0900 | [diff] [blame] | 371 | // For connection setup and write operations. |
| 372 | errMissingAddress = errors.New("missing address") |
| 373 | |
| 374 | // For both read and write operations. |
| 375 | errTimeout error = &timeoutError{} |
Paul Marks | 0d8366e | 2015-04-10 14:15:54 -0700 | [diff] [blame] | 376 | errCanceled = errors.New("operation was canceled") |
Mikio Hara | a2a3514 | 2014-04-08 06:14:49 +0900 | [diff] [blame] | 377 | errClosing = errors.New("use of closed network connection") |
| 378 | ErrWriteToConnected = errors.New("use of WriteTo with pre-connected connection") |
| 379 | ) |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 380 | |
Brad Fitzpatrick | b6b4004 | 2016-04-14 17:47:25 -0700 | [diff] [blame] | 381 | // mapErr maps from the context errors to the historical internal net |
| 382 | // error values. |
| 383 | // |
| 384 | // TODO(bradfitz): get rid of this after adjusting tests and making |
| 385 | // context.DeadlineExceeded implement net.Error? |
| 386 | func mapErr(err error) error { |
| 387 | switch err { |
| 388 | case context.Canceled: |
| 389 | return errCanceled |
| 390 | case context.DeadlineExceeded: |
| 391 | return errTimeout |
| 392 | default: |
| 393 | return err |
| 394 | } |
| 395 | } |
| 396 | |
Brad Fitzpatrick | 158a035 | 2013-02-14 09:29:34 -0800 | [diff] [blame] | 397 | // OpError is the error type usually returned by functions in the net |
| 398 | // package. It describes the operation, network type, and address of |
| 399 | // an error. |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 400 | type OpError struct { |
Brad Fitzpatrick | 158a035 | 2013-02-14 09:29:34 -0800 | [diff] [blame] | 401 | // Op is the operation which caused the error, such as |
| 402 | // "read" or "write". |
| 403 | Op string |
| 404 | |
| 405 | // Net is the network type on which this error occurred, |
| 406 | // such as "tcp" or "udp6". |
| 407 | Net string |
| 408 | |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 409 | // For operations involving a remote network connection, like |
| 410 | // Dial, Read, or Write, Source is the corresponding local |
| 411 | // network address. |
| 412 | Source Addr |
| 413 | |
| 414 | // Addr is the network address for which this error occurred. |
| 415 | // For local operations, like Listen or SetDeadline, Addr is |
| 416 | // the address of the local endpoint being manipulated. |
| 417 | // For operations involving a remote network connection, like |
| 418 | // Dial, Read, or Write, Addr is the remote address of that |
| 419 | // connection. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 420 | Addr Addr |
Brad Fitzpatrick | 158a035 | 2013-02-14 09:29:34 -0800 | [diff] [blame] | 421 | |
| 422 | // Err is the error that occurred during the operation. |
| 423 | Err error |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 426 | func (e *OpError) Error() string { |
Andrew Gerrand | fc4ba15 | 2010-07-27 17:22:22 +1000 | [diff] [blame] | 427 | if e == nil { |
| 428 | return "<nil>" |
| 429 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 430 | s := e.Op |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 431 | if e.Net != "" { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 432 | s += " " + e.Net |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 433 | } |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 434 | if e.Source != nil { |
| 435 | s += " " + e.Source.String() |
| 436 | } |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 437 | if e.Addr != nil { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 438 | if e.Source != nil { |
| 439 | s += "->" |
| 440 | } else { |
| 441 | s += " " |
| 442 | } |
| 443 | s += e.Addr.String() |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 444 | } |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 445 | s += ": " + e.Err.Error() |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 446 | return s |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Brad Fitzpatrick | 24a83d3 | 2015-12-14 22:21:48 +0000 | [diff] [blame] | 449 | var ( |
| 450 | // aLongTimeAgo is a non-zero time, far in the past, used for |
| 451 | // immediate cancelation of dials. |
| 452 | aLongTimeAgo = time.Unix(233431200, 0) |
| 453 | |
| 454 | // nonDeadline and noCancel are just zero values for |
| 455 | // readability with functions taking too many parameters. |
| 456 | noDeadline = time.Time{} |
| 457 | noCancel = (chan struct{})(nil) |
| 458 | ) |
Brad Fitzpatrick | ef6806f | 2012-11-08 10:35:16 -0600 | [diff] [blame] | 459 | |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 460 | type timeout interface { |
| 461 | Timeout() bool |
| 462 | } |
| 463 | |
| 464 | func (e *OpError) Timeout() bool { |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 465 | if ne, ok := e.Err.(*os.SyscallError); ok { |
| 466 | t, ok := ne.Err.(timeout) |
| 467 | return ok && t.Timeout() |
| 468 | } |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 469 | t, ok := e.Err.(timeout) |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 470 | return ok && t.Timeout() |
| 471 | } |
| 472 | |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 473 | type temporary interface { |
| 474 | Temporary() bool |
| 475 | } |
| 476 | |
| 477 | func (e *OpError) Temporary() bool { |
| 478 | if ne, ok := e.Err.(*os.SyscallError); ok { |
| 479 | t, ok := ne.Err.(temporary) |
| 480 | return ok && t.Temporary() |
| 481 | } |
| 482 | t, ok := e.Err.(temporary) |
| 483 | return ok && t.Temporary() |
| 484 | } |
| 485 | |
Brad Fitzpatrick | 01507b9 | 2011-12-20 14:32:33 -0800 | [diff] [blame] | 486 | type timeoutError struct{} |
| 487 | |
| 488 | func (e *timeoutError) Error() string { return "i/o timeout" } |
| 489 | func (e *timeoutError) Timeout() bool { return true } |
| 490 | func (e *timeoutError) Temporary() bool { return true } |
| 491 | |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 492 | // A ParseError is the error type of literal network address parsers. |
| 493 | type ParseError struct { |
| 494 | // Type is the type of string that was expected, such as |
| 495 | // "IP address", "CIDR address". |
| 496 | Type string |
| 497 | |
| 498 | // Text is the malformed text string. |
| 499 | Text string |
| 500 | } |
| 501 | |
| 502 | func (e *ParseError) Error() string { return "invalid " + e.Type + ": " + e.Text } |
| 503 | |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 504 | type AddrError struct { |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 505 | Err string |
| 506 | Addr string |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 509 | func (e *AddrError) Error() string { |
Andrew Gerrand | fc4ba15 | 2010-07-27 17:22:22 +1000 | [diff] [blame] | 510 | if e == nil { |
| 511 | return "<nil>" |
| 512 | } |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 513 | s := e.Err |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 514 | if e.Addr != "" { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 515 | s += " " + e.Addr |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 516 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 517 | return s |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Mikio Hara | 0fc582e8 | 2015-04-19 20:54:01 +0900 | [diff] [blame] | 520 | func (e *AddrError) Timeout() bool { return false } |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 521 | func (e *AddrError) Temporary() bool { return false } |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 522 | |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 523 | type UnknownNetworkError string |
Robert Griesemer | 5d37705 | 2009-11-04 23:16:46 -0800 | [diff] [blame] | 524 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 525 | func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) } |
Russ Cox | 47a0533 | 2010-04-26 22:15:25 -0700 | [diff] [blame] | 526 | func (e UnknownNetworkError) Timeout() bool { return false } |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 527 | func (e UnknownNetworkError) Temporary() bool { return false } |
Brad Fitzpatrick | 549ca93 | 2012-01-31 13:01:34 -0800 | [diff] [blame] | 528 | |
Mikio Hara | db84a45 | 2013-08-10 09:46:22 +0900 | [diff] [blame] | 529 | type InvalidAddrError string |
| 530 | |
| 531 | func (e InvalidAddrError) Error() string { return string(e) } |
| 532 | func (e InvalidAddrError) Timeout() bool { return false } |
| 533 | func (e InvalidAddrError) Temporary() bool { return false } |
| 534 | |
Brad Fitzpatrick | 549ca93 | 2012-01-31 13:01:34 -0800 | [diff] [blame] | 535 | // DNSConfigError represents an error reading the machine's DNS configuration. |
Alex A Skinner | f390135 | 2015-04-25 20:50:21 -0400 | [diff] [blame] | 536 | // (No longer used; kept for compatibility.) |
Brad Fitzpatrick | 549ca93 | 2012-01-31 13:01:34 -0800 | [diff] [blame] | 537 | type DNSConfigError struct { |
| 538 | Err error |
| 539 | } |
| 540 | |
Mikio Hara | 0fc582e8 | 2015-04-19 20:54:01 +0900 | [diff] [blame] | 541 | func (e *DNSConfigError) Error() string { return "error reading DNS config: " + e.Err.Error() } |
Brad Fitzpatrick | 549ca93 | 2012-01-31 13:01:34 -0800 | [diff] [blame] | 542 | func (e *DNSConfigError) Timeout() bool { return false } |
| 543 | func (e *DNSConfigError) Temporary() bool { return false } |
Anthony Martin | 253ed02 | 2012-11-30 11:41:50 -0800 | [diff] [blame] | 544 | |
Mikio Hara | 0fc582e8 | 2015-04-19 20:54:01 +0900 | [diff] [blame] | 545 | // Various errors contained in DNSError. |
| 546 | var ( |
| 547 | errNoSuchHost = errors.New("no such host") |
| 548 | ) |
| 549 | |
| 550 | // DNSError represents a DNS lookup error. |
| 551 | type DNSError struct { |
Dan Peterson | ced0646 | 2015-09-01 22:53:46 -0300 | [diff] [blame] | 552 | Err string // description of the error |
| 553 | Name string // name looked for |
| 554 | Server string // server used |
| 555 | IsTimeout bool // if true, timed out; not all timeouts set this |
| 556 | IsTemporary bool // if true, error is temporary; not all errors set this |
Mikio Hara | 0fc582e8 | 2015-04-19 20:54:01 +0900 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | func (e *DNSError) Error() string { |
| 560 | if e == nil { |
| 561 | return "<nil>" |
| 562 | } |
| 563 | s := "lookup " + e.Name |
| 564 | if e.Server != "" { |
| 565 | s += " on " + e.Server |
| 566 | } |
| 567 | s += ": " + e.Err |
| 568 | return s |
| 569 | } |
| 570 | |
| 571 | // Timeout reports whether the DNS lookup is known to have timed out. |
| 572 | // This is not always known; a DNS lookup may fail due to a timeout |
| 573 | // and return a DNSError for which Timeout returns false. |
| 574 | func (e *DNSError) Timeout() bool { return e.IsTimeout } |
| 575 | |
| 576 | // Temporary reports whether the DNS error is known to be temporary. |
| 577 | // This is not always known; a DNS lookup may fail due to a temporary |
| 578 | // error and return a DNSError for which Temporary returns false. |
Dan Peterson | ced0646 | 2015-09-01 22:53:46 -0300 | [diff] [blame] | 579 | func (e *DNSError) Temporary() bool { return e.IsTimeout || e.IsTemporary } |
Mikio Hara | 0fc582e8 | 2015-04-19 20:54:01 +0900 | [diff] [blame] | 580 | |
Anthony Martin | 253ed02 | 2012-11-30 11:41:50 -0800 | [diff] [blame] | 581 | type writerOnly struct { |
| 582 | io.Writer |
| 583 | } |
| 584 | |
| 585 | // Fallback implementation of io.ReaderFrom's ReadFrom, when sendfile isn't |
| 586 | // applicable. |
| 587 | func genericReadFrom(w io.Writer, r io.Reader) (n int64, err error) { |
| 588 | // Use wrapper to hide existing r.ReadFrom from io.Copy. |
| 589 | return io.Copy(writerOnly{w}, r) |
| 590 | } |
Dave Cheney | 9fb9699 | 2012-12-05 15:59:01 +1100 | [diff] [blame] | 591 | |
Russ Cox | 1d3efd6 | 2013-08-16 22:43:05 -0400 | [diff] [blame] | 592 | // Limit the number of concurrent cgo-using goroutines, because |
| 593 | // each will block an entire operating system thread. The usual culprit |
| 594 | // is resolving many DNS names in separate goroutines but the DNS |
| 595 | // server is not responding. Then the many lookups each use a different |
| 596 | // thread, and the system or the program runs out of threads. |
| 597 | |
| 598 | var threadLimit = make(chan struct{}, 500) |
| 599 | |
| 600 | func acquireThread() { |
Russ Cox | bab302d | 2013-09-11 20:29:22 -0400 | [diff] [blame] | 601 | threadLimit <- struct{}{} |
Russ Cox | 1d3efd6 | 2013-08-16 22:43:05 -0400 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | func releaseThread() { |
Russ Cox | bab302d | 2013-09-11 20:29:22 -0400 | [diff] [blame] | 605 | <-threadLimit |
Russ Cox | 1d3efd6 | 2013-08-16 22:43:05 -0400 | [diff] [blame] | 606 | } |