Skip to content

Commit

Permalink
Add network device driver model, netdev
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
scottfeldman authored and deadprogram committed Dec 4, 2023
1 parent b8c7bf0 commit 9d45d06
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 2 deletions.
2 changes: 2 additions & 0 deletions loader/goroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/crypto/tls/common.go
Original file line number Diff line number Diff line change
@@ -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
}
63 changes: 63 additions & 0 deletions src/crypto/tls/tls.go
Original file line number Diff line number Diff line change
@@ -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 {
}
8 changes: 8 additions & 0 deletions src/os/file_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions src/syscall/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
1 change: 0 additions & 1 deletion src/syscall/syscall_libc_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const (
DT_UNKNOWN = 0x0
DT_WHT = 0xe
F_GETFL = 0x3
F_SETFL = 0x4
O_NONBLOCK = 0x4
)

Expand Down
1 change: 0 additions & 1 deletion src/syscall/syscall_libc_wasi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9d45d06

Please sign in to comment.