github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/hypervisor/dhcpd/api.go (about) 1 package dhcpd 2 3 import ( 4 "net" 5 "sync" 6 "time" 7 8 "github.com/Cloud-Foundations/Dominator/lib/log" 9 "github.com/Cloud-Foundations/Dominator/lib/net/util" 10 proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor" 11 ) 12 13 type DhcpServer struct { 14 dynamicLeasesFile string 15 logger log.DebugLogger 16 cleanupTrigger chan<- struct{} 17 interfaceIPs map[string][]net.IP // Key: interface name. 18 myIPs []net.IP 19 networkBootImage []byte 20 requestInterface string 21 routeTable map[string]*util.RouteEntry // Key: interface name. 22 mutex sync.RWMutex // Protect everything below. 23 ackChannels map[string]chan struct{} // Key: IPaddr. 24 dynamicLeases map[string]*leaseType // Key: MACaddr. 25 interfaceSubnets map[string][]*subnetType // Key: interface name. 26 ipAddrToMacAddr map[string]string // Key: IPaddr, V: MACaddr. 27 staticLeases map[string]leaseType // Key: MACaddr. 28 requestChannels map[string]chan net.IP // Key: MACaddr. 29 subnets []*subnetType 30 } 31 32 type leaseType struct { 33 proto.Address 34 clientHostname string 35 expires time.Time 36 hostname string 37 doNetboot bool 38 subnet *subnetType 39 } 40 41 type subnetType struct { 42 amGateway bool 43 myIP net.IP 44 nextDynamicIP net.IP 45 proto.Subnet 46 } 47 48 func New(interfaceNames []string, dynamicLeasesFile string, 49 logger log.DebugLogger) (*DhcpServer, error) { 50 return newServer(interfaceNames, dynamicLeasesFile, logger) 51 } 52 53 func (s *DhcpServer) AddLease(address proto.Address, hostname string) error { 54 return s.addLease(address, false, hostname, nil) 55 } 56 57 func (s *DhcpServer) AddNetbootLease(address proto.Address, 58 hostname string, subnet *proto.Subnet) error { 59 return s.addLease(address, true, hostname, subnet) 60 } 61 62 func (s *DhcpServer) AddSubnet(subnet proto.Subnet) { 63 s.addSubnet(subnet) 64 } 65 66 func (s *DhcpServer) MakeAcknowledgmentChannel(ipAddr net.IP) <-chan struct{} { 67 return s.makeAcknowledgmentChannel(ipAddr) 68 } 69 70 func (s *DhcpServer) MakeRequestChannel(macAddr string) <-chan net.IP { 71 return s.makeRequestChannel(macAddr) 72 } 73 74 func (s *DhcpServer) RemoveLease(ipAddr net.IP) { 75 s.removeLease(ipAddr) 76 } 77 78 func (s *DhcpServer) RemoveSubnet(subnetId string) { 79 s.removeSubnet(subnetId) 80 } 81 82 func (s *DhcpServer) SetNetworkBootImage(nbiName string) error { 83 s.networkBootImage = []byte(nbiName) 84 return nil 85 }