This repository was archived by the owner on Mar 14, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathnetworkmessage.h
More file actions
100 lines (77 loc) · 2.59 KB
/
Copy pathnetworkmessage.h
File metadata and controls
100 lines (77 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//////////////////////////////////////////////////////////////////////
// OTAdmin - OpenTibia
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#ifndef __NETWORK_MESSAGE_H__
#define __NETWORK_MESSAGE_H__
#include "definitions.h"
#include "rsa.h"
#define RETRY_TIME 10000
enum SocketCode{
SOCKET_CODE_OK,
SOCKET_CODE_TIMEOUT,
SOCKET_CODE_ERROR,
};
class NetworkMessage
{
public:
// constructor/destructor
NetworkMessage();
~NetworkMessage();
// resets the internal buffer to an empty message
void Reset();
// socket functions
SocketCode ReadFromSocket(SOCKET socket, int timeout = RETRY_TIME);
SocketCode WriteToSocket(SOCKET socket, int timeout = RETRY_TIME);
// simply read functions for incoming message
uint8_t InspectByte();
uint8_t GetByte();
uint16_t GetU16();
uint32_t GetU32();
std::string GetString();
std::string GetRaw();
void setEncryptionState(bool state);
void setEncryptionKey(const uint32_t* key);
// skips count unknown/unused bytes in an incoming message
void SkipBytes(int count);
// simply write functions for outgoing message
void AddByte(uint8_t value);
void AddU16 (uint16_t value);
void AddU32 (uint32_t value);
void AddString(const std::string &value);
void AddString(const char* value);
int getMessageLength(){
return m_MsgSize;
}
bool RSA_encrypt();
void setRSAInstance(RSA* rsa);
protected:
inline bool canAdd(int size){
return (size + m_ReadPos < NETWORKMESSAGE_MAXSIZE - 16);
};
void XTEA_encrypt();
void XTEA_decrypt();
int m_MsgSize;
int m_ReadPos;
uint8_t m_MsgBuf[NETWORKMESSAGE_MAXSIZE];
static bool m_encryptionEnabled;
static bool m_keyset;
static uint32_t m_key[4];
static RSA* m_RSA;
};
#endif // #ifndef __NETWORK_MESSAGE_H__