-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDHCPserver.cs
143 lines (134 loc) · 5.65 KB
/
DHCPserver.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
using System;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using nanoFramework.Networking;
using DHCPSharp.Common;
using DHCPSharp.Common.Enums;
namespace WiFiAP
{
public class DHCPserver
{
// Constants
private const int DHCP_PORT = 67;
private const int DHCP_CLIENT_PORT = 68;
//
byte[] dhcp_ip;
static Socket _listenerDHCP;
static Socket _sender;
Thread _DHCPserverThread;
bool listening;
public bool Start()
{
if (_listenerDHCP == null)
{
try
{
//listen socket
_listenerDHCP = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress dsip = new IPAddress(0xFFFFFFFF);
IPEndPoint ep = new IPEndPoint(dsip, DHCP_PORT);
_listenerDHCP.Bind(ep);
//send socket
_sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_sender.Bind(new IPEndPoint(IPAddress.Parse(WirelessAP.GetIP()), 0));
_sender.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.Broadcast, true);
_sender.Connect(new IPEndPoint(IPAddress.Parse("255.255.255.255"), DHCP_CLIENT_PORT));
//make dirty dynamic ip pool
dhcp_ip = IPAddress.Parse(WirelessAP.GetIP()).GetAddressBytes();
//start server thread
_DHCPserverThread = new Thread(RunServer);
_DHCPserverThread.Start();
return true;
}
catch (SocketException ex)
{
Debug.WriteLine($"DHCP: ** Socket exception occurred: {ex.Message} error code {ex.ErrorCode}!**");
}
catch (Exception ex)
{
Debug.WriteLine($"DHCP: ** Exception occurred: {ex.Message}!**");
}
return false;
}
return true;
}
public void Stop()
{
listening = false;
}
private void RunServer()
{
listening = true;
// setup buffer to read data from socket
byte[] buffer = new byte[1024];
Debug.WriteLine($"DHCP: start listen...");
//_listener.Listen(1);
while (listening)
{
//check if socket have any bytes to read
int bytes = _listenerDHCP.Available;
if (bytes > 0)
{
Debug.WriteLine($"DHCP: Have {bytes} bytes");
bytes = _listenerDHCP.Receive(buffer);
Debug.WriteLine($"DHCP: <- Read {bytes} bytes from {(IPEndPoint)_listenerDHCP.LocalEndPoint }");
// we have data!
// output as string
//Debug.WriteLine(new String(Encoding.UTF8.GetChars(buffer, 0, bytes)));
//Debug.WriteLine(BitConverter.ToString(buffer, 0, bytes));
DhcpMessage dhcp_req = new DhcpMessage();
dhcp_req.parse(ref buffer);
string sname = dhcp_req.HostName;
switch (dhcp_req.DhcpMessageType)
{
case (DhcpMessageType.Discover):
{
//increment dynamic ip
if (dhcp_ip[3] < 254)
{ dhcp_ip[3]++; }
else
{ dhcp_ip[3] = 2; }
Debug.WriteLine($"DHCP: Discover from host: {sname}");
_sender.Send(dhcp_req.offer(new IPAddress(dhcp_ip).ToString(), "255.255.255.0", WirelessAP.GetIP()));
Debug.WriteLine($"DHCP: -> Sent Offer {dhcp_req.DHCPpacketSize} bytes to client");
//Debug.WriteLine(BitConverter.ToString(dhcp_req.offer()));
break;
}
case (DhcpMessageType.Request):
{
Debug.WriteLine($"DHCP: Request from host: {sname}");
Debug.WriteLine($"DHCP Request: Requested address {dhcp_req.RequestedIpAddress}");
Debug.WriteLine($"DHCP Request: Server Identifier {dhcp_req.DhcpAddress}");
string confirmip = dhcp_req.RequestedIpAddress;
_sender.Send(dhcp_req.ack(confirmip, "255.255.255.0", WirelessAP.GetIP()));
//Debug.WriteLine(BitConverter.ToString(buffer, 0, bytes));
break;
}
default:
{
Debug.WriteLine($"DHCP: Unknown ({dhcp_req.DhcpMessageType}) from host: {sname}");
break;
}
}
}
else
{
//free cpu time if no bytes in socket
Thread.Sleep(100);
}
}
try
{
_listenerDHCP.Close();
_sender.Close();
}
catch { }
_listenerDHCP = null;
_sender = null;
Debug.WriteLine($"DHCP: stoped");
}
}
}