From 40b4c4aa8a1aac7d4c7a99feefccfbbbf2abfb69 Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Mon, 4 Oct 2021 23:52:52 +1300 Subject: [PATCH] Modified memory leaks, untested --- Simple DHCP Server (C++)/DHCP.cpp | 18 ++++++++--------- Simple DHCP Server (C++)/DHCP.h | 2 +- Simple DHCP Server (C++)/Net.cpp | 20 ++++++++++--------- Simple DHCP Server (C++)/Net.h | 6 +++--- .../Simple DHCP Server (C++).cpp | 14 ++++++++----- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/Simple DHCP Server (C++)/DHCP.cpp b/Simple DHCP Server (C++)/DHCP.cpp index 51055e4..0caa61d 100644 --- a/Simple DHCP Server (C++)/DHCP.cpp +++ b/Simple DHCP Server (C++)/DHCP.cpp @@ -305,16 +305,16 @@ std::vector> DHCP::ProcessDHCP(unsigned char* rxBuffe } return std::vector>(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 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; diff --git a/Simple DHCP Server (C++)/DHCP.h b/Simple DHCP Server (C++)/DHCP.h index 47ca23b..4f7ff92 100644 --- a/Simple DHCP Server (C++)/DHCP.h +++ b/Simple DHCP Server (C++)/DHCP.h @@ -52,7 +52,7 @@ public: std::vector> 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 dnsServers, unsigned char dnsServerCount); ~DHCP(); }; diff --git a/Simple DHCP Server (C++)/Net.cpp b/Simple DHCP Server (C++)/Net.cpp index f0ccb2c..20327ce 100644 --- a/Simple DHCP Server (C++)/Net.cpp +++ b/Simple DHCP Server (C++)/Net.cpp @@ -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 Datagram, IPEndPoint ep) { } int Net::UdpClient::Send(std::vector Datagram, std::vector 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); } diff --git a/Simple DHCP Server (C++)/Net.h b/Simple DHCP Server (C++)/Net.h index 9cd3549..c42d066 100644 --- a/Simple DHCP Server (C++)/Net.h +++ b/Simple DHCP Server (C++)/Net.h @@ -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: diff --git a/Simple DHCP Server (C++)/Simple DHCP Server (C++).cpp b/Simple DHCP Server (C++)/Simple DHCP Server (C++).cpp index 87181ec..dc1993c 100644 --- a/Simple DHCP Server (C++)/Simple DHCP Server (C++).cpp +++ b/Simple DHCP Server (C++)/Simple DHCP Server (C++).cpp @@ -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 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> result = dhcp.ProcessDHCP(udpClient.Recieve(&remote, (unsigned short)waitingBytes), waitingBytes); + unsigned char* buffBytes = udpClient.Recieve(&remote, (unsigned short)waitingBytes); + std::vector> 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);