vendor update

This commit is contained in:
Nathan Gieseker
2019-06-17 21:33:55 -07:00
parent 13fbc4afdf
commit e8c953999e
900 changed files with 36135 additions and 265442 deletions

View File

@ -1,10 +1,7 @@
package leasepool
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"time"
)
@ -21,35 +18,37 @@ 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
ClientID []byte //ClientID of the request
Hostname string //Hostname From option 12
Expiry time.Time //Expiry Time
}
//leaseMarshal is a mirror of Lease used for marshalling, since
//net.HardwareAddr has no native marshalling capability.
type leaseMarshal struct {
IP string
Status int
MACAddress string
ClientID string
Hostname string
Expiry time.Time
}
func (this Lease) MarshalJSON() ([]byte, error) {
return json.Marshal(leaseMarshal{
IP: this.IP.String(),
Status: int(this.Status),
MACAddress: this.MACAddress.String(),
ClientID: hex.EncodeToString(this.ClientID),
Hostname: this.Hostname,
Expiry: this.Expiry,
})
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 := leaseMarshal{}
stringUnMarshal := struct {
IP string
Status int
MACAddress string
Hostname string
Expiry time.Time
}{}
err := json.Unmarshal(data, &stringUnMarshal)
if err != nil {
return err
@ -59,14 +58,12 @@ func (this *Lease) UnmarshalJSON(data []byte) error {
this.Status = LeaseStatus(stringUnMarshal.Status)
if stringUnMarshal.MACAddress != "" {
this.MACAddress, err = net.ParseMAC(stringUnMarshal.MACAddress)
if err != nil {
return fmt.Errorf("error parsing MAC address: %v", err)
}
}
this.ClientID, err = hex.DecodeString(stringUnMarshal.ClientID)
if err != nil {
return fmt.Errorf("error decoding clientID: %v", err)
return err
}
this.Hostname = stringUnMarshal.Hostname
this.Expiry = stringUnMarshal.Expiry
@ -86,10 +83,6 @@ func (this Lease) Equal(other Lease) bool {
return false
}
if !bytes.Equal(this.ClientID, other.ClientID) {
return false
}
if this.Hostname != other.Hostname {
return false
}

View File

@ -1,52 +0,0 @@
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())
}
startLease.ClientID = []byte("adsfasdfasf")
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

@ -25,8 +25,8 @@ type LeasePool interface {
*/
GetLease(net.IP) (bool, Lease, error)
//Get the lease already in use by that hardware address and/or client identifier.
GetLeaseForClient(net.HardwareAddr, []byte) (bool, Lease, error)
//Get the lease already in use by that hardware address.
GetLeaseForHardwareAddress(net.HardwareAddr) (bool, Lease, error)
/*
* -Lease Available

View File

@ -81,23 +81,13 @@ func (t *MemoryPool) GetLease(leaseIP net.IP) (bool, leasepool.Lease, error) {
return false, leasepool.Lease{}, nil
}
func makeKey(macAddress net.HardwareAddr, clientID []byte) []byte {
key := []byte(macAddress)
if len(clientID) > 0 {
key = append(key, clientID...)
}
return key
}
//Get the lease already in use by that hardware address and/or client identifier.
func (t *MemoryPool) GetLeaseForClient(macAddress net.HardwareAddr, clientID []byte) (bool, leasepool.Lease, error) {
//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()
needleKey := makeKey(macAddress, clientID)
for i := range t.pool {
haystackKey := makeKey(t.pool[i].MACAddress, t.pool[i].ClientID)
if bytes.Equal(needleKey, haystackKey) {
if bytes.Equal(t.pool[i].MACAddress, macAddress) {
return true, t.pool[i], nil
}
}
@ -149,7 +139,6 @@ func (t *MemoryPool) UpdateLease(lease leasepool.Lease) (bool, error) {
if t.pool[i].IP.Equal(lease.IP) {
t.pool[i].MACAddress = lease.MACAddress
t.pool[i].ClientID = lease.ClientID
t.pool[i].Hostname = lease.Hostname
t.pool[i].Expiry = lease.Expiry
t.pool[i].Status = lease.Status

View File

@ -1,56 +0,0 @@
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 {
test.Error("Error Getting Lease:" + err.Error())
}
if !hasLease {
test.Error("Failed to get get lease (none free?)")
}
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 {
test.Error("Error Getting Lease:" + err.Error())
}
if !hasLease {
test.Error("Failed to get get lease (none free?)")
}
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())
}
}