-
Notifications
You must be signed in to change notification settings - Fork 1
/
dhcpserver.h
131 lines (108 loc) · 3.82 KB
/
dhcpserver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef DHCPSERVER_H
#define DHCPSERVER_H
#include <QHostAddress>
#include <QUdpSocket>
#include <QNetworkAddressEntry>
class DHCPServer : public QObject
{
Q_OBJECT
public:
enum class OpValue
{
NONE = 0,
BOOTREQUEST = 1,
BOOTREPLY = 2
};
enum class DHCPOption
{
NONE = 0,
DHCPRequestAddress = 50,
DHCPLeaseTime = 51,
DHCPMessageType = 53,
DHCPServerIdentifier = 54,
END = 255
};
enum class DHCPMessageType
{
NONE = 0,
DISCOVER = 1,
OFFER = 2,
REQUEST = 3,
DECLINE = 4,
ACK = 5,
NAK = 6,
RELEASE = 7,
INFORM = 8
};
typedef QPair<DHCPOption, QByteArray> Option;
typedef QPair<QByteArray, QHostAddress> DHCPTableEntry;
private:
struct DHCPMessage
{
quint8 op = 0;
quint8 htype = 0;
quint8 hlen = 0;
quint8 hops = 0;
QByteArray xid;
quint16 secs = 0;
QByteArray flags;
quint32 ciaddr = 0;
quint32 yiaddr = 0;
quint32 siaddr = 0;
quint32 giaddr = 0;
QByteArray chaddr;
QByteArray sname;
QByteArray file;
QVector<Option> options;
};
const QByteArray magicCookies = QByteArray::fromHex("63825363");
const quint16 dhcpServerPort = 67;
const quint16 dhcpClientPort = 68;
QUdpSocket m_socket;
const QHostAddress m_startAddress;
const QHostAddress m_stopAddress;
const QByteArray m_serverName = "DHCP Server";
const int m_leaseTime = 3600;
const QNetworkAddressEntry m_address;
quint32 mask() const;
quint32 revMask() const;
QVector<DHCPTableEntry> m_DHCPTable;
QVector<DHCPTableEntry> m_offeredAddressesTable;
void parseDatagram(const QByteArray& datagram);
void handleDhcpDiscover(const DHCPMessage& request);
void handleDhcpRequest(const DHCPMessage& request);
void handleDhcpDecline(const DHCPMessage& request);
void handleDhcpRelease(const DHCPMessage& request);
void handleDhcpInform(const DHCPMessage& request);
void makePacket(const DHCPMessage& response, QByteArray& result);
QHostAddress getAddress(quint32 requested = 0, const QByteArray& chaddr = QByteArray(), bool checkOffered = false);
template<typename T1, typename T2>
QByteArray getBytes(T1 t1, T2);
public:
explicit DHCPServer(QObject *parent = nullptr);
explicit DHCPServer(const QNetworkAddressEntry &address, QObject *parent = nullptr);
explicit DHCPServer(const QHostAddress& startAddress = QHostAddress(),
const QHostAddress& stopAddress = QHostAddress(),
const QByteArray& serverName = "DHCP Server",
int leaseTime = 3600, QObject* parent = nullptr);
explicit DHCPServer(const QNetworkAddressEntry& address, const QHostAddress& startAddress = QHostAddress(),
const QHostAddress& stopAddress = QHostAddress(),
const QByteArray& serverName = "DHCP Server",
int leaseTime = 3600, QObject* parent = nullptr);
virtual ~DHCPServer() override;
static QNetworkAddressEntry getAddressEntry();
bool initializeDHCPServer();
QVector<DHCPTableEntry> getDhcpTable();
bool isRunning();
void stopDHCPServer();
QString getStartAddressStr();
QString getStopAddressStr();
signals:
void serverStarted(const QHostAddress& address, const QHostAddress& mask, int prefixLength);
void serverStopped();
void dhcpDatagramReceived(const QByteArray& datagram);
void dhcpMessageReveived(DHCPMessageType type, const QByteArray& from);
void dhcpAddressOffered(const QByteArray& chaddr, const QHostAddress& addr);
void dhcpAddressAssigned(const QByteArray& chaddr, const QHostAddress& addr);
};
#endif // DHCPSERVER_H