From 17d12e9a9663d6af6a1929e09399f085812340e9 Mon Sep 17 00:00:00 2001 From: Scott Feldman Date: Mon, 27 Mar 2023 14:15:21 -0700 Subject: [PATCH] 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). --- loader/goroot.go | 2 + src/crypto/tls/common.go | 12 ++++++ src/crypto/tls/tls.go | 63 ++++++++++++++++++++++++++++++ src/os/file_other.go | 8 ++++ src/syscall/net.go | 19 +++++++++ src/syscall/syscall_libc_darwin.go | 1 - src/syscall/syscall_libc_wasi.go | 1 - 7 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/crypto/tls/common.go create mode 100644 src/crypto/tls/tls.go diff --git a/loader/goroot.go b/loader/goroot.go index 0da8afa0c7..33e24aaa70 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 0000000000..f97c47e19c --- /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 0000000000..1d1eee105c --- /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 d093e3d184..0ceee0020b 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 531fa80d8f..5f8c50da9a 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 d64f1061f3..9abaef51d9 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 29d79b50c1..aa2cc38ed0 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.