Starting DHCP packets.

This commit is contained in:
2023-05-02 23:33:14 +00:00
parent 3ec9af1fde
commit ac41c7a480
17 changed files with 11743 additions and 32 deletions

View File

@@ -10,6 +10,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//config",
"//dhcp",
"//log",
],
)

View File

@@ -5,6 +5,7 @@ import (
"net"
"git.wntrmute.dev/kyle/kdhcp/config"
"git.wntrmute.dev/kyle/kdhcp/dhcp"
"git.wntrmute.dev/kyle/kdhcp/log"
)
@@ -22,7 +23,9 @@ func (s *Server) Close() error {
}
func (s *Server) Bind() (err error) {
s.Conn, err = BindInterface(s.Config.IP, s.Config.Port, s.Config.Interface)
// In order to read DHCP packets, we'll need to listen on all addresses.
ip := net.IP([]byte{0, 0, 0, 0})
s.Conn, err = BindInterface(ip, s.Config.Port, s.Config.Interface)
return err
}
@@ -37,17 +40,27 @@ func (s *Server) ReadFrom() ([]byte, net.Addr, error) {
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 %db packet from %s", len(pkt), 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 {
packet, addr, err := s.ReadFrom()
request, err := s.ReadDHCPRequest()
if err != nil {
return err
}
log.Debugf("accepted %d byte packet from %s", len(packet), addr)
log.Debugf("BOOTP request received from %x", request.HardwareAddress)
return nil
}