intf

package
v0.0.0-...-671ff1b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2024 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package intf implements mechanisms to access underlying interfaces in a manner that is agnostic to the underlying implementation. An implementation is included that uses netlink to write to a Linux kernl implementation. Additional implementations can be provided to allow writing to e.g. a gRPCwire implementation.

Index

Constants

View Source
const (
	// RTM_NEWNEIGH is the event sent from netlink when an ARP entry is added.
	RTM_NEWNEIGH uint16 = 28
	// RTM_DELNEIGH is the event sent from netlink when an ARP entry is removed.
	RTM_DELNEIGH uint16 = 29
)

Variables

This section is empty.

Functions

func ARPSubscribe

func ARPSubscribe(updates chan ARPUpdate, done chan struct{}) error

ARPSubscribe subscribes to ARP updates from the underlying accessor.

func AddIP

func AddIP(intf string, addr *net.IPNet) error

AddIP adds an IP address addr to the interface intf.

func AwaitARP

func AwaitARP(ctx context.Context, addr net.IP) (net.HardwareAddr, error)

AwaitARP waits for the IPv4 address addr to be resolved via ARP. It uses the supplied context to determine whether it should continue awaiting the entry. If the ARP entry is resolved, it is returned directly.

func InterfaceState

func InterfaceState(name string, state IntState) error

InterfaceState changes the administrative state of an interface on the local system.

func OverrideAccessor

func OverrideAccessor(acc NetworkAccessor)

OverrideAccessor overrides the package network accessor to custom implementation.

func ValidInterface

func ValidInterface(name string) bool

ValidInterface determines whether the interface name is valid for the current system.

Types

type ARPEntry

type ARPEntry struct {
	// IP is the IP address of the neighbour.
	IP net.IP
	// Interface is the name of the interface on which the ARP entry was learnt.
	Interface *Interface
	// MAC is the MAC address of the neighbour.
	MAC net.HardwareAddr
}

ARPEntry is a representation of an ARP entry on the underlying system.

func ARPList

func ARPList() ([]*ARPEntry, error)

ARPList returns a list of ARP neighbours.

type ARPEvent

type ARPEvent int64

ARPEvent is used to describe a particular event on the ARP table.

const (
	// ARPUnknown is used when the event is not an add or delete.
	ARPUnknown ARPEvent = iota
	// ARPAdd indicates that the ARP neighbour was added.
	ARPAdd
	// ARPDelete inidcates that the ARP neighbour was deleted.
	ARPDelete
)

type ARPUpdate

type ARPUpdate struct {
	// Type indicates whether the event was an add or delete.
	Type ARPEvent
	// Neigh is the neighbour that the change corresponds to.
	Neigh ARPEntry
}

ARPUpdate is used to describe a change to the ARP table.

type IntState

type IntState int64

IntState sets enumerated state values that an interface can use.

const (
	InterfaceStateUnknown IntState = iota
	// InterfaceUp sets a link to administratively and operationally
	// up.
	InterfaceUp
	// InterfaceDown sets a link to administratively and operationally
	// down.
	InterfaceDown
)

type Interface

type Interface struct {
	// Index is an integer index used to refer to the interface.
	Index int
	// Name is the name used to refer to the interface in the system.
	Name string
	// MAC is the MAC address of the interface.
	MAC net.HardwareAddr
	// OperState is the operational status of the interface.
	OperState IntState
	// AdminState is the administrative state of the interface.
	AdminState IntState
}

Interface represents information corresponding to a single interface on the underlying system.

func InterfaceByName

func InterfaceByName(name string) (*Interface, error)

InterfaceByName returns an interface's attributes by the name of the interface.

func Interfaces

func Interfaces() ([]*Interface, error)

Interfaces returns a list of interfaces from the local system.

func (Interface) String

func (i Interface) String() string

String is the string representation of an interface that can be read by humans.

type NetworkAccessor

type NetworkAccessor interface {
	// Interfaces returns a set of interfaces that are present on the local system.
	Interfaces() ([]*Interface, error)
	// Interface retrieves the interface with the specified name.
	Interface(name string) (*Interface, error)
	// InterfaceAdddresses lists the IP addresses configured on a particular interface.
	InterfaceAddresses(name string) ([]*net.IPNet, error)
	// AddInterfaceIP adds address ip to the interface name.
	AddInterfaceIP(name string, ip *net.IPNet) error
	// ARPList lists the set of ARP neighbours on the system.
	ARPList() ([]*ARPEntry, error)
	// ARPSubscribe writes changes to the ARP table to the channel updates, and uses
	// done to indicate that the subscription is complete. The ARPSubscribe function
	// should not be blocking and rather return once it has started a separate goroutine
	// that writes updates to the updates channel, and can be cancelled by sending a
	// message to the done channel.
	ARPSubscribe(updates chan ARPUpdate, done chan struct{}) error
	// InterfaceState manipulates the state of the interface specified by name.
	InterfaceState(name string, state IntState) error
}

NetworkAccessor is an interface implemented by the underlying system access interface. Through implementing the NetworkAccessor interface it is possible to use the functions within this package whilst using a different underlying implementation (e.g., netlink on Linux to access the kernel).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL