summaryrefslogtreecommitdiff
path: root/bouncer/client.cpp
blob: deb3e7f22cc7d7f885c476c78f09ea3695b495a7 (plain)
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
#include "core.h"

static bool isNullSessionKey(uint8_t *key) {
	for (int i = 0; i < SESSION_KEY_SIZE; i++)
		if (key[i] != 0)
			return false;

	return true;
}



Client::Client(NetCore *_netCore) : SocketRWCommon(_netCore) {
	authState = AS_LOGIN_WAIT;
	memset(sessionKey, 0, sizeof(sessionKey));
	readBufPosition = 0;

	nextPacketID = 1;
	lastReceivedPacketID = 0;
}
Client::~Client() {
	std::list<Packet *>::iterator
		i = packetCache.begin(),
		  e = packetCache.end();

	for (; i != e; ++i)
		delete *i;
}


void Client::startService(int _sock, bool withTls) {
	close();

	sock = _sock;

	if (!setSocketNonBlocking(sock)) {
		perror("[Client::startService] Could not set non-blocking");
		close();
		return;
	}

#ifdef USE_GNUTLS
	if (withTls) {
		int initRet = gnutls_init(&tls, GNUTLS_SERVER);
		if (initRet != GNUTLS_E_SUCCESS) {
			printf("[Client::startService] gnutls_init borked\n");
			gnutls_perror(initRet);
			close();
			return;
		}

		// TODO: error check this
		int ret;
		const char *errPos;

		ret = gnutls_priority_set_direct(tls, "PERFORMANCE:%SERVER_PRECEDENCE", &errPos);
		if (ret != GNUTLS_E_SUCCESS) {
			printf("gnutls_priority_set_direct failure: %s\n", gnutls_strerror(ret));
			close();
			return;
		}

		ret = gnutls_credentials_set(tls, GNUTLS_CRD_CERTIFICATE, g_clientCreds);
		if (ret != GNUTLS_E_SUCCESS) {
			printf("gnutls_credentials_set failure: %s\n", gnutls_strerror(ret));
			close();
			return;
		}

		gnutls_certificate_server_set_request(tls, GNUTLS_CERT_IGNORE);

		gnutls_transport_set_int(tls, sock);

		tlsActive = true;

		state = CS_TLS_HANDSHAKE;

		printf("[fd=%d] preparing for TLS handshake\n", sock);
	} else
#endif
	{
		state = CS_CONNECTED;
	}
}

void Client::close() {
	SocketRWCommon::close();

	if (authState == AS_AUTHED)
		deadTime = time(NULL) + netCore->sessionKeepalive;
	else
		deadTime = time(NULL) - 1; // kill instantly
}


void Client::generateSessionKey() {
	time_t now = time(NULL);

	while (true) {
		for (int i = 0; i < SESSION_KEY_SIZE; i++) {
			if (i < sizeof(time_t))
				sessionKey[i] = ((uint8_t*)&now)[i];
			else
				sessionKey[i] = rand() & 255;
		}

		// Is any other client already using this key?
		// It's ridiculously unlikely, but... probably best
		// to check just in case!
		bool foundMatch = false;

		for (int i = 0; i < netCore->clientCount; i++) {
			if (netCore->clients[i] != this) {
				if (!memcmp(netCore->clients[i]->sessionKey, sessionKey, SESSION_KEY_SIZE))
					foundMatch = true;
			}
		}

		// If there's none, we can safely leave!
		if (!foundMatch)
			break;
	}
}

void Client::clearCachedPackets(int maxID) {
	packetCache.remove_if([maxID](Packet *&pkt) {
		if (pkt->id <= maxID) {
			delete pkt;
			return true;
		} else {
			return false;
		}
	});
}


void Client::handlePacket(Packet::Type type, char *data, int size) {
	Buffer pkt;
	pkt.useExistingBuffer(data, size);

	printf("[fd=%d] Packet : type %d, size %d\n", sock, type, size);

	if (authState == AS_LOGIN_WAIT) {
		if (type == Packet::C2B_OOB_LOGIN) {
			int error = 0;

			uint32_t protocolVersion = pkt.readU32();
			if (protocolVersion != PROTOCOL_VERSION)
				error = 1;

			uint32_t lastReceivedByClient = pkt.readU32();

			char pwBuf[512] = "";
			pkt.readStr(pwBuf, sizeof(pwBuf));
			if (strcmp(pwBuf, netCore->bouncerPassword.c_str()) != 0)
				error = 3;

			if (!pkt.readRemains(SESSION_KEY_SIZE))
				error = 2;



			if (error != 0) {
				// Send an error...
				Buffer pkt;
				pkt.writeU32(error);
				sendPacket(Packet::B2C_OOB_LOGIN_FAILED, pkt, /*allowUnauthed=*/true);

				// Would close() now but this means the login failed packet never gets sent
				// need to figure out a fix for this. TODO FIXME etc etc.

			} else {
				// or log us in!
				uint8_t reqKey[SESSION_KEY_SIZE];
				pkt.read((char *)reqKey, SESSION_KEY_SIZE);

				printf("[fd=%d] Client authenticating\n", sock);

				if (!isNullSessionKey(reqKey)) {
					printf("[fd=%d] Trying to resume session...", sock);
					printf("(last they received = %d)\n", lastReceivedByClient);

					Client *other = netCore->findClientWithSessionKey(reqKey);
					printf("[fd=%d] Got client %p\n", sock, other);

					if (other && other->authState == AS_AUTHED) {
						printf("Valid: last packet we sent = %d\n", other->nextPacketID - 1);
						// Yep, we can go!
						other->resumeSession(this, lastReceivedByClient);
						return;
					}
				}

				// If we got here, it means we couldn't resume the session.
				// Start over.
				printf("[fd=%d] Creating new session\n", sock);

				generateSessionKey();
				authState = AS_AUTHED;

				Buffer pkt;
				pkt.append((char *)sessionKey, SESSION_KEY_SIZE);
				sendPacket(Packet::B2C_OOB_LOGIN_SUCCESS, pkt);

				sessionStartEvent();
			}

		} else {
			printf("[fd=%d] Unrecognised packet in AS_LOGIN_WAIT authstate: type %d, size %d\n",
					sock, type, size);
		}
	} else if (authState == AS_AUTHED) {
		packetReceivedEvent(type, pkt);
	}
}

