-
Notifications
You must be signed in to change notification settings - Fork 33
/
crysis.c
264 lines (221 loc) · 5.66 KB
/
crysis.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
/*
* qstat
* by Steve Jankowski
*
* Crysis query protocol
* Copyright 2012 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 <ctype.h>
#include "debug.h"
#include "utils.h"
#include "qstat.h"
#include "md5.h"
#include "packet_manip.h"
char *
decode_crysis_val(char *val)
{
// Very basic html conversion
val = str_replace(val, """, "\"");
return (str_replace(val, "&", "&"));
}
query_status_t
send_crysis_request_packet(struct qserver *server)
{
char cmd[256], buf[1024], *password, *md5;
debug(2, "challenge: %ld", server->challenge);
switch (server->challenge) {
case 0:
// Not seen a challenge yet, request it
server->challenge++;
sprintf(cmd, "challenge");
break;
case 1:
server->challenge++;
password = get_param_value(server, "password", "");
sprintf(cmd, "%s:%s", server->challenge_string, password);
md5 = md5_hex(cmd, strlen(cmd));
sprintf(cmd, "authenticate %s", md5);
free(md5);
break;
case 2:
// NOTE: we currently don't support player info
server->challenge++;
server->flags |= TF_STATUS_QUERY;
server->n_servers = 3;
sprintf(cmd, "status");
break;
case 3:
return (DONE_FORCE);
}
server->saved_data.pkt_max = -1;
sprintf(buf, "POST /RPC2 HTTP/1.1\015\012Keep-Alive: 300\015\012User-Agent: qstat %s\015\012Content-Length: %d\015\012Content-Type: text/xml\015\012\015\012<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>%s</methodName><params /></methodCall>", QSTAT_VERSION, (int)(98 + strlen(cmd)), cmd);
return (send_packet(server, buf, strlen(buf)));
}
query_status_t
valid_crysis_response(struct qserver *server, char *rawpkt, int pktlen)
{
char *s;
int len;
int cnt = packet_count(server);
if ((0 == cnt) && (0 != strncmp("HTTP/1.1 200 OK", rawpkt, 15))) {
// not valid response
return (REQ_ERROR);
}
s = strnstr(rawpkt, "Content-Length: ", pktlen);
if (NULL == s) {
// not valid response
return (INPROGRESS);
}
s += 16;
if (1 != sscanf(s, "%d", &len)) {
return (INPROGRESS);
}
s = strnstr(rawpkt, "\015\012\015\012", pktlen);
if (NULL == s) {
return (INPROGRESS);
}
s += 4;
if (pktlen != (s - rawpkt + len)) {
return (INPROGRESS);
}
return (DONE_FORCE);
}
char *
crysis_response(struct qserver *server, char *rawpkt, int pktlen)
{
char *s, *e;
int len = pktlen;
s = strnstr(rawpkt, "<methodResponse><params><param><value><string>", len);
if (NULL == s) {
// not valid response
return (NULL);
}
s += 46;
len += rawpkt - s;
e = strnstr(s, "</string></value>", len);
if (NULL == e) {
// not valid response
return (NULL);
}
*e = '\0';
return (strdup(s));
}
query_status_t
deal_with_crysis_packet(struct qserver *server, char *rawpkt, int pktlen)
{
char *s, *val, *line;
query_status_t state = INPROGRESS;
debug(2, "processing...");
if (!server->combined) {
state = valid_crysis_response(server, rawpkt, pktlen);
server->retry1 = n_retries;
if (0 == server->n_requests) {
server->ping_total = time_delta(&packet_recv_time, &server->packet_time1);
server->n_requests++;
}
switch (state) {
case INPROGRESS:
{
// response fragment recieved
int pkt_id;
int pkt_max;
// We're expecting more to come
debug(5, "fragment recieved...");
pkt_id = packet_count(server);
pkt_max = pkt_id++;
if (!add_packet(server, 0, pkt_id, pkt_max, pktlen, rawpkt, 1)) {
// fatal error e.g. out of memory
return (MEM_ERROR);
}
// combine_packets will call us recursively
return (combine_packets(server));
}
case DONE_FORCE:
break; // single packet response fall through
default:
return (state);
}
}
if (DONE_FORCE != state) {
state = valid_crysis_response(server, rawpkt, pktlen);
switch (state) {
case DONE_FORCE:
break; // actually process
default:
return (state);
}
}
debug(3, "packet: challenge = %ld", server->challenge);
s = NULL;
switch (server->challenge) {
case 1:
s = crysis_response(server, rawpkt, pktlen);
if (NULL != s) {
server->challenge_string = s;
return (send_crysis_request_packet(server));
}
return (REQ_ERROR);
case 2:
s = crysis_response(server, rawpkt, pktlen);
if (NULL == s) {
return (REQ_ERROR);
}
if (0 != strncmp(s, "authorized", 10)) {
free(s);
return (REQ_ERROR);
}
free(s);
return (send_crysis_request_packet(server));
case 3:
s = crysis_response(server, rawpkt, pktlen);
if (NULL == s) {
return (REQ_ERROR);
}
}
// Correct ping
// Not quite right but gives a good estimate
server->ping_total = (server->ping_total * server->n_requests) / 2;
debug(3, "processing response...");
s = decode_crysis_val(s);
line = strtok(s, "\012");
// NOTE: id=XXX and msg=XXX will be processed by the mod following the one they where the response of
while (NULL != line) {
debug(4, "LINE: %s\n", line);
val = strstr(line, ":");
if (NULL != val) {
*val = '\0';
val += 2;
debug(4, "var: %s, val: %s", line, val);
if (0 == strcmp("name", line)) {
server->server_name = strdup(val);
} else if (0 == strcmp("level", line)) {
server->map_name = strdup(val);
} else if (0 == strcmp("players", line)) {
if (2 == sscanf(val, "%d/%d", &server->num_players, &server->max_players)) {
}
} else if (
(0 == strcmp("version", line)) ||
(0 == strcmp("gamerules", line)) ||
(0 == strcmp("time remaining", line))
) {
add_rule(server, line, val, NO_FLAGS);
}
}
line = strtok(NULL, "\012");
}
gettimeofday(&server->packet_time1, NULL);
return (DONE_FORCE);
}