Documentation
¶
Overview ¶
Package netdev implements a host OS Netdever for TinyGo.
TinyGo's net package routes all networking through a Netdever registered via UseNetdev. Embedded Wi-Fi drivers fill that role on microcontrollers; this package fills it on Linux, macOS, and Windows so the same net/http code can run under TinyGo on a desktop host.
Compatible with the Netdever interface described by https://github.com/tinygo-org/drivers/tree/dev/netdev
Usage:
import _ "github.com/shibukawa/tinygodriver/netdev"
The blank import registers the host driver during init.
Index ¶
- Constants
- Variables
- func Use(d *Device)
- type Device
- func (d *Device) Accept(sockfd int) (int, netip.AddrPort, error)
- func (d *Device) Addr() (netip.Addr, error)
- func (d *Device) Bind(sockfd int, ip netip.AddrPort) error
- func (d *Device) Close(sockfd int) error
- func (d *Device) Connect(sockfd int, host string, ip netip.AddrPort) error
- func (d *Device) GetHostByName(name string) (netip.Addr, error)
- func (d *Device) Listen(sockfd int, backlog int) error
- func (d *Device) LocalAddr(sockfd int) (netip.AddrPort, error)
- func (d *Device) Recv(sockfd int, buf []byte, flags int, deadline time.Time) (int, error)
- func (d *Device) Send(sockfd int, buf []byte, flags int, deadline time.Time) (int, error)
- func (d *Device) SetSockOpt(sockfd int, level int, opt int, value interface{}) error
- func (d *Device) Socket(domain int, stype int, protocol int) (int, error)
Constants ¶
const ( AF_INET = 0x2 SOCK_STREAM = 0x1 SOCK_DGRAM = 0x2 SOL_SOCKET = 0x1 SO_KEEPALIVE = 0x9 SO_LINGER = 0xd SOL_TCP = 0x6 TCP_KEEPINTVL = 0x5 IPPROTO_TCP = 0x6 IPPROTO_UDP = 0x11 // Made up; TLS is implemented by the host netdev on desktop targets. IPPROTO_TLS = 0xFE F_SETFL = 0x4 )
BSD-socket style constants mirrored from tinygo-org/drivers/netdev. Values match the abstract constants TinyGo's net package passes to drivers (not always the host OS numeric values).
const NetdevDNSEnv = "NETDEV_DNS"
NetdevDNSEnv names the environment variable that overrides which resolvers are used. The value is a comma or space separated list of IPv4 addresses, each with an optional :port that defaults to 53:
NETDEV_DNS=10.0.0.53 NETDEV_DNS=10.0.0.53,10.0.0.54:5353
It exists because the configuration this package can discover on its own is not always the configuration the machine actually uses. On windows there is no /etc/resolv.conf at all, so without this every lookup went to 8.8.8.8 and no internal name could ever resolve. On macOS /etc/resolv.conf is a legacy file that the OS itself documents as not consulted, so it misses the domain-scoped resolvers a VPN installs.
Variables ¶
var ( ErrHostUnknown = errors.New("Host unknown") ErrMalAddr = errors.New("Malformed address") ErrFamilyNotSupported = errors.New("Address family not supported") ErrProtocolNotSupported = errors.New("Socket protocol/type not supported") ErrNoMoreSockets = errors.New("No more sockets") ErrClosingSocket = errors.New("Error closing socket") ErrNotSupported = errors.New("Not supported") ErrInvalidSocketFd = errors.New("Invalid socket fd") ErrTimeout = &timeoutError{} )
Errors aligned with tinygo-org/drivers/netdev.
var ( ErrAddrNotAvailable = errors.New("can't assign requested address") ErrAddrInUse = errors.New("address already in use") ErrConnRefused = errors.New("connection refused") ErrConnReset = errors.New("connection reset by peer") ErrNotConnected = errors.New("socket is not connected") ErrConnTimedOut = errors.New("connection timed out") ErrWouldBlock = errors.New("resource temporarily unavailable") ErrSyscall = errors.New("syscall error") )
Socket error classes shared by the three backends. Each platform maps its native code onto one of these, so application and test code can branch with errors.Is regardless of the OS. Messages match the standard Go wording.
Functions ¶
Types ¶
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device is a host OS implementation of Netdever.
func (*Device) LocalAddr ¶ added in v1.0.5
LocalAddr returns the address the OS assigned to sockfd, including the port chosen for a bind on port 0.
This is an extension beyond the Netdever interface. TinyGo's net.Listener keeps its own copy of the requested address and has no way to receive this value, so net.Listener.Addr() still reports port 0; see requirement:netdev-bound-port.