v1.4.2 Latest Latest

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 6 Imported by: 21



Package rtnl provides a convenient API on top of the rtnetlink library.



This section is empty.


This section is empty.


func MustParseAddr

func MustParseAddr(s string) *net.IPNet

MustParseAddr wraps ParseAddr, but panics on error. Use to conveniently parse a known-valid or hardcoded address into a function argument.

iface, _ := net.InterfaceByName("enp2s0")
conn.AddrDel(iface, rtnl.MustParseAddr(""))

func ParseAddr

func ParseAddr(s string) (*net.IPNet, error)

ParseAddr parses a CIDR string into a host address and network mask. This is a convenience wrapper around net.ParseCIDR(), which surprisingly returns the network address and mask instead of the host address and mask.


type Conn

type Conn struct {
	Conn *rtnetlink.Conn // a route netlink connection

Conn represents the underlying netlink connection

func Dial

func Dial(cfg *netlink.Config) (*Conn, error)

Dial the netlink socket. Establishes a new connection. The typical initialisation is:

conn, err := rtnl.Dial(nil)
if err != nil {
	log.Fatal("can't establish netlink connection: ", err)
defer conn.Close()
// use conn for your calls

func (*Conn) AddrAdd

func (c *Conn) AddrAdd(ifc *net.Interface, addr *net.IPNet) error

AddrAdd associates an IP-address with an interface.

iface, _ := net.InterfaceByName("lo")
conn.AddrAdd(iface, rtnl.MustParseAddr(""))

func (*Conn) AddrDel

func (c *Conn) AddrDel(ifc *net.Interface, addr *net.IPNet) error

AddrDel revokes an IP-address from an interface.

iface, _ := net.InterfaceByName("lo")
conn.AddrDel(iface, rtnl.MustParseAddr(""))

func (*Conn) Addrs

func (c *Conn) Addrs(ifc *net.Interface, family int) (out []*net.IPNet, err error)

Addrs returns IP addresses matching the interface and address family. To retrieve all addresses configured for the system, run:

conn.Addrs(nil, 0)

func (*Conn) Close

func (c *Conn) Close() error

Close the connection.

func (*Conn) LinkByIndex

func (c *Conn) LinkByIndex(ifindex int) (*net.Interface, error)

LinkByIndex returns an interface by its index. Similar to net.InterfaceByIndex.

func (*Conn) LinkDown

func (c *Conn) LinkDown(ifc *net.Interface) error

LinkDown takes an inteface down, disabling the link.

func (*Conn) LinkSetHardwareAddr

func (c *Conn) LinkSetHardwareAddr(ifc *net.Interface, hw net.HardwareAddr) error

LinkSetHardwareAddr overrides the L2 address (MAC-address) for the interface.

func (*Conn) LinkUp

func (c *Conn) LinkUp(ifc *net.Interface) error

LinkUp drives an inteface up, enabling the link.

func (c *Conn) Links() (r []*net.Interface, err error)

Links return the list of interfaces available on the system.

func (*Conn) Neighbours

func (c *Conn) Neighbours(ifc *net.Interface, family int) (r []*Neigh, err error)

Neighbours lists entries from the neighbor table (e.g. the ARP table).

func (*Conn) RouteAdd

func (c *Conn) RouteAdd(ifc *net.Interface, dst net.IPNet, gw net.IP, options ...RouteOption) (err error)

RouteAdd adds information about a network route.

func (*Conn) RouteDel

func (c *Conn) RouteDel(ifc *net.Interface, dst net.IPNet) error

RouteDel deletes the route to the given destination.

func (*Conn) RouteGet

func (c *Conn) RouteGet(dst net.IP) (*Route, error)

RouteGet gets a single route to the given destination address.

func (*Conn) RouteGetAll

func (c *Conn) RouteGetAll(dst net.IP) (ret []*Route, err error)

RouteGetAll returns all routes to the given destination IP in the main routing table.

func (*Conn) RouteReplace

func (c *Conn) RouteReplace(ifc *net.Interface, dst net.IPNet, gw net.IP, options ...RouteOption) (err error)

RouteReplace adds or replace information about a network route.

type Neigh

type Neigh struct {
	HwAddr    net.HardwareAddr // Link-layer address
	IP        net.IP           // Network-layer address
	Interface *net.Interface   // Network interface

Neigh represents a neighbour table entry (e.g. an entry in the ARP table)

type Route

type Route struct {
	Destination *net.IPNet
	Gateway     net.IP
	Interface   *net.Interface
	Metric      uint32

Route represents a route table entry

type RouteOption

type RouteOption func(*RouteOptions)

RouteOption is the functional options func

func WithRouteAttrs

func WithRouteAttrs(attrs rtnetlink.RouteAttributes) RouteOption

WithRouteAttrs sets the attributes.

func WithRouteSrc

func WithRouteSrc(src *net.IPNet) RouteOption

WithRouteSrc sets the src address.

type RouteOptions

type RouteOptions struct {
	Src   *net.IPNet
	Attrs rtnetlink.RouteAttributes

RouteOptions is the functional options struct

func DefaultRouteOptions

func DefaultRouteOptions(ifc *net.Interface, dst net.IPNet, gw net.IP) *RouteOptions

DefaultRouteOptions defines the default route options.

Jump to

Keyboard shortcuts

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