-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcquery.cpp
312 lines (249 loc) · 8.97 KB
/
mcquery.cpp
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
//#include <boost/asio.hpp>
//#include <array>
#include <functional> // for bind
#include <algorithm> // std::equal
#include "mcquery.hpp"
#include <iomanip> // remove this!
using namespace boost::asio;
using namespace std::placeholders;
using namespace std;
using boost::posix_time::seconds;
using boost::system::error_code;
using uchar = unsigned char;
using uint = unsigned int;
struct debuglog {
template<typename T>
debuglog& operator<< (T rhs) {
//cout<< rhs;
return *this;
}
} debug;
/*************************
* mcQuery definitions *
*************************/
// non-class helper function;
vector<string> extractPlugins(string raw);
mcQuery::mcQuery(const char* host /* = "localhost" */,
const char* port /* = "25565" */,
const int timeoutsecs /* = 5 */)
: ioService {},
t {ioService},
Resolver {ioService},
Query {host, port},
Socket {ioService},
timeout {seconds(timeoutsecs)}
{ }
mcDataBasic mcQuery::getBasic() {
fullreq = false;
connect();
return move(static_cast<mcDataBasic>(data));
}
mcDataFull mcQuery::getFull() {
fullreq = true;
connect();
return move(data);
}
void mcQuery::connect() {
data.success = false;
t.expires_from_now(timeout);
t.async_wait(
[&](const error_code& e) {
if(e) return;
data.error = "User-defined timeout reached";
Socket.cancel(); // causes event handlers to be called with error code 125 (asio::error::operation_aborted)
} );
try {
Endpoint = *Resolver.resolve(Query); // has a good timeout of itself, so async probably isnt necessary
Socket.connect(Endpoint);
// request for challenge token
// [-magic--] [type] [----session id------]
uchar req[] = { 0xFE, 0xFD, 0x09, 0x01, 0x02, 0x03, 0x04 };
debug<< "sending..." << '\n';
size_t len = Socket.send_to(buffer(req), Endpoint); // connectionless UDP: doesn't need to be async
debug<< "sent " << len << " bytes" << '\n';
debug<< "preparing recieve buffer" << '\n';
Socket.async_receive_from(buffer(recvBuffer), Endpoint,
bind(&mcQuery::challengeReceiver, this, _1, _2));
ioService.reset();
ioService.run();
} catch(exception& e) {
data.error = e.what();
}
}
void mcQuery::challengeReceiver(const error_code& error, size_t nBytes) {
if(error) return; // recieve failed, probably cancelled by timer
debug<< "received " << nBytes << " bytes" << '\n';
// byte 0 is 0x09
// byte 1 to 4 is the session id (last 4 bytes of the request we sent xor'ed with 0F0F0F0F).
// These bytes don't hold usefull info, but we check if they are correct anyways
const array<uchar,5> expected = { 0x09, 0x01, 0x02, 0x03, 0x04 };
if( !equal(expected.begin(), expected.end(), recvBuffer.begin()) )
throw runtime_error("Incorrect response from server when recieving challenge token");
// byte 5 onwards is the challange token: a null-terminated ASCII number string which should be sent back as a 32-bit integer
uint challtoken = atoi((char*)&recvBuffer[5]);
// the actual request
// [-magic--] [type] [----session id------]
vector<uchar> req { 0xFE, 0xFD, 0x00, 0x01, 0x02, 0x03, 0x04,
static_cast<uchar>(challtoken>>24 & 0xFF),
static_cast<uchar>(challtoken>>16 & 0xFF),
static_cast<uchar>(challtoken>>8 & 0xFF),
static_cast<uchar>(challtoken>>0 & 0xFF)
};
if(fullreq) {
req.push_back(0x00);
req.push_back(0x00);
req.push_back(0x00);
req.push_back(0x00);
}
debug<< "sending actual request" << '\n';
Socket.send_to(buffer(req), Endpoint);
Socket.async_receive_from(buffer(recvBuffer), Endpoint, bind(&mcQuery::dataReceiver, this, _1, _2));
}
void mcQuery::dataReceiver(const boost::system::error_code& error, size_t nBytes) {
t.cancel(); // causes event handler to be called with boost::asio::error::operation_aborted
if(error) return; // recieve failed
debug<< "received " << nBytes << " bytes" << '\n';
const array<uchar,5> expected = { 0x00, 0x01, 0x02, 0x03, 0x04 };
if( !equal(expected.begin(), expected.end(), recvBuffer.begin()) )
throw runtime_error("Incorrect response from server when recieving data");
// tokenize answer into mcData struct
iss.rdbuf()->pubsetbuf(reinterpret_cast<char*>(&recvBuffer[5]), recvBuffer.size()-5);
extract();
data.success = true; // the only place where this flag can be set to true
}
void mcQuery::extract() {
if( fullreq ) extractFull();
else extractBasic();
}
void mcQuery::extractBasic() {
getline(iss, data.motd, '\0');
getline(iss, data.gametype, '\0');
getline(iss, data.map, '\0');
iss >> data.numplayers;
iss.ignore(1);
iss >> data.maxplayers;
iss.ignore(1);
iss.readsome(reinterpret_cast<char*>(&data.hostport), sizeof(data.hostport));
getline(iss, data.hostip, '\0');
}
void mcQuery::extractFull() {
extractKey("splitnum");
iss.ignore(2);
extractKey("hostname");
getline(iss, data.motd, '\0');
extractKey("gametype");
getline(iss, data.gametype, '\0');
extractKey("game_id");
getline(iss, data.game_id, '\0');
extractKey("version");
getline(iss, data.version, '\0');
extractKey("plugins");
string rawPlugins;
getline(iss, rawPlugins, '\0');
data.plugins = extractPlugins(rawPlugins);
extractKey("map");
getline(iss, data.map, '\0');
extractKey("numplayers");
iss >> data.numplayers;
iss.ignore(1);
extractKey("maxplayers");
iss >> data.maxplayers;
iss.ignore(1);
extractKey("hostport");
iss >> data.hostport;
iss.ignore(1);
extractKey("hostip");
getline(iss, data.hostip, '\0');
iss.ignore(2);
extractKey("player_");
iss.ignore(1);
string name;
while( iss.good() && iss.peek()!='\0' ) {
getline(iss, name, '\0');
data.playernames.push_back(name);
}
}
void mcQuery::extractKey(const char* expected) {
string temp;
getline(iss, temp, '\0');
if( temp.compare(expected) )
throw runtime_error("Unexpected key found in server data");
}
vector<string> extractPlugins(string raw) {
vector<string> vs;
if( !raw.size() )
return vs;
istringstream pss(raw);
string temp;
getline(pss, temp, ':'); // craftbukkit description
vs.push_back(temp);
while( pss.good() ) {
pss.ignore(1); // whitespace
getline(pss, temp, ';'); // plugin name
vs.push_back(temp);
}
return vs;
}
/*******************************
* mcQuerySimple definitions *
*******************************/
mcQuerySimple::mcQuerySimple(
const char* host /* = "localhost" */,
const char* port /* = "25565" */,
const int timeoutsecs /* = 5 */)
: ioService {},
t {ioService},
Resolver {ioService},
Query {host, port},
Socket {ioService},
timeout {seconds(timeoutsecs)}
{ }
mcDataSimple mcQuerySimple::get() {
data.success = false;
t.expires_from_now(timeout);
t.async_wait(
[&](const error_code& e) {
if(e) return;
data.error = "User-defined timeout reached";
Socket.cancel(); // causes event handlers to be called with error code 125 (asio::error::operation_aborted)
} );
try {
Endpoint = *Resolver.resolve(Query);
Socket.async_connect(Endpoint, bind(&mcQuerySimple::connector, this, _1));
ioService.reset();
ioService.run();
} catch(exception& e) {
data.error = e.what();
}
return data;
}
void mcQuerySimple::connector(const error_code& e) { // TODO: try totally different ordering
if(e) return;
uchar req[] = { 0xFE, 0x01 };
Socket.async_send(buffer(req), bind(&mcQuerySimple::sender, this, _1, _2));
}
void mcQuerySimple::sender(const error_code& e, size_t numBytes) {
if(e) return;
Socket.async_receive(buffer(recvBuffer), bind(&mcQuerySimple::receiver, this, _1, _2));
}
void mcQuerySimple::receiver(const error_code& e, size_t numBytes) { // does not work for mc 1.3 and earlier
t.cancel();
if(e) return;
debug<< "received " << numBytes << " bytes\n";
array<uchar,2> expected = { 0xFF, 0x00 };
if( !equal(expected.begin(), expected.end(), recvBuffer.begin()) )
throw runtime_error("Incorrect response from server when recieving data");
// remove all uneven indexed bytes (they're all zero)
for( int i=0; i<recvBuffer.size()/2; i++) {
recvBuffer[i] = recvBuffer[i*2];
recvBuffer[i*2] = '\0';
}
istringstream iss;
iss.rdbuf()->pubsetbuf(reinterpret_cast<char*>(&recvBuffer[8]), recvBuffer.size()-8);
getline(iss, data.version, '\0');
getline(iss, data.motd, '\0');
iss >> data.numplayers;
iss.ignore(1);
iss >> data.maxplayers;
data.success = true;
}