mirror of
https://github.com/jsimonetti/rtnetlink.git
synced 2026-03-27 06:51:07 +01:00
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>
49 lines
974 B
Go
49 lines
974 B
Go
//go:build linux
|
|
// +build linux
|
|
|
|
package rtnetlink_test
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
|
|
"github.com/jsimonetti/rtnetlink/v2"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// Set the operational state an interface to Up
|
|
func Example_setLinkUp() {
|
|
// Gather the interface Index
|
|
iface, _ := net.InterfaceByName("dummy0")
|
|
|
|
// Dial a connection to the rtnetlink socket
|
|
conn, err := rtnetlink.Dial(nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
// Request the details of the interface
|
|
msg, err := conn.Link.Get(uint32(iface.Index))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
state := msg.Attributes.OperationalState
|
|
// If the link is already up, return immediately
|
|
if state == rtnetlink.OperStateUp || state == rtnetlink.OperStateUnknown {
|
|
return
|
|
}
|
|
|
|
// Set the interface operationally UP
|
|
err = conn.Link.Set(&rtnetlink.LinkMessage{
|
|
Family: unix.AF_UNSPEC,
|
|
Type: msg.Type,
|
|
Index: uint32(iface.Index),
|
|
Flags: unix.IFF_UP,
|
|
Change: unix.IFF_UP,
|
|
})
|
|
|
|
log.Fatal(err)
|
|
}
|