Skip to content

Commit 4bc93e2

Browse files
authored
stub out more types/funcs to compile against golang.org/x/net/internal/socket (#17)
* stub out more types/funcs to compile against golang.org/x/net/internal/socket These are changes need to compile github.com/domainr/dnsr/ with TinyGo. See issue #14. These change are mostly to fix missing symbols in src/net. Missing types and functions are cut-and-pasted from go1.21.4. Functions are stubbed out returning errors.New("not implemented"). DNRS is compiled by running tinygo test: sfeldma@nuc:~/work/dnsr$ tinygo test -target=wasi With this patch, and a corresponding patch for tinygo/ to fixup crypto/tls, you should get a clean compile.
1 parent 4f0d965 commit 4bc93e2

File tree

10 files changed

+410
-1
lines changed

10 files changed

+410
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ src/net
4949
│   ├── status.go
5050
│   ├── transfer.go *
5151
│   └── transport.go *
52+
├── interface.go *
5253
├── ip.go
5354
├── iprawsock.go *
5455
├── ipsock.go *
56+
├── lookup.go *
5557
├── mac.go
5658
├── mac_test.go
5759
├── netdev.go +
@@ -61,10 +63,12 @@ src/net
6163
├── README.md
6264
├── tcpsock.go *
6365
├── tlssock.go +
64-
└── udpsock.go *
66+
├── udpsock.go *
67+
└── unixsock.go *
6568
6669
src/crypto/tls/
6770
├── common.go *
71+
├── ticket.go *
6872
└── tls.go *
6973
```
7074

dial.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ package net
1414

1515
import (
1616
"context"
17+
"errors"
1718
"fmt"
19+
"syscall"
1820
"time"
1921
)
2022

@@ -24,6 +26,9 @@ const (
2426
defaultTCPKeepAlive = 15 * time.Second
2527
)
2628

29+
// mptcpStatus is a tristate for Multipath TCP, see go.dev/issue/56539
30+
type mptcpStatus uint8
31+
2732
// A Dialer contains options for connecting to an address.
2833
//
2934
// The zero value for each field is equivalent to dialing
@@ -146,6 +151,46 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn
146151
return nil, fmt.Errorf("Network %s not supported", network)
147152
}
148153

154+
// ListenConfig contains options for listening to an address.
155+
type ListenConfig struct {
156+
// If Control is not nil, it is called after creating the network
157+
// connection but before binding it to the operating system.
158+
//
159+
// Network and address parameters passed to Control method are not
160+
// necessarily the ones passed to Listen. For example, passing "tcp" to
161+
// Listen will cause the Control function to be called with "tcp4" or "tcp6".
162+
Control func(network, address string, c syscall.RawConn) error
163+
164+
// KeepAlive specifies the keep-alive period for network
165+
// connections accepted by this listener.
166+
// If zero, keep-alives are enabled if supported by the protocol
167+
// and operating system. Network protocols or operating systems
168+
// that do not support keep-alives ignore this field.
169+
// If negative, keep-alives are disabled.
170+
KeepAlive time.Duration
171+
172+
// If mptcpStatus is set to a value allowing Multipath TCP (MPTCP) to be
173+
// used, any call to Listen with "tcp(4|6)" as network will use MPTCP if
174+
// supported by the operating system.
175+
mptcpStatus mptcpStatus
176+
}
177+
178+
// Listen announces on the local network address.
179+
//
180+
// See func Listen for a description of the network and address
181+
// parameters.
182+
func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (Listener, error) {
183+
return nil, errors.New("dial:ListenConfig:Listen not implemented")
184+
}
185+
186+
// ListenPacket announces on the local network address.
187+
//
188+
// See func ListenPacket for a description of the network and address
189+
// parameters.
190+
func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (PacketConn, error) {
191+
return nil, errors.New("dial:ListenConfig:ListenPacket not implemented")
192+
}
193+
149194
// Listen announces on the local network address.
150195
//
151196
// See Go "net" package Listen() for more information.

interface.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// TINYGO: The following is copied and modified from Go 1.21.4 official implementation.
2+
3+
// Copyright 2011 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package net
8+
9+
import (
10+
"errors"
11+
)
12+
13+
// BUG(mikio): On JS, methods and functions related to
14+
// Interface are not implemented.
15+
16+
// BUG(mikio): On AIX, DragonFly BSD, NetBSD, OpenBSD, Plan 9 and
17+
// Solaris, the MulticastAddrs method of Interface is not implemented.
18+
19+
var (
20+
errInvalidInterface = errors.New("invalid network interface")
21+
errInvalidInterfaceIndex = errors.New("invalid network interface index")
22+
errInvalidInterfaceName = errors.New("invalid network interface name")
23+
errNoSuchInterface = errors.New("no such network interface")
24+
errNoSuchMulticastInterface = errors.New("no such multicast network interface")
25+
)
26+
27+
// Interface represents a mapping between network interface name
28+
// and index. It also represents network interface facility
29+
// information.
30+
type Interface struct {
31+
Index int // positive integer that starts at one, zero is never used
32+
MTU int // maximum transmission unit
33+
Name string // e.g., "en0", "lo0", "eth0.100"
34+
HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
35+
Flags Flags // e.g., FlagUp, FlagLoopback, FlagMulticast
36+
}
37+
38+
type Flags uint
39+
40+
const (
41+
FlagUp Flags = 1 << iota // interface is administratively up
42+
FlagBroadcast // interface supports broadcast access capability
43+
FlagLoopback // interface is a loopback interface
44+
FlagPointToPoint // interface belongs to a point-to-point link
45+
FlagMulticast // interface supports multicast access capability
46+
FlagRunning // interface is in running state
47+
)
48+
49+
var flagNames = []string{
50+
"up",
51+
"broadcast",
52+
"loopback",
53+
"pointtopoint",
54+
"multicast",
55+
"running",
56+
}
57+
58+
func (f Flags) String() string {
59+
s := ""
60+
for i, name := range flagNames {
61+
if f&(1<<uint(i)) != 0 {
62+
if s != "" {
63+
s += "|"
64+
}
65+
s += name
66+
}
67+
}
68+
if s == "" {
69+
s = "0"
70+
}
71+
return s
72+
}
73+
74+
// Interfaces returns a list of the system's network interfaces.
75+
func Interfaces() ([]Interface, error) {
76+
return nil, errors.New("Interfaces not implemented")
77+
}
78+
79+
// InterfaceByIndex returns the interface specified by index.
80+
//
81+
// On Solaris, it returns one of the logical network interfaces
82+
// sharing the logical data link; for more precision use
83+
// InterfaceByName.
84+
func InterfaceByIndex(index int) (*Interface, error) {
85+
return nil, errors.New("InterfaceByIndex not implemented")
86+
}

iprawsock.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
package net
88

9+
import (
10+
"errors"
11+
"syscall"
12+
)
13+
914
// BUG(mikio): On every POSIX platform, reads from the "ip4" network
1015
// using the ReadFrom or ReadFromIP method might not return a complete
1116
// IPv4 packet, including its header, even if there is space
@@ -57,3 +62,42 @@ func (a *IPAddr) opAddr() Addr {
5762
}
5863
return a
5964
}
65+
66+
// IPConn is the implementation of the Conn and PacketConn interfaces
67+
// for IP network connections.
68+
type IPConn struct {
69+
conn
70+
}
71+
72+
// SyscallConn returns a raw network connection.
73+
// This implements the syscall.Conn interface.
74+
func (c *IPConn) SyscallConn() (syscall.RawConn, error) {
75+
return nil, errors.New("SyscallConn not implemented")
76+
}
77+
78+
// ReadMsgIP reads a message from c, copying the payload into b and
79+
// the associated out-of-band data into oob. It returns the number of
80+
// bytes copied into b, the number of bytes copied into oob, the flags
81+
// that were set on the message and the source address of the message.
82+
//
83+
// The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
84+
// used to manipulate IP-level socket options in oob.
85+
func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) {
86+
err = errors.New("ReadMsgIP not implemented")
87+
return
88+
}
89+
90+
// ReadFrom implements the PacketConn ReadFrom method.
91+
func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) {
92+
return 0, nil, errors.New("ReadFrom not implemented")
93+
}
94+
95+
// WriteToIP acts like WriteTo but takes an IPAddr.
96+
func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) {
97+
return 0, errors.New("WriteToIP not implemented")
98+
}
99+
100+
// WriteTo implements the PacketConn WriteTo method.
101+
func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) {
102+
return 0, errors.New("WriteTo not implemented")
103+
}

lookup.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// TINYGO: The following is copied and modified from Go 1.21.4 official implementation.
2+
3+
// Copyright 2012 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package net
8+
9+
import (
10+
"errors"
11+
)
12+
13+
// LookupPort looks up the port for the given network and service.
14+
//
15+
// LookupPort uses context.Background internally; to specify the context, use
16+
// Resolver.LookupPort.
17+
func LookupPort(network, service string) (port int, err error) {
18+
return 0, errors.New("net:LookupPort not implemented")
19+
}

net.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package net
88

99
import (
10+
"errors"
1011
"time"
1112
)
1213

@@ -80,6 +81,102 @@ type Conn interface {
8081
SetWriteDeadline(t time.Time) error
8182
}
8283

84+
type conn struct {
85+
// TINYGO: no fd defined
86+
}
87+
88+
// Close closes the connection.
89+
func (c *conn) Close() error {
90+
return errors.New("conn.Close not implemented")
91+
}
92+
93+
// LocalAddr returns the local network address.
94+
// The Addr returned is shared by all invocations of LocalAddr, so
95+
// do not modify it.
96+
func (c *conn) LocalAddr() Addr {
97+
// TINYGO: not implemented
98+
return nil
99+
}
100+
101+
// SetDeadline implements the Conn SetDeadline method.
102+
func (c *conn) SetDeadline(t time.Time) error {
103+
return errors.New("conn.SetDeadline not implemented")
104+
}
105+
106+
// SetReadDeadline implements the Conn SetReadDeadline method.
107+
func (c *conn) SetReadDeadline(t time.Time) error {
108+
return errors.New("conn.SetReadDeadline not implemented")
109+
}
110+
111+
// SetWriteDeadline implements the Conn SetWriteDeadline method.
112+
func (c *conn) SetWriteDeadline(t time.Time) error {
113+
return errors.New("conn.SetWriteDeadline not implemented")
114+
}
115+
116+
// PacketConn is a generic packet-oriented network connection.
117+
//
118+
// Multiple goroutines may invoke methods on a PacketConn simultaneously.
119+
type PacketConn interface {
120+
// ReadFrom reads a packet from the connection,
121+
// copying the payload into p. It returns the number of
122+
// bytes copied into p and the return address that
123+
// was on the packet.
124+
// It returns the number of bytes read (0 <= n <= len(p))
125+
// and any error encountered. Callers should always process
126+
// the n > 0 bytes returned before considering the error err.
127+
// ReadFrom can be made to time out and return an error after a
128+
// fixed time limit; see SetDeadline and SetReadDeadline.
129+
ReadFrom(p []byte) (n int, addr Addr, err error)
130+
131+
// WriteTo writes a packet with payload p to addr.
132+
// WriteTo can be made to time out and return an Error after a
133+
// fixed time limit; see SetDeadline and SetWriteDeadline.
134+
// On packet-oriented connections, write timeouts are rare.
135+
WriteTo(p []byte, addr Addr) (n int, err error)
136+
137+
// Close closes the connection.
138+
// Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
139+
Close() error
140+
141+
// LocalAddr returns the local network address, if known.
142+
LocalAddr() Addr
143+
144+
// SetDeadline sets the read and write deadlines associated
145+
// with the connection. It is equivalent to calling both
146+
// SetReadDeadline and SetWriteDeadline.
147+
//
148+
// A deadline is an absolute time after which I/O operations
149+
// fail instead of blocking. The deadline applies to all future
150+
// and pending I/O, not just the immediately following call to
151+
// Read or Write. After a deadline has been exceeded, the
152+
// connection can be refreshed by setting a deadline in the future.
153+
//
154+
// If the deadline is exceeded a call to Read or Write or to other
155+
// I/O methods will return an error that wraps os.ErrDeadlineExceeded.
156+
// This can be tested using errors.Is(err, os.ErrDeadlineExceeded).
157+
// The error's Timeout method will return true, but note that there
158+
// are other possible errors for which the Timeout method will
159+
// return true even if the deadline has not been exceeded.
160+
//
161+
// An idle timeout can be implemented by repeatedly extending
162+
// the deadline after successful ReadFrom or WriteTo calls.
163+
//
164+
// A zero value for t means I/O operations will not time out.
165+
SetDeadline(t time.Time) error
166+
167+
// SetReadDeadline sets the deadline for future ReadFrom calls
168+
// and any currently-blocked ReadFrom call.
169+
// A zero value for t means ReadFrom will not time out.
170+
SetReadDeadline(t time.Time) error
171+
172+
// SetWriteDeadline sets the deadline for future WriteTo calls
173+
// and any currently-blocked WriteTo call.
174+
// Even if write times out, it may return n > 0, indicating that
175+
// some of the data was successfully written.
176+
// A zero value for t means WriteTo will not time out.
177+
SetWriteDeadline(t time.Time) error
178+
}
179+
83180
// A Listener is a generic network listener for stream-oriented protocols.
84181
//
85182
// Multiple goroutines may invoke methods on a Listener simultaneously.

netdev.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
_SOCK_DGRAM = 0x2
1414
_SOL_SOCKET = 0x1
1515
_SO_KEEPALIVE = 0x9
16+
_SO_LINGER = 0xd
1617
_SOL_TCP = 0x6
1718
_TCP_KEEPINTVL = 0x5
1819
_IPPROTO_TCP = 0x6

0 commit comments

Comments
 (0)