Skip to content

Commit

Permalink
WIP netlink stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Oct 30, 2023
1 parent 7b5cdbe commit 58ce5e9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ require (
nullprogram.com/x/rng v1.1.0
)

require golang.org/x/sys v0.13.0 // indirect
require (
github.com/vishvananda/netlink v1.1.0 // indirect
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
golang.org/x/sys v0.13.0 // indirect
)

retract (
v0.9.1 // premature (race condition)
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
nullprogram.com/x/rng v1.1.0 h1:SMU7DHaQSWtKJNTpNFIFt8Wd/KSmOuSDPXrMFp/UMro=
Expand Down
26 changes: 26 additions & 0 deletions linux/net/netlink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build linux

package net

import (
"errors"
"fmt"
"syscall"

"github.com/vishvananda/netlink"
)

var ErrNoDefaultRoute = errors.New("no default route")

func getDefaultIPv4RouteString() (string, error) {
routes, err := netlink.RouteListFiltered(syscall.AF_INET, &netlink.Route{Dst: nil}, netlink.RT_FILTER_DST)
if err != nil {
return "", fmt.Errorf("failed to get default route with error: %w", err)
}
for _, route := range routes {
if route.Dst == nil {
return route.Gw.String(), nil
}
}
return "", ErrNoDefaultRoute
}
43 changes: 43 additions & 0 deletions linux/net/netlink_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build linux

package net

import (
"bytes"
"os"
"os/exec"
"strings"
"testing"
)

func Test_getDefaultIPv4Route(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("skipping test; must be root to run")
}
// use cmd exec to parse and check `ip route show default` for a control value
cmd := exec.Command("ip", "route", "show", "default")
out, err := cmd.Output()
if err != nil {
t.Skipf("failed to get default route with error: %e", err)
}
if len(out) < 1 {
t.Skip("failed to get default route control value")
}
fields := bytes.Fields(out)
if len(fields) < 3 {
t.Skip("failed to get default route control value")
}
value := fields[2]
if len(value) < 1 {
t.Skip("failed to get default route control value")
}
t.Logf("control value: %s", string(value))
var res string
if res, err = getDefaultIPv4RouteString(); err != nil {
t.Fatalf("failed to get default route with error: %e", err)
}
if !strings.EqualFold(res, string(value)) {
t.Fatalf("failed to get default route with error: %e", err)
}
t.Logf("default route: %s", res)
}

0 comments on commit 58ce5e9

Please sign in to comment.