f-28: add GetDefaultGateway to netif
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
bb5698fdda
commit
c0caf1a24c
2 changed files with 35 additions and 0 deletions
23
internal/netif/gateway_linux.go
Normal file
23
internal/netif/gateway_linux.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
//go:build linux
|
||||
|
||||
package netif
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
)
|
||||
|
||||
func GetDefaultGateway() (net.IP, error) {
|
||||
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list routes: %w", err)
|
||||
}
|
||||
for _, r := range routes {
|
||||
if r.Dst == nil && r.Gw != nil {
|
||||
return r.Gw, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("no default gateway found")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue