-
Notifications
You must be signed in to change notification settings - Fork 98
/
netmessages.cpp
336 lines (284 loc) · 11.1 KB
/
netmessages.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "netmessages.h"
#include "bitbuf.h"
#include "const.h"
#include "../engine/net_chan.h"
#include "mathlib/mathlib.h"
#include "networkstringtabledefs.h"
#include "../engine/audio/public/sound.h"
#include "../engine/event_system.h"
#include "../engine/dt.h"
#include "mathlib/IceKey.H"
#include "tier0/vprof.h"
static char s_text[1024];
const char *g_MostCommonPathIDs[] =
{
"GAME",
"MOD"
};
const char *g_MostCommonPrefixes[] =
{
"materials",
"models",
"sounds",
"scripts"
};
static int FindCommonPathID( const char *pPathID )
{
for ( int i = 0; i < ARRAYSIZE( g_MostCommonPathIDs ); i++ )
{
if ( V_stricmp( pPathID, g_MostCommonPathIDs[i] ) == 0 )
return i;
}
return -1;
}
static int FindCommonPrefix( const char *pStr )
{
for ( int i = 0; i < ARRAYSIZE( g_MostCommonPrefixes ); i++ )
{
if ( V_stristr( pStr, g_MostCommonPrefixes[i] ) == pStr )
{
int iNextChar = V_strlen( g_MostCommonPrefixes[i] );
if ( pStr[iNextChar] == '/' || pStr[iNextChar] == '\\' )
return i;
}
}
return -1;
}
void CCLCMsg_FileCRCCheck_t::SetPath( CCLCMsg_FileCRCCheck& msg, const char *path )
{
int iCode = FindCommonPathID( path );
if ( iCode == -1 )
{
msg.set_code_path( -1 );
msg.set_path( path );
}
else
{
msg.set_code_path( iCode );
}
}
const char *CCLCMsg_FileCRCCheck_t::GetPath( const CCLCMsg_FileCRCCheck& msg )
{
int iCode = msg.code_path();
if( ( iCode >= 0 ) && ( iCode < ARRAYSIZE( g_MostCommonPathIDs ) ) )
{
return g_MostCommonPathIDs[ iCode ];
}
Assert( msg.code_path() == -1 );
return msg.path().c_str();
}
void CCLCMsg_FileCRCCheck_t::SetFileName( CCLCMsg_FileCRCCheck& msg, const char *fileName )
{
int iCode = FindCommonPrefix( fileName );
if ( iCode == -1 )
{
msg.set_code_filename( -1 );
msg.set_filename( fileName );
}
else
{
msg.set_code_filename( iCode );
msg.set_filename( &fileName[ V_strlen( g_MostCommonPrefixes[ iCode ] ) + 1 ] );
}
}
const char *CCLCMsg_FileCRCCheck_t::GetFileName( const CCLCMsg_FileCRCCheck& msg )
{
int iCode = msg.code_filename();
if( ( iCode >= 0 ) && ( iCode < ARRAYSIZE( g_MostCommonPrefixes ) ) )
{
return va( "%s%c%s", g_MostCommonPrefixes[ iCode ], CORRECT_PATH_SEPARATOR, msg.filename().c_str() );
}
Assert( msg.code_filename() == -1 );
return msg.filename().c_str();
}
void CmdKeyValuesHelper::CLCMsg_SetKeyValues( CCLCMsg_CmdKeyValues& msg, const KeyValues *keyValues )
{
CUtlBuffer bufData;
keyValues->WriteAsBinary( bufData );
int numBytes = bufData.TellPut();
msg.set_keyvalues( bufData.Base(), numBytes );
}
KeyValues *CmdKeyValuesHelper::CLCMsg_GetKeyValues ( const CCLCMsg_CmdKeyValues& msg )
{
KeyValues *pKeyValues = new KeyValues( "" );
const std::string& msgStr = msg.keyvalues();
int numBytes = msgStr.size();
CUtlBuffer bufRead( msgStr.data(), numBytes, CUtlBuffer::READ_ONLY );
if ( !pKeyValues->ReadAsBinary( bufRead, 90 ) ) // we are expecting very few nest levels of keyvalues here!
{
Assert( false );
}
return pKeyValues;
}
void CmdKeyValuesHelper::SVCMsg_SetKeyValues( CSVCMsg_CmdKeyValues& msg, const KeyValues *keyValues )
{
CUtlBuffer bufData;
keyValues->WriteAsBinary( bufData );
int numBytes = bufData.TellPut();
msg.set_keyvalues( bufData.Base(), numBytes );
}
KeyValues *CmdKeyValuesHelper::SVCMsg_GetKeyValues ( const CSVCMsg_CmdKeyValues& msg )
{
KeyValues *pKeyValues = new KeyValues( "" );
const std::string& msgStr = msg.keyvalues();
int numBytes = msgStr.size();
CUtlBuffer bufRead( msgStr.data(), numBytes, CUtlBuffer::READ_ONLY );
if ( !pKeyValues->ReadAsBinary( bufRead ) )
{
Assert( false );
}
return pKeyValues;
}
/*
bool CmdEncryptedDataMessageCodec::SVCMsg_EncryptedData_EncryptMessage( CSVCMsg_EncryptedData_t &msgEncryptedResult, const ::google::protobuf::Message &msgPlaintextInput )
{
static char const *szEncryptedTag = "[[ENCRYPTED_DATA]]";
static size_t nEncryptedLen = Q_strlen( szEncryptedTag );
int32 const numBytesWritten = msgPlaintextInput.ByteSize();
msgEncryptedResult.mutable_encrypted()->resize( nEncryptedLen + sizeof( int32 ) + numBytesWritten );
Q_memcpy( &msgEncryptedResult.mutable_encrypted()->at(0), szEncryptedTag, nEncryptedLen );
int32 const numBytesWrittenWire = BigLong( numBytesWritten ); // byteswap for the wire
Q_memcpy( &msgEncryptedResult.mutable_encrypted()->at( nEncryptedLen ), &numBytesWrittenWire, sizeof( numBytesWrittenWire ) );
return msgPlaintextInput.SerializeWithCachedSizesToArray( ( uint8 * ) &msgEncryptedResult.mutable_encrypted()->at( nEncryptedLen + sizeof( int32 ) ) );
}
*/
bool CmdEncryptedDataMessageCodec::SVCMsg_EncryptedData_EncryptMessage( CSVCMsg_EncryptedData_t &msgEncryptedResult, INetMessage *pMsgPlaintextInput, char const *key )
{
// Prepare encryption key
IceKey iceKey( 2 );
if ( iceKey.keySize() != Q_strlen( key ) )
{
Warning( "CmdEncryptedDataMessageCodec key size is %d, but %d is expected!\n", Q_strlen( key ), iceKey.keySize() );
return false; // we cannot encrypt with the supplied key
}
iceKey.set( ( const unsigned char * ) key );
// supporting smaller stack
net_scratchbuffer_t scratch;
bf_write msg( "SVCMsg_EncryptedData_EncryptMessage", scratch.GetBuffer(), scratch.Size() );
if ( !pMsgPlaintextInput->WriteToBuffer( msg ) )
return false;
int32 const numBytesWritten = msg.GetNumBytesWritten();
// Generate some random fudge, ICE operates on 64-bit blocks, so make sure our total size is a multiple of 8 bytes
int numRandomFudgeBytes = RandomInt( 16, 72 );
int numTotalEncryptedBytes = 1 + numRandomFudgeBytes + sizeof( int32 ) + numBytesWritten;
numRandomFudgeBytes += iceKey.blockSize() - ( numTotalEncryptedBytes % iceKey.blockSize() );
numTotalEncryptedBytes = 1 + numRandomFudgeBytes + sizeof( int32 ) + numBytesWritten;
char *pchRandomFudgeBytes = ( char * ) stackalloc( numRandomFudgeBytes );
for ( int k = 0; k < numRandomFudgeBytes; ++ k )
pchRandomFudgeBytes[k] = RandomInt( 16, 250 );
msgEncryptedResult.mutable_encrypted()->resize( numTotalEncryptedBytes );
msgEncryptedResult.mutable_encrypted()->at(0) = numRandomFudgeBytes;
Q_memcpy( &msgEncryptedResult.mutable_encrypted()->at(1), pchRandomFudgeBytes, numRandomFudgeBytes );
int32 const numBytesWrittenWire = BigLong( numBytesWritten ); // byteswap for the wire
Q_memcpy( &msgEncryptedResult.mutable_encrypted()->at( 1 + numRandomFudgeBytes ), &numBytesWrittenWire, sizeof( numBytesWrittenWire ) );
Q_memcpy( &msgEncryptedResult.mutable_encrypted()->at( 1 + numRandomFudgeBytes + sizeof( int32 ) ), msg.GetBasePointer(), numBytesWritten );
// Encrypt the message
unsigned char *pchCryptoBuffer = ( unsigned char * ) stackalloc( iceKey.blockSize() );
for ( int k = 0; k < numTotalEncryptedBytes; k += iceKey.blockSize() )
{
iceKey.encrypt( ( const unsigned char * ) &msgEncryptedResult.mutable_encrypted()->at(k), pchCryptoBuffer );
Q_memcpy( &msgEncryptedResult.mutable_encrypted()->at(k), pchCryptoBuffer, iceKey.blockSize() );
}
return true;
}
bool CmdEncryptedDataMessageCodec::SVCMsg_EncryptedData_Process( CSVCMsg_EncryptedData const &msgEncryptedInput, INetChannel *pProcessingChannelInterface, char const *key )
{
CNetChan *pProcessingChannel = ( CNetChan * ) pProcessingChannelInterface;
if ( !pProcessingChannel )
return false;
if ( !msgEncryptedInput.has_encrypted() )
return true;
if ( !key || !*key )
return true; // key is not supplied, so ignore the message
// Decrypt the message
IceKey iceKey( 2 );
if ( iceKey.keySize() != Q_strlen( key ) )
return true; // we cannot decrypt with the supplied key
iceKey.set( ( const unsigned char * ) key );
if ( msgEncryptedInput.encrypted().size() % iceKey.blockSize() )
return true; // message malformed, cannot decrypt
if ( msgEncryptedInput.encrypted().size() > NET_MAX_PAYLOAD )
return true; // size too large, cannot decrypt
net_scratchbuffer_t scratch;
byte *buffer = scratch.GetBuffer();
unsigned char *pchCryptoBuffer = ( unsigned char * ) stackalloc( iceKey.blockSize() );
for ( int k = 0; k < ( int ) msgEncryptedInput.encrypted().size(); k += iceKey.blockSize() )
{
iceKey.decrypt( ( const unsigned char * ) &msgEncryptedInput.encrypted().at(k), pchCryptoBuffer );
Q_memcpy( &buffer[k], pchCryptoBuffer, iceKey.blockSize() );
}
// Check how much random fudge we have
int numRandomFudgeBytes = *buffer;
if ( ( numRandomFudgeBytes > 0 ) && ( numRandomFudgeBytes + 1 + sizeof( int32 ) < msgEncryptedInput.encrypted().size() ) )
{
// Fetch the size of the encrypted message
int32 numBytesWrittenWire = 0;
Q_memcpy( &numBytesWrittenWire, &buffer[ 1 + numRandomFudgeBytes ], sizeof( int32 ) );
int32 const numBytesWritten = BigLong( numBytesWrittenWire ); // byteswap from the wire
// Make sure the total size of the message matches the expectations
if ( numRandomFudgeBytes + 1 + sizeof( int32 ) + numBytesWritten == msgEncryptedInput.encrypted().size() )
{
bf_read bufRead( &buffer[ 1 + numRandomFudgeBytes + sizeof( int32 ) ], numBytesWritten );
unsigned char cmd = bufRead.ReadVarInt32();
// See if the netchan has the required binder registered
int iMsgHandler = 0;
INetMessageBinder *pMsgBind = pProcessingChannel->FindMessageBinder( cmd, iMsgHandler++ );
if ( pMsgBind )
{
int startbit = bufRead.GetNumBitsRead();
INetMessage *pEmbeddedMessage = pMsgBind->CreateFromBuffer( bufRead );
if ( !pEmbeddedMessage )
{
Msg( "CmdEncryptedDataMessageCodec failed to parse embedded message" );
Assert( 0 );
return false;
}
pEmbeddedMessage->SetReliable( pProcessingChannel->WasLastMessageReliable() );
pProcessingChannel->UpdateMessageStats( pEmbeddedMessage->GetGroup(), bufRead.GetNumBitsRead() - startbit );
do
{
bool bRet = pMsgBind->Process( *pEmbeddedMessage );
if ( !bRet )
{
ConDMsg( "CmdEncryptedDataMessageCodec: netchannel failed processing embedded message %s.\n", pEmbeddedMessage->GetName() );
Assert ( 0 );
delete pEmbeddedMessage;
return false;
}
// Move to another binder
pMsgBind = pProcessingChannel->FindMessageBinder( cmd, iMsgHandler++ );
} while ( pMsgBind );
delete pEmbeddedMessage;
}
}
}
return true;
}
CTSPool< net_scratchbuffer_t::buffer_t > net_scratchbuffer_t::sm_NetScratchBuffers;
#if defined( REPLAY_ENABLED )
bool CLC_SaveReplay::WriteToBuffer( bf_write &buffer ) const
{
buffer.WriteUBitLong( GetType(), NETMSG_TYPE_BITS );
buffer.WriteString( m_szFilename );
buffer.WriteUBitLong( m_nStartSendByte, sizeof( m_nStartSendByte ) );
buffer.WriteFloat( m_flPostDeathRecordTime );
return !buffer.IsOverflowed();
}
bool CLC_SaveReplay::ReadFromBuffer( bf_read &buffer )
{
buffer.ReadString( m_szFilename, sizeof( m_szFilename ) );
m_nStartSendByte = buffer.ReadUBitLong( sizeof( m_nStartSendByte ) );
m_flPostDeathRecordTime = buffer.ReadFloat();
return !buffer.IsOverflowed();
}
const char *CLC_SaveReplay::ToString() const
{
V_snprintf( s_text, sizeof( s_text ), "%s: filename: %s, start byte: %i, post death record time: %f", GetName(), m_szFilename, m_nStartSendByte, m_flPostDeathRecordTime );
return s_text;
}
#endif