DEV9: Apply const to basic payload types

Propagate const into PacketReader classes as needed
Provide non-const editor version for pcap
This commit is contained in:
TheLastRar 2024-12-05 16:37:28 +00:00 committed by lightningterror
parent d7937943b0
commit a8a170ebe6
17 changed files with 74 additions and 45 deletions

View File

@ -11,7 +11,7 @@
namespace PacketReader::ARP namespace PacketReader::ARP
{ {
ARP_PacketEditor::ARP_PacketEditor(PayloadPtr* pkt) ARP_PacketEditor::ARP_PacketEditor(PayloadPtrEditor* pkt)
: basePkt{pkt} : basePkt{pkt}
{ {
} }

View File

@ -10,10 +10,10 @@ namespace PacketReader::ARP
class ARP_PacketEditor class ARP_PacketEditor
{ {
private: private:
PayloadPtr* basePkt; PayloadPtrEditor* basePkt;
public: public:
ARP_PacketEditor(PayloadPtr* pkt); ARP_PacketEditor(PayloadPtrEditor* pkt);
u16 GetHardwareType(); u16 GetHardwareType();
u16 GetProtocol(); u16 GetProtocol();

View File

@ -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 have to worry about the Ethernet Frame CRC as it is not included in the packet
//Note: We don't support tagged frames //Note: We don't support tagged frames
payload = std::make_unique<PayloadPtr>((u8*)&basePkt->buffer[14], pkt->size - headerLength); payload = std::make_unique<PayloadPtrEditor>((u8*)&basePkt->buffer[14], pkt->size - headerLength);
} }
MAC_Address EthernetFrameEditor::GetDestinationMAC() MAC_Address EthernetFrameEditor::GetDestinationMAC()
@ -45,7 +45,7 @@ namespace PacketReader
return ntohs(*(u16*)&basePkt->buffer[12]); return ntohs(*(u16*)&basePkt->buffer[12]);
} }
PayloadPtr* EthernetFrameEditor::GetPayload() PayloadPtrEditor* EthernetFrameEditor::GetPayload()
{ {
return payload.get(); return payload.get();
} }

View File

@ -16,7 +16,7 @@ namespace PacketReader
//Length //Length
private: private:
NetPacket* basePkt; NetPacket* basePkt;
std::unique_ptr<PayloadPtr> payload; std::unique_ptr<PayloadPtrEditor> payload;
public: public:
EthernetFrameEditor(NetPacket* pkt); EthernetFrameEditor(NetPacket* pkt);
@ -28,6 +28,6 @@ namespace PacketReader
u16 GetProtocol(); u16 GetProtocol();
PayloadPtr* GetPayload(); PayloadPtrEditor* GetPayload();
}; };
} // namespace PacketReader } // namespace PacketReader

View File

