summaryrefslogtreecommitdiff
path: root/engine/host_phonehome.cpp
blob: 48fad5e653481d1fc12845e383a30dd881f4b7c1 (plain) (blame)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#ifdef _WIN32
#if !defined( _X360 )
#include <windows.h>
#endif
#elif defined(POSIX)
#include <sys/socket.h>
#include <netinet/in.h>
#include <pwd.h>
#include <sys/types.h>
#else
#error
#endif

#include "host.h"
#include "quakedef.h"
#include "net.h"
#include "bitbuf.h"
#include "tier0/icommandline.h"
#include "cserserverprotocol_engine.h"
#include "host_phonehome.h"
#include "mathlib/IceKey.H"
#include "tier0/vcrmode.h"
#include "blockingudpsocket.h"

#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#endif

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

#define PHONE_HOME_TIMEOUT	1.5f
#define PHONE_HOME_RETRIES	3

//-----------------------------------------------------------------------------
// Purpose: returns a pointer to a function, given a module
// Input  : pModuleName - module name
//			*pName - proc name
//-----------------------------------------------------------------------------
static char const *g_pszExitMsg = "Renderer:  Out of memory, message code %i";

class CPhoneHome : public IPhoneHome
{
public:
	CPhoneHome() :
		m_bPhoneHome( false ),
		m_uSessionID( 0 ),
		m_pSocket( 0 )
	{
		  Q_memset( &m_cserIP, 0, sizeof( m_cserIP ) );
		  Q_memset( m_szBuildIdentifier, 0, sizeof( m_szBuildIdentifier ) );
	}

	virtual void Shutdown()
	{
		delete m_pSocket;
		m_pSocket = NULL;
	}

	virtual void Init()
	{
		char build_identifier[ 32 ];

		Q_strncpy( build_identifier, "VLV_INTERNAL                    ", sizeof( build_identifier ) );
		int iBI = CommandLine()->FindParm("-bi");
		if ( iBI > 0 )
		{
			if ( (iBI+1) < CommandLine()->ParmCount() )
			{
				char const *pBuildParam = CommandLine()->GetParm( iBI + 1 );
				Q_memset( build_identifier, 0, sizeof( build_identifier ) );
				Q_strncpy( build_identifier, pBuildParam, sizeof( build_identifier ) );
			}
			else
			{
				build_identifier[ 0 ] = '!';
			}
		}

		if ( Q_strlen( build_identifier ) >= 1 &&
			Q_strnicmp( build_identifier, "VLV_INTERNAL", Q_strlen( "VLV_INTERNAL" ) ) )
		{
			// Strip trailing spaces from identifer
			char *identifer = &build_identifier[ Q_strlen( build_identifier ) - 1 ];
			while ( identifer > build_identifier && *identifer == ' ' )
			{
				*identifer-- = 0;
			}
			
			// FIXME:  Don't hardcode CSER ip, get from Steam!!!
			if ( NET_StringToAdr( "207.173.177.12:27013", &m_cserIP ) )
			{
				m_bPhoneHome = true;
				
				Q_strncpy( m_szBuildIdentifier, build_identifier, sizeof( m_szBuildIdentifier ) );

				m_pSocket = new CBlockingUDPSocket();
			}
		}
		
	}

	virtual void Message( byte msgtype, char const *mapname )
	{
		if ( !m_bPhoneHome )
			return;

		if ( !m_pSocket )
			return;

		switch ( msgtype )
		{
		default:
			break;
		case PHONE_MSG_ENGINESTART:
			if ( !RequestSessionId( m_uSessionID ) )
			{
				ExitApp();
			}
			// Note we always return here!!!
			return;
		case PHONE_MSG_ENGINEEND:
			break;
		case PHONE_MSG_MAPSTART:
			{
				if ( m_bLevelStarted )
				{
					return;
				}

				m_bLevelStarted = true;

				// Tracker 22394:  Don't send map start/finish when building reslists...
				if ( CommandLine()->FindParm( "-makereslists" ) )
				{
					return;
				}
			}
			break;
		case PHONE_MSG_MAPEND:
			{
				if ( !m_bLevelStarted )
				{
					return;
				}

				m_bLevelStarted = false;

				// Tracker 22394:  Don't send map start/finish when building reslists...
				if ( CommandLine()->FindParm( "-makereslists" ) )
				{
					return;
				}
			}
			break;
		}

		SendSessionMessage( msgtype, mapname );
	}

private:

