Upstream tarball 20080720
[amule.git] / src / DownloadQueue.cpp
blob7d2b4038ca1a0fbb182a11c58a6b3aa2444f18d8
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 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 "DownloadQueue.h" // Interface declarations
28 #include <protocol/Protocols.h>
29 #include <protocol/kad/Constants.h>
30 #include <common/Macros.h>
31 #include <common/MenuIDs.h>
32 #include <common/Constants.h>
34 #include <wx/textfile.h> // Needed for wxTextFile
35 #include <wx/utils.h>
37 #include "Server.h" // Needed for CServer
38 #include "Packet.h" // Needed for CPacket
39 #include "MemFile.h" // Needed for CMemFile
40 #include "ClientList.h" // Needed for CClientList
41 #include "updownclient.h" // Needed for CUpDownClient
42 #include "ServerList.h" // Needed for CServerList
43 #include "ServerConnect.h" // Needed for CServerConnect
44 #include "ED2KLink.h" // Needed for CED2KFileLink
45 #include "SearchList.h" // Needed for CSearchFile
46 #include "SharedFileList.h" // Needed for CSharedFileList
47 #include "PartFile.h" // Needed for CPartFile
48 #include "Preferences.h" // Needed for thePrefs
49 #include "amule.h" // Needed for theApp
50 #include "AsyncDNS.h" // Needed for CAsyncDNS
51 #include "Statistics.h" // Needed for theStats
52 #include "Logger.h"
53 #include <common/Format.h> // Needed for CFormat
54 #include "IPFilter.h"
55 #include <common/FileFunctions.h> // Needed for CDirIterator
56 #include "FileLock.h" // Needed for CFileLock
57 #include "GuiEvents.h" // Needed for Notify_*
58 #include "UserEvents.h"
59 #include "MagnetURI.h" // Needed for CMagnetED2KConverter
60 #include "ScopedPtr.h" // Needed for CScopedPtr
61 #include "PlatformSpecific.h" // Needed for CanFSHandleLargeFiles
63 #include "kademlia/kademlia/Kademlia.h"
65 #include <string> // Do_not_auto_remove (mingw-gcc-3.4.5)
68 // Max. file IDs per UDP packet
69 // ----------------------------
70 // 576 - 30 bytes of header (28 for UDP, 2 for "E3 9A" edonkey proto) = 546 bytes
71 // 546 / 16 = 34
74 #define MAX_FILES_PER_UDP_PACKET 31 // 2+16*31 = 498 ... is still less than 512 bytes!!
75 #define MAX_REQUESTS_PER_SERVER 35
78 CDownloadQueue::CDownloadQueue()
79 // Needs to be recursive that that is can own an observer assigned to itself
80 : m_mutex( wxMUTEX_RECURSIVE )
82 m_datarate = 0;
83 m_udpserver = 0;
84 m_lastsorttime = 0;
85 m_lastudpsearchtime = 0;
86 m_lastudpstattime = 0;
87 m_udcounter = 0;
88 m_nLastED2KLinkCheck = 0;
89 m_dwNextTCPSrcReq = 0;
90 m_cRequestsSentToServer = 0;
91 m_lastDiskCheck = 0;
92 SetLastKademliaFileRequest();
96 CDownloadQueue::~CDownloadQueue()
98 if ( !m_filelist.empty() ) {
99 for ( unsigned int i = 0; i < m_filelist.size(); i++ ) {
100 printf("\rSaving PartFile %u of %u", i + 1, (unsigned int)m_filelist.size());
101 fflush(stdout);
102 delete m_filelist[i];
104 printf("\nAll PartFiles Saved.\n");
109 void CDownloadQueue::LoadMetFiles(const CPath& path)
111 printf("Loading temp files from %s.\n",
112 (const char *)unicode2char(path.GetPrintable()));
114 std::vector<CPath> files;
116 // Locate part-files to be loaded
117 CDirIterator TempDir(path);
118 CPath fileName = TempDir.GetFirstFile(CDirIterator::File, wxT("*.part.met"));
119 while (fileName.IsOk()) {
120 files.push_back(path.JoinPaths(fileName));
122 fileName = TempDir.GetNextFile();
125 // Loading in order makes it easier to figure which
126 // file is broken in case of crashes, or the like.
127 std::sort(files.begin(), files.end());
129 // Load part-files
130 for ( size_t i = 0; i < files.size(); i++ ) {
131 printf("\rLoading PartFile %u of %u", (unsigned int)(i + 1), (unsigned int)files.size());
132 fileName = files[i].GetFullName();
133 CPartFile *toadd = new CPartFile();
134 bool result = toadd->LoadPartFile(path, fileName) != 0;
135 if (!result) {
136 // Try from backup
137 result = toadd->LoadPartFile(path, fileName, true) != 0;
139 if (result && !IsFileExisting(toadd->GetFileHash())) {
141 wxMutexLocker lock(m_mutex);
142 m_filelist.push_back(toadd);
144 NotifyObservers(EventType(EventType::INSERTED, toadd));
145 Notify_DownloadCtrlAddFile(toadd);
146 } else {
147 wxString msg;
148 if (result) {
149 msg << CFormat(wxT("WARNING: Duplicate partfile with hash '%s' found, skipping: %s"))
150 % toadd->GetFileHash().Encode() % fileName;
151 } else {
152 // If result is false, then reading of both the primary and the backup .met failed
153 AddLogLineM(false,
154 _("ERROR: Failed to load backup file. Search http://forum.amule.org for .part.met recovery solutions."));
155 msg << CFormat(wxT("ERROR: Failed to load PartFile '%s'")) % fileName;
157 AddDebugLogLineM(true, logPartFile, msg);
159 // Newline so that the error stays visible.
160 printf(": %s\n", (const char*)unicode2char(msg));
162 // Delete the partfile object in the end.
163 delete toadd;
166 printf("\nAll PartFiles Loaded.\n");
168 if ( GetFileCount() == 0 ) {
169 AddLogLineM(false, _("No part files found"));
170 } else {
171 AddLogLineM(false, wxString::Format(wxPLURAL("Found %u part file", "Found %u part files", GetFileCount()), GetFileCount()) );
173 DoSortByPriority();
174 CheckDiskspace( path );
179 uint16 CDownloadQueue::GetFileCount() const
181 wxMutexLocker lock( m_mutex );
183 return m_filelist.size();
187 CServer* CDownloadQueue::GetUDPServer() const
189 wxMutexLocker lock( m_mutex );
191 return m_udpserver;
195 void CDownloadQueue::SetUDPServer( CServer* server )
197 wxMutexLocker lock( m_mutex );
199 m_udpserver = server;
203 void CDownloadQueue::SaveSourceSeeds()
205 for ( uint16 i = 0; i < GetFileCount(); i++ ) {
206 GetFileByIndex( i )->SaveSourceSeeds();
211 void CDownloadQueue::LoadSourceSeeds()
213 for ( uint16 i = 0; i < GetFileCount(); i++ ) {
214 GetFileByIndex( i )->LoadSourceSeeds();
219 void CDownloadQueue::AddSearchToDownload(CSearchFile* toadd, uint8 category)
221 if ( IsFileExisting(toadd->GetFileHash()) ) {
222 return;
225 if (toadd->GetFileSize() > OLD_MAX_FILE_SIZE) {
226 if (!PlatformSpecific::CanFSHandleLargeFiles(thePrefs::GetTempDir())) {
227 AddLogLineM(true, _("Filesystem for Temp directory cannot handle large files."));
228 return;
229 } else if (!PlatformSpecific::CanFSHandleLargeFiles(theApp->glob_prefs->GetCatPath(category))) {
230 AddLogLineM(true, _("Filesystem for Incoming directory cannot handle large files."));
231 return;
235 CPartFile* newfile = NULL;
236 try {
237 newfile = new CPartFile(toadd);
238 } catch (const CInvalidPacket& WXUNUSED(e)) {
239 AddDebugLogLineM(true, logDownloadQueue, wxT("Search-result contained invalid tags, could not add"));
242 if ( newfile && newfile->GetStatus() != PS_ERROR ) {
243 AddDownload( newfile, thePrefs::AddNewFilesPaused(), category );
244 // Add any possible sources
245 if (toadd->GetClientID() && toadd->GetClientPort()) {
246 CMemFile sources(1+4+2);
247 sources.WriteUInt8(1);
248 sources.WriteUInt32(toadd->GetClientID());
249 sources.WriteUInt16(toadd->GetClientPort());
250 sources.Reset();
251 newfile->AddSources(sources, toadd->GetClientServerIP(), toadd->GetClientServerPort(), SF_SEARCH_RESULT, false);
253 for (std::list<CSearchFile::ClientStruct>::const_iterator it = toadd->GetClients().begin(); it != toadd->GetClients().end(); ++it) {
254 CMemFile sources(1+4+2);
255 sources.WriteUInt8(1);
256 sources.WriteUInt32(it->m_ip);
257 sources.WriteUInt16(it->m_port);
258 sources.Reset();
259 newfile->AddSources(sources, it->m_serverIP, it->m_serverPort, SF_SEARCH_RESULT, false);
261 } else {
262 delete newfile;
267 struct SFindBestPF
269 void operator()(CPartFile* file) {
270 // Check if we should filter out other categories
271 if ((m_category != -1) && (file->GetCategory() != m_category)) {
272 return;
273 } else if (file->GetStatus() != PS_PAUSED) {
274 return;
277 if (!m_result || (file->GetDownPriority() > m_result->GetDownPriority())) {
278 m_result = file;
282 //! The category to look for, or -1 if any category is good
283 int m_category;
284 //! If any acceptable files are found, this variable store their pointer
285 CPartFile* m_result;
289 void CDownloadQueue::StartNextFile(CPartFile* oldfile)
291 if ( thePrefs::StartNextFile() ) {
292 SFindBestPF visitor = { -1, NULL };
295 wxMutexLocker lock(m_mutex);
297 if (thePrefs::StartNextFileSame()) {
298 // Get a download in the same category
299 visitor.m_category = oldfile->GetCategory();
301 visitor = std::for_each(m_filelist.begin(), m_filelist.end(), visitor);
304 if (visitor.m_result == NULL) {
305 // Get a download, regardless of category
306 visitor.m_category = -1;
308 visitor = std::for_each(m_filelist.begin(), m_filelist.end(), visitor);
312 if (visitor.m_result) {
313 visitor.m_result->ResumeFile();
319 void CDownloadQueue::AddDownload(CPartFile* file, bool paused, uint8 category)
321 wxCHECK_RET(!IsFileExisting(file->GetFileHash()), wxT("Adding duplicate part-file"));
323 if (file->GetStatus(true) == PS_ALLOCATING) {
324 file->PauseFile();
325 } else if (paused && GetFileCount()) {
326 file->StopFile();
330 wxMutexLocker lock(m_mutex);
331 m_filelist.push_back( file );
332 DoSortByPriority();
335 NotifyObservers( EventType( EventType::INSERTED, file ) );
337 file->SetCategory(category);
338 Notify_DownloadCtrlAddFile( file );
339 AddLogLineM(true, CFormat(_("Downloading %s")) % file->GetFileName() );
343 bool CDownloadQueue::IsFileExisting( const CMD4Hash& fileid ) const
345 if (CKnownFile* file = theApp->sharedfiles->GetFileByID(fileid)) {
346 if (file->IsPartFile()) {
347 AddLogLineM(true, CFormat( _("You are already trying to download the file '%s'") ) % file->GetFileName());
348 } else {
349 // Check if the file exists, since otherwise the user is forced to
350 // manually reload the shares to download a file again.
351 CPath fullpath = file->GetFilePath().JoinPaths(file->GetFileName());
352 if (!fullpath.FileExists()) {
353 // The file is no longer available, unshare it
354 theApp->sharedfiles->RemoveFile(file);
356 return false;
359 AddLogLineM(true, CFormat( _("You already have the file '%s'") ) % file->GetFileName());
362 return true;
363 } else if ((file = GetFileByID(fileid))) {
364 AddLogLineM(true, CFormat( _("You are already trying to download the file %s") ) % file->GetFileName());
365 return true;
368 return false;
372 void CDownloadQueue::Process()
374 // send src requests to local server
375 ProcessLocalRequests();
378 wxMutexLocker lock(m_mutex);
380 uint32 downspeed = 0;
381 if (thePrefs::GetMaxDownload() != UNLIMITED && m_datarate > 1500) {
382 downspeed = (((uint32)thePrefs::GetMaxDownload())*1024*100)/(m_datarate+1);
383 if (downspeed < 50) {
384 downspeed = 50;
385 } else if (downspeed > 200) {
386 downspeed = 200;
390 m_datarate = 0;
391 m_udcounter++;
392 uint32 cur_datarate = 0;
393 uint32 cur_udcounter = m_udcounter;
395 for ( uint16 i = 0; i < m_filelist.size(); i++ ) {
396 CPartFile* file = m_filelist[i];
398 CMutexUnlocker unlocker(m_mutex);
400 if ( file->GetStatus() == PS_READY || file->GetStatus() == PS_EMPTY ){
401 cur_datarate += file->Process( downspeed, cur_udcounter );
402 } else {
403 //This will make sure we don't keep old sources to paused and stoped files..
404 file->StopPausedFile();
408 m_datarate += cur_datarate;
411 if (m_udcounter == 5) {
412 if (theApp->serverconnect->IsUDPSocketAvailable()) {
413 if( (::GetTickCount() - m_lastudpstattime) > UDPSERVERSTATTIME) {
414 m_lastudpstattime = ::GetTickCount();
416 CMutexUnlocker unlocker(m_mutex);
417 theApp->serverlist->ServerStats();
422 if (m_udcounter == 10) {
423 m_udcounter = 0;
424 if (theApp->serverconnect->IsUDPSocketAvailable()) {
425 if ( (::GetTickCount() - m_lastudpsearchtime) > UDPSERVERREASKTIME) {
426 SendNextUDPPacket();
431 if ( (::GetTickCount() - m_lastsorttime) > 10000 ) {
434 DoSortByPriority();
436 // Check if any paused files can be resumed
438 CheckDiskspace(thePrefs::GetTempDir());
442 // Check for new links once per second.
443 if ((::GetTickCount() - m_nLastED2KLinkCheck) >= 1000) {
444 AddLinksFromFile();
445 m_nLastED2KLinkCheck = ::GetTickCount();
450 CPartFile* CDownloadQueue::GetFileByID(const CMD4Hash& filehash) const
452 wxMutexLocker lock( m_mutex );
454 for ( uint16 i = 0; i < m_filelist.size(); ++i ) {
455 if ( filehash == m_filelist[i]->GetFileHash()) {
456 return m_filelist[ i ];
460 return NULL;
464 CPartFile* CDownloadQueue::GetFileByIndex(unsigned int index) const
466 wxMutexLocker lock( m_mutex );
468 if ( index < m_filelist.size() ) {
469 return m_filelist[ index ];
472 wxASSERT( false );
473 return NULL;
477 bool CDownloadQueue::IsPartFile(const CKnownFile* file) const
479 wxMutexLocker lock(m_mutex);
481 for (uint16 i = 0; i < m_filelist.size(); ++i) {
482 if (file == m_filelist[i]) {
483 return true;
487 return false;
491 void CDownloadQueue::OnConnectionState(bool bConnected)
493 wxMutexLocker lock(m_mutex);
495 for (uint16 i = 0; i < m_filelist.size(); ++i) {
496 if ( m_filelist[i]->GetStatus() == PS_READY ||
497 m_filelist[i]->GetStatus() == PS_EMPTY) {
498 m_filelist[i]->SetActive(bConnected);
504 void CDownloadQueue::CheckAndAddSource(CPartFile* sender, CUpDownClient* source)
506 // if we block loopbacks at this point it should prevent us from connecting to ourself
507 if ( source->HasValidHash() ) {
508 if ( source->GetUserHash() == thePrefs::GetUserHash() ) {
509 AddDebugLogLineM( false, logDownloadQueue, wxT("Tried to add source with matching hash to your own.") );
510 source->Safe_Delete();
511 return;
515 if (sender->IsStopped()) {
516 source->Safe_Delete();
517 return;
520 // Filter sources which are known to be dead/useless
521 if ( theApp->clientlist->IsDeadSource( source ) || sender->IsDeadSource(source) ) {
522 source->Safe_Delete();
523 return;
526 // Filter sources which are incompatible with our encryption setting (one requires it, and the other one doesn't supports it)
527 if ( (source->RequiresCryptLayer() && (!thePrefs::IsClientCryptLayerSupported() || !source->HasValidHash())) || (thePrefs::IsClientCryptLayerRequired() && (!source->SupportsCryptLayer() || !source->HasValidHash()))) {
528 source->Safe_Delete();
529 return;
532 // Find all clients with the same hash
533 if ( source->HasValidHash() ) {
534 CClientList::SourceList found = theApp->clientlist->GetClientsByHash( source->GetUserHash() );
536 CClientList::SourceList::iterator it = found.begin();
537 for ( ; it != found.end(); it++ ) {
538 CKnownFile* file = (*it)->GetRequestFile();
540 // Only check files on the download-queue
541 if ( file ) {
542 // Is the found source queued for something else?
543 if ( file != sender ) {
544 // Try to add a request for the other file
545 if ( (*it)->AddRequestForAnotherFile(sender)) {
546 // Add it to downloadlistctrl
547 Notify_DownloadCtrlAddSource(sender, *it, A4AF_SOURCE);
551 source->Safe_Delete();
552 return;
559 // Our new source is real new but maybe it is already uploading to us?
560 // If yes the known client will be attached to the var "source" and the old
561 // source-client will be deleted. However, if the request file of the known
562 // source is NULL, then we have to treat it almost like a new source and if
563 // it isn't NULL and not "sender", then we shouldn't move it, but rather add
564 // a request for the new file.
565 ESourceFrom nSourceFrom = source->GetSourceFrom();
566 if ( theApp->clientlist->AttachToAlreadyKnown(&source, 0) ) {
567 // Already queued for another file?
568 if ( source->GetRequestFile() ) {
569 // If we're already queued for the right file, then there's nothing to do
570 if ( sender != source->GetRequestFile() ) {
571 // Add the new file to the request list
572 source->AddRequestForAnotherFile( sender );
574 } else {
575 // Source was known, but reqfile NULL.
576 source->SetRequestFile( sender );
577 if (source->GetSourceFrom() != nSourceFrom) {
578 if (source->GetSourceFrom() != SF_NONE) {
579 theStats::RemoveSourceOrigin(source->GetSourceFrom());
580 theStats::RemoveFoundSource();
582 source->SetSourceFrom(nSourceFrom);
584 sender->AddSource( source );
585 if ( source->GetFileRating() || !source->GetFileComment().IsEmpty() ) {
586 sender->UpdateFileRatingCommentAvail();
589 Notify_DownloadCtrlAddSource(sender, source, UNAVAILABLE_SOURCE);
591 } else {
592 // Unknown client, add it to the clients list
593 source->SetRequestFile( sender );
595 theApp->clientlist->AddClient(source);
597 sender->AddSource( source );
598 if ( source->GetFileRating() || !source->GetFileComment().IsEmpty() ) {
599 sender->UpdateFileRatingCommentAvail();
602 Notify_DownloadCtrlAddSource(sender, source, UNAVAILABLE_SOURCE);
607 void CDownloadQueue::CheckAndAddKnownSource(CPartFile* sender,CUpDownClient* source)
609 // Kad reviewed
611 if (sender->IsStopped()) {
612 return;
615 // Filter sources which are known to be dead/useless
616 if ( sender->IsDeadSource(source) ) {
617 return;
620 // "Filter LAN IPs" -- this may be needed here in case we are connected to the internet and are also connected
621 // to a LAN and some client from within the LAN connected to us. Though this situation may be supported in future
622 // by adding that client to the source list and filtering that client's LAN IP when sending sources to
623 // a client within the internet.
625 // "IPfilter" is not needed here, because that "known" client was already IPfiltered when receiving OP_HELLO.
626 if (!source->HasLowID()) {
627 uint32 nClientIP = wxUINT32_SWAP_ALWAYS(source->GetUserIDHybrid());
628 if (!IsGoodIP(nClientIP, thePrefs::FilterLanIPs())) { // check for 0-IP, localhost and LAN addresses
629 AddDebugLogLineM(false, logIPFilter, wxT("Ignored already known source with IP=%s") + Uint32toStringIP(nClientIP));
630 return;
634 // Filter sources which are incompatible with our encryption setting (one requires it, and the other one doesn't supports it)
635 if ( (source->RequiresCryptLayer() && (!thePrefs::IsClientCryptLayerSupported() || !source->HasValidHash())) || (thePrefs::IsClientCryptLayerRequired() && (!source->SupportsCryptLayer() || !source->HasValidHash())))
637 source->Safe_Delete();
638 return;
641 CPartFile* file = source->GetRequestFile();
643 // Check if the file is already queued for something else
644 if ( file ) {
645 if ( file != sender ) {
646 if ( source->AddRequestForAnotherFile( sender ) ) {
647 Notify_DownloadCtrlAddSource( sender, source, A4AF_SOURCE );
650 } else {
651 source->SetRequestFile( sender );
653 if ( source->GetFileRating() || !source->GetFileComment().IsEmpty() ) {
654 sender->UpdateFileRatingCommentAvail();
657 source->SetSourceFrom(SF_PASSIVE);
658 sender->AddSource( source );
659 Notify_DownloadCtrlAddSource( sender, source, UNAVAILABLE_SOURCE);
664 bool CDownloadQueue::RemoveSource(CUpDownClient* toremove, bool WXUNUSED(updatewindow), bool bDoStatsUpdate)
666 bool removed = false;
667 toremove->DeleteAllFileRequests();
669 for ( uint16 i = 0; i < GetFileCount(); i++ ) {
670 CPartFile* cur_file = GetFileByIndex( i );
672 // Remove from source-list
673 if ( cur_file->DelSource( toremove ) ) {
674 cur_file->RemoveDownloadingSource(toremove);
675 removed = true;
676 if ( bDoStatsUpdate ) {
677 cur_file->UpdatePartsInfo();
681 // Remove from A4AF-list
682 cur_file->RemoveA4AFSource( toremove );
686 if ( !toremove->GetFileComment().IsEmpty() || toremove->GetFileRating()>0) {
687 toremove->GetRequestFile()->UpdateFileRatingCommentAvail();
690 toremove->SetRequestFile( NULL );
691 toremove->SetDownloadState(DS_NONE);
693 // Remove from downloadlist widget
694 Notify_DownloadCtrlRemoveSource(toremove, (CPartFile*)NULL);
695 toremove->ResetFileStatusInfo();
697 return removed;
701 void CDownloadQueue::RemoveFile(CPartFile* file)
703 RemoveLocalServerRequest( file );
705 NotifyObservers( EventType( EventType::REMOVED, file ) );
707 wxMutexLocker lock( m_mutex );
709 EraseValue( m_filelist, file );
713 CUpDownClient* CDownloadQueue::GetDownloadClientByIP_UDP(uint32 dwIP, uint16 nUDPPort) const
715 wxMutexLocker lock( m_mutex );
717 for ( unsigned int i = 0; i < m_filelist.size(); i++ ) {
718 const CPartFile::SourceSet& set = m_filelist[i]->GetSourceList();
720 for ( CPartFile::SourceSet::const_iterator it = set.begin(); it != set.end(); it++ ) {
721 if ( (*it)->GetIP() == dwIP && (*it)->GetUDPPort() == nUDPPort ) {
722 return *it;
726 return NULL;
731 * Checks if the specified server is the one we are connected to.
733 bool IsConnectedServer(const CServer* server)
735 if (server && theApp->serverconnect->GetCurrentServer()) {
736 wxString srvAddr = theApp->serverconnect->GetCurrentServer()->GetAddress();
737 uint16 srvPort = theApp->serverconnect->GetCurrentServer()->GetPort();
739 return server->GetAddress() == srvAddr && server->GetPort() == srvPort;
742 return false;
746 bool CDownloadQueue::SendNextUDPPacket()
748 if ( m_filelist.empty() || !theApp->serverconnect->IsUDPSocketAvailable() || !theApp->IsConnectedED2K()) {
749 return false;
752 // Start monitoring the server and the files list
753 if ( !m_queueServers.IsActive() ) {
754 AddObserver( &m_queueFiles );
756 theApp->serverlist->AddObserver( &m_queueServers );
760 bool packetSent = false;
761 while ( !packetSent ) {
762 // Get max files ids per packet for current server
763 int filesAllowed = GetMaxFilesPerUDPServerPacket();
765 if (filesAllowed < 1 || !m_udpserver || IsConnectedServer(m_udpserver)) {
766 // Select the next server to ask, must not be the connected server
767 do {
768 m_udpserver = m_queueServers.GetNext();
769 } while (IsConnectedServer(m_udpserver));
771 m_cRequestsSentToServer = 0;
772 filesAllowed = GetMaxFilesPerUDPServerPacket();
776 // Check if we have asked all servers, in which case we are done
777 if (m_udpserver == NULL) {
778 DoStopUDPRequests();
780 return false;
783 // Memoryfile containing the hash of every file to request
784 // 28bytes allocation because 16b + 4b + 8b is the worse case scenario.
785 CMemFile hashlist( 28 );
787 CPartFile* file = m_queueFiles.GetNext();
789 while ( file && filesAllowed ) {
790 uint8 status = file->GetStatus();
792 if ( ( status == PS_READY || status == PS_EMPTY ) && file->GetSourceCount() < thePrefs::GetMaxSourcePerFileUDP() ) {
793 if (file->IsLargeFile() && !m_udpserver->SupportsLargeFilesUDP()) {
794 AddDebugLogLineM(false, logDownloadQueue, wxT("UDP Request for sources on a large file ignored: server doesn't support it"));
795 } else {
796 ++m_cRequestsSentToServer;
797 hashlist.WriteHash( file->GetFileHash() );
798 // See the notes on TCP packet
799 if ( m_udpserver->GetUDPFlags() & SRV_UDPFLG_EXT_GETSOURCES2 ) {
800 if (file->IsLargeFile()) {
801 wxASSERT(m_udpserver->SupportsLargeFilesUDP());
802 hashlist.WriteUInt32( 0 );
803 hashlist.WriteUInt64( file->GetFileSize() );
804 } else {
805 hashlist.WriteUInt32( file->GetFileSize() );
808 --filesAllowed;
812 // Avoid skipping a file if we can't send any more currently
813 if ( filesAllowed ) {
814 file = m_queueFiles.GetNext();
818 // See if we have anything to send
819 if ( hashlist.GetLength() ) {
820 packetSent = SendGlobGetSourcesUDPPacket(hashlist);
823 // Check if we've covered every file
824 if ( file == NULL ) {
825 // Reset the list of asked files so that the loop will start over
826 m_queueFiles.Reset();
828 // Unset the server so that the next server will be used
829 m_udpserver = NULL;
833 return true;
837 void CDownloadQueue::StopUDPRequests()
839 wxMutexLocker lock( m_mutex );
841 DoStopUDPRequests();
845 void CDownloadQueue::DoStopUDPRequests()
847 // No need to observe when we wont be using the results
848 theApp->serverlist->RemoveObserver( &m_queueServers );
849 RemoveObserver( &m_queueFiles );
851 m_udpserver = 0;
852 m_lastudpsearchtime = ::GetTickCount();
856 // Comparison function needed by sort. Returns true if file1 preceeds file2
857 bool ComparePartFiles(const CPartFile* file1, const CPartFile* file2) {
858 if (file1->GetDownPriority() != file2->GetDownPriority()) {
859 // To place high-priority files before low priority files we have to
860 // invert this test, since PR_LOW is lower than PR_HIGH, and since
861 // placing a PR_LOW file before a PR_HIGH file would mean that
862 // the PR_LOW file gets sources before the PR_HIGH file ...
863 return (file1->GetDownPriority() > file2->GetDownPriority());
864 } else {
865 int sourcesA = file1->GetSourceCount();
866 int sourcesB = file2->GetSourceCount();
868 int notSourcesA = file1->GetNotCurrentSourcesCount();
869 int notSourcesB = file2->GetNotCurrentSourcesCount();
871 int cmp = CmpAny( sourcesA - notSourcesA, sourcesB - notSourcesB );
873 if ( cmp == 0 ) {
874 cmp = CmpAny( notSourcesA, notSourcesB );
877 return cmp < 0;
882 void CDownloadQueue::DoSortByPriority()
884 m_lastsorttime = ::GetTickCount();
885 sort( m_filelist.begin(), m_filelist.end(), ComparePartFiles );
889 void CDownloadQueue::ResetLocalServerRequests()
891 wxMutexLocker lock( m_mutex );
893 m_dwNextTCPSrcReq = 0;
894 m_localServerReqQueue.clear();
896 for ( uint16 i = 0; i < m_filelist.size(); i++ ) {
897 m_filelist[i]->SetLocalSrcRequestQueued(false);
902 void CDownloadQueue::RemoveLocalServerRequest( CPartFile* file )
904 wxMutexLocker lock( m_mutex );
906 EraseValue( m_localServerReqQueue, file );
908 file->SetLocalSrcRequestQueued(false);
912 void CDownloadQueue::ProcessLocalRequests()
914 wxMutexLocker lock( m_mutex );
916 bool bServerSupportsLargeFiles = theApp->serverconnect
917 && theApp->serverconnect->GetCurrentServer()
918 && theApp->serverconnect->GetCurrentServer()->SupportsLargeFilesTCP();
920 if ( (!m_localServerReqQueue.empty()) && (m_dwNextTCPSrcReq < ::GetTickCount()) ) {
921 CMemFile dataTcpFrame(22);
922 const int iMaxFilesPerTcpFrame = 15;
923 int iFiles = 0;
924 while (!m_localServerReqQueue.empty() && iFiles < iMaxFilesPerTcpFrame) {
925 // find the file with the longest waitingtime
926 uint32 dwBestWaitTime = 0xFFFFFFFF;
928 std::list<CPartFile*>::iterator posNextRequest = m_localServerReqQueue.end();
929 std::list<CPartFile*>::iterator it = m_localServerReqQueue.begin();
930 while( it != m_localServerReqQueue.end() ) {
931 CPartFile* cur_file = (*it);
932 if (cur_file->GetStatus() == PS_READY || cur_file->GetStatus() == PS_EMPTY) {
933 uint8 nPriority = cur_file->GetDownPriority();
934 if (nPriority > PR_HIGH) {
935 wxASSERT(0);
936 nPriority = PR_HIGH;
939 if (cur_file->GetLastSearchTime() + (PR_HIGH-nPriority) < dwBestWaitTime ){
940 dwBestWaitTime = cur_file->GetLastSearchTime() + (PR_HIGH - nPriority);
941 posNextRequest = it;
944 it++;
945 } else {
946 it = m_localServerReqQueue.erase(it);
947 cur_file->SetLocalSrcRequestQueued(false);
948 AddDebugLogLineM( false, logDownloadQueue,
949 CFormat(wxT("Local server source request for file '%s' not sent because of status '%s'"))
950 % cur_file->GetFileName() % cur_file->getPartfileStatus());
954 if (posNextRequest != m_localServerReqQueue.end()) {
955 CPartFile* cur_file = (*posNextRequest);
956 cur_file->SetLocalSrcRequestQueued(false);
957 cur_file->SetLastSearchTime(::GetTickCount());
958 m_localServerReqQueue.erase(posNextRequest);
959 iFiles++;
961 if (!bServerSupportsLargeFiles && cur_file->IsLargeFile()) {
962 AddDebugLogLineM(false, logDownloadQueue, wxT("TCP Request for sources on a large file ignored: server doesn't support it"));
963 } else {
964 AddDebugLogLineM(false, logDownloadQueue,
965 CFormat(wxT("Creating local sources request packet for '%s'")) % cur_file->GetFileName());
966 // create request packet
967 CMemFile data(16 + (cur_file->IsLargeFile() ? 8 : 4));
968 data.WriteHash(cur_file->GetFileHash());
969 // Kry - lugdunum extended protocol on 17.3 to handle filesize properly.
970 // There is no need to check anything, old server ignore the extra 4 bytes.
971 // As of 17.9, servers accept a 0 32-bits size and then a 64bits size
972 if (cur_file->IsLargeFile()) {
973 wxASSERT(bServerSupportsLargeFiles);
974 data.WriteUInt32(0);
975 data.WriteUInt64(cur_file->GetFileSize());
976 } else {
977 data.WriteUInt32(cur_file->GetFileSize());
979 uint8 byOpcode = 0;
980 if (thePrefs::IsClientCryptLayerSupported() && theApp->serverconnect->GetCurrentServer() != NULL && theApp->serverconnect->GetCurrentServer()->SupportsGetSourcesObfuscation()) {
981 byOpcode = OP_GETSOURCES_OBFU;
982 } else {
983 byOpcode = OP_GETSOURCES;
985 CPacket packet(data, OP_EDONKEYPROT, byOpcode);
986 dataTcpFrame.Write(packet.GetPacket(), packet.GetRealPacketSize());
991 int iSize = dataTcpFrame.GetLength();
992 if (iSize > 0) {
993 // create one 'packet' which contains all buffered OP_GETSOURCES ED2K packets to be sent with one TCP frame
994 // server credits: (16+4)*regularfiles + (16+4+8)*largefiles +1
995 CPacket* packet = new CPacket(new byte[iSize], dataTcpFrame.GetLength(), true, false);
996 dataTcpFrame.Seek(0, wxFromStart);
997 dataTcpFrame.Read(packet->GetPacket(), iSize);
998 uint32 size = packet->GetPacketSize();
999 theApp->serverconnect->SendPacket(packet, true); // Deletes `packet'.
1000 AddDebugLogLineM(false, logDownloadQueue, wxT("Sent local sources request packet."));
1001 theStats::AddUpOverheadServer(size);
1004 // next TCP frame with up to 15 source requests is allowed to be sent in..
1005 m_dwNextTCPSrcReq = ::GetTickCount() + SEC2MS(iMaxFilesPerTcpFrame*(16+4));
1011 void CDownloadQueue::SendLocalSrcRequest(CPartFile* sender)
1013 wxMutexLocker lock( m_mutex );
1015 m_localServerReqQueue.push_back(sender);
1019 void CDownloadQueue::AddLinksFromFile()
1021 const wxString fullPath = theApp->ConfigDir + wxT("ED2KLinks");
1022 if (!wxFile::Exists(fullPath)) {
1023 return;
1026 // Attempt to lock the ED2KLinks file.
1027 CFileLock lock((const char*)unicode2char(fullPath));
1029 wxTextFile file(fullPath);
1030 if ( file.Open() ) {
1031 for ( unsigned int i = 0; i < file.GetLineCount(); i++ ) {
1032 wxString line = file.GetLine( i ).Strip( wxString::both );
1034 if ( !line.IsEmpty() ) {
1035 // Special case! used by a secondary running mule to raise this one.
1036 if ( line == wxT("RAISE_DIALOG") ) {
1037 Notify_ShowGUI();
1038 continue;
1041 AddLink( line );
1045 file.Close();
1046 } else {
1047 printf("Failed to open ED2KLinks file.\n");
1050 // Delete the file.
1051 wxRemoveFile(theApp->ConfigDir + wxT("ED2KLinks"));
1055 void CDownloadQueue::ResetCatParts(uint8 cat)
1057 for ( uint16 i = 0; i < GetFileCount(); i++ ) {
1058 CPartFile* file = GetFileByIndex( i );
1060 if ( file->GetCategory() == cat ) {
1061 // Reset the category
1062 file->SetCategory( 0 );
1063 } else if ( file->GetCategory() > cat ) {
1064 // Set to the new position of the original category
1065 file->SetCategory( file->GetCategory() - 1 );
1071 void CDownloadQueue::SetCatPrio(uint8 cat, uint8 newprio)
1073 for ( uint16 i = 0; i < GetFileCount(); i++ ) {
1074 CPartFile* file = GetFileByIndex( i );
1076 if ( !cat || file->GetCategory() == cat ) {
1077 if ( newprio == PR_AUTO ) {
1078 file->SetAutoDownPriority(true);
1079 } else {
1080 file->SetAutoDownPriority(false);
1081 file->SetDownPriority(newprio);
1088 void CDownloadQueue::SetCatStatus(uint8 cat, int newstatus)
1090 std::list<CPartFile*> files;
1093 wxMutexLocker lock(m_mutex);
1095 for ( uint16 i = 0; i < m_filelist.size(); i++ ) {
1096 if ( m_filelist[i]->CheckShowItemInGivenCat(cat) ) {
1097 files.push_back( m_filelist[i] );
1102 std::list<CPartFile*>::iterator it = files.begin();
1104 for ( ; it != files.end(); it++ ) {
1105 switch ( newstatus ) {
1106 case MP_CANCEL: (*it)->Delete(); break;
1107 case MP_PAUSE: (*it)->PauseFile(); break;
1108 case MP_STOP: (*it)->StopFile(); break;
1109 case MP_RESUME: (*it)->ResumeFile(); break;
1115 uint16 CDownloadQueue::GetDownloadingFileCount() const
1117 wxMutexLocker lock( m_mutex );
1119 uint16 count = 0;
1120 for ( uint16 i = 0; i < m_filelist.size(); i++ ) {
1121 uint8 status = m_filelist[i]->GetStatus();
1122 if ( status == PS_READY || status == PS_EMPTY ) {
1123 count++;
1127 return count;
1131 uint16 CDownloadQueue::GetPausedFileCount() const
1133 wxMutexLocker lock( m_mutex );
1135 uint16 count = 0;
1136 for ( uint16 i = 0; i < m_filelist.size(); i++ ) {
1137 if ( m_filelist[i]->GetStatus() == PS_PAUSED ) {
1138 count++;
1142 return count;
1146 void CDownloadQueue::CheckDiskspace( const CPath& path )
1148 if ( ::GetTickCount() - m_lastDiskCheck < DISKSPACERECHECKTIME ) {
1149 return;
1152 m_lastDiskCheck = ::GetTickCount();
1154 uint64 min = 0;
1155 // Check if the user has set an explicit limit
1156 if ( thePrefs::IsCheckDiskspaceEnabled() ) {
1157 min = thePrefs::GetMinFreeDiskSpace();
1160 // The very least acceptable diskspace is a single PART
1161 if ( min < PARTSIZE ) {
1162 min = PARTSIZE;
1165 uint64 free = CPath::GetFreeSpaceAt(path);
1166 if (free == static_cast<uint64>(wxInvalidOffset)) {
1167 return;
1168 } else if (free < min) {
1169 CUserEvents::ProcessEvent(
1170 CUserEvents::OutOfDiskSpace,
1171 wxT("Temporary partition"));
1174 for (unsigned int i = 0; i < m_filelist.size(); ++i) {
1175 CPartFile* file = m_filelist[i];
1177 switch ( file->GetStatus() ) {
1178 case PS_ERROR:
1179 case PS_COMPLETING:
1180 case PS_COMPLETE:
1181 continue;
1184 if ( free >= min && file->GetInsufficient() ) {
1185 // We'll try to resume files if there is enough free space
1186 if ( free - file->GetNeededSpace() > min ) {
1187 file->ResumeFile();
1189 } else if ( free < min && !file->IsPaused() ) {
1190 // No space left, stop the files.
1191 file->PauseFile( true );
1197 int CDownloadQueue::GetMaxFilesPerUDPServerPacket() const
1199 if ( m_udpserver ) {
1200 if ( m_udpserver->GetUDPFlags() & SRV_UDPFLG_EXT_GETSOURCES ) {
1201 // get max. file ids per packet
1202 if ( m_cRequestsSentToServer < MAX_REQUESTS_PER_SERVER ) {
1203 return std::min(
1204 MAX_FILES_PER_UDP_PACKET,
1205 MAX_REQUESTS_PER_SERVER - m_cRequestsSentToServer
1208 } else if ( m_cRequestsSentToServer < MAX_REQUESTS_PER_SERVER ) {
1209 return 1;
1213 return 0;
1217 bool CDownloadQueue::SendGlobGetSourcesUDPPacket(CMemFile& data)
1219 if (!m_udpserver) {
1220 return false;
1223 CPacket packet(data, OP_EDONKEYPROT, ((m_udpserver->GetUDPFlags() & SRV_UDPFLG_EXT_GETSOURCES2) ? OP_GLOBGETSOURCES2 : OP_GLOBGETSOURCES));
1225 theStats::AddUpOverheadServer(packet.GetPacketSize());
1226 theApp->serverconnect->SendUDPPacket(&packet,m_udpserver,false);
1228 return true;
1232 void CDownloadQueue::AddToResolve(const CMD4Hash& fileid, const wxString& pszHostname, uint16 port, const wxString& hash, uint8 cryptoptions)
1234 // double checking
1235 if ( !GetFileByID(fileid) ) {
1236 return;
1239 wxMutexLocker lock( m_mutex );
1241 Hostname_Entry entry = { fileid, pszHostname, port, hash, cryptoptions };
1242 m_toresolve.push_front(entry);
1244 // Check if there are other DNS lookups on queue
1245 if (m_toresolve.size() == 1) {
1246 // Check if it is a simple dot address
1247 uint32 ip = StringIPtoUint32(pszHostname);
1249 if (ip) {
1250 OnHostnameResolved(ip);
1251 } else {
1252 CAsyncDNS* dns = new CAsyncDNS(pszHostname, DNS_SOURCE, theApp);
1254 if ((dns->Create() != wxTHREAD_NO_ERROR) || (dns->Run() != wxTHREAD_NO_ERROR)) {
1255 dns->Delete();
1256 m_toresolve.pop_front();
1263 void CDownloadQueue::OnHostnameResolved(uint32 ip)
1265 wxMutexLocker lock( m_mutex );
1267 wxASSERT( m_toresolve.size() );
1269 Hostname_Entry resolved = m_toresolve.front();
1270 m_toresolve.pop_front();
1272 if ( ip ) {
1273 CPartFile* file = GetFileByID( resolved.fileid );
1274 if ( file ) {
1275 CMemFile sources(1+4+2);
1276 sources.WriteUInt8(1); // No. Sources
1277 sources.WriteUInt32(ip);
1278 sources.WriteUInt16(resolved.port);
1279 sources.WriteUInt8(resolved.cryptoptions);
1280 if (resolved.cryptoptions & 0x80) {
1281 wxASSERT(!resolved.hash.IsEmpty());
1282 CMD4Hash sourcehash;
1283 sourcehash.Decode(resolved.hash);
1284 sources.WriteHash(sourcehash);
1286 sources.Seek(0,wxFromStart);
1288 file->AddSources(sources, 0, 0, SF_LINK, true);
1292 while (m_toresolve.size()) {
1293 Hostname_Entry entry = m_toresolve.front();
1295 // Check if it is a simple dot address
1296 uint32 tmpIP = StringIPtoUint32(entry.strHostname);
1298 if (tmpIP) {
1299 OnHostnameResolved(tmpIP);
1300 } else {
1301 CAsyncDNS* dns = new CAsyncDNS(entry.strHostname, DNS_SOURCE, theApp);
1303 if ((dns->Create() != wxTHREAD_NO_ERROR) || (dns->Run() != wxTHREAD_NO_ERROR)) {
1304 dns->Delete();
1305 m_toresolve.pop_front();
1306 } else {
1307 break;
1314 bool CDownloadQueue::AddLink( const wxString& link, int category )
1316 wxString uri(link);
1318 if (link.compare(0, 7, wxT("magnet:")) == 0) {
1319 uri = CMagnetED2KConverter(link);
1320 if (uri.empty()) {
1321 AddLogLineM(true, CFormat(_("Cannot convert magnet link to eD2k: %s")) % link);
1322 return false;
1326 if (uri.compare(0, 7, wxT("ed2k://")) == 0) {
1327 return AddED2KLink(uri, category);
1328 } else {
1329 AddLogLineM(true, CFormat(_("Unknown protocol of link: %s")) % link);
1330 return false;
1335 bool CDownloadQueue::AddED2KLink( const wxString& link, int category )
1337 wxASSERT( !link.IsEmpty() );
1338 wxString URI = link;
1340 // Need the links to end with /, otherwise CreateLinkFromUrl crashes us.
1341 if ( URI.Last() != wxT('/') ) {
1342 URI += wxT("/");
1345 try {
1346 CScopedPtr<CED2KLink> uri(CED2KLink::CreateLinkFromUrl(URI));
1348 return AddED2KLink( uri.get(), category );
1349 } catch ( const wxString& err ) {
1350 AddLogLineM( true, CFormat( _("Invalid eD2k link! ERROR: %s")) % err);
1353 return false;
1357 bool CDownloadQueue::AddED2KLink( const CED2KLink* link, int category )
1359 switch ( link->GetKind() ) {
1360 case CED2KLink::kFile:
1361 return AddED2KLink( dynamic_cast<const CED2KFileLink*>( link ), category );
1363 case CED2KLink::kServer:
1364 return AddED2KLink( dynamic_cast<const CED2KServerLink*>( link ) );
1366 case CED2KLink::kServerList:
1367 return AddED2KLink( dynamic_cast<const CED2KServerListLink*>( link ) );
1369 default:
1370 return false;
1376 bool CDownloadQueue::AddED2KLink( const CED2KFileLink* link, int category )
1378 CPartFile* file = NULL;
1379 if (IsFileExisting(link->GetHashKey())) {
1380 // Must be a shared file if we are to add hashes or sources
1381 if ((file = GetFileByID(link->GetHashKey())) == NULL) {
1382 return false;
1384 } else {
1385 if (link->GetSize() > OLD_MAX_FILE_SIZE) {
1386 if (!PlatformSpecific::CanFSHandleLargeFiles(thePrefs::GetTempDir())) {
1387 AddLogLineM(true, _("Filesystem for Temp directory cannot handle large files."));
1388 return false;
1389 } else if (!PlatformSpecific::CanFSHandleLargeFiles(theApp->glob_prefs->GetCatPath(category))) {
1390 AddLogLineM(true, _("Filesystem for Incoming directory cannot handle large files."));
1391 return false;
1395 file = new CPartFile(link);
1397 if (file->GetStatus() == PS_ERROR) {
1398 delete file;
1399 return false;
1402 AddDownload(file, thePrefs::AddNewFilesPaused(), category);
1405 if (link->HasValidAICHHash()) {
1406 CAICHHashSet* hashset = file->GetAICHHashset();
1408 if (!hashset->HasValidMasterHash() || (hashset->GetMasterHash() != link->GetAICHHash())) {
1409 hashset->SetMasterHash(link->GetAICHHash(), AICH_VERIFIED);
1410 hashset->FreeHashSet();
1414 const CED2KFileLink::CED2KLinkSourceList& list = link->m_sources;
1415 CED2KFileLink::CED2KLinkSourceList::const_iterator it = list.begin();
1416 for (; it != list.end(); ++it) {
1417 AddToResolve(link->GetHashKey(), it->addr, it->port, it->hash, it->cryptoptions);
1420 return true;
1424 bool CDownloadQueue::AddED2KLink( const CED2KServerLink* link )
1426 CServer *server = new CServer( link->GetPort(), Uint32toStringIP( link->GetIP() ) );
1428 server->SetListName( Uint32toStringIP( link->GetIP() ) );
1430 theApp->serverlist->AddServer(server);
1432 Notify_ServerAdd(server);
1434 return true;
1438 bool CDownloadQueue::AddED2KLink( const CED2KServerListLink* link )
1440 theApp->serverlist->UpdateServerMetFromURL( link->GetAddress() );
1442 return true;
1446 void CDownloadQueue::ObserverAdded( ObserverType* o )
1448 CObservableQueue<CPartFile*>::ObserverAdded( o );
1450 EventType::ValueList list;
1453 wxMutexLocker lock(m_mutex);
1454 list.reserve( m_filelist.size() );
1455 list.insert( list.begin(), m_filelist.begin(), m_filelist.end() );
1458 NotifyObservers( EventType( EventType::INITIAL, &list ), o );
1461 void CDownloadQueue::KademliaSearchFile(uint32 searchID, const Kademlia::CUInt128* pcontactID, const Kademlia::CUInt128* pbuddyID, uint8 type, uint32 ip, uint16 tcp, uint16 udp, uint32 serverip, uint16 serverport, uint8 byCryptOptions)
1464 AddDebugLogLineM(false, logKadSearch, wxString::Format(wxT("Search result sources (type %i)"),type));
1466 //Safety measure to make sure we are looking for these sources
1467 CPartFile* temp = GetFileByKadFileSearchID(searchID);
1468 if( !temp ) {
1469 AddDebugLogLineM(false, logKadSearch, wxT("This is not the file we're looking for..."));
1470 return;
1473 //Do we need more sources?
1474 if(!(!temp->IsStopped() && thePrefs::GetMaxSourcePerFile() > temp->GetSourceCount())) {
1475 AddDebugLogLineM(false, logKadSearch, wxT("No more sources needed for this file"));
1476 return;
1479 uint32 ED2KID = wxUINT32_SWAP_ALWAYS(ip);
1481 if (theApp->ipfilter->IsFiltered(ED2KID)) {
1482 AddDebugLogLineM(false, logKadSearch, wxT("Source ip got filtered"));
1483 AddDebugLogLineM(false, logIPFilter, CFormat(wxT("IPfiltered source IP=%s received from Kademlia")) % Uint32toStringIP(ED2KID));
1484 return;
1487 if( (ip == Kademlia::CKademlia::GetIPAddress() || ED2KID == theApp->GetED2KID()) && tcp == thePrefs::GetPort()) {
1488 AddDebugLogLineM(false, logKadSearch, wxT("Trying to add myself as source, ignore"));
1489 return;
1492 CUpDownClient* ctemp = NULL;
1493 switch( type ) {
1494 case 4:
1495 case 1: {
1496 //NonFirewalled users
1497 if(!tcp) {
1498 AddDebugLogLineM(false, logKadSearch, CFormat(wxT("Ignored source (IP=%s) received from Kademlia, no tcp port received")) % Uint32toStringIP(ip));
1499 return;
1501 if (!IsGoodIP(ED2KID,thePrefs::FilterLanIPs())) {
1502 AddDebugLogLineM(false, logKadSearch, CFormat(wxT("%s got filtered")) % Uint32toStringIP(ED2KID));
1503 AddDebugLogLineM(false, logIPFilter, CFormat(wxT("Ignored source (IP=%s) received from Kademlia, filtered")) % Uint32toStringIP(ED2KID));
1504 return;
1506 ctemp = new CUpDownClient(tcp,ip,0,0,temp,false, true);
1507 ctemp->SetSourceFrom(SF_KADEMLIA);
1508 ctemp->SetServerIP(serverip);
1509 ctemp->SetServerPort(serverport);
1510 ctemp->SetKadPort(udp);
1511 byte cID[16];
1512 pcontactID->ToByteArray(cID);
1513 ctemp->SetUserHash(CMD4Hash(cID));
1514 break;
1516 case 2: {
1517 //Don't use this type... Some clients will process it wrong..
1518 break;
1520 case 5:
1521 case 3: {
1522 //This will be a firewaled client connected to Kad only.
1523 //We set the clientID to 1 as a Kad user only has 1 buddy.
1524 ctemp = new CUpDownClient(tcp,1,0,0,temp,false, true);
1525 //The only reason we set the real IP is for when we get a callback
1526 //from this firewalled source, the compare method will match them.
1527 ctemp->SetSourceFrom(SF_KADEMLIA);
1528 ctemp->SetKadPort(udp);
1529 byte cID[16];
1530 pcontactID->ToByteArray(cID);
1531 ctemp->SetUserHash(CMD4Hash(cID));
1532 pbuddyID->ToByteArray(cID);
1533 ctemp->SetBuddyID(cID);
1534 ctemp->SetBuddyIP(serverip);
1535 ctemp->SetBuddyPort(serverport);
1536 break;
1538 case 6: {
1539 // firewalled source which supports direct udp callback
1540 // if we are firewalled ourself, the source is useless to us
1541 if (theApp->IsFirewalled()) {
1542 break;
1545 if ((byCryptOptions & 0x08) == 0){
1546 AddDebugLogLineM(false, logKadSearch, CFormat(wxT("Received Kad source type 6 (direct callback) which has the direct callback flag not set (%s)")) % Uint32toStringIP(ED2KID));
1547 break;
1550 ctemp = new CUpDownClient(tcp, 1, 0, 0, temp, false, true);
1551 ctemp->SetSourceFrom(SF_KADEMLIA);
1552 ctemp->SetKadPort(udp);
1553 ctemp->SetIP(ED2KID); // need to set the Ip address, which cannot be used for TCP but for UDP
1554 byte cID[16];
1555 pcontactID->ToByteArray(cID);
1556 ctemp->SetUserHash(CMD4Hash(cID));
1557 pbuddyID->ToByteArray(cID);
1561 if (ctemp) {
1562 // add encryption settings
1563 ctemp->SetConnectOptions(byCryptOptions);
1565 AddDebugLogLineM(false, logKadSearch, CFormat(wxT("Happily adding a source (%s) type %d")) % Uint32_16toStringIP_Port(ED2KID, ctemp->GetUserPort()) % type);
1566 CheckAndAddSource(temp, ctemp);
1570 CPartFile* CDownloadQueue::GetFileByKadFileSearchID(uint32 id) const
1573 wxMutexLocker lock( m_mutex );
1575 for ( uint16 i = 0; i < m_filelist.size(); ++i ) {
1576 if ( id == m_filelist[i]->GetKadFileSearchID()) {
1577 return m_filelist[ i ];
1581 return NULL;
1585 bool CDownloadQueue::DoKademliaFileRequest()
1587 return ((::GetTickCount() - lastkademliafilerequest) > KADEMLIAASKTIME);
1589 // File_checked_for_headers