dhcp reads done, moving on to offers

This commit is contained in:
2023-05-09 07:28:54 +00:00
parent 6ba2bf3911
commit f66fbc0f6c
20 changed files with 196 additions and 58 deletions

View File

@@ -11,7 +11,10 @@ go_library(
],
importpath = "git.wntrmute.dev/kyle/kdhcp/iptools",
visibility = ["//visibility:public"],
deps = ["@dev_wntrmute_git_kyle_goutils//assert"],
deps = [
"@dev_wntrmute_git_kyle_goutils//assert",
"@dev_wntrmute_git_kyle_goutils//log",
],
)
go_test(

View File

@@ -15,6 +15,10 @@ type LeaseInfo struct {
Expires time.Time `yaml:"expires"`
}
func (li *LeaseInfo) String() string {
return fmt.Sprintf("lease[hostname=%s addr=%s hw=%x expires=%s]", li.HostName, li.Addr, li.HardwareAddress, li.Expires)
}
type sortableLease []*LeaseInfo
func (a sortableLease) Len() int { return len(a) }

View File

@@ -7,6 +7,7 @@ import (
"time"
"git.wntrmute.dev/kyle/goutils/assert"
"git.wntrmute.dev/kyle/goutils/log"
)
const DefaultExpiry = 168 * time.Hour
@@ -23,7 +24,7 @@ type Pool struct {
}
func (p *Pool) lock() {
if p.lock == nil {
if p.mtx == nil {
p.mtx = &sync.Mutex{}
}
@@ -48,6 +49,7 @@ func NewPool(name string, r *Range) (*Pool, error) {
Expiry: r.Expiry,
NoHostName: r.NoHostName,
Available: enumerateRange(name, r, true),
Limbo: map[netip.Addr]*LeaseInfo{},
}
return p, nil
@@ -114,39 +116,40 @@ func (p *Pool) Accept(addr netip.Addr, t time.Time) error {
// Update checks expirations on leases, moving any expired leases back
// to the available pool and removing any limbo leases that are expired.
func (p *Pool) Update(t time.Time) {
func (p *Pool) Update(t time.Time) bool {
p.lock()
defer p.unlock()
p.update(t)
return p.update(t)
}
func (p *Pool) update(t time.Time) {
evictedHosts := []netip.Addr{}
func (p *Pool) update(t time.Time) bool {
var updated bool
for k, v := range p.Active {
if v.IsExpired(t) {
evictedHosts = append(evictedHosts, k)
updated = true
log.Infof("expiring active address %s for %x", v.Addr, v.HardwareAddress)
delete(p.Active, k)
v = v.Reset()
p.Available = append(p.Available, v)
}
}
for _, ip := range evictedHosts {
delete(p.Active, ip)
}
for k, v := range p.Limbo {
if v.IsExpired(t) {
updated = true
log.Infof("expiring limbo address %s for %x", v.Addr, v.HardwareAddress)
delete(p.Limbo, k)
v = v.Reset()
p.Available = append(p.Available, v)
}
}
p.Sort()
p.sort()
return updated
}
func (p *Pool) Renew(lease LeaseInfo, t time.Time) {
func (p *Pool) Renew(lease *LeaseInfo, t time.Time) {
p.Active[lease.Addr].ResetExpiry(t, p.Expiry)
}