diff options
author | Wataru Ishida <ishida.wataru@lab.ntt.co.jp> | 2016-11-15 07:02:49 +0000 |
---|---|---|
committer | Wataru Ishida <ishida.wataru@lab.ntt.co.jp> | 2016-11-15 07:03:53 +0000 |
commit | 25fe17c8176d5da2c0ffb7bca7dfd1780a34504c (patch) | |
tree | 486bd16e5bbf0c5b8e658aa642f36fde4bbc3edd /server | |
parent | ee8ce99e2df1f732ef515e20fb3054c0cff0249b (diff) |
client: remove server package dependency
move ROA under table package
Signed-off-by: Wataru Ishida <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'server')
-rw-r--r-- | server/rpki.go | 75 | ||||
-rw-r--r-- | server/rpki_test.go | 27 | ||||
-rw-r--r-- | server/server.go | 2 |
3 files changed, 33 insertions, 71 deletions
diff --git a/server/rpki.go b/server/rpki.go index cbef5dee..7f771f27 100644 --- a/server/rpki.go +++ b/server/rpki.go @@ -37,51 +37,12 @@ func before(a, b uint32) bool { return int32(a-b) < 0 } -type ipPrefix struct { - Prefix net.IP - Length uint8 -} - -func (p *ipPrefix) String() string { - return fmt.Sprintf("%s/%d", p.Prefix, p.Length) -} - type roaBucket struct { - Prefix *ipPrefix - entries []*ROA -} - -type ROA struct { - Family int - Prefix *ipPrefix - MaxLen uint8 - AS uint32 - Src string -} - -func NewROA(family int, prefixByte []byte, prefixLen uint8, maxLen uint8, as uint32, src string) *ROA { - p := make([]byte, len(prefixByte)) - copy(p, prefixByte) - return &ROA{ - Family: family, - Prefix: &ipPrefix{ - Prefix: p, - Length: prefixLen, - }, - MaxLen: maxLen, - AS: as, - Src: src, - } -} - -func (r *ROA) Equal(roa *ROA) bool { - if r.MaxLen == roa.MaxLen && r.Src == roa.Src && r.AS == roa.AS { - return true - } - return false + Prefix *table.IPPrefix + entries []*table.ROA } -type roas []*ROA +type roas []*table.ROA func (r roas) Len() int { return len(r) @@ -185,7 +146,7 @@ func (m *roaManager) deleteAllROA(network string) { deleteKeys := make([]string, 0, tree.Len()) tree.Walk(func(s string, v interface{}) bool { b, _ := v.(*roaBucket) - newEntries := make([]*ROA, 0, len(b.entries)) + newEntries := make([]*table.ROA, 0, len(b.entries)) for _, r := range b.entries { if r.Src != network { newEntries = append(newEntries, r) @@ -275,7 +236,7 @@ func (m *roaManager) HandleROAEvent(ev *ROAEvent) { client.state.Downtime = time.Now().Unix() // clear state client.endOfData = false - client.pendingROAs = make([]*ROA, 0) + client.pendingROAs = make([]*table.ROA, 0) client.state.RpkiMessages = config.RpkiMessages{} client.conn = nil client.t = tomb.Tomb{} @@ -307,7 +268,7 @@ func (m *roaManager) HandleROAEvent(ev *ROAEvent) { } } -func (m *roaManager) roa2tree(roa *ROA) (*radix.Tree, string) { +func (m *roaManager) roa2tree(roa *table.ROA) (*radix.Tree, string) { tree := m.Roas[bgp.RF_IPv4_UC] if roa.Family == bgp.AFI_IP6 { tree = m.Roas[bgp.RF_IPv6_UC] @@ -315,12 +276,12 @@ func (m *roaManager) roa2tree(roa *ROA) (*radix.Tree, string) { return tree, table.IpToRadixkey(roa.Prefix.Prefix, roa.Prefix.Length) } -func (m *roaManager) deleteROA(roa *ROA) { +func (m *roaManager) deleteROA(roa *table.ROA) { tree, key := m.roa2tree(roa) b, _ := tree.Get(key) if b != nil { bucket := b.(*roaBucket) - newEntries := make([]*ROA, 0, len(bucket.entries)) + newEntries := make([]*table.ROA, 0, len(bucket.entries)) for _, r := range bucket.entries { if !r.Equal(roa) { newEntries = append(newEntries, r) @@ -343,14 +304,14 @@ func (m *roaManager) deleteROA(roa *ROA) { }).Info("Can't withdraw a ROA") } -func (m *roaManager) addROA(roa *ROA) { +func (m *roaManager) addROA(roa *table.ROA) { tree, key := m.roa2tree(roa) b, _ := tree.Get(key) var bucket *roaBucket if b == nil { bucket = &roaBucket{ Prefix: roa.Prefix, - entries: make([]*ROA, 0), + entries: make([]*table.ROA, 0), } tree.Insert(key, bucket) } else { @@ -394,7 +355,7 @@ func (c *roaManager) handleRTRMsg(client *roaClient, state *config.RpkiServerSta family = bgp.AFI_IP6 received.Ipv6Prefix++ } - roa := NewROA(family, msg.Prefix, msg.PrefixLen, msg.MaxLen, msg.AS, client.host) + roa := table.NewROA(family, msg.Prefix, msg.PrefixLen, msg.MaxLen, msg.AS, client.host) if (msg.Flags & 1) == 1 { if client.endOfData { c.addROA(roa) @@ -421,7 +382,7 @@ func (c *roaManager) handleRTRMsg(client *roaClient, state *config.RpkiServerSta for _, roa := range client.pendingROAs { c.addROA(roa) } - client.pendingROAs = make([]*ROA, 0) + client.pendingROAs = make([]*table.ROA, 0) case *rtr.RTRCacheReset: client.softReset() received.CacheReset++ @@ -496,9 +457,9 @@ func (c *roaManager) GetServers() []*config.RpkiServer { return l } -func (c *roaManager) GetRoa(family bgp.RouteFamily) ([]*ROA, error) { +func (c *roaManager) GetRoa(family bgp.RouteFamily) ([]*table.ROA, error) { if len(c.clientMap) == 0 { - return []*ROA{}, fmt.Errorf("RPKI server isn't configured.") + return []*table.ROA{}, fmt.Errorf("RPKI server isn't configured.") } var rfList []bgp.RouteFamily switch family { @@ -509,7 +470,7 @@ func (c *roaManager) GetRoa(family bgp.RouteFamily) ([]*ROA, error) { default: rfList = []bgp.RouteFamily{bgp.RF_IPv4_UC, bgp.RF_IPv6_UC} } - l := make([]*ROA, 0) + l := make([]*table.ROA, 0) for _, rf := range rfList { if tree, ok := c.Roas[rf]; ok { tree.Walk(func(s string, v interface{}) bool { @@ -598,7 +559,7 @@ type roaClient struct { timer *time.Timer lifetime int64 endOfData bool - pendingROAs []*ROA + pendingROAs []*table.ROA } func NewRoaClient(address, port string, ch chan *ROAEvent, lifetime int64) *roaClient { @@ -606,7 +567,7 @@ func NewRoaClient(address, port string, ch chan *ROAEvent, lifetime int64) *roaC host: net.JoinHostPort(address, port), eventCh: ch, lifetime: lifetime, - pendingROAs: make([]*ROA, 0), + pendingROAs: make([]*table.ROA, 0), } } @@ -633,7 +594,7 @@ func (c *roaClient) softReset() error { } c.state.RpkiMessages.RpkiSent.ResetQuery++ c.endOfData = false - c.pendingROAs = make([]*ROA, 0) + c.pendingROAs = make([]*table.ROA, 0) } return nil } diff --git a/server/rpki_test.go b/server/rpki_test.go index ee4689af..8405738b 100644 --- a/server/rpki_test.go +++ b/server/rpki_test.go @@ -24,6 +24,7 @@ import ( "github.com/armon/go-radix" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" + "github.com/osrg/gobgp/table" "github.com/stretchr/testify/assert" ) @@ -61,8 +62,8 @@ func TestValidate0(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 32, 100, "")) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 24, 200, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 32, 100, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 24, 200, "")) var r config.RpkiValidationResultType @@ -90,7 +91,7 @@ func TestValidate1(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) var r config.RpkiValidationResultType @@ -121,7 +122,7 @@ func TestValidate3(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) var r config.RpkiValidationResultType @@ -133,7 +134,7 @@ func TestValidate3(t *testing.T) { assert.Equal(r, config.RPKI_VALIDATION_RESULT_TYPE_INVALID) manager, _ = NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) tree = manager.Roas[bgp.RF_IPv4_UC] r = validateOne(tree, "10.0.0.0/17", "65000") @@ -144,8 +145,8 @@ func TestValidate4(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65001, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65001, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] @@ -160,8 +161,8 @@ func TestValidate5(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 17, 17, 65000, "")) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.128.0").To4(), 17, 17, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 17, 17, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.128.0").To4(), 17, 17, 65000, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] @@ -173,7 +174,7 @@ func TestValidate6(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 8, 32, 0, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 8, 32, 0, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] @@ -191,7 +192,7 @@ func TestValidate7(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] @@ -209,8 +210,8 @@ func TestValidate8(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 0, "")) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 0, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] diff --git a/server/server.go b/server/server.go index 6b336849..d71e45b2 100644 --- a/server/server.go +++ b/server/server.go @@ -2265,7 +2265,7 @@ func (s *BgpServer) GetRpki() (l []*config.RpkiServer, err error) { return l, err } -func (s *BgpServer) GetRoa(family bgp.RouteFamily) (l []*ROA, err error) { +func (s *BgpServer) GetRoa(family bgp.RouteFamily) (l []*table.ROA, err error) { ch := make(chan struct{}) defer func() { <-ch }() |