	void	ExitApp()
	{
		byte msgtype = 212;
		Error( g_pszExitMsg, msgtype );
	}

	//-----------------------------------------------------------------------------
	// Purpose: encrypts an 8-byte sequence
	//-----------------------------------------------------------------------------
	inline void Encrypt8ByteSequence( IceKey& cipher, const unsigned char *plainText, unsigned char *cipherText)
	{
		cipher.encrypt(plainText, cipherText);
	}

	//-----------------------------------------------------------------------------
	// Purpose: 
	//-----------------------------------------------------------------------------
	void EncryptBuffer( IceKey& cipher, unsigned char *bufData, uint bufferSize)
	{
		unsigned char *cipherText = bufData;
		unsigned char *plainText = bufData;
		uint bytesEncrypted = 0;

		while (bytesEncrypted < bufferSize)
		{
			// encrypt 8 byte section
			Encrypt8ByteSequence( cipher, plainText, cipherText);
			bytesEncrypted += 8;
			cipherText += 8;
			plainText += 8;
		}
	}

	void BuildMessage( bf_write& buf, byte msgtype, char const *mapname, unsigned int uSessionID )
	{
	
		bf_write	encrypted;
		ALIGN4 byte		encrypted_data[ 2048 ] ALIGN4_POST;

		buf.WriteByte( C2M_PHONEHOME );
		buf.WriteByte( '\n' );
		buf.WriteByte( C2M_PHONEHOME_PROTOCOL_VERSION );
		buf.WriteLong( uSessionID ); // sessionid (request new id by sending 0)

		// encryption object
		IceKey cipher(1); /* medium encryption level */
		unsigned char ucEncryptionKey[8] = { 191, 1, 0, 222, 85, 39, 154, 1 };
		cipher.set( ucEncryptionKey );

		encrypted.StartWriting( encrypted_data, sizeof( encrypted_data ) );

		byte corruption_identifier = 0x01;

		encrypted.WriteByte( corruption_identifier );

		// Data version protocol
		encrypted.WriteByte( 1 );

		// Write the "build identifier"  -- unique to each person we give a build to.
		encrypted.WriteString( m_szBuildIdentifier ); 
		{
			char computername[ 64 ];
			Q_memset( computername, 0, sizeof( computername ) );
#if defined ( _WIN32 )
			DWORD length = sizeof( computername ) - 1;
			if ( !GetComputerName( computername, &length ) )
			{
				Q_strncpy( computername, "???", sizeof( computername )  );
			}
#else
			if ( gethostname( computername, sizeof(computername) ) == -1 )
			{
				Q_strncpy( computername, "Linux????", sizeof( computername ) );
			}
			computername[sizeof(computername)-1] = '\0';
#endif
			encrypted.WriteString( computername );
		}

		{
			char username[ 64 ];
			Q_memset( username, 0, sizeof( username ) );
#if defined ( _WIN32 )
			DWORD length = sizeof( username ) - 1;
			if ( !GetUserName( username, &length ) )
			{
				Q_strncpy( username, "???", sizeof( username )  );
			}
#else
			struct passwd *pass = getpwuid( getuid() );
			if ( pass )
			{
				Q_strncpy( username, pass->pw_name, sizeof( username ) );
			}
			else
			{
				Q_strncpy( username, "LinuxUser??", sizeof( username ) );
			}
			username[sizeof(username)-1] = '\0';
#endif
			encrypted.WriteString( username );
		}

		char gamedir[ 64 ];
		Q_FileBase( com_gamedir, gamedir, sizeof( gamedir ) );
		encrypted.WriteString( gamedir );

		unsigned int uBuildNumber = build_number();

		encrypted.WriteLong( (int)uBuildNumber );

		// WRite timestamp of engine
		encrypted.WriteFloat( (float)realtime );

		encrypted.WriteByte( msgtype );
		if ( mapname != NULL )
		{
			encrypted.WriteString( mapname );
		}

		int isDebugUser = ( Sys_IsDebuggerPresent() || CommandLine()->FindParm( "-allowdebug" ) ) ? 1 : 0;

		encrypted.WriteByte( isDebugUser );

		while ( encrypted.GetNumBytesWritten() % 8 )
		{
			encrypted.WriteByte( 0 );
		}

		EncryptBuffer( cipher, (unsigned char *)encrypted.GetData(), encrypted.GetNumBytesWritten() );

		buf.WriteShort( (int)encrypted.GetNumBytesWritten() );
		buf.WriteBytes( (unsigned char *)encrypted.GetData(), encrypted.GetNumBytesWritten() );
	}

