NetBurner 3.5.6
PDF Version
dhcpd.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
21#ifndef __DHCPD_H
22#define __DHCPD_H
23
24#include <predef.h>
25#include <constants.h>
26#include <buffers.h>
27#include <nettypes.h>
28
29#define DHCP_SERV_MAX_INTF (4)
30#define DHCP_OFFER_DURATION (2 * TICKS_PER_SECOND)
31#define DHCP_SERV_MAX_HOSTNAME_LEN (32)
32
33#define LEASE_POOL_SIZE 150
34#define DHCPD_STARTING_ADDRESS 0xC0A80184
35
38namespace DHCP
39{
40
50
60
69
87
90{
91 LeaseAllocator *m_pNext;
92
93 public:
96
97 inline LeaseAllocator *SetNextAllocator(LeaseAllocator *nextAlloc)
98 {
99 m_pNext = nextAlloc;
100 return m_pNext;
101 }
102 inline LeaseAllocator *GetNextAllocator() { return m_pNext; }
103
104 virtual uint32_t GetLeaseTime() = 0;
105 virtual bool OfferLease(DhcpLeaseRequest *pLease, int intfNum) = 0;
106 virtual bool RequestLease(DhcpLeaseRequest *pLease, int intfNum) = 0;
107 virtual bool ReleaseLease(DhcpLeaseRequest *pLease, int intfNum) = 0;
108 virtual bool LeaseValid(DhcpLeaseRequest *pLease, int intfNum) = 0;
109 virtual bool GetDhcpInfo(DhcpInfo &infoBlock, MACADR &client_mac, int intfNum) = 0;
110 virtual bool AddInterface(int intfNum) = 0;
111 virtual void RemoveInterface(int intfNum) = 0;
112 virtual bool GetLeaseData(DhcpLeaseData *data) = 0;
113};
114
120{
121 IPADDR4 m_theIP;
122 int m_intfNum;
123
124 protected:
125 uint32_t m_leaseDuration; //< @brief Lease duration to hand out, in seconds
126 DhcpInfo m_configInfo;
127
128 public:
129 SingleAllocator(IPADDR4 ip) : LeaseAllocator(), m_theIP(ip), m_intfNum(-1), m_leaseDuration(3600) {}
131
132 inline void ChangeAllocAddr(IPADDR4 ip) { m_theIP = ip; }
133
134 inline virtual bool OfferLease(DhcpLeaseRequest *pLease, int intfNum)
135 {
136 if (!m_intfNum)
137 return false;
138 if ((m_intfNum > 0) && (m_intfNum != intfNum))
139 return false;
140 pLease->ip = m_theIP;
141 pLease->duration = m_leaseDuration;
142 return true;
143 }
144 inline virtual bool RequestLease(DhcpLeaseRequest *pLease, int intfNum)
145 {
146 if (!m_intfNum)
147 return false;
148 if ((m_intfNum > 0) && (m_intfNum != intfNum))
149 return false;
150 if (pLease->ip != m_theIP)
151 {
152 pLease->ip = 0x00000000;
153 return false;
154 }
155 pLease->ip = m_theIP;
156 pLease->duration = m_leaseDuration;
157 return true;
158 }
159 inline virtual bool ReleaseLease(DhcpLeaseRequest *pLease, int intfNum)
160 {
161 if (!m_intfNum)
162 return false;
163 if ((m_intfNum > 0) && (m_intfNum != intfNum))
164 return false;
165 return pLease->ip == m_theIP;
166 }
167 inline virtual bool LeaseValid(DhcpLeaseRequest *pLease, int intfNum)
168 {
169 if (!m_intfNum)
170 return false;
171 if ((m_intfNum > 0) && (m_intfNum != intfNum))
172 return false;
173 pLease->duration = m_leaseDuration;
174 return pLease->ip == m_theIP;
175 }
176 virtual bool SetStaticLease(DhcpLeaseRequest *pLease) { return false; }
177 virtual uint32_t GetLeaseTime() { return m_leaseDuration; }
178
179 void SetLeaseTime(uint32_t hours, uint32_t minutes = 0, uint32_t seconds = 0);
180 virtual bool GetDhcpInfo(DhcpInfo &infoBlock, MACADR &client_mac, int intfNum);
181 virtual void UpdateDhcpInfo(const DhcpInfo *infoBlock) { return; }
182 virtual bool AddInterface(int intfNum) { m_intfNum = intfNum; return true; }
183 virtual void RemoveInterface(int intfNum) { if (m_intfNum == intfNum) m_intfNum = 0; return; }
184 virtual bool IsRegisteredInterface(int intfNum) { return true; }
185 virtual bool GetLeaseData(DhcpLeaseData *data) { return false; }
186};
187
190{
191 IPADDR4 m_startIP;
192 const int m_leaseCount;
193 DhcpLeaseData *const m_leaseBlock;
194 bool m_staticLeaseExist;
195 uint32_t m_lastIndex;
196 int m_validIntf[DHCP_SERV_MAX_INTF];
197
198 bool IsValidIntf(int intfNum);
199 uint32_t m_leaseDuration;
200 DhcpInfo m_configInfo;
201
202 public:
203 BlockAllocator(const IPADDR4 startIP, const int leaseCount, DhcpLeaseData *const leaseBlock);
205 void SetLeaseTime(uint32_t hours, uint32_t minutes = 0, uint32_t seconds = 0);
206
207 virtual uint32_t GetLeaseTime() { return m_leaseDuration; }
208 virtual bool OfferLease(DhcpLeaseRequest *pLease, int intfNum);
209 virtual bool RequestLease(DhcpLeaseRequest *pLease, int intfNum);
210 virtual bool ReleaseLease(DhcpLeaseRequest *pLease, int intfNum);
211 virtual bool LeaseValid(DhcpLeaseRequest *pLease, int intfNum);
212 virtual bool SetStaticLease(DhcpLeaseRequest *pLease);
213 virtual bool GetDhcpInfo(DhcpInfo &infoBlock, MACADR &client_mac, int intfNum);
214 virtual void UpdateDhcpInfo(const DhcpInfo *infoBlock);
215 virtual bool AddInterface(int intfNum);
216 virtual void RemoveInterface(int intfNum);
217 inline virtual bool IsRegisteredInterface(int intfNum)
218 {
219 for (int i = 0; i < DHCP_SERV_MAX_INTF; i++)
220 {
221 if (m_validIntf[i] == intfNum) { return true; }
222 }
223 return false;
224 }
225 virtual bool GetLeaseData(DhcpLeaseData *data);
226
227 void ResetLeases();
228 inline void SetStartIP(const IPADDR4 newStartIP)
229 {
230 m_startIP = newStartIP;
231 ResetLeases();
232 }
233};
234
240{
241 const MACADR m_macMask; // the bitmask to filter against
242 const MACADR m_macPrefix; // the MAC prefix to scan against
243 const bool m_whitelist; // are we operating in Whitelist mode?
244
245 public:
246 MacPrefixAllocator(const MACADR prefix,
247 const MACADR mask,
248 const bool whitelist,
249 const IPADDR4 startIP,
250 const int leaseCount,
251 DhcpLeaseData *const leaseBlock);
253 virtual bool OfferLease(DhcpLeaseRequest *pLease, int intfNum);
254 virtual bool RequestLease(DhcpLeaseRequest *pLease, int intfNum);
255 virtual bool SetStaticLease(DhcpLeaseRequest *pLease);
256};
257
262{
263 static Server *theInstance;
264 LeaseAllocator *m_pLeaseAlloc;
265 uint32_t m_nextTick;
266
267 void ProcessDiscover(PoolPtr pp);
268 void ProcessRequest(PoolPtr pp);
269 void ProcessDecline(PoolPtr pp);
270 void ProcessRelease(PoolPtr pp);
271 void ProcessInform(PoolPtr pp);
272
273 void ProcessParamReq(uint8_t *&optBuf, uint8_t *ReqList, uint8_t reqLen, const DhcpInfo &info, IPADDR4 intfIP);
274
275 public:
276 Server();
277 ~Server();
278 bool AddLeaseAllocator(LeaseAllocator *newAllocator);
279 bool ProcessServerMessage(PoolPtr pp);
280
281 bool GetDhcpClients(DhcpLeaseData *data);
282
283 bool AddInterface(int intfNum);
284 void RemoveInterface(int intfNum);
285
286 static inline bool AddServerInterface(int intfNum)
287 {
288 if (theInstance) { return theInstance->AddInterface(intfNum); }
289 return false;
290 }
291 static inline void RemoveServerInterface(int intfNum)
292 {
293 if (theInstance) { theInstance->RemoveInterface(intfNum); }
294 }
295 static inline void ProcessMessage(PoolPtr pp)
296 {
297 if (theInstance) { theInstance->ProcessServerMessage(pp); }
298 }
299 static inline Server *GetInstance() { return theInstance; }
300};
301} // namespace DHCP
302
317bool AddStandardDHCPServer(int intf = 0, IPADDR4 startAddr = IPADDR4::NullIP());
318
319#endif /* ----- #ifndef __DHCPD_H ----- */
320
Basic allocator that handles multiple leases in a contiguous IP block.
Definition dhcpd.h:190
Base class/interface for lease allocators for the DHCP server.
Definition dhcpd.h:90
Derived from BlockAllocator Has the ability to whitelist/blacklist certain mac address ranges based o...
Definition dhcpd.h:240
DHCP Server class Requires a lease allocator to be added in order to function.
Definition dhcpd.h:262
Stripped down allocator Fails to conform to many standard behaviors, but shows the basics of what nee...
Definition dhcpd.h:120
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:225
static IPADDR4 NullIP()
C++ static function for a null IP address.
Definition nettypes.h:363
Used to store and manipulate MAC addresses.
Definition nettypes.h:69
bool AddStandardDHCPServer(int intf=0, IPADDR4 startAddr=IPADDR4::NullIP())
Starts a standard allocator DHCP server.
#define DHCP_SERV_MAX_HOSTNAME_LEN
Definition dhcpd.h:31
#define DHCP_SERV_MAX_INTF
Definition dhcpd.h:29
DHCP Namespace.
Definition dhcpd.h:39
enum DHCP::LeaseState LeaseState_t
Lease State.
LeaseState
Lease State.
Definition dhcpd.h:43
@ LEASE_OPEN
Definition dhcpd.h:44
@ LEASE_TAKEN
Definition dhcpd.h:46
@ LEASE_STATIC
Definition dhcpd.h:47
@ LEASE_OFFERED
Definition dhcpd.h:45
@ ARP_CONFLICT
Definition dhcpd.h:48
DHCP Info.
Definition dhcpd.h:72
IPADDR4 dns_1
primary DNS Server
Definition dhcpd.h:75
IPADDR4 dns_2
secondary DNS Server
Definition dhcpd.h:76
uint8_t * bonusOpts
additional options, pre written
Definition dhcpd.h:85
IPADDR4 logServ
Syslog Server.
Definition dhcpd.h:77
IPADDR4 ntpServ
NTP Server.
Definition dhcpd.h:79
char * bootfile
tftp boot file, null terminated
Definition dhcpd.h:83
uint16_t bonusLength
Length of any additional options, pre written.
Definition dhcpd.h:84
IPADDR4 netmask
Netmask for the lease offered.
Definition dhcpd.h:73
IPADDR4 gateway
gateway to be offered
Definition dhcpd.h:74
char * hostname
Hostname.
Definition dhcpd.h:81
char * tftp_name
tftp server name/dotted ip, null terminated
Definition dhcpd.h:82
IPADDR4 smtpServ
SMTP Server.
Definition dhcpd.h:78
const char * domain_name
Domain name for hosts.
Definition dhcpd.h:80
Lease Data.
Definition dhcpd.h:63
char hostname[DHCP_SERV_MAX_HOSTNAME_LEN+1]
buffer for client hostname
Definition dhcpd.h:67
uint32_t expiration
The tick that the lease expires on.
Definition dhcpd.h:66
MACADR mac
source mac address of the request
Definition dhcpd.h:65
IPADDR4 ip
the IP Address to be offered
Definition dhcpd.h:64
Lease Request.
Definition dhcpd.h:53
uint32_t xid
transaction id of the DHCP message
Definition dhcpd.h:57
uint32_t duration
The duration of the lease, in seconds.
Definition dhcpd.h:56
MACADR mac
source mac address of the request
Definition dhcpd.h:55
IPADDR4 ip
the IP Address to be offered
Definition dhcpd.h:54
char hostname[DHCP_SERV_MAX_HOSTNAME_LEN+1]
buffer for client hostname
Definition dhcpd.h:58
Main buffer structure for network and serial communication.
Definition buffers.h:90