From a8a170ebe6af5e6abd59dabe7048286a8622fe96 Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Thu, 5 Dec 2024 16:37:28 +0000 Subject: [PATCH] DEV9: Apply const to basic payload types Propagate const into PacketReader classes as needed Provide non-const editor version for pcap --- .../PacketReader/ARP/ARP_PacketEditor.cpp | 2 +- .../DEV9/PacketReader/ARP/ARP_PacketEditor.h | 4 +- .../DEV9/PacketReader/EthernetFrameEditor.cpp | 4 +- pcsx2/DEV9/PacketReader/EthernetFrameEditor.h | 4 +- .../DEV9/PacketReader/IP/ICMP/ICMP_Packet.cpp | 2 +- pcsx2/DEV9/PacketReader/IP/ICMP/ICMP_Packet.h | 2 +- pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp | 2 +- pcsx2/DEV9/PacketReader/IP/IP_Packet.h | 2 +- pcsx2/DEV9/PacketReader/IP/IP_Payload.h | 28 +++++------ pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.cpp | 2 +- pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.h | 2 +- pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp | 2 +- pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h | 2 +- pcsx2/DEV9/PacketReader/Payload.h | 49 +++++++++++++++---- .../Sessions/ICMP_Session/ICMP_Session.cpp | 4 +- .../DEV9/Sessions/ICMP_Session/ICMP_Session.h | 2 +- pcsx2/DEV9/pcap_io.cpp | 6 +-- 17 files changed, 74 insertions(+), 45 deletions(-) diff --git a/pcsx2/DEV9/PacketReader/ARP/ARP_PacketEditor.cpp b/pcsx2/DEV9/PacketReader/ARP/ARP_PacketEditor.cpp index b0a27834ec..a5312f86a7 100644 --- a/pcsx2/DEV9/PacketReader/ARP/ARP_PacketEditor.cpp +++ b/pcsx2/DEV9/PacketReader/ARP/ARP_PacketEditor.cpp @@ -11,7 +11,7 @@ namespace PacketReader::ARP { - ARP_PacketEditor::ARP_PacketEditor(PayloadPtr* pkt) + ARP_PacketEditor::ARP_PacketEditor(PayloadPtrEditor* pkt) : basePkt{pkt} { } diff --git a/pcsx2/DEV9/PacketReader/ARP/ARP_PacketEditor.h b/pcsx2/DEV9/PacketReader/ARP/ARP_PacketEditor.h index 8bc3ead545..691dcaa00d 100644 --- a/pcsx2/DEV9/PacketReader/ARP/ARP_PacketEditor.h +++ b/pcsx2/DEV9/PacketReader/ARP/ARP_PacketEditor.h @@ -10,10 +10,10 @@ namespace PacketReader::ARP class ARP_PacketEditor { private: - PayloadPtr* basePkt; + PayloadPtrEditor* basePkt; public: - ARP_PacketEditor(PayloadPtr* pkt); + ARP_PacketEditor(PayloadPtrEditor* pkt); u16 GetHardwareType(); u16 GetProtocol(); diff --git a/pcsx2/DEV9/PacketReader/EthernetFrameEditor.cpp b/pcsx2/DEV9/PacketReader/EthernetFrameEditor.cpp index 1ad012f6e1..cd4d3c67d0 100644 --- a/pcsx2/DEV9/PacketReader/EthernetFrameEditor.cpp +++ b/pcsx2/DEV9/PacketReader/EthernetFrameEditor.cpp @@ -19,7 +19,7 @@ namespace PacketReader //Note: we don't have to worry about the Ethernet Frame CRC as it is not included in the packet //Note: We don't support tagged frames - payload = std::make_unique((u8*)&basePkt->buffer[14], pkt->size - headerLength); + payload = std::make_unique((u8*)&basePkt->buffer[14], pkt->size - headerLength); } MAC_Address EthernetFrameEditor::GetDestinationMAC() @@ -45,7 +45,7 @@ namespace PacketReader return ntohs(*(u16*)&basePkt->buffer[12]); } - PayloadPtr* EthernetFrameEditor::GetPayload() + PayloadPtrEditor* EthernetFrameEditor::GetPayload() { return payload.get(); } diff --git a/pcsx2/DEV9/PacketReader/EthernetFrameEditor.h b/pcsx2/DEV9/PacketReader/EthernetFrameEditor.h index e5e4151f98..f92f0aee10 100644 --- a/pcsx2/DEV9/PacketReader/EthernetFrameEditor.h +++ b/pcsx2/DEV9/PacketReader/EthernetFrameEditor.h @@ -16,7 +16,7 @@ namespace PacketReader //Length private: NetPacket* basePkt; - std::unique_ptr payload; + std::unique_ptr payload; public: EthernetFrameEditor(NetPacket* pkt); @@ -28,6 +28,6 @@ namespace PacketReader u16 GetProtocol(); - PayloadPtr* GetPayload(); + PayloadPtrEditor* GetPayload(); }; } // namespace PacketReader diff --git a/pcsx2/DEV9/PacketReader/IP/ICMP/ICMP_Packet.cpp b/pcsx2/DEV9/PacketReader/IP/ICMP/ICMP_Packet.cpp index f5da6dd86f..31cdd750ca 100644 --- a/pcsx2/DEV9/PacketReader/IP/ICMP/ICMP_Packet.cpp +++ b/pcsx2/DEV9/PacketReader/IP/ICMP/ICMP_Packet.cpp @@ -10,7 +10,7 @@ namespace PacketReader::IP::ICMP : payload{data} { } - ICMP_Packet::ICMP_Packet(u8* buffer, int bufferSize) + ICMP_Packet::ICMP_Packet(const u8* buffer, int bufferSize) { int offset = 0; //Bits 0-31 diff --git a/pcsx2/DEV9/PacketReader/IP/ICMP/ICMP_Packet.h b/pcsx2/DEV9/PacketReader/IP/ICMP/ICMP_Packet.h index 8c73d77f29..bdd1c6cf13 100644 --- a/pcsx2/DEV9/PacketReader/IP/ICMP/ICMP_Packet.h +++ b/pcsx2/DEV9/PacketReader/IP/ICMP/ICMP_Packet.h @@ -27,7 +27,7 @@ namespace PacketReader::IP::ICMP public: //Takes ownership of payload ICMP_Packet(Payload* data); - ICMP_Packet(u8* buffer, int bufferSize); + ICMP_Packet(const u8* buffer, int bufferSize); ICMP_Packet(const ICMP_Packet&); Payload* GetPayload(); diff --git a/pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp b/pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp index 8b6ce41895..fd028112b3 100644 --- a/pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp +++ b/pcsx2/DEV9/PacketReader/IP/IP_Packet.cpp @@ -66,7 +66,7 @@ namespace PacketReader::IP { } - IP_Packet::IP_Packet(u8* buffer, int bufferSize, bool fromICMP) + IP_Packet::IP_Packet(const u8* buffer, int bufferSize, bool fromICMP) { int offset = 0; diff --git a/pcsx2/DEV9/PacketReader/IP/IP_Packet.h b/pcsx2/DEV9/PacketReader/IP/IP_Packet.h index 6b134b0827..0a91f7d5a8 100644 --- a/pcsx2/DEV9/PacketReader/IP/IP_Packet.h +++ b/pcsx2/DEV9/PacketReader/IP/IP_Packet.h @@ -114,7 +114,7 @@ namespace PacketReader::IP //Takes ownership of payload IP_Packet(IP_Payload* data); - IP_Packet(u8* buffer, int bufferSize, bool fromICMP = false); + IP_Packet(const u8* buffer, int bufferSize, bool fromICMP = false); IP_Packet(const IP_Packet&); IP_Payload* GetPayload(); diff --git a/pcsx2/DEV9/PacketReader/IP/IP_Payload.h b/pcsx2/DEV9/PacketReader/IP/IP_Payload.h index 2f9a949875..3c2cd076fb 100644 --- a/pcsx2/DEV9/PacketReader/IP/IP_Payload.h +++ b/pcsx2/DEV9/PacketReader/IP/IP_Payload.h @@ -25,23 +25,21 @@ namespace PacketReader::IP std::unique_ptr data; private: - int length; - u8 protocol; + const int length; + const u8 protocol; public: IP_PayloadData(int len, u8 prot) + : length{len} + , protocol{prot} { - protocol = prot; - length = len; - if (len != 0) data = std::make_unique(len); } IP_PayloadData(const IP_PayloadData& original) + : length{original.length} + , protocol{original.protocol} { - protocol = original.protocol; - length = original.length; - if (length != 0) { data = std::make_unique(length); @@ -74,18 +72,18 @@ namespace PacketReader::IP class IP_PayloadPtr : public IP_Payload { public: - u8* data; + const u8* data; private: - int length; - u8 protocol; + const int length; + const u8 protocol; public: - IP_PayloadPtr(u8* ptr, int len, u8 prot) + IP_PayloadPtr(const u8* ptr, int len, u8 prot) + : data{ptr} + , length{len} + , protocol{prot} { - data = ptr; - length = len; - protocol = prot; } IP_PayloadPtr(const IP_PayloadPtr&) = delete; virtual int GetLength() diff --git a/pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.cpp b/pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.cpp index c8d6971f6e..c47e8ea0a5 100644 --- a/pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.cpp +++ b/pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.cpp @@ -96,7 +96,7 @@ namespace PacketReader::IP::TCP , payload{data} { } - TCP_Packet::TCP_Packet(u8* buffer, int bufferSize) + TCP_Packet::TCP_Packet(const u8* buffer, int bufferSize) { int offset = 0; //Bits 0-31 diff --git a/pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.h b/pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.h index 2539b56484..223790e0e6 100644 --- a/pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.h +++ b/pcsx2/DEV9/PacketReader/IP/TCP/TCP_Packet.h @@ -67,7 +67,7 @@ namespace PacketReader::IP::TCP //Takes ownership of payload TCP_Packet(Payload* data); - TCP_Packet(u8* buffer, int bufferSize); + TCP_Packet(const u8* buffer, int bufferSize); TCP_Packet(const TCP_Packet&); Payload* GetPayload(); diff --git a/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp b/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp index b00721f511..7850586305 100644 --- a/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp +++ b/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.cpp @@ -12,7 +12,7 @@ namespace PacketReader::IP::UDP : payload{data} { } - UDP_Packet::UDP_Packet(u8* buffer, int bufferSize) + UDP_Packet::UDP_Packet(const u8* buffer, int bufferSize) { int offset = 0; //Bits 0-31 diff --git a/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h b/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h index 6c10b08cce..65dda85a12 100644 --- a/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h +++ b/pcsx2/DEV9/PacketReader/IP/UDP/UDP_Packet.h @@ -24,7 +24,7 @@ namespace PacketReader::IP::UDP public: //Takes ownership of payload UDP_Packet(Payload* data); - UDP_Packet(u8* buffer, int bufferSize); + UDP_Packet(const u8* buffer, int bufferSize); UDP_Packet(const UDP_Packet&); Payload* GetPayload(); diff --git a/pcsx2/DEV9/PacketReader/Payload.h b/pcsx2/DEV9/PacketReader/Payload.h index f36a0040c4..0be8fd69fa 100644 --- a/pcsx2/DEV9/PacketReader/Payload.h +++ b/pcsx2/DEV9/PacketReader/Payload.h @@ -6,6 +6,7 @@ #include #include +#include "common/Assertions.h" #include "common/Pcsx2Defs.h" namespace PacketReader @@ -30,16 +31,14 @@ namespace PacketReader public: PayloadData(int len) + : length{len} { - length = len; - if (len != 0) data = std::make_unique(len); } PayloadData(const PayloadData& original) + : length{original.length} { - length = original.length; - if (length != 0) { data = std::make_unique(length); @@ -68,16 +67,16 @@ namespace PacketReader class PayloadPtr : public Payload { public: - u8* data; + const u8* data; private: - int length; + const int length; public: - PayloadPtr(u8* ptr, int len) + PayloadPtr(const u8* ptr, int len) + : data{ptr} + , length{len} { - data = ptr; - length = len; } PayloadPtr(const PayloadPtr&) = delete; virtual int GetLength() @@ -101,4 +100,36 @@ namespace PacketReader return ret; } }; + + //Pointer to bytes not owned by class, used by *Editor classes only + class PayloadPtrEditor : public Payload + { + public: + u8* data; + + private: + const int length; + + public: + PayloadPtrEditor(u8* ptr, int len) + : data{ptr} + , length{len} + { + } + PayloadPtrEditor(const PayloadPtrEditor&) = delete; + virtual int GetLength() + { + return length; + } + virtual void WriteBytes(u8* buffer, int* offset) + { + pxAssert(false); + } + virtual Payload* Clone() const + { + PayloadData* ret = new PayloadData(length); + memcpy(ret->data.get(), data, length); + return ret; + } + }; } // namespace PacketReader diff --git a/pcsx2/DEV9/Sessions/ICMP_Session/ICMP_Session.cpp b/pcsx2/DEV9/Sessions/ICMP_Session/ICMP_Session.cpp index 5ba07fdf8e..cc125cceae 100644 --- a/pcsx2/DEV9/Sessions/ICMP_Session/ICMP_Session.cpp +++ b/pcsx2/DEV9/Sessions/ICMP_Session/ICMP_Session.cpp @@ -508,10 +508,10 @@ namespace Sessions ipInfo.Ttl = parTimeToLive; DWORD ret; if (parAdapterIP.integer != 0) - ret = IcmpSendEcho2Ex(icmpFile, icmpEvent, nullptr, nullptr, parAdapterIP.integer, parDestIP.integer, parPayload->data, parPayload->GetLength(), &ipInfo, icmpResponseBuffer.get(), icmpResponseBufferLen, + ret = IcmpSendEcho2Ex(icmpFile, icmpEvent, nullptr, nullptr, parAdapterIP.integer, parDestIP.integer, const_cast(parPayload->data), parPayload->GetLength(), &ipInfo, icmpResponseBuffer.get(), icmpResponseBufferLen, static_cast(std::chrono::duration_cast(ICMP_TIMEOUT).count())); else - ret = IcmpSendEcho2(icmpFile, icmpEvent, nullptr, nullptr, parDestIP.integer, parPayload->data, parPayload->GetLength(), &ipInfo, icmpResponseBuffer.get(), icmpResponseBufferLen, + ret = IcmpSendEcho2(icmpFile, icmpEvent, nullptr, nullptr, parDestIP.integer, const_cast(parPayload->data), parPayload->GetLength(), &ipInfo, icmpResponseBuffer.get(), icmpResponseBufferLen, static_cast(std::chrono::duration_cast(ICMP_TIMEOUT).count())); // Documentation states that IcmpSendEcho2 returns ERROR_IO_PENDING diff --git a/pcsx2/DEV9/Sessions/ICMP_Session/ICMP_Session.h b/pcsx2/DEV9/Sessions/ICMP_Session/ICMP_Session.h index 64f7ca66b4..9c70853807 100644 --- a/pcsx2/DEV9/Sessions/ICMP_Session/ICMP_Session.h +++ b/pcsx2/DEV9/Sessions/ICMP_Session/ICMP_Session.h @@ -23,7 +23,7 @@ namespace Sessions int type; int code; int dataLength; - u8* data; + const u8* data; }; class Ping diff --git a/pcsx2/DEV9/pcap_io.cpp b/pcsx2/DEV9/pcap_io.cpp index 62fa8c1287..3664a778c9 100644 --- a/pcsx2/DEV9/pcap_io.cpp +++ b/pcsx2/DEV9/pcap_io.cpp @@ -342,7 +342,7 @@ void PCAPAdapter::SetMACBridgedRecv(NetPacket* pkt) if (frame.GetProtocol() == static_cast(EtherType::IPv4)) // IP { // Compare DEST IP in IP with the PS2's IP, if they match, change DEST MAC to ps2MAC. - PayloadPtr* payload = frame.GetPayload(); + PayloadPtrEditor* payload = frame.GetPayload(); IP_Packet ippkt(payload->data, payload->GetLength()); if (ippkt.destinationIP == ps2IP) frame.SetDestinationMAC(ps2MAC); @@ -364,7 +364,7 @@ void PCAPAdapter::SetMACBridgedSend(NetPacket* pkt) EthernetFrameEditor frame(pkt); if (frame.GetProtocol() == static_cast(EtherType::IPv4)) // IP { - PayloadPtr* payload = frame.GetPayload(); + PayloadPtrEditor* payload = frame.GetPayload(); IP_Packet ippkt(payload->data, payload->GetLength()); ps2IP = ippkt.sourceIP; } @@ -403,7 +403,7 @@ void PCAPAdapter::HandleFrameCheckSequence(NetPacket* pkt) int payloadSize = -1; if (frame.GetProtocol() == static_cast(EtherType::IPv4)) // IP { - PayloadPtr* payload = frame.GetPayload(); + PayloadPtrEditor* payload = frame.GetPayload(); IP_Packet ippkt(payload->data, payload->GetLength()); payloadSize = ippkt.GetLength(); }