	void SendSessionMessage( byte msgtype, char const *mapname )
	{
		if ( m_uSessionID == 0 )
			return;

		bf_write	buf;
		ALIGN4 byte		data[ 2048 ] ALIGN4_POST;
		
		buf.StartWriting( data, sizeof( data ) );

		BuildMessage( buf, msgtype, mapname, m_uSessionID );

		struct sockaddr_in sa;

		m_cserIP.ToSockadr( (struct sockaddr *)&sa );

		m_pSocket->SendSocketMessage( sa, (const byte *)buf.GetData(), buf.GetNumBytesWritten() );

		// If we already have a sessionid, don't wait for the server to give us back a new one...
		if ( m_uSessionID != 0 )
		{
			return;
		}

		if ( m_pSocket->WaitForMessage( PHONE_HOME_TIMEOUT ) )
		{	
			ALIGN4 byte		readbuf[ 128 ] ALIGN4_POST;

			bf_read		replybuf( readbuf, sizeof( readbuf ) );

			struct sockaddr_in replyaddress;
			uint bytesReceived = m_pSocket->ReceiveSocketMessage( &replyaddress, (byte *)readbuf, sizeof( readbuf ) );
			if ( bytesReceived > 0 )
			{
				// Fixup actual size
				replybuf.StartReading( readbuf, bytesReceived );

				// Parse out data
				byte responseType = (byte)replybuf.ReadByte();
				if ( M2C_ACKPHONEHOME == responseType  )
				{
					bool allowPlay = replybuf.ReadByte() == 1 ? true : false;
					if ( allowPlay )
					{
						m_uSessionID = replybuf.ReadLong();
					}
				}
			}
		}
	}

	bool RequestSessionId( unsigned int& id )
	{
		id = 0u;

		bf_write	buf;
		ALIGN4 byte		data[ 2048 ] ALIGN4_POST;
		
		buf.StartWriting( data, sizeof( data ) );

		BuildMessage( buf, PHONE_MSG_ENGINESTART, NULL, id );

		struct sockaddr_in sa;

		m_cserIP.ToSockadr( (struct sockaddr *)&sa );

		for ( int retries = 0; retries < PHONE_HOME_RETRIES; ++retries )
		{
			m_pSocket->SendSocketMessage( sa, (const byte *)buf.GetData(), buf.GetNumBytesWritten() ); //lint !e534
			if ( m_pSocket->WaitForMessage( PHONE_HOME_TIMEOUT ) )
			{	
				ALIGN4 byte		readbuf[ 128 ] ALIGN4_POST;

				bf_read		replybuf( readbuf, sizeof( readbuf ) );

				struct sockaddr_in replyaddress;
				uint bytesReceived = m_pSocket->ReceiveSocketMessage( &replyaddress, (byte *)readbuf, sizeof( readbuf ) );
				if ( bytesReceived > 0 )
				{
					// Fixup actual size
					replybuf.StartReading( readbuf, bytesReceived );

					// Parse out data
					byte responseType = (byte)replybuf.ReadByte();
					if ( M2C_ACKPHONEHOME == responseType  )
					{
						bool allowPlay = replybuf.ReadByte() == 1 ? true : false;
						if ( allowPlay )
						{
							id = replybuf.ReadLong();
							return true;
						}
					}
					break;
				}

			}
		}

		return false;
	}

	// FIXME, this is BS
	bool IsExternalBuild()
	{
		if ( CommandLine()->FindParm( "-publicbuild" ) )
		{
			return true;
		}

		if ( !CommandLine()->FindParm( "-internalbuild" ) && 
			 !CommandLine()->CheckParm("-dev") )
		{
			return true;
		}

		// It's an external build...
		if ( m_bPhoneHome )
		{
			if ( !Q_stricmp( m_szBuildIdentifier, "beta-playtest" ) )
			{
				// Still internal
				return false;
			}
			return true;
		}
		return false;
	}


	bool		m_bPhoneHome;
	netadr_t	m_cserIP;
	char		m_szBuildIdentifier[ 32 ];
	bool		m_bLevelStarted;
	unsigned int m_uSessionID;

	CBlockingUDPSocket *m_pSocket;
};

CPhoneHome g_PhoneHome;

IPhoneHome *phonehome = &g_PhoneHome;