NetBurner 3.1
dhcpd.h
Go to the documentation of this file.
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
19 #ifndef __DHCPD_H
20 #define __DHCPD_H
21 
22 // NB Definitions
23 #include <predef.h>
24 
25 // NB Constants
26 #include <constants.h>
27 
28 // NB Libs
29 #include <buffers.h>
30 #include <nettypes.h>
31 
32 #define DHCP_SERV_MAX_INTF (4)
33 #define DHCP_OFFER_DURATION (2 * TICKS_PER_SECOND)
34 #define DHCP_SERV_MAX_HOSTNAME_LEN (32)
35 
36 #define LEASE_POOL_SIZE 150
37 #define DHCPD_STARTING_ADDRESS 0xC0A80184
38 
39 namespace DHCP
40 {
41 typedef enum LeaseState
42 {
43  LEASE_OPEN = 0x0,
44  LEASE_OFFERED = 0x1,
45  LEASE_TAKEN = 0x2,
46  LEASE_STATIC = 0x3,
47  ARP_CONFLICT = 0x4,
48 } LeaseState_t;
49 
50 struct DhcpLeaseRequest
51 {
52  IPADDR4 ip; // the IP Address to be offered
53  MACADR mac; // source mac address of the request
54  uint32_t duration; // The duration of the lease, in seconds
55  uint32_t xid; // transaction id of the DHCP message
56  char hostname[DHCP_SERV_MAX_HOSTNAME_LEN + 1]; // buffer for client hostname
57 };
58 
59 struct DhcpLeaseData
60 {
61  IPADDR4 ip; // the IP Address to be offered
62  MACADR mac; // source mac address of the request
63  uint32_t expiration; // The tick that the lease expires on
64  char hostname[DHCP_SERV_MAX_HOSTNAME_LEN + 1]; // buffer for client hostname
65 };
66 
67 struct DhcpInfo
68 {
69  IPADDR4 netmask; // Netmask for the lease offered
70  IPADDR4 gateway; // gateway to be offered
71  IPADDR4 dns_1; // primary DNS Server
72  IPADDR4 dns_2; // secondary DNS Server
73  IPADDR4 logServ; // Syslog Server
74  IPADDR4 smtpServ; // SMTP Server
75  IPADDR4 ntpServ; // NTP Server
76  const char *domain_name; // Domain name for hosts
77  char *hostname; // Hostname
78  char *tftp_name; // tftp server name/dotted ip, null terminated
79  char *bootfile; // tftp boot file, null terminated
80  uint16_t bonusLength; // Length of any additional options, pre written
81  uint8_t *bonusOpts; // additional options, pre written
82 };
83 
84 // Lease Allocator is the base class/interface for lease allocators for the DHCP server
85 class LeaseAllocator
86 {
87  LeaseAllocator *m_pNext;
88 
89  public:
90  LeaseAllocator();
91  ~LeaseAllocator();
92 
93  inline LeaseAllocator *SetNextAllocator(LeaseAllocator *nextAlloc)
94  {
95  m_pNext = nextAlloc;
96  return m_pNext;
97  }
98  inline LeaseAllocator *GetNextAllocator() { return m_pNext; }
99 
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;
109 };
110 
111 // SingleAllocator, an incredibly stripped down allocator that fails to conform to many
112 // standard behaviors, but shows the basics of what needs to be implemented
113 class SingleAllocator : public LeaseAllocator
114 {
115  IPADDR4 m_theIP;
116 
117  protected:
118  uint32_t m_leaseDuration; // Lease duration to hand out, in seconds
119  DhcpInfo m_configInfo;
120 
121  public:
122  SingleAllocator(IPADDR4 ip) : LeaseAllocator(), m_theIP(ip), m_leaseDuration(3600) {}
123  ~SingleAllocator();
124 
125  inline virtual bool OfferLease(DhcpLeaseRequest *pLease, int intfNum)
126  {
127  pLease->ip = m_theIP;
128  pLease->duration = m_leaseDuration;
129  return true;
130  }
131  inline virtual bool RequestLease(DhcpLeaseRequest *pLease, int intfNum)
132  {
133  if (pLease->ip != m_theIP)
134  {
135  pLease->ip = 0x00000000;
136  return false;
137  }
138  pLease->ip = m_theIP;
139  pLease->duration = m_leaseDuration;
140  return true;
141  }
142  inline virtual bool ReleaseLease(DhcpLeaseRequest *pLease, int intfNum) { return pLease->ip == m_theIP; }
143  inline virtual bool LeaseValid(DhcpLeaseRequest *pLease, int intfNum)
144  {
145  pLease->duration = m_leaseDuration;
146  return pLease->ip == m_theIP;
147  }
148  virtual bool SetStaticLease(DhcpLeaseRequest *pLease) { return false; }
149  virtual uint32_t GetLeaseTime() { return m_leaseDuration; }
150 
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; }
158 };
159 
160 // BlockAllocator, A basic allocator that handles multiple leases in a contiguous IP block
161 class BlockAllocator : public LeaseAllocator
162 {
163  IPADDR4 m_startIP;
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];
169 
170  bool IsValidIntf(int intfNum);
171  uint32_t m_leaseDuration; // Lease duration to hand out, in seconds
172  DhcpInfo m_configInfo;
173 
174  public:
175  BlockAllocator(const IPADDR4 startIP, const int leaseCount, DhcpLeaseData *const leaseBlock);
176  ~BlockAllocator();
177  void SetLeaseTime(uint32_t hours, uint32_t minutes = 0, uint32_t seconds = 0);
178 
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)
190  {
191  for (int i = 0; i < DHCP_SERV_MAX_INTF; i++)
192  {
193  if (m_validIntf[i] == intfNum) { return true; }
194  }
195  return false;
196  }
197  virtual bool GetLeaseData(DhcpLeaseData *data);
198 
199  void ResetLeases();
200  inline void SetStartIP(const IPADDR4 newStartIP)
201  {
202  m_startIP = newStartIP;
203  ResetLeases();
204  }
205 };
206 
207 // MacPrefixAllocator is derived from BlockAllocator, with the ability to whitelist/blacklist
208 // certain mac address ranges based on a mask. Useful for allocating addresses based on
209 // device manufacturer
210 class MacPrefixAllocator : public BlockAllocator
211 {
212  const MACADR m_macMask; // the bitmask to filter against
213  const MACADR m_macPrefix; // the MAC prefix to scan against
214  const bool m_whitelist; // are we operating in Whitelist mode?
215 
216  public:
217  MacPrefixAllocator(const MACADR prefix,
218  const MACADR mask,
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);
227 };
228 
229 // DHCP Server class
230 // It requires a lease allocator to be added in order to function.
231 class Server
232 {
233  static Server *theInstance;
234  LeaseAllocator *m_pLeaseAlloc;
235  uint32_t m_nextTick;
236 
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);
242 
243  void ProcessParamReq(uint8_t *&optBuf, uint8_t *ReqList, uint8_t reqLen, const DhcpInfo &info, IPADDR4 intfIP);
244 
245  public:
246  Server();
247  ~Server();
248  bool AddLeaseAllocator(LeaseAllocator *newAllocator);
249  bool ProcessServerMessage(PoolPtr pp);
250 
251  bool GetDhcpClients(DhcpLeaseData *data);
252 
253  bool AddInterface(int intfNum);
254  void RemoveInterface(int intfNum);
255 
256  static inline bool AddServerInterface(int intfNum)
257  {
258  if (theInstance) { return theInstance->AddInterface(intfNum); }
259  return false;
260  }
261  static inline void RemoveServerInterface(int intfNum)
262  {
263  if (theInstance) { theInstance->RemoveInterface(intfNum); }
264  }
265  static inline void ProcessMessage(PoolPtr pp)
266  {
267  if (theInstance) { theInstance->ProcessServerMessage(pp); }
268  }
269  static inline Server *GetInstance() { return theInstance; }
270 };
271 } // namespace DHCP
272 
287 bool AddStandardDHCPServer(int intf = 0, IPADDR4 startAddr = IPADDR4::NullIP());
288 
289 #endif /* ----- #ifndef __DHCPD_H ----- */
bool AddStandardDHCPServer(int intf=0, IPADDR4 startAddr=IPADDR4::NullIP())
Starts a standard allocator DHCP server.
Definition: dhcpd_standard.cpp:34
Definition: dhcpd.h:39
NetBurner Buffers API.