mirror of
https://github.com/danderson/netboot.git
synced 2025-10-17 02:21:20 +02:00
Moved address pool-related code into dhcp6/pool package.
This commit is contained in:
parent
a0fc6b6e06
commit
b36c94fc0a
@ -1,4 +1,4 @@
|
|||||||
package dhcp6
|
package pool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
@ -8,11 +8,12 @@ import (
|
|||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"sync"
|
"sync"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go.universe.tf/netboot/dhcp6"
|
||||||
)
|
)
|
||||||
|
|
||||||
type associationExpiration struct {
|
type associationExpiration struct {
|
||||||
expiresAt time.Time
|
expiresAt time.Time
|
||||||
ia *IdentityAssociation
|
ia *dhcp6.IdentityAssociation
|
||||||
}
|
}
|
||||||
|
|
||||||
type fifo struct {q []interface{}}
|
type fifo struct {q []interface{}}
|
||||||
@ -46,7 +47,7 @@ func (f *fifo) Peek() interface{} {
|
|||||||
type RandomAddressPool struct {
|
type RandomAddressPool struct {
|
||||||
poolStartAddress *big.Int
|
poolStartAddress *big.Int
|
||||||
poolSize uint64
|
poolSize uint64
|
||||||
identityAssociations map[uint64]*IdentityAssociation
|
identityAssociations map[uint64]*dhcp6.IdentityAssociation
|
||||||
usedIps map[uint64]struct{}
|
usedIps map[uint64]struct{}
|
||||||
identityAssociationExpirations fifo
|
identityAssociationExpirations fifo
|
||||||
validLifetime uint32 // in seconds
|
validLifetime uint32 // in seconds
|
||||||
@ -62,7 +63,7 @@ func NewRandomAddressPool(poolStartAddress net.IP, poolSize uint64, validLifetim
|
|||||||
ret.poolStartAddress = big.NewInt(0)
|
ret.poolStartAddress = big.NewInt(0)
|
||||||
ret.poolStartAddress.SetBytes(poolStartAddress)
|
ret.poolStartAddress.SetBytes(poolStartAddress)
|
||||||
ret.poolSize = poolSize
|
ret.poolSize = poolSize
|
||||||
ret.identityAssociations = make(map[uint64]*IdentityAssociation)
|
ret.identityAssociations = make(map[uint64]*dhcp6.IdentityAssociation)
|
||||||
ret.usedIps = make(map[uint64]struct{})
|
ret.usedIps = make(map[uint64]struct{})
|
||||||
ret.identityAssociationExpirations = newFifo()
|
ret.identityAssociationExpirations = newFifo()
|
||||||
ret.timeNow = func() time.Time { return time.Now() }
|
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.
|
// 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()
|
p.lock.Lock()
|
||||||
defer p.lock.Unlock()
|
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()))
|
rng := rand.New(rand.NewSource(p.timeNow().UnixNano()))
|
||||||
|
|
||||||
for _, interfaceID := range (interfaceIDs) {
|
for _, interfaceID := range (interfaceIDs) {
|
||||||
@ -105,7 +106,7 @@ func (p *RandomAddressPool) ReserveAddresses(clientID []byte, interfaceIDs [][]b
|
|||||||
_, exists := p.usedIps[newIP.Uint64()];
|
_, exists := p.usedIps[newIP.Uint64()];
|
||||||
if !exists {
|
if !exists {
|
||||||
timeNow := p.timeNow()
|
timeNow := p.timeNow()
|
||||||
association := &IdentityAssociation{ClientID: clientID,
|
association := &dhcp6.IdentityAssociation{ClientID: clientID,
|
||||||
InterfaceID: interfaceID,
|
InterfaceID: interfaceID,
|
||||||
IPAddress: newIP.Bytes(),
|
IPAddress: newIP.Bytes(),
|
||||||
CreatedAt: timeNow}
|
CreatedAt: timeNow}
|
@ -1,4 +1,4 @@
|
|||||||
package dhcp6
|
package pool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go.universe.tf/netboot/pixiecorev6"
|
"go.universe.tf/netboot/pixiecorev6"
|
||||||
"go.universe.tf/netboot/dhcp6"
|
"go.universe.tf/netboot/dhcp6"
|
||||||
|
"go.universe.tf/netboot/dhcp6/pool"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -77,7 +78,7 @@ var bootIPv6Cmd = &cobra.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fatalf("Error reading flag: %s", err)
|
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)
|
s.PacketBuilder = dhcp6.MakePacketBuilder(addressPoolValidLifetime - addressPoolValidLifetime*3/100, addressPoolValidLifetime)
|
||||||
|
|
||||||
fmt.Println(s.Serve())
|
fmt.Println(s.Serve())
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go.universe.tf/netboot/pixiecorev6"
|
"go.universe.tf/netboot/pixiecorev6"
|
||||||
"go.universe.tf/netboot/dhcp6"
|
"go.universe.tf/netboot/dhcp6"
|
||||||
|
"go.universe.tf/netboot/dhcp6/pool"
|
||||||
"time"
|
"time"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
@ -74,7 +75,7 @@ var ipv6ApiCmd = &cobra.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fatalf("Error reading flag: %s", err)
|
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)
|
s.PacketBuilder = dhcp6.MakePacketBuilder(addressPoolValidLifetime - addressPoolValidLifetime*3/100, addressPoolValidLifetime)
|
||||||
|
|
||||||
fmt.Println(s.Serve())
|
fmt.Println(s.Serve())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user