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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: defines a RCon class used to send rcon commands to remote servers
//
// $NoKeywords: $
//=============================================================================
#include "rcon.h"
#include "Iresponse.h"
#include "RconMsgHandler.h"
#include "Socket.h"
#include "proto_oob.h"
#include "DialogGameInfo.h"
#include "inetapi.h"
#include "dialogkickplayer.h"
extern void v_strncpy(char *dest, const char *src, int bufsize);
typedef enum
{
NONE = 0,
INFO_REQUESTED,
INFO_RECEIVED
} RCONSTATUS;
CRcon::CRcon(IResponse *target,serveritem_t &server,const char *password) {
memcpy(&m_Server, &server,sizeof(serveritem_t));
m_pResponseTarget=target;
m_bIsRefreshing = false;
m_bChallenge = false;
m_bNewRcon = false;
m_bPasswordFail = false;
m_bDisable = false;
m_bGotChallenge = false;
m_iChallenge = 0;
m_fQuerySendTime= 0;
v_strncpy(m_sPassword,password,100);
int bytecode = S2A_INFO_DETAILED;
m_pQuery = new CSocket("internet rcon query", -1);
m_pQuery->AddMessageHandler(new CRconMsgHandlerDetails(this, CMsgHandler::MSGHANDLER_ALL, &bytecode));
}
CRcon::~CRcon() {
delete m_pQuery;
}
//-----------------------------------------------------------------------------
// Purpose: resets the state of the rcon object back to startup conditions (i.e get challenge again)
//-----------------------------------------------------------------------------
void CRcon::Reset()
{
m_bIsRefreshing = false;
m_bChallenge = false;
m_bNewRcon = false;
m_bPasswordFail = false;
m_bDisable = false;
m_bGotChallenge = false;
m_iChallenge = 0;
m_fQuerySendTime= 0;
}
//-----------------------------------------------------------------------------
// Purpose: sends a challenge request to the server if we have yet to get one,
// else it sends the rcon itself
//-----------------------------------------------------------------------------
void CRcon::SendRcon(const char *command)
{
if(m_bDisable==true) // rcon is disabled because it has failed.
{
m_pResponseTarget->ServerFailedToRespond();
return;
}
if(m_bIsRefreshing)
{ // we are already processing a request, lets queue this
queue_requests_t queue;
strncpy(queue.queued,command,1024);
if(requests.Count()>10) // to many request already...
return;
requests.AddToTail(queue);
return;
}
m_bIsRefreshing=true;
m_bPasswordFail=false;
if(m_bGotChallenge==false) // haven't got the challenge id yet
{
GetChallenge();
v_strncpy(m_sCmd,command,1024); // store away the command for later :)
m_bChallenge=true; // note that we are requesting a challenge and need to still run this command
}
else
{
RconRequest(command,m_iChallenge);
}
}
//-----------------------------------------------------------------------------
// Purpose: runs a frame of the net handler
//-----------------------------------------------------------------------------
void CRcon::RunFrame()
{
// only the "ping" command has a timeout
/* float curtime = CSocket::GetClock();
if(m_fQuerySendTime!=0 && (curtime-m_fQuerySendTime)> 10.0f) // 10 seconds
{
m_fQuerySendTime= 0;
m_pResponseTarget->ServerFailedToRespond();
}
*/
if (m_pQuery)
{
m_pQuery->Frame();
}
}
//-----------------------------------------------------------------------------
// Purpose: emits a challenge request
//-----------------------------------------------------------------------------
void CRcon::GetChallenge()
{
CMsgBuffer *buffer = m_pQuery->GetSendBuffer();
assert( buffer );
if ( !buffer )
{
return;
}
netadr_t adr;
adr.ip[0] = m_Server.ip[0];
adr.ip[1] = m_Server.ip[1];
adr.ip[2] = m_Server.ip[2];
adr.ip[3] = m_Server.ip[3];
adr.port = (m_Server.port & 0xff) << 8 | (m_Server.port & 0xff00) >> 8;
adr.type = NA_IP;
// Set state
m_Server.received = (int)INFO_REQUESTED;
// Create query message
buffer->Clear();
// Write control sequence
buffer->WriteLong(0xffffffff);
// Write query string
buffer->WriteString("challenge rcon");
// Sendmessage
m_pQuery->SendMessage( &adr );
// set the clock for this send
m_fQuerySendTime = CSocket::GetClock();
}
//-----------------------------------------------------------------------------
// Purpose: emits a valid rcon request, the challenge id has already been found
//-----------------------------------------------------------------------------
void CRcon::RconRequest(const char *command, int challenge)
{
CMsgBuffer *buffer = m_pQuery->GetSendBuffer();
assert( buffer );
if ( !buffer )
{
return;
}
netadr_t adr;
adr.ip[0] = m_Server.ip[0];
adr.ip[1] = m_Server.ip[1];
adr.ip[2] = m_Server.ip[2];
adr.ip[3] = m_Server.ip[3];
adr.port = (m_Server.port & 0xff) << 8 | (m_Server.port & 0xff00) >> 8;
adr.type = NA_IP;
// Set state
m_Server.received = (int)INFO_REQUESTED;
// Create query message
buffer->Clear();
// Write control sequence
buffer->WriteLong(0xffffffff);
// Write query string
char rcon_cmd[600];
_snprintf(rcon_cmd,600,"rcon %u \"%s\" %s",challenge,m_sPassword,command);
buffer->WriteString(rcon_cmd);
// Sendmessage
m_pQuery->SendMessage( &adr );
// set the clock for this send
m_fQuerySendTime = CSocket::GetClock();
}
//-----------------------------------------------------------------------------
// Purpose: called when an rcon responds
//-----------------------------------------------------------------------------
void CRcon::UpdateServer(netadr_t *adr, int challenge, const char *resp)
{
m_fQuerySendTime= 0;
if(m_bChallenge==true) // now send the RCON request itself
{
m_bChallenge=false; // m_bChallenge is set to say we just requested the challenge value
m_iChallenge=challenge;
m_bGotChallenge=true;
RconRequest(m_sCmd,m_iChallenge);
}
else // this is the result of the RCON request
{
m_bNewRcon=true;
v_strncpy(m_sRconResponse,resp,2048);
m_bIsRefreshing=false;
// this must be before the SeverResponded() :)
if(requests.Count()>0)
{ // we have queued requests
SendRcon(requests[0].queued);
requests.Remove(0); // now delete this element
}
// notify the UI of the new server info
m_pResponseTarget->ServerResponded();
}
}
//-----------------------------------------------------------------------------
// Purpose: run when a refresh is asked for
//-----------------------------------------------------------------------------
void CRcon::Refresh()
{
}
//-----------------------------------------------------------------------------
// Purpose: returns if a rcon is currently being performed
//-----------------------------------------------------------------------------
bool CRcon::IsRefreshing()
{
return m_bIsRefreshing;
}
//-----------------------------------------------------------------------------
// Purpose: the server to which this rcon class is bound
//-----------------------------------------------------------------------------
serveritem_t &CRcon::GetServer()
{
return m_Server;
}
//-----------------------------------------------------------------------------
// Purpose: the challenge id used in rcons
//-----------------------------------------------------------------------------
bool CRcon::Challenge()
{
return m_bChallenge;
}
//-----------------------------------------------------------------------------
// Purpose: returns if a new rcon result is available
//-----------------------------------------------------------------------------
bool CRcon::NewRcon()
{
bool val = m_bNewRcon;
m_bNewRcon=false;
return val;
}
//-----------------------------------------------------------------------------
// Purpose: returns the response of the last rcon
//-----------------------------------------------------------------------------
const char *CRcon::RconResponse()
{
return (const char *)m_sRconResponse;
}
//-----------------------------------------------------------------------------
// Purpose: called when the wrong password is used, when a ServerFailed is called
//-----------------------------------------------------------------------------
bool CRcon::PasswordFail()
{
bool val=m_bPasswordFail;
m_bPasswordFail=false;
return val;
//m_pResponseTarget->ServerFailedToRespond();
}
//-----------------------------------------------------------------------------
// Purpose: called by the rcon message handler to denote a bad password
//-----------------------------------------------------------------------------
void CRcon::BadPassword(const char *info)
{
strncpy(m_sRconResponse,info,100);
m_bPasswordFail=true;
m_bDisable=true;
m_fQuerySendTime= 0;
m_bIsRefreshing=false;
/* // this must be before the ServerFailedToRespond() :)
if(requests.Count()>0)
{ // we have queued requests
SendRcon(requests[0].queued);
requests.Remove(0); // now delete this element
}
*/
m_pResponseTarget->ServerFailedToRespond();
}
//-----------------------------------------------------------------------------
// Purpose: return whether rcon has been disabled (due to bad passwords)
//-----------------------------------------------------------------------------
bool CRcon::Disabled()
{
return m_bDisable;
}
//-----------------------------------------------------------------------------
// Purpose: sets the password to use for rcons
//-----------------------------------------------------------------------------
void CRcon::SetPassword(const char *newPass)
{
strncpy(m_sPassword,newPass,100);
m_bDisable=false; // new password, so we can try again
}
|