1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The net package provides a portable interface to Unix
// networks sockets, including TCP/IP, UDP, domain name
// resolution, and Unix domain sockets.
package net
// TODO(rsc):
// support for raw IP sockets
// support for raw ethernet sockets
import "os"
// Addr represents a network end point address.
type Addr interface {
Network() string; // name of the network
String() string; // string form of address
}
// Conn is a generic stream-oriented network connection.
type Conn interface {
// Read reads data from the connection.
// Read can be made to time out and return err == os.EAGAIN
// after a fixed time limit; see SetTimeout and SetReadTimeout.
Read(b []byte) (n int, err os.Error);
// Write writes data to the connection.
// Write can be made to time out and return err == os.EAGAIN
// after a fixed time limit; see SetTimeout and SetReadTimeout.
Write(b []byte) (n int, err os.Error);
// Close closes the connection.
Close() os.Error;
// LocalAddr returns the local network address.
LocalAddr() Addr;
// RemoteAddr returns the remote network address.
RemoteAddr() Addr;
// SetTimeout sets the read and write deadlines associated
// with the connection.
SetTimeout(nsec int64) os.Error;
// SetReadTimeout sets the time (in nanoseconds) that
// Read will wait for data before returning os.EAGAIN.
// Setting nsec == 0 (the default) disables the deadline.
SetReadTimeout(nsec int64) os.Error;
// SetWriteTimeout sets the time (in nanoseconds) that
// Write will wait to send its data before returning os.EAGAIN.
// Setting nsec == 0 (the default) disables the deadline.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
SetWriteTimeout(nsec int64) os.Error;
}
// PacketConn is a generic packet-oriented network connection.
type PacketConn interface {
// ReadFrom reads a packet from the connection,
// copying the payload into b. It returns the number of
// bytes copied into b and the return address that
// was on the packet.
// ReadFrom can be made to time out and return err == os.EAGAIN
// after a fixed time limit; see SetTimeout and SetReadTimeout.
ReadFrom(b []byte) (n int, addr Addr, err os.Error);
// WriteTo writes a packet with payload b to addr.
// WriteTo can be made to time out and return err == os.EAGAIN
// after a fixed time limit; see SetTimeout and SetWriteTimeout.
// On packet-oriented connections, write timeouts are rare.
WriteTo(b []byte, addr Addr) (n int, err os.Error);
// Close closes the connection.
Close() os.Error;
// LocalAddr returns the local network address.
LocalAddr() Addr;
// SetTimeout sets the read and write deadlines associated
// with the connection.
SetTimeout(nsec int64) os.Error;
// SetReadTimeout sets the time (in nanoseconds) that
// Read will wait for data before returning os.EAGAIN.
// Setting nsec == 0 (the default) disables the deadline.
SetReadTimeout(nsec int64) os.Error;
// SetWriteTimeout sets the time (in nanoseconds) that
// Write will wait to send its data before returning os.EAGAIN.
// Setting nsec == 0 (the default) disables the deadline.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
SetWriteTimeout(nsec int64) os.Error;
}
// A Listener is a generic network listener for stream-oriented protocols.
// Accept waits for the next connection and Close closes the connection.
type Listener interface {
Accept() (c Conn, err os.Error);
Close() os.Error;
Addr() Addr; // Listener's network address
}
// Dial connects to the remote address raddr on the network net.
// If the string laddr is not empty, it is used as the local address
// for the connection.
//
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
// "udp", "udp4" (IPv4-only), and "udp6" (IPv6-only).
//
// For IP networks, addresses have the form host:port. If host is
// a literal IPv6 address, it must be enclosed in square brackets.
//
// Examples:
// Dial("tcp", "", "12.34.56.78:80")
// Dial("tcp", "", "google.com:80")
// Dial("tcp", "", "[de:ad:be:ef::ca:fe]:80")
// Dial("tcp", "127.0.0.1:123", "127.0.0.1:88")
//
func Dial(net, laddr, raddr string) (c Conn, err os.Error) {
switch net {
case "tcp", "tcp4", "tcp6":
var la, ra *TCPAddr;
if laddr != "" {
if la, err = ResolveTCPAddr(laddr); err != nil {
goto Error
}
}
if raddr != "" {
if ra, err = ResolveTCPAddr(raddr); err != nil {
goto Error
}
}
return DialTCP(net, la, ra);
case "udp", "udp4", "upd6":
var la, ra *UDPAddr;
if laddr != "" {
if la, err = ResolveUDPAddr(laddr); err != nil {
goto Error
}
}
if raddr != "" {
if ra, err = ResolveUDPAddr(raddr); err != nil {
goto Error
}
}
return DialUDP(net, la, ra);
case "unix", "unixgram":
var la, ra *UnixAddr;
if raddr != "" {
if ra, err = ResolveUnixAddr(net, raddr); err != nil {
goto Error
}
}
if laddr != "" {
if la, err = ResolveUnixAddr(net, laddr); err != nil {
goto Error
}
}
return DialUnix(net, la, ra);
}
err = UnknownNetworkError(net);
Error:
return nil, &OpError{"dial", net + " " + raddr, nil, err};
}
// Listen announces on the local network address laddr.
// The network string net must be a stream-oriented
// network: "tcp", "tcp4", "tcp6", or "unix".
func Listen(net, laddr string) (l Listener, err os.Error) {
switch net {
case "tcp", "tcp4", "tcp6":
var la *TCPAddr;
if laddr != "" {
if la, err = ResolveTCPAddr(laddr); err != nil {
return nil, err
}
}
l, err := ListenTCP(net, la);
if err != nil {
return nil, err
}
return l, nil;
case "unix":
var la *UnixAddr;
if laddr != "" {
if la, err = ResolveUnixAddr(net, laddr); err != nil {
return nil, err
}
}
l, err := ListenUnix(net, la);
if err != nil {
return nil, err
}
return l, nil;
}
return nil, UnknownNetworkError(net);
}
// ListenPacket announces on the local network address laddr.
// The network string net must be a packet-oriented network:
// "udp", "udp4", "udp6", or "unixgram".
func ListenPacket(net, laddr string) (c PacketConn, err os.Error) {
switch net {
case "udp", "udp4", "udp6":
var la *UDPAddr;
if laddr != "" {
if la, err = ResolveUDPAddr(laddr); err != nil {
return nil, err
}
}
c, err := ListenUDP(net, la);
if err != nil {
return nil, err
}
return c, nil;
case "unixgram":
var la *UnixAddr;
if laddr != "" {
if la, err = ResolveUnixAddr(net, laddr); err != nil {
return nil, err
}
}
c, err := DialUnix(net, la, nil);
if err != nil {
return nil, err
}
return c, nil;
}
return nil, UnknownNetworkError(net);
}
var errMissingAddress = os.ErrorString("missing address")
type OpError struct {
Op string;
Net string;
Addr Addr;
Error os.Error;
}
func (e *OpError) String() string {
s := e.Op;
if e.Net != "" {
s += " " + e.Net
}
if e.Addr != nil {
s += " " + e.Addr.String()
}
s += ": " + e.Error.String();
return s;
}
type AddrError struct {
Error string;
Addr string;
}
func (e *AddrError) String() string {
s := e.Error;
if e.Addr != "" {
s += " " + e.Addr
}
return s;
}
type UnknownNetworkError string
func (e UnknownNetworkError) String() string { return "unknown network " + string(e) }
|