NetBurner 3.5.0
PDF Version
 
dhcpd.h
Go to the documentation of this file.
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
53{
56 uint32_t duration;
57 uint32_t xid;
58 char hostname[DHCP_SERV_MAX_HOSTNAME_LEN + 1];
59};
60
63{
66 uint32_t expiration;
67 char hostname[DHCP_SERV_MAX_HOSTNAME_LEN + 1];
68};
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
123 protected:
124 uint32_t m_leaseDuration; //< @brief Lease duration to hand out, in seconds
125 DhcpInfo m_configInfo;
126
127 public:
128 SingleAllocator(IPADDR4 ip) : LeaseAllocator(), m_theIP(ip), m_leaseDuration(3600) {}
130
131 inline void ChangeAllocAddr(IPADDR4 ip) { m_theIP = ip; }
132
133 inline virtual bool OfferLease(DhcpLeaseRequest *pLease, int intfNum)
134 {
135 pLease->ip = m_theIP;
136 pLease->duration = m_leaseDuration;
137 return true;
138 }
139 inline virtual bool RequestLease(DhcpLeaseRequest *pLease, int intfNum)
140 {
141 if (pLease->ip != m_theIP)
142 {
143 pLease->ip = 0x00000000;
144 return false;
145 }
146 pLease->ip = m_theIP;
147 pLease->duration = m_leaseDuration;
148 return true;
149 }
150 inline virtual bool ReleaseLease(DhcpLeaseRequest *pLease, int intfNum) { return pLease->ip == m_theIP; }
151 inline virtual bool LeaseValid(DhcpLeaseRequest *pLease, int intfNum)
152 {
153 pLease->duration = m_leaseDuration;
154 return pLease->ip == m_theIP;
155 }
156 virtual bool SetStaticLease(DhcpLeaseRequest *pLease) { return false; }
157 virtual uint32_t GetLeaseTime() { return m_leaseDuration; }
158
159 void SetLeaseTime(uint32_t hours, uint32_t minutes = 0, uint32_t seconds = 0);
160 virtual bool GetDhcpInfo(DhcpInfo &infoBlock, MACADR &client_mac, int intfNum);
161 virtual void UpdateDhcpInfo(const DhcpInfo *infoBlock) { return; }
162 virtual bool AddInterface(int intfNum) { return true; }
163 virtual void RemoveInterface(int intfNum) { return; }
164 virtual bool IsRegisteredInterface(int intfNum) { return true; }
165 virtual bool GetLeaseData(DhcpLeaseData *data) { return false; }
166};
167
170{
171 IPADDR4 m_startIP;
172 const int m_leaseCount;
173 DhcpLeaseData *const m_leaseBlock;
174 bool m_staticLeaseExist;
175 uint32_t m_lastIndex;
176 int m_validIntf[DHCP_SERV_MAX_INTF];
177
178 bool IsValidIntf(int intfNum);
179 uint32_t m_leaseDuration;
180 DhcpInfo m_configInfo;
181
182 public:
183 BlockAllocator(const IPADDR4 startIP, const int leaseCount, DhcpLeaseData *const leaseBlock);
185 void SetLeaseTime(uint32_t hours, uint32_t minutes = 0, uint32_t seconds = 0);
186
187 virtual uint32_t GetLeaseTime() { return m_leaseDuration; }
188 virtual bool OfferLease(DhcpLeaseRequest *pLease, int intfNum);
189 virtual bool RequestLease(DhcpLeaseRequest *pLease, int intfNum);
190 virtual bool ReleaseLease(DhcpLeaseRequest *pLease, int intfNum);
191 virtual bool LeaseValid(DhcpLeaseRequest *pLease, int intfNum);
192 virtual bool SetStaticLease(DhcpLeaseRequest *pLease);
193 virtual bool GetDhcpInfo(DhcpInfo &infoBlock, MACADR &client_mac, int intfNum);
194 virtual void UpdateDhcpInfo(const DhcpInfo *infoBlock);
195 virtual bool AddInterface(int intfNum);
196 virtual void RemoveInterface(int intfNum);
197 inline virtual bool IsRegisteredInterface(int intfNum)
198 {
199 for (int i = 0; i < DHCP_SERV_MAX_INTF; i++)
200 {
201 if (m_validIntf[i] == intfNum) { return true; }
202 }
203 return false;
204 }
205 virtual bool GetLeaseData(DhcpLeaseData *data);
206
207 void ResetLeases();
208 inline void SetStartIP(const IPADDR4 newStartIP)
209 {
210 m_startIP = newStartIP;
211 ResetLeases();
212 }
213};
214
220{
221 const MACADR m_macMask; // the bitmask to filter against
222 const MACADR m_macPrefix; // the MAC prefix to scan against
223 const bool m_whitelist; // are we operating in Whitelist mode?
224
225 public:
226 MacPrefixAllocator(const MACADR prefix,
227 const MACADR mask,
228 const bool whitelist,
229 const IPADDR4 startIP,
230 const int leaseCount,
231 DhcpLeaseData *const leaseBlock);
233 virtual bool OfferLease(DhcpLeaseRequest *pLease, int intfNum);
234 virtual bool RequestLease(DhcpLeaseRequest *pLease, int intfNum);
235 virtual bool SetStaticLease(DhcpLeaseRequest *pLease);
236};
237
242{
243 static Server *theInstance;
244 LeaseAllocator *m_pLeaseAlloc;
245 uint32_t m_nextTick;
246
247 void ProcessDiscover(PoolPtr pp);
248 void ProcessRequest(PoolPtr pp);
249 void ProcessDecline(PoolPtr pp);
250 void ProcessRelease(PoolPtr pp);
251 void ProcessInform(PoolPtr pp);
252
253 void ProcessParamReq(uint8_t *&optBuf, uint8_t *ReqList, uint8_t reqLen, const DhcpInfo &info, IPADDR4 intfIP);
254
255 public:
256 Server();
257 ~Server();
258 bool AddLeaseAllocator(LeaseAllocator *newAllocator);
259 bool ProcessServerMessage(PoolPtr pp);
260
261 bool GetDhcpClients(DhcpLeaseData *data);
262
263 bool AddInterface(int intfNum);
264 void RemoveInterface(int intfNum);
265
266 static inline bool AddServerInterface(int intfNum)
267 {
268 if (theInstance) { return theInstance->AddInterface(intfNum); }
269 return false;
270 }
271 static inline void RemoveServerInterface(int intfNum)
272 {
273 if (theInstance) { theInstance->RemoveInterface(intfNum); }
274 }
275 static inline void ProcessMessage(PoolPtr pp)
276 {
277 if (theInstance) { theInstance->ProcessServerMessage(pp); }
278 }
279 static inline Server *GetInstance() { return theInstance; }
280};
281} // namespace DHCP
282
297bool AddStandardDHCPServer(int intf = 0, IPADDR4 startAddr = IPADDR4::NullIP());
298
299#endif /* ----- #ifndef __DHCPD_H ----- */
300
NetBurner Buffers API.
Basic allocator that handles multiple leases in a contiguous IP block.
Definition dhcpd.h:170
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:220
DHCP Server class Requires a lease allocator to be added in order to function.
Definition dhcpd.h:242
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:208
static IPADDR4 NullIP()
C++ static function for a null IP address.
Definition nettypes.h:346
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.
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
NetBurner System Constants.
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.
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