Fixed some build warnings in release mode
[amule.git] / src / ServerList.cpp
blobda50a1bb1f818fb710d248431fa4b8a9cc32c52d
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 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
55 #include <common/TextFile.h> // Needed for CTextFile
57 CServerList::CServerList()
59 m_serverpos = m_servers.end();
60 m_statserverpos = m_servers.end();
61 m_nLastED2KServerLinkCheck = ::GetTickCount();
65 bool CServerList::Init()
67 // Load Metfile
68 bool bRes = LoadServerMet(CPath(theApp->ConfigDir + wxT("server.met")));
70 // insert static servers from textfile
71 m_staticServersConfig = theApp->ConfigDir + wxT("staticservers.dat");
72 LoadStaticServers();
74 // Send the auto-update of server.met via HTTPThread requests
75 current_url_index = 0;
76 if ( thePrefs::AutoServerlist()) {
77 AutoUpdate();
80 return bRes;
84 bool CServerList::LoadServerMet(const CPath& path)
86 AddLogLineN(CFormat(_("Loading server.met file: %s")) % path);
88 bool merge = !m_servers.empty();
90 if (!path.FileExists()) {
91 AddLogLineN(_("Server.met file not found!"));
92 return false;
95 // Try to unpack the file, might be an archive
96 const wxChar* mets[] = { wxT("server.met"), NULL };
97 // Try to unpack the file, might be an archive
98 if (UnpackArchive(path, mets).second != EFT_Met) {
99 AddLogLineC(CFormat(_("Failed to load server.met file '%s', unknown format encountered.")) % path);
100 return false;
103 CFile servermet(path, CFile::read);
104 if ( !servermet.IsOpened() ){
105 AddLogLineN(_("Failed to open server.met!") );
106 return false;
110 try {
111 Notify_ServerFreeze();
113 byte version = servermet.ReadUInt8();
115 if (version != 0xE0 && version != MET_HEADER) {
116 AddLogLineC(CFormat(_("Server.met file corrupt, found invalid versiontag: 0x%x, size %i")) % version % sizeof(version));
117 Notify_ServerThaw();
118 return false;
121 uint32 fservercount = servermet.ReadUInt32();
123 ServerMet_Struct sbuffer;
124 uint32 iAddCount = 0;
126 for ( uint32 j = 0; j < fservercount; ++j ) {
127 sbuffer.ip = servermet.ReadUInt32();
128 sbuffer.port = servermet.ReadUInt16();
129 sbuffer.tagcount = servermet.ReadUInt32();
131 CServer* newserver = new CServer(&sbuffer);
133 // Load tags
134 for ( uint32 i = 0; i < sbuffer.tagcount; ++i ) {
135 newserver->AddTagFromFile(&servermet);
138 // Server priorities are not in sorted order
139 // High = 1, Low = 2, Normal = 0, so we have to check
140 // in a less logical fashion.
141 int priority = newserver->GetPreferences();
142 if (priority < SRV_PR_MIN || priority > SRV_PR_MAX) {
143 newserver->SetPreference(SRV_PR_NORMAL);
146 // set listname for server
147 if ( newserver->GetListName().IsEmpty() ) {
148 newserver->SetListName(wxT("Server ") +newserver->GetAddress());
152 if ( !theApp->AddServer(newserver) ) {
153 CServer* update = GetServerByAddress(newserver->GetAddress(), newserver->GetPort());
154 if(update) {
155 update->SetListName( newserver->GetListName());
156 if(!newserver->GetDescription().IsEmpty()) {
157 update->SetDescription( newserver->GetDescription());
159 Notify_ServerRefresh(update);
161 delete newserver;
162 } else {
163 ++iAddCount;
168 Notify_ServerThaw();
170 if (!merge) {
171 AddLogLineC(CFormat(wxPLURAL("%i server in server.met found", "%i servers in server.met found", fservercount)) % fservercount);
172 } else {
173 AddLogLineC(CFormat(wxPLURAL("%d server added", "%d servers added", iAddCount)) % iAddCount);
175 } catch (const CInvalidPacket& err) {
176 AddLogLineC(_("Error: the file 'server.met' is corrupted: ") + err.what());
177 Notify_ServerThaw();
178 return false;
179 } catch (const CSafeIOException& err) {
180 AddLogLineC(_("IO error while reading 'server.met': ") + err.what());
181 Notify_ServerThaw();
182 return false;
185 return true;
189 bool CServerList::AddServer(CServer* in_server, bool fromUser)
191 if ( !in_server->GetPort() ) {
192 if ( fromUser ) {
193 AddLogLineC(CFormat( _("Server not added: [%s:%d] does not specify a valid port.") )
194 % in_server->GetAddress()
195 % in_server->GetPort()
199 return false;
200 } else if (
201 !in_server->HasDynIP() &&
203 !IsGoodIP( in_server->GetIP(), thePrefs::FilterLanIPs() ) ||
204 ( // don't test for filtered while ipfilter is still loading,
205 // it will be filtered when filter loading is finished
206 theApp->ipfilter->IsReady()
207 && theApp->ipfilter->IsFiltered(in_server->GetIP(), true))
210 if ( fromUser ) {
211 AddLogLineC(CFormat( _("Server not added: The IP of [%s:%d] is filtered or invalid.") )
212 % in_server->GetAddress()
213 % in_server->GetPort()
217 return false;
220 CServer* test_server = GetServerByAddress(in_server->GetAddress(), in_server->GetPort());
221 // Avoid duplicate (dynIP) servers: If the server which is to be added, is a dynIP-server
222 // but we don't know yet it's DN, we need to search for an already available server with
223 // that IP.
224 if (test_server == NULL && in_server->GetIP() != 0) {
225 test_server = GetServerByIPTCP(in_server->GetIP(), in_server->GetPort());
228 if (test_server) {
229 if ( fromUser ) {
230 AddLogLineC(CFormat( _("Server not added: Server with matching IP:Port [%s:%d] found in list.") )
231 % in_server->GetAddress()
232 % in_server->GetPort()
236 test_server->ResetFailedCount();
237 Notify_ServerRefresh( test_server );
239 return false;
242 theStats::AddServer();
244 m_servers.push_back(in_server);
245 NotifyObservers( EventType( EventType::INSERTED, in_server ) );
247 if ( fromUser ) {
248 AddLogLineC(CFormat( _("Server added: Server at [%s:%d] using the name '%s'.") )
249 % in_server->GetAddress()
250 % in_server->GetPort()
251 % in_server->GetListName()
256 return true;
260 void CServerList::ServerStats()
262 uint32 tNow = ::GetTickCount();
264 if (theApp->IsConnectedED2K() && m_servers.size() > 0) {
265 CServer* ping_server = GetNextStatServer();
266 CServer* test = ping_server;
267 if (!ping_server) {
268 return;
271 while (ping_server->GetLastPingedTime() && (tNow - ping_server->GetLastPingedTime()) < UDPSERVSTATREASKTIME) {
272 ping_server = GetNextStatServer();
273 if (ping_server == test) {
274 return;
278 if (ping_server->GetFailedCount() >= thePrefs::GetDeadserverRetries() && thePrefs::DeadServer() && !ping_server->IsStaticMember()) {
279 RemoveServer(ping_server);
280 return;
283 srand((unsigned)time(NULL));
284 ping_server->SetRealLastPingedTime(tNow); // this is not used to calcualte the next ping, but only to ensure a minimum delay for premature pings
285 if (!ping_server->GetCryptPingReplyPending() && (!ping_server->GetLastPingedTime() || (tNow - ping_server->GetLastPingedTime()) >= UDPSERVSTATREASKTIME) && theApp->GetPublicIP() && thePrefs::IsServerCryptLayerUDPEnabled()) {
286 // We try a obfsucation ping first and wait 20 seconds for an answer
287 // if it doesn't get responsed, we don't count it as error but continue with a normal ping
288 ping_server->SetCryptPingReplyPending(true);
289 uint32 nPacketLen = 4 + (uint8)(rand() % 16); // max padding 16 bytes
290 CScopedArray<byte> pRawPacket(nPacketLen);
291 uint32 dwChallenge = (rand() << 17) | (rand() << 2) | (rand() & 0x03);
292 if (dwChallenge == 0) {
293 dwChallenge++;
296 memcpy(pRawPacket.get(), &dwChallenge, sizeof(uint32));
297 for (uint32 i = 4; i < nPacketLen; i++) { // fillng up the remaining bytes with random data
298 pRawPacket[i] = (uint8)rand();
301 ping_server->SetChallenge(dwChallenge);
302 ping_server->SetLastPinged(tNow);
303 ping_server->SetLastPingedTime((tNow - (uint32)UDPSERVSTATREASKTIME) + 20); // give it 20 seconds to respond
305 AddDebugLogLineN(logServerUDP, CFormat(wxT(">> Sending OP__GlobServStatReq (obfuscated) to server %s:%u")) % ping_server->GetAddress() % ping_server->GetPort());
307 CPacket* packet = new CPacket(pRawPacket[1], nPacketLen - 2, pRawPacket[0]);
308 packet->CopyToDataBuffer(0, pRawPacket.get() + 2, nPacketLen - 2);
310 theStats::AddUpOverheadServer(packet->GetPacketSize());
311 theApp->serverconnect->SendUDPPacket(packet, ping_server, true, true /*raw packet*/, 12 /* Port offset is 12 for obfuscated encryption*/);
312 } else if (ping_server->GetCryptPingReplyPending() || theApp->GetPublicIP() == 0 || !thePrefs::IsServerCryptLayerUDPEnabled()){
313 // our obfsucation ping request was not answered, so probably the server doesn'T supports obfuscation
314 // continue with a normal request
315 if (ping_server->GetCryptPingReplyPending() && thePrefs::IsServerCryptLayerUDPEnabled()) {
316 AddDebugLogLineN(logServerUDP, wxT("CryptPing failed for server ") + ping_server->GetListName());
317 } else if (thePrefs::IsServerCryptLayerUDPEnabled()) {
318 AddDebugLogLineN(logServerUDP, wxT("CryptPing skipped because our public IP is unknown for server ") + ping_server->GetListName());
321 ping_server->SetCryptPingReplyPending(false);
323 CPacket* packet = new CPacket(OP_GLOBSERVSTATREQ, 4, OP_EDONKEYPROT);
324 uint32 challenge = 0x55AA0000 + (uint16)rand();
325 ping_server->SetChallenge(challenge);
326 packet->CopyUInt32ToDataBuffer(challenge);
327 ping_server->SetLastPinged(tNow);
328 ping_server->SetLastPingedTime(tNow - (rand() % HR2S(1)));
329 ping_server->AddFailedCount();
330 Notify_ServerRefresh(ping_server);
331 theStats::AddUpOverheadServer(packet->GetPacketSize());
332 theApp->serverconnect->SendUDPPacket(packet, ping_server, true);
333 } else {
334 wxFAIL;
340 void CServerList::RemoveServer(CServer* in_server)
342 if (in_server == theApp->serverconnect->GetCurrentServer()) {
343 theApp->ShowAlert(_("You are connected to the server you are trying to delete. please disconnect first."), _("Info"), wxOK);
344 } else {
345 CInternalList::iterator it = std::find(m_servers.begin(), m_servers.end(), in_server);
346 if ( it != m_servers.end() ) {
347 if (theApp->downloadqueue->GetUDPServer() == in_server) {
348 theApp->downloadqueue->SetUDPServer( 0 );
351 NotifyObservers( EventType( EventType::REMOVED, in_server ) );
353 if (m_serverpos == it) {
354 ++m_serverpos;
356 if (m_statserverpos == it) {
357 ++m_statserverpos;
359 m_servers.erase(it);
360 theStats::DeleteServer();
362 Notify_ServerRemove(in_server);
363 delete in_server;
369 void CServerList::RemoveAllServers()
371 NotifyObservers( EventType( EventType::CLEARED ) );
373 theStats::DeleteAllServers();
374 // no connection, safely remove all servers
375 while ( !m_servers.empty() ) {
376 delete m_servers.back();
377 m_servers.pop_back();
379 m_serverpos = m_servers.end();
380 m_statserverpos = m_servers.end();
384 void CServerList::GetStatus(uint32 &failed, uint32 &user, uint32 &file, uint32 &tuser, uint32 &tfile,float &occ)
386 failed = 0;
387 user = 0;
388 file = 0;
389 tuser=0;
390 tfile = 0;
391 occ=0;
392 uint32 maxusers=0;
393 uint32 tuserk = 0;
395 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
396 const CServer* const curr = *it;
397 if( curr->GetFailedCount() ) {
398 ++failed;
399 } else {
400 user += curr->GetUsers();
401 file += curr->GetFiles();
403 tuser += curr->GetUsers();
404 tfile += curr->GetFiles();
406 if (curr->GetMaxUsers()) {
407 tuserk += curr->GetUsers(); // total users on servers with known maximum
408 maxusers+=curr->GetMaxUsers();
411 if (maxusers>0) {
412 occ=(float)(tuserk*100)/maxusers;
417 void CServerList::GetUserFileStatus(uint32 &user, uint32 &file)
419 user = 0;
420 file = 0;
421 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
422 const CServer* const curr = *it;
423 if( !curr->GetFailedCount() ) {
424 user += curr->GetUsers();
425 file += curr->GetFiles();
431 CServerList::~CServerList()
433 if (thePrefs::GetNetworkED2K()) {
434 SaveServerMet();
436 while ( !m_servers.empty() ) {
437 delete m_servers.back();
438 m_servers.pop_back();
443 void CServerList::LoadStaticServers()
445 if ( !CPath::FileExists(m_staticServersConfig) ) {
446 return;
449 wxFileInputStream stream(m_staticServersConfig);
450 wxTextInputStream f(stream);
452 while ( !stream.Eof() ) {
453 wxString line = f.ReadLine();
455 // Skip comments
456 if ( line.GetChar(0) == '#' || line.GetChar(0) == '/') {
457 continue;
460 wxStringTokenizer tokens( line, wxT(",") );
462 if ( tokens.CountTokens() != 3 ) {
463 continue;
467 // format is host:port,priority,Name
468 wxString addy = tokens.GetNextToken().Strip( wxString::both );
469 wxString prio = tokens.GetNextToken().Strip( wxString::both );
470 wxString name = tokens.GetNextToken().Strip( wxString::both );
472 wxString host = addy.BeforeFirst( wxT(':') );
473 wxString port = addy.AfterFirst( wxT(':') );
476 int priority = StrToLong( prio );
477 if (priority < SRV_PR_MIN || priority > SRV_PR_MAX) {
478 priority = SRV_PR_NORMAL;
482 // We need a valid name for the list
483 if ( name.IsEmpty() ) {
484 name = addy;
488 // create server object and add it to the list
489 CServer* server = new CServer( StrToLong( port ), host );
491 server->SetListName( name );
492 server->SetIsStaticMember( true );
493 server->SetPreference( priority );
496 // Try to add the server to the list
497 if ( !theApp->AddServer( server ) ) {
498 delete server;
499 CServer* existing = GetServerByAddress( host, StrToULong( port ) );
500 if ( existing) {
501 existing->SetListName( name );
502 existing->SetIsStaticMember( true );
503 existing->SetPreference( priority );
505 Notify_ServerRefresh( existing );
511 void CServerList::SaveStaticServers()
513 CTextFile file;
514 if (!file.Open(m_staticServersConfig, CTextFile::write)) {
515 AddLogLineN(CFormat( _("Failed to open '%s'") ) % m_staticServersConfig );
516 return;
519 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
520 const CServer* server = *it;
522 if (server->IsStaticMember()) {
523 file.WriteLine(CFormat(wxT("%s:%u,%u,%s"))
524 % server->GetAddress() % server->GetPort()
525 % server->GetPreferences() % server->GetListName());
529 file.Close();
532 struct ServerPriorityComparator {
533 // Return true iff lhs should strictly appear earlier in the list than rhs.
534 // In this case, we want higher priority servers to appear earlier.
535 bool operator()(const CServer* lhs, const CServer* rhs) {
536 wxASSERT
538 rhs->GetPreferences() == SRV_PR_LOW ||
539 rhs->GetPreferences() == SRV_PR_NORMAL ||
540 rhs->GetPreferences() == SRV_PR_HIGH
542 switch (lhs->GetPreferences()) {
543 case SRV_PR_LOW:
544 return false;
545 case SRV_PR_NORMAL:
546 return rhs->GetPreferences() == SRV_PR_LOW;
547 case SRV_PR_HIGH:
548 return rhs->GetPreferences() != SRV_PR_HIGH;
549 default:
550 wxFAIL;
551 return false;
556 void CServerList::Sort()
558 m_servers.sort(ServerPriorityComparator());
559 // Once the list has been sorted, it doesn't really make sense to continue
560 // traversing the new order from the old position. Plus, there's a bug in
561 // version of libstdc++ before gcc4 such that iterators that were equal to
562 // end() were left dangling.
563 m_serverpos = m_servers.begin();
564 m_statserverpos = m_servers.begin();
568 CServer* CServerList::GetNextServer(bool bOnlyObfuscated)
570 while (bOnlyObfuscated && (m_serverpos != m_servers.end()) && !((*m_serverpos)->SupportsObfuscationTCP() || (*m_serverpos)->SupportsObfuscationUDP())) {
571 wxASSERT(*m_serverpos != NULL);
572 ++m_serverpos;
575 if (m_serverpos == m_servers.end()) {
576 return 0;
577 } else {
578 if (*m_serverpos) {
579 return *m_serverpos++;
580 } else {
581 return 0;
587 CServer* CServerList::GetNextStatServer()
589 if (m_statserverpos == m_servers.end()) {
590 m_statserverpos = m_servers.begin();
593 if (m_statserverpos == m_servers.end()) {
594 return 0;
595 } else {
596 wxASSERT(*m_statserverpos != NULL);
597 return *m_statserverpos++;
602 CServer* CServerList::GetServerByAddress(const wxString& address, uint16 port) const
604 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
605 CServer* const s = *it;
606 if (port == s->GetPort() && s->GetAddress() == address) {
607 return s;
610 return NULL;
614 CServer* CServerList::GetServerByIP(uint32 nIP) const
616 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
617 CServer* const s = *it;
618 if (s->GetIP() == nIP)
619 return s;
621 return NULL;
625 CServer* CServerList::GetServerByIPTCP(uint32 nIP, uint16 nPort) const
627 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
628 CServer* const s = *it;
629 if (s->GetIP() == nIP && s->GetPort() == nPort)
630 return s;
632 return NULL;
636 CServer* CServerList::GetServerByIPUDP(uint32 nIP, uint16 nUDPPort, bool bObfuscationPorts) const
638 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
639 CServer* const s =*it;
640 if (s->GetIP() == nIP
641 && (s->GetPort() == nUDPPort-4
642 || (bObfuscationPorts && s->GetObfuscationPortUDP() == nUDPPort)
643 || s->GetPort() == nUDPPort - 12))
644 return s;
646 return NULL;
650 CServer* CServerList::GetServerByECID(uint32 ecid) const
652 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
653 CServer* const s = *it;
654 if (s->ECID() == ecid) {
655 return s;
658 return NULL;
662 void CServerList::SetStaticServer(CServer* server, bool isStatic)
664 server->SetIsStaticMember(isStatic);
665 Notify_ServerRefresh(server);
666 SaveStaticServers();
670 void CServerList::SetServerPrio(CServer* server, uint32 prio)
672 server->SetPreference(prio);
673 Notify_ServerRefresh(server);
677 bool CServerList::SaveServerMet()
679 CPath newservermet = CPath(theApp->ConfigDir + wxT("server.met.new"));
681 CFile servermet( newservermet, CFile::write );
682 if (!servermet.IsOpened()) {
683 AddLogLineN(_("Failed to save server.met!"));
684 return false;
688 try {
689 servermet.WriteUInt8(0xE0);
690 servermet.WriteUInt32( m_servers.size() );
692 for ( CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
693 const CServer* const server = *it;
695 uint16 tagcount = 12;
696 if (!server->GetListName().IsEmpty()) {
697 ++tagcount;
699 if (!server->GetDynIP().IsEmpty()) {
700 ++tagcount;
702 if (!server->GetDescription().IsEmpty()) {
703 ++tagcount;
705 if (server->GetConnPort() != server->GetPort()) {
706 ++tagcount;
709 // For unicoded name, description, and dynip
710 if ( !server->GetListName().IsEmpty() ) {
711 ++tagcount;
713 if ( !server->GetDynIP().IsEmpty() ) {
714 ++tagcount;
716 if ( !server->GetDescription().IsEmpty() ) {
717 ++tagcount;
719 if (!server->GetVersion().IsEmpty()) {
720 ++tagcount;
723 if (server->GetServerKeyUDP(true)) {
724 ++tagcount;
727 if (server->GetServerKeyUDPIP()) {
728 ++tagcount;
731 if (server->GetObfuscationPortTCP()) {
732 ++tagcount;
735 if (server->GetObfuscationPortUDP()) {
736 ++tagcount;
739 servermet.WriteUInt32(server->GetIP());
740 servermet.WriteUInt16(server->GetPort());
741 servermet.WriteUInt32(tagcount);
743 if ( !server->GetListName().IsEmpty() ) {
744 // This is BOM to keep eMule compatibility
745 CTagString( ST_SERVERNAME, server->GetListName()).WriteTagToFile( &servermet, utf8strOptBOM);
746 CTagString( ST_SERVERNAME, server->GetListName()).WriteTagToFile( &servermet );
749 if ( !server->GetDynIP().IsEmpty() ) {
750 // This is BOM to keep eMule compatibility
751 CTagString( ST_DYNIP, server->GetDynIP()).WriteTagToFile( &servermet, utf8strOptBOM );
752 CTagString( ST_DYNIP, server->GetDynIP()).WriteTagToFile( &servermet );
755 if ( !server->GetDescription().IsEmpty() ) {
756 // This is BOM to keep eMule compatibility
757 CTagString( ST_DESCRIPTION, server->GetDescription()).WriteTagToFile( &servermet, utf8strOptBOM );
758 CTagString( ST_DESCRIPTION, server->GetDescription()).WriteTagToFile( &servermet );
761 if ( server->GetConnPort() != server->GetPort() ) {
762 CTagString( ST_AUXPORTSLIST, server->GetAuxPortsList() ).WriteTagToFile( &servermet );
765 CTagInt32( ST_FAIL, server->GetFailedCount() ).WriteTagToFile( &servermet );
766 CTagInt32( ST_PREFERENCE, server->GetPreferences() ).WriteTagToFile( &servermet );
767 CTagInt32( wxT("users"), server->GetUsers() ).WriteTagToFile( &servermet );
768 CTagInt32( wxT("files"), server->GetFiles() ).WriteTagToFile( &servermet );
769 CTagInt32( ST_PING, server->GetPing() ).WriteTagToFile( &servermet );
770 CTagInt32( ST_LASTPING, server->GetLastPingedTime()).WriteTagToFile( &servermet );
771 CTagInt32( ST_MAXUSERS, server->GetMaxUsers() ).WriteTagToFile( &servermet );
772 CTagInt32( ST_SOFTFILES, server->GetSoftFiles() ).WriteTagToFile( &servermet );
773 CTagInt32( ST_HARDFILES, server->GetHardFiles() ).WriteTagToFile( &servermet );
774 if (!server->GetVersion().IsEmpty()){
775 CTagString( ST_VERSION, server->GetVersion() ).WriteTagToFile( &servermet, utf8strOptBOM );
776 CTagString( ST_VERSION, server->GetVersion() ).WriteTagToFile( &servermet );
778 CTagInt32( ST_UDPFLAGS, server->GetUDPFlags() ).WriteTagToFile( &servermet );
779 CTagInt32( ST_LOWIDUSERS, server->GetLowIDUsers() ).WriteTagToFile( &servermet );
781 if (server->GetServerKeyUDP(true)) {
782 CTagInt32(ST_UDPKEY, server->GetServerKeyUDP(true)).WriteTagToFile( &servermet );;
785 if (server->GetServerKeyUDPIP()) {
786 CTagInt32(ST_UDPKEYIP, server->GetServerKeyUDPIP()).WriteTagToFile( &servermet );;;
789 if (server->GetObfuscationPortTCP()) {
790 CTagInt16(ST_TCPPORTOBFUSCATION, server->GetObfuscationPortTCP()).WriteTagToFile( &servermet );;;
793 if (server->GetObfuscationPortUDP()) {
794 CTagInt16(ST_UDPPORTOBFUSCATION, server->GetObfuscationPortUDP()).WriteTagToFile( &servermet );;;
798 } catch (const CIOFailureException& e) {
799 AddLogLineC(wxT("IO failure while writing 'server.met': ") + e.what());
800 return false;
803 servermet.Close();
804 const CPath curservermet = CPath(theApp->ConfigDir + wxT("server.met"));
805 const CPath oldservermet = CPath(theApp->ConfigDir + wxT("server_met.old"));
807 if (oldservermet.FileExists()) {
808 CPath::RemoveFile(oldservermet);
811 if (curservermet.FileExists()) {
812 CPath::RenameFile(curservermet, oldservermet);
815 CPath::RenameFile(newservermet, curservermet);
817 return true;
821 void CServerList::RemoveDeadServers()
823 if ( thePrefs::DeadServer() ) {
824 for ( CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ) {
825 CServer* server = *it++;
826 if ( server->GetFailedCount() > thePrefs::GetDeadserverRetries() && !server->IsStaticMember()) {
827 RemoveServer(server);
833 void CServerList::UpdateServerMetFromURL(const wxString& strURL)
835 if (strURL.Find(wxT("://")) == -1) {
836 AddLogLineC(_("Invalid URL"));
837 return;
839 m_URLUpdate = strURL;
840 wxString strTempFilename(theApp->ConfigDir + wxT("server.met.download"));
841 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(strURL, strTempFilename, theApp->ConfigDir + wxT("server.met"), HTTP_ServerMet, false, false);
842 downloader->Create();
843 downloader->Run();
847 void CServerList::DownloadFinished(uint32 result)
849 if(result == HTTP_Success) {
850 const CPath tempFilename = CPath(theApp->ConfigDir + wxT("server.met.download"));
852 // curl succeeded. proceed with server.met loading
853 LoadServerMet(tempFilename);
854 SaveServerMet();
856 // So, file is loaded and merged, and also saved
857 CPath::RemoveFile(tempFilename);
858 AddLogLineN(CFormat(_("Finished downloading the server list from %s")) % m_URLUpdate);
859 } else if (result == HTTP_Skipped) {
860 AddLogLineN(CFormat(_("Skipped download of %s, because requested file is not newer.")) % wxT("server.met"));
861 } else {
862 AddLogLineC(CFormat(_("Failed to download %s from %s")) % wxT("server.met") % m_URLUpdate);
867 void CServerList::AutoUpdate()
870 uint8 url_count = theApp->glob_prefs->adresses_list.GetCount();
872 if (!url_count) {
873 AddLogLineC(_("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"));
874 return;
876 // Do current URL. Callback function will take care of the others.
877 while ( current_url_index < url_count ) {
878 wxString URI = theApp->glob_prefs->adresses_list[current_url_index];
879 // We use wxURL to validate the URI
880 if ( wxURL( URI ).GetError() == wxURL_NOERR ) {
881 // Ok, got a valid URI
882 m_URLUpdate = URI;
883 wxString strTempFilename =
884 theApp->ConfigDir + wxT("server_auto.met");
885 AddLogLineC(CFormat(
886 _("Start downloading server list from %s")) % URI);
887 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(
888 URI, strTempFilename, theApp->ConfigDir + wxT("server.met"), HTTP_ServerMetAuto, false, false);
889 downloader->Create();
890 downloader->Run();
892 return;
893 } else {
894 AddLogLineC(CFormat(
895 _("WARNING: invalid URL specified for auto-updating of servers: %s") ) % URI);
897 current_url_index++;
899 AddLogLineC(_("No valid server.met auto-download url on addresses.dat"));
903 void CServerList::AutoDownloadFinished(uint32 result)
905 if (result == HTTP_Success) {
906 CPath tempFilename = CPath(theApp->ConfigDir + wxT("server_auto.met"));
908 // curl succeeded. proceed with server.met loading
909 LoadServerMet(tempFilename);
910 SaveServerMet();
912 // So, file is loaded and merged, and also saved
913 CPath::RemoveFile(tempFilename);
914 } else {
915 AddLogLineC(CFormat(_("Failed to download the server list from %s") ) % m_URLUpdate);
918 ++current_url_index;
920 if (current_url_index < theApp->glob_prefs->adresses_list.GetCount()) {
921 // Next!
922 AutoUpdate();
927 void CServerList::ObserverAdded( ObserverType* o )
929 CObservableQueue<CServer*>::ObserverAdded( o );
931 EventType::ValueList ilist;
932 ilist.reserve( m_servers.size() );
933 ilist.assign( m_servers.begin(), m_servers.end() );
935 NotifyObservers( EventType( EventType::INITIAL, &ilist ), o );
939 uint32 CServerList::GetAvgFile() const
941 //Since there is no real way to know how many files are in the kad network,
942 //I figure to try to use the ED2K network stats to find how many files the
943 //average user shares..
944 uint32 totaluser = 0;
945 uint32 totalfile = 0;
946 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it){
947 const CServer* const curr = *it;
948 //If this server has reported Users/Files and doesn't limit it's files too much
949 //use this in the calculation..
950 if( curr->GetUsers() && curr->GetFiles() && curr->GetSoftFiles() > 1000 ) {
951 totaluser += curr->GetUsers();
952 totalfile += curr->GetFiles();
955 //If the user count is a little low, don't send back a average..
956 //I added 50 to the count as many servers do not allow a large amount of files to be shared..
957 //Therefore the estimate here will be lower then the actual.
958 //I would love to add a way for the client to send some statistics back so we could see the real
959 //values here..
960 if ( totaluser > 500000 ) {
961 return (totalfile/totaluser)+50;
962 } else {
963 return 0;
968 std::vector<const CServer*> CServerList::CopySnapshot() const
970 std::vector<const CServer*> result;
971 result.reserve(m_servers.size());
972 result.assign(m_servers.begin(), m_servers.end());
973 return result;
977 void CServerList::FilterServers()
979 CInternalList::iterator it = m_servers.begin();
980 while (it != m_servers.end()) {
981 CServer* server = *it++;
983 if (server->HasDynIP()) {
984 continue;
987 if (theApp->ipfilter->IsFiltered(server->GetIP(), true)) {
988 if (server == theApp->serverconnect->GetCurrentServer()) {
989 AddLogLineC(_("Local server is filtered by the IPFilters, reconnecting to a different server!"));
990 theApp->serverconnect->Disconnect();
991 RemoveServer(server);
992 theApp->serverconnect->ConnectToAnyServer();
993 } else {
994 RemoveServer(server);
1000 void CServerList::CheckForExpiredUDPKeys() {
1002 if (!thePrefs::IsServerCryptLayerUDPEnabled()) {
1003 return;
1006 uint32 cKeysTotal = 0;
1007 uint32 cKeysExpired = 0;
1008 uint32 cPingDelayed = 0;
1009 const uint32 dwIP = theApp->GetPublicIP();
1010 const uint32 tNow = ::GetTickCount();
1011 wxASSERT( dwIP != 0 );
1013 for (CInternalList::const_iterator it = m_servers.begin(); it != m_servers.end(); ++it) {
1014 CServer* pServer = *it;
1015 if (pServer->SupportsObfuscationUDP() && pServer->GetServerKeyUDP(true) != 0 && pServer->GetServerKeyUDPIP() != dwIP){
1016 cKeysTotal++;
1017 cKeysExpired++;
1018 if (tNow - pServer->GetRealLastPingedTime() < UDPSERVSTATMINREASKTIME){
1019 cPingDelayed++;
1020 // next ping: Now + (MinimumDelay - already elapsed time)
1021 pServer->SetLastPingedTime((tNow - (uint32)UDPSERVSTATREASKTIME) + (UDPSERVSTATMINREASKTIME - (tNow - pServer->GetRealLastPingedTime())));
1022 } else {
1023 pServer->SetLastPingedTime(0);
1025 } else if (pServer->SupportsObfuscationUDP() && pServer->GetServerKeyUDP(false) != 0) {
1026 cKeysTotal++;
1030 // File_checked_for_headers