-
Notifications
You must be signed in to change notification settings - Fork 33
/
ut2004.c
638 lines (533 loc) · 15.3 KB
/
ut2004.c
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* qstat
* by Steve Jankowski
*
* UT2004 master query functions
* Copyright 2004 Ludwig Nussel
*
* Licensed under the Artistic License, see LICENSE.txt for license terms
*
* This code is inspired by ideas from 'Nurulwai'
*
*/
#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include "debug.h"
#include "qstat.h"
#include "md5.h"
/** \brief convert bytes into hex string
*
* \param in data to convert
* \param len length of data
* \param out location to store string to. Must be 2*len
*/
static void bin2hex(const char *in, size_t len, char *out);
#define CD_KEY_LENGTH 23
// arbitrary
#define MAX_LISTING_RECORD_LEN 0x04FF
#define RESPONSE_OFFSET_CDKEY 5
#define RESPONSE_OFFSET_CHALLENGE 39
static const char challenge_response[] =
{
0x68, 0x00, 0x00, 0x00, '!', 0xCD, 0xCD, 0xCD,
/* length | ! MD5SUM, CD is placeholder */
0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD,
0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD,
0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD,
0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0x00,
'!', 0xCD,
0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD,
0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD,
0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD,
0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0x00,
0x0c, 'U', 'T', '2', 'K', '4', 'C', 'L',
/*^^ 12 byte string */
'I', 'E', 'N', 'T', 0x00, 0xfb, 0x0c, 0x00,
/* | unknown */
0x00, 0x06, 0x04, 'i', 'n', 't', 0x00, 0x00,
/* | ^^ 4 byte string | ? */
0x00, 0x00, 0x00, 0xee, 0xee, 0x00, 0x00, 0x11,
/* unknown */
0x00, 0x00, 0x00, 0x01
};
static const char approved[] =
{
0x0e, 0x00, 0x00, 0x00, 0x09, 'A', 'P', 'P',
'R', 'O', 'V', 'E', 'D', 0x00, 0x03, 0x00,
0x00, 0x00
};
static const char approved_response[] =
{
0x22, 0x00, 0x00, 0x00, '!', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', 0x00
};
static const char verified[] =
{
0x0a, 0x00, 0x00, 0x00, 0x09, 'V', 'E', 'R',
'I', 'F', 'I', 'E', 'D', 0x00
};
#if 0
struct server_listing_record_head {
unsigned len;
unsigned ip;
short port;
short queryport;
char name[];
// char map[]
};
struct server_listing_record_foot {
unsigned char marker1[3];
unsigned char unknown1;
unsigned char maxplayers;
unsigned char unknown2[4];
unsigned char marker2[3];
};
#endif
static char cdkey[CD_KEY_LENGTH + 1] = "";
enum ut2004_state {
STATE_CHALLENGE = 0x00,
STATE_APPROVED = 0x01,
STATE_VERIFIED = 0x02,
STATE_LISTING = 0x03,
};
query_status_t
send_ut2004master_request_packet(struct qserver *server)
{
int ret;
if (server->n_packets) {
return (DONE_FORCE);
}
if (!*cdkey) {
char *param = get_param_value(server, "cdkey", NULL);
if (!param) {
debug(0, "Error: missing cdkey parameter");
server->server_name = SYSERROR;
return (PKT_ERROR);
}
if (strstr(param, "/") != NULL || strstr(param, "\\") != NULL) {
FILE *fp = fopen(param, "r");
if (!fp || (fread(cdkey, 1, CD_KEY_LENGTH, fp) != CD_KEY_LENGTH)) {
debug(0, "Error: can't key from %s", param);
server->server_name = SYSERROR;
if (fp) {
fclose(fp);
}
return (PKT_ERROR);
}
fclose(fp);
} else if (strchr(param, '-') && (strlen(param) == CD_KEY_LENGTH)) {
memcpy(cdkey, param, CD_KEY_LENGTH);
} else if ((*param == '$') &&
(param = getenv(param + 1)) && // replaces param!
(strlen(param) == CD_KEY_LENGTH)) {
memcpy(cdkey, param, CD_KEY_LENGTH);
} else {
debug(0, "Error: invalid cdkey parameter");
server->server_name = SYSERROR;
return (PKT_ERROR);
}
}
ret = qserver_send(server, NULL, 0);
#if 0
// XXX since we do not send but rather expect a reply directly after
// connect it's pointless to retry doing nothing
debug(0, "retry1: %d", server->retry1);
server->retry1 = 0;
#endif
server->master_query_tag[0] = STATE_CHALLENGE;
return (ret);
}
static void
ut2004_server_done(struct qserver *server)
{
if (server->saved_data.next) {
debug(0, "%d bytes of unprocessed data left. Premature EOF!?", server->saved_data.next->datalen);
free(server->saved_data.next->data);
free(server->saved_data.next);
server->saved_data.next = NULL;
}
}
// we use n_servers to store number of used bytes in master_pkt so
// it needs to be divided by 6 when finished
static void
ut2004_parse_record(struct qserver *server, char *pkt)
{
char *dest;
#if 0
unsigned ip;
unsigned short port;
memcpy(&ip, pkt + 4, 4);
port = swap_short_from_little(pkt + 4 + 4);
debug(2, "got %d.%d.%d.%d:%hu", ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff, port);
#endif
if (server->n_servers + 6 > server->master_pkt_len) {
if (!server->master_pkt_len) {
server->master_pkt_len = 180;
} else {
server->master_pkt_len *= 2;
}
server->master_pkt = (char *)realloc(server->master_pkt, server->master_pkt_len);
}
dest = server->master_pkt + server->n_servers;
memcpy(dest, pkt + 4, 4);
dest[4] = pkt[9];
dest[5] = pkt[8];
server->n_servers += 6;
}
static char *
put_bytes(char *buf, const char *bytes, size_t len, size_t *left)
{
if (!buf || (len > *left)) {
return (NULL);
}
memcpy(buf, bytes, len);
*left -= len;
return (buf + len);
}
static char *
put_string(char *buf, const char *string, size_t *left)
{
size_t len = strlen(string) + 1;
char l;
if (!buf || (len > 0xFF) || (*left < len + 1)) {
return (NULL);
}
l = len;
buf = put_bytes(buf, &l, 1, left);
return (put_bytes(buf, string, len, left));
}
/** \brief assemble the server filter and send the master query
*
* the query consists of four bytes length (excluding the four length bytes), a
* null byte and then the number of item pairs that follow.
*
* Each pair consists of two ut2 strings (length+null terminated string)
* followed by a byte which is either zero or 0x04 which means negate the query
* (e.g. not zero curplayers means not empty).
*/
static int
ut2004_send_query(struct qserver *server)
{
char buf[4096] = { 0 };
size_t left = sizeof(buf);
char *b = buf;
char *param, *r, *sep = "";
unsigned flen = 0;
unsigned char items = 0;
// header is done later
b += 6;
left -= 6;
param = get_param_value(server, "gametype", NULL);
if (param) {
++items;
b = put_string(b, "gametype", &left);
b = put_string(b, param, &left);
b = put_bytes(b, "", 1, &left);
}
param = get_param_value(server, "status", NULL);
r = param;
while (param && sep) {
sep = strchr(r, ':');
if (sep) {
flen = sep - r;
} else {
flen = strlen(r);
}
if ((strncmp(r, "standard", flen) == 0) ||
(strncmp(r, "nostandard", flen) == 0)) {
++items;
b = put_string(b, "standard", &left);
if (*r == 'n') {
b = put_string(b, "false", &left);
} else {
b = put_string(b, "true", &left);
}
b = put_bytes(b, "", 1, &left);
} else if ((strncmp(r, "password", flen) == 0) ||
(strncmp(r, "nopassword", flen) == 0)) {
++items;
b = put_string(b, "password", &left);
if (*r == 'n') {
b = put_string(b, "false", &left);
} else {
b = put_string(b, "true", &left);
}
b = put_bytes(b, "", 1, &left);
} else if (strncmp(r, "notempty", flen) == 0) {
++items;
b = put_string(b, "currentplayers", &left);
b = put_string(b, "0", &left);
b = put_bytes(b, "\x04", 1, &left);
} else if (strncmp(r, "notfull", flen) == 0) {
++items;
b = put_string(b, "freespace", &left);
b = put_string(b, "0", &left);
b = put_bytes(b, "\x04", 1, &left);
} else if (strncmp(r, "nobots", flen) == 0) {
++items;
b = put_string(b, "nobots", &left);
b = put_string(b, "true", &left);
b = put_bytes(b, "", 1, &left);
} else if ((strncmp(r, "stats", flen) == 0) ||
(strncmp(r, "nostats", flen) == 0)) {
++items;
b = put_string(b, "stats", &left);
if (*r == 'n') {
b = put_string(b, "false", &left);
} else {
b = put_string(b, "true", &left);
}
b = put_bytes(b, "", 1, &left);
} else if ((strncmp(r, "weaponstay", flen) == 0) ||
(strncmp(r, "noweaponstay", flen) == 0)) {
++items;
b = put_string(b, "weaponstay", &left);
if (*r == 'n') {
b = put_string(b, "false", &left);
} else {
b = put_string(b, "true", &left);
}
b = put_bytes(b, "", 1, &left);
} else if ((strncmp(r, "transloc", flen) == 0) ||
(strncmp(r, "notransloc", flen) == 0)) {
++items;
b = put_string(b, "transloc", &left);
if (*r == 'n') {
b = put_string(b, "false", &left);
} else {
b = put_string(b, "true", &left);
}
b = put_bytes(b, "", 1, &left);
}
r = sep + 1;
}
param = get_param_value(server, "mutator", NULL);
r = param;
sep = "";
while (param && sep) {
char neg = '\0';
unsigned char l;
sep = strchr(r, ':');
if (sep) {
flen = sep - r;
} else {
flen = strlen(r);
}
if (*r == '-') {
neg = '\x04';
++r;
--flen;
}
if (!flen) {
continue;
}
b = put_string(b, "mutator", &left);
l = flen + 1;
b = put_bytes(b, (char *)&l, 1, &left);
b = put_bytes(b, r, flen, &left);
b = put_bytes(b, "", 1, &left);
b = put_bytes(b, &neg, 1, &left);
++items;
r = sep + 1;
}
if (!b) {
debug(0, "Error: query buffer too small. Please file a bug report!");
return (0);
}
put_long_little(b - buf - 4, buf);
buf[5] = items;
return (qserver_send(server, buf, sizeof(buf) - left) > 0);
}
query_status_t
deal_with_ut2004master_packet(struct qserver *server, char *rawpkt, int pktlen)
{
unsigned char *state = (unsigned char *)&server->master_query_tag[0];
md5_state_t md5;
if (!pktlen) {
ut2004_server_done(server);
goto cleanup_out;
}
server->ping_total += time_delta(&packet_recv_time, &server->packet_time1);
switch (*state) {
case STATE_CHALLENGE:
// ensure at least one byte challenge, fit into buffer,
// match challenge, null terminated
if ((pktlen < 4 + 1 + 1 + 1) ||
(pktlen > 4 + 1 + 8 + 1) ||
(rawpkt[pktlen - 1] != '\0')) {
malformed_packet(server, "invalid challenge");
goto cleanup_out;
} else {
char response[sizeof(challenge_response)];
char *challenge = rawpkt + 5;
char sum[16];
memcpy(response, challenge_response, sizeof(challenge_response));
debug(2, "challenge: %s", challenge);
md5_init(&md5);
md5_append(&md5, (unsigned char *)cdkey, CD_KEY_LENGTH);
md5_finish(&md5, (unsigned char *)sum);
bin2hex(sum, 16, response + RESPONSE_OFFSET_CDKEY);
md5_init(&md5);
md5_append(&md5, (unsigned char *)cdkey, CD_KEY_LENGTH);
md5_append(&md5, (unsigned char *)challenge, strlen(challenge));
md5_finish(&md5, (unsigned char *)sum);
bin2hex(sum, 16, response + RESPONSE_OFFSET_CHALLENGE);
if (qserver_send(server, response, sizeof(response))) {
goto cleanup_out;
}
server->server_name = MASTER;
*state = STATE_APPROVED;
}
break;
case STATE_APPROVED:
if ((pktlen != sizeof(approved)) ||
(0 != memcmp(rawpkt, approved, pktlen))) {
malformed_packet(server, "CD key not approved");
goto cleanup_out;
}
debug(2, "got approval, sending verify");
if (qserver_send(server, approved_response, sizeof(approved_response))) {
goto cleanup_out;
}
*state = STATE_VERIFIED;
break;
case STATE_VERIFIED:
if ((pktlen != sizeof(verified)) ||
(0 != memcmp(rawpkt, verified, pktlen))) {
malformed_packet(server, "CD key not verified");
goto cleanup_out;
}
debug(2, "CD key verified, sending query");
if (ut2004_send_query(server)) {
goto cleanup_out;
}
*state = STATE_LISTING;
break;
case STATE_LISTING:
// first packet. contains number of servers to expect
if (!server->saved_data.pkt_id) {
/*
* server->saved_data.data = malloc(pktlen);
* memcpy(server->saved_data.data, rawpkt, pktlen);
* server->saved_data.datalen = pktlen;
*/
server->saved_data.pkt_id = 1;
if (pktlen == 9) {
unsigned num = swap_long_from_little(rawpkt + 4);
debug(2, "expect %u servers", num);
#if 1
if (num < 10000) {
server->master_pkt_len = num * 6;
server->master_pkt = (char *)realloc(server->master_pkt, server->master_pkt_len);
}
#endif
}
} else if (pktlen < 4) {
malformed_packet(server, "packet too short");
goto cleanup_out;
} else {
char *p = rawpkt;
unsigned recordlen = 0;
if (server->saved_data.next) {
unsigned need = 0;
SavedData *data = server->saved_data.next;
// nasty, four bytes of record length are split up. since
// we alloc'ed at least four bytes we just copy the 4-x
// bytes to data->data
if (data->datalen < 4) {
need = 4 - data->datalen;
debug(2, "need %d bytes more for recordlen", need);
if (need > pktlen) {
// XXX ok, im lazy now. Stupid server can't even
// send four bytes in a row
malformed_packet(server, "chunk too small");
goto cleanup_out;
}
memcpy(data->data + data->datalen, p, need);
p += need;
data->datalen = 4;
}
recordlen = swap_long_from_little(data->data);
if (!recordlen || (recordlen > MAX_LISTING_RECORD_LEN)) {
malformed_packet(server,
"record lengthx %x out of range, position %d", recordlen, (int)(p - rawpkt));
goto cleanup_out;
}
need = 4 + recordlen - data->datalen;
debug(2, "recordlen: %d, saved: %d, pkglen: %d, needed: %d", recordlen, data->datalen, pktlen, need);
if (need <= pktlen) {
data->data = realloc(data->data, 4 + recordlen);
memcpy(data->data + data->datalen, p, need);
ut2004_parse_record(server, data->data);
p += need;
free(data->data);
free(data);
server->saved_data.next = NULL;
}
}
while (!server->saved_data.next && p - rawpkt + 4 < pktlen) {
recordlen = swap_long_from_little(p);
// record too large
if (!recordlen || (recordlen > MAX_LISTING_RECORD_LEN)) {
malformed_packet(server,
"record length %x out of range, position %d", recordlen, (int)(p - rawpkt));
goto cleanup_out;
}
// recordlen itself is four bytes
recordlen += 4;
// record fully inside packet
if (p - rawpkt + recordlen <= pktlen) {
ut2004_parse_record(server, p);
p += recordlen;
} else {
break;
}
}
// record continues in next packet. save it.
if (p - rawpkt < pktlen) {
SavedData *data = server->saved_data.next;
unsigned tosave = pktlen - (p - rawpkt);
if (!data) {
data = malloc(sizeof(SavedData));
data->data = malloc(tosave < 4 ? 4 : tosave); // alloc at least four bytes
data->datalen = tosave;
memcpy(data->data, p, data->datalen);
data->next = NULL;
server->saved_data.next = data;
debug(1, "saved %d bytes", data->datalen);
} else {
data->data = realloc(data->data, data->datalen + tosave);
memcpy(data->data + data->datalen, p, tosave);
data->datalen += tosave;
debug(1, "saved %d bytes (+)", data->datalen);
}
}
}
break;
}
debug(2, "%d servers total", server->n_servers / 6);
return (INPROGRESS);
cleanup_out:
server->master_pkt_len = server->n_servers;
server->n_servers /= 6;
return (DONE_FORCE);
}
static const char hexchar[] = "0123456789abcdef";
static void
bin2hex(const char *in, size_t len, char *out)
{
char *o = out + len * 2;
in += len;
do {
*--o = hexchar[*--in & 0x0F];
*--o = hexchar[(*in >> 4) & 0x0F];
} while (o != out);
}
// vim: sw=4 ts=4 noet