2017-09-19 04:34:08 -04:00
2013-07-29 07:13:03 -04:00
// Connection.h
// Interfaces to the cConnection class representing a single pair of connected sockets
# pragma once
# include "ByteBuffer.h"
2017-08-30 10:00:06 -04:00
# include "mbedTLS++/AesCfb128Decryptor.h"
# include "mbedTLS++/AesCfb128Encryptor.h"
2013-07-29 07:13:03 -04:00
class cServer ;
class cConnection
{
2017-12-23 07:49:08 -05:00
/** Base for the log filename and all files connected to this log */
AString m_LogNameBase ;
/** Index for the next file into which item metadata should be written (ParseSlot() function) */
int m_ItemIdx ;
2013-07-29 07:13:03 -04:00
cCriticalSection m_CSLog ;
FILE * m_LogFile ;
2017-12-23 07:49:08 -05:00
2013-07-29 07:13:03 -04:00
cServer & m_Server ;
SOCKET m_ClientSocket ;
SOCKET m_ServerSocket ;
2017-12-23 07:49:08 -05:00
2014-11-23 09:22:05 -05:00
std : : chrono : : steady_clock : : time_point m_BeginTick ; // Tick when the relative time was first retrieved (used for GetRelativeTime())
2017-12-23 07:49:08 -05:00
2013-07-29 07:13:03 -04:00
enum eConnectionState
{
csUnencrypted , // The connection is not encrypted. Packets must be decoded in order to be able to start decryption.
csEncryptedUnderstood , // The communication is encrypted and so far all packets have been understood, so they can be still decoded
csEncryptedUnknown , // The communication is encrypted, but an unknown packet has been received, so packets cannot be decoded anymore
csWaitingForEncryption , // The communication is waiting for the other line to establish encryption
} ;
2017-12-23 07:49:08 -05:00
2013-07-29 07:13:03 -04:00
eConnectionState m_ClientState ;
eConnectionState m_ServerState ;
2017-12-23 07:49:08 -05:00
2013-07-29 07:13:03 -04:00
int m_Nonce ;
2017-12-23 07:49:08 -05:00
2013-07-29 07:13:03 -04:00
public :
cConnection ( SOCKET a_ClientSocket , cServer & a_Server ) ;
~ cConnection ( ) ;
2017-12-23 07:49:08 -05:00
2013-07-29 07:13:03 -04:00
void Run ( void ) ;
2017-12-23 07:49:08 -05:00
2020-05-04 14:21:48 -04:00
void vLog ( const char * a_Format , fmt : : printf_args a_ArgList ) ;
2018-01-03 12:41:16 -05:00
2020-05-05 17:52:14 -04:00
template < typename . . . Args >
void Log ( const char * a_Format , const Args & . . . a_Args )
{
vLog ( a_Format , fmt : : make_printf_args ( a_Args . . . ) ) ;
}
2020-05-04 14:21:48 -04:00
void vDataLog ( const void * a_Data , size_t a_Size , const char * a_Format , fmt : : printf_args a_ArgList ) ;
2020-05-05 17:52:14 -04:00
template < typename . . . Args >
void DataLog ( const void * a_Data , size_t a_Size , const char * a_Format , const Args & . . . a_Args )
{
vDataLog ( a_Data , a_Size , a_Format , fmt : : make_printf_args ( a_Args . . . ) ) ;
}
2018-01-03 12:41:16 -05:00
2013-07-29 07:13:03 -04:00
void LogFlush ( void ) ;
2017-12-23 07:49:08 -05:00
2013-07-29 07:13:03 -04:00
protected :
2014-01-25 13:19:37 -05:00
2013-07-29 07:13:03 -04:00
cByteBuffer m_ClientBuffer ;
cByteBuffer m_ServerBuffer ;
2017-12-23 07:49:08 -05:00
2014-04-29 11:37:15 -04:00
cAesCfb128Decryptor m_ServerDecryptor ;
cAesCfb128Encryptor m_ServerEncryptor ;
2013-07-29 07:13:03 -04:00
AString m_ServerEncryptionBuffer ; // Buffer for the data to be sent to the server once encryption is established
2017-12-23 07:49:08 -05:00
/** Set to true when PACKET_PING is received from the client; will cause special parsing for server kick */
2013-07-29 07:13:03 -04:00
bool m_HasClientPinged ;
2017-12-23 07:49:08 -05:00
2013-10-29 10:47:22 -04:00
/*
The protocol states can be one of :
- 1 : no initial handshake received yet
1 : status
2 : login
3 : game
*/
2017-12-23 07:49:08 -05:00
/** State the to-server protocol is in (as defined by the initial handshake / login), -1 if no initial handshake received yet */
2013-10-29 10:47:22 -04:00
int m_ServerProtocolState ;
2017-12-23 07:49:08 -05:00
/** State the to-client protocol is in (as defined by the initial handshake / login), -1 if no initial handshake received yet */
2013-10-29 10:47:22 -04:00
int m_ClientProtocolState ;
2017-12-23 07:49:08 -05:00
/** True if the server connection has provided encryption keys */
2013-10-29 10:47:22 -04:00
bool m_IsServerEncrypted ;
2017-12-23 07:49:08 -05:00
2013-07-29 07:13:03 -04:00
bool ConnectToServer ( void ) ;
2017-12-23 07:49:08 -05:00
/** Relays data from server to client; returns false if connection aborted */
2013-07-29 07:13:03 -04:00
bool RelayFromServer ( void ) ;
2017-12-23 07:49:08 -05:00
/** Relays data from client to server; returns false if connection aborted */
2013-07-29 07:13:03 -04:00
bool RelayFromClient ( void ) ;
2017-12-23 07:49:08 -05:00
/** Returns the time relative to the first call of this function, in the fractional seconds elapsed */
2013-07-29 07:13:03 -04:00
double GetRelativeTime ( void ) ;
2017-12-23 07:49:08 -05:00
/** Sends data to the specified socket. If sending fails, prints a fail message using a_Peer and returns false. */
2014-04-04 02:55:48 -04:00
bool SendData ( SOCKET a_Socket , const char * a_Data , size_t a_Size , const char * a_Peer ) ;
2017-12-23 07:49:08 -05:00
/** Sends data to the specified socket. If sending fails, prints a fail message using a_Peer and returns false. */
2013-07-29 07:13:03 -04:00
bool SendData ( SOCKET a_Socket , cByteBuffer & a_Data , const char * a_Peer ) ;
2017-12-23 07:49:08 -05:00
/** Sends data to the specfied socket, after encrypting it using a_Encryptor. If sending fails, prints a fail message using a_Peer and returns false */
2014-04-29 11:37:15 -04:00
bool SendEncryptedData ( SOCKET a_Socket , cAesCfb128Encryptor & a_Encryptor , const char * a_Data , size_t a_Size , const char * a_Peer ) ;
2017-12-23 07:49:08 -05:00
/** Sends data to the specfied socket, after encrypting it using a_Encryptor. If sending fails, prints a fail message using a_Peer and returns false */
2014-04-29 11:37:15 -04:00
bool SendEncryptedData ( SOCKET a_Socket , cAesCfb128Encryptor & a_Encryptor , cByteBuffer & a_Data , const char * a_Peer ) ;
2017-12-23 07:49:08 -05:00
/** Decodes packets coming from the client, sends appropriate counterparts to the server; returns false if the connection is to be dropped */
2013-07-29 07:13:03 -04:00
bool DecodeClientsPackets ( const char * a_Data , int a_Size ) ;
2017-12-23 07:49:08 -05:00
/** Decodes packets coming from the server, sends appropriate counterparts to the client; returns false if the connection is to be dropped */
2013-07-29 07:13:03 -04:00
bool DecodeServersPackets ( const char * a_Data , int a_Size ) ;
2017-12-23 07:49:08 -05:00
2013-10-29 10:47:22 -04:00
// Packet handling, client-side, initial:
bool HandleClientHandshake ( void ) ;
2017-12-23 07:49:08 -05:00
2013-10-29 10:47:22 -04:00
// Packet handling, client-side, status:
bool HandleClientStatusPing ( void ) ;
bool HandleClientStatusRequest ( void ) ;
2017-12-23 07:49:08 -05:00
2013-10-29 10:47:22 -04:00
// Packet handling, client-side, login:
bool HandleClientLoginEncryptionKeyResponse ( void ) ;
bool HandleClientLoginStart ( void ) ;
// Packet handling, client-side, game:
2013-07-29 07:13:03 -04:00
bool HandleClientAnimation ( void ) ;
bool HandleClientBlockDig ( void ) ;
bool HandleClientBlockPlace ( void ) ;
bool HandleClientChatMessage ( void ) ;
bool HandleClientClientStatuses ( void ) ;
bool HandleClientCreativeInventoryAction ( void ) ;
2013-08-25 15:57:12 -04:00
bool HandleClientDisconnect ( void ) ;
2013-07-29 07:13:03 -04:00
bool HandleClientEntityAction ( void ) ;
bool HandleClientKeepAlive ( void ) ;
bool HandleClientLocaleAndView ( void ) ;
bool HandleClientPing ( void ) ;
bool HandleClientPlayerAbilities ( void ) ;
bool HandleClientPlayerLook ( void ) ;
bool HandleClientPlayerOnGround ( void ) ;
bool HandleClientPlayerPosition ( void ) ;
bool HandleClientPlayerPositionLook ( void ) ;
bool HandleClientPluginMessage ( void ) ;
bool HandleClientSlotSelect ( void ) ;
2013-07-31 08:32:05 -04:00
bool HandleClientTabCompletion ( void ) ;
2013-07-29 07:13:03 -04:00
bool HandleClientUpdateSign ( void ) ;
bool HandleClientUseEntity ( void ) ;
bool HandleClientWindowClick ( void ) ;
bool HandleClientWindowClose ( void ) ;
2017-12-23 07:49:08 -05:00
2013-10-29 17:55:00 -04:00
bool HandleClientUnknownPacket ( UInt32 a_PacketType , UInt32 a_PacketLen , UInt32 a_PacketReadSoFar ) ;
2013-07-29 07:13:03 -04:00
2013-10-29 10:47:22 -04:00
// Packet handling, server-side, login:
bool HandleServerLoginDisconnect ( void ) ;
bool HandleServerLoginEncryptionKeyRequest ( void ) ;
bool HandleServerLoginSuccess ( void ) ;
// Packet handling, server-side, game:
2013-07-29 07:13:03 -04:00
bool HandleServerAttachEntity ( void ) ;
bool HandleServerBlockAction ( void ) ;
bool HandleServerBlockChange ( void ) ;
bool HandleServerChangeGameState ( void ) ;
bool HandleServerChatMessage ( void ) ;
bool HandleServerCollectPickup ( void ) ;
bool HandleServerCompass ( void ) ;
bool HandleServerDestroyEntities ( void ) ;
bool HandleServerEntity ( void ) ;
bool HandleServerEntityEquipment ( void ) ;
bool HandleServerEntityHeadLook ( void ) ;
bool HandleServerEntityLook ( void ) ;
bool HandleServerEntityMetadata ( void ) ;
bool HandleServerEntityProperties ( void ) ;
bool HandleServerEntityRelativeMove ( void ) ;
bool HandleServerEntityRelativeMoveLook ( void ) ;
bool HandleServerEntityStatus ( void ) ;
bool HandleServerEntityTeleport ( void ) ;
bool HandleServerEntityVelocity ( void ) ;
2013-09-19 15:51:59 -04:00
bool HandleServerExplosion ( void ) ;
2013-07-29 07:13:03 -04:00
bool HandleServerIncrementStatistic ( void ) ;
2013-10-29 14:05:51 -04:00
bool HandleServerJoinGame ( void ) ;
2013-07-29 07:13:03 -04:00
bool HandleServerKeepAlive ( void ) ;
bool HandleServerKick ( void ) ;
bool HandleServerLogin ( void ) ;
bool HandleServerMapChunk ( void ) ;
bool HandleServerMapChunkBulk ( void ) ;
bool HandleServerMultiBlockChange ( void ) ;
bool HandleServerNamedSoundEffect ( void ) ;
bool HandleServerPlayerAbilities ( void ) ;
bool HandleServerPlayerAnimation ( void ) ;
bool HandleServerPlayerListItem ( void ) ;
bool HandleServerPlayerPositionLook ( void ) ;
bool HandleServerPluginMessage ( void ) ;
2013-10-29 14:05:51 -04:00
bool HandleServerRespawn ( void ) ;
2013-07-29 07:13:03 -04:00
bool HandleServerSetExperience ( void ) ;
bool HandleServerSetSlot ( void ) ;
bool HandleServerSlotSelect ( void ) ;
bool HandleServerSoundEffect ( void ) ;
2013-10-29 14:05:51 -04:00
bool HandleServerSpawnExperienceOrbs ( void ) ;
2013-07-29 07:13:03 -04:00
bool HandleServerSpawnMob ( void ) ;
bool HandleServerSpawnNamedEntity ( void ) ;
bool HandleServerSpawnObjectVehicle ( void ) ;
bool HandleServerSpawnPainting ( void ) ;
bool HandleServerSpawnPickup ( void ) ;
2013-10-29 14:05:51 -04:00
bool HandleServerStatistics ( void ) ;
2013-10-28 18:05:53 -04:00
bool HandleServerStatusPing ( void ) ;
bool HandleServerStatusResponse ( void ) ;
2013-07-31 08:32:05 -04:00
bool HandleServerTabCompletion ( void ) ;
2013-07-29 07:13:03 -04:00
bool HandleServerTimeUpdate ( void ) ;
bool HandleServerUpdateHealth ( void ) ;
bool HandleServerUpdateSign ( void ) ;
bool HandleServerUpdateTileEntity ( void ) ;
2013-10-29 14:05:51 -04:00
bool HandleServerUseBed ( void ) ;
2013-07-29 07:13:03 -04:00
bool HandleServerWindowClose ( void ) ;
bool HandleServerWindowContents ( void ) ;
bool HandleServerWindowOpen ( void ) ;
2017-12-23 07:49:08 -05:00
2013-10-29 17:55:00 -04:00
bool HandleServerUnknownPacket ( UInt32 a_PacketType , UInt32 a_PacketLen , UInt32 a_PacketReadSoFar ) ;
2017-12-23 07:49:08 -05:00
/** Parses the slot data in a_Buffer into item description; returns true if successful, false if not enough data */
2013-07-29 07:13:03 -04:00
bool ParseSlot ( cByteBuffer & a_Buffer , AString & a_ItemDesc ) ;
2017-12-23 07:49:08 -05:00
/** Parses the metadata in a_Buffer into raw metadata in an AString; returns true if successful, false if not enough data */
2013-07-29 07:13:03 -04:00
bool ParseMetadata ( cByteBuffer & a_Buffer , AString & a_Metadata ) ;
2017-12-23 07:49:08 -05:00
/** Logs the contents of the metadata in the AString, using Log(). Assumes a_Metadata is valid (parsed by ParseMetadata()). The log is indented by a_IndentCount spaces */
2013-07-29 07:13:03 -04:00
void LogMetadata ( const AString & a_Metadata , size_t a_IndentCount ) ;
2017-12-23 07:49:08 -05:00
/** Send EKResp to the server: */
2013-07-29 07:13:03 -04:00
void SendEncryptionKeyResponse ( const AString & a_ServerPublicKey , const AString & a_Nonce ) ;
2017-12-23 07:49:08 -05:00
/** Starts client encryption based on the parameters received */
2013-07-29 07:13:03 -04:00
void StartClientEncryption ( const AString & a_EncryptedSecret , const AString & a_EncryptedNonce ) ;
} ;