Upstream tarball 9435
[amule.git] / src / ServerList.cpp
blob2b15d19e2101aa55cdbf595bebfe798440e988ca
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <wx/wx.h>
28 #include "ServerList.h" // Interface declarations.
30 #include <protocol/Protocols.h>
31 #include <protocol/ed2k/Constants.h>
32 #include <common/DataFileVersion.h>
33 #include <tags/ServerTags.h>
35 #include <wx/txtstrm.h>
36 #include <wx/wfstream.h>
37 #include <wx/url.h> // Needed for wxURL
38 #include <wx/tokenzr.h>
40 #include "DownloadQueue.h" // Needed for CDownloadQueue
41 #include "ServerConnect.h" // Needed for CServerConnect
42 #include "Server.h" // Needed for CServer and SRV_PR_*
43 #include "OtherStructs.h" // Needed for ServerMet_Struct
44 #include "CFile.h" // Needed for CFile
45 #include "HTTPDownload.h" // Needed for HTTPThread
46 #include "Preferences.h" // Needed for thePrefs
47 #include "amule.h" // Needed for theApp
48 #include "Statistics.h" // Needed for theStats
49 #include "Packet.h" // Neeed for CPacket
50 #include "Logger.h"
51 #include "ScopedPtr.h"
52 #include <common/Format.h>
53 #include "IPFilter.h"
54 #include <common/FileFunctions.h> // Needed for UnpackArchive
56 CServerList::CServerList()
58 m_serverpos = m_servers.end();
59 m_statserverpos = m_servers.end();
60 m_nLastED2KServerLinkCheck = ::GetTickCount();
64 bool CServerList::Init()
66 // Load Metfile
67 bool bRes = LoadServerMet(CPath(theApp->ConfigDir + wxT("server.met")));
69 // insert static servers from textfile
70 LoadStaticServers(theApp->ConfigDir + wxT("staticservers.dat"));
72 // Send the auto-update of server.met via HTTPThread requests
73 current_url_index = 0;
74 if ( thePrefs::AutoServerlist()) {
75 AutoUpdate();
78 return bRes;
82 bool CServerList::LoadServerMet(const CPath& path)
84 AddLogLineM(false, CFormat(_("Loading server.met file: %s")) % path);
86 bool merge = !m_servers.empty();
88 if (!path.FileExists()) {
89 AddLogLineM(false, _("Server.met file not found!"));
90 return false;
93 // Try to unpack the file, might be an archive
94 const wxChar* mets[] = { wxT("server.met"), NULL };
95 // Try to unpack the file, might be an archive
96 if (UnpackArchive(path, mets).second != EFT_Met) {
97 AddLogLineM(true, CFormat(_("Failed to load server.met file '%s', unknown format encountered.")) % path);
98 return false;
101 CFile servermet(path, CFile::read);
102 if ( !servermet.IsOpened() ){
103 AddLogLineM( false, _("Failed to open server.met!") );
104 return false;
108 try {
109 Notify_ServerFreeze();
111 byte version = servermet.ReadUInt8();
113 if (version != 0xE0 && version != MET_HEADER) {
114 AddLogLineM(true, wxString::Format(_("Server.met file corrupt, found invalid versiontag: 0x%x, size %i"), version, sizeof(version)));
115 Notify_ServerThaw();
116 return false;
119 uint32 fservercount = servermet.ReadUInt32();
121 ServerMet_Struct sbuffer;
122 uint32 iAddCount = 0;
124 for ( uint32 j = 0; j < fservercount; ++j ) {
125 sbuffer.ip = servermet.ReadUInt32();
126 sbuffer.port = servermet.ReadUInt16();
127 sbuffer.tagcount = servermet.ReadUInt32();
129 CServer* newserver = new CServer(&sbuffer);
131 // Load tags
132 for ( uint32 i = 0; i < sbuffer.tagcount; ++i ) {
133 newserver->AddTagFromFile(&servermet);
136 // Server priorities are not in sorted order
137 // High = 1, Low = 2, Normal = 0, so we have to check
138 // in a less logical fashion.
139 int priority = newserver->GetPreferences();
140 if (priority < SRV_PR_MIN || priority > SRV_PR_MAX) {
141 newserver->SetPreference(SRV_PR_NORMAL);
144 // set listname for server
145 if ( newserver->GetListName().IsEmpty() ) {
146 newserver->SetListName(wxT("Server ") +newserver->GetAddress());
150 if ( !theApp->AddServer(newserver) ) {
151 CServer* update = GetServerByAddress(newserver->GetAddress(), newserver->GetPort());
152 if(update) {
153 update->SetListName( newserver->GetListName());
154 if(!newserver->GetDescription().IsEmpty()) {
155 update->SetDescription( newserver->GetDescription());
157 Notify_ServerRefresh(update);
159 delete newserver;
160 } else {
161 ++iAddCount;
166 Notify_ServerThaw();
168 if (!merge) {
169 AddLogLineM(true, wxString::Format(wxPLURAL("%i server in server.met found", "%i servers in server.met found", fservercount), fservercount));
170 } else {
171 AddLogLineM(true, wxString::Format(wxPLURAL("%d server added", "%d servers added", iAddCount), iAddCount));
173 } catch (const CInvalidPacket& err) {
174 AddLogLineM(true, wxT("Error: the file server.met is corrupted: ") + err.what());
175 Notify_ServerThaw();
176 return false;
177 } catch (const CSafeIOException& err) {
178 AddLogLineM(true, wxT("IO error while reading 'server.met': ") + err.what());
179 Notify_ServerThaw();
180 return false;
183 return true;
187 bool CServerList::AddServer(CServer* in_server, bool fromUser)
189 if ( !in_server->GetPort() ) {
190 if ( fromUser ) {
191 AddLogLineM( true,
192 CFormat( _("Server not added: [%s:%d] does not specify a valid port.") )
193 % in_server->GetAddress()
194 % in_server->GetPort()
198 return false;
199 } else if (
200 !in_server->HasDynIP() &&
202 !IsGoodIP( in_server->GetIP(), thePrefs::FilterLanIPs() ) ||
203 theApp->ipfilter->IsFiltered( in_server->GetIP(), true )
206 if ( fromUser ) {
207 AddLogLineM( true,
208 CFormat( _("Server not added: The IP of [%s:%d] is filtered or invalid.") )
209 % in_server->GetAddress()
210 % in_server->GetPort()
214 return false;
217 CServer* test_server = GetServerByAddress(in_server->GetAddress(), in_server->GetPort());
218 // Avoid duplicate (dynIP) servers: If the server which is to be added, is a dynIP-server
219 // but we don't know yet it's DN, we need to search for an already available server with
220 // that IP.
221 if (test_server == NULL && in_server->GetIP() != 0) {
222 test_server = GetServerByIPTCP(in_server->GetIP(), in_server->GetPort());
225 if (test_server) {
226 if ( fromUser ) {
227 AddLogLineM( true,
228 CFormat( _("Server not added: Server with matching IP:Port [%s:%d] found in list.") )
229 % in_server->GetAddress()
230 % in_server->GetPort()
234 test_server->ResetFailedCount();
235 Notify_ServerRefresh( test_server );
237 return false;
240 theStats::AddServer();
242 m_servers.push_back(in_server);
243 NotifyObservers( EventType( EventType::INSERTED, in_server ) );
245 if ( fromUser ) {
246 AddLogLineM( true,
247 CFormat( _("Server added: Server at [%s:%d] using the name '%s'.") )
248 % in_server->GetAddress()
249 % in_server->GetPort()
250 % in_server->GetListName()
255 return true;
259 void CServerList::ServerStats()
261 uint32 tNow = ::GetTickCount();
263 if (theApp->IsConnectedED2K() && m_servers.size() > 0) {
264 CServer* ping_server = GetNextStatServer();
265 CServer* test = ping_server;
266 if (!ping_server) {
267 return;
270 while (ping_server->GetLastPingedTime() && (tNow - ping_server->GetLastPingedTime()) < UDPSERVSTATREASKTIME) {
271 ping_server = GetNextStatServer();
272 if (ping_server == test) {
273 return;
277 if (ping_server->GetFailedCount() >= thePrefs::GetDeadserverRetries() && thePrefs::DeadServer() && !ping_server->IsStaticMember()) {
278 RemoveServer(ping_server);
279 return;
282 srand((unsigned)time(NULL));
283 ping_server->SetRealLastPingedTime(tNow); // this is not used to calcualte the next ping, but only to ensure a minimum delay for premature pings
284 if (!ping_server->GetCryptPingReplyPending() && (!ping_server->GetLastPingedTime() || (tNow - ping_server->GetLastPingedTime()) >= UDPSERVSTATREASKTIME) && theApp->GetPublicIP() && thePrefs::IsServerCryptLayerUDPEnabled()) {
285 // We try a obfsucation ping first and wait 20 seconds for an answer
286 // if it doesn't get responsed, we don't count it as error but continue with a normal ping
287 ping_server->SetCryptPingReplyPending(true);
288 uint32 nPacketLen = 4 + (uint8)(rand() % 16); // max padding 16 bytes
289 CScopedArray<byte> pRawPacket(nPacketLen);
290 uint32 dwChallenge = (rand() << 17) | (rand() << 2) | (rand() & 0x03);
291 if (dwChallenge == 0) {
292 dwChallenge++;
295 memcpy(pRawPacket.get(), &dwChallenge, sizeof(uint32));
296 for (uint32 i = 4; i < nPacketLen; i++) { // fillng up the remaining bytes with random data
297 pRawPacket[i] = (uint8)rand();
300 ping_server->SetChallenge(dwChallenge);
301 ping_server->SetLastPinged(tNow);
302 ping_server->SetLastPingedTime((tNow - (uint32)UDPSERVSTATREASKTIME) + 20); // give it 20 seconds to respond
304 AddDebugLogLineM(false, logServerUDP, CFormat(wxT(">> Sending OP__GlobServStatReq (obfuscated) to server %s:%u")) % ping_server->GetAddress() % ping_server->GetPort());
306 CPacket* packet = new CPacket(pRawPacket[1], nPacketLen - 2, pRawPacket[0]);
307 packet->CopyToDataBuffer(0, pRawPacket.get() + 2, nPacketLen - 2);
309 theStats::AddUpOverheadServer(packet->GetPacketSize());
310 theApp->serverconnect->SendUDPPacket(packet, ping_server, true, true /*raw packet*/, 12 /* Port offset is 12 for obfuscated encryption*/);
311 } else if (ping_server->GetCryptPingReplyPending() || theApp->GetPublicIP() == 0 || !thePrefs::IsServerCryptLayerUDPEnabled()){
312 // our obfsucation ping request was not answered, so probably the server doesn'T supports obfuscation
313 // continue with a normal request
314 if (ping_server->GetCryptPingReplyPending() && thePrefs::IsServerCryptLayerUDPEnabled()) {
315 AddDebugLogLineM(false, logServerUDP, wxT("CryptPing failed for server ") + ping_server->GetListName());
316 } else if (thePrefs::IsServerCryptLayerUDPEnabled()) {
317 AddDebugLogLineM(false, logServerUDP, wxT("CryptPing skipped because our public IP is unknown for server ") + ping_server->GetListName());
320 ping_server->SetCryptPingReplyPending(false);
322 CPacket* packet = new CPacket(OP_GLOBSERVSTATREQ, 4, OP_EDONKEYPROT);
323 uint32 challenge = 0x55AA0000 + (uint16)rand();
324 ping_server->SetChallenge(challenge);
325 packet->CopyUInt32ToDataBuffer(challenge);
326 ping_server->SetLastPinged(tNow);
327 ping_server->SetLastPingedTime(tNow - (rand() % HR2S(1)));
328 ping_server->AddFailedCount();
329 Notify_ServerRefresh(ping_server);
330 theStats::AddUpOverheadServer(packet->GetPacketSize());
331 theApp->serverconnect->SendUDPPacket(packet, ping_server, true);
332 } else {
333 wxFAIL;
339 void CServerList::RemoveServer(CServer* in_server)
341 if (in_server == theApp->serverconnect->GetCurrentServer()) {
342 theApp->ShowAlert(_("You are connected to the server you are trying to delete. please disconnect first."), _("Info"), wxOK);
343 } else {
344 CInternalList::iterator it = std::find(m_servers.begin(), m_servers.end(), in_server);
345 if ( it != m_servers.end() ) {
346 if (theApp->downloadqueue->GetUDPServer() == in_server) {
347 theApp->downloadqueue->SetUDPServer( 0 );
350 NotifyObservers( EventType( EventType::REMOVED, in_server ) );
352 if (m_serverpos == it) {
353 ++m_serverpos;
355 if (m_statserverpos == it) {
356 ++m_statserverpos;
358 m_servers.erase(it);
359 theStats::DeleteServer();
361 Notify_ServerRemove(in_server);
362 delete in_server;
368 void CServerList::RemoveAllServers()
370 NotifyObservers( EventType( EventType::CLEARED ) );
372 theStats::DeleteAllServers();
373 // no connection, safely remove all servers
374 while ( !m_servers.empty() ) {
375 delete m_servers.back();
376 m_servers.pop_back();
378 m_serverpos = m_servers.end();
379 m_statserverpos = m_servers.end();
383 void CServerList::GetStatus(uint32 &failed, uint32 &user, uint32 &file, uint32 &tuser, uint32 &tfile,float &occ)
385 failed = 0;
386 user = 0;
387 file = 0;
388 tuser=0;
389 tfile = 0;
390 occ=0;
391 uint32 maxusers=0;
392 uint32 tuserk = 0;
394 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
395 const CServer* const curr = *it;
396 if( curr->GetFailedCount() ) {
397 ++failed;
398 } else {
399 user += curr->GetUsers();
400 file += curr->GetFiles();
402 tuser += curr->GetUsers();
403 tfile += curr->GetFiles();
405 if (curr->GetMaxUsers()) {
406 tuserk += curr->GetUsers(); // total users on servers with known maximum
407 maxusers+=curr->GetMaxUsers();
410 if (maxusers>0) {
411 occ=(float)(tuserk*100)/maxusers;
416 void CServerList::GetUserFileStatus(uint32 &user, uint32 &file)
418 user = 0;
419 file = 0;
420 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
421 const CServer* const curr = *it;
422 if( !curr->GetFailedCount() ) {
423 user += curr->GetUsers();
424 file += curr->GetFiles();
430 CServerList::~CServerList()
432 SaveServerMet();
433 while ( !m_servers.empty() ) {
434 delete m_servers.back();
435 m_servers.pop_back();
440 void CServerList::LoadStaticServers( const wxString& filename )
442 if ( !CPath::FileExists( filename ) ) {
443 return;
446 wxFileInputStream stream( filename );
447 wxTextInputStream f(stream);
449 while ( !stream.Eof() ) {
450 wxString line = f.ReadLine();
452 // Skip comments
453 if ( line.GetChar(0) == '#' || line.GetChar(0) == '/') {
454 continue;
457 wxStringTokenizer tokens( line, wxT(",") );
459 if ( tokens.CountTokens() != 3 ) {
460 continue;
464 // format is host:port,priority,Name
465 wxString addy = tokens.GetNextToken().Strip( wxString::both );
466 wxString prio = tokens.GetNextToken().Strip( wxString::both );
467 wxString name = tokens.GetNextToken().Strip( wxString::both );
469 wxString host = addy.BeforeFirst( wxT(':') );
470 wxString port = addy.AfterFirst( wxT(':') );
473 int priority = StrToLong( prio );
474 if (priority < SRV_PR_MIN || priority > SRV_PR_MAX) {
475 priority = SRV_PR_NORMAL;
479 // We need a valid name for the list
480 if ( name.IsEmpty() ) {
481 name = addy;
485 // create server object and add it to the list
486 CServer* server = new CServer( StrToLong( port ), host );
488 server->SetListName( name );
489 server->SetIsStaticMember( true );
490 server->SetPreference( priority );
493 // Try to add the server to the list
494 if ( !theApp->AddServer( server ) ) {
495 delete server;
496 CServer* existing = GetServerByAddress( host, StrToULong( port ) );
497 if ( existing) {
498 existing->SetListName( name );
499 existing->SetIsStaticMember( true );
500 existing->SetPreference( priority );
502 Notify_ServerRefresh( existing );
509 struct ServerPriorityComparator {
510 // Return true iff lhs should strictly appear earlier in the list than rhs.
511 // In this case, we want higher priority servers to appear earlier.
512 bool operator()(const CServer* lhs, const CServer* rhs) {
513 wxASSERT
515 rhs->GetPreferences() == SRV_PR_LOW ||
516 rhs->GetPreferences() == SRV_PR_NORMAL ||
517 rhs->GetPreferences() == SRV_PR_HIGH
519 switch (lhs->GetPreferences()) {
520 case SRV_PR_LOW:
521 return false;
522 case SRV_PR_NORMAL:
523 return rhs->GetPreferences() == SRV_PR_LOW;
524 case SRV_PR_HIGH:
525 return rhs->GetPreferences() != SRV_PR_HIGH;
526 default:
527 wxFAIL;
528 return false;
533 void CServerList::Sort()
535 m_servers.sort(ServerPriorityComparator());
536 // Once the list has been sorted, it doesn't really make sense to continue
537 // traversing the new order from the old position. Plus, there's a bug in
538 // version of libstdc++ before gcc4 such that iterators that were equal to
539 // end() were left dangling.
540 m_serverpos = m_servers.begin();
541 m_statserverpos = m_servers.begin();
545 CServer* CServerList::GetNextServer(bool bOnlyObfuscated)
547 while (bOnlyObfuscated && (m_serverpos != m_servers.end()) && !((*m_serverpos)->SupportsObfuscationTCP() || (*m_serverpos)->SupportsObfuscationUDP())) {
548 wxASSERT(*m_serverpos != NULL);
549 ++m_serverpos;
552 if (m_serverpos == m_servers.end()) {
553 return 0;
554 } else {
555 if (*m_serverpos) {
556 return *m_serverpos++;
557 } else {
558 return 0;
564 CServer* CServerList::GetNextStatServer()
566 if (m_statserverpos == m_servers.end()) {
567 m_statserverpos = m_servers.begin();
570 if (m_statserverpos == m_servers.end()) {
571 return 0;
572 } else {
573 wxASSERT(*m_statserverpos != NULL);
574 return *m_statserverpos++;
579 CServer* CServerList::GetServerByAddress(const wxString& address, uint16 port) const
581 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
582 CServer* const s = *it;
583 if (port == s->GetPort() && s->GetAddress() == address) {
584 return s;
587 return NULL;
591 CServer* CServerList::GetServerByIP(uint32 nIP) const
593 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
594 CServer* const s = *it;
595 if (s->GetIP() == nIP)
596 return s;
598 return NULL;
602 CServer* CServerList::GetServerByIPTCP(uint32 nIP, uint16 nPort) const
604 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
605 CServer* const s = *it;
606 if (s->GetIP() == nIP && s->GetPort() == nPort)
607 return s;
609 return NULL;
612 CServer* CServerList::GetServerByIPUDP(uint32 nIP, uint16 nUDPPort, bool bObfuscationPorts) const
614 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
615 CServer* const s =*it;
616 if (s->GetIP() == nIP && (s->GetPort() == nUDPPort-4 ||
617 (bObfuscationPorts && (s->GetObfuscationPortUDP() == nUDPPort) || (s->GetPort() == nUDPPort - 12))))
618 return s;
620 return NULL;
623 bool CServerList::SaveServerMet()
625 CPath newservermet = CPath(theApp->ConfigDir + wxT("server.met.new"));
627 CFile servermet( newservermet, CFile::write );
628 if (!servermet.IsOpened()) {
629 AddLogLineM(false,_("Failed to save server.met!"));
630 return false;
634 try {
635 servermet.WriteUInt8(0xE0);
636 servermet.WriteUInt32( m_servers.size() );
638 for ( CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
639 const CServer* const server = *it;
641 uint16 tagcount = 12;
642 if (!server->GetListName().IsEmpty()) {
643 ++tagcount;
645 if (!server->GetDynIP().IsEmpty()) {
646 ++tagcount;
648 if (!server->GetDescription().IsEmpty()) {
649 ++tagcount;
651 if (server->GetConnPort() != server->GetPort()) {
652 ++tagcount;
655 // For unicoded name, description, and dynip
656 if ( !server->GetListName().IsEmpty() ) {
657 ++tagcount;
659 if ( !server->GetDynIP().IsEmpty() ) {
660 ++tagcount;
662 if ( !server->GetDescription().IsEmpty() ) {
663 ++tagcount;
665 if (!server->GetVersion().IsEmpty()) {
666 ++tagcount;
669 if (server->GetServerKeyUDP(true)) {
670 ++tagcount;
673 if (server->GetServerKeyUDPIP()) {
674 ++tagcount;
677 if (server->GetObfuscationPortTCP()) {
678 ++tagcount;
681 if (server->GetObfuscationPortUDP()) {
682 ++tagcount;
685 servermet.WriteUInt32(server->GetIP());
686 servermet.WriteUInt16(server->GetPort());
687 servermet.WriteUInt32(tagcount);
689 if ( !server->GetListName().IsEmpty() ) {
690 // This is BOM to keep eMule compatibility
691 CTagString( ST_SERVERNAME, server->GetListName()).WriteTagToFile( &servermet, utf8strOptBOM);
692 CTagString( ST_SERVERNAME, server->GetListName()).WriteTagToFile( &servermet );
695 if ( !server->GetDynIP().IsEmpty() ) {
696 // This is BOM to keep eMule compatibility
697 CTagString( ST_DYNIP, server->GetDynIP()).WriteTagToFile( &servermet, utf8strOptBOM );
698 CTagString( ST_DYNIP, server->GetDynIP()).WriteTagToFile( &servermet );
701 if ( !server->GetDescription().IsEmpty() ) {
702 // This is BOM to keep eMule compatibility
703 CTagString( ST_DESCRIPTION, server->GetDescription()).WriteTagToFile( &servermet, utf8strOptBOM );
704 CTagString( ST_DESCRIPTION, server->GetDescription()).WriteTagToFile( &servermet );
707 if ( server->GetConnPort() != server->GetPort() ) {
708 CTagString( ST_AUXPORTSLIST, server->GetAuxPortsList() ).WriteTagToFile( &servermet );
711 CTagInt32( ST_FAIL, server->GetFailedCount() ).WriteTagToFile( &servermet );
712 CTagInt32( ST_PREFERENCE, server->GetPreferences() ).WriteTagToFile( &servermet );
713 CTagInt32( wxT("users"), server->GetUsers() ).WriteTagToFile( &servermet );
714 CTagInt32( wxT("files"), server->GetFiles() ).WriteTagToFile( &servermet );
715 CTagInt32( ST_PING, server->GetPing() ).WriteTagToFile( &servermet );
716 CTagInt32( ST_LASTPING, server->GetLastPingedTime()).WriteTagToFile( &servermet );
717 CTagInt32( ST_MAXUSERS, server->GetMaxUsers() ).WriteTagToFile( &servermet );
718 CTagInt32( ST_SOFTFILES, server->GetSoftFiles() ).WriteTagToFile( &servermet );
719 CTagInt32( ST_HARDFILES, server->GetHardFiles() ).WriteTagToFile( &servermet );
720 if (!server->GetVersion().IsEmpty()){
721 CTagString( ST_VERSION, server->GetVersion() ).WriteTagToFile( &servermet, utf8strOptBOM );
722 CTagString( ST_VERSION, server->GetVersion() ).WriteTagToFile( &servermet );
724 CTagInt32( ST_UDPFLAGS, server->GetUDPFlags() ).WriteTagToFile( &servermet );
725 CTagInt32( ST_LOWIDUSERS, server->GetLowIDUsers() ).WriteTagToFile( &servermet );
727 if (server->GetServerKeyUDP(true)) {
728 CTagInt32(ST_UDPKEY, server->GetServerKeyUDP(true)).WriteTagToFile( &servermet );;
731 if (server->GetServerKeyUDPIP()) {
732 CTagInt32(ST_UDPKEYIP, server->GetServerKeyUDPIP()).WriteTagToFile( &servermet );;;
735 if (server->GetObfuscationPortTCP()) {
736 CTagInt16(ST_TCPPORTOBFUSCATION, server->GetObfuscationPortTCP()).WriteTagToFile( &servermet );;;
739 if (server->GetObfuscationPortUDP()) {
740 CTagInt16(ST_UDPPORTOBFUSCATION, server->GetObfuscationPortUDP()).WriteTagToFile( &servermet );;;
744 } catch (const CIOFailureException& e) {
745 AddLogLineM(true, wxT("IO failure while writing 'server.met': ") + e.what());
746 return false;
749 servermet.Close();
750 const CPath curservermet = CPath(theApp->ConfigDir + wxT("server.met"));
751 const CPath oldservermet = CPath(theApp->ConfigDir + wxT("server_met.old"));
753 if (oldservermet.FileExists()) {
754 CPath::RemoveFile(oldservermet);
757 if (curservermet.FileExists()) {
758 CPath::RenameFile(curservermet, oldservermet);
761 CPath::RenameFile(newservermet, curservermet);
763 return true;
767 void CServerList::RemoveDeadServers()
769 if ( thePrefs::DeadServer() ) {
770 for ( CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ) {
771 CServer* server = *it++;
772 if ( server->GetFailedCount() > thePrefs::GetDeadserverRetries() && !server->IsStaticMember()) {
773 RemoveServer(server);
779 void CServerList::UpdateServerMetFromURL(const wxString& strURL)
781 if (strURL.Find(wxT("://")) == -1) {
782 AddLogLineM(true, _("Invalid URL"));
783 return;
785 URLUpdate = strURL;
786 wxString strTempFilename(theApp->ConfigDir + wxT("server.met.download"));
787 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(strURL,strTempFilename, HTTP_ServerMet);
788 downloader->Create();
789 downloader->Run();
793 void CServerList::DownloadFinished(uint32 result)
795 if(result == 1) {
796 const CPath tempFilename = CPath(theApp->ConfigDir + wxT("server.met.download"));
798 // curl succeeded. proceed with server.met loading
799 LoadServerMet(tempFilename);
800 SaveServerMet();
802 // So, file is loaded and merged, and also saved
803 CPath::RemoveFile(tempFilename);
804 AddLogLineM(true, CFormat(
805 _("Finished to download the server list from %s")) % URLUpdate);
806 } else {
807 AddLogLineM(true, CFormat( _("Failed to download the server list from %s") ) % URLUpdate);
812 void CServerList::AutoUpdate()
815 uint8 url_count = theApp->glob_prefs->adresses_list.GetCount();
817 if (!url_count) {
818 AddLogLineM(true, _("No server list address entry in 'addresses.dat' found. Please paste a valid server list address into this file in order to auto-update your server list"));
819 return;
821 // Do current URL. Callback function will take care of the others.
822 while ( current_url_index < url_count ) {
823 wxString URI = theApp->glob_prefs->adresses_list[current_url_index];
824 // We use wxURL to validate the URI
825 if ( wxURL( URI ).GetError() == wxURL_NOERR ) {
826 // Ok, got a valid URI
827 URLAutoUpdate = URI;
828 wxString strTempFilename =
829 theApp->ConfigDir + wxT("server_auto.met");
830 AddLogLineM(true, CFormat(
831 _("Start downloading server list from %s")) % URI);
832 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(
833 URI, strTempFilename, HTTP_ServerMetAuto);
834 downloader->Create();
835 downloader->Run();
837 return;
838 } else {
839 AddLogLineM(true, CFormat(
840 _("WARNING: invalid URL specified for auto-updating of servers: %s") ) % URI);
842 current_url_index++;
844 AddLogLineM(true, _("No valid server.met auto-download url on addresses.dat"));
848 void CServerList::AutoDownloadFinished(uint32 result)
851 if (result == 1) {
852 CPath tempFilename = CPath(theApp->ConfigDir + wxT("server_auto.met"));
854 // curl succeeded. proceed with server.met loading
855 LoadServerMet(tempFilename);
856 SaveServerMet();
858 // So, file is loaded and merged, and also saved
859 CPath::RemoveFile(tempFilename);
860 } else {
861 AddLogLineM(true, CFormat(_("Failed to download the server list from %s") ) % URLUpdate);
864 ++current_url_index;
867 if (current_url_index < theApp->glob_prefs->adresses_list.GetCount()) {
868 // Next!
869 AutoUpdate();
875 void CServerList::ObserverAdded( ObserverType* o )
877 CObservableQueue<CServer*>::ObserverAdded( o );
879 EventType::ValueList ilist;
880 ilist.reserve( m_servers.size() );
881 ilist.assign( m_servers.begin(), m_servers.end() );
883 NotifyObservers( EventType( EventType::INITIAL, &ilist ), o );
887 uint32 CServerList::GetAvgFile() const
889 //Since there is no real way to know how many files are in the kad network,
890 //I figure to try to use the ED2K network stats to find how many files the
891 //average user shares..
892 uint32 totaluser = 0;
893 uint32 totalfile = 0;
894 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
895 const CServer* const curr = *it;
896 //If this server has reported Users/Files and doesn't limit it's files too much
897 //use this in the calculation..
898 if( curr->GetUsers() && curr->GetFiles() && curr->GetSoftFiles() > 1000 ) {
899 totaluser += curr->GetUsers();
900 totalfile += curr->GetFiles();
903 //If the user count is a little low, don't send back a average..
904 //I added 50 to the count as many servers do not allow a large amount of files to be shared..
905 //Therefore the estimate here will be lower then the actual.
906 //I would love to add a way for the client to send some statistics back so we could see the real
907 //values here..
908 if ( totaluser > 500000 ) {
909 return (totalfile/totaluser)+50;
910 } else {
911 return 0;
916 std::vector<const CServer*> CServerList::CopySnapshot() const
918 std::vector<const CServer*> result;
919 result.reserve(m_servers.size());
920 result.assign(m_servers.begin(), m_servers.end());
921 return result;
925 void CServerList::FilterServers()
927 CInternalList::iterator it = m_servers.begin();
928 while (it != m_servers.end()) {
929 CServer* server = *it++;
931 if (server->HasDynIP()) {
932 continue;
935 if (theApp->ipfilter->IsFiltered(server->GetIP(), true)) {
936 if (server == theApp->serverconnect->GetCurrentServer()) {
937 AddLogLineM(true, _("Local server is filtered by the IPFilters, reconnecting to a different server!"));
938 theApp->serverconnect->Disconnect();
939 RemoveServer(server);
940 theApp->serverconnect->ConnectToAnyServer();
941 } else {
942 RemoveServer(server);
948 void CServerList::CheckForExpiredUDPKeys() {
950 if (!thePrefs::IsServerCryptLayerUDPEnabled()) {
951 return;
954 uint32 cKeysTotal = 0;
955 uint32 cKeysExpired = 0;
956 uint32 cPingDelayed = 0;
957 const uint32 dwIP = theApp->GetPublicIP();
958 const uint32 tNow = ::GetTickCount();
959 wxASSERT( dwIP != 0 );
961 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
962 CServer* pServer = *it;
963 if (pServer->SupportsObfuscationUDP() && pServer->GetServerKeyUDP(true) != 0 && pServer->GetServerKeyUDPIP() != dwIP){
964 cKeysTotal++;
965 cKeysExpired++;
966 if (tNow - pServer->GetRealLastPingedTime() < UDPSERVSTATMINREASKTIME){
967 cPingDelayed++;
968 // next ping: Now + (MinimumDelay - already elapsed time)
969 pServer->SetLastPingedTime((tNow - (uint32)UDPSERVSTATREASKTIME) + (UDPSERVSTATMINREASKTIME - (tNow - pServer->GetRealLastPingedTime())));
970 } else {
971 pServer->SetLastPingedTime(0);
973 } else if (pServer->SupportsObfuscationUDP() && pServer->GetServerKeyUDP(false) != 0) {
974 cKeysTotal++;
978 // File_checked_for_headers