diff --git a/net.go b/net.go index 768d4b2..da601f7 100644 --- a/net.go +++ b/net.go @@ -192,6 +192,16 @@ type Net interface { // Interfaces returns a list of the system's network interfaces. Interfaces() ([]*Interface, error) + // 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. + InterfaceByIndex(index int) (*Interface, error) + + // InterfaceByName returns the interface specified by name. + InterfaceByName(name string) (*Interface, error) + // The following functions are extensions to Go's standard net package CreateDialer(dialer *net.Dialer) Dialer diff --git a/stdnet/net.go b/stdnet/net.go index d30830a..d755bed 100644 --- a/stdnet/net.go +++ b/stdnet/net.go @@ -66,6 +66,21 @@ func (n *Net) Interfaces() ([]*transport.Interface, error) { return n.interfaces, nil } +// 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 (n *Net) InterfaceByIndex(index int) (*transport.Interface, error) { + for _, ifc := range n.interfaces { + if ifc.Index == index { + return ifc, nil + } + } + + return nil, fmt.Errorf("%w: index=%d", transport.ErrInterfaceNotFound, index) +} + // InterfaceByName returns the interface specified by name. func (n *Net) InterfaceByName(name string) (*transport.Interface, error) { for _, ifc := range n.interfaces { diff --git a/vnet/net.go b/vnet/net.go index e3ae0f4..99e303a 100644 --- a/vnet/net.go +++ b/vnet/net.go @@ -91,6 +91,21 @@ func (v *Net) getInterface(ifName string) (*transport.Interface, error) { return v._getInterface(ifName) } +// 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 (v *Net) InterfaceByIndex(index int) (*transport.Interface, error) { + for _, ifc := range v.interfaces { + if ifc.Index == index { + return ifc, nil + } + } + + return nil, fmt.Errorf("%w: index=%d", transport.ErrInterfaceNotFound, index) +} + // InterfaceByName returns the interface specified by name. func (v *Net) InterfaceByName(ifName string) (*transport.Interface, error) { return v.getInterface(ifName)