forked from amule-project/amule
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathClientTCPSocket.cpp
More file actions
2217 lines (1960 loc) · 71.5 KB
/
Copy pathClientTCPSocket.cpp
File metadata and controls
2217 lines (1960 loc) · 71.5 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.
//
// 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 "ClientTCPSocket.h" // Interface declarations
#include <protocol/Protocols.h>
#include <protocol/ed2k/Client2Client/TCP.h>
#include <protocol/ed2k/Client2Client/UDP.h> // Sometimes we reply with UDP packets.
#include <protocol/ed2k/ClientSoftware.h>
#include <protocol/kad2/Client2Client/TCP.h>
#include <common/EventIDs.h>
#include "Preferences.h" // Needed for thePrefs
#include "Packet.h" // Needed for CPacket
#include "Statistics.h" // Needed for theStats
#include "Logger.h" // Needed for logRemoteClient
#include "updownclient.h" // Needed for CUpDownClient
#include <common/Format.h> // Needed for CFormat
#include "amule.h" // Needed for theApp
#include "SharedFileList.h" // Needed for CSharedFileList
#include "ClientList.h" // Needed for CClientList
#include "UploadQueue.h" // Needed for CUploadQueue
#include "ClientUDPSocket.h" // Needed for CClientUDPSocket
#include "PartFile.h" // Needed for CPartFile
#include "MemFile.h" // Needed for CMemFile
#include "kademlia/kademlia/Kademlia.h" // Needed for CKademlia::Kademlia
#include "kademlia/kademlia/Prefs.h" // Needed for CKademlia::CPrefs
#include "DownloadQueue.h" // Needed for CDownloadQueue
#include "Server.h" // Needed for CServer
#include "ServerList.h" // Needed for CServerList
#include "ServerConnect.h" // Needed for CServerConnect::IsServerIP (#778)
#include "IPFilter.h" // Needed for CIPFilter
#include "ListenSocket.h" // Needed for CListenSocket
#include "GuiEvents.h" // Needed for Notify_*
// #define __PACKET_RECV_DUMP__
//------------------------------------------------------------------------------
// CClientTCPSocket
//------------------------------------------------------------------------------
CClientTCPSocket::CClientTCPSocket(CUpDownClient *in_client, const CProxyData *ProxyData)
: CEMSocket(ProxyData)
{
SetClient(in_client);
if (in_client) {
m_remoteip = wxUINT32_SWAP_ALWAYS(in_client->GetUserIDHybrid());
} else {
m_remoteip = 0;
}
ResetTimeOutTimer();
Notify(true);
theApp->listensocket->AddSocket(this);
theApp->listensocket->AddConnection();
}
CClientTCPSocket::~CClientTCPSocket()
{
if (m_client) {
m_client->SetSocket(NULL);
}
m_client = NULL;
if (theApp->listensocket && !theApp->listensocket->OnShutdown()) {
theApp->listensocket->RemoveSocket(this);
}
}
bool CClientTCPSocket::InitNetworkData()
{
wxASSERT(!m_remoteip);
wxASSERT(!m_client);
m_remoteip = GetPeerInt();
MULE_CHECK(m_remoteip, false);
if (theApp->ipfilter->IsFiltered(m_remoteip)) {
AddDebugLogLineN(logClient, "Denied connection from " + GetPeer() + "(Filtered IP)");
return false;
} else if (theApp->clientlist->IsBannedClient(m_remoteip)) {
AddDebugLogLineN(logClient, "Denied connection from " + GetPeer() + "(Banned IP)");
return false;
} else {
AddDebugLogLineN(logClient, "Accepted connection from " + GetPeer());
return true;
}
}
bool CClientTCPSocket::IsDownloadThrottled() const
{
// Inbound peer connection whose source IP is the ed2k server we're
// currently connected (or trying to connect) to -- this is the
// server's HighID-callback probe, not real peer download traffic.
// Skip the global download throttler so a saturated peer-side
// budget doesn't delay the probe's read path past the server's
// verification timer (#778). Same shape as CServerSocket's
// permanent bypass (#393 / 356a59c96), just gated on IP-match
// instead of being unconditional.
if (m_remoteip != 0 && theApp->serverconnect && theApp->serverconnect->IsServerIP(m_remoteip)) {
return false;
}
return true;
}
void CClientTCPSocket::ResetTimeOutTimer()
{
timeout_timer = ::GetTickCount64();
}
bool CClientTCPSocket::CheckTimeOut()
{
// 0.42x
uint64 uTimeout = GetTimeOut();
if (m_client) {
if (m_client->GetKadState() == KS_CONNECTED_BUDDY) {
// We originally ignored the timeout here for buddies.
// This was a stupid idea on my part. There is now a ping/pong system
// for buddies. This ping/pong system now prevents timeouts.
// This release will allow lowID clients with KadVersion 0 to remain connected.
// But a soon future version needs to allow these older clients to time out to prevent
// dead connections from continuing. JOHNTODO: Don't forget to remove backward support
// in a future release.
if (m_client->GetKadVersion() == 0) {
return false;
}
uTimeout += MIN2MS(15);
}
if (m_client->GetChatState() != MS_NONE) {
uTimeout += CONNECTION_TIMEOUT;
}
}
uint64 now = ::GetTickCount64();
if (now - timeout_timer > uTimeout) {
timeout_timer = now;
Disconnect("Timeout");
return true;
}
return false;
}
void CClientTCPSocket::SetClient(CUpDownClient *pClient)
{
m_client = pClient;
if (m_client) {
m_client->SetSocket(this);
}
}
void CClientTCPSocket::OnClose(int nErrorCode)
{
// 0.42x
wxASSERT(theApp->listensocket->IsValidSocket(this));
CEMSocket::OnClose(nErrorCode);
if (nErrorCode) {
Disconnect(CFormat("Closed: %u") % nErrorCode);
} else {
Disconnect("Close");
}
}
void CClientTCPSocket::Disconnect(const wxString &strReason)
{
byConnected = ES_DISCONNECTED;
if (m_client) {
if (m_client->Disconnected(strReason, true)) {
// Somehow, Safe_Delete() is being called by Disconnected(),
// or any other function that sets m_client to NULL,
// so we must check m_client first.
if (m_client) {
m_client->SetSocket(NULL);
m_client->Safe_Delete();
}
}
m_client = NULL;
}
Safe_Delete();
}
void CClientTCPSocket::Safe_Delete()
{
// More paranoia - make sure client is unlinked in any case
if (m_client) {
m_client->SetSocket(NULL);
m_client = NULL;
}
// Destroy may be called several times
byConnected = ES_DISCONNECTED;
Destroy();
}
void CClientTCPSocket::Safe_Delete_Client()
{
if (m_client) {
m_client->Safe_Delete();
m_client = NULL;
}
}
bool CClientTCPSocket::ProcessPacket(const uint8_t *buffer, uint32 size, uint8 opcode)
{
#ifdef __PACKET_RECV_DUMP__
// printf("Rec: OPCODE %x \n",opcode);
DumpMem(buffer, size);
#endif
if (!m_client && opcode != OP_HELLO) {
throw wxString("Asks for something without saying hello");
} else if (m_client && opcode != OP_HELLO && opcode != OP_HELLOANSWER) {
m_client->CheckHandshakeFinished();
}
switch (opcode) {
case OP_HELLOANSWER: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_HELLOANSWER from " + m_client->GetFullIP());
theStats::AddDownOverheadOther(size);
m_client->ProcessHelloAnswer(buffer, size);
// start secure identification, if
// - we have received OP_EMULEINFO and OP_HELLOANSWER (old eMule)
// - we have received eMule-OP_HELLOANSWER (new eMule)
if (m_client->GetInfoPacketsReceived() == IP_BOTH) {
m_client->InfoPacketsReceived();
}
// Socket might die because of sending in InfoPacketsReceived, so check
if (m_client) {
m_client->ConnectionEstablished();
}
// Socket might die on ConnectionEstablished somehow. Check it.
if (m_client) {
Notify_SharedCtrlRefreshClient(m_client->ECID(), AVAILABLE_SOURCE);
}
break;
}
case OP_HELLO: { // 0.43b
theStats::AddDownOverheadOther(size);
bool bNewClient = !m_client;
if (bNewClient) {
// create new client to save standard information
m_client = new CUpDownClient(this);
}
// Do not move up!
AddDebugLogLineN(logRemoteClient, "Remote Client: OP_HELLO from " + m_client->GetFullIP());
bool bIsMuleHello = false;
try {
bIsMuleHello = m_client->ProcessHelloPacket(buffer, size);
} catch (...) {
if (bNewClient && m_client) {
// Don't let CUpDownClient::Disconnected be processed for a client which is
// not in the list of clients.
m_client->Safe_Delete();
m_client = NULL;
}
throw;
}
if (thePrefs::ParanoidFilter() && !IsLowID(m_client->GetUserIDHybrid()) &&
(GetRemoteIP() != wxUINT32_SWAP_ALWAYS(m_client->GetUserIDHybrid()))) {
wxString reason = "Client claims a different IP from the one we received the hello "
"packet from: ";
reason += Uint32toStringIP(wxUINT32_SWAP_ALWAYS(m_client->GetUserIDHybrid())) +
" / " + Uint32toStringIP(GetRemoteIP());
AddDebugLogLineN(logClient, reason);
if (bNewClient) {
m_client->Safe_Delete();
m_client = NULL;
}
Disconnect("Paranoid disconnecting: " + reason);
return false;
}
// if IP is filtered, dont reply but disconnect...
if (theApp->ipfilter->IsFiltered(m_client->GetIP())) {
if (bNewClient) {
m_client->Safe_Delete();
m_client = NULL;
}
Disconnect("IPFilter");
return false;
}
wxASSERT(m_client);
// now we check if we know this client already. if yes this socket will
// be attached to the known client, the new client will be deleted
// and the var. "client" will point to the known client.
// if not we keep our new-constructed client ;)
if (theApp->clientlist->AttachToAlreadyKnown(&m_client, this)) {
// update the old client information
bIsMuleHello = m_client->ProcessHelloPacket(buffer, size);
} else {
theApp->clientlist->AddClient(m_client);
m_client->SetCommentDirty();
}
Notify_SharedCtrlRefreshClient(m_client->ECID(), AVAILABLE_SOURCE);
// send a response packet with standard information
if ((m_client->GetHashType() == SO_EMULE) && !bIsMuleHello) {
m_client->SendMuleInfoPacket(false);
}
// Client might die from Sending in SendMuleInfoPacket, so check
if (m_client) {
m_client->SendHelloAnswer();
}
// Kry - If the other side supports it, send OS_INFO
// Client might die from Sending in SendHelloAnswer, so check
if (m_client && m_client->GetOSInfoSupport()) {
m_client->SendMuleInfoPacket(
false, true); // Send the OS Info tag on the recycled Mule Info
}
// Client might die from Sending in SendMuleInfoPacket, so check
if (m_client) {
m_client->ConnectionEstablished();
}
// start secure identification, if
// - we have received eMule-OP_HELLO (new eMule)
if (m_client && m_client->GetInfoPacketsReceived() == IP_BOTH) {
m_client->InfoPacketsReceived();
}
break;
}
case OP_REQUESTFILENAME: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_REQUESTFILENAME from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
// IP banned, no answer for this request
if (m_client->IsBanned()) {
break;
}
if (size >= 16) {
if (!m_client->GetWaitStartTime()) {
m_client->SetWaitStartTime();
}
CMemFile data_in(buffer, size);
CMD4Hash reqfilehash = data_in.ReadHash();
CKnownFile *reqfile = theApp->sharedfiles->GetFileByID(reqfilehash);
if (reqfile == NULL) {
reqfile = theApp->downloadqueue->GetFileByID(reqfilehash);
if (!(reqfile != NULL && reqfile->GetFileSize() > PARTSIZE)) {
break;
}
}
// if we are downloading this file, this could be a new source
// no passive adding of files with only one part
if (reqfile->IsPartFile() && reqfile->GetFileSize() > PARTSIZE) {
if (thePrefs::GetMaxSourcePerFile() >
static_cast<CPartFile *>(reqfile)->GetSourceCount()) {
theApp->downloadqueue->CheckAndAddKnownSource(
static_cast<CPartFile *>(reqfile), m_client);
}
}
// check to see if this is a new file they are asking for
if (m_client->GetUploadFileID() != reqfilehash) {
m_client->SetCommentDirty();
}
m_client->SetUploadFileID(reqfile);
m_client->ProcessExtendedInfo(&data_in, reqfile);
// send filename etc
CMemFile data_out(128);
data_out.WriteHash(reqfile->GetFileHash());
// Since it's for somebody else to see, we need to send the prettified
// filename, rather than the (possibly) mangled actual filename.
data_out.WriteString(
reqfile->GetFileName().GetPrintable(), m_client->GetUnicodeSupport());
CPacket *packet = new CPacket(data_out, OP_EDONKEYPROT, OP_REQFILENAMEANSWER);
theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
AddDebugLogLineN(logLocalClient,
"Local Client: OP_REQFILENAMEANSWER to " + m_client->GetFullIP());
SendPacket(packet, true);
// SendPacket might kill the socket, so check
if (m_client)
m_client->SendCommentInfo(reqfile);
break;
}
throw wxString("Invalid OP_REQUESTFILENAME packet size");
break;
}
case OP_SETREQFILEID: { // 0.43b EXCEPT track of bad clients
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_SETREQFILEID from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
if (m_client->IsBanned()) {
break;
}
// DbT:FileRequest
if (size == 16) {
if (!m_client->GetWaitStartTime()) {
m_client->SetWaitStartTime();
}
const CMD4Hash fileID(buffer);
CKnownFile *reqfile = theApp->sharedfiles->GetFileByID(fileID);
if (reqfile == NULL) {
reqfile = theApp->downloadqueue->GetFileByID(fileID);
if (!(reqfile != NULL && reqfile->GetFileSize() > PARTSIZE)) {
CPacket *replypacket =
new CPacket(OP_FILEREQANSNOFIL, 16, OP_EDONKEYPROT);
replypacket->Copy16ToDataBuffer(fileID.GetHash());
theStats::AddUpOverheadFileRequest(replypacket->GetPacketSize());
AddDebugLogLineN(logLocalClient,
"Local Client: OP_FILERE to " + m_client->GetFullIP());
SendPacket(replypacket, true);
break;
}
}
// check to see if this is a new file they are asking for
if (m_client->GetUploadFileID() != fileID) {
m_client->SetCommentDirty();
}
m_client->SetUploadFileID(reqfile);
// send filestatus
CMemFile data(16 + 16);
data.WriteHash(reqfile->GetFileHash());
if (reqfile->IsPartFile()) {
static_cast<CPartFile *>(reqfile)->WritePartStatus(&data);
} else {
data.WriteUInt16(0);
}
CPacket *packet = new CPacket(data, OP_EDONKEYPROT, OP_FILESTATUS);
theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
AddDebugLogLineN(
logLocalClient, "Local Client: OP_FILESTATUS to " + m_client->GetFullIP());
SendPacket(packet, true);
break;
}
throw wxString("Invalid OP_FILEREQUEST packet size");
break;
// DbT:End
}
case OP_FILEREQANSNOFIL: { // 0.43b protocol, lacks ZZ's download manager on swap
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_FILEREQANSNOFIL from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
if (size == 16) {
// if that client does not have my file maybe has another different
CPartFile *reqfile = theApp->downloadqueue->GetFileByID(CMD4Hash(buffer));
if (reqfile) {
reqfile->AddDeadSource(m_client);
} else {
break;
}
// we try to swap to another file ignoring no needed parts files
switch (m_client->GetDownloadState()) {
case DS_CONNECTED:
case DS_ONQUEUE:
case DS_NONEEDEDPARTS:
if (!m_client->SwapToAnotherFile(true, true, true, NULL)) {
theApp->downloadqueue->RemoveSource(m_client);
}
break;
}
break;
}
throw wxString("Invalid OP_FILEREQUEST packet size");
break;
}
case OP_REQFILENAMEANSWER: { // 0.43b except check for bad clients
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_REQFILENAMEANSWER from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
CMemFile data(buffer, size);
CMD4Hash hash = data.ReadHash();
const CPartFile *file = theApp->downloadqueue->GetFileByID(hash);
m_client->ProcessFileInfo(&data, file);
break;
}
case OP_FILESTATUS: { // 0.43b except check for bad clients
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_FILESTATUS from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
CMemFile data(buffer, size);
CMD4Hash hash = data.ReadHash();
const CPartFile *file = theApp->downloadqueue->GetFileByID(hash);
m_client->ProcessFileStatus(false, &data, file);
break;
}
case OP_STARTUPLOADREQ: {
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_STARTUPLOADREQ from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
if (!m_client->CheckHandshakeFinished()) {
break;
}
m_client->CheckForAggressive();
if (m_client->IsBanned()) {
break;
}
if (size == 16) {
const CMD4Hash fileID(buffer);
CKnownFile *reqfile = theApp->sharedfiles->GetFileByID(fileID);
if (reqfile) {
if (m_client->GetUploadFileID() != fileID) {
m_client->SetCommentDirty();
}
m_client->SetUploadFileID(reqfile);
m_client->SendCommentInfo(reqfile);
// Socket might die because of SendCommentInfo, so check
if (m_client)
theApp->uploadqueue->AddClientToQueue(m_client);
}
}
break;
}
case OP_QUEUERANK: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_QUEUERANK from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
CMemFile data(buffer, size);
uint32 rank = data.ReadUInt32();
m_client->SetRemoteQueueRank(rank);
break;
}
case OP_ACCEPTUPLOADREQ: { // 0.42e (xcept khaos stats)
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_ACCEPTUPLOADREQ from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
if (m_client->GetRequestFile() && !m_client->GetRequestFile()->IsStopped() &&
(m_client->GetRequestFile()->GetStatus() == PS_READY ||
m_client->GetRequestFile()->GetStatus() == PS_EMPTY)) {
if (m_client->GetDownloadState() == DS_ONQUEUE) {
m_client->SetDownloadState(DS_DOWNLOADING);
m_client->SetLastPartAsked(
0xffff); // Reset current downloaded Chunk // Maella -Enhanced Chunk
// Selection- (based on jicxicmic)
m_client->SendBlockRequests();
}
} else {
if (!m_client->GetSentCancelTransfer()) {
CPacket *packet = new CPacket(OP_CANCELTRANSFER, 0, OP_EDONKEYPROT);
theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
AddDebugLogLineN(logLocalClient,
"Local Client: OP_CANCELTRANSFER to " + m_client->GetFullIP());
m_client->SendPacket(packet, true, true);
// SendPacket can cause the socket to die, so check
if (m_client)
m_client->SetSentCancelTransfer(1);
}
if (m_client)
m_client->SetDownloadState((m_client->GetRequestFile() == NULL ||
m_client->GetRequestFile()->IsStopped())
? DS_NONE
: DS_ONQUEUE);
}
break;
}
case OP_REQUESTPARTS: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_REQUESTPARTS from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
m_client->ProcessRequestPartsPacket(buffer, size, false);
break;
}
case OP_CANCELTRANSFER: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_CANCELTRANSFER from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
theApp->uploadqueue->RemoveFromUploadQueue(m_client);
AddDebugLogLineN(
logClient, m_client->GetUserName() + ": Upload session ended due canceled transfer.");
break;
}
case OP_END_OF_DOWNLOAD: { // 0.43b except check for bad clients
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_END_OF_DOWNLOAD from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
if (size >= 16 && m_client->GetUploadFileID() == CMD4Hash(buffer)) {
theApp->uploadqueue->RemoveFromUploadQueue(m_client);
AddDebugLogLineN(logClient,
m_client->GetUserName() + ": Upload session ended due ended transfer.");
}
break;
}
case OP_HASHSETREQUEST: { // 0.43b except check for bad clients
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_HASHSETREQUEST from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
if (size != 16) {
throw wxString("Invalid OP_HASHSETREQUEST packet size");
}
m_client->SendHashsetPacket(CMD4Hash(buffer));
break;
}
case OP_HASHSETANSWER: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_HASHSETANSWER from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
m_client->ProcessHashSet(buffer, size);
break;
}
case OP_SENDINGPART: { // 0.47a
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_SENDINGPART from " + m_client->GetFullIP());
if (m_client->GetRequestFile() && !m_client->GetRequestFile()->IsStopped() &&
(m_client->GetRequestFile()->GetStatus() == PS_READY ||
m_client->GetRequestFile()->GetStatus() == PS_EMPTY)) {
m_client->ProcessBlockPacket(buffer, size, false, false);
if (m_client && (m_client->GetRequestFile()->IsStopped() ||
m_client->GetRequestFile()->GetStatus() == PS_PAUSED ||
m_client->GetRequestFile()->GetStatus() == PS_ERROR)) {
if (!m_client->GetSentCancelTransfer()) {
CPacket *packet = new CPacket(OP_CANCELTRANSFER, 0, OP_EDONKEYPROT);
theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
AddDebugLogLineN(logLocalClient,
"Local Client: OP_CANCELTRANSFER to " +
m_client->GetFullIP());
m_client->SendPacket(packet, true, true);
// Socket might die because of SendPacket, so check
if (m_client)
m_client->SetSentCancelTransfer(1);
}
if (m_client)
m_client->SetDownloadState(m_client->GetRequestFile()->IsStopped()
? DS_NONE
: DS_ONQUEUE);
}
} else {
if (!m_client->GetSentCancelTransfer()) {
CPacket *packet = new CPacket(OP_CANCELTRANSFER, 0, OP_EDONKEYPROT);
theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
AddDebugLogLineN(logLocalClient,
"Local Client: OP_CANCELTRANSFER to " + m_client->GetFullIP());
m_client->SendPacket(packet, true, true);
// Socket might die because of SendPacket, so check
m_client->SetSentCancelTransfer(1);
}
m_client->SetDownloadState((m_client->GetRequestFile() == NULL ||
m_client->GetRequestFile()->IsStopped())
? DS_NONE
: DS_ONQUEUE);
}
break;
}
case OP_OUTOFPARTREQS: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_OUTOFPARTREQS from " + m_client->GetFullIP());
theStats::AddDownOverheadFileRequest(size);
if (m_client->GetDownloadState() == DS_DOWNLOADING) {
m_client->SetDownloadState(DS_ONQUEUE);
}
break;
}
case OP_CHANGE_CLIENT_ID: { // Kad reviewed
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_CHANGE_CLIENT_ID from " + m_client->GetFullIP());
theStats::AddDownOverheadOther(size);
CMemFile data(buffer, size);
uint32 nNewUserID = data.ReadUInt32();
uint32 nNewServerIP = data.ReadUInt32();
if (IsLowID(nNewUserID)) { // client changed server and gots a LowID
CServer *pNewServer = theApp->serverlist->GetServerByIP(nNewServerIP);
if (pNewServer != NULL) {
m_client->SetUserIDHybrid(
nNewUserID); // update UserID only if we know the server
m_client->SetServerIP(nNewServerIP);
m_client->SetServerPort(pNewServer->GetPort());
}
} else if (nNewUserID == m_client->GetIP()) { // client changed server and gots a HighID(IP)
m_client->SetUserIDHybrid(wxUINT32_SWAP_ALWAYS(nNewUserID));
CServer *pNewServer = theApp->serverlist->GetServerByIP(nNewServerIP);
if (pNewServer != NULL) {
m_client->SetServerIP(nNewServerIP);
m_client->SetServerPort(pNewServer->GetPort());
}
}
break;
}
case OP_CHANGE_SLOT: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_CHANGE_SLOT from " + m_client->GetFullIP());
// sometimes sent by Hybrid
theStats::AddDownOverheadOther(size);
break;
}
case OP_MESSAGE: { // 0.43b
AddDebugLogLineN(logRemoteClient, "Remote Client: OP_MESSAGE from " + m_client->GetFullIP());
theStats::AddDownOverheadOther(size);
if (size < 2) {
throw wxString("invalid message packet");
}
CMemFile message_file(buffer, size);
uint16 length = message_file.ReadUInt16();
if (length + 2u != size) {
throw wxString("invalid message packet");
}
// limit message length
static const uint16 MAX_CLIENT_MSG_LEN = 450;
if (length > MAX_CLIENT_MSG_LEN) {
AddDebugLogLineN(logRemoteClient,
CFormat("Message from '%s' (IP:%s) exceeds limit by %u chars, truncated.") %
m_client->GetUserName() % m_client->GetFullIP() %
(length - MAX_CLIENT_MSG_LEN));
length = MAX_CLIENT_MSG_LEN;
}
wxString message =
message_file.ReadOnlyString((m_client->GetUnicodeSupport() != utf8strNone), length);
m_client->ProcessChatMessage(message);
break;
}
case OP_ASKSHAREDFILES: { // 0.43b (well, er, it does the same, but in our own way)
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_ASKSHAREDFILES from " + m_client->GetFullIP());
// client wants to know what we have in share, let's see if we allow him to know that
theStats::AddDownOverheadOther(size);
// IP banned, no answer for this request
if (m_client->IsBanned()) {
break;
}
if (thePrefs::CanSeeShares() == vsfaEverybody ||
(thePrefs::CanSeeShares() == vsfaFriends && m_client->IsFriend())) {
AddLogLineC(CFormat(_("User %s (%u) requested your sharedfiles-list -> Accepted")) %
m_client->GetUserName() % m_client->GetUserIDHybrid());
std::vector<CKnownFile *> list;
theApp->sharedfiles->CopyFileList(list);
CMemFile tempfile(80);
tempfile.WriteUInt32(list.size());
for (unsigned i = 0; i < list.size(); ++i) {
if (!list[i]->IsLargeFile() || m_client->SupportsLargeFiles()) {
list[i]->CreateOfferedFilePacket(&tempfile, NULL, m_client);
}
}
// create a packet and send it
CPacket *replypacket = new CPacket(tempfile, OP_EDONKEYPROT, OP_ASKSHAREDFILESANSWER);
AddDebugLogLineN(logLocalClient,
"Local Client: OP_ASKSHAREDFILESANSWER to " + m_client->GetFullIP());
theStats::AddUpOverheadOther(replypacket->GetPacketSize());
SendPacket(replypacket, true, true);
} else {
AddLogLineC(CFormat(_("User %s (%u) requested your sharedfiles-list -> Denied")) %
m_client->GetUserName() % m_client->GetUserIDHybrid());
CPacket *replypacket = new CPacket(OP_ASKSHAREDDENIEDANS, 0, OP_EDONKEYPROT);
theStats::AddUpOverheadOther(replypacket->GetPacketSize());
AddDebugLogLineN(logLocalClient,
"Local Client: OP_ASKSHAREDDENIEDANS to " + m_client->GetFullIP());
SendPacket(replypacket, true, true);
}
break;
}
case OP_ASKSHAREDFILESANSWER: { // 0.43b
AddDebugLogLineN(logRemoteClient,
"Remote Client: OP_ASKSHAREDFILESANSWER from " + m_client->GetFullIP());
theStats::AddDownOverheadOther(size);
wxString EmptyStr;
m_client->ProcessSharedFileList(buffer, size, EmptyStr);
break;
}
case OP_ASKSHAREDDIRS: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_ASKSHAREDDIRS from " + m_client->GetFullIP());
theStats::AddDownOverheadOther(size);
wxASSERT(size == 0);
// IP banned, no answer for this request
if (m_client->IsBanned()) {
break;
}
if ((thePrefs::CanSeeShares() == vsfaEverybody) ||
((thePrefs::CanSeeShares() == vsfaFriends) && m_client->IsFriend())) {
AddLogLineC(
CFormat(_("User %s (%u) requested your shareddirectories-list -> Accepted")) %
m_client->GetUserName() % m_client->GetUserIDHybrid());
// send the list of shared directories
m_client->SendSharedDirectories();
} else {
AddLogLineC(
CFormat(_("User %s (%u) requested your shareddirectories-list -> Denied")) %
m_client->GetUserName() % m_client->GetUserIDHybrid());
CPacket *replypacket = new CPacket(OP_ASKSHAREDDENIEDANS, 0, OP_EDONKEYPROT);
theStats::AddUpOverheadOther(replypacket->GetPacketSize());
AddDebugLogLineN(logLocalClient,
"Local Client: OP_ASKSHAREDDENIEDANS to " + m_client->GetFullIP());
SendPacket(replypacket, true, true);
}
break;
}
case OP_ASKSHAREDFILESDIR: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_ASKSHAREDFILESDIR from " + m_client->GetFullIP());
theStats::AddDownOverheadOther(size);
// IP banned, no answer for this request
if (m_client->IsBanned()) {
break;
}
CMemFile data(buffer, size);
wxString strReqDir = data.ReadString((m_client->GetUnicodeSupport() != utf8strNone));
if (thePrefs::CanSeeShares() == vsfaEverybody ||
(thePrefs::CanSeeShares() == vsfaFriends && m_client->IsFriend())) {
AddLogLineC(CFormat(_("User %s (%u) requested your sharedfiles-list for directory "
"'%s' -> accepted")) %
m_client->GetUserName() % m_client->GetUserIDHybrid() % strReqDir);
if (data.GetPosition() != data.GetLength()) {
// eMule mods routinely append extra tag blocks at
// the end of OP packets specifically so older
// clients can ignore them. Log and continue (#708).
AddDebugLogLineN(logRemoteClient,
CFormat("OP_ASKSHAREDFILESDIR: %u trailing byte(s) ignored from %s") %
(unsigned)(data.GetLength() - data.GetPosition()) %
m_client->GetFullIP());
}
// send the list of shared files for the requested directory
m_client->SendSharedFilesOfDirectory(strReqDir);
} else {
AddLogLineC(CFormat(_("User %s (%u) requested your sharedfiles-list for directory "
"'%s' -> denied")) %
m_client->GetUserName() % m_client->GetUserIDHybrid() % strReqDir);
CPacket *replypacket = new CPacket(OP_ASKSHAREDDENIEDANS, 0, OP_EDONKEYPROT);
theStats::AddUpOverheadOther(replypacket->GetPacketSize());
AddDebugLogLineN(logLocalClient,
"Local Client: OP_ASKSHAREDDENIEDANS to " + m_client->GetFullIP());
SendPacket(replypacket, true, true);
}
break;
}
case OP_ASKSHAREDDIRSANS: { // 0.43b
AddDebugLogLineN(
logRemoteClient, "Remote Client: OP_ASKSHAREDDIRSANS from " + m_client->GetFullIP());
theStats::AddDownOverheadOther(size);
if (m_client->GetFileListRequested() == 1) {
CMemFile data(buffer, size);
uint32 uDirs = data.ReadUInt32();
for (uint32 i = 0; i < uDirs; i++) {
wxString strDir =
data.ReadString((m_client->GetUnicodeSupport() != utf8strNone));
AddLogLineC(CFormat(_("User %s (%u) shares directory '%s'")) %
m_client->GetUserName() % m_client->GetUserIDHybrid() % strDir);
CMemFile tempfile(80);
tempfile.WriteString(strDir, m_client->GetUnicodeSupport());
CPacket *replypacket =
new CPacket(tempfile, OP_EDONKEYPROT, OP_ASKSHAREDFILESDIR);
theStats::AddUpOverheadOther(replypacket->GetPacketSize());
AddDebugLogLineN(logLocalClient,
"Local Client: OP_ASKSHAREDFILESDIR to " + m_client->GetFullIP());
SendPacket(replypacket, true, true);
}
if (data.GetPosition() != data.GetLength()) {
// eMule mods routinely append extra tag blocks at
// the end of OP packets specifically so older
// clients can ignore them. Log and continue (#708).
AddDebugLogLineN(logRemoteClient,
CFormat("OP_ASKSHAREDDIRSANS: %u trailing byte(s) ignored from %s") %
(unsigned)(data.GetLength() - data.GetPosition()) %
m_client->GetFullIP());
}
m_client->SetFileListRequested(uDirs);
// Total directory count drives the browse progress bar percent.
m_client->SetBrowseTotalDirs(static_cast<int>(uDirs));
} else {
AddLogLineC(CFormat(_("User %s (%u) sent unrequested shared dirs.")) %
m_client->GetUserName() % m_client->GetUserIDHybrid());
}
break;
}
case OP_ASKSHAREDFILESDIRANS: { // 0.43b
AddDebugLogLineN(logRemoteClient,
"Remote Client: OP_ASKSHAREDFILESDIRANS from " + m_client->GetFullIP());
theStats::AddDownOverheadOther(size);
CMemFile data(buffer, size);
wxString strDir = data.ReadString((m_client->GetUnicodeSupport() != utf8strNone));
if (m_client->GetFileListRequested() > 0) {
AddLogLineC(CFormat(_("User %s (%u) sent sharedfiles-list for directory '%s'")) %
m_client->GetUserName() % m_client->GetUserIDHybrid() % strDir);
m_client->ProcessSharedFileList(
buffer + data.GetPosition(), size - data.GetPosition(), strDir);
if (m_client->GetFileListRequested() == 0) {
AddLogLineC(CFormat(_("User %s (%u) finished sending sharedfiles-list")) %
m_client->GetUserName() % m_client->GetUserIDHybrid());
m_client->MarkBrowse(BROWSE_FINISHED);
}
} else {