From c0caf1a24cb9022d6259e0f4f42c332d9ad787b7 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Mon, 18 May 2026 22:49:21 +0200 Subject: [PATCH] f-28: add GetDefaultGateway to netif Signed-off-by: GnomeZworc --- internal/netif/gateway_linux.go | 23 +++++++++++++++++++++++ internal/netif/gateway_other.go | 12 ++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 internal/netif/gateway_linux.go create mode 100644 internal/netif/gateway_other.go diff --git a/internal/netif/gateway_linux.go b/internal/netif/gateway_linux.go new file mode 100644 index 0000000..7636b7a --- /dev/null +++ b/internal/netif/gateway_linux.go @@ -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") +} diff --git a/internal/netif/gateway_other.go b/internal/netif/gateway_other.go new file mode 100644 index 0000000..764867c --- /dev/null +++ b/internal/netif/gateway_other.go @@ -0,0 +1,12 @@ +//go:build !linux + +package netif + +import ( + "fmt" + "net" +) + +func GetDefaultGateway() (net.IP, error) { + return nil, fmt.Errorf("not supported on this platform") +}