-
Notifications
You must be signed in to change notification settings - Fork 33
/
gps.c
372 lines (336 loc) · 9.81 KB
/
gps.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
/*
* qstat
* by Steve Jankowski
*
* Gamespy query protocol
* Copyright 2005 Steven Hartland
*
* Licensed under the Artistic License, see LICENSE.txt for license terms
*
*/
#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "debug.h"
#include "qstat.h"
#include "packet_manip.h"
int
gps_max_players(struct qserver *server)
{
struct player *player;
int no_players = 0;
if (0 == server->num_players) {
return (0);
}
for (player = server->players; player; player = player->next) {
no_players++;
}
return ((no_players < server->num_players) ? 1 : 0);
}
int
gps_player_info_key(char *s, char *end)
{
static char *keys[] =
{
"frags_", "team_", "ping_", "species_",
"race_", "deaths_", "score_", "enemy_",
"player_", "keyhash_", "teamname_",
"playername_", "keyhash_", "kills_", "queryid"
};
int i;
for (i = 0; i < sizeof(keys) / sizeof(char *); i++) {
int len = strlen(keys[i]);
if ((s + len < end) && (strncmp(s, keys[i], len) == 0)) {
return (len);
}
}
return (0);
}
query_status_t
send_gps_request_packet(struct qserver *server)
{
return (send_packet(server, server->type->status_packet, server->type->status_len));
}
query_status_t
deal_with_gps_packet(struct qserver *server, char *rawpkt, int pktlen)
{
char *s, *key, *value, *end;
struct player *player = NULL;
int id_major = 0, id_minor = 0, final = 0, player_num;
char tmp[256];
debug(2, "processing...");
server->n_servers++;
if (server->server_name == NULL) {
server->ping_total += time_delta(&packet_recv_time, &server->packet_time1);
} else {
gettimeofday(&server->packet_time1, NULL);
}
/*
* // We're using the saved_data a bit differently here to track received
* // packets.
* // pkt_id is the id_major from the \queryid\
* // pkt_max is the total number of packets expected
* // pkt_index is a bit mask of the packets received. The id_minor of
* // \queryid\ provides packet numbers (1 through pkt_max).
*/
if (server->saved_data.pkt_index == -1) {
server->saved_data.pkt_index = 0;
}
rawpkt[pktlen] = '\0';
end = &rawpkt[pktlen];
s = rawpkt;
while (*s) {
// find the '\'
while (*s && *s == '\\') {
s++;
}
if (!*s) {
// out of packet
break;
}
// Start of key
key = s;
// while we still have data and its not a '\'
while (*s && *s != '\\') {
s++;
}
if (!*s) {
// out of packet
break;
}
// Terminate the key
*s++ = '\0';
// Now for the value
value = s;
// while we still have data and its not a '\'
while (*s && *s != '\\') {
s++;
}
if (s[0] && s[1]) {
//fprintf( stderr, "%s = %s\n", key, value );
if (!isalpha((unsigned char)s[1])) {
// escape char?
s++;
// while we still have data and its not a '\'
while (*s && *s != '\\') {
s++;
}
} else if (
isalpha((unsigned char)s[1]) &&
(0 == strncmp(key, "player_", 7)) &&
(0 != strcmp(key, "player_flags"))
) {
// possible '\' in player name
if (!gps_player_info_key(s + 1, end)) {
// yep there was an escape in the player name
s++;
// while we still have data and its not a '\'
while (*s && *s != '\\') {
s++;
}
}
}
}
if (*s) {
*s++ = '\0';
}
//fprintf( stderr, "%s = %s\n", key, value );
if (*value == '\0') {
if (strcmp(key, "final") == 0) {
final = 1;
if (id_minor > server->saved_data.pkt_max) {
server->saved_data.pkt_max = id_minor;
}
continue;
}
}
/* This must be done before looking for player info because
* "queryid" is a valid according to gps_player_info_key().
*/
if (strcmp(key, "queryid") == 0) {
sscanf(value, "%d.%d", &id_major, &id_minor);
if (server->saved_data.pkt_id == 0) {
server->saved_data.pkt_id = id_major;
}
if (id_major == server->saved_data.pkt_id) {
if (id_minor > 0) {
// pkt_index is bitmask of packets recieved
server->saved_data.pkt_index |= 1 << (id_minor - 1);
}
if (final && (id_minor > server->saved_data.pkt_max)) {
server->saved_data.pkt_max = id_minor;
}
}
continue;
}
if (player == NULL) {
int len = gps_player_info_key(key, end);
if (len) {
// We have player info
int player_number = atoi(key + len);
player = get_player_by_number(server, player_number);
// && gps_max_players( server ) due to bf1942 issue
// where the actual no players is correct but more player
// details are returned
if ((player == NULL) && gps_max_players(server)) {
player = add_player(server, player_number);
if (player) {
// init to -1 so we can tell if
// we have team info
player->team = -1;
player->deaths = -999;
}
}
}
}
if ((strcmp(key, "mapname") == 0) && !server->map_name) {
server->map_name = strdup(value);
} else if ((strcmp(key, "hostname") == 0) && !server->server_name) {
server->server_name = strdup(value);
} else if (strcmp(key, "hostport") == 0) {
change_server_port(server, atoi(value), 0);
} else if (strcmp(key, "maxplayers") == 0) {
server->max_players = atoi(value);
} else if (strcmp(key, "numplayers") == 0) {
server->num_players = atoi(value);
} else if ((strcmp(key, server->type->game_rule) == 0) && !server->game) {
server->game = strdup(value);
add_rule(server, key, value, NO_FLAGS);
} else if (strcmp(key, "final") == 0) {
final = 1;
if (id_minor > server->saved_data.pkt_max) {
server->saved_data.pkt_max = id_minor;
}
continue;
} else if ((strncmp(key, "player_", 7) == 0) || (strncmp(key, "playername_", 11) == 0)) {
int no;
if (strncmp(key, "player_", 7) == 0) {
no = atoi(key + 7);
} else {
no = atoi(key + 11);
}
if (player && (player->number == no)) {
player->name = strdup(value);
player = NULL;
} else if (NULL != (player = get_player_by_number(server, no))) {
player->name = strdup(value);
player = NULL;
} else if (gps_max_players(server)) {
// gps_max_players( server ) due to bf1942 issue
// where the actual no players is correct but more player
// details are returned
player = add_player(server, no);
if (player) {
player->name = strdup(value);
// init to -1 so we can tell if
// we have team info
player->team = -1;
player->deaths = -999;
}
}
} else if (strncmp(key, "teamname_", 9) == 0) {
// Yes plus 1 BF1942 is a silly
players_set_teamname(server, atoi(key + 9) + 1, value);
} else if (strncmp(key, "team_t", 6) == 0) {
players_set_teamname(server, atoi(key + 6), value);
} else if (strncmp(key, "frags_", 6) == 0) {
player = get_player_by_number(server, atoi(key + 6));
if (NULL != player) {
player->frags = atoi(value);
}
} else if (strncmp(key, "kills_", 6) == 0) {
player = get_player_by_number(server, atoi(key + 6));
if (NULL != player) {
player->frags = atoi(value);
}
} else if (strncmp(key, "team_", 5) == 0) {
player = get_player_by_number(server, atoi(key + 5));
if (NULL != player) {
if (!isdigit((unsigned char)*value)) {
player->team_name = strdup(value);
} else {
player->team = atoi(value);
}
server->flags |= FLAG_PLAYER_TEAMS;
}
} else if (strncmp(key, "skin_", 5) == 0) {
player = get_player_by_number(server, atoi(key + 5));
if (NULL != player) {
player->skin = strdup(value);
}
} else if (strncmp(key, "mesh_", 5) == 0) {
player = get_player_by_number(server, atoi(key + 5));
if (NULL != player) {
player->mesh = strdup(value);
}
} else if (strncmp(key, "ping_", 5) == 0) {
player = get_player_by_number(server, atoi(key + 5));
if (NULL != player) {
player->ping = atoi(value);
}
} else if (strncmp(key, "face_", 5) == 0) {
player = get_player_by_number(server, atoi(key + 5));
if (NULL != player) {
player->face = strdup(value);
}
} else if (strncmp(key, "deaths_", 7) == 0) {
player = get_player_by_number(server, atoi(key + 7));
if (NULL != player) {
player->deaths = atoi(value);
}
}
// isnum( key[6] ) as halo uses score_tX for team scores
else if ((strncmp(key, "score_", 6) == 0) && isdigit((unsigned char)key[6])) {
player = get_player_by_number(server, atoi(key + 6));
if (NULL != player) {
player->score = atoi(value);
}
} else if (player && (strncmp(key, "playertype", 10) == 0)) {
player->team_name = strdup(value);
} else if (player && (strncmp(key, "charactername", 13) == 0)) {
player->face = strdup(value);
} else if (player && (strncmp(key, "characterlevel", 14) == 0)) {
player->ship = atoi(value);
} else if (strncmp(key, "keyhash_", 8) == 0) {
// Ensure these dont make it into the rules
} else if (2 == sscanf(key, "%255[^_]_%d", tmp, &player_num)) {
// arbitary player info
player = get_player_by_number(server, player_num);
if (NULL != player) {
player_add_info(player, tmp, value, NO_FLAGS);
} else if (gps_max_players(server)) {
// gps_max_players( server ) due to bf1942 issue
// where the actual no players is correct but more player
// details are returned
player = add_player(server, player_num);
if (player) {
player->name = NULL;
// init to -1 so we can tell if
// we have team info
player->team = -1;
player->deaths = -999;
}
player_add_info(player, tmp, value, NO_FLAGS);
}
} else {
player = NULL;
add_rule(server, key, value, NO_FLAGS);
}
}
debug(2, "final %d\n", final);
debug(2, "pkt_id %d\n", server->saved_data.pkt_id);
debug(2, "pkt_max %d\n", server->saved_data.pkt_max);
debug(2, "pkt_index %x\n", server->saved_data.pkt_index);
if (
(final && (server->saved_data.pkt_id == 0)) ||
(server->saved_data.pkt_max && (server->saved_data.pkt_index >= ((1 << (server->saved_data.pkt_max)) - 1))) ||
((server->num_players < 0) && (id_minor >= 3))
) {
return (DONE_FORCE);
}
return (INPROGRESS);
}