Change dhcp plugin to send ClientID allowing container to have multiple CNI
interfaces using dhcp ipam. Vendor latest dhcp4server, dhcp4client, dhcp4 Added additional tests for new functionality in dhcp2_test.go Wrap d2g dhcp4client calls with our own which add clientID to packet.
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
|
||||
}
|
||||
|
1
vendor/github.com/d2g/dhcp4server/leasepool/lease_test.go
generated
vendored
1
vendor/github.com/d2g/dhcp4server/leasepool/lease_test.go
generated
vendored
@ -23,6 +23,7 @@ func TestMarshaling(test *testing.T) {
|
||||
if err != nil {
|
||||
test.Error("Error Parsing Mac Address:" + err.Error())
|
||||
}
|
||||
startLease.ClientID = []byte("adsfasdfasf")
|
||||
|
||||
byteStartLease, err := json.Marshal(startLease)
|
||||
if err != nil {
|
||||
|
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
|
||||
|
26
vendor/github.com/d2g/dhcp4server/server.go
generated
vendored
26
vendor/github.com/d2g/dhcp4server/server.go
generated
vendored
@ -172,9 +172,6 @@ func (s *Server) ListenAndServe() error {
|
||||
// return err
|
||||
//}
|
||||
|
||||
//Make Our Buffer (Max Buffer is 574) "I believe this 576 size comes from RFC 791" - Random Mailing list quote of the day.
|
||||
buffer := make([]byte, 576)
|
||||
|
||||
log.Println("Trace: DHCP Server Listening.")
|
||||
|
||||
for {
|
||||
@ -183,6 +180,9 @@ func (s *Server) ListenAndServe() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//Make Our Buffer (Max Buffer is 574) "I believe this 576 size comes from RFC 791" - Random Mailing list quote of the day.
|
||||
buffer := make([]byte, 576)
|
||||
|
||||
//Set Read Deadline
|
||||
s.connection.SetReadDeadline(time.Now().Add(time.Second))
|
||||
// Read Packet
|
||||
@ -213,7 +213,6 @@ func (s *Server) ListenAndServe() error {
|
||||
}
|
||||
|
||||
log.Printf("Debug: Unexpect Error from Connection Read From: %v\n", err)
|
||||
log.Printf("Debug: err type %T %#v\n", err, err)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -281,6 +280,13 @@ func (s *Server) ListenAndServe() error {
|
||||
}
|
||||
}
|
||||
|
||||
func getClientID(packetOptions dhcp4.Options) []byte {
|
||||
if clientID, ok := packetOptions[dhcp4.OptionClientIdentifier]; ok {
|
||||
return clientID
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) ServeDHCP(packet dhcp4.Packet) (dhcp4.Packet, error) {
|
||||
packetOptions := packet.ParseOptions()
|
||||
|
||||
@ -307,6 +313,7 @@ func (s *Server) ServeDHCP(packet dhcp4.Packet) (dhcp4.Packet, error) {
|
||||
|
||||
lease.Status = leasepool.Reserved
|
||||
lease.MACAddress = packet.CHAddr()
|
||||
lease.ClientID = getClientID(packetOptions)
|
||||
|
||||
//If the lease expires within the next 5 Mins increase the lease expiary (Giving the Client 5 mins to complete)
|
||||
if lease.Expiry.Before(time.Now().Add(time.Minute * 5)) {
|
||||
@ -351,6 +358,7 @@ func (s *Server) ServeDHCP(packet dhcp4.Packet) (dhcp4.Packet, error) {
|
||||
} else {
|
||||
lease.Status = leasepool.Active
|
||||
lease.MACAddress = packet.CHAddr()
|
||||
lease.ClientID = getClientID(packetOptions)
|
||||
|
||||
lease.Expiry = time.Now().Add(s.leaseDuration)
|
||||
|
||||
@ -498,6 +506,8 @@ func (s *Server) DeclinePacket(requestPacket dhcp4.Packet) dhcp4.Packet {
|
||||
func (s *Server) GetLease(packet dhcp4.Packet) (found bool, lease leasepool.Lease, err error) {
|
||||
packetOptions := packet.ParseOptions()
|
||||
|
||||
clientID := getClientID(packetOptions)
|
||||
|
||||
//Requested an IP
|
||||
if (len(packetOptions) > 0) &&
|
||||
packetOptions[dhcp4.OptionRequestedIPAddress] != nil &&
|
||||
@ -510,11 +520,15 @@ func (s *Server) GetLease(packet dhcp4.Packet) (found bool, lease leasepool.Leas
|
||||
}
|
||||
|
||||
if found {
|
||||
//If lease is free, return it to client. If it is not
|
||||
//free match against the MAC address and client
|
||||
//identifier.
|
||||
if lease.Status == leasepool.Free {
|
||||
//Lease Is Free you Can Have it.
|
||||
return
|
||||
}
|
||||
if lease.Status != leasepool.Free && bytes.Equal(lease.MACAddress, packet.CHAddr()) {
|
||||
if bytes.Equal(lease.MACAddress, packet.CHAddr()) &&
|
||||
bytes.Equal(lease.ClientID, clientID) {
|
||||
//Lease isn't free but it's yours
|
||||
return
|
||||
}
|
||||
@ -522,7 +536,7 @@ func (s *Server) GetLease(packet dhcp4.Packet) (found bool, lease leasepool.Leas
|
||||
}
|
||||
|
||||
//Ok Even if you requested an IP you can't have it.
|
||||
found, lease, err = s.leasePool.GetLeaseForHardwareAddress(packet.CHAddr())
|
||||
found, lease, err = s.leasePool.GetLeaseForClient(packet.CHAddr(), clientID)
|
||||
if found || err != nil {
|
||||
return
|
||||
}
|
||||
|
64
vendor/github.com/d2g/dhcp4server/server_test.go
generated
vendored
64
vendor/github.com/d2g/dhcp4server/server_test.go
generated
vendored
@ -3,6 +3,7 @@ package dhcp4server_test
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
@ -409,6 +410,69 @@ func BenchmarkServeDHCP(test *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
func TestLeaseByClientID(test *testing.T) {
|
||||
//Setup the Server
|
||||
myServer, err := dhcp4server.New(
|
||||
net.IPv4(127, 0, 0, 1),
|
||||
getTestLeasePool(),
|
||||
)
|
||||
if err != nil {
|
||||
test.Error("Error: Can't Configure Server " + err.Error())
|
||||
}
|
||||
|
||||
// Setup A Client
|
||||
// Although We Won't send the packets over the network we'll use the client to create the requests.
|
||||
c, err := dhcp4client.NewInetSock(dhcp4client.SetLocalAddr(net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1068}), dhcp4client.SetRemoteAddr(net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1067}))
|
||||
if err != nil {
|
||||
test.Error("Client Conection Generation:" + err.Error())
|
||||
}
|
||||
|
||||
client, err := dhcp4client.New(dhcp4client.Connection(c))
|
||||
if err != nil {
|
||||
test.Error("Error: Can't Configure Client " + err.Error())
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
//Generate Hardware Address; used by both clients
|
||||
HardwareMACAddress, err := hardwareaddr.GenerateEUI48()
|
||||
if err != nil {
|
||||
test.Error("Error: Can't Generate Valid MACAddress" + err.Error())
|
||||
}
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
client.SetOption(dhcp4client.HardwareAddr(HardwareMACAddress))
|
||||
test.Log("MAC:" + HardwareMACAddress.String())
|
||||
|
||||
clientID := []byte(fmt.Sprintf("clientid-%d", i))
|
||||
test.Log("ClientID:" + string(clientID))
|
||||
|
||||
discovery := client.DiscoverPacket()
|
||||
discovery.AddOption(dhcp4.OptionClientIdentifier, clientID)
|
||||
|
||||
//Run the Discovery On the Server
|
||||
offer, err := myServer.ServeDHCP(discovery)
|
||||
_, err = myServer.ServeDHCP(discovery)
|
||||
if err != nil {
|
||||
test.Error("Discovery Error:" + err.Error())
|
||||
}
|
||||
|
||||
request := client.RequestPacket(&offer)
|
||||
request.AddOption(dhcp4.OptionClientIdentifier, clientID)
|
||||
acknowledgement, err := myServer.ServeDHCP(request)
|
||||
if err != nil {
|
||||
test.Error("Acknowledge Error:" + err.Error())
|
||||
}
|
||||
|
||||
test.Logf("Received Lease:%v\n", acknowledgement.YIAddr().String())
|
||||
if !dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i).Equal(acknowledgement.YIAddr()) {
|
||||
test.Error("Expected IP:" + dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i).String() + " Received:" + acknowledgement.YIAddr().String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getTestLeasePool() *memorypool.MemoryPool {
|
||||
//Create a Lease Pool We're going to use a memory pool
|
||||
//Remember the memory is cleared on restart so you will reissue the same IP Addresses.
|
||||
|
Reference in New Issue
Block a user