blob: feb92a2737d5623fc57c3997d044d1b79bd78eb8 [file] [log] [blame]
Russ Coxe8a02232008-09-16 13:42:47 -07001// 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 Fitzpatrick008e64d2012-02-17 13:07:06 +11005/*
6Package net provides a portable interface for network I/O, including
7TCP/IP, UDP, domain name resolution, and Unix domain sockets.
8
9Although the package provides access to low-level networking
Brad Fitzpatrick8729d152012-02-21 11:11:18 +110010primitives, most clients will need only the basic interface provided
11by the Dial, Listen, and Accept functions and the associated
12Conn and Listener interfaces. The crypto/tls package uses
13the same interfaces and similar Dial and Listen functions.
Brad Fitzpatrick008e64d2012-02-17 13:07:06 +110014
15The Dial function connects to a server:
16
17 conn, err := net.Dial("tcp", "google.com:80")
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
25The 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
35 continue
36 }
37 go handleConnection(conn)
38 }
39*/
Russ Coxe8a02232008-09-16 13:42:47 -070040package net
41
Russ Cox35ace1d2009-11-01 11:15:34 -080042// TODO(rsc):
Russ Cox35ace1d2009-11-01 11:15:34 -080043// support for raw ethernet sockets
Russ Coxe8a02232008-09-16 13:42:47 -070044
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -080045import (
46 "errors"
Mikio Hara306afc72012-11-13 16:18:37 +090047 "os"
48 "syscall"
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -080049 "time"
50)
Russ Cox97bc2222009-05-07 17:36:29 -070051
Russ Coxc83b8382009-11-02 18:37:30 -080052// Addr represents a network end point address.
53type Addr interface {
Robert Griesemera3d10452009-12-15 15:35:38 -080054 Network() string // name of the network
55 String() string // string form of address
Russ Coxc83b8382009-11-02 18:37:30 -080056}
57
58// Conn is a generic stream-oriented network connection.
Russ Coxbabbf942012-03-07 14:55:09 -050059//
60// Multiple goroutines may invoke methods on a Conn simultaneously.
Russ Coxcc1d4b72009-05-13 18:03:41 -070061type Conn interface {
Russ Coxc83b8382009-11-02 18:37:30 -080062 // Read reads data from the connection.
Mikio Hara3d400db2012-01-29 19:11:05 +090063 // Read can be made to time out and return a Error with Timeout() == true
Mikio Hara2356e432012-01-19 12:23:30 +090064 // after a fixed time limit; see SetDeadline and SetReadDeadline.
Russ Coxeb692922011-11-01 22:05:34 -040065 Read(b []byte) (n int, err error)
Russ Coxcc1d4b72009-05-13 18:03:41 -070066
Russ Coxc83b8382009-11-02 18:37:30 -080067 // Write writes data to the connection.
Mikio Hara3d400db2012-01-29 19:11:05 +090068 // Write can be made to time out and return a Error with Timeout() == true
Mikio Hara2356e432012-01-19 12:23:30 +090069 // after a fixed time limit; see SetDeadline and SetWriteDeadline.
Russ Coxeb692922011-11-01 22:05:34 -040070 Write(b []byte) (n int, err error)
Russ Coxcc1d4b72009-05-13 18:03:41 -070071
72 // Close closes the connection.
Russ Coxbabbf942012-03-07 14:55:09 -050073 // Any blocked Read or Write operations will be unblocked and return errors.
Russ Coxeb692922011-11-01 22:05:34 -040074 Close() error
Russ Coxcc1d4b72009-05-13 18:03:41 -070075
Rob Pikeefc40882009-06-19 16:03:59 -070076 // LocalAddr returns the local network address.
Robert Griesemera3d10452009-12-15 15:35:38 -080077 LocalAddr() Addr
Rob Pikeefc40882009-06-19 16:03:59 -070078
79 // RemoteAddr returns the remote network address.
Robert Griesemera3d10452009-12-15 15:35:38 -080080 RemoteAddr() Addr
Russ Coxcc1d4b72009-05-13 18:03:41 -070081
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -080082 // SetDeadline sets the read and write deadlines associated
Brad Fitzpatrick8729d152012-02-21 11:11:18 +110083 // with the connection. It is equivalent to calling both
84 // SetReadDeadline and SetWriteDeadline.
85 //
86 // A deadline is an absolute time after which I/O operations
87 // fail with a timeout (see type Error) instead of
88 // blocking. The deadline applies to all future I/O, not just
89 // the immediately following call to Read or Write.
90 //
91 // An idle timeout can be implemented by repeatedly extending
92 // the deadline after successful Read or Write calls.
93 //
94 // A zero value for t means I/O operations will not time out.
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -080095 SetDeadline(t time.Time) error
Russ Coxcc1d4b72009-05-13 18:03:41 -070096
Russ Coxbabbf942012-03-07 14:55:09 -050097 // SetReadDeadline sets the deadline for future Read calls.
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -080098 // A zero value for t means Read will not time out.
99 SetReadDeadline(t time.Time) error
Russ Coxcc1d4b72009-05-13 18:03:41 -0700100
Russ Coxbabbf942012-03-07 14:55:09 -0500101 // SetWriteDeadline sets the deadline for future Write calls.
Russ Coxcc1d4b72009-05-13 18:03:41 -0700102 // Even if write times out, it may return n > 0, indicating that
103 // some of the data was successfully written.
Brad Fitzpatrick8729d152012-02-21 11:11:18 +1100104 // A zero value for t means Write will not time out.
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -0800105 SetWriteDeadline(t time.Time) error
Russ Coxcc1d4b72009-05-13 18:03:41 -0700106}
107
Mikio Hara306afc72012-11-13 16:18:37 +0900108type conn struct {
109 fd *netFD
110}
111
112func (c *conn) ok() bool { return c != nil && c.fd != nil }
113
114// Implementation of the Conn interface.
115
116// Read implements the Conn Read method.
117func (c *conn) Read(b []byte) (int, error) {
118 if !c.ok() {
119 return 0, syscall.EINVAL
120 }
121 return c.fd.Read(b)
122}
123
124// Write implements the Conn Write method.
125func (c *conn) Write(b []byte) (int, error) {
126 if !c.ok() {
127 return 0, syscall.EINVAL
128 }
129 return c.fd.Write(b)
130}
131
132// Close closes the connection.
133func (c *conn) Close() error {
134 if !c.ok() {
135 return syscall.EINVAL
136 }
137 return c.fd.Close()
138}
139
140// LocalAddr returns the local network address.
141func (c *conn) LocalAddr() Addr {
142 if !c.ok() {
143 return nil
144 }
145 return c.fd.laddr
146}
147
148// RemoteAddr returns the remote network address.
149func (c *conn) RemoteAddr() Addr {
150 if !c.ok() {
151 return nil
152 }
153 return c.fd.raddr
154}
155
156// SetDeadline implements the Conn SetDeadline method.
157func (c *conn) SetDeadline(t time.Time) error {
158 if !c.ok() {
159 return syscall.EINVAL
160 }
161 return setDeadline(c.fd, t)
162}
163
164// SetReadDeadline implements the Conn SetReadDeadline method.
165func (c *conn) SetReadDeadline(t time.Time) error {
166 if !c.ok() {
167 return syscall.EINVAL
168 }
169 return setReadDeadline(c.fd, t)
170}
171
172// SetWriteDeadline implements the Conn SetWriteDeadline method.
173func (c *conn) SetWriteDeadline(t time.Time) error {
174 if !c.ok() {
175 return syscall.EINVAL
176 }
177 return setWriteDeadline(c.fd, t)
178}
179
180// SetReadBuffer sets the size of the operating system's
181// receive buffer associated with the connection.
182func (c *conn) SetReadBuffer(bytes int) error {
183 if !c.ok() {
184 return syscall.EINVAL
185 }
186 return setReadBuffer(c.fd, bytes)
187}
188
189// SetWriteBuffer sets the size of the operating system's
190// transmit buffer associated with the connection.
191func (c *conn) SetWriteBuffer(bytes int) error {
192 if !c.ok() {
193 return syscall.EINVAL
194 }
195 return setWriteBuffer(c.fd, bytes)
196}
197
198// File returns a copy of the underlying os.File, set to blocking mode.
199// It is the caller's responsibility to close f when finished.
200// Closing c does not affect f, and closing f does not affect c.
201func (c *conn) File() (f *os.File, err error) { return c.fd.dup() }
202
Russ Cox47a05332010-04-26 22:15:25 -0700203// An Error represents a network error.
204type Error interface {
Russ Coxeb692922011-11-01 22:05:34 -0400205 error
Russ Cox47a05332010-04-26 22:15:25 -0700206 Timeout() bool // Is the error a timeout?
207 Temporary() bool // Is the error temporary?
208}
209
Russ Coxc83b8382009-11-02 18:37:30 -0800210// PacketConn is a generic packet-oriented network connection.
Russ Coxbabbf942012-03-07 14:55:09 -0500211//
212// Multiple goroutines may invoke methods on a PacketConn simultaneously.
Russ Coxc83b8382009-11-02 18:37:30 -0800213type PacketConn interface {
214 // ReadFrom reads a packet from the connection,
215 // copying the payload into b. It returns the number of
216 // bytes copied into b and the return address that
217 // was on the packet.
Russ Cox47a05332010-04-26 22:15:25 -0700218 // ReadFrom can be made to time out and return
219 // an error with Timeout() == true after a fixed time limit;
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -0800220 // see SetDeadline and SetReadDeadline.
Russ Coxeb692922011-11-01 22:05:34 -0400221 ReadFrom(b []byte) (n int, addr Addr, err error)
Russ Coxc83b8382009-11-02 18:37:30 -0800222
223 // WriteTo writes a packet with payload b to addr.
Russ Cox47a05332010-04-26 22:15:25 -0700224 // WriteTo can be made to time out and return
225 // an error with Timeout() == true after a fixed time limit;
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -0800226 // see SetDeadline and SetWriteDeadline.
Russ Coxc83b8382009-11-02 18:37:30 -0800227 // On packet-oriented connections, write timeouts are rare.
Russ Coxeb692922011-11-01 22:05:34 -0400228 WriteTo(b []byte, addr Addr) (n int, err error)
Russ Coxc83b8382009-11-02 18:37:30 -0800229
230 // Close closes the connection.
Russ Coxbabbf942012-03-07 14:55:09 -0500231 // Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
Russ Coxeb692922011-11-01 22:05:34 -0400232 Close() error
Russ Coxc83b8382009-11-02 18:37:30 -0800233
234 // LocalAddr returns the local network address.
Robert Griesemera3d10452009-12-15 15:35:38 -0800235 LocalAddr() Addr
Russ Coxc83b8382009-11-02 18:37:30 -0800236
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -0800237 // SetDeadline sets the read and write deadlines associated
Russ Coxc83b8382009-11-02 18:37:30 -0800238 // with the connection.
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -0800239 SetDeadline(t time.Time) error
Russ Coxc83b8382009-11-02 18:37:30 -0800240
Russ Coxbabbf942012-03-07 14:55:09 -0500241 // SetReadDeadline sets the deadline for future Read calls.
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -0800242 // If the deadline is reached, Read will fail with a timeout
243 // (see type Error) instead of blocking.
244 // A zero value for t means Read will not time out.
245 SetReadDeadline(t time.Time) error
Russ Coxc83b8382009-11-02 18:37:30 -0800246
Russ Coxbabbf942012-03-07 14:55:09 -0500247 // SetWriteDeadline sets the deadline for future Write calls.
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -0800248 // If the deadline is reached, Write will fail with a timeout
249 // (see type Error) instead of blocking.
250 // A zero value for t means Write will not time out.
Russ Coxc83b8382009-11-02 18:37:30 -0800251 // Even if write times out, it may return n > 0, indicating that
252 // some of the data was successfully written.
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -0800253 SetWriteDeadline(t time.Time) error
Russ Coxc83b8382009-11-02 18:37:30 -0800254}
255
256// A Listener is a generic network listener for stream-oriented protocols.
Russ Coxbabbf942012-03-07 14:55:09 -0500257//
258// Multiple goroutines may invoke methods on a Listener simultaneously.
Russ Cox35ace1d2009-11-01 11:15:34 -0800259type Listener interface {
Russ Cox47a05332010-04-26 22:15:25 -0700260 // Accept waits for and returns the next connection to the listener.
Russ Coxeb692922011-11-01 22:05:34 -0400261 Accept() (c Conn, err error)
Russ Cox47a05332010-04-26 22:15:25 -0700262
263 // Close closes the listener.
Russ Coxbabbf942012-03-07 14:55:09 -0500264 // Any blocked Accept operations will be unblocked and return errors.
Russ Coxeb692922011-11-01 22:05:34 -0400265 Close() error
Russ Cox47a05332010-04-26 22:15:25 -0700266
267 // Addr returns the listener's network address.
268 Addr() Addr
Russ Cox5d2ee9d2009-06-17 21:44:26 -0700269}
270
Russ Coxeb692922011-11-01 22:05:34 -0400271var errMissingAddress = errors.New("missing address")
Russ Cox35ace1d2009-11-01 11:15:34 -0800272
273type OpError struct {
Russ Coxeb692922011-11-01 22:05:34 -0400274 Op string
275 Net string
276 Addr Addr
277 Err error
Russ Cox35ace1d2009-11-01 11:15:34 -0800278}
279
Russ Coxeb692922011-11-01 22:05:34 -0400280func (e *OpError) Error() string {
Andrew Gerrandfc4ba152010-07-27 17:22:22 +1000281 if e == nil {
282 return "<nil>"
283 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800284 s := e.Op
Russ Cox35ace1d2009-11-01 11:15:34 -0800285 if e.Net != "" {
Robert Griesemer40621d52009-11-09 12:07:39 -0800286 s += " " + e.Net
Russ Cox35ace1d2009-11-01 11:15:34 -0800287 }
Russ Coxc83b8382009-11-02 18:37:30 -0800288 if e.Addr != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800289 s += " " + e.Addr.String()
Russ Cox35ace1d2009-11-01 11:15:34 -0800290 }
Russ Coxeb692922011-11-01 22:05:34 -0400291 s += ": " + e.Err.Error()
Robert Griesemera3d10452009-12-15 15:35:38 -0800292 return s
Russ Cox35ace1d2009-11-01 11:15:34 -0800293}
294
Russ Cox47a05332010-04-26 22:15:25 -0700295type temporary interface {
296 Temporary() bool
297}
298
299func (e *OpError) Temporary() bool {
Russ Coxeb692922011-11-01 22:05:34 -0400300 t, ok := e.Err.(temporary)
Russ Cox47a05332010-04-26 22:15:25 -0700301 return ok && t.Temporary()
302}
303
Brad Fitzpatrickef6806f2012-11-08 10:35:16 -0600304var noDeadline = time.Time{}
305
Russ Cox47a05332010-04-26 22:15:25 -0700306type timeout interface {
307 Timeout() bool
308}
309
310func (e *OpError) Timeout() bool {
Russ Coxeb692922011-11-01 22:05:34 -0400311 t, ok := e.Err.(timeout)
Russ Cox47a05332010-04-26 22:15:25 -0700312 return ok && t.Timeout()
313}
314
Brad Fitzpatrick01507b92011-12-20 14:32:33 -0800315type timeoutError struct{}
316
317func (e *timeoutError) Error() string { return "i/o timeout" }
318func (e *timeoutError) Timeout() bool { return true }
319func (e *timeoutError) Temporary() bool { return true }
320
321var errTimeout error = &timeoutError{}
322
Alex Brainman84e20462012-11-02 11:07:22 +1100323var errClosing = errors.New("use of closed network connection")
324
Russ Cox35ace1d2009-11-01 11:15:34 -0800325type AddrError struct {
Russ Coxeb692922011-11-01 22:05:34 -0400326 Err string
327 Addr string
Russ Cox35ace1d2009-11-01 11:15:34 -0800328}
329
Russ Coxeb692922011-11-01 22:05:34 -0400330func (e *AddrError) Error() string {
Andrew Gerrandfc4ba152010-07-27 17:22:22 +1000331 if e == nil {
332 return "<nil>"
333 }
Russ Coxeb692922011-11-01 22:05:34 -0400334 s := e.Err
Russ Cox35ace1d2009-11-01 11:15:34 -0800335 if e.Addr != "" {
Robert Griesemer40621d52009-11-09 12:07:39 -0800336 s += " " + e.Addr
Russ Cox35ace1d2009-11-01 11:15:34 -0800337 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800338 return s
Russ Cox35ace1d2009-11-01 11:15:34 -0800339}
340
Russ Cox47a05332010-04-26 22:15:25 -0700341func (e *AddrError) Temporary() bool {
342 return false
343}
344
345func (e *AddrError) Timeout() bool {
346 return false
347}
348
Russ Cox35ace1d2009-11-01 11:15:34 -0800349type UnknownNetworkError string
Robert Griesemer5d377052009-11-04 23:16:46 -0800350
Russ Coxeb692922011-11-01 22:05:34 -0400351func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) }
Russ Cox47a05332010-04-26 22:15:25 -0700352func (e UnknownNetworkError) Temporary() bool { return false }
353func (e UnknownNetworkError) Timeout() bool { return false }
Brad Fitzpatrick549ca932012-01-31 13:01:34 -0800354
355// DNSConfigError represents an error reading the machine's DNS configuration.
356type DNSConfigError struct {
357 Err error
358}
359
360func (e *DNSConfigError) Error() string {
361 return "error reading DNS config: " + e.Err.Error()
362}
363
364func (e *DNSConfigError) Timeout() bool { return false }
365func (e *DNSConfigError) Temporary() bool { return false }