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
|
// Copyright 2023 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.
//go:build js || wasip1
package net
import (
"internal/poll"
"runtime"
"time"
)
const (
readSyscallName = "fd_read"
writeSyscallName = "fd_write"
)
// Network file descriptor.
type netFD struct {
pfd poll.FD
// immutable until Close
family int
sotype int
isConnected bool // handshake completed or use of association with peer
net string
laddr Addr
raddr Addr
// The only networking available in WASI preview 1 is the ability to
// sock_accept on a pre-opened socket, and then fd_read, fd_write,
// fd_close, and sock_shutdown on the resulting connection. We
// intercept applicable netFD calls on this instance, and then pass
// the remainder of the netFD calls to fakeNetFD.
*fakeNetFD
}
func newFD(net string, sysfd int) *netFD {
return newPollFD(net, poll.FD{
Sysfd: sysfd,
IsStream: true,
ZeroReadIsEOF: true,
})
}
func newPollFD(net string, pfd poll.FD) *netFD {
var laddr Addr
var raddr Addr
// WASI preview 1 does not have functions like getsockname/getpeername,
// so we cannot get access to the underlying IP address used by connections.
//
// However, listeners created by FileListener are of type *TCPListener,
// which can be asserted by a Go program. The (*TCPListener).Addr method
// documents that the returned value will be of type *TCPAddr, we satisfy
// the documented behavior by creating addresses of the expected type here.
switch net {
case "tcp":
laddr = new(TCPAddr)
raddr = new(TCPAddr)
case "udp":
laddr = new(UDPAddr)
raddr = new(UDPAddr)
default:
laddr = unknownAddr{}
raddr = unknownAddr{}
}
return &netFD{
pfd: pfd,
net: net,
laddr: laddr,
raddr: raddr,
}
}
func (fd *netFD) init() error {
return fd.pfd.Init(fd.net, true)
}
func (fd *netFD) name() string {
return "unknown"
}
func (fd *netFD) accept() (netfd *netFD, err error) {
if fd.fakeNetFD != nil {
return fd.fakeNetFD.accept(fd.laddr)
}
d, _, errcall, err := fd.pfd.Accept()
if err != nil {
if errcall != "" {
err = wrapSyscallError(errcall, err)
}
return nil, err
}
netfd = newFD("tcp", d)
if err = netfd.init(); err != nil {
netfd.Close()
return nil, err
}
return netfd, nil
}
func (fd *netFD) setAddr(laddr, raddr Addr) {
fd.laddr = laddr
fd.raddr = raddr
runtime.SetFinalizer(fd, (*netFD).Close)
}
func (fd *netFD) Close() error {
if fd.fakeNetFD != nil {
return fd.fakeNetFD.Close()
}
runtime.SetFinalizer(fd, nil)
return fd.pfd.Close()
}
func (fd *netFD) shutdown(how int) error {
if fd.fakeNetFD != nil {
return nil
}
err := fd.pfd.Shutdown(how)
runtime.KeepAlive(fd)
return wrapSyscallError("shutdown", err)
}
func (fd *netFD) Read(p []byte) (n int, err error) {
if fd.fakeNetFD != nil {
return fd.fakeNetFD.Read(p)
}
n, err = fd.pfd.Read(p)
runtime.KeepAlive(fd)
return n, wrapSyscallError(readSyscallName, err)
}
func (fd *netFD) Write(p []byte) (nn int, err error) {
if fd.fakeNetFD != nil {
return fd.fakeNetFD.Write(p)
}
nn, err = fd.pfd.Write(p)
runtime.KeepAlive(fd)
return nn, wrapSyscallError(writeSyscallName, err)
}
func (fd *netFD) SetDeadline(t time.Time) error {
if fd.fakeNetFD != nil {
return fd.fakeNetFD.SetDeadline(t)
}
return fd.pfd.SetDeadline(t)
}
func (fd *netFD) SetReadDeadline(t time.Time) error {
if fd.fakeNetFD != nil {
return fd.fakeNetFD.SetReadDeadline(t)
}
return fd.pfd.SetReadDeadline(t)
}
func (fd *netFD) SetWriteDeadline(t time.Time) error {
if fd.fakeNetFD != nil {
return fd.fakeNetFD.SetWriteDeadline(t)
}
return fd.pfd.SetWriteDeadline(t)
}
type unknownAddr struct{}
func (unknownAddr) Network() string { return "unknown" }
func (unknownAddr) String() string { return "unknown" }
|