kdhcp/server/dhcp_transaction.go

59 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-05-15 13:09:12 +00:00
package server
import (
"net"
log "git.wntrmute.dev/kyle/goutils/log"
"git.wntrmute.dev/kyle/kdhcp/dhcp"
)
func (srv *Server) WriteDHCPResponse(req *dhcp.Packet, ip net.Addr) error {
lease := srv.SelectLease(req, srv.clock.Now())
if lease == nil {
log.Errln("server: couldn't find available lease")
return nil
}
log.Debugf("available lease: %s", lease)
response, err := dhcp.NewOffer(req, srv.Config.Network.NetworkInfo(), lease)
if err != nil {
log.Errln(err.Error())
return err
}
log.Debugf("building offer for address %s", lease.Addr)
pkt, err := dhcp.WritePacket(response)
if err != nil {
log.Errln(err.Error())
return err
}
ip, err = addrReply(ip)
if err != nil {
log.Errln(err.Error())
return err
}
log.Debugf("writing offer to %s", ip)
_, err = srv.Conn.WriteTo(pkt, ip)
if err != nil {
log.Errf("failed to write packet: %s", err)
return err
}
log.Infof("sending offer for address %s to %s", lease.Addr, req.HardwareAddress)
go func() {
if err := srv.SaveLeases(); err != nil {
log.Warningf("server: while saving leases: %s", err)
}
}()
err = srv.AcceptLease(req, lease, srv.clock.Now())
if err != nil {
log.Errln(err.Error())
return err
}
return nil
}