Upstream tarball 9964
[amule.git] / src / ServerList.cpp
blob9594dac1a51956199fac712d83651779d64515ac
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
617 && (s->GetPort() == nUDPPort-4
618 || (bObfuscationPorts && s->GetObfuscationPortUDP() == nUDPPort)
619 || s->GetPort() == nUDPPort - 12))
620 return s;
622 return NULL;
625 bool CServerList::SaveServerMet()
627 CPath newservermet = CPath(theApp->ConfigDir + wxT("server.met.new"));
629 CFile servermet( newservermet, CFile::write );
630 if (!servermet.IsOpened()) {
631 AddLogLineM(false,_("Failed to save server.met!"));
632 return false;
636 try {
637 servermet.WriteUInt8(0xE0);
638 servermet.WriteUInt32( m_servers.size() );
640 for ( CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
641 const CServer* const server = *it;
643 uint16 tagcount = 12;
644 if (!server->GetListName().IsEmpty()) {
645 ++tagcount;
647 if (!server->GetDynIP().IsEmpty()) {
648 ++tagcount;
650 if (!server->GetDescription().IsEmpty()) {
651 ++tagcount;
653 if (server->GetConnPort() != server->GetPort()) {
654 ++tagcount;
657 // For unicoded name, description, and dynip
658 if ( !server->GetListName().IsEmpty() ) {
659 ++tagcount;
661 if ( !server->GetDynIP().IsEmpty() ) {
662 ++tagcount;
664 if ( !server->GetDescription().IsEmpty() ) {
665 ++tagcount;
667 if (!server->GetVersion().IsEmpty()) {
668 ++tagcount;
671 if (server->GetServerKeyUDP(true)) {
672 ++tagcount;
675 if (server->GetServerKeyUDPIP()) {
676 ++tagcount;
679 if (server->GetObfuscationPortTCP()) {
680 ++tagcount;
683 if (server->GetObfuscationPortUDP()) {
684 ++tagcount;
687 servermet.WriteUInt32(server->GetIP());
688 servermet.WriteUInt16(server->GetPort());
689 servermet.WriteUInt32(tagcount);
691 if ( !server->GetListName().IsEmpty() ) {
692 // This is BOM to keep eMule compatibility
693 CTagString( ST_SERVERNAME, server->GetListName()).WriteTagToFile( &servermet, utf8strOptBOM);
694 CTagString( ST_SERVERNAME, server->GetListName()).WriteTagToFile( &servermet );
697 if ( !server->GetDynIP().IsEmpty() ) {
698 // This is BOM to keep eMule compatibility
699 CTagString( ST_DYNIP, server->GetDynIP()).WriteTagToFile( &servermet, utf8strOptBOM );
700 CTagString( ST_DYNIP, server->GetDynIP()).WriteTagToFile( &servermet );
703 if ( !server->GetDescription().IsEmpty() ) {
704 // This is BOM to keep eMule compatibility
705 CTagString( ST_DESCRIPTION, server->GetDescription()).WriteTagToFile( &servermet, utf8strOptBOM );
706 CTagString( ST_DESCRIPTION, server->GetDescription()).WriteTagToFile( &servermet );
709 if ( server->GetConnPort() != server->GetPort() ) {
710 CTagString( ST_AUXPORTSLIST, server->GetAuxPortsList() ).WriteTagToFile( &servermet );
713 CTagInt32( ST_FAIL, server->GetFailedCount() ).WriteTagToFile( &servermet );
714 CTagInt32( ST_PREFERENCE, server->GetPreferences() ).WriteTagToFile( &servermet );
715 CTagInt32( wxT("users"), server->GetUsers() ).WriteTagToFile( &servermet );
716 CTagInt32( wxT("files"), server->GetFiles() ).WriteTagToFile( &servermet );
717 CTagInt32( ST_PING, server->GetPing() ).WriteTagToFile( &servermet );
718 CTagInt32( ST_LASTPING, server->GetLastPingedTime()).WriteTagToFile( &servermet );
719 CTagInt32( ST_MAXUSERS, server->GetMaxUsers() ).WriteTagToFile( &servermet );
720 CTagInt32( ST_SOFTFILES, server->GetSoftFiles() ).WriteTagToFile( &servermet );
721 CTagInt32( ST_HARDFILES, server->GetHardFiles() ).WriteTagToFile( &servermet );
722 if (!server->GetVersion().IsEmpty()){
723 CTagString( ST_VERSION, server->GetVersion() ).WriteTagToFile( &servermet, utf8strOptBOM );
724 CTagString( ST_VERSION, server->GetVersion() ).WriteTagToFile( &servermet );
726 CTagInt32( ST_UDPFLAGS, server->GetUDPFlags() ).WriteTagToFile( &servermet );
727 CTagInt32( ST_LOWIDUSERS, server->GetLowIDUsers() ).WriteTagToFile( &servermet );
729 if (server->GetServerKeyUDP(true)) {
730 CTagInt32(ST_UDPKEY, server->GetServerKeyUDP(true)).WriteTagToFile( &servermet );;
733 if (server->GetServerKeyUDPIP()) {
734 CTagInt32(ST_UDPKEYIP, server->GetServerKeyUDPIP()).WriteTagToFile( &servermet );;;
737 if (server->GetObfuscationPortTCP()) {
738 CTagInt16(ST_TCPPORTOBFUSCATION, server->GetObfuscationPortTCP()).WriteTagToFile( &servermet );;;
741 if (server->GetObfuscationPortUDP()) {
742 CTagInt16(ST_UDPPORTOBFUSCATION, server->GetObfuscationPortUDP()).WriteTagToFile( &servermet );;;
746 } catch (const CIOFailureException& e) {
747 AddLogLineM(true, wxT("IO failure while writing 'server.met': ") + e.what());
748 return false;
751 servermet.Close();
752 const CPath curservermet = CPath(theApp->ConfigDir + wxT("server.met"));
753 const CPath oldservermet = CPath(theApp->ConfigDir + wxT("server_met.old"));
755 if (oldservermet.FileExists()) {
756 CPath::RemoveFile(oldservermet);
759 if (curservermet.FileExists()) {
760 CPath::RenameFile(curservermet, oldservermet);
763 CPath::RenameFile(newservermet, curservermet);
765 return true;
769 void CServerList::RemoveDeadServers()
771 if ( thePrefs::DeadServer() ) {
772 for ( CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ) {
773 CServer* server = *it++;
774 if ( server->GetFailedCount() > thePrefs::GetDeadserverRetries() && !server->IsStaticMember()) {
775 RemoveServer(server);
781 void CServerList::UpdateServerMetFromURL(const wxString& strURL)
783 if (strURL.Find(wxT("://")) == -1) {
784 AddLogLineM(true, _("Invalid URL"));
785 return;
787 URLUpdate = strURL;
788 wxString strTempFilename(theApp->ConfigDir + wxT("server.met.download"));
789 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(strURL,strTempFilename, HTTP_ServerMet);
790 downloader->Create();
791 downloader->Run();
795 void CServerList::DownloadFinished(uint32 result)
797 if(result == 1) {
798 const CPath tempFilename = CPath(theApp->ConfigDir + wxT("server.met.download"));
800 // curl succeeded. proceed with server.met loading
801 LoadServerMet(tempFilename);
802 SaveServerMet();
804 // So, file is loaded and merged, and also saved
805 CPath::RemoveFile(tempFilename);
806 AddLogLineM(true, CFormat(
807 _("Finished to download the server list from %s")) % URLUpdate);
808 } else {
809 AddLogLineM(true, CFormat( _("Failed to download the server list from %s") ) % URLUpdate);
814 void CServerList::AutoUpdate()
817 uint8 url_count = theApp->glob_prefs->adresses_list.GetCount();
819 if (!url_count) {
820 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"));
821 return;
823 // Do current URL. Callback function will take care of the others.
824 while ( current_url_index < url_count ) {
825 wxString URI = theApp->glob_prefs->adresses_list[current_url_index];
826 // We use wxURL to validate the URI
827 if ( wxURL( URI ).GetError() == wxURL_NOERR ) {
828 // Ok, got a valid URI
829 URLAutoUpdate = URI;
830 wxString strTempFilename =
831 theApp->ConfigDir + wxT("server_auto.met");
832 AddLogLineM(true, CFormat(
833 _("Start downloading server list from %s")) % URI);
834 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(
835 URI, strTempFilename, HTTP_ServerMetAuto);
836 downloader->Create();
837 downloader->Run();
839 return;
840 } else {
841 AddLogLineM(true, CFormat(
842 _("WARNING: invalid URL specified for auto-updating of servers: %s") ) % URI);
844 current_url_index++;
846 AddLogLineM(true, _("No valid server.met auto-download url on addresses.dat"));
850 void CServerList::AutoDownloadFinished(uint32 result)
853 if (result == 1) {
854 CPath tempFilename = CPath(theApp->ConfigDir + wxT("server_auto.met"));
856 // curl succeeded. proceed with server.met loading
857 LoadServerMet(tempFilename);
858 SaveServerMet();
860 // So, file is loaded and merged, and also saved
861 CPath::RemoveFile(tempFilename);
862 } else {
863 AddLogLineM(true, CFormat(_("Failed to download the server list from %s") ) % URLUpdate);
866 ++current_url_index;
869 if (current_url_index < theApp->glob_prefs->adresses_list.GetCount()) {
870 // Next!
871 AutoUpdate();
877 void CServerList::ObserverAdded( ObserverType* o )
879 CObservableQueue<CServer*>::ObserverAdded( o );
881 EventType::ValueList ilist;
882 ilist.reserve( m_servers.size() );
883 ilist.assign( m_servers.begin(), m_servers.end() );
885 NotifyObservers( EventType( EventType::INITIAL, &ilist ), o );
889 uint32 CServerList::GetAvgFile() const
891 //Since there is no real way to know how many files are in the kad network,
892 //I figure to try to use the ED2K network stats to find how many files the
893 //average user shares..
894 uint32 totaluser = 0;
895 uint32 totalfile = 0;
896 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
897 const CServer* const curr = *it;
898 //If this server has reported Users/Files and doesn't limit it's files too much
899 //use this in the calculation..
900 if( curr->GetUsers() && curr->GetFiles() && curr->GetSoftFiles() > 1000 ) {
901 totaluser += curr->GetUsers();
902 totalfile += curr->GetFiles();
905 //If the user count is a little low, don't send back a average..
906 //I added 50 to the count as many servers do not allow a large amount of files to be shared..
907 //Therefore the estimate here will be lower then the actual.
908 //I would love to add a way for the client to send some statistics back so we could see the real
909 //values here..
910 if ( totaluser > 500000 ) {
911 return (totalfile/totaluser)+50;
912 } else {
913 return 0;
918 std::vector<const CServer*> CServerList::CopySnapshot() const
920 std::vector<const CServer*> result;
921 result.reserve(m_servers.size());
922 result.assign(m_servers.begin(), m_servers.end());
923 return result;
927 void CServerList::FilterServers()
929 CInternalList::iterator it = m_servers.begin();
930 while (it != m_servers.end()) {
931 CServer* server = *it++;
933 if (server->HasDynIP()) {
934 continue;
937 if (theApp->ipfilter->IsFiltered(server->GetIP(), true)) {
938 if (server == theApp->serverconnect->GetCurrentServer()) {
939 AddLogLineM(true, _("Local server is filtered by the IPFilters, reconnecting to a different server!"));
940 theApp->serverconnect->Disconnect();
941 RemoveServer(server);
942 theApp->serverconnect->ConnectToAnyServer();
943 } else {
944 RemoveServer(server);
950 void CServerList::CheckForExpiredUDPKeys() {
952 if (!thePrefs::IsServerCryptLayerUDPEnabled()) {
953 return;
956 uint32 cKeysTotal = 0;
957 uint32 cKeysExpired = 0;
958 uint32 cPingDelayed = 0;
959 const uint32 dwIP = theApp->GetPublicIP();
960 const uint32 tNow = ::GetTickCount();
961 wxASSERT( dwIP != 0 );
963 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
964 CServer* pServer = *it;
965 if (pServer->SupportsObfuscationUDP() && pServer->GetServerKeyUDP(true) != 0 && pServer->GetServerKeyUDPIP() != dwIP){
966 cKeysTotal++;
967 cKeysExpired++;
968 if (tNow - pServer->GetRealLastPingedTime() < UDPSERVSTATMINREASKTIME){
969 cPingDelayed++;
970 // next ping: Now + (MinimumDelay - already elapsed time)
971 pServer->SetLastPingedTime((tNow - (uint32)UDPSERVSTATREASKTIME) + (UDPSERVSTATMINREASKTIME - (tNow - pServer->GetRealLastPingedTime())));
972 } else {
973 pServer->SetLastPingedTime(0);
975 } else if (pServer->SupportsObfuscationUDP() && pServer->GetServerKeyUDP(false) != 0) {
976 cKeysTotal++;
980 // File_checked_for_headers