dhcp module update
This commit is contained in:
59
vendor/github.com/d2g/dhcp4server/leasepool/lease.go
generated
vendored
59
vendor/github.com/d2g/dhcp4server/leasepool/lease.go
generated
vendored
@ -1,7 +1,10 @@
|
||||
package leasepool
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
@ -18,37 +21,35 @@ 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
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
//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
|
||||
}
|
||||
|
||||
return json.Marshal(stringMarshal)
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Lease) UnmarshalJSON(data []byte) error {
|
||||
stringUnMarshal := struct {
|
||||
IP string
|
||||
Status int
|
||||
MACAddress string
|
||||
Hostname string
|
||||
Expiry time.Time
|
||||
}{}
|
||||
|
||||
stringUnMarshal := leaseMarshal{}
|
||||
err := json.Unmarshal(data, &stringUnMarshal)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -58,12 +59,14 @@ 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 err
|
||||
return fmt.Errorf("error decoding clientID: %v", err)
|
||||
}
|
||||
|
||||
this.Hostname = stringUnMarshal.Hostname
|
||||
this.Expiry = stringUnMarshal.Expiry
|
||||
|
||||
@ -83,6 +86,10 @@ 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
|
||||
}
|
||||
|
4
vendor/github.com/d2g/dhcp4server/leasepool/leasepool.go
generated
vendored
4
vendor/github.com/d2g/dhcp4server/leasepool/leasepool.go
generated
vendored
@ -25,8 +25,8 @@ type LeasePool interface {
|
||||
*/
|
||||
GetLease(net.IP) (bool, Lease, error)
|
||||
|
||||
//Get the lease already in use by that hardware address.
|
||||
GetLeaseForHardwareAddress(net.HardwareAddr) (bool, Lease, error)
|
||||
//Get the lease already in use by that hardware address and/or client identifier.
|
||||
GetLeaseForClient(net.HardwareAddr, []byte) (bool, Lease, error)
|
||||
|
||||
/*
|
||||
* -Lease Available
|
||||
|
17
vendor/github.com/d2g/dhcp4server/leasepool/memorypool/memorypool.go
generated
vendored
17
vendor/github.com/d2g/dhcp4server/leasepool/memorypool/memorypool.go
generated
vendored
@ -81,13 +81,23 @@ func (t *MemoryPool) GetLease(leaseIP net.IP) (bool, leasepool.Lease, error) {
|
||||
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) {
|
||||
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) {
|
||||
t.poolLock.Lock()
|
||||
defer t.poolLock.Unlock()
|
||||
|
||||
needleKey := makeKey(macAddress, clientID)
|
||||
for i := range t.pool {
|
||||
if bytes.Equal(t.pool[i].MACAddress, macAddress) {
|
||||
haystackKey := makeKey(t.pool[i].MACAddress, t.pool[i].ClientID)
|
||||
if bytes.Equal(needleKey, haystackKey) {
|
||||
return true, t.pool[i], nil
|
||||
}
|
||||
}
|
||||
@ -139,6 +149,7 @@ 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
|
||||
|
Reference in New Issue
Block a user