Skip to content

Commit

Permalink
dhcpsvc: imp code, dry
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Feb 15, 2024
1 parent 20f5ef8 commit 30691f0
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 126 deletions.
16 changes: 9 additions & 7 deletions internal/dhcpsvc/dhcpsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Interface interface {
// MACByIP returns the MAC address for the given IP address leased. It
// returns nil if there is no such client, due to an assumption that a DHCP
// client must always have a MAC address.
//
// TODO(e.burkov): Think of a contract for the returned value.
MACByIP(ip netip.Addr) (mac net.HardwareAddr)

// IPByHost returns the IP address of the DHCP client with the given
Expand All @@ -46,17 +48,17 @@ type Interface interface {
// signatures instead of cloning the whole list.
Leases() (ls []*Lease)

// AddLease adds a new DHCP lease. It returns an error if the lease is
// invalid or already exists.
// AddLease adds a new DHCP lease. l must be valid. It returns an error if
// l already exists.
AddLease(l *Lease) (err error)

// UpdateStaticLease changes an existing DHCP lease. It returns an error if
// there is no lease with such hardware address or if new values are invalid
// or already exist.
// UpdateStaticLease replaces an existing static DHCP lease. l must be
// valid. It returns an error if the lease with the given hardware address
// doesn't exist or if other values match another existing lease.
UpdateStaticLease(l *Lease) (err error)

// RemoveLease removes an existing DHCP lease. It returns an error if there
// is no lease equal to l.
// RemoveLease removes an existing DHCP lease. l must be valid. It returns
// an error if there is no lease equal to l.
RemoveLease(l *Lease) (err error)

// Reset removes all the DHCP leases.
Expand Down
4 changes: 2 additions & 2 deletions internal/dhcpsvc/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func (iface *netInterface) insertLease(l *Lease) (err error) {
return nil
}

// updateLease changes an existing lease within iface. It returns an error if
// there is no lease with such hardware address.
// updateLease replaces an existing lease within iface with the given one. It
// returns an error if there is no lease with such hardware address.
func (iface *netInterface) updateLease(l *Lease) (prev *Lease, err error) {
i, found := slices.BinarySearchFunc(iface.leases, l, compareLeaseMAC)
if !found {
Expand Down
106 changes: 106 additions & 0 deletions internal/dhcpsvc/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package dhcpsvc

import (
"bytes"
"fmt"
"net"
"net/netip"
"slices"
"strings"
"time"
)

Expand Down Expand Up @@ -50,3 +52,107 @@ func (l *Lease) Clone() (clone *Lease) {
func compareLeaseMAC(a, b *Lease) (res int) {
return bytes.Compare(a.HWAddr, b.HWAddr)
}

// leaseIndex is the set of leases indexed by their identifiers for quick
// lookup.
type leaseIndex struct {
// byAddr is a lookup shortcut for leases by their IP addresses.
byAddr map[netip.Addr]*Lease

// byName is a lookup shortcut for leases by their hostnames.
//
// TODO(e.burkov): Use a slice of leases with the same hostname?
byName map[string]*Lease
}

// leaseByAddr returns a lease by its IP address.
func (idx *leaseIndex) leaseByAddr(addr netip.Addr) (l *Lease, ok bool) {
l, ok = idx.byAddr[addr]

return l, ok
}

// leaseByName returns a lease by its hostname.
func (idx *leaseIndex) leaseByName(name string) (l *Lease, ok bool) {
// TODO(e.burkov): Probably, use a case-insensitive comparison and store in
// slice. This would require a benchmark.
l, ok = idx.byName[strings.ToLower(name)]

return l, ok
}

// clear removes all leases from idx.
func (idx *leaseIndex) clear() {
clear(idx.byAddr)
clear(idx.byName)
}

// add adds l into idx and into iface.
func (idx *leaseIndex) add(l *Lease, iface *netInterface) (err error) {
loweredName := strings.ToLower(l.Hostname)

if _, ok := idx.byAddr[l.IP]; ok {
return fmt.Errorf("lease for ip %s already exists", l.IP)
} else if _, ok = idx.byName[loweredName]; ok {
return fmt.Errorf("lease for hostname %s already exists", l.Hostname)
}

err = iface.insertLease(l)
if err != nil {
return err
}

idx.byAddr[l.IP] = l
idx.byName[loweredName] = l

return nil
}

// remove removes l from idx and from iface.
func (idx *leaseIndex) remove(l *Lease, iface *netInterface) (err error) {
loweredName := strings.ToLower(l.Hostname)

if _, ok := idx.byAddr[l.IP]; !ok {
return fmt.Errorf("no lease for ip %s", l.IP)
} else if _, ok = idx.byName[loweredName]; !ok {
return fmt.Errorf("no lease for hostname %s", l.Hostname)
}

err = iface.removeLease(l)
if err != nil {
return err
}

delete(idx.byAddr, l.IP)
delete(idx.byName, loweredName)

return nil
}

// update updates l in idx and in iface.
func (idx *leaseIndex) update(l *Lease, iface *netInterface) (err error) {
loweredName := strings.ToLower(l.Hostname)

existing, ok := idx.byAddr[l.IP]
if ok && !slices.Equal(l.HWAddr, existing.HWAddr) {
return fmt.Errorf("lease for ip %s already exists", l.IP)
}

existing, ok = idx.byName[loweredName]
if ok && !slices.Equal(l.HWAddr, existing.HWAddr) {
return fmt.Errorf("lease for hostname %s already exists", l.Hostname)
}

prev, err := iface.updateLease(l)
if err != nil {
return err
}

delete(idx.byAddr, prev.IP)
delete(idx.byName, strings.ToLower(prev.Hostname))

idx.byAddr[l.IP] = l
idx.byName[loweredName] = l

return nil
}
Loading

0 comments on commit 30691f0

Please sign in to comment.