26 #include <constants.h> 32 #define DHCP_SERV_MAX_INTF (4) 33 #define DHCP_OFFER_DURATION (2 * TICKS_PER_SECOND) 34 #define DHCP_SERV_MAX_HOSTNAME_LEN (32) 36 #define LEASE_POOL_SIZE 150 37 #define DHCPD_STARTING_ADDRESS 0xC0A80184 41 typedef enum LeaseState
50 struct DhcpLeaseRequest
56 char hostname[DHCP_SERV_MAX_HOSTNAME_LEN + 1];
64 char hostname[DHCP_SERV_MAX_HOSTNAME_LEN + 1];
76 const char *domain_name;
87 LeaseAllocator *m_pNext;
93 inline LeaseAllocator *SetNextAllocator(LeaseAllocator *nextAlloc)
98 inline LeaseAllocator *GetNextAllocator() {
return m_pNext; }
100 virtual uint32_t GetLeaseTime() = 0;
101 virtual bool OfferLease(DhcpLeaseRequest *pLease,
int intfNum) = 0;
102 virtual bool RequestLease(DhcpLeaseRequest *pLease,
int intfNum) = 0;
103 virtual bool ReleaseLease(DhcpLeaseRequest *pLease,
int intfNum) = 0;
104 virtual bool LeaseValid(DhcpLeaseRequest *pLease,
int intfNum) = 0;
105 virtual bool GetDhcpInfo(DhcpInfo &infoBlock, MACADR &client_mac,
int intfNum) = 0;
106 virtual bool AddInterface(
int intfNum) = 0;
107 virtual void RemoveInterface(
int intfNum) = 0;
108 virtual bool GetLeaseData(DhcpLeaseData *data) = 0;
113 class SingleAllocator :
public LeaseAllocator
118 uint32_t m_leaseDuration;
119 DhcpInfo m_configInfo;
122 SingleAllocator(IPADDR4 ip) : LeaseAllocator(), m_theIP(ip), m_leaseDuration(3600) {}
125 inline virtual bool OfferLease(DhcpLeaseRequest *pLease,
int intfNum)
127 pLease->ip = m_theIP;
128 pLease->duration = m_leaseDuration;
131 inline virtual bool RequestLease(DhcpLeaseRequest *pLease,
int intfNum)
133 if (pLease->ip != m_theIP)
135 pLease->ip = 0x00000000;
138 pLease->ip = m_theIP;
139 pLease->duration = m_leaseDuration;
142 inline virtual bool ReleaseLease(DhcpLeaseRequest *pLease,
int intfNum) {
return pLease->ip == m_theIP; }
143 inline virtual bool LeaseValid(DhcpLeaseRequest *pLease,
int intfNum)
145 pLease->duration = m_leaseDuration;
146 return pLease->ip == m_theIP;
148 virtual bool SetStaticLease(DhcpLeaseRequest *pLease) {
return false; }
149 virtual uint32_t GetLeaseTime() {
return m_leaseDuration; }
151 void SetLeaseTime(uint32_t hours, uint32_t minutes = 0, uint32_t seconds = 0);
152 virtual bool GetDhcpInfo(DhcpInfo &infoBlock, MACADR &client_mac,
int intfNum);
153 virtual void UpdateDhcpInfo(
const DhcpInfo *infoBlock) {
return; }
154 virtual bool AddInterface(
int intfNum) {
return true; }
155 virtual void RemoveInterface(
int intfNum) {
return; }
156 virtual bool IsRegisteredInterface(
int intfNum) {
return true; }
157 virtual bool GetLeaseData(DhcpLeaseData *data) {
return false; }
161 class BlockAllocator :
public LeaseAllocator
164 const int m_leaseCount;
165 DhcpLeaseData *
const m_leaseBlock;
166 bool m_staticLeaseExist;
167 uint32_t m_lastIndex;
168 int m_validIntf[DHCP_SERV_MAX_INTF];
170 bool IsValidIntf(
int intfNum);
171 uint32_t m_leaseDuration;
172 DhcpInfo m_configInfo;
175 BlockAllocator(
const IPADDR4 startIP,
const int leaseCount, DhcpLeaseData *
const leaseBlock);
177 void SetLeaseTime(uint32_t hours, uint32_t minutes = 0, uint32_t seconds = 0);
179 virtual uint32_t GetLeaseTime() {
return m_leaseDuration; }
180 virtual bool OfferLease(DhcpLeaseRequest *pLease,
int intfNum);
181 virtual bool RequestLease(DhcpLeaseRequest *pLease,
int intfNum);
182 virtual bool ReleaseLease(DhcpLeaseRequest *pLease,
int intfNum);
183 virtual bool LeaseValid(DhcpLeaseRequest *pLease,
int intfNum);
184 virtual bool SetStaticLease(DhcpLeaseRequest *pLease);
185 virtual bool GetDhcpInfo(DhcpInfo &infoBlock, MACADR &client_mac,
int intfNum);
186 virtual void UpdateDhcpInfo(
const DhcpInfo *infoBlock);
187 virtual bool AddInterface(
int intfNum);
188 virtual void RemoveInterface(
int intfNum);
189 inline virtual bool IsRegisteredInterface(
int intfNum)
191 for (
int i = 0; i < DHCP_SERV_MAX_INTF; i++)
193 if (m_validIntf[i] == intfNum) {
return true; }
197 virtual bool GetLeaseData(DhcpLeaseData *data);
200 inline void SetStartIP(
const IPADDR4 newStartIP)
202 m_startIP = newStartIP;
210 class MacPrefixAllocator :
public BlockAllocator
212 const MACADR m_macMask;
213 const MACADR m_macPrefix;
214 const bool m_whitelist;
217 MacPrefixAllocator(
const MACADR prefix,
219 const bool whitelist,
220 const IPADDR4 startIP,
221 const int leaseCount,
222 DhcpLeaseData *
const leaseBlock);
223 ~MacPrefixAllocator();
224 virtual bool OfferLease(DhcpLeaseRequest *pLease,
int intfNum);
225 virtual bool RequestLease(DhcpLeaseRequest *pLease,
int intfNum);
226 virtual bool SetStaticLease(DhcpLeaseRequest *pLease);
233 static Server *theInstance;
234 LeaseAllocator *m_pLeaseAlloc;
237 void ProcessDiscover(PoolPtr pp);
238 void ProcessRequest(PoolPtr pp);
239 void ProcessDecline(PoolPtr pp);
240 void ProcessRelease(PoolPtr pp);
241 void ProcessInform(PoolPtr pp);
243 void ProcessParamReq(uint8_t *&optBuf, uint8_t *ReqList, uint8_t reqLen,
const DhcpInfo &info, IPADDR4 intfIP);
248 bool AddLeaseAllocator(LeaseAllocator *newAllocator);
249 bool ProcessServerMessage(PoolPtr pp);
251 bool GetDhcpClients(DhcpLeaseData *data);
253 bool AddInterface(
int intfNum);
254 void RemoveInterface(
int intfNum);
256 static inline bool AddServerInterface(
int intfNum)
258 if (theInstance) {
return theInstance->AddInterface(intfNum); }
261 static inline void RemoveServerInterface(
int intfNum)
263 if (theInstance) { theInstance->RemoveInterface(intfNum); }
265 static inline void ProcessMessage(PoolPtr pp)
267 if (theInstance) { theInstance->ProcessServerMessage(pp); }
269 static inline Server *GetInstance() {
return theInstance; }
bool AddStandardDHCPServer(int intf=0, IPADDR4 startAddr=IPADDR4::NullIP())
Starts a standard allocator DHCP server.
Definition: dhcpd_standard.cpp:34