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
{
ARP_PacketEditor::ARP_PacketEditor(PayloadPtr* pkt)
ARP_PacketEditor::ARP_PacketEditor(PayloadPtrEditor* pkt)
: basePkt{pkt}
{
}

View File

@ -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();

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 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()
@ -45,7 +45,7 @@ namespace PacketReader
return ntohs(*(u16*)&basePkt->buffer[12]);
}
PayloadPtr* EthernetFrameEditor::GetPayload()
PayloadPtrEditor* EthernetFrameEditor::GetPayload()
{
return payload.get();
}

View File

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

View File

@ -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

View File

@ -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();

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;

View File

@ -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();

View File

@ -25,23 +25,21 @@ namespace PacketReader::IP
std::unique_ptr<u8[]> 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<u8[]>(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<u8[]>(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()

View File

@ -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

View File

@ -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();

View File

@ -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

View File

@ -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();

View File

@ -6,6 +6,7 @@
#include <cstring>
#include <memory>
#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<u8[]>(len);
}
PayloadData(const PayloadData& original)
: length{original.length}
{
length = original.length;
if (length != 0)
{
data = std::make_unique<u8[]>(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

View File

@ -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<u8*>(parPayload->data), parPayload->GetLength(), &ipInfo, icmpResponseBuffer.get(), icmpResponseBufferLen,
static_cast<DWORD>(std::chrono::duration_cast<std::chrono::milliseconds>(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<u8*>(parPayload->data), parPayload->GetLength(), &ipInfo, icmpResponseBuffer.get(), icmpResponseBufferLen,
static_cast<DWORD>(std::chrono::duration_cast<std::chrono::milliseconds>(ICMP_TIMEOUT).count()));
// Documentation states that IcmpSendEcho2 returns ERROR_IO_PENDING

View File

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

View File

@ -342,7 +342,7 @@ void PCAPAdapter::SetMACBridgedRecv(NetPacket* pkt)
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.
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<u16>(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<u16>(EtherType::IPv4)) // IP
{
PayloadPtr* payload = frame.GetPayload();
PayloadPtrEditor* payload = frame.GetPayload();
IP_Packet ippkt(payload->data, payload->GetLength());
payloadSize = ippkt.GetLength();
}