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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// 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.
// IP sockets stubs for Plan 9
package net
import (
"errors"
"io"
"os"
)
// probeIPv6Stack returns two boolean values. If the first boolean value is
// true, kernel supports basic IPv6 functionality. If the second
// boolean value is true, kernel supports IPv6 IPv4-mapping.
func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
return false, false
}
// parsePlan9Addr parses address of the form [ip!]port (e.g. 127.0.0.1!80).
func parsePlan9Addr(s string) (ip IP, iport int, err error) {
var (
addr IP
p, i int
ok bool
)
addr = IPv4zero // address contains port only
i = byteIndex(s, '!')
if i >= 0 {
addr = ParseIP(s[:i])
if addr == nil {
err = errors.New("net: parsing IP failed")
goto Error
}
}
p, _, ok = dtoi(s[i+1:], 0)
if !ok {
err = errors.New("net: parsing port failed")
goto Error
}
if p < 0 || p > 0xFFFF {
err = &AddrError{"invalid port", string(p)}
goto Error
}
return addr, p, nil
Error:
return nil, 0, err
}
func readPlan9Addr(proto, filename string) (addr Addr, err error) {
var buf [128]byte
f, err := os.Open(filename)
if err != nil {
return
}
n, err := f.Read(buf[:])
if err != nil {
return
}
ip, port, err := parsePlan9Addr(string(buf[:n]))
if err != nil {
return
}
switch proto {
case "tcp":
addr = &TCPAddr{ip, port}
case "udp":
addr = &UDPAddr{ip, port}
default:
return nil, errors.New("unknown protocol " + proto)
}
return addr, nil
}
type plan9Conn struct {
proto, name, dir string
ctl, data *os.File
laddr, raddr Addr
}
func newPlan9Conn(proto, name string, ctl *os.File, laddr, raddr Addr) *plan9Conn {
return &plan9Conn{proto, name, "/net/" + proto + "/" + name, ctl, nil, laddr, raddr}
}
func (c *plan9Conn) ok() bool { return c != nil && c.ctl != nil }
// Implementation of the Conn interface - see Conn for documentation.
// Read implements the net.Conn Read method.
func (c *plan9Conn) Read(b []byte) (n int, err error) {
if !c.ok() {
return 0, os.EINVAL
}
if c.data == nil {
c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0)
if err != nil {
return 0, err
}
}
n, err = c.data.Read(b)
if c.proto == "udp" && err == io.EOF {
n = 0
err = nil
}
return
}
// Write implements the net.Conn Write method.
func (c *plan9Conn) Write(b []byte) (n int, err error) {
if !c.ok() {
return 0, os.EINVAL
}
if c.data == nil {
c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0)
if err != nil {
return 0, err
}
}
return c.data.Write(b)
}
// Close closes the connection.
func (c *plan9Conn) Close() error {
if !c.ok() {
return os.EINVAL
}
err := c.ctl.Close()
if err != nil {
return err
}
if c.data != nil {
err = c.data.Close()
}
c.ctl = nil
c.data = nil
return err
}
// LocalAddr returns the local network address.
func (c *plan9Conn) LocalAddr() Addr {
if !c.ok() {
return nil
}
return c.laddr
}
// RemoteAddr returns the remote network address.
func (c *plan9Conn) RemoteAddr() Addr {
if !c.ok() {
return nil
}
return c.raddr
}
// SetTimeout implements the net.Conn SetTimeout method.
func (c *plan9Conn) SetTimeout(nsec int64) error {
if !c.ok() {
return os.EINVAL
}
return os.EPLAN9
}
// SetReadTimeout implements the net.Conn SetReadTimeout method.
func (c *plan9Conn) SetReadTimeout(nsec int64) error {
if !c.ok() {
return os.EINVAL
}
return os.EPLAN9
}
// SetWriteTimeout implements the net.Conn SetWriteTimeout method.
func (c *plan9Conn) SetWriteTimeout(nsec int64) error {
if !c.ok() {
return os.EINVAL
}
return os.EPLAN9
}
func startPlan9(net string, addr Addr) (ctl *os.File, dest, proto, name string, err error) {
var (
ip IP
port int
)
switch a := addr.(type) {
case *TCPAddr:
proto = "tcp"
ip = a.IP
port = a.Port
case *UDPAddr:
proto = "udp"
ip = a.IP
port = a.Port
default:
err = UnknownNetworkError(net)
return
}
clone, dest, err := queryCS1(proto, ip, port)
if err != nil {
return
}
f, err := os.OpenFile(clone, os.O_RDWR, 0)
if err != nil {
return
}
var buf [16]byte
n, err := f.Read(buf[:])
if err != nil {
return
}
return f, dest, proto, string(buf[:n]), nil
}
func dialPlan9(net string, laddr, raddr Addr) (c *plan9Conn, err error) {
f, dest, proto, name, err := startPlan9(net, raddr)
if err != nil {
return
}
_, err = f.WriteString("connect " + dest)
if err != nil {
return
}
laddr, err = readPlan9Addr(proto, "/net/"+proto+"/"+name+"/local")
if err != nil {
return
}
raddr, err = readPlan9Addr(proto, "/net/"+proto+"/"+name+"/remote")
if err != nil {
return
}
return newPlan9Conn(proto, name, f, laddr, raddr), nil
}
type plan9Listener struct {
proto, name, dir string
ctl *os.File
laddr Addr
}
func listenPlan9(net string, laddr Addr) (l *plan9Listener, err error) {
f, dest, proto, name, err := startPlan9(net, laddr)
if err != nil {
return
}
_, err = f.WriteString("announce " + dest)
if err != nil {
return
}
laddr, err = readPlan9Addr(proto, "/net/"+proto+"/"+name+"/local")
if err != nil {
return
}
l = new(plan9Listener)
l.proto = proto
l.name = name
l.dir = "/net/" + proto + "/" + name
l.ctl = f
l.laddr = laddr
return l, nil
}
func (l *plan9Listener) plan9Conn() *plan9Conn {
return newPlan9Conn(l.proto, l.name, l.ctl, l.laddr, nil)
}
func (l *plan9Listener) acceptPlan9() (c *plan9Conn, err error) {
f, err := os.Open(l.dir + "/listen")
if err != nil {
return
}
var buf [16]byte
n, err := f.Read(buf[:])
if err != nil {
return
}
name := string(buf[:n])
laddr, err := readPlan9Addr(l.proto, l.dir+"/local")
if err != nil {
return
}
raddr, err := readPlan9Addr(l.proto, l.dir+"/remote")
if err != nil {
return
}
return newPlan9Conn(l.proto, name, f, laddr, raddr), nil
}
func (l *plan9Listener) Accept() (c Conn, err error) {
c1, err := l.acceptPlan9()
if err != nil {
return
}
return c1, nil
}
func (l *plan9Listener) Close() error {
if l == nil || l.ctl == nil {
return os.EINVAL
}
return l.ctl.Close()
}
func (l *plan9Listener) Addr() Addr { return l.laddr }
|