-
-
Notifications
You must be signed in to change notification settings - Fork 406
/
CConnectManager.cpp
481 lines (403 loc) · 17.4 KB
/
CConnectManager.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
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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: core/CConnectManager.cpp
* PURPOSE: Manager for connecting to servers
* DEVELOPERS: Christian Myhre Lundheim <>
* Jax <>
* Sebas Lamers <sebasdevelopment@gmx.com>
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
#include "net/packetenums.h"
using namespace std;
static CConnectManager* g_pConnectManager = NULL;
extern CCore* g_pCore;
CConnectManager::CConnectManager ( void )
{
g_pConnectManager = this;
m_Address.s_addr = 0;
m_usPort = 0;
m_bReconnect = false;
m_bIsDetectingVersion = false;
m_bIsConnecting = false;
m_bSave = true;
m_bForceInternalHTTPServer = false;
m_tConnectStarted = 0;
m_pOnCancelClick = new GUI_CALLBACK ( &CConnectManager::Event_OnCancelClick, this );
m_pServerItem = NULL;
m_bNotifyServerBrowser = false;
}
CConnectManager::~CConnectManager ( void )
{
if ( m_pOnCancelClick )
{
delete m_pOnCancelClick;
m_pOnCancelClick = NULL;
}
SAFE_DELETE ( m_pServerItem );
g_pConnectManager = NULL;
}
bool CConnectManager::Connect ( const char* szHost, unsigned short usPort, const char* szNick, const char* szPassword, bool bNotifyServerBrowser, bool bForceInternalHTTPServer )
{
assert ( szHost );
assert ( szNick );
assert ( szPassword );
m_bNotifyServerBrowser = bNotifyServerBrowser;
// Hide certain questions
CCore::GetSingleton ().GetLocalGUI ()->GetMainMenu ()->GetQuestionWindow ()->OnConnect ();
// Hide the server queue
CServerInfo::GetSingletonPtr()->Hide( );
// Save the browser state
CServerBrowser::GetSingletonPtr()->SaveOptions( );
// Are we already connecting?
CNet* pNet = CCore::GetSingleton ().GetNetwork ();
if ( m_bIsConnecting || pNet->IsConnected () )
{
CModManager::GetSingleton ().Unload ();
}
// Is the nick valid?
if ( !CheckNickProvided ( (char*) szNick ) )
{
SString strBuffer = _("Connecting failed. Invalid nick provided!");
CCore::GetSingleton ().ShowMessageBox ( _("Error")+_E("CC20"), strBuffer, MB_BUTTON_OK | MB_ICON_ERROR ); // Invalid nick provided
return false;
}
// Save the nick too
CVARS_SET ( "nick", std::string ( szNick ) );
// Reset the network
pNet->Reset ();
assert ( pNet->GetServerBitStreamVersion () == 0 );
// Save input
m_strHost = szHost;
m_strNick = szNick;
m_strPassword = szPassword;
m_Address.s_addr = 0;
m_usPort = usPort;
m_bForceInternalHTTPServer = bForceInternalHTTPServer;
m_bSave = true;
m_strLastHost = m_strHost;
m_usLastPort = m_usPort;
m_strLastPassword = m_strPassword;
// Parse host into a server item
if ( !CServerListItem::Parse ( m_strHost.c_str(), m_Address ) )
{
SString strBuffer = _("Connecting failed. Invalid host provided!");
CCore::GetSingleton ().ShowMessageBox ( _("Error")+_E("CC21"), strBuffer, MB_BUTTON_OK | MB_ICON_ERROR ); // Invalid host provided
return false;
}
// No connect if disk space is low
if ( !CCore::GetSingleton ().CheckDiskSpace () )
return false;
// Set our packet handler
pNet->RegisterPacketHandler ( CConnectManager::StaticProcessPacket );
// Try to start a network to connect
SString strAddress = inet_ntoa ( m_Address );
if ( m_usPort && !pNet->StartNetwork ( strAddress, m_usPort, CVARS_GET_VALUE < bool > ( "packet_tag" ) ) )
{
SString strBuffer ( _("Connecting to %s at port %u failed!"), m_strHost.c_str (), m_usPort );
CCore::GetSingleton ().ShowMessageBox ( _("Error")+_E("CC22"), strBuffer, MB_BUTTON_OK | MB_ICON_ERROR ); // Failed to connect
return false;
}
m_bIsConnecting = true;
m_tConnectStarted = time ( NULL );
m_bHasTriedSecondConnect = false;
// Load server password
if ( m_strPassword.empty () )
m_strPassword = CServerBrowser::GetSingletonPtr()->GetServerPassword ( m_strHost + ":" + SString ( "%u", m_usPort ) );
// Start server version detection
SAFE_DELETE ( m_pServerItem );
m_pServerItem = new CServerListItem ( m_Address, m_usPort );
m_pServerItem->m_iTimeoutLength = 2000;
m_bIsDetectingVersion = true;
OpenServerFirewall( m_Address );
// Display the status box
SString strBuffer ( _("Connecting to %s:%u ..."), m_strHost.c_str(), m_usPort );
CCore::GetSingleton ().ShowMessageBox ( _("CONNECTING"), strBuffer, MB_BUTTON_CANCEL | MB_ICON_INFO, m_pOnCancelClick );
WriteDebugEvent( SString( "Connecting to %s:%u ...", m_strHost.c_str(), m_usPort ) );
return true;
}
bool CConnectManager::Reconnect ( const char* szHost, unsigned short usPort, const char* szPassword, bool bSave, bool bForceInternalHTTPServer )
{
// Use previous connection datum when function arguments are not set
unsigned int uiPort = 0;
CVARS_GET ( "host", m_strHost );
CVARS_GET ( "port", uiPort );
m_usPort = uiPort;
// If keeping the same host & port, retrieve the password as well
if ( !szHost || !szHost[0] || m_strHost == szHost )
if ( usPort == 0 || m_usPort == usPort )
CVARS_GET ( "password", m_strPassword );
// Allocate a new host and nick buffer and store the strings in them
if ( szHost && szHost[0] )
{
m_strHost = szHost;
}
if ( szPassword && szPassword[0] )
{
m_strPassword = szPassword;
}
if ( usPort )
{
m_usPort = usPort;
}
m_bSave = bSave;
m_bReconnect = true;
m_bForceInternalHTTPServer = bForceInternalHTTPServer;
return true;
}
bool CConnectManager::Event_OnCancelClick ( CGUIElement * pElement )
{
// The user clicked cancel, so abort connecting
Abort ();
// Remove the message box next frame
g_pCore->RemoveMessageBox ( true );
return true;
}
bool CConnectManager::Abort ( void )
{
// Stop the attempt
CNet* pNet = CCore::GetSingleton ().GetNetwork ();
pNet->StopNetwork ();
pNet->Reset ();
// Reset our variables
m_strHost = "";
m_strNick = "";
m_strPassword = "";
m_Address.s_addr = 0;
m_usPort = 0;
m_bIsConnecting = false;
m_bIsDetectingVersion = false;
m_tConnectStarted = 0;
SAFE_DELETE ( m_pServerItem );
// Success
return true;
}
void CConnectManager::DoPulse ( void )
{
// Are we connecting?
if ( m_bIsConnecting )
{
// Are we also getting the server version?
if ( m_bIsDetectingVersion )
{
m_pServerItem->Pulse ( true );
// Got some sort of result?
if ( m_pServerItem->bSkipped || m_pServerItem->bScanned )
{
OnServerExists ();
m_bIsDetectingVersion = false;
// Is different version?
if ( m_pServerItem->bScanned && m_pServerItem->strVersion != MTA_DM_ASE_VERSION )
{
// Version mis-match. See about launching compatible .exe
GetVersionUpdater ()->InitiateSidegradeLaunch ( m_pServerItem->strVersion, m_strHost.c_str(), m_usPort, m_strNick.c_str (), m_strPassword.c_str() );
Abort ();
return;
}
}
}
int iConnectTimeDelta = time ( NULL ) - m_tConnectStarted;
// Try connect again if no response after 4 seconds
if ( iConnectTimeDelta >= 4 && !m_bHasTriedSecondConnect && g_pCore->GetNetwork()->GetExtendedErrorCode() == 0 )
{
m_bHasTriedSecondConnect = true;
SString strAddress = inet_ntoa ( m_Address );
g_pCore->GetNetwork()->StartNetwork ( strAddress, m_usPort, CVARS_GET_VALUE < bool > ( "packet_tag" ) );
}
// Time to timeout the connection?
if ( iConnectTimeDelta >= 8 )
{
// Show a message that the connection timed out and abort
g_pCore->ShowNetErrorMessageBox ( _("Error")+_E("CC23"), _("Connection timed out"), "connect-timed-out", true );
Abort ();
}
else
{
// Some error?
unsigned char ucError = CCore::GetSingleton ().GetNetwork ()->GetConnectionError ();
if ( ucError != 0 )
{
SString strError;
SString strErrorCode;
switch ( ucError )
{
case RID_RSA_PUBLIC_KEY_MISMATCH:
strError = _("Disconnected: unknown protocol error"); strErrorCode = _E("CC24"); // encryption key mismatch
break;
case RID_INCOMPATIBLE_PROTOCOL_VERSION:
strError = _("Disconnected: unknown protocol error"); strErrorCode = _E("CC34"); // old raknet version
break;
case RID_REMOTE_DISCONNECTION_NOTIFICATION:
strError = _("Disconnected: disconnected remotely"); strErrorCode = _E("CC25");
break;
case RID_REMOTE_CONNECTION_LOST:
strError = _("Disconnected: connection lost remotely"); strErrorCode = _E("CC26");
break;
case RID_CONNECTION_BANNED:
strError = _("Disconnected: you are banned from this server"); strErrorCode = _E("CC27");
break;
case RID_NO_FREE_INCOMING_CONNECTIONS:
CServerInfo::GetSingletonPtr()->Show ( eWindowTypes::SERVER_INFO_QUEUE, m_strHost.c_str(), m_usPort, m_strPassword.c_str() );
break;
case RID_DISCONNECTION_NOTIFICATION:
strError = _("Disconnected: disconnected from the server"); strErrorCode = _E("CC28");
break;
case RID_CONNECTION_LOST:
strError = _("Disconnected: connection to the server was lost"); strErrorCode = _E("CC29");
break;
case RID_INVALID_PASSWORD:
CServerInfo::GetSingletonPtr()->Show ( eWindowTypes::SERVER_INFO_PASSWORD, m_strHost.c_str(), m_usPort, m_strPassword.c_str() );
break;
default:
strError = _("Disconnected: connection was refused"); strErrorCode = _E("CC30");
break;
}
// Display an error, reset the error status and exit
// Only display the error if we set one
if ( strError.length() > 0 )
{
CCore::GetSingleton ().ShowNetErrorMessageBox ( _("Error")+strErrorCode, strError );
}
else // Otherwise, remove the message box and hide quick connect
{
CCore::GetSingleton ().RemoveMessageBox( false );
CCore::GetSingleton ().HideQuickConnect ();
}
CCore::GetSingleton ().GetNetwork ()->SetConnectionError ( 0 );
Abort ();
}
}
// Pulse the network interface
CCore::GetSingleton ().GetNetwork ()->DoPulse ();
}
else if ( m_bReconnect )
{
std::string strNick;
CVARS_GET ( "nick", strNick );
Connect ( m_strHost.c_str(), m_usPort, strNick.c_str(), m_strPassword.c_str(), false, m_bForceInternalHTTPServer );
m_bReconnect = false;
}
}
bool CConnectManager::StaticProcessPacket ( unsigned char ucPacketID, NetBitStreamInterface& BitStream )
{
// We're working on connecting?
if ( g_pConnectManager->m_bIsConnecting )
{
g_pConnectManager->OnServerExists ();
// The packet we're expecting?
if ( ucPacketID == PACKET_ID_MOD_NAME )
{
// Read packet data
unsigned short usServerBitStreamVersion = 0x01;
BitStream.Read ( usServerBitStreamVersion );
SString strModName;
BitStream.ReadString ( strModName );
// Process packet data
CCore::GetSingleton ().GetNetwork ()->SetServerBitStreamVersion ( usServerBitStreamVersion );
if ( strModName != "" )
{
// Populate the arguments to pass it (-c host port nick)
SString strArguments ( "%s %s", g_pConnectManager->m_strNick.c_str(), g_pConnectManager->m_strPassword.c_str() );
// Hide the messagebox we're currently showing
CCore::GetSingleton ().RemoveMessageBox ();
// If we connected from quick-connect, get rid of it
CQuickConnect * pQuickConnect = CCore::GetSingleton ().GetLocalGUI ()->GetMainMenu ()->GetQuickConnectWindow ();
if ( pQuickConnect->IsVisible () )
pQuickConnect->SetVisible ( false );
// Save the connection details into the config
if ( g_pConnectManager->m_bSave )
{
CVARS_SET ( "host", g_pConnectManager->m_strHost );
CVARS_SET ( "port", g_pConnectManager->m_usPort );
CVARS_SET ( "password", g_pConnectManager->m_strPassword );
}
SetApplicationSettingInt ( "last-server-ip", g_pConnectManager->m_Address.s_addr );
SetApplicationSettingInt ( "last-server-port", g_pConnectManager->m_usPort );
SetApplicationSettingInt ( "last-server-time", _time32 ( NULL ) );
// Kevuwk: Forced the config to save here so that the IP/Port isn't lost on crash
CCore::GetSingleton ().SaveConfig ();
// Reset our variables
g_pConnectManager->m_strNick = "";
g_pConnectManager->m_strHost = "";
g_pConnectManager->m_strPassword = "";
g_pConnectManager->m_Address.s_addr = 0;
g_pConnectManager->m_usPort = 0;
g_pConnectManager->m_bIsConnecting = false;
g_pConnectManager->m_bIsDetectingVersion = false;
g_pConnectManager->m_tConnectStarted = 0;
// Load the mod
if ( !CModManager::GetSingleton ().Load ( strModName, strArguments ) )
{
// Failed loading the mod
strArguments.Format ( _("No such mod installed (%s)"), strModName.c_str() );
CCore::GetSingleton ().ShowMessageBox ( _("Error")+_E("CC31"), strArguments, MB_BUTTON_OK | MB_ICON_ERROR ); // Mod loading failed
g_pConnectManager->Abort ();
}
}
else
{
// Show failed message and abort the attempt
CCore::GetSingleton ().ShowNetErrorMessageBox ( _("Error")+_E("CC32"), _("Bad server response (2)") );
g_pConnectManager->Abort ();
}
}
else
{
// Not the player joined packet?
if ( ucPacketID != PACKET_ID_SERVER_JOIN && ucPacketID != PACKET_ID_SERVER_JOIN_DATA )
{
// Show failed message and abort the attempt
CCore::GetSingleton ().ShowNetErrorMessageBox ( _("Error")+_E("CC33"), _("Bad server response (1)") + SString( " [%d]", ucPacketID ) );
g_pConnectManager->Abort ();
}
}
return true;
}
return false;
}
bool CConnectManager::CheckNickProvided ( const char* szNick )
{
if ( stricmp ( szNick, "admin" ) == 0 )
return false;
if ( stricmp ( szNick, "console" ) == 0 )
return false;
if ( stricmp ( szNick, "server" ) == 0 )
return false;
return true;
}
//
// Called at least once (maybe more) if a MTA server exists at the current address/port
//
void CConnectManager::OnServerExists ( void )
{
if ( m_bNotifyServerBrowser )
{
m_bNotifyServerBrowser = false;
CServerBrowser::GetSingletonPtr()->NotifyServerExists ( m_Address, m_usPort );
}
}
//
// Some server firewalls block UDP packets unless a TCP connection has previously been established
//
void CConnectManager::OpenServerFirewall( in_addr Address, ushort usHttpPort, bool bHighPriority )
{
if ( usHttpPort == 0 )
usHttpPort = 80;
uint uiTimeOut;
if ( bHighPriority )
{
// Clear previously queued requests if this is high priority
g_pCore->GetNetwork()->GetHTTPDownloadManager( EDownloadMode::CONNECT_TCP_SEND )->Reset();
uiTimeOut = 2000;
}
else
{
uiTimeOut = 1000;
}
SString strDummyUrl( "http://%s:%d/.dummy/", inet_ntoa( Address ), usHttpPort );
g_pCore->GetNetwork()->GetHTTPDownloadManager( EDownloadMode::CONNECT_TCP_SEND )->QueueFile( strDummyUrl, NULL, 0, "", 0, true, NULL, NULL, false, 1, uiTimeOut );
}