summaryrefslogtreecommitdiff
path: root/engine/sv_uploaddata.cpp
blob: 979b4ceb4c5c208c708728157b08de42ab0eb940 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//


#if defined(_WIN32) && !defined(_X360)
#include <winsock.h>
#elif POSIX
#include <sys/socket.h>
#include <netinet/in.h>
#elif !defined(_X360)
#error "define socket.h"
#endif
#include "host.h"
#include "blockingudpsocket.h"
#include "cserserverprotocol_engine.h"
#include "KeyValues.h"
#include "bitbuf.h"
#include "mathlib/IceKey.H"
#include "net.h"

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

static int CountFields( KeyValues *fields )
{
	int c = 0;
	KeyValues *kv = fields->GetFirstSubKey();
	while ( kv )
	{
		c++;
		kv = kv->GetNextKey();
	}
	return c;
}

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

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
static 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;
	}
}

static void BuildUploadDataMessage( bf_write& buf, char const *tablename, KeyValues *fields )
{
	bf_write	encrypted;
	ALIGN4 byte		encrypted_data[ 2048 ] ALIGN4_POST;

	buf.WriteByte( C2M_UPLOADDATA );
	buf.WriteByte( '\n' );
	buf.WriteByte( C2M_UPLOADDATA_PROTOCOL_VERSION );

	// encryption object
	IceKey cipher(1); /* medium encryption level */
	unsigned char ucEncryptionKey[8] = { 54, 175, 165, 5, 76, 251, 29, 113 };
	cipher.set( ucEncryptionKey );

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

	byte corruption_identifier = 0x01;

	encrypted.WriteByte( corruption_identifier );

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

	encrypted.WriteString( tablename ); 

	int fieldCount = CountFields( fields );

	if ( fieldCount > 255 )
	{
		Host_Error( "Too many fields in uploaddata (%i max = 255)\n", fieldCount );
	}

	encrypted.WriteByte( (byte)fieldCount );

	KeyValues *kv = fields->GetFirstSubKey();
	while ( kv )
	{
		encrypted.WriteString( kv->GetName() );
		encrypted.WriteString( kv->GetString() );

		kv = kv->GetNextKey();
	}

	// Round to multiple of 8 for encrypted
	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() );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *cserIP - 
//			*tablename - 
//			*fields - 
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool UploadData( char const *cserIP, char const *tablename, KeyValues *fields )
{
#ifndef _XBOX
	bf_write	buf;
	ALIGN4 byte		data[ 2048 ] ALIGN4_POST;
		
	buf.StartWriting( data, sizeof( data ) );

	BuildUploadDataMessage( buf, tablename, fields );

	netadr_t cseradr;

	if ( NET_StringToAdr( cserIP, &cseradr ) )
	{
		CBlockingUDPSocket *socket = new CBlockingUDPSocket();
		if ( socket )
		{
			struct sockaddr_in sa;
			cseradr.ToSockadr( (struct sockaddr *)&sa );

			// Don't bother waiting for response here
			socket->SendSocketMessage( sa, (const byte *)buf.GetData(), buf.GetNumBytesWritten() );
			delete socket;

			return true;
		}
	}

	return false;
#else
	return true;
#endif
}

/*
CON_COMMAND( datatest, "" )
{
	KeyValues *kv = new KeyValues( "data" );
	kv->SetString( "IDHash", "abcdefg" );
	kv->SetString( "Time", "DATETIME" );
	kv->SetString( "DXDeviceID", "1001" );
	kv->SetString( "DXVendorID", "1001" );
	kv->SetString( "Framerate", "999" );
	kv->SetString( "BuildNumber", va( "%i", build_number() ) );

	bool bret = UploadData( "127.0.0.1:27013", "benchmark", kv );

	kv->deleteThis();
}
*/