1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 06:27:45 +00:00

Cleanup cleanup cleanup

This commit is contained in:
Mordred 2018-06-03 19:31:03 +02:00
parent f6375f326e
commit 04671e2e24
8 changed files with 82 additions and 118 deletions

View file

@ -1,6 +1,3 @@
/**
* Structural definitions common to all FFXIV:ARR packets.
*/
#ifndef _CORE_NETWORK_PACKETS_COMMON_H #ifndef _CORE_NETWORK_PACKETS_COMMON_H
#define _CORE_NETWORK_PACKETS_COMMON_H #define _CORE_NETWORK_PACKETS_COMMON_H
@ -25,7 +22,7 @@ namespace Packets {
* *
* std::stringstream buf; * std::stringstream buf;
* buf << pkt_hdr; * buf << pkt_hdr;
* for (int i = 0; i < n; i++) * for( int i = 0; i < n; i++ )
* { * {
* buf << pkt_seg_hdr[i]; * buf << pkt_seg_hdr[i];
* buf << {pkt_seg_data[i]}; * buf << {pkt_seg_data[i]};
@ -46,6 +43,8 @@ namespace Packets {
* *
* 0 4 8 12 14 16 * 0 4 8 12 14 16
* +-------------------------------+---------------+-------+-------+ * +-------------------------------+---------------+-------+-------+
* | unknown_0 | unknown_8 |
* +-------------------------------+---------------+-------+-------+
* | timestamp | size | cType | count | * | timestamp | size | cType | count |
* +---+---+-------+---------------+---------------+-------+-------+ * +---+---+-------+---------------+---------------+-------+-------+
* | ? |CMP| ? | ? | * | ? |CMP| ? | ? |
@ -54,13 +53,10 @@ namespace Packets {
*/ */
struct FFXIVARR_PACKET_HEADER struct FFXIVARR_PACKET_HEADER
{ {
/** Unknown data, no actual use has been determined */
uint64_t unknown_0; uint64_t unknown_0;
uint64_t unknown_8; uint64_t unknown_8;
/** /** Represents the number of milliseconds since epoch that the packet was sent. */
* Represents the number of milliseconds since epoch that the packet was
* sent.
*/
uint64_t timestamp; uint64_t timestamp;
/** The size of the packet header and its payload */ /** The size of the packet header and its payload */
uint32_t size; uint32_t size;
@ -74,14 +70,14 @@ struct FFXIVARR_PACKET_HEADER
uint32_t unknown_24; uint32_t unknown_24;
}; };
inline ostream& operator<<(ostream& os, const FFXIVARR_PACKET_HEADER& hdr) inline ostream& operator << ( ostream& os, const FFXIVARR_PACKET_HEADER& hdr )
{ {
return os.write(reinterpret_cast<const char*>(&hdr), sizeof hdr); return os.write( reinterpret_cast< const char* >( &hdr ), sizeof hdr );
} }
inline istream& operator>>(istream& is, FFXIVARR_PACKET_HEADER& hdr) inline istream& operator >> ( istream& is, FFXIVARR_PACKET_HEADER& hdr )
{ {
return is.read(reinterpret_cast<char*>(&hdr), sizeof hdr); return is.read( reinterpret_cast< char* >( &hdr ), sizeof hdr );
} }
/** /**
@ -92,7 +88,7 @@ inline istream& operator>>(istream& is, FFXIVARR_PACKET_HEADER& hdr)
* *
* 0 4 8 12 16 * 0 4 8 12 16
* +---------------+---------------+---------------+-------+-------+ * +---------------+---------------+---------------+-------+-------+
* | size | source_actor | target_actor | type | ? | * | size | source_actor | target_actor | type | pad |
* +---------------+---------------+---------------+-------+-------+ * +---------------+---------------+---------------+-------+-------+
* | | * | |
* : type-specific data of length, size, follows : * : type-specific data of length, size, follows :
@ -109,22 +105,20 @@ struct FFXIVARR_PACKET_SEGMENT_HEADER
uint32_t target_actor; uint32_t target_actor;
/** The segment type. (1, 2, 3, 7, 8, 9, 10) */ /** The segment type. (1, 2, 3, 7, 8, 9, 10) */
uint16_t type; uint16_t type;
uint16_t _reserved_E; uint16_t padding;
}; };
inline ostream& operator<<(ostream& os, const FFXIVARR_PACKET_SEGMENT_HEADER& hdr) inline ostream& operator << ( ostream& os, const FFXIVARR_PACKET_SEGMENT_HEADER& hdr )
{ {
return os.write(reinterpret_cast<const char*>(&hdr), sizeof hdr); return os.write( reinterpret_cast< const char* >( &hdr ), sizeof hdr );
} }
inline istream& operator>>(istream& is, FFXIVARR_PACKET_SEGMENT_HEADER& hdr) inline istream& operator >> ( istream& is, FFXIVARR_PACKET_SEGMENT_HEADER& hdr )
{ {
return is.read(reinterpret_cast<char*>(&hdr), sizeof hdr); return is.read( reinterpret_cast< char* >( &hdr ), sizeof hdr );
} }
// TODO: Include structures for the individual packet segment types template < int T > struct FFXIVIpcBasePacket
template <int T> struct FFXIVIpcBasePacket
{ {
/** Creates a constant representing the IPC type */ /** Creates a constant representing the IPC type */
enum { _ServerIpcType = T }; enum { _ServerIpcType = T };
@ -133,7 +127,7 @@ template <int T> struct FFXIVIpcBasePacket
struct FFXIVARR_PACKET_RAW struct FFXIVARR_PACKET_RAW
{ {
FFXIVARR_PACKET_SEGMENT_HEADER segHdr; FFXIVARR_PACKET_SEGMENT_HEADER segHdr;
std::vector<uint8_t> data; std::vector< uint8_t > data;
}; };
/** /**
@ -142,7 +136,7 @@ struct FFXIVARR_PACKET_RAW
* *
* 0 4 6 8 12 16 * 0 4 6 8 12 16
* +-------+-------+------+----------+---------------+---------------+ * +-------+-------+------+----------+---------------+---------------+
* | 14 00 | type | ?? | serverId | timestamp | ??? | * | 14 00 | type | pad | serverId | timestamp | pad1 |
* +-------+-------+------+----------+---------------+---------------+ * +-------+-------+------+----------+---------------+---------------+
* | | * | |
* : data : * : data :
@ -153,24 +147,22 @@ struct FFXIVARR_IPC_HEADER
{ {
uint16_t reserved; uint16_t reserved;
uint16_t type; uint16_t type;
uint16_t unknown_2; uint16_t padding;
uint16_t serverId; uint16_t serverId;
uint32_t timestamp; uint32_t timestamp;
uint32_t unknown_C; uint32_t padding1;
}; };
inline ostream& operator<<(ostream& os, const FFXIVARR_IPC_HEADER& hdr) inline ostream& operator << ( ostream& os, const FFXIVARR_IPC_HEADER& hdr )
{ {
return os.write(reinterpret_cast<const char*>(&hdr), sizeof hdr); return os.write( reinterpret_cast< const char* >( &hdr ), sizeof hdr );
} }
inline istream& operator>>(istream& is, FFXIVARR_IPC_HEADER& hdr) inline istream& operator >> ( istream& is, FFXIVARR_IPC_HEADER& hdr )
{ {
return is.read(reinterpret_cast<char*>(&hdr), sizeof hdr); return is.read( reinterpret_cast< char* >( &hdr ), sizeof hdr );
} }
} /* Packets */ } /* Packets */
} /* Network */ } /* Network */
} /* Core */ } /* Core */

