diff options
author | Wataru Ishida <ishida.wataru@lab.ntt.co.jp> | 2016-11-11 10:12:57 +0000 |
---|---|---|
committer | Wataru Ishida <ishida.wataru@lab.ntt.co.jp> | 2016-11-14 02:15:45 +0000 |
commit | 3f2dd34d37b5ca4874399d7eaff9bbb8b7975cd4 (patch) | |
tree | 50a37b0a7b09d9f11b6a83d5e701bc1e7ce31806 /config/util.go | |
parent | 480eb327831d56a795ea28d2c7293ab134d78a47 (diff) |
client: add golang client library
Signed-off-by: Wataru Ishida <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'config/util.go')
-rw-r--r-- | config/util.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/config/util.go b/config/util.go index d1696eb0..13f1fa77 100644 --- a/config/util.go +++ b/config/util.go @@ -17,6 +17,10 @@ package config import ( "fmt" + "net" + "regexp" + "strconv" + "github.com/osrg/gobgp/packet/bgp" ) @@ -82,3 +86,41 @@ func CheckAfiSafisChange(x, y []AfiSafi) bool { } return false } + +func ParseMaskLength(prefix, mask string) (int, int, error) { + _, ipNet, err := net.ParseCIDR(prefix) + if err != nil { + return 0, 0, fmt.Errorf("invalid prefix: %s", prefix) + } + if mask == "" { + l, _ := ipNet.Mask.Size() + return l, l, nil + } + exp := regexp.MustCompile("(\\d+)\\.\\.(\\d+)") + elems := exp.FindStringSubmatch(mask) + if len(elems) != 3 { + return 0, 0, fmt.Errorf("invalid mask length range: %s", mask) + } + // we've already checked the range is sane by regexp + min, _ := strconv.Atoi(elems[1]) + max, _ := strconv.Atoi(elems[2]) + if min > max { + return 0, 0, fmt.Errorf("invalid mask length range: %s", mask) + } + if ipv4 := ipNet.IP.To4(); ipv4 != nil { + f := func(i int) bool { + return i >= 0 && i <= 32 + } + if !f(min) || !f(max) { + return 0, 0, fmt.Errorf("ipv4 mask length range outside scope :%s", mask) + } + } else { + f := func(i int) bool { + return i >= 0 && i <= 128 + } + if !f(min) || !f(max) { + return 0, 0, fmt.Errorf("ipv6 mask length range outside scope :%s", mask) + } + } + return min, max, nil +} |