2023-05-02 16:27:27 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2023-05-06 05:21:27 +00:00
|
|
|
"time"
|
2023-05-02 16:27:27 +00:00
|
|
|
|
2023-05-09 07:28:54 +00:00
|
|
|
log "git.wntrmute.dev/kyle/goutils/log"
|
2023-05-02 16:27:27 +00:00
|
|
|
"git.wntrmute.dev/kyle/kdhcp/config"
|
2023-05-02 23:33:14 +00:00
|
|
|
"git.wntrmute.dev/kyle/kdhcp/dhcp"
|
2023-05-06 05:21:27 +00:00
|
|
|
"git.wntrmute.dev/kyle/kdhcp/iptools"
|
2023-05-09 07:28:54 +00:00
|
|
|
"github.com/benbjohnson/clock"
|
2023-05-02 16:27:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-05-06 05:21:27 +00:00
|
|
|
DefaultPool = "default"
|
|
|
|
MaxPacketSize = 512
|
|
|
|
MaxResponseWait = 5 * time.Minute
|
2023-05-02 16:27:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Conn net.PacketConn
|
|
|
|
Config *config.Config
|
2023-05-06 05:21:27 +00:00
|
|
|
Pools map[string]*iptools.Pool
|
|
|
|
Static map[string]*iptools.LeaseInfo
|
2023-05-09 07:28:54 +00:00
|
|
|
|
|
|
|
clock clock.Clock
|
2023-05-02 16:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
return s.Conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Bind() (err error) {
|
2023-05-02 23:33:14 +00:00
|
|
|
// In order to read DHCP packets, we'll need to listen on all addresses.
|
2023-05-06 05:21:27 +00:00
|
|
|
// That being said, we also want to limit our listening to the DHCP
|
|
|
|
// network device.
|
2023-05-02 23:33:14 +00:00
|
|
|
ip := net.IP([]byte{0, 0, 0, 0})
|
|
|
|
s.Conn, err = BindInterface(ip, s.Config.Port, s.Config.Interface)
|
2023-05-02 16:27:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ReadFrom() ([]byte, net.Addr, error) {
|
|
|
|
b := make([]byte, MaxPacketSize)
|
|
|
|
n, addr, err := s.Conn.ReadFrom(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b = b[:n]
|
|
|
|
return b, addr, nil
|
|
|
|
}
|
|
|
|
|
2023-05-02 23:33:14 +00:00
|
|
|
func (s *Server) ReadDHCPRequest() (*dhcp.BootRequest, error) {
|
|
|
|
pkt, addr, err := s.ReadFrom()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-06 05:21:27 +00:00
|
|
|
log.Debugf("server: read packet from %s", addr)
|
2023-05-02 23:33:14 +00:00
|
|
|
return dhcp.ReadRequest(pkt)
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:27:27 +00:00
|
|
|
func (s *Server) WriteTo(b []byte, addr net.Addr) error {
|
|
|
|
return errors.New("server: not implemented")
|
|
|
|
}
|
|
|
|
|
2023-05-09 07:28:54 +00:00
|
|
|
func (s *Server) AcceptPacket() (*dhcp.BootRequest, error) {
|
2023-05-02 23:33:14 +00:00
|
|
|
request, err := s.ReadDHCPRequest()
|
2023-05-02 16:27:27 +00:00
|
|
|
if err != nil {
|
2023-05-09 07:28:54 +00:00
|
|
|
return nil, err
|
2023-05-02 16:27:27 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 23:33:14 +00:00
|
|
|
log.Debugf("BOOTP request received from %x", request.HardwareAddress)
|
2023-05-09 07:28:54 +00:00
|
|
|
return request, nil
|
2023-05-02 16:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Listen() {
|
2023-05-09 07:28:54 +00:00
|
|
|
go s.updatePoolLoop()
|
|
|
|
|
2023-05-02 16:27:27 +00:00
|
|
|
for {
|
2023-05-09 07:28:54 +00:00
|
|
|
req, err := s.AcceptPacket()
|
|
|
|
if err != nil {
|
2023-05-02 16:27:27 +00:00
|
|
|
log.Errf("server: error reading packet: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2023-05-09 07:28:54 +00:00
|
|
|
|
|
|
|
lease := s.SelectLease(req, s.clock.Now())
|
|
|
|
if err != nil {
|
|
|
|
log.Err("server: couldn't find available lease")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("available lease: %s", lease)
|
|
|
|
continue
|
2023-05-02 16:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(cfg *config.Config) (*Server, error) {
|
|
|
|
srv := &Server{
|
|
|
|
Config: cfg,
|
2023-05-06 05:21:27 +00:00
|
|
|
Pools: map[string]*iptools.Pool{},
|
|
|
|
Static: map[string]*iptools.LeaseInfo{},
|
2023-05-09 07:28:54 +00:00
|
|
|
clock: clock.New(),
|
2023-05-02 16:27:27 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 05:21:27 +00:00
|
|
|
if err := srv.loadPoolsFromConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.LoadLeases(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.Bind(); err != nil {
|
2023-05-02 16:27:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("server: bound to %s:%s", cfg.Interface, srv.Conn.LocalAddr())
|
|
|
|
|
|
|
|
return srv, nil
|
|
|
|
}
|
2023-05-06 05:21:27 +00:00
|
|
|
|
|
|
|
func (srv *Server) SelectLease(req *dhcp.BootRequest, t time.Time) *iptools.LeaseInfo {
|
|
|
|
if li, ok := srv.Static[req.HostName]; ok {
|
|
|
|
return li
|
|
|
|
}
|
|
|
|
|
|
|
|
if pool, ok := srv.Pools[req.HostName]; ok {
|
|
|
|
return pool.Peek(t, MaxResponseWait)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pool, ok := srv.Pools[DefaultPool]; ok {
|
|
|
|
return pool.Peek(t, MaxResponseWait)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|