View file

@ -88,7 +88,7 @@ Core::Network::Packets::GamePacket::~GamePacket()
void Core::Network::Packets::GamePacket::savePacket() void Core::Network::Packets::GamePacket::savePacket()
{ {
char filename[20]; char filename[20];
sprintf( filename, "dump_0x%x.dat", m_subType ); sprintf( filename, "dump_0x%x_%i.dat", m_subType, Util::getTimeMs() );
FILE * fp = nullptr; FILE * fp = nullptr;
fp = fopen( filename, "wb" ); fp = fopen( filename, "wb" );
fwrite( &m_dataBuf[0], 1, m_segHdr.size, fp ); fwrite( &m_dataBuf[0], 1, m_segHdr.size, fp );

View file

@ -16,11 +16,11 @@ namespace Packets {
// Must forward define these in order to enable the compiler to produce the // Must forward define these in order to enable the compiler to produce the
// correct template functions. // correct template functions.
template <typename T, typename T1> template < typename T, typename T1 >
class GamePacketNew; class GamePacketNew;
template <typename T, typename T1> template < typename T, typename T1 >
std::ostream& operator<< ( std::ostream& os, const GamePacketNew<T, T1>& packet ); std::ostream& operator<< ( std::ostream& os, const GamePacketNew< T, T1 >& packet );
template< class T > template< class T >
using ZoneChannelPacket = GamePacketNew< T, ServerZoneIpcType >; using ZoneChannelPacket = GamePacketNew< T, ServerZoneIpcType >;
@ -31,11 +31,11 @@ using ChatChannelPacket = GamePacketNew< T, ServerChatIpcType >;
/** /**
* The base implementation of a game packet. Needed for parsing packets. * The base implementation of a game packet. Needed for parsing packets.
*/ */
template <typename T1> template < typename T1 >
class GamePacketNewBase class FFXIVPacketBase
{ {
public: public:
virtual ~GamePacketNewBase() = default; virtual ~FFXIVPacketBase() = default;
/** /**
* @brief Gets the IPC type of this packet. (Useful for determining the * @brief Gets the IPC type of this packet. (Useful for determining the
* type of a parsed packet.) * type of a parsed packet.)
@ -49,8 +49,8 @@ public:
* type that represents just the IPC data portion (the bytes after the initial * type that represents just the IPC data portion (the bytes after the initial
* 32 byte header information.) * 32 byte header information.)
*/ */
template <typename T, typename T1> template < typename T, typename T1 >
class GamePacketNew : public GamePacketNewBase<T1> class GamePacketNew : public FFXIVPacketBase< T1 >
{ {
public: public:
/** /**
@ -58,7 +58,7 @@ public:
* @param sourceActorId The source actor id. * @param sourceActorId The source actor id.
* @param targetActorId The target actor id. * @param targetActorId The target actor id.
*/ */
GamePacketNew<T, T1>( uint32_t sourceActorId, uint32_t targetActorId ) GamePacketNew< T, T1 >( uint32_t sourceActorId, uint32_t targetActorId )
{ {
initialize(); initialize();
m_segHdr.source_actor = sourceActorId; m_segHdr.source_actor = sourceActorId;
@ -69,7 +69,7 @@ public:
* @brief Constructs a new game packet with the specified actors. * @brief Constructs a new game packet with the specified actors.
* @param sourceActorId The source and target actor id. * @param sourceActorId The source and target actor id.
*/ */
GamePacketNew<T, T1>( uint32_t bothActorId ) GamePacketNew< T, T1 >( uint32_t bothActorId )
{ {
initialize(); initialize();
m_segHdr.source_actor = bothActorId; m_segHdr.source_actor = bothActorId;
@ -109,7 +109,7 @@ public:
* @param actorId The source actor id. * @param actorId The source actor id.
* @return This IPC packet object (can be used for chaining). * @return This IPC packet object (can be used for chaining).
*/ */
GamePacketNew<T, T1> sourceActor( uint32_t actorId ) GamePacketNew< T, T1 > sourceActor( uint32_t actorId )
{ {
m_segHdr.source_actor = actorId; m_segHdr.source_actor = actorId;
return this; return this;
@ -129,7 +129,7 @@ public:
* @param actorId The target actor id. * @param actorId The target actor id.
* @return This IPC packet object (can be used for chaining). * @return This IPC packet object (can be used for chaining).
*/ */
GamePacketNew<T, T1> targetActor( uint32_t actorId ) GamePacketNew< T, T1 > targetActor( uint32_t actorId )
{ {
m_segHdr.target_actor = actorId; m_segHdr.target_actor = actorId;
return this; return this;
@ -144,7 +144,7 @@ public:
return m_segHdr.target_actor; return m_segHdr.target_actor;
}; };
friend std::ostream& operator<< <> ( std::ostream& os, const GamePacketNew<T, T1>& packet ); friend std::ostream& operator<< <> ( std::ostream& os, const GamePacketNew< T, T1 >& packet );
friend class GamePacketFactory; friend class GamePacketFactory;
@ -205,7 +205,7 @@ private:
}; };
}; };
template <typename T, typename T1> template < typename T, typename T1 >
std::ostream& operator<<( std::ostream& os, const GamePacketNew<T, T1>& packet ) std::ostream& operator<<( std::ostream& os, const GamePacketNew<T, T1>& packet )
{ {
#if 0 #if 0

View file

@ -11,80 +11,67 @@ namespace Core
{ {
namespace Packets namespace Packets
{ {
PacketParseResult getHeader( PacketParseResult getHeader( const std::vector< uint8_t > &buffer,
const std::vector< uint8_t > &buffer, const uint32_t offset,
const uint32_t offset, FFXIVARR_PACKET_HEADER &header )
FFXIVARR_PACKET_HEADER &header)
{ {
const auto headerSize = sizeof( FFXIVARR_PACKET_HEADER ); const auto headerSize = sizeof( FFXIVARR_PACKET_HEADER );
// Check if we have enough bytes in the buffer. // Check if we have enough bytes in the buffer.
auto remainingBytes = buffer.size() - offset; auto remainingBytes = buffer.size() - offset;
if ( remainingBytes < headerSize ) if( remainingBytes < headerSize )
{
return Incomplete; return Incomplete;
}
// Copy packet header. // Copy packet header.
memcpy( &header, buffer.data() + offset, headerSize ); memcpy( &header, buffer.data() + offset, headerSize );
if ( !checkHeader(header) ) if( !checkHeader(header) )
{
return Malformed; return Malformed;
}
return Success; return Success;
} }
PacketParseResult getSegmentHeader( PacketParseResult getSegmentHeader( const std::vector< uint8_t > &buffer,
const std::vector< uint8_t > &buffer, const uint32_t offset,
const uint32_t offset, FFXIVARR_PACKET_SEGMENT_HEADER &header )
FFXIVARR_PACKET_SEGMENT_HEADER &header)
{ {
const auto headerSize = sizeof( FFXIVARR_PACKET_SEGMENT_HEADER ); const auto headerSize = sizeof( FFXIVARR_PACKET_SEGMENT_HEADER );
// Check if we have enough bytes in the buffer. // Check if we have enough bytes in the buffer.
auto remainingBytes = buffer.size() - offset; auto remainingBytes = buffer.size() - offset;
if (remainingBytes < headerSize) if( remainingBytes < headerSize )
{
return Incomplete; return Incomplete;
}
// Copy segment header // Copy segment header
memcpy(&header, buffer.data() + offset, headerSize); memcpy( &header, buffer.data() + offset, headerSize );
return Success; return Success;
} }
PacketParseResult getPackets( PacketParseResult getPackets( const std::vector< uint8_t > &buffer,
const std::vector< uint8_t > &buffer, const uint32_t offset,
const uint32_t offset, const FFXIVARR_PACKET_HEADER &packetHeader,
const FFXIVARR_PACKET_HEADER &packetHeader, std::vector< FFXIVARR_PACKET_RAW > &packets )
std::vector< FFXIVARR_PACKET_RAW > &packets)
{ {
// sanity check: check there's enough bytes in the buffer // sanity check: check there's enough bytes in the buffer
const auto bytesExpected = packetHeader.size - sizeof(struct FFXIVARR_PACKET_HEADER); const auto bytesExpected = packetHeader.size - sizeof( struct FFXIVARR_PACKET_HEADER );
if ( buffer.size() - offset < bytesExpected ) if( buffer.size() - offset < bytesExpected )
{
return Incomplete; return Incomplete;
}
// Loop each message // Loop each message
uint32_t count = 0; uint32_t count = 0;
uint32_t bytesProcessed = 0; uint32_t bytesProcessed = 0;
while ( count < packetHeader.count ) while( count < packetHeader.count )
{ {
FFXIVARR_PACKET_RAW rawPacket; FFXIVARR_PACKET_RAW rawPacket;
// Copy ipc packet message // Copy ipc packet message
const auto packetResult = getPacket(buffer, offset + bytesProcessed, rawPacket); const auto packetResult = getPacket( buffer, offset + bytesProcessed, rawPacket );
if ( packetResult != Success ) if( packetResult != Success )
{
return packetResult; return packetResult;
}
// NOTE: isn't rawPacket is allocated on stack? // NOTE: isn't rawPacket is allocated on stack?
// why is okay to do this? // why is okay to do this?
packets.push_back( rawPacket ); packets.push_back( rawPacket );
// Add message size and count // Add message size and count
@ -94,34 +81,25 @@ namespace Core
// sanity check: check if we processed all bytes. // sanity check: check if we processed all bytes.
// this check can fail if size of messages don't add up to size reported from packet header. // this check can fail if size of messages don't add up to size reported from packet header.
if ( bytesExpected != bytesProcessed ) if( bytesExpected != bytesProcessed )
{
return Malformed; return Malformed;
}
return Success; return Success;
} }
PacketParseResult getPacket( PacketParseResult getPacket( const std::vector< uint8_t > &buffer, const uint32_t offset,
const std::vector< uint8_t > &buffer, FFXIVARR_PACKET_RAW &packet )
const uint32_t offset,
FFXIVARR_PACKET_RAW &packet
)
{ {
// Copy segment header // Copy segment header
const auto headerResult = getSegmentHeader(buffer, offset, packet.segHdr); const auto headerResult = getSegmentHeader( buffer, offset, packet.segHdr );
if ( headerResult != Success ) if( headerResult != Success )
{
return headerResult; return headerResult;
}
// Check header sanity and it's size // Check header sanity and it's size
if ( !checkSegmentHeader( packet.segHdr ) ) if( !checkSegmentHeader( packet.segHdr ) )
{
return Malformed; return Malformed;
}
const auto dataOffset = offset + sizeof(struct FFXIVARR_PACKET_SEGMENT_HEADER); const auto dataOffset = offset + sizeof( struct FFXIVARR_PACKET_SEGMENT_HEADER );
const auto dataSize = packet.segHdr.size; const auto dataSize = packet.segHdr.size;
// Allocate data buffer and copy // Allocate data buffer and copy
@ -131,30 +109,24 @@ namespace Core
return Success; return Success;
} }
bool checkHeader(const FFXIVARR_PACKET_HEADER &header) bool checkHeader( const FFXIVARR_PACKET_HEADER &header )
{ {
// Max size of the packet is capped at 1MB for now. // Max size of the packet is capped at 1MB for now.
if ( header.size > 1 * 1024 * 1024 ) if( header.size > 1 * 1024 * 1024 )
{
return false; return false;
}
// Max number of message is capped at 255 for now. // Max number of message is capped at 255 for now.
if ( header.count > 255 ) if( header.count > 255 )
{
return false; return false;
}
return true; return true;
} }
bool checkSegmentHeader(const FFXIVARR_PACKET_SEGMENT_HEADER &header) bool checkSegmentHeader( const FFXIVARR_PACKET_SEGMENT_HEADER &header )
{ {
// Max size of individual message is capped at 256KB for now. // Max size of individual message is capped at 256KB for now.
if ( header.size > 256 * 1024 ) if( header.size > 256 * 1024 )
{
return false; return false;
}
return true; return true;
} }

View file

@ -7,14 +7,14 @@
#include <chrono> #include <chrono>
Core::Network::Packets::PacketContainer::PacketContainer( void ) Core::Network::Packets::PacketContainer::PacketContainer()
{ {
memset( &m_ipcHdr, 0, sizeof( FFXIVARR_PACKET_HEADER ) ); memset( &m_ipcHdr, 0, sizeof( FFXIVARR_PACKET_HEADER ) );
m_ipcHdr.size = sizeof( FFXIVARR_PACKET_HEADER ); m_ipcHdr.size = sizeof( FFXIVARR_PACKET_HEADER );
m_ipcHdr.count = 0; m_ipcHdr.count = 0;
} }
Core::Network::Packets::PacketContainer::~PacketContainer( void ) Core::Network::Packets::PacketContainer::~PacketContainer()
{ {
m_entryList.clear(); m_entryList.clear();
} }

View file

@ -16,14 +16,14 @@ class GamePacket;
class PacketContainer class PacketContainer
{ {
public: public:
PacketContainer( void ); PacketContainer();
~PacketContainer( void ); ~PacketContainer();
void addPacket( GamePacket pEntry ); void addPacket( GamePacket pEntry );
FFXIVARR_PACKET_HEADER m_ipcHdr; FFXIVARR_PACKET_HEADER m_ipcHdr;
std::vector<GamePacket> m_entryList; std::vector< GamePacket > m_entryList;
std::string toString(); std::string toString();

View file

@ -7,7 +7,6 @@
#include <Network/GamePacket.h> #include <Network/GamePacket.h>
#include <Network/GamePacketNew.h> #include <Network/GamePacketNew.h>
#include <Network/PacketDef/Zone/ServerZoneDef.h> #include <Network/PacketDef/Zone/ServerZoneDef.h>
#include <Network/PacketContainer.h>
#include <Util/UtilMath.h> #include <Util/UtilMath.h>
using namespace Core::Common; using namespace Core::Common;

View file

@ -48,9 +48,10 @@ using namespace Core::Network::Packets::Server;
void Core::Network::GameConnection::fcInfoReqHandler( const Packets::GamePacket& inPacket, void Core::Network::GameConnection::fcInfoReqHandler( const Packets::GamePacket& inPacket,
Entity::Player& player ) Entity::Player& player )
{ {
GamePacketPtr pPe( new GamePacket( 0xDD, 0x78, player.getId(), player.getId() ) ); // TODO: use new packet struct for this
pPe->setValAt< uint8_t >( 0x48, 0x01 ); //GamePacketPtr pPe( new GamePacket( 0xDD, 0x78, player.getId(), player.getId() ) );
queueOutPacket( pPe ); //pPe->setValAt< uint8_t >( 0x48, 0x01 );
//queueOutPacket( pPe );
} }
void Core::Network::GameConnection::setSearchInfoHandler( const Packets::GamePacket& inPacket, void Core::Network::GameConnection::setSearchInfoHandler( const Packets::GamePacket& inPacket,