Upstream tarball 9882
[amule.git] / src / KnownFile.cpp
blob9f7a2307e868030e1459fce911a0b6a7ea3b0c10
1 //
2 // This file is part of the aMule Project.
3 //
4 // Parts of this file are based on work from pan One (http://home-3.tiscali.nl/~meost/pms/)
5 //
6 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
7 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
8 //
9 // Any parts of this program derived from the xMule, lMule or eMule project,
10 // or contributed by third-party developers are copyrighted by their
11 // respective authors.
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "KnownFile.h" // Do_not_auto_remove
32 #include <protocol/kad/Constants.h>
33 #include <protocol/ed2k/Client2Client/TCP.h>
34 #include <protocol/Protocols.h>
35 #include <tags/FileTags.h>
38 #include <wx/config.h>
41 #include "MemFile.h" // Needed for CMemFile
42 #include "updownclient.h" // Needed for CUpDownClient
43 #include "Packet.h" // Needed for CPacket
44 #include "Preferences.h" // Needed for CPreferences
45 #include "KnownFileList.h" // Needed for CKnownFileList
46 #include "amule.h" // Needed for theApp
47 #include "PartFile.h" // Needed for SavePartFile
48 #include "ClientList.h" // Needed for clientlist (buddy support)
49 #include "Logger.h"
50 #include "ScopedPtr.h" // Needed for CScopedArray and CScopedPtr
51 #include "GuiEvents.h" // Needed for Notify_*
52 #include "SearchFile.h" // Needed for CSearchFile
53 #include "FileArea.h" // Needed for CFileArea
54 #include "FileAutoClose.h" // Needed for CFileAutoClose
56 #include "CryptoPP_Inc.h" // Needed for MD4
58 #include <common/Format.h>
60 CFileStatistic::CFileStatistic() :
61 requested(0),
62 transferred(0),
63 accepted(0),
64 alltimerequested(0),
65 alltimetransferred(0),
66 alltimeaccepted(0)
70 #ifndef CLIENT_GUI
72 void CFileStatistic::AddRequest(){
73 requested++;
74 alltimerequested++;
75 theApp->knownfiles->requested++;
76 theApp->sharedfiles->UpdateItem(fileParent);
79 void CFileStatistic::AddAccepted(){
80 accepted++;
81 alltimeaccepted++;
82 theApp->knownfiles->accepted++;
83 theApp->sharedfiles->UpdateItem(fileParent);
86 void CFileStatistic::AddTransferred(uint64 bytes){
87 transferred += bytes;
88 alltimetransferred += bytes;
89 theApp->knownfiles->transferred += bytes;
90 theApp->sharedfiles->UpdateItem(fileParent);
93 #endif // CLIENT_GUI
96 /* Abstract File (base class)*/
98 CAbstractFile::CAbstractFile()
100 m_iRating(0),
101 m_hasComment(false),
102 m_iUserRating(0),
103 m_nFileSize(0)
108 CAbstractFile::CAbstractFile(const CAbstractFile& other)
110 m_abyFileHash(other.m_abyFileHash),
111 m_strComment(other.m_strComment),
112 m_iRating(other.m_iRating),
113 m_hasComment(other.m_hasComment),
114 m_iUserRating(other.m_iUserRating),
115 m_taglist(other.m_taglist),
116 m_nFileSize(other.m_nFileSize),
117 m_fileName(other.m_fileName)
119 /* // TODO: Currently it's not safe to duplicate the entries, but isn't needed either.
120 CKadEntryPtrList::const_iterator it = other.m_kadNotes.begin();
121 for (; it != other.m_kadNotes.end(); ++it) {
122 m_kadNotes.push_back(new Kademlia::CEntry(**it));
128 void CAbstractFile::SetFileName(const CPath& fileName)
130 m_fileName = fileName;
133 uint32 CAbstractFile::GetIntTagValue(uint8 tagname) const
135 ArrayOfCTag::const_iterator it = m_taglist.begin();
136 for (; it != m_taglist.end(); ++it){
137 if (((*it).GetNameID() == tagname) && (*it).IsInt()) {
138 return (*it).GetInt();
141 return 0;
144 bool CAbstractFile::GetIntTagValue(uint8 tagname, uint32& ruValue) const
146 ArrayOfCTag::const_iterator it = m_taglist.begin();
147 for (; it != m_taglist.end(); ++it){
148 if (((*it).GetNameID() == tagname) && (*it).IsInt()){
149 ruValue = (*it).GetInt();
150 return true;
153 return false;
156 uint32 CAbstractFile::GetIntTagValue(const wxString& tagname) const
158 ArrayOfCTag::const_iterator it = m_taglist.begin();
159 for (; it != m_taglist.end(); ++it){
160 if ((*it).IsInt() && ((*it).GetName() == tagname)) {
161 return (*it).GetInt();
164 return 0;
167 const wxString& CAbstractFile::GetStrTagValue(uint8 tagname) const
169 ArrayOfCTag::const_iterator it = m_taglist.begin();
170 for (; it != m_taglist.end(); ++it){
171 if ((*it).GetNameID() == tagname && (*it).IsStr()) {
172 return (*it).GetStr();
175 return EmptyString;
178 const wxString& CAbstractFile::GetStrTagValue(const wxString& tagname) const
180 ArrayOfCTag::const_iterator it = m_taglist.begin();
181 for (; it != m_taglist.end(); ++it){
182 if ((*it).IsStr() && ((*it).GetName() == tagname)) {
183 return (*it).GetStr();
186 return EmptyString;
189 const CTag *CAbstractFile::GetTag(uint8 tagname, uint8 tagtype) const
191 ArrayOfCTag::const_iterator it = m_taglist.begin();
192 for (; it != m_taglist.end(); ++it){
193 if ((*it).GetNameID() == tagname && (*it).GetType() == tagtype) {
194 return &(*it);
197 return NULL;
200 const CTag *CAbstractFile::GetTag(const wxString& tagname, uint8 tagtype) const
202 ArrayOfCTag::const_iterator it = m_taglist.begin();
203 for (; it != m_taglist.end(); ++it){
204 if ((*it).GetType() == tagtype && (*it).GetName() == tagname) {
205 return &(*it);
208 return NULL;
211 const CTag *CAbstractFile::GetTag(uint8 tagname) const
213 ArrayOfCTag::const_iterator it = m_taglist.begin();
214 for (; it != m_taglist.end(); ++it){
215 if ((*it).GetNameID() == tagname) {
216 return &(*it);
219 return NULL;
222 const CTag *CAbstractFile::GetTag(const wxString& tagname) const
224 ArrayOfCTag::const_iterator it = m_taglist.begin();
225 for (; it != m_taglist.end(); ++it){
226 if ((*it).GetName() == tagname) {
227 return &(*it);
230 return NULL;
233 void CAbstractFile::AddTagUnique(const CTag &rTag)
235 ArrayOfCTag::iterator it = m_taglist.begin();
236 for (; it != m_taglist.end(); ++it) {
237 if ( ( ((*it).GetNameID() != 0 &&
238 (*it).GetNameID() == rTag.GetNameID()) ||
239 (!(*it).GetName().IsEmpty() &&
240 !rTag.GetName().IsEmpty() &&
241 (*it).GetName() == rTag.GetName()) ) &&
242 (*it).GetType() == rTag.GetType()){
243 m_taglist.erase(it);
244 m_taglist.insert(it, rTag);
245 return;
248 m_taglist.push_back(rTag);
251 #ifndef CLIENT_GUI
252 void CAbstractFile::AddNote(Kademlia::CEntry *pEntry)
254 CKadEntryPtrList::iterator it = m_kadNotes.begin();
255 for (; it != m_kadNotes.end(); ++it) {
256 Kademlia::CEntry* entry = *it;
257 if(entry->m_uIP == pEntry->m_uIP || entry->m_uSourceID == pEntry->m_uSourceID) {
258 delete pEntry;
259 return;
262 m_kadNotes.push_front(pEntry);
264 #else
265 void CAbstractFile::AddNote(Kademlia::CEntry *)
268 #endif
271 /* Known File */
273 CKnownFile::CKnownFile()
275 Init();
277 m_bAutoUpPriority = thePrefs::GetNewAutoUp();
278 m_iUpPriority = ( m_bAutoUpPriority ) ? PR_HIGH : PR_NORMAL;
282 //#warning Experimental: Construct a CKnownFile from a CSearchFile
283 CKnownFile::CKnownFile(const CSearchFile &searchFile)
285 // This will copy the file hash
286 CAbstractFile(static_cast<const CAbstractFile &>(searchFile))
288 Init();
290 // Use CKnownFile::SetFileName()
291 SetFileName(searchFile.GetFileName());
293 // Use CKnownFile::SetFileSize()
294 SetFileSize(searchFile.GetFileSize());
296 m_bAutoUpPriority = thePrefs::GetNewAutoUp();
297 m_iUpPriority = ( m_bAutoUpPriority ) ? PR_HIGH : PR_NORMAL;
301 void CKnownFile::Init()
303 m_nCompleteSourcesTime = time(NULL);
304 m_nCompleteSourcesCount = 0;
305 m_nCompleteSourcesCountLo = 0;
306 m_nCompleteSourcesCountHi = 0;
307 m_bCommentLoaded = false;
308 m_iPartCount = 0;
309 m_iED2KPartCount = 0;
310 m_iED2KPartHashCount = 0;
311 m_PublishedED2K = false;
312 kadFileSearchID = 0;
313 m_lastPublishTimeKadSrc = 0;
314 m_lastPublishTimeKadNotes = 0;
315 m_lastBuddyIP = 0;
316 m_lastDateChanged = 0;
318 statistic.fileParent = this;
320 #ifndef CLIENT_GUI
321 m_pAICHHashSet = new CAICHHashSet(this);
322 #endif
326 void CKnownFile::SetFileSize(uint64 nFileSize)
328 CAbstractFile::SetFileSize(nFileSize);
329 #ifndef CLIENT_GUI
330 m_pAICHHashSet->SetFileSize(nFileSize);
331 #endif
333 // Examples of parthashs, hashsets and filehashs for different filesizes
334 // according the ed2k protocol
335 //----------------------------------------------------------------------
337 //File size: 3 bytes
338 //File hash: 2D55E87D0E21F49B9AD25F98531F3724
339 //Nr. hashs: 0
342 //File size: 1*PARTSIZE
343 //File hash: A72CA8DF7F07154E217C236C89C17619
344 //Nr. hashs: 2
345 //Hash[ 0]: 4891ED2E5C9C49F442145A3A5F608299
346 //Hash[ 1]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
349 //File size: 1*PARTSIZE + 1 byte
350 //File hash: 2F620AE9D462CBB6A59FE8401D2B3D23
351 //Nr. hashs: 2
352 //Hash[ 0]: 121795F0BEDE02DDC7C5426D0995F53F
353 //Hash[ 1]: C329E527945B8FE75B3C5E8826755747
356 //File size: 2*PARTSIZE
357 //File hash: A54C5E562D5E03CA7D77961EB9A745A4
358 //Nr. hashs: 3
359 //Hash[ 0]: B3F5CE2A06BF403BFB9BFFF68BDDC4D9
360 //Hash[ 1]: 509AA30C9EA8FC136B1159DF2F35B8A9
361 //Hash[ 2]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
364 //File size: 3*PARTSIZE
365 //File hash: 5E249B96F9A46A18FC2489B005BF2667
366 //Nr. hashs: 4
367 //Hash[ 0]: 5319896A2ECAD43BF17E2E3575278E72
368 //Hash[ 1]: D86EF157D5E49C5ED502EDC15BB5F82B
369 //Hash[ 2]: 10F2D5B1FCB95C0840519C58D708480F
370 //Hash[ 3]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
373 //File size: 3*PARTSIZE + 1 byte
374 //File hash: 797ED552F34380CAFF8C958207E40355
375 //Nr. hashs: 4
376 //Hash[ 0]: FC7FD02CCD6987DCF1421F4C0AF94FB8
377 //Hash[ 1]: 2FE466AF8A7C06DA3365317B75A5ACFE
378 //Hash[ 2]: 873D3BF52629F7C1527C6E8E473C1C30
379 //Hash[ 3]: BCE50BEE7877BB07BB6FDA56BFE142FB
382 // File size Data parts ED2K parts ED2K part hashs
383 // ---------------------------------------------------------------
384 // 1..PARTSIZE-1 1 1 0(!)
385 // PARTSIZE 1 2(!) 2(!)
386 // PARTSIZE+1 2 2 2
387 // PARTSIZE*2 2 3(!) 3(!)
388 // PARTSIZE*2+1 3 3 3
390 if (nFileSize == 0){
391 //wxFAIL; // Kry - Why commented out by lemonfan? it can never be 0
392 m_iPartCount = 0;
393 m_iED2KPartCount = 0;
394 m_iED2KPartHashCount = 0;
395 m_sizeLastPart = 0;
396 return;
399 // nr. of data parts
400 m_iPartCount = nFileSize / PARTSIZE + 1;
401 // size of last part
402 m_sizeLastPart = nFileSize % PARTSIZE;
403 // file with size of n * PARTSIZE
404 if (m_sizeLastPart == 0) {
405 m_sizeLastPart = PARTSIZE;
406 m_iPartCount--;
409 // nr. of parts to be used with OP_FILESTATUS
410 m_iED2KPartCount = nFileSize / PARTSIZE + 1;
412 // nr. of parts to be used with OP_HASHSETANSWER
413 m_iED2KPartHashCount = nFileSize / PARTSIZE;
414 if (m_iED2KPartHashCount != 0) {
415 m_iED2KPartHashCount += 1;
420 #ifdef CLIENT_GUI
422 CKnownFile::CKnownFile(CEC_SharedFile_Tag *tag)
424 Init();
426 SetFileName(CPath(tag->FileName()));
427 m_abyFileHash = tag->ID();
428 SetFileSize(tag->SizeFull());
429 m_AvailPartFrequency.insert(m_AvailPartFrequency.end(), m_iPartCount, 0);
430 m_iUpPriorityEC = tag->Prio();
431 m_AICHMasterHash = tag->GetAICHHash();
432 m_filePath = CPath(tag->FilePath());
435 CKnownFile::~CKnownFile()
439 #else // ! CLIENT_GUI
441 CKnownFile::~CKnownFile()
443 SourceSet::iterator it = m_ClientUploadList.begin();
444 for ( ; it != m_ClientUploadList.end(); ++it ) {
445 (*it)->ClearUploadFileID();
448 delete m_pAICHHashSet;
451 void CKnownFile::AddUploadingClient(CUpDownClient* client)
453 m_ClientUploadList.insert(client);
455 UpdateAutoUpPriority();
459 void CKnownFile::RemoveUploadingClient(CUpDownClient* client)
461 if (m_ClientUploadList.erase(client)) {
462 UpdateAutoUpPriority();
467 void CKnownFile::SetFilePath(const CPath& filePath)
469 m_filePath = filePath;
473 // needed for memfiles. its probably better to switch everything to CFile...
474 bool CKnownFile::LoadHashsetFromFile(const CFileDataIO* file, bool checkhash)
476 CMD4Hash checkid = file->ReadHash();
478 uint16 parts = file->ReadUInt16();
479 for (uint16 i = 0; i < parts; ++i){
480 CMD4Hash cur_hash = file->ReadHash();
481 m_hashlist.push_back(cur_hash);
484 // SLUGFILLER: SafeHash - always check for valid m_hashlist
485 if (!checkhash){
486 m_abyFileHash = checkid;
487 if (parts <= 1) { // nothing to check
488 return true;
490 } else {
491 if ( m_abyFileHash != checkid ) {
492 return false; // wrong file?
493 } else {
494 if (parts != GetED2KPartHashCount()) {
495 return false;
499 // SLUGFILLER: SafeHash
501 // trust noone ;-)
502 // lol, useless comment but made me lmao
503 // wtf you guys are weird.
505 if (!m_hashlist.empty()) {
506 CreateHashFromHashlist(m_hashlist, &checkid);
509 if ( m_abyFileHash == checkid ) {
510 return true;
511 } else {
512 m_hashlist.clear();
513 return false;
518 bool CKnownFile::LoadTagsFromFile(const CFileDataIO* file)
520 uint32 tagcount = file->ReadUInt32();
521 for (uint32 j = 0; j != tagcount; ++j) {
522 CTag newtag(*file, true);
523 switch(newtag.GetNameID()){
524 case FT_FILENAME:
525 if (GetFileName().IsOk()) {
526 // Unlike eMule, we actually prefer the second
527 // filename tag, since we use it to specify the
528 // 'universial' filename (see CPath::ToUniv).
529 CPath path = CPath::FromUniv(newtag.GetStr());
531 // May be invalid, if from older versions where
532 // unicoded filenames be saved as empty-strings.
533 if (path.IsOk()) {
534 SetFileName(path);
536 } else {
537 SetFileName(CPath(newtag.GetStr()));
539 break;
541 case FT_FILESIZE:
542 SetFileSize(newtag.GetInt());
543 m_AvailPartFrequency.clear();
544 m_AvailPartFrequency.insert(
545 m_AvailPartFrequency.begin(),
546 GetPartCount(), 0);
547 break;
549 case FT_ATTRANSFERRED:
550 statistic.alltimetransferred += newtag.GetInt();
551 break;
553 case FT_ATTRANSFERREDHI:
554 statistic.alltimetransferred =
555 (((uint64)newtag.GetInt()) << 32) +
556 ((uint64)statistic.alltimetransferred);
557 break;
559 case FT_ATREQUESTED:
560 statistic.alltimerequested = newtag.GetInt();
561 break;
563 case FT_ATACCEPTED:
564 statistic.alltimeaccepted = newtag.GetInt();
565 break;
567 case FT_ULPRIORITY:
568 m_iUpPriority = newtag.GetInt();
569 if( m_iUpPriority == PR_AUTO ){
570 m_iUpPriority = PR_HIGH;
571 m_bAutoUpPriority = true;
572 } else {
573 if ( m_iUpPriority != PR_VERYLOW &&
574 m_iUpPriority != PR_LOW &&
575 m_iUpPriority != PR_NORMAL &&
576 m_iUpPriority != PR_HIGH &&
577 m_iUpPriority != PR_VERYHIGH &&
578 m_iUpPriority != PR_POWERSHARE) {
579 m_iUpPriority = PR_NORMAL;
582 m_bAutoUpPriority = false;
584 break;
586 case FT_PERMISSIONS:
587 // Ignore it, it's not used anymore.
588 break;
590 case FT_AICH_HASH: {
591 CAICHHash hash;
592 bool hashSizeOk =
593 hash.DecodeBase32(newtag.GetStr()) == CAICHHash::GetHashSize();
594 wxASSERT(hashSizeOk);
595 if (hashSizeOk) {
596 m_pAICHHashSet->SetMasterHash(hash, AICH_HASHSETCOMPLETE);
598 break;
601 case FT_KADLASTPUBLISHSRC:
602 SetLastPublishTimeKadSrc( newtag.GetInt(), 0 );
604 if(GetLastPublishTimeKadSrc() > (uint32)time(NULL)+KADEMLIAREPUBLISHTIMES) {
605 //There may be a posibility of an older client that saved a random number here.. This will check for that..
606 SetLastPublishTimeKadSrc(0, 0);
608 break;
610 case FT_KADLASTPUBLISHNOTES:
611 SetLastPublishTimeKadNotes( newtag.GetInt() );
612 break;
614 case FT_KADLASTPUBLISHKEY:
615 // Just purge it
616 wxASSERT( newtag.IsInt() );
617 break;
619 default:
620 // Store them here and write them back on saving.
621 m_taglist.push_back(newtag);
625 return true;
629 bool CKnownFile::LoadDateFromFile(const CFileDataIO* file)
631 m_lastDateChanged = file->ReadUInt32();
633 return true;
637 bool CKnownFile::LoadFromFile(const CFileDataIO* file)
639 // SLUGFILLER: SafeHash - load first, verify later
640 bool ret1 = LoadDateFromFile(file);
641 bool ret2 = LoadHashsetFromFile(file,false);
642 bool ret3 = LoadTagsFromFile(file);
643 UpdatePartsInfo();
644 // Final hash-count verification, needs to be done after the tags are loaded.
645 return ret1 && ret2 && ret3 && GetED2KPartHashCount()==GetHashCount();
646 // SLUGFILLER: SafeHash
650 bool CKnownFile::WriteToFile(CFileDataIO* file)
652 wxCHECK(!IsPartFile(), false);
654 // date
655 file->WriteUInt32(m_lastDateChanged);
656 // hashset
657 file->WriteHash(m_abyFileHash);
659 uint16 parts = m_hashlist.size();
660 file->WriteUInt16(parts);
662 for (int i = 0; i < parts; ++i)
663 file->WriteHash(m_hashlist[i]);
665 //tags
666 const int iFixedTags = 8;
667 uint32 tagcount = iFixedTags;
668 if (HasProperAICHHashSet()) {
669 tagcount++;
671 // Float meta tags are currently not written. All older eMule versions < 0.28a have
672 // a bug in the meta tag reading+writing code. To achive maximum backward
673 // compatibility for met files with older eMule versions we just don't write float
674 // tags. This is OK, because we (eMule) do not use float tags. The only float tags
675 // we may have to handle is the '# Sent' tag from the Hybrid, which is pretty
676 // useless but may be received from us via the servers.
678 // The code for writing the float tags SHOULD BE ENABLED in SOME MONTHS (after most
679 // people are using the newer eMule versions which do not write broken float tags).
680 for (size_t j = 0; j < m_taglist.size(); ++j){
681 if (m_taglist[j].IsInt() || m_taglist[j].IsStr()) {
682 ++tagcount;
686 if (m_lastPublishTimeKadSrc) {
687 ++tagcount;
690 if (m_lastPublishTimeKadNotes){
691 ++tagcount;
694 // standard tags
696 file->WriteUInt32(tagcount);
698 // We still save the unicoded filename, for backwards
699 // compatibility with pre-2.2 and other clients.
700 CTagString nametag_unicode(FT_FILENAME, GetFileName().GetRaw());
701 // We write it with BOM to kep eMule compatibility
702 nametag_unicode.WriteTagToFile(file,utf8strOptBOM);
704 // The non-unicoded filename is written in an 'universial'
705 // format, which allows us to identify files, even if the
706 // system locale changes.
707 CTagString nametag(FT_FILENAME, CPath::ToUniv(GetFileName()));
708 nametag.WriteTagToFile(file);
710 CTagIntSized sizetag(FT_FILESIZE, GetFileSize(), IsLargeFile() ? 64 : 32);
711 sizetag.WriteTagToFile(file);
713 // statistic
714 uint32 tran;
715 tran=statistic.alltimetransferred & 0xFFFFFFFF;
716 CTagInt32 attag1(FT_ATTRANSFERRED, tran);
717 attag1.WriteTagToFile(file);
719 tran=statistic.alltimetransferred>>32;
720 CTagInt32 attag4(FT_ATTRANSFERREDHI, tran);
721 attag4.WriteTagToFile(file);
723 CTagInt32 attag2(FT_ATREQUESTED, statistic.GetAllTimeRequests());
724 attag2.WriteTagToFile(file);
726 CTagInt32 attag3(FT_ATACCEPTED, statistic.GetAllTimeAccepts());
727 attag3.WriteTagToFile(file);
729 // priority N permission
730 CTagInt32 priotag(FT_ULPRIORITY, IsAutoUpPriority() ? PR_AUTO : m_iUpPriority);
731 priotag.WriteTagToFile(file);
733 //AICH Filehash
734 if (HasProperAICHHashSet()) {
735 CTagString aichtag(FT_AICH_HASH, m_pAICHHashSet->GetMasterHash().GetString());
736 aichtag.WriteTagToFile(file);
739 // Kad sources
740 if (m_lastPublishTimeKadSrc){
741 CTagInt32 kadLastPubSrc(FT_KADLASTPUBLISHSRC, m_lastPublishTimeKadSrc);
742 kadLastPubSrc.WriteTagToFile(file);
745 // Kad notes
746 if (m_lastPublishTimeKadNotes){
747 CTagInt32 kadLastPubNotes(FT_KADLASTPUBLISHNOTES, m_lastPublishTimeKadNotes);
748 kadLastPubNotes.WriteTagToFile(file);
751 //other tags
752 for (size_t j = 0; j < m_taglist.size(); ++j){
753 if (m_taglist[j].IsInt() || m_taglist[j].IsStr()) {
754 m_taglist[j].WriteTagToFile(file);
757 return true;
761 void CKnownFile::CreateHashFromHashlist(const ArrayOfCMD4Hash& hashes, CMD4Hash* Output)
763 wxCHECK_RET(hashes.size(), wxT("No input to hash from in CreateHashFromHashlist"));
765 std::vector<byte> buffer(hashes.size() * MD4HASH_LENGTH);
766 std::vector<byte>::iterator it = buffer.begin();
768 for (size_t i = 0; i < hashes.size(); ++i) {
769 it = STLCopy_n(hashes[i].GetHash(), MD4HASH_LENGTH, it);
772 CreateHashFromInput(&buffer[0], buffer.size(), Output, NULL);
776 void CKnownFile::CreateHashFromFile(CFileAutoClose& file, uint64 offset, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut)
778 wxCHECK_RET(Length, wxT("No input to hash from in CreateHashFromFile"));
780 CFileArea area;
781 area.ReadAt(file, offset, Length);
783 CreateHashFromInput(area.GetBuffer(), Length, Output, pShaHashOut);
784 area.CheckError();
788 void CKnownFile::CreateHashFromInput(const byte* input, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut )
790 wxASSERT_MSG(Output || pShaHashOut, wxT("Nothing to do in CreateHashFromInput"));
791 wxCHECK_RET(input, wxT("No input to hash from in CreateHashFromInput"));
792 wxASSERT(Length <= PARTSIZE); // We never hash more than one PARTSIZE
794 CMemFile data(input, Length);
796 uint32 Required = Length;
797 byte X[64*128];
799 uint32 posCurrentEMBlock = 0;
800 uint32 nIACHPos = 0;
801 CScopedPtr<CAICHHashAlgo> pHashAlg(CAICHHashSet::GetNewHashAlgo());
803 // This is all AICH.
804 while (Required >= 64) {
805 uint32 len = Required / 64;
806 if (len > sizeof(X)/(64 * sizeof(X[0]))) {
807 len = sizeof(X)/(64 * sizeof(X[0]));
810 data.Read(&X, len * 64);
812 // SHA hash needs 180KB blocks
813 if (pShaHashOut) {
814 if (nIACHPos + len*64 >= EMBLOCKSIZE) {
815 uint32 nToComplete = EMBLOCKSIZE - nIACHPos;
816 pHashAlg->Add(X, nToComplete);
817 wxASSERT( nIACHPos + nToComplete == EMBLOCKSIZE );
818 pShaHashOut->SetBlockHash(EMBLOCKSIZE, posCurrentEMBlock, pHashAlg.get());
819 posCurrentEMBlock += EMBLOCKSIZE;
820 pHashAlg->Reset();
821 pHashAlg->Add(X+nToComplete,(len*64) - nToComplete);
822 nIACHPos = (len*64) - nToComplete;
824 else{
825 pHashAlg->Add(X, len*64);
826 nIACHPos += len*64;
830 Required -= len*64;
832 // bytes to read
833 Required = Length % 64;
834 if (Required != 0){
835 data.Read(&X,Required);
837 if (pShaHashOut != NULL){
838 if (nIACHPos + Required >= EMBLOCKSIZE){
839 uint32 nToComplete = EMBLOCKSIZE - nIACHPos;
840 pHashAlg->Add(X, nToComplete);
841 wxASSERT( nIACHPos + nToComplete == EMBLOCKSIZE );
842 pShaHashOut->SetBlockHash(EMBLOCKSIZE, posCurrentEMBlock, pHashAlg.get());
843 posCurrentEMBlock += EMBLOCKSIZE;
844 pHashAlg->Reset();
845 pHashAlg->Add(X+nToComplete, Required - nToComplete);
846 nIACHPos = Required - nToComplete;
848 else{
849 pHashAlg->Add(X, Required);
850 nIACHPos += Required;
854 if (pShaHashOut != NULL){
855 if(nIACHPos > 0){
856 pShaHashOut->SetBlockHash(nIACHPos, posCurrentEMBlock, pHashAlg.get());
857 posCurrentEMBlock += nIACHPos;
859 wxASSERT( posCurrentEMBlock == Length );
860 wxCHECK2( pShaHashOut->ReCalculateHash(pHashAlg.get(), false), );
863 if (Output != NULL){
864 #ifdef __WEAK_CRYPTO__
865 CryptoPP::Weak::MD4 md4_hasher;
866 #else
867 CryptoPP::MD4 md4_hasher;
868 #endif
869 md4_hasher.CalculateDigest(Output->GetHash(), input, Length);
874 const CMD4Hash& CKnownFile::GetPartHash(uint16 part) const {
875 wxASSERT( part < m_hashlist.size() );
877 return m_hashlist[part];
880 CPacket* CKnownFile::CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions)
882 // Kad reviewed
884 if (m_ClientUploadList.empty()) {
885 return NULL;
888 if ((((CKnownFile*)forClient->GetRequestFile() != this)
889 && ((CKnownFile*)forClient->GetUploadFile() != this)) || forClient->GetUploadFileID() != GetFileHash()) {
890 wxString file1 = _("Unknown");
891 if (forClient->GetRequestFile() && forClient->GetRequestFile()->GetFileName().IsOk()) {
892 file1 = forClient->GetRequestFile()->GetFileName().GetPrintable();
893 } else if (forClient->GetUploadFile() && forClient->GetUploadFile()->GetFileName().IsOk()) {
894 file1 = forClient->GetUploadFile()->GetFileName().GetPrintable();
896 wxString file2 = _("Unknown");
897 if (GetFileName().IsOk()) {
898 file2 = GetFileName().GetPrintable();
900 AddDebugLogLineM(false, logKnownFiles, wxT("File missmatch on source packet (K) Sending: ") + file1 + wxT(" From: ") + file2);
901 return NULL;
904 const BitVector& rcvstatus = forClient->GetUpPartStatus();
905 bool SupportsUploadChunksState = !rcvstatus.empty();
906 //wxASSERT(rcvstatus.size() == GetPartCount()); // Obviously!
907 if (rcvstatus.size() != GetPartCount()) {
908 // Yuck. Same file but different part count? Seriously fucked up.
909 AddDebugLogLineM(false, logKnownFiles, CFormat(wxT("Impossible situation: different partcounts for the same known file: %i (client) and %i (file)")) % rcvstatus.size() % GetPartCount());
910 return NULL;
913 CMemFile data(1024);
915 uint8 byUsedVersion;
916 bool bIsSX2Packet;
917 if (forClient->SupportsSourceExchange2() && byRequestedVersion > 0){
918 // the client uses SourceExchange2 and requested the highest version he knows
919 // and we send the highest version we know, but of course not higher than his request
920 byUsedVersion = std::min(byRequestedVersion, (uint8)SOURCEEXCHANGE2_VERSION);
921 bIsSX2Packet = true;
922 data.WriteUInt8(byUsedVersion);
924 // we don't support any special SX2 options yet, reserved for later use
925 if (nRequestedOptions != 0) {
926 AddDebugLogLineM(false, logKnownFiles, CFormat(wxT("Client requested unknown options for SourceExchange2: %u")) % nRequestedOptions);
928 } else {
929 byUsedVersion = forClient->GetSourceExchange1Version();
930 bIsSX2Packet = false;
931 if (forClient->SupportsSourceExchange2()) {
932 AddDebugLogLineM(false, logKnownFiles, wxT("Client which announced to support SX2 sent SX1 packet instead"));
936 uint16 nCount = 0;
938 data.WriteHash(forClient->GetUploadFileID());
939 data.WriteUInt16(nCount);
940 uint32 cDbgNoSrc = 0;
942 SourceSet::iterator it = m_ClientUploadList.begin();
943 for ( ; it != m_ClientUploadList.end(); it++ ) {
944 const CUpDownClient *cur_src = *it;
946 if ( cur_src->HasLowID() ||
947 cur_src == forClient ||
948 !( cur_src->GetUploadState() == US_UPLOADING ||
949 cur_src->GetUploadState() == US_ONUPLOADQUEUE)) {
950 continue;
953 bool bNeeded = false;
955 if ( SupportsUploadChunksState ) {
956 const BitVector& srcstatus = cur_src->GetUpPartStatus();
957 if ( !srcstatus.empty() ) {
958 //wxASSERT(srcstatus.size() == GetPartCount()); // Obviously!
959 if (srcstatus.size() != GetPartCount()) {
960 continue;
962 if ( cur_src->GetUpPartCount() == forClient->GetUpPartCount() ) {
963 for (int x = 0; x < GetPartCount(); x++ ) {
964 if ( srcstatus.at(x) && !rcvstatus.at(x) ) {
965 // We know the receiving client needs
966 // a chunk from this client.
967 bNeeded = true;
968 break;
972 } else {
973 cDbgNoSrc++;
974 // This client doesn't support upload chunk status.
975 // So just send it and hope for the best.
976 bNeeded = true;
978 } else {
979 // remote client does not support upload chunk status,
980 // search sources which have at least one complete part
981 // we could even sort the list of sources by available
982 // chunks to return as much sources as possible which
983 // have the most available chunks. but this could be
984 // a noticeable performance problem.
985 const BitVector& srcstatus = cur_src->GetUpPartStatus();
986 if ( !srcstatus.empty() ) {
987 //wxASSERT(srcstatus.size() == GetPartCount());
988 if (srcstatus.size() != GetPartCount()) {
989 continue;
991 for (int x = 0; x < GetPartCount(); x++ ) {
992 if ( srcstatus.at(x) ) {
993 // this client has at least one chunk
994 bNeeded = true;
995 break;
998 } else {
999 // This client doesn't support upload chunk status.
1000 // So just send it and hope for the best.
1001 bNeeded = true;
1005 if ( bNeeded ) {
1006 nCount++;
1007 uint32 dwID;
1008 if(byUsedVersion >= 3) {
1009 dwID = cur_src->GetUserIDHybrid();
1010 } else {
1011 dwID = cur_src->GetIP();
1013 data.WriteUInt32(dwID);
1014 data.WriteUInt16(cur_src->GetUserPort());
1015 data.WriteUInt32(cur_src->GetServerIP());
1016 data.WriteUInt16(cur_src->GetServerPort());
1018 if (byUsedVersion >= 2) {
1019 data.WriteHash(cur_src->GetUserHash());
1022 if (byUsedVersion >= 4){
1023 // CryptSettings - SourceExchange V4
1024 // 5 Reserved (!)
1025 // 1 CryptLayer Required
1026 // 1 CryptLayer Requested
1027 // 1 CryptLayer Supported
1028 const uint8 uSupportsCryptLayer = cur_src->SupportsCryptLayer() ? 1 : 0;
1029 const uint8 uRequestsCryptLayer = cur_src->RequestsCryptLayer() ? 1 : 0;
1030 const uint8 uRequiresCryptLayer = cur_src->RequiresCryptLayer() ? 1 : 0;
1031 const uint8 byCryptOptions = (uRequiresCryptLayer << 2) | (uRequestsCryptLayer << 1) | (uSupportsCryptLayer << 0);
1032 data.WriteUInt8(byCryptOptions);
1035 if (nCount > 500) {
1036 break;
1041 if (!nCount) {
1042 return 0;
1045 data.Seek(bIsSX2Packet ? 17 : 16, wxFromStart);
1046 data.WriteUInt16(nCount);
1048 CPacket* result = new CPacket(data, OP_EMULEPROT, bIsSX2Packet ? OP_ANSWERSOURCES2 : OP_ANSWERSOURCES);
1050 if ( result->GetPacketSize() > 354 ) {
1051 result->PackPacket();
1054 return result;
1058 // Updates priority of file if autopriority is activated
1059 void CKnownFile::UpdateAutoUpPriority()
1061 if (IsAutoUpPriority()) {
1062 uint32 queued = GetQueuedCount();
1063 uint8 priority = PR_NORMAL;
1065 if (queued > 20) {
1066 priority = PR_LOW;
1067 } else if (queued > 1) {
1068 priority = PR_NORMAL;
1069 } else {
1070 priority = PR_HIGH;
1073 if (GetUpPriority() != priority) {
1074 SetUpPriority(priority, false);
1075 Notify_SharedFilesUpdateItem(this);
1080 void CKnownFile::SetFileComment(const wxString& strNewComment)
1082 if (m_strComment != strNewComment) {
1083 SetLastPublishTimeKadNotes(0);
1084 wxString strCfgPath = wxT("/") + m_abyFileHash.Encode() + wxT("/");
1086 wxConfigBase* cfg = wxConfigBase::Get();
1087 cfg->Write( strCfgPath + wxT("Comment"), strNewComment);
1089 m_strComment = strNewComment;
1091 SourceSet::iterator it = m_ClientUploadList.begin();
1092 for ( ; it != m_ClientUploadList.end(); it++ ) {
1093 (*it)->SetCommentDirty();
1099 // For File rate
1100 void CKnownFile::SetFileRating(int8 iNewRating)
1102 if (m_iRating != iNewRating) {
1103 SetLastPublishTimeKadNotes(0);
1104 wxString strCfgPath = wxT("/") + m_abyFileHash.Encode() + wxT("/");
1105 wxConfigBase* cfg = wxConfigBase::Get();
1106 cfg->Write( strCfgPath + wxT("Rate"), (int)iNewRating);
1107 m_iRating = iNewRating;
1109 SourceSet::iterator it = m_ClientUploadList.begin();
1110 for ( ; it != m_ClientUploadList.end(); it++ ) {
1111 (*it)->SetCommentDirty();
1117 void CKnownFile::SetUpPriority(uint8 iNewUpPriority, bool m_bsave){
1118 m_iUpPriority = iNewUpPriority;
1119 if( IsPartFile() && m_bsave ) {
1120 ((CPartFile*)this)->SavePartFile();
1124 void CKnownFile::SetPublishedED2K(bool val){
1125 m_PublishedED2K = val;
1126 Notify_SharedFilesUpdateItem(this);
1129 bool CKnownFile::PublishNotes()
1131 if(m_lastPublishTimeKadNotes > (uint32)time(NULL)) {
1132 return false;
1135 if(!GetFileComment().IsEmpty()) {
1136 m_lastPublishTimeKadNotes = (uint32)time(NULL)+KADEMLIAREPUBLISHTIMEN;
1137 return true;
1140 if(GetFileRating() != 0) {
1141 m_lastPublishTimeKadNotes = (uint32)time(NULL)+KADEMLIAREPUBLISHTIMEN;
1142 return true;
1145 return false;
1148 bool CKnownFile::PublishSrc()
1150 uint32 lastBuddyIP = 0;
1152 if( theApp->IsFirewalled() ) {
1153 CUpDownClient* buddy = theApp->clientlist->GetBuddy();
1154 if( buddy ) {
1155 lastBuddyIP = theApp->clientlist->GetBuddy()->GetIP();
1156 if( lastBuddyIP != m_lastBuddyIP ) {
1157 SetLastPublishTimeKadSrc( (uint32)time(NULL)+KADEMLIAREPUBLISHTIMES, lastBuddyIP );
1158 return true;
1160 } else {
1161 return false;
1165 if(m_lastPublishTimeKadSrc > (uint32)time(NULL)) {
1166 return false;
1169 SetLastPublishTimeKadSrc((uint32)time(NULL)+KADEMLIAREPUBLISHTIMES,lastBuddyIP);
1170 return true;
1174 void CKnownFile::UpdatePartsInfo()
1176 // Cache part count
1177 uint16 partcount = GetPartCount();
1178 bool flag = (time(NULL) - m_nCompleteSourcesTime > 0);
1180 // Ensure the frequency-list is ready
1181 if ( m_AvailPartFrequency.size() != GetPartCount() ) {
1182 m_AvailPartFrequency.clear();
1183 m_AvailPartFrequency.insert(m_AvailPartFrequency.begin(), GetPartCount(), 0);
1186 if (flag) {
1187 ArrayOfUInts16 count;
1188 count.reserve(m_ClientUploadList.size());
1190 SourceSet::iterator it = m_ClientUploadList.begin();
1191 for ( ; it != m_ClientUploadList.end(); it++ ) {
1192 if ( !(*it)->GetUpPartStatus().empty() && (*it)->GetUpPartCount() == partcount ) {
1193 count.push_back((*it)->GetUpCompleteSourcesCount());
1197 m_nCompleteSourcesCount = m_nCompleteSourcesCountLo = m_nCompleteSourcesCountHi = 0;
1199 if( partcount > 0) {
1200 m_nCompleteSourcesCount = m_AvailPartFrequency[0];
1202 for (uint16 i = 1; i < partcount; ++i) {
1203 if( m_nCompleteSourcesCount > m_AvailPartFrequency[i]) {
1204 m_nCompleteSourcesCount = m_AvailPartFrequency[i];
1207 count.push_back(m_nCompleteSourcesCount);
1209 int32 n = count.size();
1210 if (n > 0) {
1211 std::sort(count.begin(), count.end(), std::less<uint16>());
1213 // calculate range
1214 int i = n >> 1; // (n / 2)
1215 int j = (n * 3) >> 2; // (n * 3) / 4
1216 int k = (n * 7) >> 3; // (n * 7) / 8
1218 // For complete files, trust the people your uploading to more...
1220 // For low guess and normal guess count
1221 // - If we see more sources then the guessed low and
1222 // normal, use what we see.
1223 // - If we see less sources then the guessed low,
1224 // adjust network accounts for 100%, we account for
1225 // 0% with what we see and make sure we are still
1226 // above the normal.
1227 // For high guess
1228 // Adjust 100% network and 0% what we see.
1229 if (n < 20) {
1230 if ( count[i] < m_nCompleteSourcesCount ) {
1231 m_nCompleteSourcesCountLo = m_nCompleteSourcesCount;
1232 } else {
1233 m_nCompleteSourcesCountLo = count[i];
1235 m_nCompleteSourcesCount= m_nCompleteSourcesCountLo;
1236 m_nCompleteSourcesCountHi = count[j];
1237 if( m_nCompleteSourcesCountHi < m_nCompleteSourcesCount ) {
1238 m_nCompleteSourcesCountHi = m_nCompleteSourcesCount;
1240 } else {
1241 // Many sources..
1242 // For low guess
1243 // Use what we see.
1244 // For normal guess
1245 // Adjust network accounts for 100%, we account for
1246 // 0% with what we see and make sure we are still above the low.
1247 // For high guess
1248 // Adjust network accounts for 100%, we account for 0%
1249 // with what we see and make sure we are still above the normal.
1251 m_nCompleteSourcesCountLo = m_nCompleteSourcesCount;
1252 m_nCompleteSourcesCount = count[j];
1253 if( m_nCompleteSourcesCount < m_nCompleteSourcesCountLo ) {
1254 m_nCompleteSourcesCount = m_nCompleteSourcesCountLo;
1256 m_nCompleteSourcesCountHi= count[k];
1257 if( m_nCompleteSourcesCountHi < m_nCompleteSourcesCount ) {
1258 m_nCompleteSourcesCountHi = m_nCompleteSourcesCount;
1262 m_nCompleteSourcesTime = time(NULL) + (60);
1265 Notify_SharedFilesUpdateItem(this);
1269 void CKnownFile::UpdateUpPartsFrequency( CUpDownClient* client, bool increment )
1271 if ( m_AvailPartFrequency.size() != GetPartCount() ) {
1272 m_AvailPartFrequency.clear();
1273 m_AvailPartFrequency.insert(m_AvailPartFrequency.begin(), GetPartCount(), 0);
1274 if ( !increment ) {
1275 return;
1279 const BitVector& freq = client->GetUpPartStatus();
1280 unsigned int size = freq.size();
1281 if ( size != m_AvailPartFrequency.size() ) {
1282 return;
1285 if ( increment ) {
1286 for ( unsigned int i = 0; i < size; ++i ) {
1287 if ( freq[i] ) {
1288 m_AvailPartFrequency[i]++;
1291 } else {
1292 for ( unsigned int i = 0; i < size; ++i ) {
1293 if ( freq[i] ) {
1294 m_AvailPartFrequency[i]--;
1300 void CKnownFile::ClearPriority() {
1301 if ( !m_bAutoUpPriority ) return;
1302 m_iUpPriority = ( m_bAutoUpPriority ) ? PR_HIGH : PR_NORMAL;
1303 UpdateAutoUpPriority();
1306 void GuessAndRemoveExt(CPath& name)
1308 wxString ext = name.GetExt();
1310 // Remove common two-part extensions, such as "tar.gz"
1311 if (ext == wxT("gz") || ext == wxT("bz2")) {
1312 name = name.RemoveExt();
1313 if (name.GetExt() == wxT("tar")) {
1314 name = name.RemoveExt();
1316 // might be an extension if length == 3
1317 // and also remove some common non-three-character extensions
1318 } else if (ext.Length() == 3 ||
1319 ext == wxT("7z") ||
1320 ext == wxT("rm") ||
1321 ext == wxT("jpeg") ||
1322 ext == wxT("mpeg")
1324 name = name.RemoveExt();
1328 void CKnownFile::SetFileName(const CPath& filename)
1330 CAbstractFile::SetFileName(filename);
1331 wordlist.clear();
1332 // Don't publish extension. That'd kill the node indexing e.g. "avi".
1333 CPath tmpName = GetFileName();
1334 GuessAndRemoveExt(tmpName);
1335 Kademlia::CSearchManager::GetWords(tmpName.GetPrintable(), &wordlist);
1338 #endif // CLIENT_GUI
1340 //For File Comment //
1341 void CKnownFile::LoadComment()
1343 #ifndef CLIENT_GUI
1344 wxString strCfgPath = wxT("/") + m_abyFileHash.Encode() + wxT("/");
1346 wxConfigBase* cfg = wxConfigBase::Get();
1348 m_strComment = cfg->Read( strCfgPath + wxT("Comment"), wxEmptyString);
1349 m_iRating = cfg->Read( strCfgPath + wxT("Rate"), 0l);
1350 m_bCommentLoaded = true;
1352 #else
1353 m_strComment = wxEmptyString;
1354 m_bCommentLoaded = true;
1355 m_iRating =0;
1356 #endif
1361 wxString CKnownFile::GetAICHMasterHash() const
1363 #ifdef CLIENT_GUI
1364 return m_AICHMasterHash;
1365 #else
1366 if (HasProperAICHHashSet()) {
1367 return m_pAICHHashSet->GetMasterHash().GetString();
1370 return wxEmptyString;
1371 #endif
1375 bool CKnownFile::HasProperAICHHashSet() const
1377 #ifdef CLIENT_GUI
1378 return m_AICHMasterHash.Length() != 0;
1379 #else
1380 return m_pAICHHashSet->HasValidMasterHash() &&
1381 (m_pAICHHashSet->GetStatus() == AICH_HASHSETCOMPLETE ||
1382 m_pAICHHashSet->GetStatus() == AICH_VERIFIED);
1383 #endif
1386 wxString CKnownFile::GetFeedback() const
1388 return wxString(_("File name")) + wxT(": ") + GetFileName().GetPrintable() + wxT("\n")
1389 + _("File size") + wxT(": ") + CastItoXBytes(GetFileSize()) + wxT("\n")
1390 + _("Share ratio") + wxString::Format(wxT(": %.2f%%\n"), (((double)statistic.GetAllTimeTransferred() / (double)GetFileSize()) * 100.0))
1391 + _("Uploaded") + wxT(": ") + CastItoXBytes(statistic.GetTransferred()) + wxT(" (") + CastItoXBytes(statistic.GetAllTimeTransferred()) + wxT(")\n")
1392 + _("Requested") + CFormat(wxT(": %u (%u)\n")) % statistic.GetRequests() % statistic.GetAllTimeRequests()
1393 + _("Accepted") + CFormat(wxT(": %u (%u)\n")) % statistic.GetAccepts() % statistic.GetAllTimeAccepts()
1394 + _("On Queue") + CFormat(wxT(": %u\n")) % GetQueuedCount()
1395 + _("Complete sources") + CFormat(wxT(": %u\n")) % m_nCompleteSourcesCount;
1397 // File_checked_for_headers