forked from amule-project/amule
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathKnownFile.cpp
More file actions
1913 lines (1675 loc) · 56.9 KB
/
Copy pathKnownFile.cpp
File metadata and controls
1913 lines (1675 loc) · 56.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This file is part of the aMule Project.
//
// Parts of this file are based on work from pan One (http://home-3.tiscali.nl/~meost/pms/)
//
// Copyright (c) 2003-2026 aMule Team ( https://amule-org.github.io )
// Copyright (c) 2002-2011 Merkur ( [email protected] / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include "KnownFile.h" // Do_not_auto_remove
#include <protocol/kad/Constants.h>
#include <protocol/ed2k/Client2Client/TCP.h>
#include <protocol/ed2k/ClientSoftware.h>
#include <protocol/Protocols.h>
#include <tags/FileTags.h>
#include <wx/config.h>
#ifdef CLIENT_GUI
#include "UpDownClientEC.h" // Needed for CUpDownClient
#else
#include "updownclient.h" // Needed for CUpDownClient
#endif
#include "MemFile.h" // Needed for CMemFile
#include "Packet.h" // Needed for CPacket
#include "Preferences.h" // Needed for CPreferences
#include "KnownFileList.h" // Needed for CKnownFileList
#include "amule.h" // Needed for theApp
#include "PartFile.h" // Needed for SavePartFile
#include "ClientList.h" // Needed for clientlist (buddy support)
#include "Logger.h"
#include "ScopedPtr.h" // Needed for CScopedArray and CScopedPtr
#include "GuiEvents.h" // Needed for Notify_*
#include "SearchFile.h" // Needed for CSearchFile
#include "FileArea.h" // Needed for CFileArea
#include "FileAutoClose.h" // Needed for CFileAutoClose
#include "Server.h" // Needed for CServer
#include "CryptoPP_Inc.h" // Needed for MD4
#include <common/Format.h>
#ifndef CLIENT_GUI
#include "kademlia/kademlia/Kademlia.h" // Needed for CKademlia (Kad state)
#include "kademlia/kademlia/Search.h" // Needed for CSearch::NOTES
#include "kademlia/kademlia/SearchManager.h" // Needed for CSearchManager::PrepareLookup
#include "kademlia/kademlia/Entry.h" // Needed for Kademlia::CEntry (Kad notes)
#include "DownloadQueue.h" // Needed for downloadqueue lookup
#include "SearchList.h" // Needed for searchlist lookup
#include "NetworkFunctions.h" // Needed for Uint32toStringIP (Kad note author)
#include <tags/FileTags.h> // Needed for TAG_FILERATING / TAG_DESCRIPTION
#include "ThreadTasks.h" // Needed for CThreadScheduler and CVerifyLocalDataTask
#endif
CFileStatistic::CFileStatistic(CKnownFile *parent)
: fileParent(parent)
, requested(0)
, transferred(0)
, accepted(0)
, alltimerequested(0)
, alltimetransferred(0)
, alltimeaccepted(0)
{
}
#ifndef CLIENT_GUI
void CFileStatistic::AddRequest()
{
requested++;
alltimerequested++;
theApp->knownfiles->requested++;
if (fileParent && fileParent->IsPartFile()) {
static_cast<CPartFile *>(fileParent)->MarkStatsDirty();
}
if (fileParent) {
fileParent->MarkECChanged();
}
theApp->sharedfiles->UpdateItem(fileParent);
}
void CFileStatistic::AddAccepted()
{
accepted++;
alltimeaccepted++;
theApp->knownfiles->accepted++;
if (fileParent && fileParent->IsPartFile()) {
static_cast<CPartFile *>(fileParent)->MarkStatsDirty();
}
if (fileParent) {
fileParent->MarkECChanged();
}
theApp->sharedfiles->UpdateItem(fileParent);
}
void CFileStatistic::AddTransferred(uint64 bytes)
{
transferred += bytes;
alltimetransferred += bytes;
theApp->knownfiles->transferred += bytes;
if (fileParent && fileParent->IsPartFile()) {
static_cast<CPartFile *>(fileParent)->MarkStatsDirty();
}
if (fileParent) {
// Upload-activity stamp (issue #466): the upload-side analogue of
// the download's m_lastDateChanged, stamped here because this is
// the single point where sent bytes are attributed to the file.
fileParent->SetLastUpload(time(nullptr));
fileParent->MarkECChanged();
}
theApp->sharedfiles->UpdateItem(fileParent);
}
#endif // CLIENT_GUI
/* Static storage for the process-wide EC change generation counter.
* See `CKnownFile::MarkECChanged()` doc in KnownFile.h. */
std::atomic<uint64> CKnownFile::s_globalEcGen{ 0 };
uint32 CKnownFile::GetMetaDataVer() const
{
// Derived from tag presence, no separate m_uMetaDataVer field.
// FT_MEDIA_LENGTH is the only tag MediaProbe populates
// unconditionally on a successful probe (bitrate and codec are
// best-effort per format), so nonzero length is the reliable
// "we've probed this and have data worth publishing" signal.
// Kad's publisher (Search.cpp:1422) uses this exact gate.
return GetIntTagValue(FT_MEDIA_LENGTH) > 0 ? 1 : 0;
}
void CKnownFile::MarkECChanged()
{
// Single atomic pre-increment + atomic store. Generation values are
// strictly ascending across all files and all threads; readers
// (`Get_EC_Response_GetUpdate`) compare against the highest gen they
// have already sent and ignore lesser ones.
m_ecGen.store(s_globalEcGen.fetch_add(1, std::memory_order_relaxed) + 1, std::memory_order_relaxed);
}
/* Abstract File (base class)*/
CAbstractFile::CAbstractFile()
: m_iRating(0)
, m_hasComment(false)
, m_iUserRating(0)
, m_kadCommentSearchRunning(false)
, m_nFileSize(0)
{
}
CAbstractFile::CAbstractFile(const CAbstractFile &other)
: m_abyFileHash(other.m_abyFileHash)
, m_strComment(other.m_strComment)
, m_iRating(other.m_iRating)
, m_hasComment(other.m_hasComment)
, m_iUserRating(other.m_iUserRating)
, m_taglist(other.m_taglist)
, m_kadNotes()
, m_kadCommentSearchRunning(false)
, m_nFileSize(other.m_nFileSize)
, m_fileName(other.m_fileName)
{
/* // TODO: Currently it's not safe to duplicate the entries, but isn't needed either.
CKadEntryPtrList::const_iterator it = other.m_kadNotes.begin();
for (; it != other.m_kadNotes.end(); ++it) {
m_kadNotes.push_back(new Kademlia::CEntry(**it));
}
*/
}
void CAbstractFile::SetFileName(const CPath &fileName)
{
m_fileName = fileName;
}
uint32 CAbstractFile::GetIntTagValue(uint8 tagname) const
{
ArrayOfCTag::const_iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if (((*it).GetNameID() == tagname) && (*it).IsInt()) {
return (*it).GetInt();
}
}
return 0;
}
bool CAbstractFile::GetIntTagValue(uint8 tagname, uint32 &ruValue) const
{
ArrayOfCTag::const_iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if (((*it).GetNameID() == tagname) && (*it).IsInt()) {
ruValue = (*it).GetInt();
return true;
}
}
return false;
}
uint32 CAbstractFile::GetIntTagValue(const wxString &tagname) const
{
ArrayOfCTag::const_iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if ((*it).IsInt() && ((*it).GetName() == tagname)) {
return (*it).GetInt();
}
}
return 0;
}
const wxString &CAbstractFile::GetStrTagValue(uint8 tagname) const
{
ArrayOfCTag::const_iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if ((*it).GetNameID() == tagname && (*it).IsStr()) {
return (*it).GetStr();
}
}
return EmptyString;
}
const wxString &CAbstractFile::GetStrTagValue(const wxString &tagname) const
{
ArrayOfCTag::const_iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if ((*it).IsStr() && ((*it).GetName() == tagname)) {
return (*it).GetStr();
}
}
return EmptyString;
}
const CTag *CAbstractFile::GetTag(uint8 tagname, uint8 tagtype) const
{
ArrayOfCTag::const_iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if ((*it).GetNameID() == tagname && (*it).GetType() == tagtype) {
return &(*it);
}
}
return NULL;
}
const CTag *CAbstractFile::GetTag(const wxString &tagname, uint8 tagtype) const
{
ArrayOfCTag::const_iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if ((*it).GetType() == tagtype && (*it).GetName() == tagname) {
return &(*it);
}
}
return NULL;
}
const CTag *CAbstractFile::GetTag(uint8 tagname) const
{
ArrayOfCTag::const_iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if ((*it).GetNameID() == tagname) {
return &(*it);
}
}
return NULL;
}
const CTag *CAbstractFile::GetTag(const wxString &tagname) const
{
ArrayOfCTag::const_iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if ((*it).GetName() == tagname) {
return &(*it);
}
}
return NULL;
}
void CAbstractFile::AddTagUnique(const CTag &rTag)
{
ArrayOfCTag::iterator it = m_taglist.begin();
for (; it != m_taglist.end(); ++it) {
if ((((*it).GetNameID() != 0 && (*it).GetNameID() == rTag.GetNameID()) ||
(!(*it).GetName().IsEmpty() && !rTag.GetName().IsEmpty() &&
(*it).GetName() == rTag.GetName())) &&
(*it).GetType() == rTag.GetType()) {
it = m_taglist.erase(it);
m_taglist.insert(it, rTag);
return;
}
}
m_taglist.push_back(rTag);
}
#ifndef CLIENT_GUI
void CAbstractFile::AddNote(Kademlia::CEntry *pEntry)
{
CKadEntryPtrList::iterator it = m_kadNotes.begin();
for (; it != m_kadNotes.end(); ++it) {
Kademlia::CEntry *entry = *it;
if (entry->m_uIP == pEntry->m_uIP || entry->m_uSourceID == pEntry->m_uSourceID) {
delete pEntry;
return;
}
}
m_kadNotes.push_front(pEntry);
}
void CAbstractFile::GetKadNotesComments(FileRatingList &list) const
{
// One entry per responding Kad node (stored by CSearch::ProcessResultNotes).
for (Kademlia::CEntry *entry : getNotes()) {
uint64_t rating = 0;
entry->GetIntTagValue(TAG_FILERATING, rating);
wxString comment = entry->GetStrTagValue(TAG_DESCRIPTION);
if (comment.IsEmpty() && rating == 0) {
continue;
}
wxString userName = entry->m_uIP ? Uint32toStringIP(entry->m_uIP) : wxString(_("Kad user"));
list.emplace_back(userName, entry->GetCommonFileName(), (sint16)rating, comment);
}
}
void CAbstractFile::GetRatingAndComments(FileRatingList &list) const
{
// Base version: just the on-demand Kad notes. This is exactly what a search
// result carries; CPartFile overrides to prepend its connected-source
// comments.
list.clear();
GetKadNotesComments(list);
}
#else
void CAbstractFile::AddNote(Kademlia::CEntry *) {}
void CAbstractFile::GetKadNotesComments(FileRatingList &) const {}
void CAbstractFile::GetRatingAndComments(FileRatingList &list) const
{
// amulegui receives the ratings/comments prebuilt over EC and cached in
// m_FileRatingList by the remote containers. One implementation serves
// downloads, shared files and search results, so no subclass overrides this.
list = m_FileRatingList;
}
#endif
/* Known File */
CKnownFile::CKnownFile()
: statistic(this)
{
Init();
}
CKnownFile::CKnownFile(uint32 ecid)
: CECID(ecid)
, statistic(this)
{
Init();
}
// #warning Experimental: Construct a CKnownFile from a CSearchFile
CKnownFile::CKnownFile(const CSearchFile &searchFile)
: // This will copy the file hash
CAbstractFile(static_cast<const CAbstractFile &>(searchFile))
, statistic(this)
{
Init();
// Use CKnownFile::SetFileName()
SetFileName(searchFile.GetFileName());
// Use CKnownFile::SetFileSize()
SetFileSize(searchFile.GetFileSize());
}
void CKnownFile::Init()
{
// Stamp the EC generation immediately so any newly-constructed file
// (search-result import, partfile creation, hashed-and-added shared
// file) is naturally `> 0` from every existing connection's
// `m_lastEcGenSeen` perspective. Without this, the first INC_UPDATE
// cycle within the 60 s backstop window after a file is added would
// skip it because its default-zero gen looked unchanged.
MarkECChanged();
m_showSources = false;
m_showPeers = false;
m_nCompleteSourcesTime = time(NULL);
m_nCompleteSourcesCount = 0;
m_nCompleteSourcesCountLo = 0;
m_nCompleteSourcesCountHi = 0;
m_bCommentLoaded = false;
m_iPartCount = 0;
m_iED2KPartCount = 0;
m_iED2KPartHashCount = 0;
m_PublishedED2K = false;
kadFileSearchID = 0;
m_lastPublishTimeKadSrc = 0;
m_lastPublishTimeKadNotes = 0;
m_lastBuddyIP = 0;
m_lastDateChanged = 0;
m_lastUploadDatetime = 0;
m_dateShared = 0;
// Sentinel "unknown": LoadFromFile fills this in from FT_LASTSEEN
// when present, else falls back to the file's own mtime
// (m_lastDateChanged) for migration -- so a known.met that
// predates this tag gets a useful aging signal on first save
// after upgrade rather than every record looking "fresh now"
// for the next TTL window. Fresh hashes (CHashingTask) bump
// this in CKnownFileList::Append's "newly added" branch.
m_lastSeen = 0;
m_bAutoUpPriority = thePrefs::GetNewAutoUp();
m_iUpPriority = (m_bAutoUpPriority) ? PR_HIGH : PR_NORMAL;
m_hashingProgress = 0;
#ifndef CLIENT_GUI
m_pAICHHashSet = new CAICHHashSet(this);
#endif
}
void CKnownFile::SetFileSize(uint64 nFileSize)
{
CAbstractFile::SetFileSize(nFileSize);
#ifndef CLIENT_GUI
m_pAICHHashSet->SetFileSize(nFileSize);
#endif
// Examples of parthashs, hashsets and filehashs for different filesizes
// according the ed2k protocol
//----------------------------------------------------------------------
//
// File size: 3 bytes
// File hash: 2D55E87D0E21F49B9AD25F98531F3724
// Nr. hashs: 0
//
//
// File size: 1*PARTSIZE
// File hash: A72CA8DF7F07154E217C236C89C17619
// Nr. hashs: 2
// Hash[ 0]: 4891ED2E5C9C49F442145A3A5F608299
// Hash[ 1]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
//
//
// File size: 1*PARTSIZE + 1 byte
// File hash: 2F620AE9D462CBB6A59FE8401D2B3D23
// Nr. hashs: 2
// Hash[ 0]: 121795F0BEDE02DDC7C5426D0995F53F
// Hash[ 1]: C329E527945B8FE75B3C5E8826755747
//
//
// File size: 2*PARTSIZE
// File hash: A54C5E562D5E03CA7D77961EB9A745A4
// Nr. hashs: 3
// Hash[ 0]: B3F5CE2A06BF403BFB9BFFF68BDDC4D9
// Hash[ 1]: 509AA30C9EA8FC136B1159DF2F35B8A9
// Hash[ 2]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
//
//
// File size: 3*PARTSIZE
// File hash: 5E249B96F9A46A18FC2489B005BF2667
// Nr. hashs: 4
// Hash[ 0]: 5319896A2ECAD43BF17E2E3575278E72
// Hash[ 1]: D86EF157D5E49C5ED502EDC15BB5F82B
// Hash[ 2]: 10F2D5B1FCB95C0840519C58D708480F
// Hash[ 3]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
//
//
// File size: 3*PARTSIZE + 1 byte
// File hash: 797ED552F34380CAFF8C958207E40355
// Nr. hashs: 4
// Hash[ 0]: FC7FD02CCD6987DCF1421F4C0AF94FB8
// Hash[ 1]: 2FE466AF8A7C06DA3365317B75A5ACFE
// Hash[ 2]: 873D3BF52629F7C1527C6E8E473C1C30
// Hash[ 3]: BCE50BEE7877BB07BB6FDA56BFE142FB
//
// File size Data parts ED2K parts ED2K part hashs
// ---------------------------------------------------------------
// 1..PARTSIZE-1 1 1 0(!)
// PARTSIZE 1 2(!) 2(!)
// PARTSIZE+1 2 2 2
// PARTSIZE*2 2 3(!) 3(!)
// PARTSIZE*2+1 3 3 3
if (nFileSize == 0) {
// wxFAIL; // Kry - Why commented out by lemonfan? it can never be 0
m_iPartCount = 0;
m_iED2KPartCount = 0;
m_iED2KPartHashCount = 0;
m_sizeLastPart = 0;
return;
}
// nr. of data parts
m_iPartCount = nFileSize / PARTSIZE + 1;
// size of last part
m_sizeLastPart = nFileSize % PARTSIZE;
// file with size of n * PARTSIZE
if (m_sizeLastPart == 0) {
m_sizeLastPart = PARTSIZE;
m_iPartCount--;
}
// nr. of parts to be used with OP_FILESTATUS
m_iED2KPartCount = nFileSize / PARTSIZE + 1;
// nr. of parts to be used with OP_HASHSETANSWER
m_iED2KPartHashCount = nFileSize / PARTSIZE;
if (m_iED2KPartHashCount != 0) {
m_iED2KPartHashCount += 1;
}
}
void CKnownFile::AddUploadingClient(CUpDownClient *client)
{
m_ClientUploadList.insert(CCLIENTREF(client, "CKnownFile::AddUploadingClient m_ClientUploadList"));
SourceItemType type = UNAVAILABLE_SOURCE;
switch (client->GetUploadState()) {
case US_UPLOADING:
case US_ONUPLOADQUEUE:
type = AVAILABLE_SOURCE;
break;
default: {
// Any other state is UNAVAILABLE_SOURCE by default.
}
}
Notify_SharedCtrlAddClient(
this, CCLIENTREF(client, "CKnownFile::AddUploadingClient Notify_SharedCtrlAddClient"), type);
UpdateAutoUpPriority();
// GetQueuedCount() = m_ClientUploadList.size() — exported via EC.
MarkECChanged();
}
void CKnownFile::RemoveUploadingClient(CUpDownClient *client)
{
if (m_ClientUploadList.erase(CCLIENTREF(client, ""))) {
Notify_SharedCtrlRemoveClient(client->ECID(), this);
UpdateAutoUpPriority();
MarkECChanged();
}
}
#ifndef CLIENT_GUI
void CKnownFile::VerifyLocalData() const
{
CThreadScheduler::AddTask(new CVerifyLocalDataTask(GetFileHash()));
}
// Live upload activity summarised from m_ClientUploadList (issue #466).
// Core-only: the list is populated on the daemon; amulegui receives the
// results over EC. m_ClientUploadList holds both uploading and queued
// clients, so queued clients (datarate 0, state != US_UPLOADING) simply
// don't contribute.
uint32 CKnownFile::GetUploadDatarate() const
{
uint32 total = 0;
for (const CClientRef &ref : m_ClientUploadList) {
total += ref.GetUploadDatarate();
}
return total;
}
uint16 CKnownFile::GetTransferringClientCount() const
{
uint16 count = 0;
for (const CClientRef &ref : m_ClientUploadList) {
if (ref.GetUploadState() == US_UPLOADING) {
++count;
}
}
return count;
}
#endif // ! CLIENT_GUI
#ifdef CLIENT_GUI
CKnownFile::CKnownFile(const CEC_SharedFile_Tag *tag)
: CECID(tag->ID())
, statistic(this)
{
Init();
m_abyFileHash = tag->FileHash();
SetFileSize(tag->SizeFull());
m_AvailPartFrequency.insert(m_AvailPartFrequency.end(), m_iPartCount, 0);
m_queuedCount = 0;
m_uploadDatarateEC = 0;
m_transferringClientCountEC = 0;
}
CKnownFile::~CKnownFile() {}
void CKnownFile::UpdateAutoUpPriority() {}
#else // ! CLIENT_GUI
CKnownFile::~CKnownFile()
{
SourceSet::iterator it = m_ClientUploadList.begin();
for (; it != m_ClientUploadList.end(); ++it) {
it->ClearUploadFileID();
}
delete m_pAICHHashSet;
}
void CKnownFile::SetFilePath(const CPath &filePath)
{
m_filePath = filePath;
// EC exports the path printable for non-partfiles (EC_TAG_KNOWNFILE_FILENAME).
MarkECChanged();
}
// needed for memfiles. its probably better to switch everything to CFile...
bool CKnownFile::LoadHashsetFromFile(const CFileDataIO *file, bool checkhash)
{
CMD4Hash checkid = file->ReadHash();
uint16 parts = file->ReadUInt16();
m_hashlist.clear();
for (uint16 i = 0; i < parts; ++i) {
CMD4Hash cur_hash = file->ReadHash();
m_hashlist.push_back(cur_hash);
}
// SLUGFILLER: SafeHash - always check for valid m_hashlist
if (!checkhash) {
m_abyFileHash = checkid;
if (parts <= 1) { // nothing to check
return true;
}
} else {
if (m_abyFileHash != checkid) {
return false; // wrong file?
} else {
if (parts != GetED2KPartHashCount()) {
return false;
}
}
}
// SLUGFILLER: SafeHash
// trust noone ;-)
// lol, useless comment but made me lmao
// wtf you guys are weird.
if (!m_hashlist.empty()) {
CreateHashFromHashlist(m_hashlist, &checkid);
}
if (m_abyFileHash == checkid) {
return true;
} else {
m_hashlist.clear();
return false;
}
}
bool CKnownFile::LoadTagsFromFile(const CFileDataIO *file)
{
uint32 tagcount = file->ReadUInt32();
m_taglist.clear();
for (uint32 j = 0; j != tagcount; ++j) {
CTag newtag(*file, true);
switch (newtag.GetNameID()) {
case FT_FILENAME:
if (GetFileName().IsOk()) {
// Unlike eMule, we actually prefer the second
// filename tag, since we use it to specify the
// 'universial' filename (see CPath::ToUniv).
CPath path = CPath::FromUniv(newtag.GetStr());
// May be invalid, if from older versions where
// unicoded filenames be saved as empty-strings.
if (path.IsOk()) {
SetFileName(path);
}
} else {
SetFileName(CPath(newtag.GetStr()));
}
break;
case FT_FILESIZE:
SetFileSize(newtag.GetInt());
m_AvailPartFrequency.clear();
m_AvailPartFrequency.insert(m_AvailPartFrequency.begin(), GetPartCount(), 0);
break;
case FT_ATTRANSFERRED:
statistic.SetAllTimeTransferred(statistic.GetAllTimeTransferred() + newtag.GetInt());
break;
case FT_ATTRANSFERREDHI:
statistic.SetAllTimeTransferred(
statistic.GetAllTimeTransferred() + (((uint64)newtag.GetInt()) << 32));
break;
case FT_ATREQUESTED:
statistic.SetAllTimeRequests(newtag.GetInt());
break;
case FT_ATACCEPTED:
statistic.SetAllTimeAccepts(newtag.GetInt());
break;
case FT_ULPRIORITY:
m_iUpPriority = newtag.GetInt();
if (m_iUpPriority == PR_AUTO) {
m_iUpPriority = PR_HIGH;
m_bAutoUpPriority = true;
} else {
if (m_iUpPriority != PR_VERY_LOW && m_iUpPriority != PR_LOW &&
m_iUpPriority != PR_NORMAL && m_iUpPriority != PR_HIGH &&
m_iUpPriority != PR_VERYHIGH && m_iUpPriority != PR_POWERSHARE) {
m_iUpPriority = PR_NORMAL;
}
m_bAutoUpPriority = false;
}
break;
case FT_PERMISSIONS:
case FT_KADLASTPUBLISHKEY:
case FT_PARTFILENAME:
// Old tags, not used anymore. Just purge them.
break;
case FT_AICH_HASH: {
CAICHHash hash;
bool hashSizeOk = hash.DecodeBase32(newtag.GetStr()) == CAICHHash::GetHashSize();
wxASSERT(hashSizeOk);
if (hashSizeOk) {
m_pAICHHashSet->SetMasterHash(hash, AICH_HASHSETCOMPLETE);
// EC exports GetAICHMasterHash() as a wxString tag.
MarkECChanged();
}
break;
}
case FT_KADLASTPUBLISHSRC:
SetLastPublishTimeKadSrc(newtag.GetInt(), 0);
if (GetLastPublishTimeKadSrc() > (uint32)time(NULL) + KADEMLIAREPUBLISHTIMES) {
// There may be a possibility of an older client that saved a random number
// here.. This will check for that..
SetLastPublishTimeKadSrc(0, 0);
}
break;
case FT_KADLASTPUBLISHNOTES:
SetLastPublishTimeKadNotes(newtag.GetInt());
break;
case FT_LASTSEEN:
m_lastSeen = newtag.GetInt();
break;
case FT_LASTUPLOADED:
// Live upload-activity timestamp (issue #466). Absent on a
// known.met that predates the feature -> stays 0 (unknown).
m_lastUploadDatetime = static_cast<time_t>(newtag.GetInt());
break;
case FT_SHAREDSINCE:
m_dateShared = static_cast<time_t>(newtag.GetInt());
break;
default:
// Store them here and write them back on saving.
m_taglist.push_back(newtag);
}
}
return true;
}
bool CKnownFile::LoadDateFromFile(const CFileDataIO *file)
{
m_lastDateChanged = file->ReadUInt32();
return true;
}
bool CKnownFile::LoadFromFile(const CFileDataIO *file)
{
// SLUGFILLER: SafeHash - load first, verify later
bool ret1 = LoadDateFromFile(file);
bool ret2 = LoadHashsetFromFile(file, false);
bool ret3 = LoadTagsFromFile(file);
UpdatePartsInfo();
// Migration: a known.met written before FT_LASTSEEN was added
// leaves m_lastSeen at its Init() sentinel of 0. Fall back to
// the file's stored mtime as a proxy for "last known to be on
// disk at this name/date/size" -- accurate enough to drive the
// TTL prune on first save after upgrade rather than waiting a
// TTL window for all records to look "fresh now".
if (m_lastSeen == 0) {
m_lastSeen = (uint32)m_lastDateChanged;
}
// Final hash-count verification, needs to be done after the tags are loaded.
return ret1 && ret2 && ret3 && GetED2KPartHashCount() == GetHashCount();
// SLUGFILLER: SafeHash
}
bool CKnownFile::WriteToFile(CFileDataIO *file)
{
wxCHECK(!IsPartFile(), false);
// date
file->WriteUInt32((uint32)m_lastDateChanged);
// hashset
file->WriteHash(m_abyFileHash);
uint16 parts = m_hashlist.size();
file->WriteUInt16(parts);
for (int i = 0; i < parts; ++i)
file->WriteHash(m_hashlist[i]);
// tags
const int iFixedTags = 9; // +1 for FT_LASTSEEN
uint32 tagcount = iFixedTags;
if (HasProperAICHHashSet()) {
tagcount++;
}
// Float meta tags are currently not written. All older eMule versions < 0.28a have
// a bug in the meta tag reading+writing code. To achieve maximum backward
// compatibility for met files with older eMule versions we just don't write float
// tags. This is OK, because we (eMule) do not use float tags. The only float tags
// we may have to handle is the '# Sent' tag from the Hybrid, which is pretty
// useless but may be received from us via the servers.
//
// The code for writing the float tags SHOULD BE ENABLED in SOME MONTHS (after most
// people are using the newer eMule versions which do not write broken float tags).
for (size_t j = 0; j < m_taglist.size(); ++j) {
if (m_taglist[j].IsInt() || m_taglist[j].IsStr()) {
++tagcount;
}
}
if (m_lastPublishTimeKadSrc) {
++tagcount;
}
if (m_lastPublishTimeKadNotes) {
++tagcount;
}
// Upload-activity tags (issue #466) — only persisted once set.
if (m_lastUploadDatetime) {
++tagcount;
}
if (m_dateShared) {
++tagcount;
}
// standard tags
file->WriteUInt32(tagcount);
// We still save the unicoded filename, for backwards
// compatibility with pre-2.2 and other clients.
CTagString nametag_unicode(FT_FILENAME, GetFileName().GetRaw());
// We write it with BOM to keep eMule compatibility
nametag_unicode.WriteTagToFile(file, utf8strOptBOM);
// The non-unicoded filename is written in an 'universial'
// format, which allows us to identify files, even if the
// system locale changes.
CTagString nametag(FT_FILENAME, CPath::ToUniv(GetFileName()));
nametag.WriteTagToFile(file);
CTagIntSized sizetag(FT_FILESIZE, GetFileSize(), IsLargeFile() ? 64 : 32);
sizetag.WriteTagToFile(file);
// statistic
uint32 tran;
tran = statistic.GetAllTimeTransferred() & 0xFFFFFFFF;
CTagInt32 attag1(FT_ATTRANSFERRED, tran);
attag1.WriteTagToFile(file);
tran = statistic.GetAllTimeTransferred() >> 32;
CTagInt32 attag4(FT_ATTRANSFERREDHI, tran);
attag4.WriteTagToFile(file);
CTagInt32 attag2(FT_ATREQUESTED, statistic.GetAllTimeRequests());
attag2.WriteTagToFile(file);
CTagInt32 attag3(FT_ATACCEPTED, statistic.GetAllTimeAccepts());
attag3.WriteTagToFile(file);
// priority N permission
CTagInt32 priotag(FT_ULPRIORITY, IsAutoUpPriority() ? PR_AUTO : m_iUpPriority);
priotag.WriteTagToFile(file);
// Last time this record was matched against a real on-disk file
// (or freshly hashed). Drives the TTL prune in CKnownFileList.
CTagInt32 lastseentag(FT_LASTSEEN, m_lastSeen);
lastseentag.WriteTagToFile(file);
// AICH Filehash
if (HasProperAICHHashSet()) {
CTagString aichtag(FT_AICH_HASH, m_pAICHHashSet->GetMasterHash().GetString());
aichtag.WriteTagToFile(file);
}
// Kad sources
if (m_lastPublishTimeKadSrc) {
CTagInt32 kadLastPubSrc(FT_KADLASTPUBLISHSRC, m_lastPublishTimeKadSrc);
kadLastPubSrc.WriteTagToFile(file);
}
// Kad notes
if (m_lastPublishTimeKadNotes) {
CTagInt32 kadLastPubNotes(FT_KADLASTPUBLISHNOTES, m_lastPublishTimeKadNotes);
kadLastPubNotes.WriteTagToFile(file);
}
// Upload activity (issue #466)
if (m_lastUploadDatetime) {
CTagInt32 lastUpTag(FT_LASTUPLOADED, (uint32)m_lastUploadDatetime);
lastUpTag.WriteTagToFile(file);
}
if (m_dateShared) {
CTagInt32 sharedSinceTag(FT_SHAREDSINCE, (uint32)m_dateShared);
sharedSinceTag.WriteTagToFile(file);
}
// other tags
for (size_t j = 0; j < m_taglist.size(); ++j) {
if (m_taglist[j].IsInt() || m_taglist[j].IsStr()) {
m_taglist[j].WriteTagToFile(file);
}
}
return true;
}
void CKnownFile::CreateHashFromHashlist(const ArrayOfCMD4Hash &hashes, CMD4Hash *Output)
{
wxCHECK_RET(hashes.size(), "No input to hash from in CreateHashFromHashlist");
std::vector<uint8_t> buffer(hashes.size() * MD4HASH_LENGTH);
std::vector<uint8_t>::iterator it = buffer.begin();
for (size_t i = 0; i < hashes.size(); ++i) {
it = STLCopy_n(hashes[i].GetHash(), MD4HASH_LENGTH, it);
}
CreateHashFromInput(&buffer[0], buffer.size(), Output, NULL);
}
void CKnownFile::CreateHashFromFile(
CFileAutoClose &file, uint64 offset, uint32 Length, CMD4Hash *Output, CAICHHashTree *pShaHashOut)
{
wxCHECK_RET(Length, "No input to hash from in CreateHashFromFile");
CFileArea area;
area.ReadAt(file, offset, Length);
CreateHashFromInput(area.GetBuffer(), Length, Output, pShaHashOut);
area.CheckError();
}
void CKnownFile::CreateHashFromInput(
const uint8_t *input, uint32 Length, CMD4Hash *Output, CAICHHashTree *pShaHashOut)
{
wxASSERT_MSG(Output || pShaHashOut, "Nothing to do in CreateHashFromInput");
{
wxCHECK_RET(input, "No input to hash from in CreateHashFromInput");
}
wxASSERT(Length <= PARTSIZE); // We never hash more than one PARTSIZE
CMemFile data(input, Length);
uint32 Required = Length;
uint8 X[64 * 128];
uint32 posCurrentEMBlock = 0;