1 |
747 |
jeremybenn |
// 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 |
|
|
|
5 |
|
|
// Package net provides a portable interface to Unix networks sockets,
|
6 |
|
|
// including TCP/IP, UDP, domain name resolution, and Unix domain sockets.
|
7 |
|
|
package net
|
8 |
|
|
|
9 |
|
|
// TODO(rsc):
|
10 |
|
|
// support for raw ethernet sockets
|
11 |
|
|
|
12 |
|
|
import (
|
13 |
|
|
"errors"
|
14 |
|
|
"time"
|
15 |
|
|
)
|
16 |
|
|
|
17 |
|
|
// Addr represents a network end point address.
|
18 |
|
|
type Addr interface {
|
19 |
|
|
Network() string // name of the network
|
20 |
|
|
String() string // string form of address
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
// Conn is a generic stream-oriented network connection.
|
24 |
|
|
type Conn interface {
|
25 |
|
|
// Read reads data from the connection.
|
26 |
|
|
// Read can be made to time out and return a Error with Timeout() == true
|
27 |
|
|
// after a fixed time limit; see SetDeadline and SetReadDeadline.
|
28 |
|
|
Read(b []byte) (n int, err error)
|
29 |
|
|
|
30 |
|
|
// Write writes data to the connection.
|
31 |
|
|
// Write can be made to time out and return a Error with Timeout() == true
|
32 |
|
|
// after a fixed time limit; see SetDeadline and SetWriteDeadline.
|
33 |
|
|
Write(b []byte) (n int, err error)
|
34 |
|
|
|
35 |
|
|
// Close closes the connection.
|
36 |
|
|
Close() error
|
37 |
|
|
|
38 |
|
|
// LocalAddr returns the local network address.
|
39 |
|
|
LocalAddr() Addr
|
40 |
|
|
|
41 |
|
|
// RemoteAddr returns the remote network address.
|
42 |
|
|
RemoteAddr() Addr
|
43 |
|
|
|
44 |
|
|
// SetDeadline sets the read and write deadlines associated
|
45 |
|
|
// with the connection.
|
46 |
|
|
SetDeadline(t time.Time) error
|
47 |
|
|
|
48 |
|
|
// SetReadDeadline sets the deadline for all Read calls to return.
|
49 |
|
|
// If the deadline is reached, Read will fail with a timeout
|
50 |
|
|
// (see type Error) instead of blocking.
|
51 |
|
|
// A zero value for t means Read will not time out.
|
52 |
|
|
SetReadDeadline(t time.Time) error
|
53 |
|
|
|
54 |
|
|
// SetWriteDeadline sets the deadline for all Write calls to return.
|
55 |
|
|
// If the deadline is reached, Write will fail with a timeout
|
56 |
|
|
// (see type Error) instead of blocking.
|
57 |
|
|
// A zero value for t means Write will not time out.
|
58 |
|
|
// Even if write times out, it may return n > 0, indicating that
|
59 |
|
|
// some of the data was successfully written.
|
60 |
|
|
SetWriteDeadline(t time.Time) error
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
// An Error represents a network error.
|
64 |
|
|
type Error interface {
|
65 |
|
|
error
|
66 |
|
|
Timeout() bool // Is the error a timeout?
|
67 |
|
|
Temporary() bool // Is the error temporary?
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
// PacketConn is a generic packet-oriented network connection.
|
71 |
|
|
type PacketConn interface {
|
72 |
|
|
// ReadFrom reads a packet from the connection,
|
73 |
|
|
// copying the payload into b. It returns the number of
|
74 |
|
|
// bytes copied into b and the return address that
|
75 |
|
|
// was on the packet.
|
76 |
|
|
// ReadFrom can be made to time out and return
|
77 |
|
|
// an error with Timeout() == true after a fixed time limit;
|
78 |
|
|
// see SetDeadline and SetReadDeadline.
|
79 |
|
|
ReadFrom(b []byte) (n int, addr Addr, err error)
|
80 |
|
|
|
81 |
|
|
// WriteTo writes a packet with payload b to addr.
|
82 |
|
|
// WriteTo can be made to time out and return
|
83 |
|
|
// an error with Timeout() == true after a fixed time limit;
|
84 |
|
|
// see SetDeadline and SetWriteDeadline.
|
85 |
|
|
// On packet-oriented connections, write timeouts are rare.
|
86 |
|
|
WriteTo(b []byte, addr Addr) (n int, err error)
|
87 |
|
|
|
88 |
|
|
// Close closes the connection.
|
89 |
|
|
Close() error
|
90 |
|
|
|
91 |
|
|
// LocalAddr returns the local network address.
|
92 |
|
|
LocalAddr() Addr
|
93 |
|
|
|
94 |
|
|
// SetDeadline sets the read and write deadlines associated
|
95 |
|
|
// with the connection.
|
96 |
|
|
SetDeadline(t time.Time) error
|
97 |
|
|
|
98 |
|
|
// SetReadDeadline sets the deadline for all Read calls to return.
|
99 |
|
|
// If the deadline is reached, Read will fail with a timeout
|
100 |
|
|
// (see type Error) instead of blocking.
|
101 |
|
|
// A zero value for t means Read will not time out.
|
102 |
|
|
SetReadDeadline(t time.Time) error
|
103 |
|
|
|
104 |
|
|
// SetWriteDeadline sets the deadline for all Write calls to return.
|
105 |
|
|
// If the deadline is reached, Write will fail with a timeout
|
106 |
|
|
// (see type Error) instead of blocking.
|
107 |
|
|
// A zero value for t means Write will not time out.
|
108 |
|
|
// Even if write times out, it may return n > 0, indicating that
|
109 |
|
|
// some of the data was successfully written.
|
110 |
|
|
SetWriteDeadline(t time.Time) error
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
// A Listener is a generic network listener for stream-oriented protocols.
|
114 |
|
|
type Listener interface {
|
115 |
|
|
// Accept waits for and returns the next connection to the listener.
|
116 |
|
|
Accept() (c Conn, err error)
|
117 |
|
|
|
118 |
|
|
// Close closes the listener.
|
119 |
|
|
Close() error
|
120 |
|
|
|
121 |
|
|
// Addr returns the listener's network address.
|
122 |
|
|
Addr() Addr
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
var errMissingAddress = errors.New("missing address")
|
126 |
|
|
|
127 |
|
|
type OpError struct {
|
128 |
|
|
Op string
|
129 |
|
|
Net string
|
130 |
|
|
Addr Addr
|
131 |
|
|
Err error
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
func (e *OpError) Error() string {
|
135 |
|
|
if e == nil {
|
136 |
|
|
return ""
|
137 |
|
|
}
|
138 |
|
|
s := e.Op
|
139 |
|
|
if e.Net != "" {
|
140 |
|
|
s += " " + e.Net
|
141 |
|
|
}
|
142 |
|
|
if e.Addr != nil {
|
143 |
|
|
s += " " + e.Addr.String()
|
144 |
|
|
}
|
145 |
|
|
s += ": " + e.Err.Error()
|
146 |
|
|
return s
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
type temporary interface {
|
150 |
|
|
Temporary() bool
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
func (e *OpError) Temporary() bool {
|
154 |
|
|
t, ok := e.Err.(temporary)
|
155 |
|
|
return ok && t.Temporary()
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
type timeout interface {
|
159 |
|
|
Timeout() bool
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
func (e *OpError) Timeout() bool {
|
163 |
|
|
t, ok := e.Err.(timeout)
|
164 |
|
|
return ok && t.Timeout()
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
type timeoutError struct{}
|
168 |
|
|
|
169 |
|
|
func (e *timeoutError) Error() string { return "i/o timeout" }
|
170 |
|
|
func (e *timeoutError) Timeout() bool { return true }
|
171 |
|
|
func (e *timeoutError) Temporary() bool { return true }
|
172 |
|
|
|
173 |
|
|
var errTimeout error = &timeoutError{}
|
174 |
|
|
|
175 |
|
|
type AddrError struct {
|
176 |
|
|
Err string
|
177 |
|
|
Addr string
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
func (e *AddrError) Error() string {
|
181 |
|
|
if e == nil {
|
182 |
|
|
return ""
|
183 |
|
|
}
|
184 |
|
|
s := e.Err
|
185 |
|
|
if e.Addr != "" {
|
186 |
|
|
s += " " + e.Addr
|
187 |
|
|
}
|
188 |
|
|
return s
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
func (e *AddrError) Temporary() bool {
|
192 |
|
|
return false
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
func (e *AddrError) Timeout() bool {
|
196 |
|
|
return false
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
type UnknownNetworkError string
|
200 |
|
|
|
201 |
|
|
func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) }
|
202 |
|
|
func (e UnknownNetworkError) Temporary() bool { return false }
|
203 |
|
|
func (e UnknownNetworkError) Timeout() bool { return false }
|
204 |
|
|
|
205 |
|
|
// DNSConfigError represents an error reading the machine's DNS configuration.
|
206 |
|
|
type DNSConfigError struct {
|
207 |
|
|
Err error
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
func (e *DNSConfigError) Error() string {
|
211 |
|
|
return "error reading DNS config: " + e.Err.Error()
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
func (e *DNSConfigError) Timeout() bool { return false }
|
215 |
|
|
func (e *DNSConfigError) Temporary() bool { return false }
|