@ -10,7 +10,7 @@ namespace PacketReader::IP::ICMP
: payload{data} : payload{data}
{ {
} }
ICMP_Packet::ICMP_Packet(u8* buffer, int bufferSize) ICMP_Packet::ICMP_Packet(const u8* buffer, int bufferSize)
{ {
int offset = 0; int offset = 0;
//Bits 0-31 //Bits 0-31

View File

@ -27,7 +27,7 @@ namespace PacketReader::IP::ICMP
public: public:
//Takes ownership of payload //Takes ownership of payload
ICMP_Packet(Payload* data); ICMP_Packet(Payload* data);
ICMP_Packet(u8* buffer, int bufferSize); ICMP_Packet(const u8* buffer, int bufferSize);
ICMP_Packet(const ICMP_Packet&); ICMP_Packet(const ICMP_Packet&);
Payload* GetPayload(); Payload* GetPayload();

View File

@ -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; int offset = 0;

View File

@ -114,7 +114,7 @@ namespace PacketReader::IP
//Takes ownership of payload //Takes ownership of payload
IP_Packet(IP_Payload* data); 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_Packet(const IP_Packet&);
IP_Payload* GetPayload(); IP_Payload* GetPayload();

View File

@ -25,23 +25,21 @@ namespace PacketReader::IP
std::unique_ptr<u8[]> data; std::unique_ptr<u8[]> data;
private: private:
int length; const int length;
u8 protocol; const u8 protocol;
public: public:
IP_PayloadData(int len, u8 prot) IP_PayloadData(int len, u8 prot)
: length{len}
, protocol{prot}
{ {
protocol = prot;
length = len;
if (len != 0) if (len != 0)
data = std::make_unique<u8[]>(len); data = std::make_unique<u8[]>(len);
} }
IP_PayloadData(const IP_PayloadData& original) IP_PayloadData(const IP_PayloadData& original)
: length{original.length}
, protocol{original.protocol}
{ {
protocol = original.protocol;
length = original.length;
if (length != 0) if (length != 0)
{ {
data = std::make_unique<u8[]>(length); data = std::make_unique<u8[]>(length);
@ -74,18 +72,18 @@ namespace PacketReader::IP
class IP_PayloadPtr : public IP_Payload class IP_PayloadPtr : public IP_Payload
{ {
public: public:
u8* data; const u8* data;
private: private:
int length; const int length;
u8 protocol; const u8 protocol;
public: 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; IP_PayloadPtr(const IP_PayloadPtr&) = delete;
virtual int GetLength() virtual int GetLength()

View File

@ -96,7 +96,7 @@ namespace PacketReader::IP::TCP
, payload{data} , payload{data}
{ {
} }
TCP_Packet::TCP_Packet(u8* buffer, int bufferSize) TCP_Packet::TCP_Packet(const u8* buffer, int bufferSize)
{ {
int offset = 0; int offset = 0;
//Bits 0-31 //Bits 0-31

View File

@ -67,7 +67,7 @@ namespace PacketReader::IP::TCP
//Takes ownership of payload //Takes ownership of payload
TCP_Packet(Payload* data); TCP_Packet(Payload* data);
TCP_Packet(u8* buffer, int bufferSize); TCP_Packet(const u8* buffer, int bufferSize);
TCP_Packet(const TCP_Packet&); TCP_Packet(const TCP_Packet&);
Payload* GetPayload(); Payload* GetPayload();

View File

@ -12,7 +12,7 @@ namespace PacketReader::IP::UDP
: payload{data} : payload{data}
{ {
} }
UDP_Packet::UDP_Packet(u8* buffer, int bufferSize) UDP_Packet::UDP_Packet(const u8* buffer, int bufferSize)
{ {
int offset = 0; int offset = 0;
//Bits 0-31 //Bits 0-31

View File

@ -24,7 +24,7 @@ namespace PacketReader::IP::UDP
public: public:
//Takes ownership of payload //Takes ownership of payload
UDP_Packet(Payload* data); UDP_Packet(Payload* data);
UDP_Packet(u8* buffer, int bufferSize); UDP_Packet(const u8* buffer, int bufferSize);
UDP_Packet(const UDP_Packet&); UDP_Packet(const UDP_Packet&);
Payload* GetPayload(); Payload* GetPayload();

View File

@ -6,6 +6,7 @@
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include "common/Assertions.h"
#include "common/Pcsx2Defs.h" #include "common/Pcsx2Defs.h"
namespace PacketReader namespace PacketReader
@ -30,16 +31,14 @@ namespace PacketReader
public: public:
PayloadData(int len) PayloadData(int len)
: length{len}
{ {
length = len;
if (len != 0) if (len != 0)
data = std::make_unique<u8[]>(len); data = std::make_unique<u8[]>(len);
} }
PayloadData(const PayloadData& original) PayloadData(const PayloadData& original)
: length{original.length}
{ {
length = original.length;
if (length != 0) if (length != 0)
{ {
data = std::make_unique<u8[]>(length); data = std::make_unique<u8[]>(length);
@ -68,16 +67,16 @@ namespace PacketReader
class PayloadPtr : public Payload class PayloadPtr : public Payload
{ {
public: public:
u8* data; const u8* data;
private: private:
int length; const int length;
public: public:
PayloadPtr(u8* ptr, int len) PayloadPtr(const u8* ptr, int len)
: data{ptr}
, length{len}
{ {
data = ptr;
length = len;
} }
PayloadPtr(const PayloadPtr&) = delete; PayloadPtr(const PayloadPtr&) = delete;
virtual int GetLength() virtual int GetLength()
@ -101,4 +100,36 @@ namespace PacketReader
return ret; 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 } // namespace PacketReader

View File

@ -508,10 +508,10 @@ namespace Sessions
ipInfo.Ttl = parTimeToLive; ipInfo.Ttl = parTimeToLive;
DWORD ret; DWORD ret;
if (parAdapterIP.integer != 0) 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<u8*>(parPayload->data), parPayload->GetLength(), &ipInfo, icmpResponseBuffer.get(), icmpResponseBufferLen,
static_cast<DWORD>(std::chrono::duration_cast<std::chrono::milliseconds>(ICMP_TIMEOUT).count())); static_cast<DWORD>(std::chrono::duration_cast<std::chrono::milliseconds>(ICMP_TIMEOUT).count()));
else 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<u8*>(parPayload->data), parPayload->GetLength(), &ipInfo, icmpResponseBuffer.get(), icmpResponseBufferLen,
static_cast<DWORD>(std::chrono::duration_cast<std::chrono::milliseconds>(ICMP_TIMEOUT).count())); static_cast<DWORD>(std::chrono::duration_cast<std::chrono::milliseconds>(ICMP_TIMEOUT).count()));
// Documentation states that IcmpSendEcho2 returns ERROR_IO_PENDING // Documentation states that IcmpSendEcho2 returns ERROR_IO_PENDING

View File

@ -23,7 +23,7 @@ namespace Sessions
int type; int type;
int code; int code;
int dataLength; int dataLength;
u8* data; const u8* data;
}; };
class Ping class Ping

View File

@ -342,7 +342,7 @@ void PCAPAdapter::SetMACBridgedRecv(NetPacket* pkt)
if (frame.GetProtocol() == static_cast<u16>(EtherType::IPv4)) // IP if (frame.GetProtocol() == static_cast<u16>(EtherType::IPv4)) // IP
{ {
// Compare DEST IP in IP with the PS2's IP, if they match, change DEST MAC to ps2MAC. // 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()); IP_Packet ippkt(payload->data, payload->GetLength());
if (ippkt.destinationIP == ps2IP) if (ippkt.destinationIP == ps2IP)
frame.SetDestinationMAC(ps2MAC); frame.SetDestinationMAC(ps2MAC);
@ -364,7 +364,7 @@ void PCAPAdapter::SetMACBridgedSend(NetPacket* pkt)
EthernetFrameEditor frame(pkt); EthernetFrameEditor frame(pkt);
if (frame.GetProtocol() == static_cast<u16>(EtherType::IPv4)) // IP if (frame.GetProtocol() == static_cast<u16>(EtherType::IPv4)) // IP
{ {
PayloadPtr* payload = frame.GetPayload(); PayloadPtrEditor* payload = frame.GetPayload();
IP_Packet ippkt(payload->data, payload->GetLength()); IP_Packet ippkt(payload->data, payload->GetLength());
ps2IP = ippkt.sourceIP; ps2IP = ippkt.sourceIP;
} }
@ -403,7 +403,7 @@ void PCAPAdapter::HandleFrameCheckSequence(NetPacket* pkt)
int payloadSize = -1; int payloadSize = -1;
if (frame.GetProtocol() == static_cast<u16>(EtherType::IPv4)) // IP if (frame.GetProtocol() == static_cast<u16>(EtherType::IPv4)) // IP
{ {
PayloadPtr* payload = frame.GetPayload(); PayloadPtrEditor* payload = frame.GetPayload();
IP_Packet ippkt(payload->data, payload->GetLength()); IP_Packet ippkt(payload->data, payload->GetLength());
payloadSize = ippkt.GetLength(); payloadSize = ippkt.GetLength();
} }