mirror of
https://github.com/danderson/netboot.git
synced 2025-10-16 10:01: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 (
|
||||
"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}
|
@ -1,4 +1,4 @@
|
||||
package dhcp6
|
||||
package pool
|
||||
|
||||
import (
|
||||
"testing"
|
@ -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())
|
||||
|
@ -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())
|
||||
|
Loading…
x
Reference in New Issue
Block a user