package server import ( "errors" "net" "time" log "git.wntrmute.dev/kyle/goutils/syslog" "git.wntrmute.dev/kyle/kdhcp/config" "git.wntrmute.dev/kyle/kdhcp/dhcp" "git.wntrmute.dev/kyle/kdhcp/iptools" ) const ( DefaultPool = "default" MaxPacketSize = 512 MaxResponseWait = 5 * time.Minute ) type Server struct { Conn net.PacketConn Config *config.Config Pools map[string]*iptools.Pool Static map[string]*iptools.LeaseInfo } func (s *Server) Close() error { return s.Conn.Close() } func (s *Server) Bind() (err error) { // In order to read DHCP packets, we'll need to listen on all addresses. // That being said, we also want to limit our listening to the DHCP // network device. ip := net.IP([]byte{0, 0, 0, 0}) s.Conn, err = BindInterface(ip, s.Config.Port, s.Config.Interface) 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 } func (s *Server) ReadDHCPRequest() (*dhcp.BootRequest, error) { pkt, addr, err := s.ReadFrom() if err != nil { return nil, err } log.Debugf("server: read packet from %s", addr) return dhcp.ReadRequest(pkt) } func (s *Server) WriteTo(b []byte, addr net.Addr) error { return errors.New("server: not implemented") } func (s *Server) AcceptPacket() error { request, err := s.ReadDHCPRequest() if err != nil { return err } log.Debugf("BOOTP request received from %x", request.HardwareAddress) return nil } func (s *Server) Listen() { for { if err := s.AcceptPacket(); err != nil { log.Errf("server: error reading packet: %s", err) continue } break } } func New(cfg *config.Config) (*Server, error) { srv := &Server{ Config: cfg, Pools: map[string]*iptools.Pool{}, Static: map[string]*iptools.LeaseInfo{}, } if err := srv.loadPoolsFromConfig(); err != nil { return nil, err } if err := srv.LoadLeases(); err != nil { return nil, err } if err := srv.Bind(); err != nil { return nil, err } log.Infof("server: bound to %s:%s", cfg.Interface, srv.Conn.LocalAddr()) return srv, nil } 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 }