forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDhcpMessage.cs
453 lines (400 loc) · 15.4 KB
/
DhcpMessage.cs
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.Net;
using System.Text;
using Iot.Device.DhcpServer.Enums;
namespace Iot.Device.DhcpServer
{
/// <summary>
/// DHCP Message class.
/// </summary>
public class DhcpMessage
{
private const int DhcppacketSize = 300;
private const int IndexToOptions = 240;
/// <summary>
/// Gets or sets the operation Code.
/// </summary>
public DhcpOperation OperationCode { get; set; }
/// <summary>
/// Gets or setsthe hardware type.
/// </summary>
public HardwareType HardwareType { get; set; }
/// <summary>
/// Gets or sets the hardware address lenght.
/// </summary>
public byte HardwareAddressLength { get; set; }
/// <summary>
/// Gets or sets the hops.
/// </summary>
public byte Hops { get; set; }
/// <summary>
/// Gets or sets the transaction ID.
/// </summary>
public uint TransactionId { get; set; }
/// <summary>
/// Gets or sets the seconds elapsed.
/// </summary>
public ushort SecondsElapsed { get; set; }
/// <summary>
/// Gets or sets the Flags.
/// </summary>
public ushort Flags { get; set; }
/// <summary>
/// Gets or sets the client IP addres.
/// </summary>
public IPAddress ClientIPAddress { get; set; } = new IPAddress(0);
/// <summary>
/// Gets or sets your server IP address.
/// </summary>
public IPAddress YourIPAddress { get; set; } = new IPAddress(0);
/// <summary>
/// Gets or sets the server IP address.
/// </summary>
public IPAddress ServerIPAddress { get; set; } = new IPAddress(0);
/// <summary>
/// Gets or sets the gateway IP address.
/// </summary>
public IPAddress GatewayIPAddress { get; set; } = new IPAddress(0);
/// <summary>
/// Gets or sets the client hardware address.
/// </summary>
public byte[] ClientHardwareAddress { get; set; }
/// <summary>
/// Gets or sets the magic cookie.
/// </summary>
public byte[] Cookie { get; set; }
/// <summary>
/// Gets or sets the options.
/// </summary>
public byte[] Options { get; set; }
/// <summary>
/// Gets the message type.
/// </summary>
public DhcpMessageType DhcpMessageType
{
get
{
if (IsOptionsInvalid())
{
return DhcpMessageType.Unknown;
}
if (OptionsContainsKey(DhcpOptionCode.DhcpMessageType))
{
var data = GetOption(DhcpOptionCode.DhcpMessageType)[0];
return (DhcpMessageType)data;
}
return DhcpMessageType.Unknown;
}
}
/// <summary>
/// Gets the host name.
/// </summary>
public string HostName
{
get
{
if (IsOptionsInvalid())
{
return string.Empty;
}
if (OptionsContainsKey(DhcpOptionCode.Hostname))
{
var data = GetOption(DhcpOptionCode.Hostname);
return Encoding.UTF8.GetString(data, 0, data.Length);
}
return string.Empty;
}
}
/// <summary>
/// Gets the request IP aAddress.
/// </summary>
public IPAddress RequestedIpAddress
{
get
{
if (IsOptionsInvalid())
{
return new IPAddress(0);
}
if (OptionsContainsKey(DhcpOptionCode.RequestedIpAddress))
{
var data = GetOption(DhcpOptionCode.RequestedIpAddress);
return new IPAddress(data);
}
return new IPAddress(0);
}
}
/// <summary>
/// Gets the DHCP addres.
/// </summary>
public IPAddress DhcpAddress
{
get
{
if (IsOptionsInvalid())
{
return new IPAddress(0);
}
if (OptionsContainsKey(DhcpOptionCode.DhcpAddress))
{
var data = GetOption(DhcpOptionCode.DhcpAddress);
return new IPAddress(data);
}
return new IPAddress(0);
}
}
/// <summary>
/// Parses the message.
/// </summary>
/// <param name="dhcppacket">The byte array message.</param>
public void Parse(ref byte[] dhcppacket)
{
// See the build function for details on a message.
const int LongSize = 4;
int inc = 0;
OperationCode = (DhcpOperation)dhcppacket[0];
HardwareType = (HardwareType)dhcppacket[1];
HardwareAddressLength = dhcppacket[2];
Hops = dhcppacket[3];
inc += LongSize;
TransactionId = BitConverter.ToUInt32(dhcppacket, inc);
inc += LongSize;
SecondsElapsed = BitConverter.ToUInt16(dhcppacket, inc);
inc += 2;
Flags = BitConverter.ToUInt16(dhcppacket, inc);
inc += 2;
ClientIPAddress = new IPAddress(BitConverter.GetBytes(BitConverter.ToUInt32(dhcppacket, inc)));
inc += LongSize;
YourIPAddress = new IPAddress(BitConverter.GetBytes(BitConverter.ToUInt32(dhcppacket, inc)));
inc += LongSize;
ServerIPAddress = new IPAddress(BitConverter.GetBytes(BitConverter.ToUInt32(dhcppacket, inc)));
inc += LongSize;
GatewayIPAddress = new IPAddress(BitConverter.GetBytes(BitConverter.ToUInt32(dhcppacket, inc)));
inc += LongSize;
ClientHardwareAddress = new byte[HardwareAddressLength];
Array.Copy(dhcppacket, inc, ClientHardwareAddress, 0, HardwareAddressLength);
Cookie = new byte[4];
// We directly go to the magic cookie.
inc = 236;
Array.Copy(dhcppacket, inc, Cookie, 0, 4);
// check copy options array
inc = IndexToOptions;
int offset = inc;
if (dhcppacket[offset] != 0)
{
while (dhcppacket[offset] != 0xff)
{
byte optcode = dhcppacket[offset++];
int optlen = dhcppacket[offset++];
offset += optlen;
}
Options = new byte[offset - inc + 1];
Array.Copy(dhcppacket, inc, Options, 0, Options.Length);
}
}
/// <summary>
/// Build a message.
/// </summary>
/// <returns>The message as a byte array.</returns>
public byte[] Build()
{
// Example of a discovery message
// byte 0 byte 1 byte 2 byte 3
// OP HTYPE HLEN HOPS
// 0x01 0x01 0x06 0x00
// XID
// 0x3903F326
// SECS FLAGS
// 0x0000 0x0000
// CIADDR(Client IP address)
// 0x00000000
// YIADDR(Your IP address)
// 0x00000000
// SIADDR(Server IP address)
// 0x00000000
// GIADDR(Gateway IP address)
// 0x00000000
// CHADDR(Client hardware address)
// 0x00053C04
// 0x8D590000
// 0x00000000
// 0x00000000
// 192 octets of 0s, or overflow space for additional options; BOOTP legacy.
// Magic cookie
// 0x63825363
// DHCP options
// 0x350101 53: 1(DHCP Discover)
// 0x3204c0a80164 50: 192.168.1.100 requested
// 0x370401030f06 55(Parameter Request List):
// - 1 (Request Subnet Mask),
// - 3 (Router),
// - 15 (Domain Name),
// - 6 (Domain Name Server)
// 0xff 255(Endmark)
const int LongSize = 4;
int inc = 0;
byte[] dhcpPacket = new byte[DhcppacketSize];
dhcpPacket[0] = (byte)OperationCode;
dhcpPacket[1] = (byte)HardwareType;
dhcpPacket[2] = HardwareAddressLength;
dhcpPacket[3] = Hops;
inc += LongSize;
BitConverter.GetBytes(TransactionId).CopyTo(dhcpPacket, inc);
inc += LongSize;
BitConverter.GetBytes(SecondsElapsed).CopyTo(dhcpPacket, inc);
// Only 2 bytes for the previous one
inc += 2;
BitConverter.GetBytes(Flags).CopyTo(dhcpPacket, inc);
inc += 2;
ClientIPAddress.GetAddressBytes().CopyTo(dhcpPacket, inc);
inc += LongSize;
YourIPAddress.GetAddressBytes().CopyTo(dhcpPacket, inc);
inc += LongSize;
ServerIPAddress.GetAddressBytes().CopyTo(dhcpPacket, inc);
inc += LongSize;
GatewayIPAddress.GetAddressBytes().CopyTo(dhcpPacket, inc);
inc += LongSize;
ClientHardwareAddress.CopyTo(dhcpPacket, inc);
// We directly jump to the Magic cookie
inc = 236;
Cookie.CopyTo(dhcpPacket, inc);
inc += LongSize;
Options.CopyTo(dhcpPacket, inc);
return dhcpPacket;
}
private byte[] BuildType(DhcpMessageType acktype, IPAddress cip, IPAddress mask, IPAddress sip, byte[] additionalOptions = null)
{
OperationCode = DhcpOperation.BootReply;
YourIPAddress = cip;
ResetOptions();
AddOption(DhcpOptionCode.DhcpMessageType, new byte[] { (byte)acktype });
if (acktype != DhcpMessageType.Nak)
{
AddOption(DhcpOptionCode.SubnetMask, mask.GetAddressBytes());
AddOption(DhcpOptionCode.DhcpAddress, sip.GetAddressBytes());
}
if (additionalOptions != null)
{
AddOptionRaw(ref additionalOptions);
}
return Build();
}
/// <summary>
/// Offer message.
/// </summary>
/// <param name="cip">Client IP addres..</param>
/// <param name="mask">Network mask.</param>
/// <param name="sip">Server IP address.</param>
/// <param name="additionalOptions">Additional options to send.</param>
/// <returns>A byte arry with the message.</returns>
public byte[] Offer(IPAddress cip, IPAddress mask, IPAddress sip, byte[] additionalOptions = null) => BuildType(DhcpMessageType.Offer, cip, mask, sip, additionalOptions);
/// <summary>
/// Ackanoledge message.
/// </summary>
/// <param name="cip">Client IP addres..</param>
/// <param name="mask">Network mask.</param>
/// <param name="sip">Server IP address.</param>
/// <param name="additionalOptions">Additional options to send.</param>
/// <returns>A byte arry with the message.</returns>
public byte[] Acknoledge(IPAddress cip, IPAddress mask, IPAddress sip, byte[] additionalOptions = null) => BuildType(DhcpMessageType.Ack, cip, mask, sip, additionalOptions);
/// <summary>
/// Not Ackanoledge message.
/// </summary>
/// <returns>A byte arry with the message.</returns>
public byte[] NotAcknoledge()
{
YourIPAddress = new IPAddress(0);
return BuildType(DhcpMessageType.Nak, new IPAddress(0), new IPAddress(0), new IPAddress(0));
}
/// <summary>
/// Not Ackanoledge message.
/// </summary>
/// <param name="cip">Client IP addres..</param>
/// <param name="mask">Network mask.</param>
/// <param name="sip">Server IP address.</param>
/// <returns>A byte arry with the message.</returns>
public byte[] Decline(IPAddress cip, IPAddress mask, IPAddress sip) => BuildType(DhcpMessageType.Decline, cip, mask, sip);
private int OptionsFindKey(DhcpOptionCode lookOpt)
{
int offset = 0;
if (Options[offset] != (byte)DhcpOptionCode.Pad)
{
while (Options[offset] != (byte)DhcpOptionCode.End)
{
byte optcode = Options[offset++];
int optlen = Options[offset++];
if ((DhcpOptionCode)optcode == lookOpt)
{
return offset - 2;
}
offset += optlen;
}
}
return -1;
}
/// <summary>
/// Resets the options.
/// </summary>
public void ResetOptions()
{
// 240 is where the options are starting, right after the magic cookie
Options = new byte[DhcppacketSize - IndexToOptions];
Options[0] = 0xff;
}
/// <summary>
/// Add an option. This will just add the option to the option list, you are responsible to use the proper code and encoding.
/// </summary>
/// <param name="optdata">The options to add.</param>
private void AddOptionRaw(ref byte[] optdata)
{
int offset = 0;
while (Options[offset] != 0xff)
{
byte optcode = Options[offset++];
int optlen = Options[offset++];
offset += optlen;
}
optdata.CopyTo(Options, offset);
Options[offset + optdata.Length] = (byte)DhcpOptionCode.End; // set end of options
}
/// <summary>
/// Add an option to the options.
/// </summary>
/// <param name="optType">The option code.</param>
/// <param name="optData">The option data.</param>
public void AddOption(DhcpOptionCode optType, byte[] optData)
{
byte[] optTyData = new byte[2 + optData.Length];
optTyData[0] = (byte)optType;
optTyData[1] = (byte)optData.Length;
optData.CopyTo(optTyData, 2);
AddOptionRaw(ref optTyData);
}
/// <summary>
/// Checks if the option contains a specific key.
/// </summary>
/// <param name="lookOpt">The option to check.</param>
/// <returns>True if found.</returns>
public bool OptionsContainsKey(DhcpOptionCode lookOpt) => OptionsFindKey(lookOpt) == -1 ? false : true;
/// <summary>
/// Gets the option contained in a key.
/// </summary>
/// <param name="lookOpt">The option to check.</param>
/// <returns>The byte array with the raw option value.</returns>
public byte[] GetOption(DhcpOptionCode lookOpt)
{
int optofs = OptionsFindKey(lookOpt);
if (optofs == -1)
{
return null;
}
byte[] optVal = new byte[Options[optofs + 1]];
Array.Copy(Options, optofs + 2, optVal, 0, optVal.Length);
return optVal;
}
private bool IsOptionsInvalid() => !((Options != null) && (Options.Length > 0));
}
}