Upstream tarball 9440
[amule.git] / src / KnownFile.cpp
blob897504be2b00cf5340752a5d908b4c34d6b596a1
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
55 #include "CryptoPP_Inc.h" // Needed for MD4
57 #include <common/Format.h>
59 CFileStatistic::CFileStatistic() :
60 requested(0),
61 transferred(0),
62 accepted(0),
63 alltimerequested(0),
64 alltimetransferred(0),
65 alltimeaccepted(0)
69 #ifndef CLIENT_GUI
71 void CFileStatistic::AddRequest(){
72 requested++;
73 alltimerequested++;
74 theApp->knownfiles->requested++;
75 theApp->sharedfiles->UpdateItem(fileParent);
78 void CFileStatistic::AddAccepted(){
79 accepted++;
80 alltimeaccepted++;
81 theApp->knownfiles->accepted++;
82 theApp->sharedfiles->UpdateItem(fileParent);
85 void CFileStatistic::AddTransferred(uint64 bytes){
86 transferred += bytes;
87 alltimetransferred += bytes;
88 theApp->knownfiles->transferred += bytes;
89 theApp->sharedfiles->UpdateItem(fileParent);
92 #endif // CLIENT_GUI
95 /* Abstract File (base class)*/
97 CAbstractFile::CAbstractFile()
99 m_iRating(0),
100 m_hasComment(false),
101 m_iUserRating(0),
102 m_nFileSize(0)
107 CAbstractFile::CAbstractFile(const CAbstractFile& other)
109 m_abyFileHash(other.m_abyFileHash),
110 m_strComment(other.m_strComment),
111 m_iRating(other.m_iRating),
112 m_hasComment(other.m_hasComment),
113 m_iUserRating(other.m_iUserRating),
114 m_taglist(other.m_taglist),
115 m_nFileSize(other.m_nFileSize),
116 m_fileName(other.m_fileName)
118 /* // TODO: Currently it's not safe to duplicate the entries, but isn't needed either.
119 CKadEntryPtrList::const_iterator it = other.m_kadNotes.begin();
120 for (; it != other.m_kadNotes.end(); ++it) {
121 m_kadNotes.push_back(new Kademlia::CEntry(**it));
127 void CAbstractFile::SetFileName(const CPath& fileName)
129 m_fileName = fileName;
132 uint32 CAbstractFile::GetIntTagValue(uint8 tagname) const
134 ArrayOfCTag::const_iterator it = m_taglist.begin();
135 for (; it != m_taglist.end(); ++it){
136 if (((*it).GetNameID() == tagname) && (*it).IsInt()) {
137 return (*it).GetInt();
140 return 0;
143 bool CAbstractFile::GetIntTagValue(uint8 tagname, uint32& ruValue) const
145 ArrayOfCTag::const_iterator it = m_taglist.begin();
146 for (; it != m_taglist.end(); ++it){
147 if (((*it).GetNameID() == tagname) && (*it).IsInt()){
148 ruValue = (*it).GetInt();
149 return true;
152 return false;
155 uint32 CAbstractFile::GetIntTagValue(const wxString& tagname) const
157 ArrayOfCTag::const_iterator it = m_taglist.begin();
158 for (; it != m_taglist.end(); ++it){
159 if ((*it).IsInt() && ((*it).GetName() == tagname)) {
160 return (*it).GetInt();
163 return 0;
166 const wxString& CAbstractFile::GetStrTagValue(uint8 tagname) const
168 ArrayOfCTag::const_iterator it = m_taglist.begin();
169 for (; it != m_taglist.end(); ++it){
170 if ((*it).GetNameID() == tagname && (*it).IsStr()) {
171 return (*it).GetStr();
174 return EmptyString;
177 const wxString& CAbstractFile::GetStrTagValue(const wxString& tagname) const
179 ArrayOfCTag::const_iterator it = m_taglist.begin();
180 for (; it != m_taglist.end(); ++it){
181 if ((*it).IsStr() && ((*it).GetName() == tagname)) {
182 return (*it).GetStr();
185 return EmptyString;
188 const CTag *CAbstractFile::GetTag(uint8 tagname, uint8 tagtype) const
190 ArrayOfCTag::const_iterator it = m_taglist.begin();
191 for (; it != m_taglist.end(); ++it){
192 if ((*it).GetNameID() == tagname && (*it).GetType() == tagtype) {
193 return &(*it);
196 return NULL;
199 const CTag *CAbstractFile::GetTag(const wxString& tagname, uint8 tagtype) const
201 ArrayOfCTag::const_iterator it = m_taglist.begin();
202 for (; it != m_taglist.end(); ++it){
203 if ((*it).GetType() == tagtype && (*it).GetName() == tagname) {
204 return &(*it);
207 return NULL;
210 const CTag *CAbstractFile::GetTag(uint8 tagname) const
212 ArrayOfCTag::const_iterator it = m_taglist.begin();
213 for (; it != m_taglist.end(); ++it){
214 if ((*it).GetNameID() == tagname) {
215 return &(*it);
218 return NULL;
221 const CTag *CAbstractFile::GetTag(const wxString& tagname) const
223 ArrayOfCTag::const_iterator it = m_taglist.begin();
224 for (; it != m_taglist.end(); ++it){
225 if ((*it).GetName() == tagname) {
226 return &(*it);
229 return NULL;
232 void CAbstractFile::AddTagUnique(const CTag &rTag)
234 ArrayOfCTag::iterator it = m_taglist.begin();
235 for (; it != m_taglist.end(); ++it) {
236 if ( ( ((*it).GetNameID() != 0 &&
237 (*it).GetNameID() == rTag.GetNameID()) ||
238 (!(*it).GetName().IsEmpty() &&
239 !rTag.GetName().IsEmpty() &&
240 (*it).GetName() == rTag.GetName()) ) &&
241 (*it).GetType() == rTag.GetType()){
242 m_taglist.erase(it);
243 m_taglist.insert(it, rTag);
244 return;
247 m_taglist.push_back(rTag);
250 #ifndef CLIENT_GUI
251 void CAbstractFile::AddNote(Kademlia::CEntry *pEntry)
253 CKadEntryPtrList::iterator it = m_kadNotes.begin();
254 for (; it != m_kadNotes.end(); ++it) {
255 Kademlia::CEntry* entry = *it;
256 if(entry->m_uIP == pEntry->m_uIP || entry->m_uSourceID == pEntry->m_uSourceID) {
257 delete pEntry;
258 return;
261 m_kadNotes.push_front(pEntry);
263 #else
264 void CAbstractFile::AddNote(Kademlia::CEntry *)
267 #endif
270 /* Known File */
272 CKnownFile::CKnownFile()
274 Init();
276 m_bAutoUpPriority = thePrefs::GetNewAutoUp();
277 m_iUpPriority = ( m_bAutoUpPriority ) ? PR_HIGH : PR_NORMAL;
281 //#warning Experimental: Construct a CKnownFile from a CSearchFile
282 CKnownFile::CKnownFile(const CSearchFile &searchFile)
284 // This will copy the file hash
285 CAbstractFile(static_cast<const CAbstractFile &>(searchFile))
287 Init();
289 // Use CKnownFile::SetFileName()
290 SetFileName(searchFile.GetFileName());
292 // Use CKnownFile::SetFileSize()
293 SetFileSize(searchFile.GetFileSize());
295 m_bAutoUpPriority = thePrefs::GetNewAutoUp();
296 m_iUpPriority = ( m_bAutoUpPriority ) ? PR_HIGH : PR_NORMAL;
300 void CKnownFile::Init()
302 m_nCompleteSourcesTime = time(NULL);
303 m_nCompleteSourcesCount = 0;
304 m_nCompleteSourcesCountLo = 0;
305 m_nCompleteSourcesCountHi = 0;
306 m_bCommentLoaded = false;
307 m_iPartCount = 0;
308 m_iED2KPartCount = 0;
309 m_iED2KPartHashCount = 0;
310 m_PublishedED2K = false;
311 kadFileSearchID = 0;
312 m_lastPublishTimeKadSrc = 0;
313 m_lastPublishTimeKadNotes = 0;
314 m_lastBuddyIP = 0;
315 m_lastDateChanged = 0;
317 statistic.fileParent = this;
319 #ifndef CLIENT_GUI
320 m_pAICHHashSet = new CAICHHashSet(this);
321 #endif
325 void CKnownFile::SetFileSize(uint64 nFileSize)
327 CAbstractFile::SetFileSize(nFileSize);
328 #ifndef CLIENT_GUI
329 m_pAICHHashSet->SetFileSize(nFileSize);
330 #endif
332 // Examples of parthashs, hashsets and filehashs for different filesizes
333 // according the ed2k protocol
334 //----------------------------------------------------------------------
336 //File size: 3 bytes
337 //File hash: 2D55E87D0E21F49B9AD25F98531F3724
338 //Nr. hashs: 0
341 //File size: 1*PARTSIZE
342 //File hash: A72CA8DF7F07154E217C236C89C17619
343 //Nr. hashs: 2
344 //Hash[ 0]: 4891ED2E5C9C49F442145A3A5F608299
345 //Hash[ 1]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
348 //File size: 1*PARTSIZE + 1 byte
349 //File hash: 2F620AE9D462CBB6A59FE8401D2B3D23
350 //Nr. hashs: 2
351 //Hash[ 0]: 121795F0BEDE02DDC7C5426D0995F53F
352 //Hash[ 1]: C329E527945B8FE75B3C5E8826755747
355 //File size: 2*PARTSIZE
356 //File hash: A54C5E562D5E03CA7D77961EB9A745A4
357 //Nr. hashs: 3
358 //Hash[ 0]: B3F5CE2A06BF403BFB9BFFF68BDDC4D9
359 //Hash[ 1]: 509AA30C9EA8FC136B1159DF2F35B8A9
360 //Hash[ 2]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
363 //File size: 3*PARTSIZE
364 //File hash: 5E249B96F9A46A18FC2489B005BF2667
365 //Nr. hashs: 4
366 //Hash[ 0]: 5319896A2ECAD43BF17E2E3575278E72
367 //Hash[ 1]: D86EF157D5E49C5ED502EDC15BB5F82B
368 //Hash[ 2]: 10F2D5B1FCB95C0840519C58D708480F
369 //Hash[ 3]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
372 //File size: 3*PARTSIZE + 1 byte
373 //File hash: 797ED552F34380CAFF8C958207E40355
374 //Nr. hashs: 4
375 //Hash[ 0]: FC7FD02CCD6987DCF1421F4C0AF94FB8
376 //Hash[ 1]: 2FE466AF8A7C06DA3365317B75A5ACFE
377 //Hash[ 2]: 873D3BF52629F7C1527C6E8E473C1C30
378 //Hash[ 3]: BCE50BEE7877BB07BB6FDA56BFE142FB
381 // File size Data parts ED2K parts ED2K part hashs
382 // ---------------------------------------------------------------
383 // 1..PARTSIZE-1 1 1 0(!)
384 // PARTSIZE 1 2(!) 2(!)
385 // PARTSIZE+1 2 2 2
386 // PARTSIZE*2 2 3(!) 3(!)
387 // PARTSIZE*2+1 3 3 3
389 if (nFileSize == 0){
390 //wxFAIL; // Kry - Why commented out by lemonfan? it can never be 0
391 m_iPartCount = 0;
392 m_iED2KPartCount = 0;
393 m_iED2KPartHashCount = 0;
394 m_sizeLastPart = 0;
395 return;
398 // nr. of data parts
399 m_iPartCount = nFileSize / PARTSIZE + 1;
400 // size of last part
401 m_sizeLastPart = nFileSize % PARTSIZE;
402 // file with size of n * PARTSIZE
403 if (m_sizeLastPart == 0) {
404 m_sizeLastPart = PARTSIZE;
405 m_iPartCount--;
408 // nr. of parts to be used with OP_FILESTATUS
409 m_iED2KPartCount = nFileSize / PARTSIZE + 1;
411 // nr. of parts to be used with OP_HASHSETANSWER
412 m_iED2KPartHashCount = nFileSize / PARTSIZE;
413 if (m_iED2KPartHashCount != 0) {
414 m_iED2KPartHashCount += 1;
419 #ifdef CLIENT_GUI
421 CKnownFile::CKnownFile(CEC_SharedFile_Tag *tag)
423 Init();
425 SetFileName(CPath(tag->FileName()));
426 m_abyFileHash = tag->ID();
427 SetFileSize(tag->SizeFull());
428 m_AvailPartFrequency.insert(m_AvailPartFrequency.end(), m_iPartCount, 0);
429 m_iUpPriority = tag->Prio();
430 if ( m_iUpPriority >= 10 ) {
431 m_iUpPriority-= 10;
432 m_bAutoUpPriority = true;
433 } else {
434 m_bAutoUpPriority = false;
437 m_AICHMasterHash = tag->GetAICHHash();
440 CKnownFile::~CKnownFile()
444 #else // ! CLIENT_GUI
446 CKnownFile::~CKnownFile()
448 SourceSet::iterator it = m_ClientUploadList.begin();
449 for ( ; it != m_ClientUploadList.end(); ++it ) {
450 (*it)->ClearUploadFileID();
453 delete m_pAICHHashSet;
456 void CKnownFile::AddUploadingClient(CUpDownClient* client)
458 m_ClientUploadList.insert(client);
460 UpdateAutoUpPriority();
464 void CKnownFile::RemoveUploadingClient(CUpDownClient* client)
466 if (m_ClientUploadList.erase(client)) {
467 UpdateAutoUpPriority();
472 void CKnownFile::SetFilePath(const CPath& filePath)
474 m_filePath = filePath;
478 // needed for memfiles. its probably better to switch everything to CFile...
479 bool CKnownFile::LoadHashsetFromFile(const CFileDataIO* file, bool checkhash)
481 CMD4Hash checkid = file->ReadHash();
483 uint16 parts = file->ReadUInt16();
484 for (uint16 i = 0; i < parts; ++i){
485 CMD4Hash cur_hash = file->ReadHash();
486 m_hashlist.push_back(cur_hash);
489 // SLUGFILLER: SafeHash - always check for valid m_hashlist
490 if (!checkhash){
491 m_abyFileHash = checkid;
492 if (parts <= 1) { // nothing to check
493 return true;
495 } else {
496 if ( m_abyFileHash != checkid ) {
497 return false; // wrong file?
498 } else {
499 if (parts != GetED2KPartHashCount()) {
500 return false;
504 // SLUGFILLER: SafeHash
506 // trust noone ;-)
507 // lol, useless comment but made me lmao
508 // wtf you guys are weird.
510 if (!m_hashlist.empty()) {
511 CreateHashFromHashlist(m_hashlist, &checkid);
514 if ( m_abyFileHash == checkid ) {
515 return true;
516 } else {
517 m_hashlist.clear();
518 return false;
523 bool CKnownFile::LoadTagsFromFile(const CFileDataIO* file)
525 uint32 tagcount = file->ReadUInt32();
526 for (uint32 j = 0; j != tagcount; ++j) {
527 CTag newtag(*file, true);
528 switch(newtag.GetNameID()){
529 case FT_FILENAME:
530 if (GetFileName().IsOk()) {
531 // Unlike eMule, we actually prefer the second
532 // filename tag, since we use it to specify the
533 // 'universial' filename (see CPath::ToUniv).
534 CPath path = CPath::FromUniv(newtag.GetStr());
536 // May be invalid, if from older versions where
537 // unicoded filenames be saved as empty-strings.
538 if (path.IsOk()) {
539 SetFileName(path);
541 } else {
542 SetFileName(CPath(newtag.GetStr()));
544 break;
546 case FT_FILESIZE:
547 SetFileSize(newtag.GetInt());
548 m_AvailPartFrequency.clear();
549 m_AvailPartFrequency.insert(
550 m_AvailPartFrequency.begin(),
551 GetPartCount(), 0);
552 break;
554 case FT_ATTRANSFERRED:
555 statistic.alltimetransferred += newtag.GetInt();
556 break;
558 case FT_ATTRANSFERREDHI:
559 statistic.alltimetransferred =
560 (((uint64)newtag.GetInt()) << 32) +
561 ((uint64)statistic.alltimetransferred);
562 break;
564 case FT_ATREQUESTED:
565 statistic.alltimerequested = newtag.GetInt();
566 break;
568 case FT_ATACCEPTED:
569 statistic.alltimeaccepted = newtag.GetInt();
570 break;
572 case FT_ULPRIORITY:
573 m_iUpPriority = newtag.GetInt();
574 if( m_iUpPriority == PR_AUTO ){
575 m_iUpPriority = PR_HIGH;
576 m_bAutoUpPriority = true;
577 } else {
578 if ( m_iUpPriority != PR_VERYLOW &&
579 m_iUpPriority != PR_LOW &&
580 m_iUpPriority != PR_NORMAL &&
581 m_iUpPriority != PR_HIGH &&
582 m_iUpPriority != PR_VERYHIGH &&
583 m_iUpPriority != PR_POWERSHARE) {
584 m_iUpPriority = PR_NORMAL;
587 m_bAutoUpPriority = false;
589 break;
591 case FT_PERMISSIONS:
592 // Ignore it, it's not used anymore.
593 break;
595 case FT_AICH_HASH: {
596 CAICHHash hash;
597 bool hashSizeOk =
598 hash.DecodeBase32(newtag.GetStr()) == CAICHHash::GetHashSize();
599 wxASSERT(hashSizeOk);
600 if (hashSizeOk) {
601 m_pAICHHashSet->SetMasterHash(hash, AICH_HASHSETCOMPLETE);
603 break;
606 case FT_KADLASTPUBLISHSRC:
607 SetLastPublishTimeKadSrc( newtag.GetInt(), 0 );
609 if(GetLastPublishTimeKadSrc() > (uint32)time(NULL)+KADEMLIAREPUBLISHTIMES) {
610 //There may be a posibility of an older client that saved a random number here.. This will check for that..
611 SetLastPublishTimeKadSrc(0, 0);
613 break;
615 case FT_KADLASTPUBLISHNOTES:
616 SetLastPublishTimeKadNotes( newtag.GetInt() );
617 break;
619 case FT_KADLASTPUBLISHKEY:
620 // Just purge it
621 wxASSERT( newtag.IsInt() );
622 break;
624 default:
625 // Store them here and write them back on saving.
626 m_taglist.push_back(newtag);
630 return true;
634 bool CKnownFile::LoadDateFromFile(const CFileDataIO* file)
636 m_lastDateChanged = file->ReadUInt32();
638 return true;
642 bool CKnownFile::LoadFromFile(const CFileDataIO* file)
644 // SLUGFILLER: SafeHash - load first, verify later
645 bool ret1 = LoadDateFromFile(file);
646 bool ret2 = LoadHashsetFromFile(file,false);
647 bool ret3 = LoadTagsFromFile(file);
648 UpdatePartsInfo();
649 // Final hash-count verification, needs to be done after the tags are loaded.
650 return ret1 && ret2 && ret3 && GetED2KPartHashCount()==GetHashCount();
651 // SLUGFILLER: SafeHash
655 bool CKnownFile::WriteToFile(CFileDataIO* file)
657 wxCHECK(!IsPartFile(), false);
659 // date
660 file->WriteUInt32(m_lastDateChanged);
661 // hashset
662 file->WriteHash(m_abyFileHash);
664 uint16 parts = m_hashlist.size();
665 file->WriteUInt16(parts);
667 for (int i = 0; i < parts; ++i)
668 file->WriteHash(m_hashlist[i]);
670 //tags
671 const int iFixedTags = 8;
672 uint32 tagcount = iFixedTags;
673 if (HasProperAICHHashSet()) {
674 tagcount++;
676 // Float meta tags are currently not written. All older eMule versions < 0.28a have
677 // a bug in the meta tag reading+writing code. To achive maximum backward
678 // compatibility for met files with older eMule versions we just don't write float
679 // tags. This is OK, because we (eMule) do not use float tags. The only float tags
680 // we may have to handle is the '# Sent' tag from the Hybrid, which is pretty
681 // useless but may be received from us via the servers.
683 // The code for writing the float tags SHOULD BE ENABLED in SOME MONTHS (after most
684 // people are using the newer eMule versions which do not write broken float tags).
685 for (size_t j = 0; j < m_taglist.size(); ++j){
686 if (m_taglist[j].IsInt() || m_taglist[j].IsStr()) {
687 ++tagcount;
691 if (m_lastPublishTimeKadSrc) {
692 ++tagcount;
695 if (m_lastPublishTimeKadNotes){
696 ++tagcount;
699 // standard tags
701 file->WriteUInt32(tagcount);
703 // We still save the unicoded filename, for backwards
704 // compatibility with pre-2.2 and other clients.
705 CTagString nametag_unicode(FT_FILENAME, GetFileName().GetRaw());
706 // We write it with BOM to kep eMule compatibility
707 nametag_unicode.WriteTagToFile(file,utf8strOptBOM);
709 // The non-unicoded filename is written in an 'universial'
710 // format, which allows us to identify files, even if the
711 // system locale changes.
712 CTagString nametag(FT_FILENAME, CPath::ToUniv(GetFileName()));
713 nametag.WriteTagToFile(file);
715 CTagIntSized sizetag(FT_FILESIZE, GetFileSize(), IsLargeFile() ? 64 : 32);
716 sizetag.WriteTagToFile(file);
718 // statistic
719 uint32 tran;
720 tran=statistic.alltimetransferred & 0xFFFFFFFF;
721 CTagInt32 attag1(FT_ATTRANSFERRED, tran);
722 attag1.WriteTagToFile(file);
724 tran=statistic.alltimetransferred>>32;
725 CTagInt32 attag4(FT_ATTRANSFERREDHI, tran);
726 attag4.WriteTagToFile(file);
728 CTagInt32 attag2(FT_ATREQUESTED, statistic.GetAllTimeRequests());
729 attag2.WriteTagToFile(file);
731 CTagInt32 attag3(FT_ATACCEPTED, statistic.GetAllTimeAccepts());
732 attag3.WriteTagToFile(file);
734 // priority N permission
735 CTagInt32 priotag(FT_ULPRIORITY, IsAutoUpPriority() ? PR_AUTO : m_iUpPriority);
736 priotag.WriteTagToFile(file);
738 //AICH Filehash
739 if (HasProperAICHHashSet()) {
740 CTagString aichtag(FT_AICH_HASH, m_pAICHHashSet->GetMasterHash().GetString());
741 aichtag.WriteTagToFile(file);
744 // Kad sources
745 if (m_lastPublishTimeKadSrc){
746 CTagInt32 kadLastPubSrc(FT_KADLASTPUBLISHSRC, m_lastPublishTimeKadSrc);
747 kadLastPubSrc.WriteTagToFile(file);
750 // Kad notes
751 if (m_lastPublishTimeKadNotes){
752 CTagInt32 kadLastPubNotes(FT_KADLASTPUBLISHNOTES, m_lastPublishTimeKadNotes);
753 kadLastPubNotes.WriteTagToFile(file);
756 //other tags
757 for (size_t j = 0; j < m_taglist.size(); ++j){
758 if (m_taglist[j].IsInt() || m_taglist[j].IsStr()) {
759 m_taglist[j].WriteTagToFile(file);
762 return true;
766 void CKnownFile::CreateHashFromHashlist(const ArrayOfCMD4Hash& hashes, CMD4Hash* Output)
768 wxCHECK_RET(hashes.size(), wxT("No input to hash from in CreateHashFromHashlist"));
770 std::vector<byte> buffer(hashes.size() * MD4HASH_LENGTH);
771 std::vector<byte>::iterator it = buffer.begin();
773 for (size_t i = 0; i < hashes.size(); ++i) {
774 it = STLCopy_n(hashes[i].GetHash(), MD4HASH_LENGTH, it);
777 CreateHashFromInput(&buffer[0], buffer.size(), Output, NULL);
781 void CKnownFile::CreateHashFromFile(CFile& file, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut)
783 wxCHECK_RET(Length, wxT("No input to hash from in CreateHashFromFile"));
785 CFileArea area;
786 area.Read(file, Length);
788 CreateHashFromInput(area.GetBuffer(), Length, Output, pShaHashOut);
792 void CKnownFile::CreateHashFromInput(const byte* input, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut )
794 wxASSERT_MSG(Output || pShaHashOut, wxT("Nothing to do in CreateHashFromInput"));
795 wxCHECK_RET(input, wxT("No input to hash from in CreateHashFromInput"));
796 wxASSERT(Length <= PARTSIZE); // We never hash more than one PARTSIZE
798 CMemFile data(input, Length);
800 uint32 Required = Length;
801 byte X[64*128];
803 uint32 posCurrentEMBlock = 0;
804 uint32 nIACHPos = 0;
805 CScopedPtr<CAICHHashAlgo> pHashAlg(CAICHHashSet::GetNewHashAlgo());
807 // This is all AICH.
808 while (Required >= 64) {
809 uint32 len = Required / 64;
810 if (len > sizeof(X)/(64 * sizeof(X[0]))) {
811 len = sizeof(X)/(64 * sizeof(X[0]));
814 data.Read(&X, len * 64);
816 // SHA hash needs 180KB blocks
817 if (pShaHashOut) {
818 if (nIACHPos + len*64 >= EMBLOCKSIZE) {
819 uint32 nToComplete = EMBLOCKSIZE - nIACHPos;
820 pHashAlg->Add(X, nToComplete);
821 wxASSERT( nIACHPos + nToComplete == EMBLOCKSIZE );
822 pShaHashOut->SetBlockHash(EMBLOCKSIZE, posCurrentEMBlock, pHashAlg.get());
823 posCurrentEMBlock += EMBLOCKSIZE;
824 pHashAlg->Reset();
825 pHashAlg->Add(X+nToComplete,(len*64) - nToComplete);
826 nIACHPos = (len*64) - nToComplete;
828 else{
829 pHashAlg->Add(X, len*64);
830 nIACHPos += len*64;
834 Required -= len*64;
836 // bytes to read
837 Required = Length % 64;
838 if (Required != 0){
839 data.Read(&X,Required);
841 if (pShaHashOut != NULL){
842 if (nIACHPos + Required >= EMBLOCKSIZE){
843 uint32 nToComplete = EMBLOCKSIZE - nIACHPos;
844 pHashAlg->Add(X, nToComplete);
845 wxASSERT( nIACHPos + nToComplete == EMBLOCKSIZE );
846 pShaHashOut->SetBlockHash(EMBLOCKSIZE, posCurrentEMBlock, pHashAlg.get());
847 posCurrentEMBlock += EMBLOCKSIZE;
848 pHashAlg->Reset();
849 pHashAlg->Add(X+nToComplete, Required - nToComplete);
850 nIACHPos = Required - nToComplete;
852 else{
853 pHashAlg->Add(X, Required);
854 nIACHPos += Required;
858 if (pShaHashOut != NULL){
859 if(nIACHPos > 0){
860 pShaHashOut->SetBlockHash(nIACHPos, posCurrentEMBlock, pHashAlg.get());
861 posCurrentEMBlock += nIACHPos;
863 wxASSERT( posCurrentEMBlock == Length );
864 wxCHECK2( pShaHashOut->ReCalculateHash(pHashAlg.get(), false), );
867 if (Output != NULL){
868 #ifdef __WEAK_CRYPTO__
869 CryptoPP::Weak::MD4 md4_hasher;
870 #else
871 CryptoPP::MD4 md4_hasher;
872 #endif
873 md4_hasher.CalculateDigest(Output->GetHash(), input, Length);
878 const CMD4Hash& CKnownFile::GetPartHash(uint16 part) const {
879 wxASSERT( part < m_hashlist.size() );
881 return m_hashlist[part];
884 CPacket* CKnownFile::CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions)
886 // Kad reviewed
888 if (m_ClientUploadList.empty()) {
889 return NULL;
892 if ((((CKnownFile*)forClient->GetRequestFile() != this)
893 && ((CKnownFile*)forClient->GetUploadFile() != this)) || forClient->GetUploadFileID() != GetFileHash()) {
894 wxString file1 = _("Unknown");
895 if (forClient->GetRequestFile() && forClient->GetRequestFile()->GetFileName().IsOk()) {
896 file1 = forClient->GetRequestFile()->GetFileName().GetPrintable();
897 } else if (forClient->GetUploadFile() && forClient->GetUploadFile()->GetFileName().IsOk()) {
898 file1 = forClient->GetUploadFile()->GetFileName().GetPrintable();
900 wxString file2 = _("Unknown");
901 if (GetFileName().IsOk()) {
902 file2 = GetFileName().GetPrintable();
904 AddDebugLogLineM(false, logKnownFiles, wxT("File missmatch on source packet (K) Sending: ") + file1 + wxT(" From: ") + file2);
905 return NULL;
908 const BitVector& rcvstatus = forClient->GetUpPartStatus();
909 bool SupportsUploadChunksState = !rcvstatus.empty();
910 //wxASSERT(rcvstatus.size() == GetPartCount()); // Obviously!
911 if (rcvstatus.size() != GetPartCount()) {
912 // Yuck. Same file but different part count? Seriously fucked up.
913 AddDebugLogLineM(false, logKnownFiles, wxString::Format(wxT("Impossible situation: different partcounts for the same known file: %i (client) and %i (file)"),rcvstatus.size(),GetPartCount()));
914 return NULL;
917 CMemFile data(1024);
919 uint8 byUsedVersion;
920 bool bIsSX2Packet;
921 if (forClient->SupportsSourceExchange2() && byRequestedVersion > 0){
922 // the client uses SourceExchange2 and requested the highest version he knows
923 // and we send the highest version we know, but of course not higher than his request
924 byUsedVersion = std::min(byRequestedVersion, (uint8)SOURCEEXCHANGE2_VERSION);
925 bIsSX2Packet = true;
926 data.WriteUInt8(byUsedVersion);
928 // we don't support any special SX2 options yet, reserved for later use
929 if (nRequestedOptions != 0) {
930 AddDebugLogLineM(false, logKnownFiles, CFormat(wxT("Client requested unknown options for SourceExchange2: %u")) % nRequestedOptions);
932 } else {
933 byUsedVersion = forClient->GetSourceExchange1Version();
934 bIsSX2Packet = false;
935 if (forClient->SupportsSourceExchange2()) {
936 AddDebugLogLineM(false, logKnownFiles, wxT("Client which announced to support SX2 sent SX1 packet instead"));
940 uint16 nCount = 0;
942 data.WriteHash(forClient->GetUploadFileID());
943 data.WriteUInt16(nCount);
944 uint32 cDbgNoSrc = 0;
946 SourceSet::iterator it = m_ClientUploadList.begin();
947 for ( ; it != m_ClientUploadList.end(); it++ ) {
948 const CUpDownClient *cur_src = *it;
950 if ( cur_src->HasLowID() ||
951 cur_src == forClient ||
952 !( cur_src->GetUploadState() == US_UPLOADING ||
953 cur_src->GetUploadState() == US_ONUPLOADQUEUE)) {
954 continue;
957 bool bNeeded = false;
959 if ( SupportsUploadChunksState ) {
960 const BitVector& srcstatus = cur_src->GetUpPartStatus();
961 if ( !srcstatus.empty() ) {
962 //wxASSERT(srcstatus.size() == GetPartCount()); // Obviously!
963 if (srcstatus.size() != GetPartCount()) {
964 continue;
966 if ( cur_src->GetUpPartCount() == forClient->GetUpPartCount() ) {
967 for (int x = 0; x < GetPartCount(); x++ ) {
968 if ( srcstatus.at(x) && !rcvstatus.at(x) ) {
969 // We know the receiving client needs
970 // a chunk from this client.
971 bNeeded = true;
972 break;
976 } else {
977 cDbgNoSrc++;
978 // This client doesn't support upload chunk status.
979 // So just send it and hope for the best.
980 bNeeded = true;
982 } else {
983 // remote client does not support upload chunk status,
984 // search sources which have at least one complete part
985 // we could even sort the list of sources by available
986 // chunks to return as much sources as possible which
987 // have the most available chunks. but this could be
988 // a noticeable performance problem.
989 const BitVector& srcstatus = cur_src->GetUpPartStatus();
990 if ( !srcstatus.empty() ) {
991 //wxASSERT(srcstatus.size() == GetPartCount());
992 if (srcstatus.size() != GetPartCount()) {
993 continue;
995 for (int x = 0; x < GetPartCount(); x++ ) {
996 if ( srcstatus.at(x) ) {
997 // this client has at least one chunk
998 bNeeded = true;
999 break;
1002 } else {
1003 // This client doesn't support upload chunk status.
1004 // So just send it and hope for the best.
1005 bNeeded = true;
1009 if ( bNeeded ) {
1010 nCount++;
1011 uint32 dwID;
1012 if(byUsedVersion >= 3) {
1013 dwID = cur_src->GetUserIDHybrid();
1014 } else {
1015 dwID = cur_src->GetIP();
1017 data.WriteUInt32(dwID);
1018 data.WriteUInt16(cur_src->GetUserPort());
1019 data.WriteUInt32(cur_src->GetServerIP());
1020 data.WriteUInt16(cur_src->GetServerPort());
1022 if (byUsedVersion >= 2) {
1023 data.WriteHash(cur_src->GetUserHash());
1026 if (byUsedVersion >= 4){
1027 // CryptSettings - SourceExchange V4
1028 // 5 Reserved (!)
1029 // 1 CryptLayer Required
1030 // 1 CryptLayer Requested
1031 // 1 CryptLayer Supported
1032 const uint8 uSupportsCryptLayer = cur_src->SupportsCryptLayer() ? 1 : 0;
1033 const uint8 uRequestsCryptLayer = cur_src->RequestsCryptLayer() ? 1 : 0;
1034 const uint8 uRequiresCryptLayer = cur_src->RequiresCryptLayer() ? 1 : 0;
1035 const uint8 byCryptOptions = (uRequiresCryptLayer << 2) | (uRequestsCryptLayer << 1) | (uSupportsCryptLayer << 0);
1036 data.WriteUInt8(byCryptOptions);
1039 if (nCount > 500) {
1040 break;
1045 if (!nCount) {
1046 return 0;
1049 data.Seek(bIsSX2Packet ? 17 : 16, wxFromStart);
1050 data.WriteUInt16(nCount);
1052 CPacket* result = new CPacket(data, OP_EMULEPROT, bIsSX2Packet ? OP_ANSWERSOURCES2 : OP_ANSWERSOURCES);
1054 if ( result->GetPacketSize() > 354 ) {
1055 result->PackPacket();
1058 return result;
1062 // Updates priority of file if autopriority is activated
1063 void CKnownFile::UpdateAutoUpPriority()
1065 if (IsAutoUpPriority()) {
1066 uint32 queued = GetQueuedCount();
1067 uint8 priority = PR_NORMAL;
1069 if (queued > 20) {
1070 priority = PR_LOW;
1071 } else if (queued > 1) {
1072 priority = PR_NORMAL;
1073 } else {
1074 priority = PR_HIGH;
1077 if (GetUpPriority() != priority) {
1078 SetUpPriority(priority, false);
1079 Notify_SharedFilesUpdateItem(this);
1084 void CKnownFile::SetFileComment(const wxString& strNewComment)
1086 if (m_strComment != strNewComment) {
1087 SetLastPublishTimeKadNotes(0);
1088 wxString strCfgPath = wxT("/") + m_abyFileHash.Encode() + wxT("/");
1090 wxConfigBase* cfg = wxConfigBase::Get();
1091 cfg->Write( strCfgPath + wxT("Comment"), strNewComment);
1093 m_strComment = strNewComment;
1095 SourceSet::iterator it = m_ClientUploadList.begin();
1096 for ( ; it != m_ClientUploadList.end(); it++ ) {
1097 (*it)->SetCommentDirty();
1103 // For File rate
1104 void CKnownFile::SetFileRating(int8 iNewRating)
1106 if (m_iRating != iNewRating) {
1107 SetLastPublishTimeKadNotes(0);
1108 wxString strCfgPath = wxT("/") + m_abyFileHash.Encode() + wxT("/");
1109 wxConfigBase* cfg = wxConfigBase::Get();
1110 cfg->Write( strCfgPath + wxT("Rate"), (int)iNewRating);
1111 m_iRating = iNewRating;
1113 SourceSet::iterator it = m_ClientUploadList.begin();
1114 for ( ; it != m_ClientUploadList.end(); it++ ) {
1115 (*it)->SetCommentDirty();
1121 void CKnownFile::SetUpPriority(uint8 iNewUpPriority, bool m_bsave){
1122 m_iUpPriority = iNewUpPriority;
1123 if( IsPartFile() && m_bsave ) {
1124 ((CPartFile*)this)->SavePartFile();
1128 void CKnownFile::SetPublishedED2K(bool val){
1129 m_PublishedED2K = val;
1130 Notify_SharedFilesUpdateItem(this);
1133 bool CKnownFile::PublishNotes()
1135 if(m_lastPublishTimeKadNotes > (uint32)time(NULL)) {
1136 return false;
1139 if(!GetFileComment().IsEmpty()) {
1140 m_lastPublishTimeKadNotes = (uint32)time(NULL)+KADEMLIAREPUBLISHTIMEN;
1141 return true;
1144 if(GetFileRating() != 0) {
1145 m_lastPublishTimeKadNotes = (uint32)time(NULL)+KADEMLIAREPUBLISHTIMEN;
1146 return true;
1149 return false;
1152 bool CKnownFile::PublishSrc()
1154 uint32 lastBuddyIP = 0;
1156 if( theApp->IsFirewalled() ) {
1157 CUpDownClient* buddy = theApp->clientlist->GetBuddy();
1158 if( buddy ) {
1159 lastBuddyIP = theApp->clientlist->GetBuddy()->GetIP();
1160 if( lastBuddyIP != m_lastBuddyIP ) {
1161 SetLastPublishTimeKadSrc( (uint32)time(NULL)+KADEMLIAREPUBLISHTIMES, lastBuddyIP );
1162 return true;
1164 } else {
1165 return false;
1169 if(m_lastPublishTimeKadSrc > (uint32)time(NULL)) {
1170 return false;
1173 SetLastPublishTimeKadSrc((uint32)time(NULL)+KADEMLIAREPUBLISHTIMES,lastBuddyIP);
1174 return true;
1178 void CKnownFile::UpdatePartsInfo()
1180 // Cache part count
1181 uint16 partcount = GetPartCount();
1182 bool flag = (time(NULL) - m_nCompleteSourcesTime > 0);
1184 // Ensure the frequency-list is ready
1185 if ( m_AvailPartFrequency.size() != GetPartCount() ) {
1186 m_AvailPartFrequency.clear();
1187 m_AvailPartFrequency.insert(m_AvailPartFrequency.begin(), GetPartCount(), 0);
1190 if (flag) {
1191 ArrayOfUInts16 count;
1192 count.reserve(m_ClientUploadList.size());
1194 SourceSet::iterator it = m_ClientUploadList.begin();
1195 for ( ; it != m_ClientUploadList.end(); it++ ) {
1196 if ( !(*it)->GetUpPartStatus().empty() && (*it)->GetUpPartCount() == partcount ) {
1197 count.push_back((*it)->GetUpCompleteSourcesCount());
1201 m_nCompleteSourcesCount = m_nCompleteSourcesCountLo = m_nCompleteSourcesCountHi = 0;
1203 if( partcount > 0) {
1204 m_nCompleteSourcesCount = m_AvailPartFrequency[0];
1206 for (uint16 i = 1; i < partcount; ++i) {
1207 if( m_nCompleteSourcesCount > m_AvailPartFrequency[i]) {
1208 m_nCompleteSourcesCount = m_AvailPartFrequency[i];
1211 count.push_back(m_nCompleteSourcesCount);
1213 int32 n = count.size();
1214 if (n > 0) {
1215 std::sort(count.begin(), count.end(), std::less<uint16>());
1217 // calculate range
1218 int i = n >> 1; // (n / 2)
1219 int j = (n * 3) >> 2; // (n * 3) / 4
1220 int k = (n * 7) >> 3; // (n * 7) / 8
1222 // For complete files, trust the people your uploading to more...
1224 // For low guess and normal guess count
1225 // - If we see more sources then the guessed low and
1226 // normal, use what we see.
1227 // - If we see less sources then the guessed low,
1228 // adjust network accounts for 100%, we account for
1229 // 0% with what we see and make sure we are still
1230 // above the normal.
1231 // For high guess
1232 // Adjust 100% network and 0% what we see.
1233 if (n < 20) {
1234 if ( count[i] < m_nCompleteSourcesCount ) {
1235 m_nCompleteSourcesCountLo = m_nCompleteSourcesCount;
1236 } else {
1237 m_nCompleteSourcesCountLo = count[i];
1239 m_nCompleteSourcesCount= m_nCompleteSourcesCountLo;
1240 m_nCompleteSourcesCountHi = count[j];
1241 if( m_nCompleteSourcesCountHi < m_nCompleteSourcesCount ) {
1242 m_nCompleteSourcesCountHi = m_nCompleteSourcesCount;
1244 } else {
1245 // Many sources..
1246 // For low guess
1247 // Use what we see.
1248 // For normal guess
1249 // Adjust network accounts for 100%, we account for
1250 // 0% with what we see and make sure we are still above the low.
1251 // For high guess
1252 // Adjust network accounts for 100%, we account for 0%
1253 // with what we see and make sure we are still above the normal.
1255 m_nCompleteSourcesCountLo = m_nCompleteSourcesCount;
1256 m_nCompleteSourcesCount = count[j];
1257 if( m_nCompleteSourcesCount < m_nCompleteSourcesCountLo ) {
1258 m_nCompleteSourcesCount = m_nCompleteSourcesCountLo;
1260 m_nCompleteSourcesCountHi= count[k];
1261 if( m_nCompleteSourcesCountHi < m_nCompleteSourcesCount ) {
1262 m_nCompleteSourcesCountHi = m_nCompleteSourcesCount;
1266 m_nCompleteSourcesTime = time(NULL) + (60);
1269 Notify_SharedFilesUpdateItem(this);
1273 void CKnownFile::UpdateUpPartsFrequency( CUpDownClient* client, bool increment )
1275 if ( m_AvailPartFrequency.size() != GetPartCount() ) {
1276 m_AvailPartFrequency.clear();
1277 m_AvailPartFrequency.insert(m_AvailPartFrequency.begin(), GetPartCount(), 0);
1278 if ( !increment ) {
1279 return;
1283 const BitVector& freq = client->GetUpPartStatus();
1284 unsigned int size = freq.size();
1285 if ( size != m_AvailPartFrequency.size() ) {
1286 return;
1289 if ( increment ) {
1290 for ( unsigned int i = 0; i < size; ++i ) {
1291 if ( freq[i] ) {
1292 m_AvailPartFrequency[i]++;
1295 } else {
1296 for ( unsigned int i = 0; i < size; ++i ) {
1297 if ( freq[i] ) {
1298 m_AvailPartFrequency[i]--;
1304 void CKnownFile::ClearPriority() {
1305 if ( !m_bAutoUpPriority ) return;
1306 m_iUpPriority = ( m_bAutoUpPriority ) ? PR_HIGH : PR_NORMAL;
1307 UpdateAutoUpPriority();
1310 void CKnownFile::SetFileName(const CPath& filename)
1312 CAbstractFile::SetFileName(filename);
1313 #ifndef CLIENT_GUI
1314 wordlist.clear();
1315 Kademlia::CSearchManager::GetWords(GetFileName().GetPrintable(), &wordlist);
1316 #endif
1319 #endif // CLIENT_GUI
1321 //For File Comment //
1322 void CKnownFile::LoadComment()
1324 #ifndef CLIENT_GUI
1325 wxString strCfgPath = wxT("/") + m_abyFileHash.Encode() + wxT("/");
1327 wxConfigBase* cfg = wxConfigBase::Get();
1329 m_strComment = cfg->Read( strCfgPath + wxT("Comment"), wxEmptyString);
1330 m_iRating = cfg->Read( strCfgPath + wxT("Rate"), 0l);
1331 m_bCommentLoaded = true;
1333 #else
1334 m_strComment = wxT("Comments are not allowed on remote gui yet");
1335 m_bCommentLoaded = true;
1336 m_iRating =0;
1337 #endif
1342 wxString CKnownFile::GetAICHMasterHash() const
1344 #ifdef CLIENT_GUI
1345 return m_AICHMasterHash;
1346 #else
1347 if (HasProperAICHHashSet()) {
1348 return m_pAICHHashSet->GetMasterHash().GetString();
1351 return wxEmptyString;
1352 #endif
1356 bool CKnownFile::HasProperAICHHashSet() const
1358 #ifdef CLIENT_GUI
1359 return m_AICHMasterHash.Length() != 0;
1360 #else
1361 return m_pAICHHashSet->HasValidMasterHash() &&
1362 (m_pAICHHashSet->GetStatus() == AICH_HASHSETCOMPLETE ||
1363 m_pAICHHashSet->GetStatus() == AICH_VERIFIED);
1364 #endif
1367 wxString CKnownFile::GetFeedback() const
1369 return wxString(_("File name")) + wxT(": ") + GetFileName().GetPrintable() + wxT("\n")
1370 + _("File size") + wxT(": ") + CastItoXBytes(GetFileSize()) + wxT("\n")
1371 + _("Share ratio") + wxString::Format(wxT(": %.2f%%\n"), (((double)statistic.GetAllTimeTransferred() / (double)GetFileSize()) * 100.0))
1372 + _("Uploaded") + wxT(": ") + CastItoXBytes(statistic.GetTransferred()) + wxT(" (") + CastItoXBytes(statistic.GetAllTimeTransferred()) + wxT(")\n")
1373 + _("Requested") + CFormat(wxT(": %u (%u)\n")) % statistic.GetRequests() % statistic.GetAllTimeRequests()
1374 + _("Accepted") + CFormat(wxT(": %u (%u)\n")) % statistic.GetAccepts() % statistic.GetAllTimeAccepts()
1375 + _("On Queue") + CFormat(wxT(": %u\n")) % GetQueuedCount()
1376 + _("Complete sources") + CFormat(wxT(": %u\n")) % m_nCompleteSourcesCount;
1378 // File_checked_for_headers