-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdhcp_message.cpp
More file actions
272 lines (239 loc) · 6.88 KB
/
Copy pathdhcp_message.cpp
File metadata and controls
272 lines (239 loc) · 6.88 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "dhcp_message.h"
#include "bootp.h"
#include <QString>
#include <QDataStream>
dhcp_message_t::dhcp_message_t( QByteArray message_data )
{
auto preamble = message_data.left( 240 );
message_data.remove( 0, 240 );
bool good = header.deserialize( preamble );
if( !good )
{
qDebug() << "couldn't deserialize header";
throw std::invalid_argument( "something" );
}
parseOptions( message_data );
}
void dhcp_message_t::parseOptions( QByteArray data )
{
qDebug() << "***********************************";
qDebug() << "[PARSING OPTIONS]";
while( data.size() )
{
auto option_type = getOptionType( data.at( 0 ) );
if( option_type == DhcpOption::UNKNOWN )
{
qDebug() << "uh oh";
}
qDebug() << "--->[" << DhcpOptionToString( data.at( 0 ) ) << "]";
data.remove( 0, 1 );
if( option_type == DhcpOption::PAD )
{
continue;
}
else if( option_type == DhcpOption::END )
{
qDebug() << "padding=" << data.size();
data.clear();
}
else if( option_type == DhcpOption::DHCP_MAX_SIZE )
{
quint8 option_length = data.at( 0 );
data.remove( 0, 1 );
data.remove( 0, option_length );
}
else if( option_type == DhcpOption::PARAMETER_REQUEST_LIST )
{
quint8 option_length = data.at( 0 );
data.remove( 0, 1 );
qDebug() << "requesting " << option_length << " items";
for( int i = 0; i < option_length; i++ )
{
quint8 requested_parameter = data.at( 0 );
data.remove( 0, 1 );
qDebug() << "requested: " << DhcpOptionToString(requested_parameter);
}
}
else if( option_type == DhcpOption::REQUESTED_IP )
{
quint8 option_length = data.at( 0 );
data.remove( 0, 1 );
if( option_length != 4 )
{
qDebug() << "some problem with length: " << option_length;
}
quint8 byte_1 = data.at( 0 );
quint8 byte_2 = data.at( 1 );
quint8 byte_3 = data.at( 2 );
quint8 byte_4 = data.at( 3 );
auto ip =
QString( "%0.%1.%2.%3" )
.arg( byte_1 )
.arg( byte_2 )
.arg( byte_3 )
.arg( byte_4 )
;
QHostAddress address( ip );
qDebug() << "requesting address " << address.toString();
data.remove( 0, 4 );
}
else if( option_type == DhcpOption::DHCP_MESSAGE_TYPE )
{
quint8 option_length = data.at( 0 );
data.remove( 0, 1 );
if( option_length != 1 )
{
qDebug() << "invalid length for opt";
}
quint8 type = data.at( 0 );
request_type = static_cast<DhcpRequestType>( type );
qDebug() << "type: " << ::toString( request_type );
data.remove( 0, option_length );
}
else if( option_type == DhcpOption::HOSTNAME )
{
quint8 option_length = data.at( 0 );
data.remove( 0, 1 );
m_client_hostname = data.left( option_length );
qDebug() << "hostname " << m_client_hostname;
data.remove( 0, option_length );
}
else if( option_type == DhcpOption::CLIENT_ID )
{
quint8 option_length = data.at( 0 );
data.remove( 0, 1 );
quint8 type = data.at( 0 );
data.remove( 0, 1 );
option_length--;
QByteArray client_id = data.left( option_length );
data.remove( 0, option_length );
if( static_cast<BootpHwType>(type) == BootpHwType::ETHERNET )
{
m_client_id = mac_address_t( client_id );
qDebug() << "Client ID: " << m_client_id.toString();
}
else
{
qDebug() << "Client ID type unknown: " << type;
}
}
else
{
quint8 option_length = data.at( 0 );
data.remove( 0, 1 );
qDebug() << "Removing " << int(option_length);
data.remove( 0, option_length );
}
}
qDebug() << "***********************************";
}
void dhcp_message_t::SetRequestType(DhcpRequestType type)
{
request_type = type;
QByteArray array;
array.append( static_cast<quint8>(type) );
SetOption( DhcpOption::DHCP_MESSAGE_TYPE, array );
}
void dhcp_message_t::SetClientMAC( mac_address_t client_mac )
{
header.hardware_address_client = client_mac;
header.hardware_address_length = 6;
header.hardware_address_type = BootpHwType::ETHERNET;
}
void dhcp_message_t::SetOption( DhcpOption option, QByteArray options_data )
{
if( option == DhcpOption::END ) return; // end option needs to at the end of every message... we'll do it manually
m_options[option] = options_data;
}
void dhcp_message_t::SetRouter( QHostAddress address )
{
QByteArray data;
{
QDataStream stream( &data, QIODevice::WriteOnly );
stream << address.toIPv4Address();
}
SetOption( DhcpOption::ROUTER, data );
}
void dhcp_message_t::SetServerIdentifier( QHostAddress address )
{
QByteArray data;
{
QDataStream stream( &data, QIODevice::WriteOnly );
stream << address.toIPv4Address();
}
SetOption( DhcpOption::DHCP_SERVER_IDENTIFIER, data );
}
void dhcp_message_t::SetLeaseTime( int seconds )
{
QByteArray lease_time;
{
QDataStream stream( &lease_time, QIODevice::WriteOnly );
stream << quint32( seconds );
}
SetOption( DhcpOption::IP_LEASE_TIME, lease_time );
}
void dhcp_message_t::SetSubnet( QHostAddress address )
{
QByteArray data;
{
QDataStream stream( &data, QIODevice::WriteOnly );
stream << address.toIPv4Address();
}
SetOption( DhcpOption::SUBNET_MASK, data );
}
void dhcp_message_t::SetDns( QHostAddress address )
{
QByteArray data;
{
QDataStream stream( &data, QIODevice::WriteOnly );
stream << address.toIPv4Address();
}
SetOption( DhcpOption::DNS, data );
}
QString dhcp_message_t::toString() const
{
return
QString( "[%1] [%2] [%3]" )
.arg( ::toString(request_type) )
.arg( m_client_hostname )
.arg( header.hardware_address_client.toString() )
;
}
QByteArray dhcp_message_t::serialize() const
{
QByteArray array;
array.append( header.serialize() );
auto i = m_options.constBegin();
while( i != m_options.constEnd() )
{
array.append( static_cast<quint8>( i.key() ) );
array.append( (quint8)i.value().length() );
array.append( i.value() );
++i;
}
array.append( static_cast<quint8>( DhcpOption::END ) ); // always the last option
return array;
}
dhcp_message_t dhcp_message_t::CreateOffer( QHostAddress offered_address )
{
dhcp_message_t dhcp;
dhcp.header.operation = BootpOpType::BOOT_REPLY;
dhcp.SetRequestType( DhcpRequestType::DHCPOFFER );
dhcp.m_address_yours = offered_address;
return dhcp;
}
dhcp_message_t dhcp_message_t::CreateACK( uint32_t transaction_id, QHostAddress client_addresss, mac_address_t client_mac )
{
dhcp_message_t dhcp;
dhcp.SetRequestType( DhcpRequestType::DHCPACK );
dhcp.header.operation = BootpOpType::BOOT_REPLY;
dhcp.m_address_yours = client_addresss;
dhcp.SetClientMAC( client_mac );
return dhcp;
}
dhcp_message_t dhcp_message_t::CreateNAK()
{
dhcp_message_t dhcp;
dhcp.SetRequestType( DhcpRequestType::DHCPNAK );
return dhcp;
}