-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b5cdbe
commit 58ce5e9
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |