rtnetlink/example_neigh_list_test.go
Ben Kochie 8026e5db33
Fix Go mod path (#226)
In order to update to v2.0.0, we need to update the module path per the
Go module documentation.
* https://go.dev/doc/modules/major-version

Fixes: https://github.com/jsimonetti/rtnetlink/issues/225

This can be released as v2.0.1

Signed-off-by: SuperQ <superq@gmail.com>
2024-05-10 16:40:56 +02:00

51 lines
942 B
Go

//go:build linux
// +build linux
package rtnetlink_test
import (
"log"
"net"
"github.com/jsimonetti/rtnetlink/v2"
"golang.org/x/sys/unix"
)
// List all neighbors on interface 'lo'
func Example_listNeighbors() {
// Gather the interface Index
iface, _ := net.InterfaceByName("lo")
// Get an ip address to add to the interface
family := uint8(unix.AF_INET)
// Dial a connection to the rtnetlink socket
conn, err := rtnetlink.Dial(nil)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
// Request all neighbors
msg, err := conn.Neigh.List()
if err != nil {
log.Fatal(err)
}
// Filter neighbors by family and interface index
var neigh []rtnetlink.NeighMessage
for _, v := range msg {
add := true
if iface != nil && v.Index != uint32(iface.Index) {
add = false
}
if family != 0 && v.Family != uint16(family) {
add = false
}
if add {
neigh = append(neigh, v)
}
}
log.Printf("%#v", neigh)
}