diff options
author | gVisor bot <gvisor-bot@google.com> | 2020-07-08 16:36:33 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-07-08 16:36:33 -0700 |
commit | 4f7af437e25382bdf75b880d1bf3184eae725231 (patch) | |
tree | 7c42719b3cba6fb94b2c3071b82d40e594560e04 /test/iptables/iptables_util.go | |
parent | a75d9f7bee72b9d7611cb015e473ac0bed3d9b02 (diff) | |
parent | 14ff2ea9bfc83fb37afe8a5e17e8b8173f85eb68 (diff) |
Merge pull request #3171 from kevinGC:ipv6-kokoro
PiperOrigin-RevId: 320290162
Diffstat (limited to 'test/iptables/iptables_util.go')
-rw-r--r-- | test/iptables/iptables_util.go | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/test/iptables/iptables_util.go b/test/iptables/iptables_util.go index 7146edbb9..d4bc55b24 100644 --- a/test/iptables/iptables_util.go +++ b/test/iptables/iptables_util.go @@ -18,6 +18,7 @@ import ( "fmt" "net" "os/exec" + "strings" "time" "gvisor.dev/gvisor/pkg/test/testutil" @@ -157,8 +158,10 @@ func connectTCP(ip net.IP, port int, timeout time.Duration) error { return nil } -// localAddrs returns a list of local network interface addresses. -func localAddrs() ([]string, error) { +// localAddrs returns a list of local network interface addresses. When ipv6 is +// true, only IPv6 addresses are returned. Otherwise only IPv4 addresses are +// returned. +func localAddrs(ipv6 bool) ([]string, error) { addrs, err := net.InterfaceAddrs() if err != nil { return nil, err @@ -167,7 +170,19 @@ func localAddrs() ([]string, error) { for _, addr := range addrs { addrStrs = append(addrStrs, addr.String()) } - return addrStrs, nil + return filterAddrs(addrStrs, ipv6), nil +} + +func filterAddrs(addrs []string, ipv6 bool) []string { + addrStrs := make([]string, 0, len(addrs)) + for _, addr := range addrs { + // Add only IPv4 or only IPv6 addresses. + parts := strings.Split(addr, "/") + if isIPv6 := net.ParseIP(parts[0]).To4() == nil; isIPv6 == ipv6 { + addrStrs = append(addrStrs, parts[0]) + } + } + return addrStrs } // getInterfaceName returns the name of the interface other than loopback. |