Modified memory leaks, untested

This commit is contained in:
Brychan Dempsey 2021-10-04 23:52:52 +13:00
parent ec0b312f7f
commit 40b4c4aa8a
5 changed files with 33 additions and 27 deletions

View File

@ -305,16 +305,16 @@ std::vector<std::vector<unsigned char>> DHCP::ProcessDHCP(unsigned char* rxBuffe
}
return std::vector<std::vector<unsigned char>>(0);
}
DHCP::DHCP(unsigned char* deviceAddress, unsigned char* subnetMask, unsigned char maxLeases, unsigned char leaseStart, unsigned int leaseTime, unsigned int* dnsServers, unsigned char dnsServerCount) {
localAddress1 = deviceAddress[0];
localAddress2 = deviceAddress[1];
localAddress3 = deviceAddress[2];
deviceIP = deviceAddress[3];
DHCP::DHCP(unsigned long deviceAddress, unsigned long subnetMask, unsigned char maxLeases, unsigned char leaseStart, unsigned int leaseTime, std::vector<unsigned long> dnsServers, unsigned char dnsServerCount) {
localAddress1 = (deviceAddress >> 24) & 0xFF;
localAddress2 = (deviceAddress >> 16) & 0xFF;
localAddress3 = (deviceAddress >> 8) & 0xFF;
deviceIP = deviceAddress & 0xFF;
localSubnet1 = subnetMask[0];
localSubnet2 = subnetMask[1];
localSubnet3 = subnetMask[2];
localSubnet4 = subnetMask[3];
localSubnet1 = (subnetMask >> 24) & 0xFF;
localSubnet2 = (subnetMask >> 16) & 0xFF;
localSubnet3 = (subnetMask >> 8) & 0xFF;
localSubnet4 = subnetMask & 0xFF;
this->maxLeases = maxLeases;
this->leaseStart = leaseStart;

View File

@ -52,7 +52,7 @@ public:
std::vector<std::vector<unsigned char>> ProcessDHCP(unsigned char* rxBuffer, unsigned long bufferLength);
DHCP(unsigned char* deviceAddress, unsigned char* subnetMask, unsigned char maxLeases, unsigned char leaseStart, unsigned int leaseTime, unsigned int* dnsServers, unsigned char dnsServerCount);
DHCP(unsigned long deviceAddress, unsigned long subnetMask, unsigned char maxLeases, unsigned char leaseStart, unsigned int leaseTime, std::vector<unsigned long> dnsServers, unsigned char dnsServerCount);
~DHCP();
};

View File

@ -13,11 +13,11 @@ Net::IPAddress::IPAddress(unsigned char c1, unsigned char c2, unsigned char c3,
address[3] = c4;
}
Net::IPAddress::IPAddress(unsigned char* IP) {
address[0] = *IP;
address[1] = *(IP + 1);
address[2] = *(IP + 2);
address[3] = *(IP + 3);
Net::IPAddress::IPAddress(unsigned long IP) {
address[0] = IP & 0xFF;
address[1] = (IP >> 8) & 0xFF;
address[2] = (IP >> 16) & 0xFF;
address[3] = (IP >> 24) & 0xFF;
}
Net::IPAddress Net::IPAddress::Empty() {
@ -34,10 +34,10 @@ bool Net::IPAddress::Equals(IPAddress other) {
return true;
}
Net::IPEndPoint::IPEndPoint(unsigned char* IP, unsigned short Port) {
Net::IPEndPoint::IPEndPoint(unsigned long IP, unsigned short Port) {
Address = IPAddress(IP);
socks.sin_family = AF_INET;
socks.sin_addr.S_un.S_addr = (IP[3] << 24) | (IP[2] << 16) | (IP[1] << 8) | IP[0]; // Order of the IP bytes is swapped, so 192.168.1.32 would be 32.1.168.192
socks.sin_addr.S_un.S_addr = IP; // Order of the IP bytes is swapped, so 192.168.1.32 would be 32.1.168.192
socks.sin_port = htons(Port); // Likewise, the port is also byte-order reversed.
}
@ -64,7 +64,7 @@ void Net::UdpClient::Socket::Bind(IPEndPoint ep) {
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof broadcast);
}
void Net::UdpClient::Socket::Bind(unsigned char* IP, unsigned short Port) {
void Net::UdpClient::Socket::Bind(unsigned long IP, unsigned short Port) {
IPEndPoint ep = IPEndPoint(IP, Port);
Bind(ep);
}
@ -84,7 +84,9 @@ int Net::UdpClient::Send(std::vector<unsigned char> Datagram, IPEndPoint ep) {
}
int Net::UdpClient::Send(std::vector<unsigned char> Datagram, std::vector<unsigned char> DestinationIP, int DestPort) {
IPEndPoint ep = IPEndPoint(DestinationIP.data(), (u_short)DestPort);
unsigned char* dataStart = DestinationIP.data();
unsigned long result = (*(dataStart+3) << 24) | (*(dataStart + 2) << 16 ) | (*(dataStart + 1) << 8) | *dataStart; // Merge the bytes into a single long
IPEndPoint ep = IPEndPoint(result, (u_short)DestPort);
return Send(Datagram, ep);
}

View File

@ -17,7 +17,7 @@ public:
IPAddress();
IPAddress(unsigned char c1, unsigned char c2, unsigned char c3, unsigned char c4);
IPAddress(unsigned char* IP);
IPAddress(unsigned long IP);
static IPAddress Empty();
@ -28,7 +28,7 @@ public:
struct sockaddr_in socks;
IPAddress Address;
IPEndPoint(unsigned char* IP, unsigned short Port);
IPEndPoint(unsigned long IP, unsigned short Port);
};
class UdpClient {
@ -37,7 +37,7 @@ public:
SOCKET sock;
void Bind(IPEndPoint ep);
void Bind(unsigned char* IP, unsigned short Port);
void Bind(unsigned long IP, unsigned short Port);
};
public:

View File

@ -11,19 +11,23 @@
// 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";
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);
std::vector<unsigned long> servers;
servers.push_back(0x1010101);
DHCP dhcp = DHCP((192 << 24 ) | (168 << 16 ) | (250 << 8) | 1 , 0xFFFFFF00, 32, 2, 900, servers, 1); // 0xFFFFFF00 = 255.255.255.0; 0x1010101 = 1.1.1.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);
udpClient.Client.Bind(Net::IPEndPoint((dhcp.localAddress1 << 24) | (dhcp.localAddress2 << 16) | (dhcp.localAddress3 << 8) | dhcp.deviceIP, 67));
Net::IPEndPoint remote = Net::IPEndPoint(0, 0);
unsigned long waitingBytes = 0;
while (true) {
waitingBytes = udpClient.Available();
if (waitingBytes > 240) { // DHCP requires *at least* 240 bytes, the packet is malformed or incorrect if it is less
std::vector<std::vector<unsigned char>> result = dhcp.ProcessDHCP(udpClient.Recieve(&remote, (unsigned short)waitingBytes), waitingBytes);
unsigned char* buffBytes = udpClient.Recieve(&remote, (unsigned short)waitingBytes);
std::vector<std::vector<unsigned char>> result = dhcp.ProcessDHCP(buffBytes, waitingBytes);
delete[] buffBytes; // Handle the assigned buffer array
if (result.size() == 2) {
if (remote.Address.Equals(Net::IPAddress::Empty())) {
udpClient.Send(result[1], result[0], 68);