From b36c94fc0a5dd5ab218fce9954cbb290e96f0989 Mon Sep 17 00:00:00 2001 From: Dmitri Dolguikh Date: Thu, 4 Jan 2018 12:50:47 -0800 Subject: [PATCH] Moved address pool-related code into dhcp6/pool package. --- dhcp6/{ => pool}/random_address_pool.go | 15 ++++++++------- dhcp6/{ => pool}/random_address_pool_test.go | 2 +- pixiecore/cli/bootipv6cmd.go | 3 ++- pixiecore/cli/ipv6apicmd.go | 3 ++- 4 files changed, 13 insertions(+), 10 deletions(-) rename dhcp6/{ => pool}/random_address_pool.go (92%) rename dhcp6/{ => pool}/random_address_pool_test.go (99%) diff --git a/dhcp6/random_address_pool.go b/dhcp6/pool/random_address_pool.go similarity index 92% rename from dhcp6/random_address_pool.go rename to dhcp6/pool/random_address_pool.go index 8505feb..79d8904 100644 --- a/dhcp6/random_address_pool.go +++ b/dhcp6/pool/random_address_pool.go @@ -1,4 +1,4 @@ -package dhcp6 +package pool import ( "net" @@ -8,11 +8,12 @@ import ( "hash/fnv" "sync" "fmt" + "go.universe.tf/netboot/dhcp6" ) type associationExpiration struct { expiresAt time.Time - ia *IdentityAssociation + ia *dhcp6.IdentityAssociation } type fifo struct {q []interface{}} @@ -46,7 +47,7 @@ func (f *fifo) Peek() interface{} { type RandomAddressPool struct { poolStartAddress *big.Int poolSize uint64 - identityAssociations map[uint64]*IdentityAssociation + identityAssociations map[uint64]*dhcp6.IdentityAssociation usedIps map[uint64]struct{} identityAssociationExpirations fifo validLifetime uint32 // in seconds @@ -62,7 +63,7 @@ func NewRandomAddressPool(poolStartAddress net.IP, poolSize uint64, validLifetim ret.poolStartAddress = big.NewInt(0) ret.poolStartAddress.SetBytes(poolStartAddress) ret.poolSize = poolSize - ret.identityAssociations = make(map[uint64]*IdentityAssociation) + ret.identityAssociations = make(map[uint64]*dhcp6.IdentityAssociation) ret.usedIps = make(map[uint64]struct{}) ret.identityAssociationExpirations = newFifo() ret.timeNow = func() time.Time { return time.Now() } @@ -79,11 +80,11 @@ func NewRandomAddressPool(poolStartAddress net.IP, poolSize uint64, validLifetim } // ReserveAddresses creates new or retrieves active associations for interfaces in interfaceIDs list. -func (p *RandomAddressPool) ReserveAddresses(clientID []byte, interfaceIDs [][]byte) ([]*IdentityAssociation, error) { +func (p *RandomAddressPool) ReserveAddresses(clientID []byte, interfaceIDs [][]byte) ([]*dhcp6.IdentityAssociation, error) { p.lock.Lock() defer p.lock.Unlock() - ret := make([]*IdentityAssociation, 0, len(interfaceIDs)) + ret := make([]*dhcp6.IdentityAssociation, 0, len(interfaceIDs)) rng := rand.New(rand.NewSource(p.timeNow().UnixNano())) for _, interfaceID := range (interfaceIDs) { @@ -105,7 +106,7 @@ func (p *RandomAddressPool) ReserveAddresses(clientID []byte, interfaceIDs [][]b _, exists := p.usedIps[newIP.Uint64()]; if !exists { timeNow := p.timeNow() - association := &IdentityAssociation{ClientID: clientID, + association := &dhcp6.IdentityAssociation{ClientID: clientID, InterfaceID: interfaceID, IPAddress: newIP.Bytes(), CreatedAt: timeNow} diff --git a/dhcp6/random_address_pool_test.go b/dhcp6/pool/random_address_pool_test.go similarity index 99% rename from dhcp6/random_address_pool_test.go rename to dhcp6/pool/random_address_pool_test.go index 4b6035a..192af33 100644 --- a/dhcp6/random_address_pool_test.go +++ b/dhcp6/pool/random_address_pool_test.go @@ -1,4 +1,4 @@ -package dhcp6 +package pool import ( "testing" diff --git a/pixiecore/cli/bootipv6cmd.go b/pixiecore/cli/bootipv6cmd.go index 234638f..b34420b 100644 --- a/pixiecore/cli/bootipv6cmd.go +++ b/pixiecore/cli/bootipv6cmd.go @@ -5,6 +5,7 @@ import ( "fmt" "go.universe.tf/netboot/pixiecorev6" "go.universe.tf/netboot/dhcp6" + "go.universe.tf/netboot/dhcp6/pool" "net" "strings" ) @@ -77,7 +78,7 @@ var bootIPv6Cmd = &cobra.Command{ if err != nil { fatalf("Error reading flag: %s", err) } - s.AddressPool = dhcp6.NewRandomAddressPool(net.ParseIP(addressPoolStart), addressPoolSize, addressPoolValidLifetime) + s.AddressPool = pool.NewRandomAddressPool(net.ParseIP(addressPoolStart), addressPoolSize, addressPoolValidLifetime) s.PacketBuilder = dhcp6.MakePacketBuilder(addressPoolValidLifetime - addressPoolValidLifetime*3/100, addressPoolValidLifetime) fmt.Println(s.Serve()) diff --git a/pixiecore/cli/ipv6apicmd.go b/pixiecore/cli/ipv6apicmd.go index c059605..39f445b 100644 --- a/pixiecore/cli/ipv6apicmd.go +++ b/pixiecore/cli/ipv6apicmd.go @@ -5,6 +5,7 @@ import ( "fmt" "go.universe.tf/netboot/pixiecorev6" "go.universe.tf/netboot/dhcp6" + "go.universe.tf/netboot/dhcp6/pool" "time" "net" "strings" @@ -74,7 +75,7 @@ var ipv6ApiCmd = &cobra.Command{ if err != nil { fatalf("Error reading flag: %s", err) } - s.AddressPool = dhcp6.NewRandomAddressPool(net.ParseIP(addressPoolStart), addressPoolSize, addressPoolValidLifetime) + s.AddressPool = pool.NewRandomAddressPool(net.ParseIP(addressPoolStart), addressPoolSize, addressPoolValidLifetime) s.PacketBuilder = dhcp6.MakePacketBuilder(addressPoolValidLifetime - addressPoolValidLifetime*3/100, addressPoolValidLifetime) fmt.Println(s.Serve())