Files
net/netdev.go
T
Scott Feldman 693edae782 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).
2023-07-07 20:00:29 +02:00

47 lines
1.6 KiB
Go

package net
import (
"time"
)
// netdev is the current netdev, set by the application with useNetdev()
var netdev netdever
// (useNetdev is go:linkname'd from tinygo/drivers package)
func useNetdev(dev netdever) {
netdev = dev
}
// Netdev is TinyGo's network device driver model. Network drivers implement
// the netdever interface, providing a common network I/O interface to TinyGo's
// "net" package. The interface is modeled after the BSD socket interface.
// net.Conn implementations (TCPConn, UDPConn, and TLSConn) use the netdev
// interface for device I/O access.
//
// A netdever is passed to the "net" package using net.useNetdev().
//
// Just like a net.Conn, multiple goroutines may invoke methods on a netdever
// simultaneously.
//
// NOTE: The netdever interface is mirrored in drivers/netdev.go.
// NOTE: If making changes to this interface, mirror the changes in
// NOTE: drivers/netdev.go, and visa-versa.
type netdever interface {
// GetHostByName returns the IP address of either a hostname or IPv4
// address in standard dot notation
GetHostByName(name string) (IP, error)
// Berkely Sockets-like interface, Go-ified. See man page for socket(2), etc.
Socket(domain int, stype int, protocol int) (int, error)
Bind(sockfd int, ip IP, port int) error
Connect(sockfd int, host string, ip IP, port int) error
Listen(sockfd int, backlog int) error
Accept(sockfd int, ip IP, port int) (int, error)
Send(sockfd int, buf []byte, flags int, timeout time.Duration) (int, error)
Recv(sockfd int, buf []byte, flags int, timeout time.Duration) (int, error)
Close(sockfd int) error
SetSockOpt(sockfd int, level int, opt int, value interface{}) error
}