From db220001f6c097d33649f48817046c04e9803a82 Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Fri, 10 Sep 2021 01:10:58 +0100 Subject: [PATCH] DEV9: Add cloning of packets --- pcsx2/DEV9/PacketReader/IP/IP_Options.h | 18 +++- pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp | 24 +++++ pcsx2/DEV9/PacketReader/IP/IP_Packet.h | 4 + pcsx2/DEV9/PacketReader/IP/IP_Payload.h | 23 ++++ .../PacketReader/IP/UDP/DHCP/DHCP_Options.h | 100 ++++++++++++++++++ .../PacketReader/IP/UDP/DHCP/DHCP_Packet.cpp | 29 +++++ .../PacketReader/IP/UDP/DHCP/DHCP_Packet.h | 2 + .../PacketReader/IP/UDP/DNS/DNS_Packet.cpp | 5 + .../DEV9/PacketReader/IP/UDP/DNS/DNS_Packet.h | 1 + pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp | 12 +++ pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h | 2 + pcsx2/DEV9/PacketReader/Payload.h | 22 ++++ 12 files changed, 241 insertions(+), 1 deletion(-) diff --git a/pcsx2/DEV9/PacketReader/IP/IP_Options.h b/pcsx2/DEV9/PacketReader/IP/IP_Options.h index 0c078d776f..ffc5441454 100644 --- a/pcsx2/DEV9/PacketReader/IP/IP_Options.h +++ b/pcsx2/DEV9/PacketReader/IP/IP_Options.h @@ -23,7 +23,7 @@ namespace PacketReader::IP virtual u8 GetLength() = 0; virtual u8 GetCode() = 0; virtual void WriteBytes(u8* buffer, int* offset) = 0; - + virtual BaseOption* Clone() const = 0; virtual ~BaseOption() {} }; @@ -33,6 +33,7 @@ namespace PacketReader::IP bool IsCopyOnFragment(); u8 GetClass(); //0 = control, 2 = debugging and measurement u8 GetNumber(); + virtual IPOption* Clone() const = 0; }; class IPopUnk : public IPOption @@ -48,6 +49,11 @@ namespace PacketReader::IP virtual u8 GetCode() { return code; } void WriteBytes(u8* buffer, int* offset); + + virtual IPopUnk* Clone() const + { + return new IPopUnk(*this); + } }; class IPopNOP : public IPOption @@ -61,6 +67,11 @@ namespace PacketReader::IP buffer[*offset] = GetCode(); (*offset)++; } + + virtual IPopNOP* Clone() const + { + return new IPopNOP(*this); + } }; class IPopRouterAlert : public IPOption @@ -76,5 +87,10 @@ namespace PacketReader::IP virtual u8 GetCode() { return 148; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual IPopRouterAlert* Clone() const + { + return new IPopRouterAlert(*this); + } }; } // namespace PacketReader::IP diff --git a/pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp b/pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp index 8e6ed8fae0..c47675441e 100644 --- a/pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp +++ b/pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp @@ -146,6 +146,25 @@ namespace PacketReader::IP payload = std::make_unique(&buffer[offset], length - offset, protocol); } + IP_Packet::IP_Packet(const IP_Packet& original) + : headerLength{original.headerLength} + , dscp{original.dscp} + , id{original.id} + , fragmentFlags1{original.fragmentFlags1} + , fragmentFlags2{original.fragmentFlags2} + , timeToLive{original.timeToLive} + , protocol{original.protocol} + , checksum{original.checksum} + , sourceIP{original.sourceIP} + , destinationIP{original.destinationIP} + , payload{original.payload->Clone()} + { + //Clone options + options.reserve(original.options.size()); + for (size_t i = 0; i < options.size(); i++) + options.push_back(original.options[i]->Clone()); + } + IP_Payload* IP_Packet::GetPayload() { return payload.get(); @@ -191,6 +210,11 @@ namespace PacketReader::IP payload->WriteBytes(buffer, offset); } + IP_Packet* IP_Packet::Clone() const + { + return new IP_Packet(*this); + } + void IP_Packet::ReComputeHeaderLen() { int opOffset = 20; diff --git a/pcsx2/DEV9/PacketReader/IP/IP_Packet.h b/pcsx2/DEV9/PacketReader/IP/IP_Packet.h index 0261a3e655..907e2be9c9 100644 --- a/pcsx2/DEV9/PacketReader/IP/IP_Packet.h +++ b/pcsx2/DEV9/PacketReader/IP/IP_Packet.h @@ -63,6 +63,8 @@ namespace PacketReader::IP std::unique_ptr payload; public: + int GetHeaderLength(); + //DSCP/TOS Flags /* Upper 3 Bits @@ -122,11 +124,13 @@ namespace PacketReader::IP //Takes ownership of payload IP_Packet(IP_Payload* data); IP_Packet(u8* buffer, int bufferSize, bool fromICMP = false); + IP_Packet(const IP_Packet&); IP_Payload* GetPayload(); virtual int GetLength(); virtual void WriteBytes(u8* buffer, int* offset); + virtual IP_Packet* Clone() const; bool VerifyChecksum(); static u16 InternetChecksum(u8* buffer, int length); diff --git a/pcsx2/DEV9/PacketReader/IP/IP_Payload.h b/pcsx2/DEV9/PacketReader/IP/IP_Payload.h index 0b03e1730b..7ea9192c96 100644 --- a/pcsx2/DEV9/PacketReader/IP/IP_Payload.h +++ b/pcsx2/DEV9/PacketReader/IP/IP_Payload.h @@ -25,6 +25,7 @@ namespace PacketReader::IP virtual u8 GetProtocol() = 0; virtual bool VerifyChecksum(IP_Address srcIP, IP_Address dstIP) { return false; } virtual void CalculateChecksum(IP_Address srcIP, IP_Address dstIP) {} + virtual IP_Payload* Clone() const = 0; virtual ~IP_Payload() {} }; @@ -46,6 +47,17 @@ namespace PacketReader::IP if (len != 0) data = std::make_unique(len); } + IP_PayloadData(const IP_PayloadData& original) + { + protocol = original.protocol; + length = original.length; + + if (length != 0) + { + data = std::make_unique(length); + memcpy(data.get(), original.data.get(), length); + } + } virtual int GetLength() { return length; @@ -62,6 +74,10 @@ namespace PacketReader::IP { return protocol; } + virtual IP_PayloadData* Clone() const + { + return new IP_PayloadData(*this); + } }; //Pointer to bytes not owned by class @@ -81,6 +97,7 @@ namespace PacketReader::IP length = len; protocol = prot; } + IP_PayloadPtr(const IP_PayloadPtr&) = delete; virtual int GetLength() { return length; @@ -95,6 +112,12 @@ namespace PacketReader::IP memcpy(buffer, data, length); *offset += length; } + virtual IP_Payload* Clone() const + { + IP_PayloadData* ret = new IP_PayloadData(length, protocol); + memcpy(ret->data.get(), data, length); + return ret; + } virtual u8 GetProtocol() { return protocol; diff --git a/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Options.h b/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Options.h index dd4ae7b025..46adb5cc54 100644 --- a/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Options.h +++ b/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Options.h @@ -34,6 +34,11 @@ namespace PacketReader::IP::UDP::DHCP buffer[*offset] = GetCode(); (*offset)++; } + + virtual DHCPopNOP* Clone() const + { + return new DHCPopNOP(*this); + } }; class DHCPopSubnet : public BaseOption @@ -48,6 +53,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 1; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopSubnet* Clone() const + { + return new DHCPopSubnet(*this); + } }; class DHCPopRouter : public BaseOption //can be longer then 1 address @@ -61,6 +71,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 3; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopRouter* Clone() const + { + return new DHCPopRouter(*this); + } }; class DHCPopDNS : public BaseOption //can be longer then 1 address @@ -74,6 +89,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 6; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopDNS* Clone() const + { + return new DHCPopDNS(*this); + } }; class DHCPopHostName : public BaseOption @@ -89,6 +109,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 12; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopHostName* Clone() const + { + return new DHCPopHostName(*this); + } }; class DHCPopDnsName : public BaseOption @@ -104,6 +129,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 15; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopDnsName* Clone() const + { + return new DHCPopDnsName(*this); + } }; class DHCPopBCIP : public BaseOption //The IP to send broadcasts to @@ -118,6 +148,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 28; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopBCIP* Clone() const + { + return new DHCPopBCIP(*this); + } }; //What even sent this? @@ -146,6 +181,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 46; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopNBIOSType* Clone() const + { + return new DHCPopNBIOSType(*this); + } }; class DHCPopREQIP : public BaseOption //The IP to send broadcasts to @@ -160,6 +200,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 50; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopREQIP* Clone() const + { + return new DHCPopREQIP(*this); + } }; class DHCPopIPLT : public BaseOption @@ -174,6 +219,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 51; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopIPLT* Clone() const + { + return new DHCPopIPLT(*this); + } }; class DHCPopMSG : public BaseOption @@ -187,6 +237,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 53; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopMSG* Clone() const + { + return new DHCPopMSG(*this); + } }; class DHCPopSERVIP : public BaseOption //DHCP server ip @@ -201,6 +256,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 54; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopSERVIP* Clone() const + { + return new DHCPopSERVIP(*this); + } }; class DHCPopREQLIST : public BaseOption @@ -215,6 +275,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 55; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopREQLIST* Clone() const + { + return new DHCPopREQLIST(*this); + } }; class DHCPopMSGStr : public BaseOption @@ -230,6 +295,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 56; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopMSGStr* Clone() const + { + return new DHCPopMSGStr(*this); + } }; class DHCPopMMSGS : public BaseOption @@ -244,6 +314,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 57; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopMMSGS* Clone() const + { + return new DHCPopMMSGS(*this); + } }; class DHCPopT1 : public BaseOption @@ -258,6 +333,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 58; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopT1* Clone() const + { + return new DHCPopT1(*this); + } }; class DHCPopT2 : public BaseOption @@ -272,6 +352,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 59; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopT2* Clone() const + { + return new DHCPopT2(*this); + } }; class DHCPopClassID : public BaseOption @@ -287,6 +372,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 60; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopClassID* Clone() const + { + return new DHCPopClassID(*this); + } }; class DHCPopClientID final : public BaseOption @@ -301,6 +391,11 @@ namespace PacketReader::IP::UDP::DHCP virtual u8 GetCode() { return 61; } virtual void WriteBytes(u8* buffer, int* offset); + + virtual DHCPopClientID* Clone() const + { + return new DHCPopClientID(*this); + } }; class DHCPopEND : public BaseOption @@ -316,5 +411,10 @@ namespace PacketReader::IP::UDP::DHCP buffer[*offset] = GetCode(); (*offset)++; } + + virtual DHCPopEND* Clone() const + { + return new DHCPopEND(*this); + } }; } // namespace PacketReader::IP::UDP::DHCP diff --git a/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Packet.cpp b/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Packet.cpp index 0df4c9f176..53cf95de91 100644 --- a/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Packet.cpp +++ b/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Packet.cpp @@ -151,6 +151,30 @@ namespace PacketReader::IP::UDP::DHCP } } while (opReadFin == false); } + DHCP_Packet::DHCP_Packet(const DHCP_Packet& original) + : op{original.op} + , hardwareType(original.hardwareType) + , hardwareAddressLength{original.hardwareAddressLength} + , hops{original.hops} + , transactionID{original.transactionID} + , seconds{original.seconds} + , flags{original.flags} + , clientIP{original.clientIP} + , yourIP{original.yourIP} + , serverIP{original.serverIP} + , gatewayIP{original.gatewayIP} + , magicCookie(original.magicCookie) + , maxLength{original.maxLength} + { + memcpy(clientHardwareAddress, original.clientHardwareAddress, 16); + //Assume BOOTP unused + + //Clone options + options.reserve(original.options.size()); + for (size_t i = 0; i < options.size(); i++) + options.push_back(original.options[i]->Clone()); + } + int DHCP_Packet::GetLength() { @@ -215,6 +239,11 @@ namespace PacketReader::IP::UDP::DHCP *offset = start + GetLength(); } + DHCP_Packet* DHCP_Packet::Clone() const + { + return new DHCP_Packet(*this); + } + DHCP_Packet::~DHCP_Packet() { for (size_t i = 0; i < options.size(); i++) diff --git a/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Packet.h b/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Packet.h index 183836469d..53e9d52d7d 100644 --- a/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Packet.h +++ b/pcsx2/DEV9/PacketReader/IP/UDP/DHCP/DHCP_Packet.h @@ -46,9 +46,11 @@ namespace PacketReader::IP::UDP::DHCP DHCP_Packet() {} DHCP_Packet(u8* buffer, int bufferSize); + DHCP_Packet(const DHCP_Packet&); virtual int GetLength(); virtual void WriteBytes(u8* buffer, int* offset); + virtual DHCP_Packet* Clone() const; virtual ~DHCP_Packet(); }; diff --git a/pcsx2/DEV9/PacketReader/IP/UDP/DNS/DNS_Packet.cpp b/pcsx2/DEV9/PacketReader/IP/UDP/DNS/DNS_Packet.cpp index 56368b860b..bd1619bc64 100644 --- a/pcsx2/DEV9/PacketReader/IP/UDP/DNS/DNS_Packet.cpp +++ b/pcsx2/DEV9/PacketReader/IP/UDP/DNS/DNS_Packet.cpp @@ -191,4 +191,9 @@ namespace PacketReader::IP::UDP::DNS for (size_t i = 0; i < additional.size(); i++) additional[i].WriteBytes(buffer, offset); } + + DNS_Packet* DNS_Packet::Clone() const + { + return new DNS_Packet(*this); + } } // namespace PacketReader::IP::UDP::DNS diff --git a/pcsx2/DEV9/PacketReader/IP/UDP/DNS/DNS_Packet.h b/pcsx2/DEV9/PacketReader/IP/UDP/DNS/DNS_Packet.h index a9bae2d542..ab2e15b2cc 100644 --- a/pcsx2/DEV9/PacketReader/IP/UDP/DNS/DNS_Packet.h +++ b/pcsx2/DEV9/PacketReader/IP/UDP/DNS/DNS_Packet.h @@ -77,5 +77,6 @@ namespace PacketReader::IP::UDP::DNS virtual int GetLength(); virtual void WriteBytes(u8* buffer, int* offset); + virtual DNS_Packet* Clone() const; }; } // namespace PacketReader::IP::UDP::DNS diff --git a/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp b/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp index d22570edb7..d449497e10 100644 --- a/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp +++ b/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp @@ -46,6 +46,13 @@ namespace PacketReader::IP::UDP payload = std::make_unique(&buffer[offset], length - offset); //AllDone } + UDP_Packet::UDP_Packet(const UDP_Packet& original) + : sourcePort{original.sourcePort} + , destinationPort{original.destinationPort} + , checksum{original.destinationPort} + , payload{original.payload->Clone()} + { + } Payload* UDP_Packet::GetPayload() { @@ -67,6 +74,11 @@ namespace PacketReader::IP::UDP payload->WriteBytes(buffer, offset); } + UDP_Packet* UDP_Packet::Clone() const + { + return new UDP_Packet(*this); + } + u8 UDP_Packet::GetProtocol() { return (u8)protocol; diff --git a/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h b/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h index 618755989d..ee2d779f09 100644 --- a/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h +++ b/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h @@ -37,11 +37,13 @@ namespace PacketReader::IP::UDP //Takes ownership of payload UDP_Packet(Payload* data); UDP_Packet(u8* buffer, int bufferSize); + UDP_Packet(const UDP_Packet&); Payload* GetPayload(); virtual int GetLength(); virtual void WriteBytes(u8* buffer, int* offset); + virtual UDP_Packet* Clone() const; virtual u8 GetProtocol(); diff --git a/pcsx2/DEV9/PacketReader/Payload.h b/pcsx2/DEV9/PacketReader/Payload.h index fb0c3f07cf..e505187b12 100644 --- a/pcsx2/DEV9/PacketReader/Payload.h +++ b/pcsx2/DEV9/PacketReader/Payload.h @@ -22,6 +22,7 @@ namespace PacketReader public: virtual int GetLength() = 0; virtual void WriteBytes(u8* buffer, int* offset) = 0; + virtual Payload* Clone() const = 0; virtual ~Payload() {} }; @@ -42,6 +43,16 @@ namespace PacketReader if (len != 0) data = std::make_unique(len); } + PayloadData(const PayloadData& original) + { + length = original.length; + + if (length != 0) + { + data = std::make_unique(length); + memcpy(data.get(), original.data.get(), length); + } + } virtual int GetLength() { return length; @@ -54,6 +65,10 @@ namespace PacketReader memcpy(&buffer[*offset], data.get(), length); *offset += length; } + virtual PayloadData* Clone() const + { + return new PayloadData(*this); + } }; //Pointer to bytes not owned by class @@ -71,6 +86,7 @@ namespace PacketReader data = ptr; length = len; } + PayloadPtr(const PayloadPtr&) = delete; virtual int GetLength() { return length; @@ -85,5 +101,11 @@ namespace PacketReader memcpy(&buffer[*offset], data, length); *offset += length; } + virtual Payload* Clone() const + { + PayloadData* ret = new PayloadData(length); + memcpy(ret->data.get(), data, length); + return ret; + } }; } // namespace PacketReader