aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorScott Feldman <[email protected]>2023-03-27 14:15:21 -0700
committerRon Evans <[email protected]>2023-12-06 13:11:44 +0100
commit4229e670ce40eea0775c89b5ca07ba7e53f1e643 (patch)
treea53d596795363f74e697a25393594684bca013f6
parent76a7ad2a3ea27ad1571b445d753d7d7515d327b8 (diff)
downloadtinygo-4229e670ce40eea0775c89b5ca07ba7e53f1e643.tar.gz
tinygo-4229e670ce40eea0775c89b5ca07ba7e53f1e643.zip
Add network device driver model, netdev
This PR adds a network device driver model called netdev. There will be a companion PR for TinyGo drivers to update the netdev drivers and network examples. This PR covers the core "net" package. An RFC for the work is here: #tinygo-org/drivers#487. Some things have changed from the RFC, but nothing major. The "net" package is a partial port of Go's "net" package, version 1.19.3. The src/net/README file has details on what is modified from Go's "net" package. Most "net" features are working as they would in normal Go. TCP/UDP/TLS protocol support is there. As well as HTTP client and server support. Standard Go network packages such as golang.org/x/net/websockets and Paho MQTT client work as-is. Other packages are likely to work as-is. Testing results are here (https://docs.google.com/spreadsheets/d/e/2PACX-1vT0cCjBvwXf9HJf6aJV2Sw198F2ief02gmbMV0sQocKT4y4RpfKv3dh6Jyew8lQW64FouZ8GwA2yjxI/pubhtml?gid=1013173032&single=true).
-rw-r--r--loader/goroot.go2
-rw-r--r--src/crypto/tls/common.go12
-rw-r--r--src/crypto/tls/tls.go63
-rw-r--r--src/os/file_other.go8
-rw-r--r--src/syscall/net.go19
-rw-r--r--src/syscall/syscall_libc_darwin.go1
-rw-r--r--src/syscall/syscall_libc_wasi.go1
7 files changed, 104 insertions, 2 deletions
diff --git a/loader/goroot.go b/loader/goroot.go
index 0da8afa0c..33e24aaa7 100644
--- a/loader/goroot.go
+++ b/loader/goroot.go
@@ -232,6 +232,7 @@ func pathsToOverride(goMinor int, needsSyscallPackage bool) map[string]bool {
"": true,
"crypto/": true,
"crypto/rand/": false,
+ "crypto/tls/": false,
"device/": false,
"examples/": false,
"internal/": true,
@@ -241,6 +242,7 @@ func pathsToOverride(goMinor int, needsSyscallPackage bool) map[string]bool {
"internal/task/": false,
"machine/": false,
"net/": true,
+ "net/http/": false,
"os/": true,
"reflect/": false,
"runtime/": false,
diff --git a/src/crypto/tls/common.go b/src/crypto/tls/common.go
new file mode 100644
index 000000000..f97c47e19
--- /dev/null
+++ b/src/crypto/tls/common.go
@@ -0,0 +1,12 @@
+// TINYGO: The following is copied and modified from Go 1.19.3 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 tls
+
+// ConnectionState records basic TLS details about the connection.
+type ConnectionState struct {
+ // TINYGO: empty; TLS connection offloaded to device
+}
diff --git a/src/crypto/tls/tls.go b/src/crypto/tls/tls.go
new file mode 100644
index 000000000..1d1eee105
--- /dev/null
+++ b/src/crypto/tls/tls.go
@@ -0,0 +1,63 @@
+// TINYGO: The following is copied and modified from Go 1.19.3 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 tls partially implements TLS 1.2, as specified in RFC 5246,
+// and TLS 1.3, as specified in RFC 8446.
+package tls
+
+// BUG(agl): The crypto/tls package only implements some countermeasures
+// against Lucky13 attacks on CBC-mode encryption, and only on SHA1
+// variants. See http://www.isg.rhul.ac.uk/tls/TLStiming.pdf and
+// https://www.imperialviolet.org/2013/02/04/luckythirteen.html.
+
+import (
+ "fmt"
+ "net"
+)
+
+// Client returns a new TLS client side connection
+// using conn as the underlying transport.
+// The config cannot be nil: users must set either ServerName or
+// InsecureSkipVerify in the config.
+func Client(conn net.Conn, config *Config) *net.TLSConn {
+ panic("tls.Client() not implemented")
+ return nil
+}
+
+// DialWithDialer connects to the given network address using dialer.Dial and
+// then initiates a TLS handshake, returning the resulting TLS connection. Any
+// timeout or deadline given in the dialer apply to connection and TLS
+// handshake as a whole.
+//
+// DialWithDialer interprets a nil configuration as equivalent to the zero
+// configuration; see the documentation of Config for the defaults.
+//
+// DialWithDialer uses context.Background internally; to specify the context,
+// use Dialer.DialContext with NetDialer set to the desired dialer.
+func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*net.TLSConn, error) {
+ switch network {
+ case "tcp", "tcp4":
+ default:
+ return nil, fmt.Errorf("Network %s not supported", network)
+ }
+
+ return net.DialTLS(addr)
+}
+
+// Dial connects to the given network address using net.Dial
+// and then initiates a TLS handshake, returning the resulting
+// TLS connection.
+// Dial interprets a nil configuration as equivalent to
+// the zero configuration; see the documentation of Config
+// for the defaults.
+func Dial(network, addr string, config *Config) (*net.TLSConn, error) {
+ return DialWithDialer(new(net.Dialer), network, addr, config)
+}
+
+// Config is a placeholder for future compatibility with
+// tls.Config.
+type Config struct {
+}
diff --git a/src/os/file_other.go b/src/os/file_other.go
index d093e3d18..0ceee0020 100644
--- a/src/os/file_other.go
+++ b/src/os/file_other.go
@@ -42,6 +42,14 @@ func NewFile(fd uintptr, name string) *File {
return &File{&file{handle: stdioFileHandle(fd), name: name}}
}
+// Rename renames (moves) oldpath to newpath.
+// If newpath already exists and is not a directory, Rename replaces it.
+// OS-specific restrictions may apply when oldpath and newpath are in different directories.
+// If there is an error, it will be of type *LinkError.
+func Rename(oldpath, newpath string) error {
+ return ErrNotImplemented
+}
+
// Read reads up to len(b) bytes from machine.Serial.
// It returns the number of bytes read and any error encountered.
func (f stdioFileHandle) Read(b []byte) (n int, err error) {
diff --git a/src/syscall/net.go b/src/syscall/net.go
index 531fa80d8..5f8c50da9 100644
--- a/src/syscall/net.go
+++ b/src/syscall/net.go
@@ -32,3 +32,22 @@ type Conn interface {
// SyscallConn returns a raw network connection.
SyscallConn() (RawConn, error)
}
+
+const (
+ AF_INET = 0x2
+ SOCK_STREAM = 0x1
+ SOCK_DGRAM = 0x2
+ SOL_SOCKET = 0x1
+ SO_KEEPALIVE = 0x9
+ SOL_TCP = 0x6
+ TCP_KEEPINTVL = 0x5
+ IPPROTO_TCP = 0x6
+ IPPROTO_UDP = 0x11
+ F_SETFL = 0x4
+
+ // TINYGO: Made up, not a real IP protocol number. This is used to
+ // create a TLS socket on the device, assuming the device supports mbed
+ // TLS.
+
+ IPPROTO_TLS = 0xFE
+)
diff --git a/src/syscall/syscall_libc_darwin.go b/src/syscall/syscall_libc_darwin.go
index d64f1061f..9abaef51d 100644
--- a/src/syscall/syscall_libc_darwin.go
+++ b/src/syscall/syscall_libc_darwin.go
@@ -53,7 +53,6 @@ const (
DT_UNKNOWN = 0x0
DT_WHT = 0xe
F_GETFL = 0x3
- F_SETFL = 0x4
O_NONBLOCK = 0x4
)
diff --git a/src/syscall/syscall_libc_wasi.go b/src/syscall/syscall_libc_wasi.go
index 29d79b50c..aa2cc38ed 100644
--- a/src/syscall/syscall_libc_wasi.go
+++ b/src/syscall/syscall_libc_wasi.go
@@ -102,7 +102,6 @@ const (
// ../../lib/wasi-libc/expected/wasm32-wasi/predefined-macros.txt
F_GETFL = 3
- F_SETFL = 4
)
// These values are needed as a stub until Go supports WASI as a full target.