vendor: add d2g/dhcp4server

This commit is contained in:
Dan Williams
2017-05-26 23:32:35 -05:00
parent 461d433911
commit 26ef6e312d
115 changed files with 13027 additions and 0 deletions

95
vendor/github.com/d2g/dhcp4server/leasepool/lease.go generated vendored Normal file
View File

@ -0,0 +1,95 @@
package leasepool
import (
"encoding/json"
"net"
"time"
)
type LeaseStatus int
const (
Free LeaseStatus = 0
Reserved LeaseStatus = 1
Active LeaseStatus = 2
)
type Lease struct {
IP net.IP //The IP of the Lease
Status LeaseStatus //Are Reserved, Active or Free
MACAddress net.HardwareAddr //Mac Address of the Device
Hostname string //Hostname From option 12
Expiry time.Time //Expiry Time
}
func (this Lease) MarshalJSON() ([]byte, error) {
stringMarshal := struct {
IP string
Status int
MACAddress string
Hostname string
Expiry time.Time
}{
(this.IP.String()),
int(this.Status),
(this.MACAddress.String()),
this.Hostname,
this.Expiry,
}
return json.Marshal(stringMarshal)
}
func (this *Lease) UnmarshalJSON(data []byte) error {
stringUnMarshal := struct {
IP string
Status int
MACAddress string
Hostname string
Expiry time.Time
}{}
err := json.Unmarshal(data, &stringUnMarshal)
if err != nil {
return err
}
this.IP = net.ParseIP(stringUnMarshal.IP)
this.Status = LeaseStatus(stringUnMarshal.Status)
if stringUnMarshal.MACAddress != "" {
this.MACAddress, err = net.ParseMAC(stringUnMarshal.MACAddress)
}
if err != nil {
return err
}
this.Hostname = stringUnMarshal.Hostname
this.Expiry = stringUnMarshal.Expiry
return nil
}
func (this Lease) Equal(other Lease) bool {
if !this.IP.Equal(other.IP) {
return false
}
if int(this.Status) != int(other.Status) {
return false
}
if this.MACAddress.String() != other.MACAddress.String() {
return false
}
if this.Hostname != other.Hostname {
return false
}
if !this.Expiry.Equal(other.Expiry) {
return false
}
return true
}

View File

@ -0,0 +1,51 @@
package leasepool
import (
"encoding/json"
"net"
"testing"
"time"
)
/*
* The Leases are Marshalled and Unmarshalled for storage.
* I JSON Marshal these for gvklite
*/
func TestMarshaling(test *testing.T) {
var err error
startLease := Lease{}
startLease.IP = net.IPv4(192, 168, 0, 1)
startLease.Hostname = "ExampleHostname"
startLease.Status = Active
startLease.Expiry = time.Now()
startLease.MACAddress, err = net.ParseMAC("01:23:45:67:89:ab")
if err != nil {
test.Error("Error Parsing Mac Address:" + err.Error())
}
byteStartLease, err := json.Marshal(startLease)
if err != nil {
test.Error("Error Marshaling to JSON:" + err.Error())
}
test.Log("StartLease As JSON:" + string(byteStartLease))
endLease := Lease{}
err = json.Unmarshal(byteStartLease, &endLease)
if err != nil {
test.Error("Error Unmarshaling to JSON:" + err.Error())
}
test.Logf("End Lease Object:%v\n", endLease)
if !startLease.Equal(endLease) {
byteEndLease, err := json.Marshal(endLease)
if err != nil {
test.Error("Can't Marshal End Lease For Debuging:" + err.Error())
}
test.Log("End Lease as JSON:" + string(byteEndLease))
test.Error("Starting Lease Doesn't Match End Lease")
}
}

View File

@ -0,0 +1,49 @@
package leasepool
import (
"net"
)
/*
* Lease.IP is the Key.
*/
type LeasePool interface {
//Add A Lease To The Pool
AddLease(Lease) error
//Remove
RemoveLease(net.IP) error
//Remove All Leases from the Pool (Required for Persistant LeaseManagers)
PurgeLeases() error
/*
* Get the Lease
* -Found
* -Copy Of the Lease
* -Any Error
*/
GetLease(net.IP) (bool, Lease, error)
//Get the lease already in use by that hardware address.
GetLeaseForHardwareAddress(net.HardwareAddr) (bool, Lease, error)
/*
* -Lease Available
* -Lease
* -Error
*/
GetNextFreeLease() (bool, Lease, error)
/*
* Return All Leases
*/
GetLeases() ([]Lease, error)
/*
* Update Lease
* - Has Updated
* - Error
*/
UpdateLease(Lease) (bool, error)
}

View 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
}

View 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())
}
}