-
Notifications
You must be signed in to change notification settings - Fork 33
/
fl.c
513 lines (418 loc) · 10.7 KB
/
fl.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
/*
* qstat
* by Steve Jankowski
*
* Frontlines-Fuel of War protocol
* Copyright 2008 Steven Hartland
*
* Licensed under the Artistic License, see LICENSE.txt for license terms
*
*/
#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include "debug.h"
#include "qstat.h"
#include "packet_manip.h"
#define FL_GETCHALLENGE "\xFF\xFF\xFF\xFF\x57"
#define FL_CHALLENGERESPONSE 0x41
#define FL_INFO "\xFF\xFF\xFF\xFF\x46LSQ"
#define FL_INFORESPONSE 0x49
#define FL_PLAYER "\xFF\xFF\xFF\xFF\x55"
#define FL_PLAYERRESPONSE 0x44
#define FL_RULES "\xFF\xFF\xFF\xFF\x56"
#define FL_RULESRESPONSE 0x45
struct fl_status {
unsigned sent_challenge : 1;
unsigned have_challenge : 1;
unsigned sent_info : 1;
unsigned have_info : 1;
unsigned sent_player : 1;
unsigned have_player : 1;
unsigned sent_rules : 1;
unsigned have_rules : 1;
unsigned challenge;
unsigned char type;
};
query_status_t
send_fl_request_packet(struct qserver *server)
{
struct fl_status *status = (struct fl_status *)server->master_query_tag;
if (SOCKET_ERROR == qserver_send_initial(server, FL_INFO, sizeof(FL_INFO) - 1)) {
return (SOCKET_ERROR);
}
status->sent_info = 1;
status->type = 0;
return (INPROGRESS);
}
query_status_t
send_fl_rule_request_packet(struct qserver *server)
{
struct fl_status *status = (struct fl_status *)server->master_query_tag;
if (1 >= status->type) {
// Not supported
return (PKT_ERROR);
}
if (!get_server_rules && !get_player_info) {
return (DONE_FORCE);
}
do {
if (!status->have_challenge) {
debug(3, "sending challenge");
if (SOCKET_ERROR == qserver_send_initial(server, FL_GETCHALLENGE, sizeof(FL_GETCHALLENGE) - 1)) {
return (SOCKET_ERROR);
}
status->sent_challenge = 1;
break;
} else if (get_server_rules && !status->have_rules) {
char buf[sizeof(FL_RULES) - 1 + 4] = FL_RULES;
memcpy(buf + sizeof(FL_RULES) - 1, &status->challenge, 4);
debug(3, "sending rule query");
if (SOCKET_ERROR == qserver_send_initial(server, buf, sizeof(buf))) {
return (SOCKET_ERROR);
}
status->sent_rules = 1;
break;
} else if (get_player_info && !status->have_player) {
char buf[sizeof(FL_PLAYER) - 1 + 4] = FL_PLAYER;
memcpy(buf + sizeof(FL_PLAYER) - 1, &status->challenge, 4);
debug(3, "sending player query");
if (SOCKET_ERROR == qserver_send_initial(server, buf, sizeof(buf))) {
return (SOCKET_ERROR);
}
status->sent_player = 1;
break;
} else {
debug(3, "timeout");
// we are probably called due to timeout, restart.
status->have_challenge = 0;
status->have_rules = 0;
}
} while (1);
return (INPROGRESS);
}
query_status_t
deal_with_fl_packet(struct qserver *server, char *rawpkt, int pktlen)
{
struct fl_status *status = (struct fl_status *)server->master_query_tag;
char *pkt = rawpkt;
char buf[16];
char *str;
unsigned cnt;
unsigned short tmp_short;
if (server->server_name == NULL) {
server->ping_total += time_delta(&packet_recv_time, &server->packet_time1);
server->n_requests++;
}
if (pktlen < 5) {
goto out_too_short;
}
if (0 == memcmp(pkt, "\xFF\xFF\xFF\xFE", 4)) {
// fragmented packet
unsigned char pkt_index, pkt_max;
unsigned int pkt_id;
SavedData *sdata;
if (pktlen < 9) {
goto out_too_short;
}
// format:
// int Header
// int RequestId
// byte PacketNumber
// byte NumPackets
// Short SizeOfPacketSplits
// Header
pkt += 4;
// RequestId
pkt_id = ntohl(*(long *)pkt);
debug(3, "RequestID: %d", pkt_id);
pkt += 4;
// The next two bytes are:
// 1. the max packets sent ( byte )
// 2. the index of this packet starting from 0 ( byte )
// 3. Size of the split ( short )
if (pktlen < 10) {
goto out_too_short;
}
// PacketNumber
pkt_index = ((unsigned char)*pkt);
// NumPackates
pkt_max = ((unsigned char)*(pkt + 1));
// SizeOfPacketSplits
debug(3, "packetid[2]: 0x%hhx => idx: %hhu, max: %hhu", *pkt, pkt_index, pkt_max);
pkt += 4;
pktlen -= 12;
// pkt_max is the total number of packets expected
// pkt_index is a bit mask of the packets received.
if (server->saved_data.data == NULL) {
sdata = &server->saved_data;
} else {
sdata = (SavedData *)calloc(1, sizeof(SavedData));
sdata->next = server->saved_data.next;
server->saved_data.next = sdata;
}
sdata->pkt_index = pkt_index;
sdata->pkt_max = pkt_max;
sdata->pkt_id = pkt_id;
sdata->datalen = pktlen;
sdata->data = (char *)malloc(pktlen);
if (NULL == sdata->data) {
malformed_packet(server, "Out of memory");
return (MEM_ERROR);
}
memcpy(sdata->data, pkt, sdata->datalen);
// combine_packets will call us recursively
return (combine_packets(server));
} else if (0 != memcmp(pkt, "\xFF\xFF\xFF\xFF", 4)) {
malformed_packet(server, "invalid packet header");
return (PKT_ERROR);
}
pkt += 4;
pktlen -= 4;
pktlen -= 1;
debug(2, "FL type = 0x%x", *pkt);
switch (*pkt++) {
case FL_CHALLENGERESPONSE:
if (pktlen < 4) {
goto out_too_short;
}
memcpy(&status->challenge, pkt, 4);
// do not count challenge as retry
if (!status->have_challenge && (server->retry1 != n_retries)) {
++server->retry1;
if (server->n_retries) {
--server->n_retries;
}
}
status->have_challenge = 1;
debug(3, "challenge %x", status->challenge);
break;
case FL_INFORESPONSE:
if (pktlen < 1) {
goto out_too_short;
}
status->type = *pkt;
if ((*pkt > 1) && (get_server_rules || get_player_info)) {
server->next_rule = ""; // trigger calling send_fl_rule_request_packet
}
snprintf(buf, sizeof(buf), "%hhX", *pkt);
add_rule(server, "protocol", buf, 0);
pktlen--;
pkt++;
// ServerName
str = memchr(pkt, '\0', pktlen);
if (!str) {
goto out_too_short;
}
server->server_name = strdup(pkt);
pktlen -= str - pkt + 1;
pkt += str - pkt + 1;
// MapName
str = memchr(pkt, '\0', pktlen);
if (!str) {
goto out_too_short;
}
server->map_name = strdup(pkt);
pktlen -= str - pkt + 1;
pkt += str - pkt + 1;
// ModName
str = memchr(pkt, '\0', pktlen);
if (!str) {
goto out_too_short;
}
server->game = strdup(pkt);
add_rule(server, "modname", pkt, 0);
pktlen -= str - pkt + 1;
pkt += str - pkt + 1;
// GameMode
str = memchr(pkt, '\0', pktlen);
if (!str) {
goto out_too_short;
}
add_rule(server, "gamemode", pkt, 0);
pktlen -= str - pkt + 1;
pkt += str - pkt + 1;
// GameDescription
str = memchr(pkt, '\0', pktlen);
if (!str) {
goto out_too_short;
}
add_rule(server, "gamedescription", pkt, 0);
pktlen -= str - pkt + 1;
pkt += str - pkt + 1;
// GameVersion
str = memchr(pkt, '\0', pktlen);
if (!str) {
goto out_too_short;
}
add_rule(server, "gameversion", pkt, 0);
pktlen -= str - pkt + 1;
pkt += str - pkt + 1;
if (pktlen < 13) {
goto out_too_short;
}
// GamePort
tmp_short = ((unsigned short)pkt[0] << 8) | ((unsigned short)pkt[1]);
change_server_port(server, tmp_short, 0);
pkt += 2;
// Num Players
server->num_players = (unsigned char)*pkt++;
// Max Players
server->max_players = (unsigned char)*pkt++;
// Dedicated
add_rule(server, "dedicated", ('d' == *pkt++) ? "1" : "0", 0);
// OS
switch (*pkt) {
case 'l':
add_rule(server, "sv_os", "linux", 0);
break;
case 'w':
add_rule(server, "sv_os", "windows", 0);
break;
default:
buf[0] = *pkt;
buf[1] = '\0';
add_rule(server, "sv_os", buf, 0);
break;
}
pkt++;
// Passworded
add_rule(server, "passworded", (*pkt++) ? "1" : "0", 0);
// Anticheat
add_rule(server, "passworded", (*pkt++) ? "1" : "0", 0);
// FrameTime
sprintf(buf, "%hhu", *pkt++);
add_rule(server, "frametime", buf, 0);
// Round
sprintf(buf, "%hhu", *pkt++);
add_rule(server, "round", buf, 0);
// RoundMax
sprintf(buf, "%hhu", *pkt++);
add_rule(server, "roundmax", buf, 0);
// RoundSeconds
tmp_short = ((unsigned short)pkt[0] << 8) | ((unsigned short)pkt[1]);
sprintf(buf, "%hu", tmp_short);
add_rule(server, "roundseconds", buf, 0);
pkt += 2;
status->have_info = 1;
server->retry1 = n_retries;
server->next_player_info = server->num_players;
break;
case FL_RULESRESPONSE:
if (pktlen < 2) {
goto out_too_short;
}
cnt = ((unsigned char)pkt[0] << 8) + ((unsigned char)pkt[1]);
pktlen -= 2;
pkt += 2;
debug(3, "num_rules: %d", cnt);
for ( ; cnt && pktlen > 0; --cnt) {
char *key, *value;
str = memchr(pkt, '\0', pktlen);
if (!str) {
break;
}
key = pkt;
pktlen -= str - pkt + 1;
pkt += str - pkt + 1;
str = memchr(pkt, '\0', pktlen);
if (!str) {
break;
}
value = pkt;
pktlen -= str - pkt + 1;
pkt += str - pkt + 1;
add_rule(server, key, value, NO_FLAGS);
}
if (cnt) {
malformed_packet(server, "packet contains too few rules, missing %d", cnt);
server->missing_rules = 1;
}
if (pktlen) {
malformed_packet(server, "garbage at end of rules, %d bytes left", pktlen);
}
status->have_rules = 1;
server->retry1 = n_retries;
break;
case FL_PLAYERRESPONSE:
if (pktlen < 1) {
goto out_too_short;
}
cnt = (unsigned char)pkt[0];
pktlen -= 1;
pkt += 1;
debug(3, "num_players: %d", cnt);
for ( ; cnt && pktlen > 0; --cnt) {
unsigned idx;
const char *name;
struct player *p;
// Index
idx = *pkt++;
--pktlen;
// PlayerName
str = memchr(pkt, '\0', pktlen);
if (!str) {
break;
}
name = pkt;
pktlen -= str - pkt + 1;
pkt += str - pkt + 1;
if (pktlen < 8) {
goto out_too_short;
}
debug(3, "player index %d", idx);
p = add_player(server, server->n_player_info);
if (p) {
union {
int i;
float fl;
}
temp;
p->name = strdup(name);
// Score
p->frags = ntohl(*(unsigned int *)pkt);
// TimeConnected
temp.i = ntohl(*(unsigned int *)(pkt + 4));
p->connect_time = temp.fl;
// Ping
p->ping = 0;
p->ping = ntohs(*(unsigned int *)(pkt + 8));
//((unsigned char*)&p->ping)[0] = pkt[9];
//((unsigned char*)&p->ping)[1] = pkt[8];
// ProfileId
//p->profileid = ntohl( *(unsigned int *)pkt+10 );
// Team
p->team = *(pkt + 14);
}
pktlen -= 15;
pkt += 15;
}
if (pktlen) {
malformed_packet(server, "garbage at end of player info, %d bytes left", pktlen);
}
status->have_player = 1;
server->retry1 = n_retries;
break;
default:
malformed_packet(server, "invalid packet id %hhx", *--pkt);
return (PKT_ERROR);
}
if (
(!get_player_info || (get_player_info && status->have_player)) &&
(!get_server_rules || (get_server_rules && status->have_rules))
) {
server->next_rule = NULL;
}
return (DONE_AUTO);
out_too_short:
malformed_packet(server, "packet too short");
return (PKT_ERROR);
}
// vim: sw=4 ts=4 noet