Skip to content

Commit

Permalink
NetworkChaos on all address of DNS lookup result (chaos-mesh#693)
Browse files Browse the repository at this point in the history
Signed-off-by: Yang Keao <[email protected]>
  • Loading branch information
YangKeao authored Jul 14, 2020
1 parent eb71a18 commit 4a8ff5c
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions controllers/networkchaos/netutils/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

package netutils

import "net"
import (
"net"
"strings"
)

// IPToCidr converts from an ip to a full mask cidr
func IPToCidr(ip string) string {
Expand All @@ -31,28 +34,36 @@ func ResolveCidrs(names []string) ([]string, error) {
return nil, err
}

cidrs = append(cidrs, cidr)
cidrs = append(cidrs, cidr...)
}

return cidrs, nil
}

// ResolveCidr converts cidr/ip/domain into cidr
func ResolveCidr(name string) (string, error) {
func ResolveCidr(name string) ([]string, error) {
_, ipnet, err := net.ParseCIDR(name)
if err == nil {
return ipnet.String(), nil
return []string{ipnet.String()}, nil
}

if net.ParseIP(name) != nil {
return IPToCidr(name), nil
return []string{IPToCidr(name)}, nil
}

addrs, err := net.LookupIP(name)
if err != nil {
return "", err
return nil, err
}

// TODO: support IPv6
return IPToCidr(addrs[0].String()), nil
cidrs := []string{}
for _, addr := range addrs {
addr := addr.String()

// TODO: support IPv6
if strings.Contains(addr, ".") {
cidrs = append(cidrs, IPToCidr(addr))
}
}
return cidrs, nil
}

0 comments on commit 4a8ff5c

Please sign in to comment.