two/internal/netif/gateway_linux.go
GnomeZworc 732a293857
f-28: fix: debug somme minor errors
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-05-21 00:15:45 +02:00

29 lines
499 B
Go

//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.Gw == nil {
continue
}
if r.Dst == nil {
return r.Gw, nil
}
if ones, _ := r.Dst.Mask.Size(); ones == 0 {
return r.Gw, nil
}
}
return nil, fmt.Errorf("no default gateway found")
}