NetBurner 3.5.8
PDF Version
Toggle main menu visibility
dhcpd.h
1
/*NB_REVISION*/
2
3
/*NB_COPYRIGHT*/
4
12
20
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
38
namespace
DHCP
39
{
40
42
typedef
enum
LeaseState
43
{
44
LEASE_OPEN
= 0x0,
45
LEASE_OFFERED
= 0x1,
46
LEASE_TAKEN
= 0x2,
47
LEASE_STATIC
= 0x3,
48
ARP_CONFLICT
= 0x4,
49
} LeaseState_t;
50
52
struct
DhcpLeaseRequest
53
{
54
IPADDR4
ip
;
55
MACADR
mac
;
56
uint32_t
duration
;
57
uint32_t
xid
;
58
char
hostname
[
DHCP_SERV_MAX_HOSTNAME_LEN
+ 1];
59
};
60
62
struct
DhcpLeaseData
63
{
64
IPADDR4
ip
;
65
MACADR
mac
;
66
uint32_t
expiration
;
67
char
hostname
[
DHCP_SERV_MAX_HOSTNAME_LEN
+ 1];
68
};
69
71
struct
DhcpInfo
72
{
73
IPADDR4
netmask
;
74
IPADDR4
gateway
;
75
IPADDR4
dns_1
;
76
IPADDR4
dns_2
;
77
IPADDR4
logServ
;
78
IPADDR4
smtpServ
;
79
IPADDR4
ntpServ
;
80
const
char
*
domain_name
;
81
char
*
hostname
;
82
char
*
tftp_name
;
83
char
*
bootfile
;
84
uint16_t
bonusLength
;
85
uint8_t *
bonusOpts
;
86
};
87
89
class
LeaseAllocator
90
{
91
LeaseAllocator *m_pNext;
92
93
public
:
94
LeaseAllocator();
95
~LeaseAllocator();
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
119
class
SingleAllocator :
public
LeaseAllocator
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) {}
130
~SingleAllocator();
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
189
class
BlockAllocator :
public
LeaseAllocator
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);
204
~BlockAllocator();
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
239
class
MacPrefixAllocator :
public
BlockAllocator
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);
252
~MacPrefixAllocator();
253
virtual
bool
OfferLease(
DhcpLeaseRequest
*pLease,
int
intfNum);
254
virtual
bool
RequestLease(
DhcpLeaseRequest
*pLease,
int
intfNum);
255
virtual
bool
SetStaticLease(
DhcpLeaseRequest
*pLease);
256
};
257
261
class
Server
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
317
bool
AddStandardDHCPServer
(
int
intf = 0,
IPADDR4
startAddr =
IPADDR4::NullIP
());
318
319
#endif
/* ----- #ifndef __DHCPD_H ----- */
320
DHCP::LeaseAllocator
Base class/interface for lease allocators for the DHCP server.
Definition
dhcpd.h:90
IPADDR4
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition
nettypes.h:225
IPADDR4::NullIP
static IPADDR4 NullIP()
C++ static function for a null IP address.
Definition
nettypes.h:363
MACADR
Used to store and manipulate MAC addresses.
Definition
nettypes.h:69
AddStandardDHCPServer
bool AddStandardDHCPServer(int intf=0, IPADDR4 startAddr=IPADDR4::NullIP())
Starts a standard allocator DHCP server.
DHCP_SERV_MAX_HOSTNAME_LEN
#define DHCP_SERV_MAX_HOSTNAME_LEN
Definition
dhcpd.h:31
DHCP_SERV_MAX_INTF
#define DHCP_SERV_MAX_INTF
Definition
dhcpd.h:29
DHCP
DHCP Namespace.
Definition
dhcpd.h:39
DHCP::LeaseState
LeaseState
Lease State.
Definition
dhcpd.h:43
DHCP::LEASE_OPEN
@ LEASE_OPEN
Definition
dhcpd.h:44
DHCP::LEASE_TAKEN
@ LEASE_TAKEN
Definition
dhcpd.h:46
DHCP::LEASE_STATIC
@ LEASE_STATIC
Definition
dhcpd.h:47
DHCP::LEASE_OFFERED
@ LEASE_OFFERED
Definition
dhcpd.h:45
DHCP::ARP_CONFLICT
@ ARP_CONFLICT
Definition
dhcpd.h:48
DHCP::DhcpInfo
DHCP Info.
Definition
dhcpd.h:72
DHCP::DhcpInfo::dns_1
IPADDR4 dns_1
primary DNS Server
Definition
dhcpd.h:75
DHCP::DhcpInfo::dns_2
IPADDR4 dns_2
secondary DNS Server
Definition
dhcpd.h:76
DHCP::DhcpInfo::bonusOpts
uint8_t * bonusOpts
additional options, pre written
Definition
dhcpd.h:85
DHCP::DhcpInfo::logServ
IPADDR4 logServ
Syslog Server.
Definition
dhcpd.h:77
DHCP::DhcpInfo::ntpServ
IPADDR4 ntpServ
NTP Server.
Definition
dhcpd.h:79
DHCP::DhcpInfo::bootfile
char * bootfile
tftp boot file, null terminated
Definition
dhcpd.h:83
DHCP::DhcpInfo::bonusLength
uint16_t bonusLength
Length of any additional options, pre written.
Definition
dhcpd.h:84
DHCP::DhcpInfo::netmask
IPADDR4 netmask
Netmask for the lease offered.
Definition
dhcpd.h:73
DHCP::DhcpInfo::gateway
IPADDR4 gateway
gateway to be offered
Definition
dhcpd.h:74
DHCP::DhcpInfo::hostname
char * hostname
Hostname.
Definition
dhcpd.h:81
DHCP::DhcpInfo::tftp_name
char * tftp_name
tftp server name/dotted ip, null terminated
Definition
dhcpd.h:82
DHCP::DhcpInfo::smtpServ
IPADDR4 smtpServ
SMTP Server.
Definition
dhcpd.h:78
DHCP::DhcpInfo::domain_name
const char * domain_name
Domain name for hosts.
Definition
dhcpd.h:80
DHCP::DhcpLeaseData
Lease Data.
Definition
dhcpd.h:63
DHCP::DhcpLeaseData::hostname
char hostname[DHCP_SERV_MAX_HOSTNAME_LEN+1]
buffer for client hostname
Definition
dhcpd.h:67
DHCP::DhcpLeaseData::expiration
uint32_t expiration
The tick that the lease expires on.
Definition
dhcpd.h:66
DHCP::DhcpLeaseData::mac
MACADR mac
source mac address of the request
Definition
dhcpd.h:65
DHCP::DhcpLeaseData::ip
IPADDR4 ip
the IP Address to be offered
Definition
dhcpd.h:64
DHCP::DhcpLeaseRequest
Lease Request.
Definition
dhcpd.h:53
DHCP::DhcpLeaseRequest::xid
uint32_t xid
transaction id of the DHCP message
Definition
dhcpd.h:57
DHCP::DhcpLeaseRequest::duration
uint32_t duration
The duration of the lease, in seconds.
Definition
dhcpd.h:56
DHCP::DhcpLeaseRequest::mac
MACADR mac
source mac address of the request
Definition
dhcpd.h:55
DHCP::DhcpLeaseRequest::ip
IPADDR4 ip
the IP Address to be offered
Definition
dhcpd.h:54
DHCP::DhcpLeaseRequest::hostname
char hostname[DHCP_SERV_MAX_HOSTNAME_LEN+1]
buffer for client hostname
Definition
dhcpd.h:58
nbrtos
include
dhcpd.h
Generated by
1.18.0