option.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package reservation
  2. import (
  3. "context"
  4. "dhcp/internal/dhcpServer/data"
  5. "github.com/insomniacslk/dhcp/dhcpv4"
  6. "net/netip"
  7. )
  8. // setDHCPOpts takes a client dhcpServer packet and data (typically from a macAndIPXE) and creates a slice of dhcpServer packet modifiers.
  9. // m is the dhcpServer request from a client. d is the data to use to create the dhcpServer packet modifiers.
  10. // This is most likely the place where we would have any business logic for determining dhcpServer option setting.
  11. func (h *Handler) setDHCPOpts(_ context.Context, _ *dhcpv4.DHCPv4, d *data.DHCP) []dhcpv4.Modifier {
  12. mods := []dhcpv4.Modifier{
  13. dhcpv4.WithLeaseTime(d.LeaseTime),
  14. dhcpv4.WithYourIP(d.IPAddress.AsSlice()),
  15. }
  16. if len(d.NameServers) > 0 {
  17. mods = append(mods, dhcpv4.WithDNS(d.NameServers...))
  18. }
  19. if len(d.DomainSearch) > 0 {
  20. mods = append(mods, dhcpv4.WithDomainSearchList(d.DomainSearch...))
  21. }
  22. if len(d.NTPServers) > 0 {
  23. mods = append(mods, dhcpv4.WithOption(dhcpv4.OptNTPServers(d.NTPServers...)))
  24. }
  25. if d.BroadcastAddress.Compare(netip.Addr{}) != 0 {
  26. mods = append(mods, dhcpv4.WithGeneric(dhcpv4.OptionBroadcastAddress, d.BroadcastAddress.AsSlice()))
  27. }
  28. if d.DomainName != "" {
  29. mods = append(mods, dhcpv4.WithGeneric(dhcpv4.OptionDomainName, []byte(d.DomainName)))
  30. }
  31. if len(d.SubnetMask) > 0 {
  32. mods = append(mods, dhcpv4.WithNetmask(d.SubnetMask))
  33. }
  34. if d.DefaultGateway.Compare(netip.Addr{}) != 0 {
  35. mods = append(mods, dhcpv4.WithRouter(d.DefaultGateway.AsSlice()))
  36. }
  37. return mods
  38. }