mirror of
https://github.com/rancher/plugins.git
synced 2025-09-01 04:37:58 +00:00
vendor: add d2g/dhcp4server
This commit is contained in:
150
vendor/github.com/d2g/dhcp4server/leasepool/memorypool/memorypool.go
generated
vendored
Normal file
150
vendor/github.com/d2g/dhcp4server/leasepool/memorypool/memorypool.go
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
package memorypool
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/d2g/dhcp4server/leasepool"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type MemoryPool struct {
|
||||
pool []leasepool.Lease
|
||||
poolLock sync.Mutex
|
||||
}
|
||||
|
||||
//Add A Lease To The Pool
|
||||
func (t *MemoryPool) AddLease(newLease leasepool.Lease) error {
|
||||
t.poolLock.Lock()
|
||||
defer t.poolLock.Unlock()
|
||||
|
||||
if t.pool == nil {
|
||||
t.pool = make([]leasepool.Lease, 0)
|
||||
}
|
||||
|
||||
for i := range t.pool {
|
||||
if t.pool[i].IP.Equal(newLease.IP) {
|
||||
//Lease Already Exists In Pool
|
||||
return errors.New("Error: Lease IP \"" + newLease.IP.String() + "\" alreay exists in Pool")
|
||||
}
|
||||
}
|
||||
|
||||
t.pool = append([]leasepool.Lease{newLease}, t.pool...)
|
||||
return nil
|
||||
}
|
||||
|
||||
//Remove a Lease From The Pool
|
||||
func (t *MemoryPool) RemoveLease(leaseIP net.IP) error {
|
||||
t.poolLock.Lock()
|
||||
defer t.poolLock.Unlock()
|
||||
|
||||
for i := range t.pool {
|
||||
if t.pool[i].IP.Equal(leaseIP) {
|
||||
|
||||
//Move the Last Element to This Position.
|
||||
t.pool[i] = t.pool[len(t.pool)-1]
|
||||
|
||||
//Shortern the Pool By One.
|
||||
t.pool = t.pool[0:(len(t.pool) - 1)]
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return errors.New("Error: Lease IP \"" + leaseIP.String() + "\" Is Not In The Pool")
|
||||
}
|
||||
|
||||
//Remove All Leases from the Pool (Required for Persistant LeaseManagers)
|
||||
func (t *MemoryPool) PurgeLeases() error {
|
||||
t.poolLock.Lock()
|
||||
defer t.poolLock.Unlock()
|
||||
|
||||
t.pool = nil
|
||||
t.pool = make([]leasepool.Lease, 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the Lease
|
||||
* -Found
|
||||
* -Copy Of the Lease
|
||||
* -Any Error
|
||||
*/
|
||||
func (t *MemoryPool) GetLease(leaseIP net.IP) (bool, leasepool.Lease, error) {
|
||||
t.poolLock.Lock()
|
||||
defer t.poolLock.Unlock()
|
||||
|
||||
for i := range t.pool {
|
||||
if t.pool[i].IP.Equal(leaseIP) {
|
||||
return true, t.pool[i], nil
|
||||
}
|
||||
}
|
||||
return false, leasepool.Lease{}, nil
|
||||
}
|
||||
|
||||
//Get the lease already in use by that hardware address.
|
||||
func (t *MemoryPool) GetLeaseForHardwareAddress(macAddress net.HardwareAddr) (bool, leasepool.Lease, error) {
|
||||
t.poolLock.Lock()
|
||||
defer t.poolLock.Unlock()
|
||||
|
||||
for i := range t.pool {
|
||||
if bytes.Equal(t.pool[i].MACAddress, macAddress) {
|
||||
return true, t.pool[i], nil
|
||||
}
|
||||
}
|
||||
return false, leasepool.Lease{}, nil
|
||||
}
|
||||
|
||||
/*
|
||||
* -Lease Available
|
||||
* -Lease
|
||||
* -Error
|
||||
*/
|
||||
func (t *MemoryPool) GetNextFreeLease() (bool, leasepool.Lease, error) {
|
||||
t.poolLock.Lock()
|
||||
defer t.poolLock.Unlock()
|
||||
|
||||
//Loop Through the elements backwards.
|
||||
for i := (len(t.pool) - 1); i >= 0; i-- {
|
||||
//If the Lease Is Free
|
||||
if t.pool[i].Status == leasepool.Free {
|
||||
//Take the Element
|
||||
iLease := t.pool[i]
|
||||
//Shrink the Pool By 1
|
||||
t.pool = t.pool[:(len(t.pool) - 1)]
|
||||
//Place the Lease At the Begining (This saves us having some sort of counter...)
|
||||
t.pool = append([]leasepool.Lease{iLease}, t.pool...)
|
||||
return true, iLease, nil
|
||||
}
|
||||
}
|
||||
return false, leasepool.Lease{}, nil
|
||||
}
|
||||
|
||||
/*
|
||||
* Return All Leases
|
||||
*/
|
||||
func (t *MemoryPool) GetLeases() ([]leasepool.Lease, error) {
|
||||
return t.pool, nil
|
||||
}
|
||||
|
||||
/*
|
||||
* Update Lease
|
||||
* - Has Updated
|
||||
* - Error
|
||||
*/
|
||||
func (t *MemoryPool) UpdateLease(lease leasepool.Lease) (bool, error) {
|
||||
t.poolLock.Lock()
|
||||
defer t.poolLock.Unlock()
|
||||
|
||||
for i := range t.pool {
|
||||
if t.pool[i].IP.Equal(lease.IP) {
|
||||
|
||||
t.pool[i].MACAddress = lease.MACAddress
|
||||
t.pool[i].Hostname = lease.Hostname
|
||||
t.pool[i].Expiry = lease.Expiry
|
||||
t.pool[i].Status = lease.Status
|
||||
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
51
vendor/github.com/d2g/dhcp4server/leasepool/memorypool/memorypool_test.go
generated
vendored
Normal file
51
vendor/github.com/d2g/dhcp4server/leasepool/memorypool/memorypool_test.go
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
package memorypool
|
||||
|
||||
import (
|
||||
"github.com/d2g/dhcp4"
|
||||
"github.com/d2g/dhcp4server/leasepool"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLeaseCycle(test *testing.T) {
|
||||
myMemoryLeasePool := MemoryPool{}
|
||||
|
||||
//Lets add a list of IPs to the pool these will be served to the clients so make sure they work for you.
|
||||
// So Create Array of IPs 192.168.1.1 to 192.168.1.30
|
||||
for i := 0; i < 30; i++ {
|
||||
err := myMemoryLeasePool.AddLease(leasepool.Lease{IP: dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i)})
|
||||
if err != nil {
|
||||
test.Error("Error Creating Lease:" + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 30; i++ {
|
||||
hasLease, iLease, err := myMemoryLeasePool.GetNextFreeLease()
|
||||
if err != nil || !hasLease {
|
||||
test.Error("Error Getting Lease:" + err.Error())
|
||||
}
|
||||
|
||||
if !dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i).Equal(iLease.IP) {
|
||||
test.Error("Expected Lease:" + dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i).String() + " Received:" + iLease.IP.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingleLease(test *testing.T) {
|
||||
myMemoryLeasePool := MemoryPool{}
|
||||
|
||||
err := myMemoryLeasePool.AddLease(leasepool.Lease{IP: dhcp4.IPAdd(net.IPv4(192, 168, 1, 5), 0)})
|
||||
if err != nil {
|
||||
test.Error("Error Creating Lease:" + err.Error())
|
||||
}
|
||||
|
||||
hasLease, iLease, err := myMemoryLeasePool.GetNextFreeLease()
|
||||
if err != nil || !hasLease {
|
||||
test.Error("Error Getting Lease:" + err.Error())
|
||||
}
|
||||
|
||||
if !dhcp4.IPAdd(net.IPv4(192, 168, 1, 5), 0).Equal(iLease.IP) {
|
||||
test.Error("Expected Lease:" + dhcp4.IPAdd(net.IPv4(192, 168, 1, 5), 0).String() + " Received:" + iLease.IP.String())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user