void Client::processReadBuffer() {
	// Try to process as many packets as we have in inputBuf

	// Basic header is 8 bytes
	// Extended (non-OOB) header is 16 bytes
	inputBuf.readSeek(0);
	readBufPosition = 0;

	while (inputBuf.readRemains(8)) {
		// We have 8 bytes, so we can try to read a basic header
		Packet::Type type = (Packet::Type)inputBuf.readU16();
		int reserved = inputBuf.readU16();
		uint32_t packetSize = inputBuf.readU32();
		bool silentlyIgnore = false;

		// Do we now have the whole packet in memory...?
		int extHeaderSize = (type & Packet::T_OUT_OF_BAND_FLAG) ? 0 : 8;

		if (!inputBuf.readRemains(packetSize + extHeaderSize))
			break;


		if (!(type & Packet::T_OUT_OF_BAND_FLAG)) {
			// Handle packet system things for non-OOB packets
			uint32_t packetID = inputBuf.readU32();
			uint32_t lastReceivedByClient = inputBuf.readU32();

			if (packetID > lastReceivedPacketID) {
				// This is a new packet
				lastReceivedPacketID = packetID;
			} else {
				// We've already seen this packet, silently ignore it!
				silentlyIgnore = true;
			}

			clearCachedPackets(lastReceivedByClient);
		}

		// Yep, we can process it!

		// Save the position of the next packet
		readBufPosition = inputBuf.readTell() + packetSize;

		if (!silentlyIgnore)
			handlePacket(type, &inputBuf.data()[inputBuf.readTell()], packetSize);

		inputBuf.readSeek(readBufPosition);
	}

	// If we managed to handle anything, lop it off the buffer
	inputBuf.trimFromStart(readBufPosition);
	readBufPosition = 0;
}


void Client::resumeSession(Client *other, int lastReceivedByClient) {
	close();

	inputBuf.clear();
	inputBuf.append(
			&other->inputBuf.data()[other->readBufPosition],
			other->inputBuf.size() - other->readBufPosition);

	// Not sure if we need to copy the outputbuf but it can't hurt
	outputBuf.clear();
	outputBuf.append(other->outputBuf.data(), other->outputBuf.size());

	sock = other->sock;
	state = other->state;
#ifdef USE_GNUTLS
	tls = other->tls;
	tlsActive = other->tlsActive;
#endif

	other->sock = -1;
	other->state = CS_DISCONNECTED;
#ifdef USE_GNUTLS
	other->tls = 0;
	other->tlsActive = false;
#endif

	other->close();

	// Now send them everything we've got!
	Buffer pkt;
	pkt.writeU32(lastReceivedPacketID);
	sendPacket(Packet::B2C_OOB_SESSION_RESUMED, pkt);

	clearCachedPackets(lastReceivedByClient);

	std::list<Packet*>::iterator
		i = packetCache.begin(),
		  e = packetCache.end();

	for (; i != e; ++i)
		sendPacketOverWire(*i);
}

void Client::sendPacket(Packet::Type type, const Buffer &data, bool allowUnauthed) {
	Packet *packet = new Packet;
	packet->type = type;
	packet->data.append(data);

	if (type & Packet::T_OUT_OF_BAND_FLAG) {
		packet->id = 0;
	} else {
		packet->id = nextPacketID;
		nextPacketID++;
	}

	if (state == CS_CONNECTED)
		if (authState == AS_AUTHED || allowUnauthed)
			sendPacketOverWire(packet);

	if (type & Packet::T_OUT_OF_BAND_FLAG)
		delete packet;
	else
		packetCache.push_back(packet);
}

void Client::sendPacketOverWire(const Packet *packet) {
	Buffer header;
	header.writeU16(packet->type);
	header.writeU16(0);
	header.writeU32(packet->data.size());

	if (!(packet->type & Packet::T_OUT_OF_BAND_FLAG)) {
		header.writeU32(packet->id);
		header.writeU32(lastReceivedPacketID);
	}

	outputBuf.append(header);
	outputBuf.append(packet->data);
}