2021-01-07 00:03:30 +13:00
|
|
|
// Simple DHCP Server (C++).cpp : This file contains the 'main' function. Program execution begins and ends there.
|
|
|
|
//
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2021-01-07 17:14:55 +13:00
|
|
|
#include <thread>
|
2021-01-07 15:49:25 +13:00
|
|
|
#include "Net.h"
|
|
|
|
#include "DHCP.h"
|
2021-01-07 00:03:30 +13:00
|
|
|
|
|
|
|
// Maintain a C#-esque network client for simple handling
|
|
|
|
// As per https://stackoverflow.com/questions/14665543/how-do-i-receive-udp-packets-with-winsock-in-c
|
|
|
|
// A handler class is helpful. This implementation is based on the implementation in .NET
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
std::cout << "Creating DHCP Server...\n";
|
2021-01-07 15:49:25 +13:00
|
|
|
DHCP dhcp = DHCP(new unsigned char[] {192, 168, 250, 1}, new unsigned char[] {255, 255, 255, 0}, 32, 2, 900, new unsigned int[] { 16843009 }, 1);
|
|
|
|
Net::UdpClient udpClient = Net::UdpClient();
|
|
|
|
udpClient.Client.Bind(Net::IPEndPoint(new unsigned char[] {dhcp.localAddress1, dhcp.localAddress2, dhcp.localAddress3, dhcp.deviceIP}, 67));
|
|
|
|
Net::IPEndPoint remote = Net::IPEndPoint(new unsigned char[] {0, 0, 0, 0}, 0);
|
2021-01-07 17:14:55 +13:00
|
|
|
unsigned long waitingBytes = 0;
|
2021-01-07 00:03:30 +13:00
|
|
|
while (true) {
|
2021-01-07 17:14:55 +13:00
|
|
|
waitingBytes = udpClient.Available();
|
|
|
|
if (waitingBytes > 240) { // DHCP requires *at least* 240 bytes, the packet is malformed or incorrect if it is less
|
2021-01-07 17:26:00 +13:00
|
|
|
std::vector<std::vector<unsigned char>> result = dhcp.ProcessDHCP(udpClient.Recieve(&remote, (unsigned short)waitingBytes), waitingBytes);
|
2021-01-07 17:14:55 +13:00
|
|
|
if (result.size() == 2) {
|
|
|
|
if (remote.Address.Equals(Net::IPAddress::Empty())) {
|
|
|
|
udpClient.Send(result[1], result[0], 68);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
udpClient.Send(result[1], remote);
|
|
|
|
}
|
2021-01-07 00:03:30 +13:00
|
|
|
}
|
|
|
|
}
|
2021-01-07 17:14:55 +13:00
|
|
|
// Artificial delay
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
2021-01-07 00:03:30 +13:00
|
|
|
}
|
|
|
|
}
|