2 // This file is part of the aMule Project.
4 // Parts of this file are based on work from pan One (http://home-3.tiscali.nl/~meost/pms/)
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 )
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.
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)
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() :
65 alltimetransferred(0),
72 void CFileStatistic::AddRequest(){
75 theApp
->knownfiles
->requested
++;
76 theApp
->sharedfiles
->UpdateItem(fileParent
);
79 void CFileStatistic::AddAccepted(){
82 theApp
->knownfiles
->accepted
++;
83 theApp
->sharedfiles
->UpdateItem(fileParent
);
86 void CFileStatistic::AddTransferred(uint64 bytes
){
88 alltimetransferred
+= bytes
;
89 theApp
->knownfiles
->transferred
+= bytes
;
90 theApp
->sharedfiles
->UpdateItem(fileParent
);
96 /* Abstract File (base class)*/
98 CAbstractFile::CAbstractFile()
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();
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();
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();
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();
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();
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
) {
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
) {
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
) {
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
) {
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()){
244 m_taglist
.insert(it
, rTag
);
248 m_taglist
.push_back(rTag
);
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
) {
262 m_kadNotes
.push_front(pEntry
);
265 void CAbstractFile::AddNote(Kademlia::CEntry
*)
273 CKnownFile::CKnownFile()
278 CKnownFile::CKnownFile(uint32 ecid
) : CECID(ecid
)
284 //#warning Experimental: Construct a CKnownFile from a CSearchFile
285 CKnownFile::CKnownFile(const CSearchFile
&searchFile
)
287 // This will copy the file hash
288 CAbstractFile(static_cast<const CAbstractFile
&>(searchFile
))
292 // Use CKnownFile::SetFileName()
293 SetFileName(searchFile
.GetFileName());
295 // Use CKnownFile::SetFileSize()
296 SetFileSize(searchFile
.GetFileSize());
300 void CKnownFile::Init()
302 m_showSources
= false;
303 m_nCompleteSourcesTime
= time(NULL
);
304 m_nCompleteSourcesCount
= 0;
305 m_nCompleteSourcesCountLo
= 0;
306 m_nCompleteSourcesCountHi
= 0;
307 m_bCommentLoaded
= false;
309 m_iED2KPartCount
= 0;
310 m_iED2KPartHashCount
= 0;
311 m_PublishedED2K
= false;
313 m_lastPublishTimeKadSrc
= 0;
314 m_lastPublishTimeKadNotes
= 0;
316 m_lastDateChanged
= 0;
317 m_bAutoUpPriority
= thePrefs::GetNewAutoUp();
318 m_iUpPriority
= ( m_bAutoUpPriority
) ? PR_HIGH
: PR_NORMAL
;
320 statistic
.fileParent
= this;
323 m_pAICHHashSet
= new CAICHHashSet(this);
328 void CKnownFile::SetFileSize(uint64 nFileSize
)
330 CAbstractFile::SetFileSize(nFileSize
);
332 m_pAICHHashSet
->SetFileSize(nFileSize
);
335 // Examples of parthashs, hashsets and filehashs for different filesizes
336 // according the ed2k protocol
337 //----------------------------------------------------------------------
340 //File hash: 2D55E87D0E21F49B9AD25F98531F3724
344 //File size: 1*PARTSIZE
345 //File hash: A72CA8DF7F07154E217C236C89C17619
347 //Hash[ 0]: 4891ED2E5C9C49F442145A3A5F608299
348 //Hash[ 1]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
351 //File size: 1*PARTSIZE + 1 byte
352 //File hash: 2F620AE9D462CBB6A59FE8401D2B3D23
354 //Hash[ 0]: 121795F0BEDE02DDC7C5426D0995F53F
355 //Hash[ 1]: C329E527945B8FE75B3C5E8826755747
358 //File size: 2*PARTSIZE
359 //File hash: A54C5E562D5E03CA7D77961EB9A745A4
361 //Hash[ 0]: B3F5CE2A06BF403BFB9BFFF68BDDC4D9
362 //Hash[ 1]: 509AA30C9EA8FC136B1159DF2F35B8A9
363 //Hash[ 2]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
366 //File size: 3*PARTSIZE
367 //File hash: 5E249B96F9A46A18FC2489B005BF2667
369 //Hash[ 0]: 5319896A2ECAD43BF17E2E3575278E72
370 //Hash[ 1]: D86EF157D5E49C5ED502EDC15BB5F82B
371 //Hash[ 2]: 10F2D5B1FCB95C0840519C58D708480F
372 //Hash[ 3]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
375 //File size: 3*PARTSIZE + 1 byte
376 //File hash: 797ED552F34380CAFF8C958207E40355
378 //Hash[ 0]: FC7FD02CCD6987DCF1421F4C0AF94FB8
379 //Hash[ 1]: 2FE466AF8A7C06DA3365317B75A5ACFE
380 //Hash[ 2]: 873D3BF52629F7C1527C6E8E473C1C30
381 //Hash[ 3]: BCE50BEE7877BB07BB6FDA56BFE142FB
384 // File size Data parts ED2K parts ED2K part hashs
385 // ---------------------------------------------------------------
386 // 1..PARTSIZE-1 1 1 0(!)
387 // PARTSIZE 1 2(!) 2(!)
389 // PARTSIZE*2 2 3(!) 3(!)
390 // PARTSIZE*2+1 3 3 3
393 //wxFAIL; // Kry - Why commented out by lemonfan? it can never be 0
395 m_iED2KPartCount
= 0;
396 m_iED2KPartHashCount
= 0;
402 m_iPartCount
= nFileSize
/ PARTSIZE
+ 1;
404 m_sizeLastPart
= nFileSize
% PARTSIZE
;
405 // file with size of n * PARTSIZE
406 if (m_sizeLastPart
== 0) {
407 m_sizeLastPart
= PARTSIZE
;
411 // nr. of parts to be used with OP_FILESTATUS
412 m_iED2KPartCount
= nFileSize
/ PARTSIZE
+ 1;
414 // nr. of parts to be used with OP_HASHSETANSWER
415 m_iED2KPartHashCount
= nFileSize
/ PARTSIZE
;
416 if (m_iED2KPartHashCount
!= 0) {
417 m_iED2KPartHashCount
+= 1;
422 void CKnownFile::AddUploadingClient(CUpDownClient
* client
)
424 m_ClientUploadList
.insert(client
);
426 SourceItemType type
= UNAVAILABLE_SOURCE
;
427 switch (client
->GetUploadState()) {
429 case US_ONUPLOADQUEUE
:
430 type
= AVAILABLE_SOURCE
;
433 // Any other state is UNAVAILABLE_SOURCE by default.
437 Notify_SharedCtrlAddClient(this, client
, type
);
439 UpdateAutoUpPriority();
443 void CKnownFile::RemoveUploadingClient(CUpDownClient
* client
)
445 if (m_ClientUploadList
.erase(client
)) {
446 Notify_SharedCtrlRemoveClient(this, client
);
447 UpdateAutoUpPriority();
454 CKnownFile::CKnownFile(CEC_SharedFile_Tag
*tag
) : CECID(tag
->ID())
458 m_abyFileHash
= tag
->FileHash();
459 SetFileSize(tag
->SizeFull());
460 m_AvailPartFrequency
.insert(m_AvailPartFrequency
.end(), m_iPartCount
, 0);
464 CKnownFile::~CKnownFile()
468 void CKnownFile::UpdateAutoUpPriority()
473 #else // ! CLIENT_GUI
475 CKnownFile::~CKnownFile()
477 SourceSet::iterator it
= m_ClientUploadList
.begin();
478 for ( ; it
!= m_ClientUploadList
.end(); ++it
) {
479 (*it
)->ClearUploadFileID();
482 delete m_pAICHHashSet
;
486 void CKnownFile::SetFilePath(const CPath
& filePath
)
488 m_filePath
= filePath
;
492 // needed for memfiles. its probably better to switch everything to CFile...
493 bool CKnownFile::LoadHashsetFromFile(const CFileDataIO
* file
, bool checkhash
)
495 CMD4Hash checkid
= file
->ReadHash();
497 uint16 parts
= file
->ReadUInt16();
498 for (uint16 i
= 0; i
< parts
; ++i
){
499 CMD4Hash cur_hash
= file
->ReadHash();
500 m_hashlist
.push_back(cur_hash
);
503 // SLUGFILLER: SafeHash - always check for valid m_hashlist
505 m_abyFileHash
= checkid
;
506 if (parts
<= 1) { // nothing to check
510 if ( m_abyFileHash
!= checkid
) {
511 return false; // wrong file?
513 if (parts
!= GetED2KPartHashCount()) {
518 // SLUGFILLER: SafeHash
521 // lol, useless comment but made me lmao
522 // wtf you guys are weird.
524 if (!m_hashlist
.empty()) {
525 CreateHashFromHashlist(m_hashlist
, &checkid
);
528 if ( m_abyFileHash
== checkid
) {
537 bool CKnownFile::LoadTagsFromFile(const CFileDataIO
* file
)
539 uint32 tagcount
= file
->ReadUInt32();
540 for (uint32 j
= 0; j
!= tagcount
; ++j
) {
541 CTag
newtag(*file
, true);
542 switch(newtag
.GetNameID()){
544 if (GetFileName().IsOk()) {
545 // Unlike eMule, we actually prefer the second
546 // filename tag, since we use it to specify the
547 // 'universial' filename (see CPath::ToUniv).
548 CPath path
= CPath::FromUniv(newtag
.GetStr());
550 // May be invalid, if from older versions where
551 // unicoded filenames be saved as empty-strings.
556 SetFileName(CPath(newtag
.GetStr()));
561 SetFileSize(newtag
.GetInt());
562 m_AvailPartFrequency
.clear();
563 m_AvailPartFrequency
.insert(
564 m_AvailPartFrequency
.begin(),
568 case FT_ATTRANSFERRED
:
569 statistic
.alltimetransferred
+= newtag
.GetInt();
572 case FT_ATTRANSFERREDHI
:
573 statistic
.alltimetransferred
=
574 (((uint64
)newtag
.GetInt()) << 32) +
575 ((uint64
)statistic
.alltimetransferred
);
579 statistic
.alltimerequested
= newtag
.GetInt();
583 statistic
.alltimeaccepted
= newtag
.GetInt();
587 m_iUpPriority
= newtag
.GetInt();
588 if( m_iUpPriority
== PR_AUTO
){
589 m_iUpPriority
= PR_HIGH
;
590 m_bAutoUpPriority
= true;
592 if ( m_iUpPriority
!= PR_VERYLOW
&&
593 m_iUpPriority
!= PR_LOW
&&
594 m_iUpPriority
!= PR_NORMAL
&&
595 m_iUpPriority
!= PR_HIGH
&&
596 m_iUpPriority
!= PR_VERYHIGH
&&
597 m_iUpPriority
!= PR_POWERSHARE
) {
598 m_iUpPriority
= PR_NORMAL
;
601 m_bAutoUpPriority
= false;
606 // Ignore it, it's not used anymore.
612 hash
.DecodeBase32(newtag
.GetStr()) == CAICHHash::GetHashSize();
613 wxASSERT(hashSizeOk
);
615 m_pAICHHashSet
->SetMasterHash(hash
, AICH_HASHSETCOMPLETE
);
620 case FT_KADLASTPUBLISHSRC
:
621 SetLastPublishTimeKadSrc( newtag
.GetInt(), 0 );
623 if(GetLastPublishTimeKadSrc() > (uint32
)time(NULL
)+KADEMLIAREPUBLISHTIMES
) {
624 //There may be a posibility of an older client that saved a random number here.. This will check for that..
625 SetLastPublishTimeKadSrc(0, 0);
629 case FT_KADLASTPUBLISHNOTES
:
630 SetLastPublishTimeKadNotes( newtag
.GetInt() );
633 case FT_KADLASTPUBLISHKEY
:
635 wxASSERT( newtag
.IsInt() );
639 // Store them here and write them back on saving.
640 m_taglist
.push_back(newtag
);
648 bool CKnownFile::LoadDateFromFile(const CFileDataIO
* file
)
650 m_lastDateChanged
= file
->ReadUInt32();
656 bool CKnownFile::LoadFromFile(const CFileDataIO
* file
)
658 // SLUGFILLER: SafeHash - load first, verify later
659 bool ret1
= LoadDateFromFile(file
);
660 bool ret2
= LoadHashsetFromFile(file
,false);
661 bool ret3
= LoadTagsFromFile(file
);
663 // Final hash-count verification, needs to be done after the tags are loaded.
664 return ret1
&& ret2
&& ret3
&& GetED2KPartHashCount()==GetHashCount();
665 // SLUGFILLER: SafeHash
669 bool CKnownFile::WriteToFile(CFileDataIO
* file
)
671 wxCHECK(!IsPartFile(), false);
674 file
->WriteUInt32(m_lastDateChanged
);
676 file
->WriteHash(m_abyFileHash
);
678 uint16 parts
= m_hashlist
.size();
679 file
->WriteUInt16(parts
);
681 for (int i
= 0; i
< parts
; ++i
)
682 file
->WriteHash(m_hashlist
[i
]);
685 const int iFixedTags
= 8;
686 uint32 tagcount
= iFixedTags
;
687 if (HasProperAICHHashSet()) {
690 // Float meta tags are currently not written. All older eMule versions < 0.28a have
691 // a bug in the meta tag reading+writing code. To achive maximum backward
692 // compatibility for met files with older eMule versions we just don't write float
693 // tags. This is OK, because we (eMule) do not use float tags. The only float tags
694 // we may have to handle is the '# Sent' tag from the Hybrid, which is pretty
695 // useless but may be received from us via the servers.
697 // The code for writing the float tags SHOULD BE ENABLED in SOME MONTHS (after most
698 // people are using the newer eMule versions which do not write broken float tags).
699 for (size_t j
= 0; j
< m_taglist
.size(); ++j
){
700 if (m_taglist
[j
].IsInt() || m_taglist
[j
].IsStr()) {
705 if (m_lastPublishTimeKadSrc
) {
709 if (m_lastPublishTimeKadNotes
){
715 file
->WriteUInt32(tagcount
);
717 // We still save the unicoded filename, for backwards
718 // compatibility with pre-2.2 and other clients.
719 CTagString
nametag_unicode(FT_FILENAME
, GetFileName().GetRaw());
720 // We write it with BOM to kep eMule compatibility
721 nametag_unicode
.WriteTagToFile(file
,utf8strOptBOM
);
723 // The non-unicoded filename is written in an 'universial'
724 // format, which allows us to identify files, even if the
725 // system locale changes.
726 CTagString
nametag(FT_FILENAME
, CPath::ToUniv(GetFileName()));
727 nametag
.WriteTagToFile(file
);
729 CTagIntSized
sizetag(FT_FILESIZE
, GetFileSize(), IsLargeFile() ? 64 : 32);
730 sizetag
.WriteTagToFile(file
);
734 tran
=statistic
.alltimetransferred
& 0xFFFFFFFF;
735 CTagInt32
attag1(FT_ATTRANSFERRED
, tran
);
736 attag1
.WriteTagToFile(file
);
738 tran
=statistic
.alltimetransferred
>>32;
739 CTagInt32
attag4(FT_ATTRANSFERREDHI
, tran
);
740 attag4
.WriteTagToFile(file
);
742 CTagInt32
attag2(FT_ATREQUESTED
, statistic
.GetAllTimeRequests());
743 attag2
.WriteTagToFile(file
);
745 CTagInt32
attag3(FT_ATACCEPTED
, statistic
.GetAllTimeAccepts());
746 attag3
.WriteTagToFile(file
);
748 // priority N permission
749 CTagInt32
priotag(FT_ULPRIORITY
, IsAutoUpPriority() ? PR_AUTO
: m_iUpPriority
);
750 priotag
.WriteTagToFile(file
);
753 if (HasProperAICHHashSet()) {
754 CTagString
aichtag(FT_AICH_HASH
, m_pAICHHashSet
->GetMasterHash().GetString());
755 aichtag
.WriteTagToFile(file
);
759 if (m_lastPublishTimeKadSrc
){
760 CTagInt32
kadLastPubSrc(FT_KADLASTPUBLISHSRC
, m_lastPublishTimeKadSrc
);
761 kadLastPubSrc
.WriteTagToFile(file
);
765 if (m_lastPublishTimeKadNotes
){
766 CTagInt32
kadLastPubNotes(FT_KADLASTPUBLISHNOTES
, m_lastPublishTimeKadNotes
);
767 kadLastPubNotes
.WriteTagToFile(file
);
771 for (size_t j
= 0; j
< m_taglist
.size(); ++j
){
772 if (m_taglist
[j
].IsInt() || m_taglist
[j
].IsStr()) {
773 m_taglist
[j
].WriteTagToFile(file
);
780 void CKnownFile::CreateHashFromHashlist(const ArrayOfCMD4Hash
& hashes
, CMD4Hash
* Output
)
782 wxCHECK_RET(hashes
.size(), wxT("No input to hash from in CreateHashFromHashlist"));
784 std::vector
<byte
> buffer(hashes
.size() * MD4HASH_LENGTH
);
785 std::vector
<byte
>::iterator it
= buffer
.begin();
787 for (size_t i
= 0; i
< hashes
.size(); ++i
) {
788 it
= STLCopy_n(hashes
[i
].GetHash(), MD4HASH_LENGTH
, it
);
791 CreateHashFromInput(&buffer
[0], buffer
.size(), Output
, NULL
);
795 void CKnownFile::CreateHashFromFile(CFileAutoClose
& file
, uint64 offset
, uint32 Length
, CMD4Hash
* Output
, CAICHHashTree
* pShaHashOut
)
797 wxCHECK_RET(Length
, wxT("No input to hash from in CreateHashFromFile"));
800 area
.ReadAt(file
, offset
, Length
);
802 CreateHashFromInput(area
.GetBuffer(), Length
, Output
, pShaHashOut
);
807 void CKnownFile::CreateHashFromInput(const byte
* input
, uint32 Length
, CMD4Hash
* Output
, CAICHHashTree
* pShaHashOut
)
809 wxASSERT_MSG(Output
|| pShaHashOut
, wxT("Nothing to do in CreateHashFromInput"));
810 wxCHECK_RET(input
, wxT("No input to hash from in CreateHashFromInput"));
811 wxASSERT(Length
<= PARTSIZE
); // We never hash more than one PARTSIZE
813 CMemFile
data(input
, Length
);
815 uint32 Required
= Length
;
818 uint32 posCurrentEMBlock
= 0;
820 CScopedPtr
<CAICHHashAlgo
> pHashAlg(CAICHHashSet::GetNewHashAlgo());
823 while (Required
>= 64) {
824 uint32 len
= Required
/ 64;
825 if (len
> sizeof(X
)/(64 * sizeof(X
[0]))) {
826 len
= sizeof(X
)/(64 * sizeof(X
[0]));
829 data
.Read(&X
, len
* 64);
831 // SHA hash needs 180KB blocks
833 if (nIACHPos
+ len
*64 >= EMBLOCKSIZE
) {
834 uint32 nToComplete
= EMBLOCKSIZE
- nIACHPos
;
835 pHashAlg
->Add(X
, nToComplete
);
836 wxASSERT( nIACHPos
+ nToComplete
== EMBLOCKSIZE
);
837 pShaHashOut
->SetBlockHash(EMBLOCKSIZE
, posCurrentEMBlock
, pHashAlg
.get());
838 posCurrentEMBlock
+= EMBLOCKSIZE
;
840 pHashAlg
->Add(X
+nToComplete
,(len
*64) - nToComplete
);
841 nIACHPos
= (len
*64) - nToComplete
;
844 pHashAlg
->Add(X
, len
*64);
852 Required
= Length
% 64;
854 data
.Read(&X
,Required
);
856 if (pShaHashOut
!= NULL
){
857 if (nIACHPos
+ Required
>= EMBLOCKSIZE
){
858 uint32 nToComplete
= EMBLOCKSIZE
- nIACHPos
;
859 pHashAlg
->Add(X
, nToComplete
);
860 wxASSERT( nIACHPos
+ nToComplete
== EMBLOCKSIZE
);
861 pShaHashOut
->SetBlockHash(EMBLOCKSIZE
, posCurrentEMBlock
, pHashAlg
.get());
862 posCurrentEMBlock
+= EMBLOCKSIZE
;
864 pHashAlg
->Add(X
+nToComplete
, Required
- nToComplete
);
865 nIACHPos
= Required
- nToComplete
;
868 pHashAlg
->Add(X
, Required
);
869 nIACHPos
+= Required
;
873 if (pShaHashOut
!= NULL
){
875 pShaHashOut
->SetBlockHash(nIACHPos
, posCurrentEMBlock
, pHashAlg
.get());
876 posCurrentEMBlock
+= nIACHPos
;
878 wxASSERT( posCurrentEMBlock
== Length
);
879 wxCHECK2( pShaHashOut
->ReCalculateHash(pHashAlg
.get(), false), );
883 #ifdef __WEAK_CRYPTO__
884 CryptoPP::Weak::MD4 md4_hasher
;
886 CryptoPP::MD4 md4_hasher
;
888 md4_hasher
.CalculateDigest(Output
->GetHash(), input
, Length
);
893 const CMD4Hash
& CKnownFile::GetPartHash(uint16 part
) const {
894 wxASSERT( part
< m_hashlist
.size() );
896 return m_hashlist
[part
];
899 CPacket
* CKnownFile::CreateSrcInfoPacket(const CUpDownClient
* forClient
, uint8 byRequestedVersion
, uint16 nRequestedOptions
)
903 if (m_ClientUploadList
.empty()) {
907 if ((((CKnownFile
*)forClient
->GetRequestFile() != this)
908 && ((CKnownFile
*)forClient
->GetUploadFile() != this)) || forClient
->GetUploadFileID() != GetFileHash()) {
909 wxString file1
= _("Unknown");
910 if (forClient
->GetRequestFile() && forClient
->GetRequestFile()->GetFileName().IsOk()) {
911 file1
= forClient
->GetRequestFile()->GetFileName().GetPrintable();
912 } else if (forClient
->GetUploadFile() && forClient
->GetUploadFile()->GetFileName().IsOk()) {
913 file1
= forClient
->GetUploadFile()->GetFileName().GetPrintable();
915 wxString file2
= _("Unknown");
916 if (GetFileName().IsOk()) {
917 file2
= GetFileName().GetPrintable();
919 AddDebugLogLineM(false, logKnownFiles
, wxT("File missmatch on source packet (K) Sending: ") + file1
+ wxT(" From: ") + file2
);
923 const BitVector
& rcvstatus
= forClient
->GetUpPartStatus();
924 bool SupportsUploadChunksState
= !rcvstatus
.empty();
925 //wxASSERT(rcvstatus.size() == GetPartCount()); // Obviously!
926 if (rcvstatus
.size() != GetPartCount()) {
927 // Yuck. Same file but different part count? Seriously fucked up.
928 AddDebugLogLineM(false, logKnownFiles
, CFormat(wxT("Impossible situation: different partcounts for the same known file: %i (client) and %i (file)")) % rcvstatus
.size() % GetPartCount());
936 if (forClient
->SupportsSourceExchange2() && byRequestedVersion
> 0){
937 // the client uses SourceExchange2 and requested the highest version he knows
938 // and we send the highest version we know, but of course not higher than his request
939 byUsedVersion
= std::min(byRequestedVersion
, (uint8
)SOURCEEXCHANGE2_VERSION
);
941 data
.WriteUInt8(byUsedVersion
);
943 // we don't support any special SX2 options yet, reserved for later use
944 if (nRequestedOptions
!= 0) {
945 AddDebugLogLineM(false, logKnownFiles
, CFormat(wxT("Client requested unknown options for SourceExchange2: %u")) % nRequestedOptions
);
948 byUsedVersion
= forClient
->GetSourceExchange1Version();
949 bIsSX2Packet
= false;
950 if (forClient
->SupportsSourceExchange2()) {
951 AddDebugLogLineM(false, logKnownFiles
, wxT("Client which announced to support SX2 sent SX1 packet instead"));
957 data
.WriteHash(forClient
->GetUploadFileID());
958 data
.WriteUInt16(nCount
);
959 uint32 cDbgNoSrc
= 0;
961 SourceSet::iterator it
= m_ClientUploadList
.begin();
962 for ( ; it
!= m_ClientUploadList
.end(); it
++ ) {
963 const CUpDownClient
*cur_src
= *it
;
965 if ( cur_src
->HasLowID() ||
966 cur_src
== forClient
||
967 !( cur_src
->GetUploadState() == US_UPLOADING
||
968 cur_src
->GetUploadState() == US_ONUPLOADQUEUE
)) {
972 bool bNeeded
= false;
974 if ( SupportsUploadChunksState
) {
975 const BitVector
& srcstatus
= cur_src
->GetUpPartStatus();
976 if ( !srcstatus
.empty() ) {
977 //wxASSERT(srcstatus.size() == GetPartCount()); // Obviously!
978 if (srcstatus
.size() != GetPartCount()) {
981 if ( cur_src
->GetUpPartCount() == forClient
->GetUpPartCount() ) {
982 for (int x
= 0; x
< GetPartCount(); x
++ ) {
983 if ( srcstatus
.get(x
) && !rcvstatus
.get(x
) ) {
984 // We know the receiving client needs
985 // a chunk from this client.
993 // This client doesn't support upload chunk status.
994 // So just send it and hope for the best.
998 // remote client does not support upload chunk status,
999 // search sources which have at least one complete part
1000 // we could even sort the list of sources by available
1001 // chunks to return as much sources as possible which
1002 // have the most available chunks. but this could be
1003 // a noticeable performance problem.
1004 const BitVector
& srcstatus
= cur_src
->GetUpPartStatus();
1005 if ( !srcstatus
.empty() ) {
1006 //wxASSERT(srcstatus.size() == GetPartCount());
1007 if (srcstatus
.size() != GetPartCount()) {
1010 for (int x
= 0; x
< GetPartCount(); x
++ ) {
1011 if ( srcstatus
.get(x
) ) {
1012 // this client has at least one chunk
1018 // This client doesn't support upload chunk status.
1019 // So just send it and hope for the best.
1027 if(byUsedVersion
>= 3) {
1028 dwID
= cur_src
->GetUserIDHybrid();
1030 dwID
= cur_src
->GetIP();
1032 data
.WriteUInt32(dwID
);
1033 data
.WriteUInt16(cur_src
->GetUserPort());
1034 data
.WriteUInt32(cur_src
->GetServerIP());
1035 data
.WriteUInt16(cur_src
->GetServerPort());
1037 if (byUsedVersion
>= 2) {
1038 data
.WriteHash(cur_src
->GetUserHash());
1041 if (byUsedVersion
>= 4){
1042 // CryptSettings - SourceExchange V4
1044 // 1 CryptLayer Required
1045 // 1 CryptLayer Requested
1046 // 1 CryptLayer Supported
1047 const uint8 uSupportsCryptLayer
= cur_src
->SupportsCryptLayer() ? 1 : 0;
1048 const uint8 uRequestsCryptLayer
= cur_src
->RequestsCryptLayer() ? 1 : 0;
1049 const uint8 uRequiresCryptLayer
= cur_src
->RequiresCryptLayer() ? 1 : 0;
1050 const uint8 byCryptOptions
= (uRequiresCryptLayer
<< 2) | (uRequestsCryptLayer
<< 1) | (uSupportsCryptLayer
<< 0);
1051 data
.WriteUInt8(byCryptOptions
);
1064 data
.Seek(bIsSX2Packet
? 17 : 16, wxFromStart
);
1065 data
.WriteUInt16(nCount
);
1067 CPacket
* result
= new CPacket(data
, OP_EMULEPROT
, bIsSX2Packet
? OP_ANSWERSOURCES2
: OP_ANSWERSOURCES
);
1069 if ( result
->GetPacketSize() > 354 ) {
1070 result
->PackPacket();
1077 // Updates priority of file if autopriority is activated
1078 void CKnownFile::UpdateAutoUpPriority()
1080 if (IsAutoUpPriority()) {
1081 uint32 queued
= GetQueuedCount();
1082 uint8 priority
= PR_NORMAL
;
1086 } else if (queued
> 1) {
1087 priority
= PR_NORMAL
;
1092 if (GetUpPriority() != priority
) {
1093 SetUpPriority(priority
, false);
1094 Notify_SharedFilesUpdateItem(this);
1099 void CKnownFile::SetFileComment(const wxString
& strNewComment
)
1101 if (m_strComment
!= strNewComment
) {
1102 SetLastPublishTimeKadNotes(0);
1103 wxString strCfgPath
= wxT("/") + m_abyFileHash
.Encode() + wxT("/");
1105 wxConfigBase
* cfg
= wxConfigBase::Get();
1106 cfg
->Write( strCfgPath
+ wxT("Comment"), strNewComment
);
1108 m_strComment
= strNewComment
;
1110 SourceSet::iterator it
= m_ClientUploadList
.begin();
1111 for ( ; it
!= m_ClientUploadList
.end(); it
++ ) {
1112 (*it
)->SetCommentDirty();
1119 void CKnownFile::SetFileRating(int8 iNewRating
)
1121 if (m_iRating
!= iNewRating
) {
1122 SetLastPublishTimeKadNotes(0);
1123 wxString strCfgPath
= wxT("/") + m_abyFileHash
.Encode() + wxT("/");
1124 wxConfigBase
* cfg
= wxConfigBase::Get();
1125 cfg
->Write( strCfgPath
+ wxT("Rate"), (int)iNewRating
);
1126 m_iRating
= iNewRating
;
1128 SourceSet::iterator it
= m_ClientUploadList
.begin();
1129 for ( ; it
!= m_ClientUploadList
.end(); it
++ ) {
1130 (*it
)->SetCommentDirty();
1136 void CKnownFile::SetUpPriority(uint8 iNewUpPriority
, bool m_bsave
){
1137 m_iUpPriority
= iNewUpPriority
;
1138 if( IsPartFile() && m_bsave
) {
1139 ((CPartFile
*)this)->SavePartFile();
1143 void CKnownFile::SetPublishedED2K(bool val
){
1144 m_PublishedED2K
= val
;
1145 Notify_SharedFilesUpdateItem(this);
1148 bool CKnownFile::PublishNotes()
1150 if(m_lastPublishTimeKadNotes
> (uint32
)time(NULL
)) {
1154 if(!GetFileComment().IsEmpty()) {
1155 m_lastPublishTimeKadNotes
= (uint32
)time(NULL
)+KADEMLIAREPUBLISHTIMEN
;
1159 if(GetFileRating() != 0) {
1160 m_lastPublishTimeKadNotes
= (uint32
)time(NULL
)+KADEMLIAREPUBLISHTIMEN
;
1167 bool CKnownFile::PublishSrc()
1169 uint32 lastBuddyIP
= 0;
1171 if( theApp
->IsFirewalled() ) {
1172 CUpDownClient
* buddy
= theApp
->clientlist
->GetBuddy();
1174 lastBuddyIP
= theApp
->clientlist
->GetBuddy()->GetIP();
1175 if( lastBuddyIP
!= m_lastBuddyIP
) {
1176 SetLastPublishTimeKadSrc( (uint32
)time(NULL
)+KADEMLIAREPUBLISHTIMES
, lastBuddyIP
);
1184 if(m_lastPublishTimeKadSrc
> (uint32
)time(NULL
)) {
1188 SetLastPublishTimeKadSrc((uint32
)time(NULL
)+KADEMLIAREPUBLISHTIMES
,lastBuddyIP
);
1193 void CKnownFile::UpdatePartsInfo()
1196 uint16 partcount
= GetPartCount();
1197 bool flag
= (time(NULL
) - m_nCompleteSourcesTime
> 0);
1199 // Ensure the frequency-list is ready
1200 if ( m_AvailPartFrequency
.size() != GetPartCount() ) {
1201 m_AvailPartFrequency
.clear();
1202 m_AvailPartFrequency
.insert(m_AvailPartFrequency
.begin(), GetPartCount(), 0);
1206 ArrayOfUInts16 count
;
1207 count
.reserve(m_ClientUploadList
.size());
1209 SourceSet::iterator it
= m_ClientUploadList
.begin();
1210 for ( ; it
!= m_ClientUploadList
.end(); it
++ ) {
1211 if ( !(*it
)->GetUpPartStatus().empty() && (*it
)->GetUpPartCount() == partcount
) {
1212 count
.push_back((*it
)->GetUpCompleteSourcesCount());
1216 m_nCompleteSourcesCount
= m_nCompleteSourcesCountLo
= m_nCompleteSourcesCountHi
= 0;
1218 if( partcount
> 0) {
1219 m_nCompleteSourcesCount
= m_AvailPartFrequency
[0];
1221 for (uint16 i
= 1; i
< partcount
; ++i
) {
1222 if( m_nCompleteSourcesCount
> m_AvailPartFrequency
[i
]) {
1223 m_nCompleteSourcesCount
= m_AvailPartFrequency
[i
];
1226 count
.push_back(m_nCompleteSourcesCount
);
1228 int32 n
= count
.size();
1230 std::sort(count
.begin(), count
.end(), std::less
<uint16
>());
1233 int i
= n
>> 1; // (n / 2)
1234 int j
= (n
* 3) >> 2; // (n * 3) / 4
1235 int k
= (n
* 7) >> 3; // (n * 7) / 8
1237 // For complete files, trust the people your uploading to more...
1239 // For low guess and normal guess count
1240 // - If we see more sources then the guessed low and
1241 // normal, use what we see.
1242 // - If we see less sources then the guessed low,
1243 // adjust network accounts for 100%, we account for
1244 // 0% with what we see and make sure we are still
1245 // above the normal.
1247 // Adjust 100% network and 0% what we see.
1249 if ( count
[i
] < m_nCompleteSourcesCount
) {
1250 m_nCompleteSourcesCountLo
= m_nCompleteSourcesCount
;
1252 m_nCompleteSourcesCountLo
= count
[i
];
1254 m_nCompleteSourcesCount
= m_nCompleteSourcesCountLo
;
1255 m_nCompleteSourcesCountHi
= count
[j
];
1256 if( m_nCompleteSourcesCountHi
< m_nCompleteSourcesCount
) {
1257 m_nCompleteSourcesCountHi
= m_nCompleteSourcesCount
;
1264 // Adjust network accounts for 100%, we account for
1265 // 0% with what we see and make sure we are still above the low.
1267 // Adjust network accounts for 100%, we account for 0%
1268 // with what we see and make sure we are still above the normal.
1270 m_nCompleteSourcesCountLo
= m_nCompleteSourcesCount
;
1271 m_nCompleteSourcesCount
= count
[j
];
1272 if( m_nCompleteSourcesCount
< m_nCompleteSourcesCountLo
) {
1273 m_nCompleteSourcesCount
= m_nCompleteSourcesCountLo
;
1275 m_nCompleteSourcesCountHi
= count
[k
];
1276 if( m_nCompleteSourcesCountHi
< m_nCompleteSourcesCount
) {
1277 m_nCompleteSourcesCountHi
= m_nCompleteSourcesCount
;
1281 m_nCompleteSourcesTime
= time(NULL
) + (60);
1284 Notify_SharedFilesUpdateItem(this);
1288 void CKnownFile::UpdateUpPartsFrequency( CUpDownClient
* client
, bool increment
)
1290 if ( m_AvailPartFrequency
.size() != GetPartCount() ) {
1291 m_AvailPartFrequency
.clear();
1292 m_AvailPartFrequency
.insert(m_AvailPartFrequency
.begin(), GetPartCount(), 0);
1298 const BitVector
& freq
= client
->GetUpPartStatus();
1299 unsigned int size
= freq
.size();
1300 if ( size
!= m_AvailPartFrequency
.size() ) {
1305 for ( unsigned int i
= 0; i
< size
; ++i
) {
1306 if ( freq
.get(i
) ) {
1307 m_AvailPartFrequency
[i
]++;
1311 for ( unsigned int i
= 0; i
< size
; ++i
) {
1312 if ( freq
.get(i
) ) {
1313 m_AvailPartFrequency
[i
]--;
1319 void CKnownFile::ClearPriority() {
1320 if ( !m_bAutoUpPriority
) return;
1321 m_iUpPriority
= ( m_bAutoUpPriority
) ? PR_HIGH
: PR_NORMAL
;
1322 UpdateAutoUpPriority();
1325 void GuessAndRemoveExt(CPath
& name
)
1327 wxString ext
= name
.GetExt();
1329 // Remove common two-part extensions, such as "tar.gz"
1330 if (ext
== wxT("gz") || ext
== wxT("bz2")) {
1331 name
= name
.RemoveExt();
1332 if (name
.GetExt() == wxT("tar")) {
1333 name
= name
.RemoveExt();
1335 // might be an extension if length == 3
1336 // and also remove some common non-three-character extensions
1337 } else if (ext
.Length() == 3 ||
1340 ext
== wxT("jpeg") ||
1343 name
= name
.RemoveExt();
1347 void CKnownFile::SetFileName(const CPath
& filename
)
1349 CAbstractFile::SetFileName(filename
);
1351 // Don't publish extension. That'd kill the node indexing e.g. "avi".
1352 CPath tmpName
= GetFileName();
1353 GuessAndRemoveExt(tmpName
);
1354 Kademlia::CSearchManager::GetWords(tmpName
.GetPrintable(), &wordlist
);
1357 #endif // CLIENT_GUI
1359 //For File Comment //
1360 void CKnownFile::LoadComment()
1363 wxString strCfgPath
= wxT("/") + m_abyFileHash
.Encode() + wxT("/");
1365 wxConfigBase
* cfg
= wxConfigBase::Get();
1367 m_strComment
= cfg
->Read( strCfgPath
+ wxT("Comment"), wxEmptyString
);
1368 m_iRating
= cfg
->Read( strCfgPath
+ wxT("Rate"), 0l);
1369 m_bCommentLoaded
= true;
1372 m_strComment
.Clear();
1373 m_bCommentLoaded
= true;
1380 wxString
CKnownFile::GetAICHMasterHash() const
1383 return m_AICHMasterHash
;
1385 if (HasProperAICHHashSet()) {
1386 return m_pAICHHashSet
->GetMasterHash().GetString();
1389 return wxEmptyString
;
1394 bool CKnownFile::HasProperAICHHashSet() const
1397 return m_AICHMasterHash
.Length() != 0;
1399 return m_pAICHHashSet
->HasValidMasterHash() &&
1400 (m_pAICHHashSet
->GetStatus() == AICH_HASHSETCOMPLETE
||
1401 m_pAICHHashSet
->GetStatus() == AICH_VERIFIED
);
1405 wxString
CKnownFile::GetFeedback() const
1407 return wxString(_("File name")) + wxT(": ") + GetFileName().GetPrintable() + wxT("\n")
1408 + _("File size") + wxT(": ") + CastItoXBytes(GetFileSize()) + wxT("\n")
1409 + _("Share ratio") + wxString::Format(wxT(": %.2f%%\n"), (((double)statistic
.GetAllTimeTransferred() / (double)GetFileSize()) * 100.0))
1410 + _("Uploaded") + wxT(": ") + CastItoXBytes(statistic
.GetTransferred()) + wxT(" (") + CastItoXBytes(statistic
.GetAllTimeTransferred()) + wxT(")\n")
1411 + _("Requested") + CFormat(wxT(": %u (%u)\n")) % statistic
.GetRequests() % statistic
.GetAllTimeRequests()
1412 + _("Accepted") + CFormat(wxT(": %u (%u)\n")) % statistic
.GetAccepts() % statistic
.GetAllTimeAccepts()
1413 + _("On Queue") + CFormat(wxT(": %u\n")) % GetQueuedCount()
1414 + _("Complete sources") + CFormat(wxT(": %u\n")) % m_nCompleteSourcesCount
;
1417 // File_checked_for_headers