Skip to content

Commit

Permalink
Add more interface getters to Net
Browse files Browse the repository at this point in the history
Add more interface getters to Net
  • Loading branch information
stv0g committed Nov 19, 2022
1 parent c9fb36e commit 20b7857
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions net.go
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions stdnet/net.go
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions vnet/net.go
Expand Up @@ -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)
Expand Down

0 comments on commit 20b7857

Please sign in to comment.