From 2908db94861ad3faa69d95c30f4c6577389c6c6d Mon Sep 17 00:00:00 2001 From: Daniel Czerwonk <daniel@dan-nrw.de> Date: Mon, 14 May 2018 10:44:28 +0200 Subject: [PATCH] support for multiple prefix lists --- routingtable/filter/from.go | 14 +++++++++-- routingtable/filter/prefix_list.go | 11 +++++++-- routingtable/filter/term_test.go | 38 +++++++++--------------------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/routingtable/filter/from.go b/routingtable/filter/from.go index 2237fe4e..10825397 100644 --- a/routingtable/filter/from.go +++ b/routingtable/filter/from.go @@ -6,9 +6,19 @@ import ( ) type From struct { - prefixList *PrefixList + prefixLists []*PrefixList } func (f *From) Matches(p net.Prefix, pa *route.Path) bool { - return f.prefixList.Matches(p) + return f.matchesAnyPrefixList(p) +} + +func (t *From) matchesAnyPrefixList(p net.Prefix) bool { + for _, l := range t.prefixLists { + if l.Matches(p) { + return true + } + } + + return false } diff --git a/routingtable/filter/prefix_list.go b/routingtable/filter/prefix_list.go index b783a07a..8ccf36e8 100644 --- a/routingtable/filter/prefix_list.go +++ b/routingtable/filter/prefix_list.go @@ -6,8 +6,15 @@ type PrefixList struct { allowed []net.Prefix } -func (f *PrefixList) Matches(p net.Prefix) bool { - for _, a := range f.allowed { +func NewPrefixList(pfxs ...net.Prefix) *PrefixList { + l := &PrefixList{ + allowed: pfxs, + } + return l +} + +func (l *PrefixList) Matches(p net.Prefix) bool { + for _, a := range l.allowed { if !a.Contains(p) { return false } diff --git a/routingtable/filter/term_test.go b/routingtable/filter/term_test.go index bd0a00be..bf045323 100644 --- a/routingtable/filter/term_test.go +++ b/routingtable/filter/term_test.go @@ -46,10 +46,8 @@ func TestProcess(t *testing.T) { path: &route.Path{}, from: []*From{ { - &PrefixList{ - allowed: []net.Prefix{ - net.NewPfx(0, 0), - }, + []*PrefixList{ + NewPrefixList(net.NewPfx(0, 0)), }, }, }, @@ -65,10 +63,8 @@ func TestProcess(t *testing.T) { path: &route.Path{}, from: []*From{ { - &PrefixList{ - allowed: []net.Prefix{ - net.NewPfx(0, 32), - }, + []*PrefixList{ + NewPrefixList(net.NewPfx(0, 32)), }, }, }, @@ -84,10 +80,8 @@ func TestProcess(t *testing.T) { path: &route.Path{}, from: []*From{ { - &PrefixList{ - allowed: []net.Prefix{ - net.NewPfx(0, 0), - }, + []*PrefixList{ + NewPrefixList(net.NewPfx(0, 0)), }, }, }, @@ -103,10 +97,8 @@ func TestProcess(t *testing.T) { path: &route.Path{}, from: []*From{ { - &PrefixList{ - allowed: []net.Prefix{ - net.NewPfx(0, 0), - }, + []*PrefixList{ + NewPrefixList(net.NewPfx(0, 0)), }, }, }, @@ -123,17 +115,9 @@ func TestProcess(t *testing.T) { path: &route.Path{}, from: []*From{ { - &PrefixList{ - allowed: []net.Prefix{ - net.NewPfx(0, 32), - }, - }, - }, - { - &PrefixList{ - allowed: []net.Prefix{ - net.NewPfx(0, 0), - }, + []*PrefixList{ + NewPrefixList(net.NewPfx(0, 32)), + NewPrefixList(net.NewPfx(0, 0)), }, }, }, -- GitLab