mirror of
https://github.com/tinygo-org/net.git
synced 2026-07-26 08:18:39 +00:00
d5da3ddeef
* net: add UDP listen, resolver, DNSError, and TCP listener APIs for wasm TinyGo's net package was missing several symbols that upstream Go's js/wasm net declares, blocking builds that pull pion/transport, pion/dtls, and similar (via netbird's WASM client). Add them, backed by the existing netdev abstraction so they compile for all targets and no-op cleanly under nopNetdev (matching Go's js runtime behavior): - DNSError type (dnserror.go), copied from Go 1.26.2. - Resolver + DefaultResolver with LookupHost/LookupIP/LookupIPAddr/ LookupNetIP/LookupPort; LookupHost/LookupIP package funcs; Dialer.Resolver field for API compatibility. - UDPConn: ListenUDP, ReadFromUDP(AddrPort), WriteToUDP(AddrPort), SetReadBuffer, SetWriteBuffer. - TCPConn: CloseRead, SetNoDelay, SetReadBuffer, SetWriteBuffer, ReadFrom(io.Reader). - TCPListener: ListenTCP, AcceptTCP, SetDeadline; ListenPacket package func. - Interface: Addrs, MulticastAddrs stubs. * more complete PR for adding more net support (#64) * format unixsock
117 lines
3.7 KiB
Go
117 lines
3.7 KiB
Go
// TINYGO: The following is copied and modified from Go 1.26.2 official implementation.
|
|
|
|
// Copyright 2011 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 net
|
|
|
|
import (
|
|
"errors"
|
|
)
|
|
|
|
// BUG(mikio): On JS, methods and functions related to
|
|
// Interface are not implemented.
|
|
|
|
// BUG(mikio): On AIX, DragonFly BSD, NetBSD, OpenBSD, Plan 9 and
|
|
// Solaris, the MulticastAddrs method of Interface is not implemented.
|
|
|
|
var (
|
|
errInvalidInterface = errors.New("invalid network interface")
|
|
errInvalidInterfaceIndex = errors.New("invalid network interface index")
|
|
errInvalidInterfaceName = errors.New("invalid network interface name")
|
|
errNoSuchInterface = errors.New("no such network interface")
|
|
errNoSuchMulticastInterface = errors.New("no such multicast network interface")
|
|
)
|
|
|
|
// Interface represents a mapping between network interface name
|
|
// and index. It also represents network interface facility
|
|
// information.
|
|
type Interface struct {
|
|
Index int // positive integer that starts at one, zero is never used
|
|
MTU int // maximum transmission unit
|
|
Name string // e.g., "en0", "lo0", "eth0.100"; may be the empty string
|
|
HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
|
|
Flags Flags // e.g., FlagUp, FlagLoopback, FlagMulticast
|
|
}
|
|
|
|
// Addrs returns a list of unicast interface addresses for a specific
|
|
// interface.
|
|
//
|
|
// TINYGO: not implemented; netdev exposes no per-interface address list.
|
|
func (ifi *Interface) Addrs() ([]Addr, error) {
|
|
return nil, errors.New("Interface.Addrs not implemented")
|
|
}
|
|
|
|
// MulticastAddrs returns a list of multicast, joined group addresses
|
|
// for a specific interface.
|
|
//
|
|
// TINYGO: not implemented; netdev exposes no per-interface address list.
|
|
func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
|
|
return nil, errors.New("Interface.MulticastAddrs not implemented")
|
|
}
|
|
|
|
type Flags uint
|
|
|
|
const (
|
|
FlagUp Flags = 1 << iota // interface is administratively up
|
|
FlagBroadcast // interface supports broadcast access capability
|
|
FlagLoopback // interface is a loopback interface
|
|
FlagPointToPoint // interface belongs to a point-to-point link
|
|
FlagMulticast // interface supports multicast access capability
|
|
FlagRunning // interface is in running state
|
|
)
|
|
|
|
var flagNames = []string{
|
|
"up",
|
|
"broadcast",
|
|
"loopback",
|
|
"pointtopoint",
|
|
"multicast",
|
|
"running",
|
|
}
|
|
|
|
func (f Flags) String() string {
|
|
s := ""
|
|
for i, name := range flagNames {
|
|
if f&(1<<uint(i)) != 0 {
|
|
if s != "" {
|
|
s += "|"
|
|
}
|
|
s += name
|
|
}
|
|
}
|
|
if s == "" {
|
|
s = "0"
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Interfaces returns a list of the system's network interfaces.
|
|
func Interfaces() ([]Interface, error) {
|
|
return nil, errors.New("Interfaces not implemented")
|
|
}
|
|
|
|
// InterfaceAddrs returns a list of the system's unicast interface
|
|
// addresses.
|
|
//
|
|
// The returned list does not identify the associated interface; use
|
|
// Interfaces and [Interface.Addrs] for more detail.
|
|
func InterfaceAddrs() ([]Addr, error) {
|
|
return nil, errors.New("InterfaceAddrs not implemented")
|
|
}
|
|
|
|
// InterfaceByIndex returns the interface specified by index.
|
|
//
|
|
// On Solaris, it returns one of the logical network interfaces
|
|
// sharing the logical data link; for more precision use
|
|
// [InterfaceByName].
|
|
func InterfaceByIndex(index int) (*Interface, error) {
|
|
return nil, errors.New("InterfaceByIndex not implemented")
|
|
}
|
|
|
|
// InterfaceByName returns the interface specified by name.
|
|
func InterfaceByName(name string) (*Interface, error) {
|
|
return nil, errors.New("InterfaceByName not implemented")
|
|
}
|