2023-05-06 05:21:27 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
2023-05-09 07:28:54 +00:00
|
|
|
"time"
|
2023-05-06 05:21:27 +00:00
|
|
|
|
2023-05-09 07:28:54 +00:00
|
|
|
log "git.wntrmute.dev/kyle/goutils/log"
|
2023-05-06 05:21:27 +00:00
|
|
|
"git.wntrmute.dev/kyle/kdhcp/iptools"
|
2023-05-15 13:09:12 +00:00
|
|
|
"git.wntrmute.dev/kyle/kdhcp/leases"
|
2023-05-06 05:21:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// pools.go adds pool functionality to the server.
|
|
|
|
|
|
|
|
func (srv *Server) loadPoolsFromConfig() error {
|
|
|
|
for host, ip := range srv.Config.Statics {
|
|
|
|
addr, ok := netip.AddrFromSlice(ip.To4())
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("server: while instantiating pools, could not load IP %s", ip)
|
|
|
|
}
|
|
|
|
log.Debugf("server: added static host entry %s -> %s", host, addr)
|
2023-05-15 13:09:12 +00:00
|
|
|
srv.Static[host] = &leases.Info{
|
2023-05-06 05:21:27 +00:00
|
|
|
HostName: host,
|
|
|
|
Addr: addr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, ipRange := range srv.Config.Pools {
|
|
|
|
pool, err := iptools.NewPool(name, ipRange)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("server: couldn't load pool %s: %w", name, err)
|
|
|
|
}
|
|
|
|
log.Debugf("server: added pool %s: %s -> %s", name, ipRange.Start, ipRange.End)
|
|
|
|
|
|
|
|
srv.Pools[name] = pool
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-09 07:28:54 +00:00
|
|
|
|
|
|
|
func (srv *Server) updatePoolLoop() {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
for _, p := range srv.Pools {
|
|
|
|
if p.Update(srv.clock.Now()) {
|
|
|
|
log.Debugln("pools updated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|