-
Notifications
You must be signed in to change notification settings - Fork 8
/
tcpsock.go
336 lines (284 loc) · 7.9 KB
/
tcpsock.go
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// TINYGO: The following is copied and modified from Go 1.21.4 official implementation.
// 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.
package net
import (
"errors"
"fmt"
"internal/itoa"
"io"
"net/netip"
"strconv"
"syscall"
"time"
)
// TCPAddr represents the address of a TCP end point.
type TCPAddr struct {
IP IP
Port int
Zone string // IPv6 scoped addressing zone
}
// AddrPort returns the TCPAddr a as a netip.AddrPort.
//
// If a.Port does not fit in a uint16, it's silently truncated.
//
// If a is nil, a zero value is returned.
func (a *TCPAddr) AddrPort() netip.AddrPort {
if a == nil {
return netip.AddrPort{}
}
na, _ := netip.AddrFromSlice(a.IP)
na = na.WithZone(a.Zone)
return netip.AddrPortFrom(na, uint16(a.Port))
}
// Network returns the address's network name, "tcp".
func (a *TCPAddr) Network() string { return "tcp" }
func (a *TCPAddr) String() string {
if a == nil {
return "<nil>"
}
ip := ipEmptyString(a.IP)
if a.Zone != "" {
return JoinHostPort(ip+"%"+a.Zone, itoa.Itoa(a.Port))
}
return JoinHostPort(ip, itoa.Itoa(a.Port))
}
func (a *TCPAddr) isWildcard() bool {
if a == nil || a.IP == nil {
return true
}
return a.IP.IsUnspecified()
}
func (a *TCPAddr) opAddr() Addr {
if a == nil {
return nil
}
return a
}
// ResolveTCPAddr returns an address of TCP end point.
//
// The network must be a TCP network name.
//
// If the host in the address parameter is not a literal IP address or
// the port is not a literal port number, ResolveTCPAddr resolves the
// address to an address of TCP end point.
// Otherwise, it parses the address as a pair of literal IP address
// and port number.
// The address parameter can use a host name, but this is not
// recommended, because it will return at most one of the host name's
// IP addresses.
//
// See func Dial for a description of the network and address
// parameters.
func ResolveTCPAddr(network, address string) (*TCPAddr, error) {
switch network {
case "tcp", "tcp4":
default:
return nil, fmt.Errorf("Network '%s' not supported", network)
}
switch address {
case ":http":
address = ":80"
}
// TINYGO: Use netdev resolver
host, sport, err := SplitHostPort(address)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(sport)
if err != nil {
return nil, fmt.Errorf("Error parsing port '%s' in address: %s",
sport, err)
}
if host == "" {
return &TCPAddr{Port: port}, nil
}
ip, err := netdev.GetHostByName(host)
if err != nil {
return nil, fmt.Errorf("Lookup of host name '%s' failed: %s", host, err)
}
return &TCPAddr{IP: ip.AsSlice(), Port: port}, nil
}
// TCPAddrFromAddrPort returns addr as a TCPAddr. If addr.IsValid() is false,
// then the returned TCPAddr will contain a nil IP field, indicating an
// address family-agnostic unspecified address.
func TCPAddrFromAddrPort(addr netip.AddrPort) *TCPAddr {
return &TCPAddr{
IP: addr.Addr().AsSlice(),
Zone: addr.Addr().Zone(),
Port: int(addr.Port()),
}
}
// TCPConn is an implementation of the Conn interface for TCP network
// connections.
type TCPConn struct {
fd int
net string
laddr *TCPAddr
raddr *TCPAddr
readDeadline time.Time
writeDeadline time.Time
}
// DialTCP acts like Dial for TCP networks.
//
// The network must be a TCP network name; see func Dial for details.
//
// If laddr is nil, a local address is automatically chosen.
// If the IP field of raddr is nil or an unspecified IP address, the
// local system is assumed.
func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
switch network {
case "tcp", "tcp4":
default:
return nil, errors.New("Network not supported: '" + network + "'")
}
// TINYGO: Use netdev to create TCP socket and connect
if raddr == nil {
raddr = &TCPAddr{}
}
if raddr.IP.IsUnspecified() {
return nil, errors.New("Sorry, localhost isn't available on Tinygo")
} else if len(raddr.IP) != 4 {
return nil, errors.New("only ipv4 supported")
}
fd, err := netdev.Socket(_AF_INET, _SOCK_STREAM, _IPPROTO_TCP)
if err != nil {
return nil, err
}
rip, _ := netip.AddrFromSlice(raddr.IP)
raddrport := netip.AddrPortFrom(rip, uint16(raddr.Port))
if err = netdev.Connect(fd, "", raddrport); err != nil {
netdev.Close(fd)
return nil, err
}
return &TCPConn{
fd: fd,
net: network,
laddr: laddr,
raddr: raddr,
}, nil
}
// TINYGO: Use netdev for Conn methods: Read = Recv, Write = Send, etc.
// SyscallConn returns a raw network connection.
// This implements the syscall.Conn interface.
func (c *TCPConn) SyscallConn() (syscall.RawConn, error) {
return nil, errors.New("SyscallConn not implemented")
}
func (c *TCPConn) Read(b []byte) (int, error) {
n, err := netdev.Recv(c.fd, b, 0, c.readDeadline)
// Turn the -1 socket error into 0 and let err speak for error
if n < 0 {
n = 0
}
if err != nil && err != io.EOF {
err = &OpError{Op: "read", Net: c.net, Source: c.laddr, Addr: c.raddr, Err: err}
}
return n, err
}
func (c *TCPConn) Write(b []byte) (int, error) {
n, err := netdev.Send(c.fd, b, 0, c.writeDeadline)
// Turn the -1 socket error into 0 and let err speak for error
if n < 0 {
n = 0
}
if err != nil {
err = &OpError{Op: "write", Net: c.net, Source: c.laddr, Addr: c.raddr, Err: err}
}
return n, err
}
func (c *TCPConn) Close() error {
return netdev.Close(c.fd)
}
func (c *TCPConn) LocalAddr() Addr {
return c.laddr
}
func (c *TCPConn) RemoteAddr() Addr {
return c.raddr
}
func (c *TCPConn) SetDeadline(t time.Time) error {
c.readDeadline = t
c.writeDeadline = t
return nil
}
// SetLinger sets the behavior of Close on a connection which still
// has data waiting to be sent or to be acknowledged.
//
// If sec < 0 (the default), the operating system finishes sending the
// data in the background.
//
// If sec == 0, the operating system discards any unsent or
// unacknowledged data.
//
// If sec > 0, the data is sent in the background as with sec < 0.
// On some operating systems including Linux, this may cause Close to block
// until all data has been sent or discarded.
// On some operating systems after sec seconds have elapsed any remaining
// unsent data may be discarded.
func (c *TCPConn) SetLinger(sec int) error {
return netdev.SetSockOpt(c.fd, _SOL_SOCKET, _SO_LINGER, sec)
}
// SetKeepAlive sets whether the operating system should send
// keep-alive messages on the connection.
func (c *TCPConn) SetKeepAlive(keepalive bool) error {
return netdev.SetSockOpt(c.fd, _SOL_SOCKET, _SO_KEEPALIVE, keepalive)
}
// SetKeepAlivePeriod sets period between keep-alives.
func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error {
// Units are 1/2 seconds
return netdev.SetSockOpt(c.fd, _SOL_TCP, _TCP_KEEPINTVL, 2*d.Seconds())
}
func (c *TCPConn) SetReadDeadline(t time.Time) error {
c.readDeadline = t
return nil
}
func (c *TCPConn) SetWriteDeadline(t time.Time) error {
c.writeDeadline = t
return nil
}
func (c *TCPConn) CloseWrite() error {
return fmt.Errorf("CloseWrite not implemented")
}
type listener struct {
fd int
laddr *TCPAddr
}
func (l *listener) Accept() (Conn, error) {
fd, raddr, err := netdev.Accept(l.fd)
if err != nil {
return nil, err
}
return &TCPConn{
fd: fd,
net: "tcp",
laddr: l.laddr,
raddr: TCPAddrFromAddrPort(raddr),
}, nil
}
func (l *listener) Close() error {
return netdev.Close(l.fd)
}
func (l *listener) Addr() Addr {
return l.laddr
}
func listenTCP(laddr *TCPAddr) (Listener, error) {
fd, err := netdev.Socket(_AF_INET, _SOCK_STREAM, _IPPROTO_TCP)
if err != nil {
return nil, err
}
laddrport := laddr.AddrPort()
err = netdev.Bind(fd, laddrport)
if err != nil {
return nil, err
}
err = netdev.Listen(fd, 5)
if err != nil {
return nil, err
}
return &listener{fd: fd, laddr: laddr}, nil
}
// TCPListener is a TCP network listener. Clients should typically
// use variables of type Listener instead of assuming TCP.
type TCPListener struct {
listener
}