From b2ca825f6d55fb316a01909aedab44e865d6f53d Mon Sep 17 00:00:00 2001 From: Satoshi Fujimoto Date: Mon, 11 Dec 2017 16:19:50 +0900 Subject: table/policy: Support prefix representation in NeighborSet Currently, "neighbor-set" supports only IP address representation and IP prefix representation(such as "192.168.0.0/24") is not supported. This commit enables to accept the prefix representation for "neighbor-set" to allow neighbors to be specified as range. Signed-off-by: Satoshi Fujimoto --- table/policy.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'table/policy.go') diff --git a/table/policy.go b/table/policy.go index fe153cf4..e1b7d690 100644 --- a/table/policy.go +++ b/table/policy.go @@ -536,7 +536,7 @@ func NewPrefixSet(c config.PrefixSet) (*PrefixSet, error) { type NeighborSet struct { name string - list []net.IP + list []net.IPNet } func (s *NeighborSet) Name() string { @@ -561,11 +561,11 @@ func (lhs *NeighborSet) Remove(arg DefinedSet) error { if !ok { return fmt.Errorf("type cast failed") } - ps := make([]net.IP, 0, len(lhs.list)) + ps := make([]net.IPNet, 0, len(lhs.list)) for _, x := range lhs.list { found := false for _, y := range rhs.list { - if x.Equal(y) { + if x.String() == y.String() { found = true break } @@ -610,7 +610,7 @@ func (s *NeighborSet) MarshalJSON() ([]byte, error) { return json.Marshal(s.ToConfig()) } -func NewNeighborSetFromApiStruct(name string, list []net.IP) (*NeighborSet, error) { +func NewNeighborSetFromApiStruct(name string, list []net.IPNet) (*NeighborSet, error) { return &NeighborSet{ name: name, list: list, @@ -625,13 +625,24 @@ func NewNeighborSet(c config.NeighborSet) (*NeighborSet, error) { } return nil, fmt.Errorf("empty neighbor set name") } - list := make([]net.IP, 0, len(c.NeighborInfoList)) + list := make([]net.IPNet, 0, len(c.NeighborInfoList)) for _, x := range c.NeighborInfoList { - addr := net.ParseIP(x) - if addr == nil { - return nil, fmt.Errorf("invalid address: %s", x) + _, cidr, err := net.ParseCIDR(x) + if err != nil { + addr := net.ParseIP(x) + if addr == nil { + return nil, fmt.Errorf("invalid address or prefix: %s", x) + } + mask := net.CIDRMask(32, 32) + if addr.To4() == nil { + mask = net.CIDRMask(128, 128) + } + cidr = &net.IPNet{ + IP: addr, + Mask: mask, + } } - list = append(list, addr) + list = append(list, *cidr) } return &NeighborSet{ name: name, @@ -1330,7 +1341,6 @@ func (c *NeighborCondition) Option() MatchOption { // and, subsequent comparisons are skipped if that matches the conditions. // If NeighborList's length is zero, return true. func (c *NeighborCondition) Evaluate(path *Path, options *PolicyOptions) bool { - if len(c.set.list) == 0 { log.WithFields(log.Fields{ "Topic": "Policy", @@ -1348,7 +1358,7 @@ func (c *NeighborCondition) Evaluate(path *Path, options *PolicyOptions) bool { } result := false for _, n := range c.set.list { - if neighbor.Equal(n) { + if n.Contains(neighbor) { result = true break } -- cgit v1.2.3