diff --git a/interface.go b/interface.go index c05ae36..da78583 100644 --- a/interface.go +++ b/interface.go @@ -37,18 +37,20 @@ type Interface struct { // 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") + if ifi == nil { + return nil, errInvalidInterface + } + return interfaceAddrTable(ifi) } // 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") + if ifi == nil { + return nil, errInvalidInterface + } + return interfaceMulticastAddrTable(ifi) } type Flags uint @@ -89,7 +91,7 @@ func (f Flags) String() string { // Interfaces returns a list of the system's network interfaces. func Interfaces() ([]Interface, error) { - return nil, errors.New("Interfaces not implemented") + return interfaceTable(0) } // InterfaceAddrs returns a list of the system's unicast interface @@ -98,7 +100,7 @@ func Interfaces() ([]Interface, error) { // 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") + return interfaceAddrTable(nil) } // InterfaceByIndex returns the interface specified by index. @@ -107,10 +109,34 @@ func InterfaceAddrs() ([]Addr, error) { // sharing the logical data link; for more precision use // [InterfaceByName]. func InterfaceByIndex(index int) (*Interface, error) { - return nil, errors.New("InterfaceByIndex not implemented") + if index <= 0 { + return nil, errInvalidInterfaceIndex + } + ift, err := interfaceTable(index) + if err != nil { + return nil, err + } + for i := range ift { + if index == ift[i].Index { + return &ift[i], nil + } + } + return nil, errNoSuchInterface } // InterfaceByName returns the interface specified by name. func InterfaceByName(name string) (*Interface, error) { - return nil, errors.New("InterfaceByName not implemented") + if name == "" { + return nil, errInvalidInterfaceName + } + ift, err := interfaceTable(0) + if err != nil { + return nil, err + } + for i := range ift { + if name == ift[i].Name { + return &ift[i], nil + } + } + return nil, errNoSuchInterface } diff --git a/interface_other.go b/interface_other.go new file mode 100644 index 0000000..3dce9a0 --- /dev/null +++ b/interface_other.go @@ -0,0 +1,28 @@ +// 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. + +//go:build !(js || wasip1) + +package net + +import ( + "errors" +) + +// TINYGO: not implemented; netdev exposes neither an interface table nor a +// per-interface address list. + +func interfaceTable(ifindex int) ([]Interface, error) { + return nil, errors.New("Interfaces not implemented") +} + +func interfaceAddrTable(ifi *Interface) ([]Addr, error) { + return nil, errors.New("InterfaceAddrs not implemented") +} + +func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) { + return nil, errors.New("Interface.MulticastAddrs not implemented") +} diff --git a/interface_stub.go b/interface_stub.go new file mode 100644 index 0000000..880b3a2 --- /dev/null +++ b/interface_stub.go @@ -0,0 +1,27 @@ +// 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. + +//go:build js || wasip1 + +package net + +// TINYGO: mirrors upstream net/interface_stub.go. These platforms have no +// interface table at all, so enumeration yields an empty list and no error, +// rather than failing. Callers that merely enumerate then see zero interfaces +// instead of a hard error; InterfaceByIndex/InterfaceByName still report +// errNoSuchInterface, since an empty table contains nothing. + +func interfaceTable(ifindex int) ([]Interface, error) { + return nil, nil +} + +func interfaceAddrTable(ifi *Interface) ([]Addr, error) { + return nil, nil +} + +func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) { + return nil, nil +}