blob: b236dfdb1dd81ae5daa50537485a521da4cf6702 [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
Nigel Tao6a186d32011-04-20 09:57:05 +10005// Package net provides a portable interface to Unix networks sockets,
6// including TCP/IP, UDP, domain name resolution, and Unix domain sockets.
Russ Coxe8a02232008-09-16 13:42:47 -07007package net
8
Russ Cox35ace1d2009-11-01 11:15:34 -08009// TODO(rsc):
Russ Cox35ace1d2009-11-01 11:15:34 -080010// support for raw ethernet sockets
Russ Coxe8a02232008-09-16 13:42:47 -070011
Russ Coxeb692922011-11-01 22:05:34 -040012import "errors"
Russ Cox97bc2222009-05-07 17:36:29 -070013
Russ Coxc83b8382009-11-02 18:37:30 -080014// Addr represents a network end point address.
15type Addr interface {
Robert Griesemera3d10452009-12-15 15:35:38 -080016 Network() string // name of the network
17 String() string // string form of address
Russ Coxc83b8382009-11-02 18:37:30 -080018}
19
20// Conn is a generic stream-oriented network connection.
Russ Coxcc1d4b72009-05-13 18:03:41 -070021type Conn interface {
Russ Coxc83b8382009-11-02 18:37:30 -080022 // Read reads data from the connection.
Russ Cox47a05332010-04-26 22:15:25 -070023 // Read can be made to time out and return a net.Error with Timeout() == true
Russ Coxc83b8382009-11-02 18:37:30 -080024 // after a fixed time limit; see SetTimeout and SetReadTimeout.
Russ Coxeb692922011-11-01 22:05:34 -040025 Read(b []byte) (n int, err error)
Russ Coxcc1d4b72009-05-13 18:03:41 -070026
Russ Coxc83b8382009-11-02 18:37:30 -080027 // Write writes data to the connection.
Russ Cox47a05332010-04-26 22:15:25 -070028 // 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 Coxeb692922011-11-01 22:05:34 -040030 Write(b []byte) (n int, err error)
Russ Coxcc1d4b72009-05-13 18:03:41 -070031
32 // Close closes the connection.
Russ Coxeb692922011-11-01 22:05:34 -040033 Close() error
Russ Coxcc1d4b72009-05-13 18:03:41 -070034
Rob Pikeefc40882009-06-19 16:03:59 -070035 // LocalAddr returns the local network address.
Robert Griesemera3d10452009-12-15 15:35:38 -080036 LocalAddr() Addr
Rob Pikeefc40882009-06-19 16:03:59 -070037
38 // RemoteAddr returns the remote network address.
Robert Griesemera3d10452009-12-15 15:35:38 -080039 RemoteAddr() Addr
Russ Coxcc1d4b72009-05-13 18:03:41 -070040
41 // SetTimeout sets the read and write deadlines associated
42 // with the connection.
Russ Coxeb692922011-11-01 22:05:34 -040043 SetTimeout(nsec int64) error
Russ Coxcc1d4b72009-05-13 18:03:41 -070044
45 // SetReadTimeout sets the time (in nanoseconds) that
Russ Cox47a05332010-04-26 22:15:25 -070046 // Read will wait for data before returning an error with Timeout() == true.
Russ Coxcc1d4b72009-05-13 18:03:41 -070047 // Setting nsec == 0 (the default) disables the deadline.
Russ Coxeb692922011-11-01 22:05:34 -040048 SetReadTimeout(nsec int64) error
Russ Coxcc1d4b72009-05-13 18:03:41 -070049
50 // SetWriteTimeout sets the time (in nanoseconds) that
Russ Cox47a05332010-04-26 22:15:25 -070051 // Write will wait to send its data before returning an error with Timeout() == true.
Russ Coxcc1d4b72009-05-13 18:03:41 -070052 // 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 Coxeb692922011-11-01 22:05:34 -040055 SetWriteTimeout(nsec int64) error
Russ Coxcc1d4b72009-05-13 18:03:41 -070056}
57
Russ Cox47a05332010-04-26 22:15:25 -070058// An Error represents a network error.
59type Error interface {
Russ Coxeb692922011-11-01 22:05:34 -040060 error
Russ Cox47a05332010-04-26 22:15:25 -070061 Timeout() bool // Is the error a timeout?
62 Temporary() bool // Is the error temporary?
63}
64
Russ Coxc83b8382009-11-02 18:37:30 -080065// PacketConn is a generic packet-oriented network connection.
66type 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 Cox47a05332010-04-26 22:15:25 -070071 // 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 Coxeb692922011-11-01 22:05:34 -040074 ReadFrom(b []byte) (n int, addr Addr, err error)
Russ Coxc83b8382009-11-02 18:37:30 -080075
76 // WriteTo writes a packet with payload b to addr.
Russ Cox47a05332010-04-26 22:15:25 -070077 // 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 Coxc83b8382009-11-02 18:37:30 -080080 // On packet-oriented connections, write timeouts are rare.
Russ Coxeb692922011-11-01 22:05:34 -040081 WriteTo(b []byte, addr Addr) (n int, err error)
Russ Coxc83b8382009-11-02 18:37:30 -080082
83 // Close closes the connection.
Russ Coxeb692922011-11-01 22:05:34 -040084 Close() error
Russ Coxc83b8382009-11-02 18:37:30 -080085
86 // LocalAddr returns the local network address.
Robert Griesemera3d10452009-12-15 15:35:38 -080087 LocalAddr() Addr
Russ Coxc83b8382009-11-02 18:37:30 -080088
89 // SetTimeout sets the read and write deadlines associated
90 // with the connection.
Russ Coxeb692922011-11-01 22:05:34 -040091 SetTimeout(nsec int64) error
Russ Coxc83b8382009-11-02 18:37:30 -080092
93 // SetReadTimeout sets the time (in nanoseconds) that
Russ Cox47a05332010-04-26 22:15:25 -070094 // Read will wait for data before returning an error with Timeout() == true.
Russ Coxc83b8382009-11-02 18:37:30 -080095 // Setting nsec == 0 (the default) disables the deadline.
Russ Coxeb692922011-11-01 22:05:34 -040096 SetReadTimeout(nsec int64) error
Russ Coxc83b8382009-11-02 18:37:30 -080097
98 // SetWriteTimeout sets the time (in nanoseconds) that
Russ Cox47a05332010-04-26 22:15:25 -070099 // Write will wait to send its data before returning an error with Timeout() == true.
Russ Coxc83b8382009-11-02 18:37:30 -0800100 // 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 Coxeb692922011-11-01 22:05:34 -0400103 SetWriteTimeout(nsec int64) error
Russ Coxc83b8382009-11-02 18:37:30 -0800104}
105
106// A Listener is a generic network listener for stream-oriented protocols.
Russ Cox35ace1d2009-11-01 11:15:34 -0800107type Listener interface {
Russ Cox47a05332010-04-26 22:15:25 -0700108 // Accept waits for and returns the next connection to the listener.
Russ Coxeb692922011-11-01 22:05:34 -0400109 Accept() (c Conn, err error)
Russ Cox47a05332010-04-26 22:15:25 -0700110
111 // Close closes the listener.
Russ Coxeb692922011-11-01 22:05:34 -0400112 Close() error
Russ Cox47a05332010-04-26 22:15:25 -0700113
114 // Addr returns the listener's network address.
115 Addr() Addr
Russ Cox5d2ee9d2009-06-17 21:44:26 -0700116}
117
Russ Coxeb692922011-11-01 22:05:34 -0400118var errMissingAddress = errors.New("missing address")
Russ Cox35ace1d2009-11-01 11:15:34 -0800119
120type OpError struct {
Russ Coxeb692922011-11-01 22:05:34 -0400121 Op string
122 Net string
123 Addr Addr
124 Err error
Russ Cox35ace1d2009-11-01 11:15:34 -0800125}
126
Russ Coxeb692922011-11-01 22:05:34 -0400127func (e *OpError) Error() string {
Andrew Gerrandfc4ba152010-07-27 17:22:22 +1000128 if e == nil {
129 return "<nil>"
130 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800131 s := e.Op
Russ Cox35ace1d2009-11-01 11:15:34 -0800132 if e.Net != "" {
Robert Griesemer40621d52009-11-09 12:07:39 -0800133 s += " " + e.Net
Russ Cox35ace1d2009-11-01 11:15:34 -0800134 }
Russ Coxc83b8382009-11-02 18:37:30 -0800135 if e.Addr != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800136 s += " " + e.Addr.String()
Russ Cox35ace1d2009-11-01 11:15:34 -0800137 }
Russ Coxeb692922011-11-01 22:05:34 -0400138 s += ": " + e.Err.Error()
Robert Griesemera3d10452009-12-15 15:35:38 -0800139 return s
Russ Cox35ace1d2009-11-01 11:15:34 -0800140}
141
Russ Cox47a05332010-04-26 22:15:25 -0700142type temporary interface {
143 Temporary() bool
144}
145
146func (e *OpError) Temporary() bool {
Russ Coxeb692922011-11-01 22:05:34 -0400147 t, ok := e.Err.(temporary)
Russ Cox47a05332010-04-26 22:15:25 -0700148 return ok && t.Temporary()
149}
150
151type timeout interface {
152 Timeout() bool
153}
154
155func (e *OpError) Timeout() bool {
Russ Coxeb692922011-11-01 22:05:34 -0400156 t, ok := e.Err.(timeout)
Russ Cox47a05332010-04-26 22:15:25 -0700157 return ok && t.Timeout()
158}
159
Brad Fitzpatrick01507b92011-12-20 14:32:33 -0800160type timeoutError struct{}
161
162func (e *timeoutError) Error() string { return "i/o timeout" }
163func (e *timeoutError) Timeout() bool { return true }
164func (e *timeoutError) Temporary() bool { return true }
165
166var errTimeout error = &timeoutError{}
167
Russ Cox35ace1d2009-11-01 11:15:34 -0800168type AddrError struct {
Russ Coxeb692922011-11-01 22:05:34 -0400169 Err string
170 Addr string
Russ Cox35ace1d2009-11-01 11:15:34 -0800171}
172
Russ Coxeb692922011-11-01 22:05:34 -0400173func (e *AddrError) Error() string {
Andrew Gerrandfc4ba152010-07-27 17:22:22 +1000174 if e == nil {
175 return "<nil>"
176 }
Russ Coxeb692922011-11-01 22:05:34 -0400177 s := e.Err
Russ Cox35ace1d2009-11-01 11:15:34 -0800178 if e.Addr != "" {
Robert Griesemer40621d52009-11-09 12:07:39 -0800179 s += " " + e.Addr
Russ Cox35ace1d2009-11-01 11:15:34 -0800180 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800181 return s
Russ Cox35ace1d2009-11-01 11:15:34 -0800182}
183
Russ Cox47a05332010-04-26 22:15:25 -0700184func (e *AddrError) Temporary() bool {
185 return false
186}
187
188func (e *AddrError) Timeout() bool {
189 return false
190}
191
Russ Cox35ace1d2009-11-01 11:15:34 -0800192type UnknownNetworkError string
Robert Griesemer5d377052009-11-04 23:16:46 -0800193
Russ Coxeb692922011-11-01 22:05:34 -0400194func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) }
Russ Cox47a05332010-04-26 22:15:25 -0700195func (e UnknownNetworkError) Temporary() bool { return false }
196func (e UnknownNetworkError) Timeout() bool { return false }