2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "ClientList.h" // Interface declarations.
28 #include <protocol/Protocols.h>
29 #include <protocol/ed2k/Constants.h>
30 #include <protocol/kad/Client2Client/UDP.h>
31 #include <protocol/kad/Constants.h>
32 #include <protocol/kad2/Client2Client/TCP.h>
34 #include "amule.h" // Needed for theApp
35 #include "ClientTCPSocket.h" // Needed for CClientTCPSocket
36 #include "DownloadQueue.h" // Needed for CDownloadQueue
37 #include "UploadQueue.h" // Needed for CUploadQueue
38 #include "IPFilter.h" // Needed for CIPFIlter
39 #include "updownclient.h" // Needed for CUpDownClient
40 #include "Preferences.h" // Needed for thePrefs
41 #include "Statistics.h" // Needed for theStats
43 #include "GuiEvents.h" // Needed for Notify_*
46 #include <common/Format.h>
48 #include "kademlia/kademlia/Search.h"
49 #include "kademlia/kademlia/SearchManager.h"
50 #include "kademlia/kademlia/UDPFirewallTester.h"
51 #include "kademlia/net/KademliaUDPListener.h"
52 #include "kademlia/routing/Contact.h"
56 * CDeletedClient Class
58 * This class / list is a bit overkill, but currently needed to avoid any
59 * exploit possibility. It will keep track of certain clients attributes
60 * for 2 hours, while the CUpDownClient object might be deleted already.
61 * Currently saves: IP, Port, UserHash.
66 CDeletedClient(CUpDownClient
* pClient
)
68 m_dwInserted
= ::GetTickCount();
69 PortAndHash porthash
= { pClient
->GetUserPort(), pClient
->GetCreditsHash()};
70 m_ItemsList
.push_back(porthash
);
79 typedef std::list
<PortAndHash
> PaHList
;
86 CClientList::CClientList()
87 : m_deadSources( true )
89 m_dwLastBannCleanUp
= 0;
90 m_dwLastTrackedCleanUp
= 0;
91 m_dwLastClientCleanUp
= 0;
93 m_nBuddyStatus
= Disconnected
;
95 m_delete_queue_closed
= false;
100 CClientList::~CClientList()
102 std::map
<uint32
, CDeletedClient
*>::iterator it
= m_trackedClientsList
.begin();
103 for ( ; it
!= m_trackedClientsList
.end(); ++it
){
107 m_trackedClientsList
.clear();
109 wxASSERT(m_clientList
.empty());
110 wxASSERT(m_delete_queue
.empty());
114 void CClientList::AddClient( CUpDownClient
* toadd
)
116 // Ensure that only new clients can be added to the list
117 if ( toadd
->GetClientState() == CS_NEW
) {
118 // Update the client-state
119 toadd
->m_clientState
= CS_LISTED
;
121 Notify_ClientCtrlAddClient( toadd
);
123 // We always add the ID/ptr pair, regardles of the actual ID value
124 m_clientList
.insert( IDMapPair( toadd
->GetUserIDHybrid(), toadd
) );
126 // We only add the IP if it is valid
127 if ( toadd
->GetIP() ) {
128 m_ipList
.insert( IDMapPair( toadd
->GetIP(), toadd
) );
131 // We only add the hash if it is valid
132 if ( toadd
->HasValidHash() ) {
133 m_hashList
.insert( HashMapPair( toadd
->GetUserHash(), toadd
) );
136 toadd
->UpdateStats();
141 void CClientList::AddToDeleteQueue(CUpDownClient
* client
)
143 RemoveFromKadList( client
);
144 RemoveDirectCallback( client
);
146 // We have to remove the client from the list immediatly, to avoit it getting
147 // found by functions such as AttachToAlreadyKnown and GetClientsFromIP,
148 // however, if the client isn't on the clientlist, then it is safe to delete
149 // it right now. Otherwise, push it onto the queue.
150 if ( RemoveIDFromList( client
) ) {
151 // Also remove the ip and hash entries
152 RemoveIPFromList( client
);
153 RemoveHashFromList( client
);
155 wxASSERT(!m_delete_queue_closed
);
156 m_delete_queue
.push_back( client
);
163 void CClientList::UpdateClientID( CUpDownClient
* client
, uint32 newID
)
166 if ( ( client
->GetClientState() != CS_LISTED
) || ( client
->GetUserIDHybrid() == newID
) )
169 // First remove the ID entry
170 RemoveIDFromList( client
);
173 m_clientList
.insert( IDMapPair( newID
, client
) );
177 void CClientList::UpdateClientIP( CUpDownClient
* client
, uint32 newIP
)
180 if ( ( client
->GetClientState() != CS_LISTED
) || ( client
->GetIP() == newIP
) )
183 // Remove the old IP entry
184 RemoveIPFromList( client
);
187 m_ipList
.insert( IDMapPair( newIP
, client
) );
192 void CClientList::UpdateClientHash( CUpDownClient
* client
, const CMD4Hash
& newHash
)
195 if ( ( client
->GetClientState() != CS_LISTED
) || ( client
->GetUserHash() == newHash
) )
199 // Remove the old entry
200 RemoveHashFromList( client
);
202 // And add the new one if valid
203 if ( !newHash
.IsEmpty() ) {
204 m_hashList
.insert( HashMapPair( newHash
, client
) );
209 bool CClientList::RemoveIDFromList( CUpDownClient
* client
)
213 // First remove the ID entry
214 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_clientList
.equal_range( client
->GetUserIDHybrid() );
216 for ( ; range
.first
!= range
.second
; ++range
.first
) {
217 if ( client
== range
.first
->second
) {
218 /* erase() will invalidate the iterator, but we're not using it anymore
219 anyway (notice the break;) */
220 m_clientList
.erase( range
.first
);
231 void CClientList::RemoveIPFromList( CUpDownClient
* client
)
233 // Check if we need to look for the IP entry
234 if ( !client
->GetIP() ) {
238 // Remove the IP entry
239 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range( client
->GetIP() );
241 for ( ; range
.first
!= range
.second
; ++range
.first
) {
242 if ( client
== range
.first
->second
) {
243 /* erase() will invalidate the iterator, but we're not using it anymore
244 anyway (notice the break;) */
245 m_ipList
.erase( range
.first
);
251 void CClientList::RemoveHashFromList( CUpDownClient
* client
)
254 if ( !client
->HasValidHash() ) {
258 // Find all items with the specified hash
259 std::pair
<HashMap::iterator
, HashMap::iterator
> range
= m_hashList
.equal_range( client
->GetUserHash() );
261 for ( ; range
.first
!= range
.second
; ++range
.first
) {
262 if ( client
== range
.first
->second
) {
263 /* erase() will invalidate the iterator, but we're not using it anymore
264 anyway (notice the break;) */
265 m_hashList
.erase( range
.first
);
272 CUpDownClient
* CClientList::FindMatchingClient( CUpDownClient
* client
)
274 typedef std::pair
<IDMap::const_iterator
, IDMap::const_iterator
> IDMapIteratorPair
;
275 wxCHECK(client
, NULL
);
277 const uint32 userIP
= client
->GetIP();
278 const uint32 userID
= client
->GetUserIDHybrid();
279 const uint16 userPort
= client
->GetUserPort();
280 const uint16 userKadPort
= client
->GetKadPort();
283 // LowID clients need a different set of checks
284 if (client
->HasLowID()) {
285 // User is firewalled ... Must do two checks.
286 if (userIP
&& (userPort
|| userKadPort
)) {
287 IDMapIteratorPair range
= m_ipList
.equal_range(userIP
);
289 for ( ; range
.first
!= range
.second
; ++range
.first
) {
290 CUpDownClient
* other
= range
.first
->second
;
291 wxASSERT(userIP
== other
->GetIP());
293 if (userPort
&& (userPort
== other
->GetUserPort())) {
295 } else if (userKadPort
&& (userKadPort
== other
->GetKadPort())) {
301 const uint32 serverIP
= client
->GetServerIP();
302 const uint32 serverPort
= client
->GetServerPort();
303 if (userID
&& serverIP
&& serverPort
) {
304 IDMapIteratorPair range
= m_clientList
.equal_range(userID
);
306 for (; range
.first
!= range
.second
; ++range
.first
) {
307 CUpDownClient
* other
= range
.first
->second
;
308 wxASSERT(userID
== other
->GetUserIDHybrid());
310 // For lowid, we also have to check the server
311 if (serverIP
== other
->GetServerIP()) {
312 if (serverPort
== other
->GetServerPort()) {
318 } else if (userPort
|| userKadPort
) {
319 // Check by IP first, then by ID
320 struct { const IDMap
& map
; uint32 value
; } toCheck
[] = {
321 { m_ipList
, userIP
}, { m_clientList
, userID
}
324 for (size_t i
= 0; i
< itemsof(toCheck
); ++i
) {
325 if (toCheck
[i
].value
== 0) {
326 // We may not have both (or any) of these values.
330 IDMapIteratorPair range
= toCheck
[i
].map
.equal_range(toCheck
[i
].value
);
333 IDMap::const_iterator it
= range
.first
;
334 for (; it
!= range
.second
; ++it
) {
335 if (userPort
== it
->second
->GetUserPort()) {
342 IDMap::const_iterator it
= range
.first
;
343 for (; it
!= range
.second
; ++it
) {
344 if (userKadPort
== it
->second
->GetKadPort()) {
353 // If anything else fails, then we look at hashes
354 if ( client
->HasValidHash() ) {
355 // Find all items with the specified hash
356 std::pair
<HashMap::iterator
, HashMap::iterator
> range
= m_hashList
.equal_range( client
->GetUserHash() );
358 // Just return the first item if any
359 if ( range
.first
!= range
.second
) {
360 return range
.first
->second
;
364 // Nothing found, must be a new client
369 uint32
CClientList::GetClientCount() const
371 return m_clientList
.size();
375 void CClientList::DeleteAll()
380 while ( !m_clientList
.empty() ) {
381 IDMap::iterator it
= m_clientList
.begin();
383 // Will call the removal of the item on this same class
384 it
->second
->Disconnected(wxT("Removed while deleting all from ClientList."));
385 it
->second
->Safe_Delete();
388 // Clean up the clients now queued for deletion
390 m_delete_queue_closed
= true;
392 ProcessDeleteQueue();
396 bool CClientList::AttachToAlreadyKnown(CUpDownClient
** client
, CClientTCPSocket
* sender
)
398 CUpDownClient
* tocheck
= (*client
);
400 CUpDownClient
* found_client
= FindMatchingClient( tocheck
);
402 if ( tocheck
== found_client
) {
403 // We found the same client instance (client may have sent more than one OP_HELLO). do not delete that client!
407 if (found_client
!= NULL
){
409 if (found_client
->GetSocket()) {
410 if (found_client
->IsConnected()
411 && (found_client
->GetIP() != tocheck
->GetIP() || found_client
->GetUserPort() != tocheck
->GetUserPort() ) )
413 // if found_client is connected and has the IS_IDENTIFIED, it's safe to say that the other one is a bad guy
414 if (found_client
->IsIdentified()){
415 AddDebugLogLineM( false, logClient
, wxT("Client: ") + tocheck
->GetUserName() + wxT("(") + tocheck
->GetFullIP() + wxT("), Banreason: Userhash invalid"));
420 AddDebugLogLineM( false, logClient
, wxT("WARNING! Found matching client, to a currently connected client: ")
421 + tocheck
->GetUserName() + wxT("(") + tocheck
->GetFullIP()
422 + wxT(") and ") + found_client
->GetUserName() + wxT("(") + found_client
->GetFullIP() + wxT(")"));
425 found_client
->GetSocket()->Safe_Delete();
427 found_client
->SetSocket( sender
);
428 tocheck
->SetSocket( NULL
);
431 tocheck
->Safe_Delete();
432 *client
= found_client
;
440 CUpDownClient
* CClientList::FindClientByIP( uint32 clientip
, uint16 port
)
442 // Find all items with the specified ip
443 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range( clientip
);
445 for ( ; range
.first
!= range
.second
; ++range
.first
) {
446 CUpDownClient
* cur_client
= range
.first
->second
;
447 // Check if it's actually the client we want
448 if ( cur_client
->GetUserPort() == port
) {
457 bool CClientList::IsIPAlreadyKnown(uint32_t ip
)
459 // Find all items with the specified ip
460 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range(ip
);
461 return range
.first
!= range
.second
;
465 bool CClientList::ComparePriorUserhash(uint32 dwIP
, uint16 nPort
, void* pNewHash
)
467 std::map
<uint32
, CDeletedClient
*>::iterator it
= m_trackedClientsList
.find( dwIP
);
469 if ( it
!= m_trackedClientsList
.end() ) {
470 CDeletedClient
* pResult
= it
->second
;
472 CDeletedClient::PaHList::iterator it2
= pResult
->m_ItemsList
.begin();
473 for ( ; it2
!= pResult
->m_ItemsList
.end(); ++it2
) {
474 if ( it2
->nPort
== nPort
) {
475 if ( it2
->pHash
!= pNewHash
) {
487 void CClientList::AddTrackClient(CUpDownClient
* toadd
)
489 std::map
<uint32
, CDeletedClient
*>::iterator it
= m_trackedClientsList
.find( toadd
->GetIP() );
491 if ( it
!= m_trackedClientsList
.end() ) {
492 CDeletedClient
* pResult
= it
->second
;
494 pResult
->m_dwInserted
= ::GetTickCount();
496 CDeletedClient::PaHList::iterator it2
= pResult
->m_ItemsList
.begin();
497 for ( ; it2
!= pResult
->m_ItemsList
.end(); ++it2
) {
498 if ( it2
->nPort
== toadd
->GetUserPort() ) {
499 // already tracked, update
500 it2
->pHash
= toadd
->GetCreditsHash();
505 // New client for that IP, add an entry
506 CDeletedClient::PortAndHash porthash
= { toadd
->GetUserPort(), toadd
->GetCreditsHash()};
507 pResult
->m_ItemsList
.push_back(porthash
);
509 m_trackedClientsList
[ toadd
->GetIP() ] = new CDeletedClient(toadd
);
514 uint16
CClientList::GetClientsFromIP(uint32 dwIP
)
516 std::map
<uint32
, CDeletedClient
*>::iterator it
= m_trackedClientsList
.find( dwIP
);
518 if ( it
!= m_trackedClientsList
.end() ) {
519 return it
->second
->m_ItemsList
.size();
526 void CClientList::ProcessDeleteQueue()
528 // Delete pending clients
529 while ( !m_delete_queue
.empty() ) {
530 CUpDownClient
* toremove
= m_delete_queue
.front();
531 m_delete_queue
.pop_front();
533 // Doing what RemoveClient used to do. Just to be sure...
534 theApp
->uploadqueue
->RemoveFromUploadQueue( toremove
);
535 theApp
->uploadqueue
->RemoveFromWaitingQueue( toremove
);
536 theApp
->downloadqueue
->RemoveSource( toremove
);
538 Notify_ClientCtrlRemoveClient( toremove
);
545 void CClientList::Process()
547 const uint32 cur_tick
= ::GetTickCount();
549 ProcessDeleteQueue();
551 if (m_dwLastBannCleanUp
+ BAN_CLEANUP_TIME
< cur_tick
) {
552 m_dwLastBannCleanUp
= cur_tick
;
554 ClientMap::iterator it
= m_bannedList
.begin();
555 while ( it
!= m_bannedList
.end() ) {
556 if ( it
->second
+ CLIENTBANTIME
< cur_tick
) {
557 ClientMap::iterator tmp
= it
++;
559 m_bannedList
.erase( tmp
);
560 theStats::RemoveBannedClient();
568 if ( m_dwLastTrackedCleanUp
+ TRACKED_CLEANUP_TIME
< cur_tick
) {
569 m_dwLastTrackedCleanUp
= cur_tick
;
571 std::map
<uint32
, CDeletedClient
*>::iterator it
= m_trackedClientsList
.begin();
572 while ( it
!= m_trackedClientsList
.end() ) {
573 std::map
<uint32
, CDeletedClient
*>::iterator cur_src
= it
++;
575 if ( cur_src
->second
->m_dwInserted
+ KEEPTRACK_TIME
< cur_tick
) {
576 delete cur_src
->second
;
577 m_trackedClientsList
.erase( cur_src
);
582 //We need to try to connect to the clients in m_KadList
583 //If connected, remove them from the list and send a message back to Kad so we can send a ACK.
584 //If we don't connect, we need to remove the client..
585 //The sockets timeout should delete this object.
587 // buddy is just a flag that is used to make sure we are still connected or connecting to a buddy.
588 buddyState buddy
= Disconnected
;
590 std::set
<CUpDownClient
*>::iterator current_it
= m_KadSources
.begin();
591 while (current_it
!= m_KadSources
.end()) {
592 CUpDownClient
* cur_client
= *current_it
;
593 ++current_it
; // Won't be used anymore till while loop
594 if( !Kademlia::CKademlia::IsRunning() ) {
595 //Clear out this list if we stop running Kad.
596 //Setting the Kad state to KS_NONE causes it to be removed in the switch below.
597 cur_client
->SetKadState(KS_NONE
);
599 switch (cur_client
->GetKadState()) {
600 case KS_QUEUED_FWCHECK
:
601 case KS_QUEUED_FWCHECK_UDP
:
602 //Another client asked us to try to connect to them to check their firewalled status.
603 cur_client
->TryToConnect(true);
606 case KS_CONNECTING_FWCHECK
:
607 //Ignore this state as we are just waiting for results.
611 case KS_CONNECTING_FWCHECK_UDP
:
612 // We want a UDP firewallcheck from this client and are just waiting to get connected to send the request
615 case KS_CONNECTED_FWCHECK
:
616 //We successfully connected to the client.
617 //We now send a ack to let them know.
618 if (cur_client
->GetKadVersion() >= 7) {
619 // The result is now sent per TCP instead of UDP, because this will fail if our intern port is unreachable.
620 // But we want the TCP testresult regardless if UDP is firewalled, the new UDP state and test takes care of the rest
621 wxASSERT(cur_client
->IsConnected());
622 //AddDebugLogLineM(false, logClient, wxT("Sent OP_KAD_FWTCPCHECK_ACK"));
623 CPacket
*packet
= new CPacket(OP_KAD_FWTCPCHECK_ACK
, 0, OP_EMULEPROT
);
624 cur_client
->SafeSendPacket(packet
);
626 DebugSend(KadFirewalledAckRes
, wxUINT32_SWAP_ALWAYS(cur_client
->GetIP()), cur_client
->GetKadPort());
627 Kademlia::CKademlia::GetUDPListener()->SendNullPacket(KADEMLIA_FIREWALLED_ACK_RES
, wxUINT32_SWAP_ALWAYS(cur_client
->GetIP()), cur_client
->GetKadPort(), 0, NULL
);
629 //We are done with this client. Set Kad status to KS_NONE and it will be removed in the next cycle.
630 cur_client
->SetKadState(KS_NONE
);
633 case KS_INCOMING_BUDDY
:
634 //A firewalled client wants us to be his buddy.
635 //If we already have a buddy, we set Kad state to KS_NONE and it's removed in the next cycle.
636 //If not, this client will change to KS_CONNECTED_BUDDY when it connects.
637 if( m_nBuddyStatus
== Connected
) {
638 cur_client
->SetKadState(KS_NONE
);
642 case KS_QUEUED_BUDDY
:
643 //We are firewalled and want to request this client to be a buddy.
644 //But first we check to make sure we are not already trying another client.
645 //If we are not already trying. We try to connect to this client.
646 //If we are already connected to a buddy, we set this client to KS_NONE and it's removed next cycle.
647 //If we are trying to connect to a buddy, we just ignore as the one we are trying may fail and we can then try this one.
648 if( m_nBuddyStatus
== Disconnected
) {
650 m_nBuddyStatus
= Connecting
;
651 cur_client
->SetKadState(KS_CONNECTING_BUDDY
);
652 cur_client
->TryToConnect(true);
653 Notify_ServerUpdateED2KInfo();
655 if( m_nBuddyStatus
== Connected
) {
656 cur_client
->SetKadState(KS_NONE
);
661 case KS_CONNECTING_BUDDY
:
662 //We are trying to connect to this client.
663 //Although it should NOT happen, we make sure we are not already connected to a buddy.
664 //If we are we set to KS_NONE and it's removed next cycle.
665 //But if we are not already connected, make sure we set the flag to connecting so we know
666 //things are working correctly.
667 if( m_nBuddyStatus
== Connected
) {
668 cur_client
->SetKadState(KS_NONE
);
670 wxASSERT( m_nBuddyStatus
== Connecting
);
675 case KS_CONNECTED_BUDDY
:
676 //A potential connected buddy client wanting to me in the Kad network
677 //We set our flag to connected to make sure things are still working correctly.
680 //If m_nBuddyStatus is not connected already, we set this client as our buddy!
681 if( m_nBuddyStatus
!= Connected
) {
682 m_pBuddy
= cur_client
;
683 m_nBuddyStatus
= Connected
;
684 Notify_ServerUpdateED2KInfo();
686 if( m_pBuddy
== cur_client
&& theApp
->IsFirewalled() && cur_client
->SendBuddyPingPong() ) {
687 cur_client
->SendBuddyPing();
692 RemoveFromKadList(cur_client
);
696 //We either never had a buddy, or lost our buddy..
697 if( buddy
== Disconnected
) {
698 if( m_nBuddyStatus
!= Disconnected
|| m_pBuddy
) {
699 if( Kademlia::CKademlia::IsRunning() && theApp
->IsFirewalled() && Kademlia::CUDPFirewallTester::IsFirewalledUDP(true) ) {
700 //We are a lowID client and we just lost our buddy.
701 //Go ahead and instantly try to find a new buddy.
702 Kademlia::CKademlia::GetPrefs()->SetFindBuddy();
705 m_nBuddyStatus
= Disconnected
;
706 Notify_ServerUpdateED2KInfo();
710 if ( Kademlia::CKademlia::IsConnected() ) {
711 // we only need a buddy if direct callback is not available
712 if(Kademlia::CKademlia::IsFirewalled() && Kademlia::CUDPFirewallTester::IsFirewalledUDP(true)) {
713 // TODO: Kad buddies won't work with RequireCrypt, so it is disabled for now, but should (and will)
714 // be fixed in later version
715 // Update: buddy connections themselves support obfuscation properly since eMule 0.49a and aMule SVN 2008-05-09
716 // (this makes it work fine if our buddy uses require crypt), however callback requests don't support it yet so we
717 // wouldn't be able to answer callback requests with RequireCrypt, protocolchange intended for eMule 0.49b
718 if(m_nBuddyStatus
== Disconnected
&& Kademlia::CKademlia::GetPrefs()->GetFindBuddy() && !thePrefs::IsClientCryptLayerRequired()) {
719 AddDebugLogLineM(false, logKadMain
, wxT("Starting BuddySearch"));
720 //We are a firewalled client with no buddy. We have also waited a set time
721 //to try to avoid a false firewalled status.. So lets look for a buddy..
722 if (!Kademlia::CSearchManager::PrepareLookup(Kademlia::CSearch::FINDBUDDY
, true, Kademlia::CUInt128(true).XOR(Kademlia::CKademlia::GetPrefs()->GetKadID()))) {
723 //This search ID was already going. Most likely reason is that
724 //we found and lost our buddy very quickly and the last search hadn't
725 //had time to be removed yet. Go ahead and set this to happen again
727 Kademlia::CKademlia::GetPrefs()->SetFindBuddy();
732 //Lets make sure that if we have a buddy, they are firewalled!
733 //If they are also not firewalled, then someone must have fixed their firewall or stopped saturating their line..
734 //We just set the state of this buddy to KS_NONE and things will be cleared up with the next cycle.
735 if( !m_pBuddy
->HasLowID() ) {
736 m_pBuddy
->SetKadState(KS_NONE
);
742 //We are not connected anymore. Just set this buddy to KS_NONE and things will be cleared out on next cycle.
743 m_pBuddy
->SetKadState(KS_NONE
);
748 ProcessDirectCallbackList();
752 void CClientList::AddBannedClient(uint32 dwIP
)
754 m_bannedList
[dwIP
] = ::GetTickCount();
755 theStats::AddBannedClient();
759 bool CClientList::IsBannedClient(uint32 dwIP
)
761 ClientMap::iterator it
= m_bannedList
.find( dwIP
);
763 if ( it
!= m_bannedList
.end() ) {
764 if ( it
->second
+ CLIENTBANTIME
> ::GetTickCount() ) {
767 RemoveBannedClient(dwIP
);
774 void CClientList::RemoveBannedClient(uint32 dwIP
)
776 m_bannedList
.erase(dwIP
);
777 theStats::RemoveBannedClient();
781 void CClientList::FilterQueues()
783 // Filter client list
784 for ( IDMap::iterator it
= m_ipList
.begin(); it
!= m_ipList
.end(); ) {
785 IDMap::iterator tmp
= it
++; // Don't change this to a ++it!
787 if ( theApp
->ipfilter
->IsFiltered(tmp
->second
->GetConnectIP())) {
788 tmp
->second
->Disconnected(wxT("Filtered by IPFilter"));
789 tmp
->second
->Safe_Delete();
795 CClientList::SourceList
CClientList::GetClientsByHash( const CMD4Hash
& hash
)
799 // Find all items with the specified hash
800 std::pair
<HashMap::iterator
, HashMap::iterator
> range
= m_hashList
.equal_range( hash
);
802 for ( ; range
.first
!= range
.second
; ++range
.first
) {
803 results
.push_back( range
.first
->second
);
810 CClientList::SourceList
CClientList::GetClientsByIP( unsigned long ip
)
814 // Find all items with the specified hash
815 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range( ip
);
817 for ( ; range
.first
!= range
.second
; range
.first
++ ) {
818 results
.push_back( range
.first
->second
);
825 const CClientList::IDMap
& CClientList::GetClientList()
831 void CClientList::AddDeadSource(const CUpDownClient
* client
)
833 m_deadSources
.AddDeadSource( client
);
837 bool CClientList::IsDeadSource(const CUpDownClient
* client
)
839 return m_deadSources
.IsDeadSource( client
);
842 bool CClientList::SendMessage(uint64 client_id
, const wxString
& message
)
844 CUpDownClient
* client
= FindClientByIP(IP_FROM_GUI_ID(client_id
), PORT_FROM_GUI_ID(client_id
));
845 AddDebugLogLineM( false, logClient
, wxT("Trying to Send Message.") );
847 AddDebugLogLineM( false, logClient
, wxT("Sending.") );
849 AddDebugLogLineM( true, logClient
,
850 CFormat( wxT("No client (GUI_ID %lli [%s:%llu]) found in CClientList::SendMessage(). Creating") )
852 % Uint32toStringIP(IP_FROM_GUI_ID(client_id
))
853 % PORT_FROM_GUI_ID(client_id
) );
854 client
= new CUpDownClient(PORT_FROM_GUI_ID(client_id
),IP_FROM_GUI_ID(client_id
),0,0,NULL
, true, true);
857 return client
->SendMessage(message
);
860 void CClientList::SetChatState(uint64 client_id
, uint8 state
) {
861 CUpDownClient
* client
= FindClientByIP(IP_FROM_GUI_ID(client_id
), PORT_FROM_GUI_ID(client_id
));
863 client
->SetChatState(state
);
869 bool CClientList::RequestTCP(Kademlia::CContact
* contact
, uint8_t connectOptions
)
871 uint32_t nContactIP
= wxUINT32_SWAP_ALWAYS(contact
->GetIPAddress());
872 // don't connect ourself
873 if (theApp
->GetPublicIP() == nContactIP
&& thePrefs::GetPort() == contact
->GetTCPPort()) {
877 CUpDownClient
* pNewClient
= FindClientByIP(nContactIP
, contact
->GetTCPPort());
880 //#warning Do we actually have to check friendstate here?
881 pNewClient
= new CUpDownClient(contact
->GetTCPPort(), contact
->GetIPAddress(), 0, 0, NULL
, false, true);
882 } else if (pNewClient
->GetKadState() != KS_NONE
) {
883 return false; // already busy with this client in some way (probably buddy stuff), don't mess with it
886 //Add client to the lists to be processed.
887 pNewClient
->SetKadPort(contact
->GetUDPPort());
888 pNewClient
->SetKadState(KS_QUEUED_FWCHECK
);
889 if (contact
->GetClientID() != 0) {
891 contact
->GetClientID().ToByteArray(ID
);
892 pNewClient
->SetUserHash(CMD4Hash(ID
));
893 pNewClient
->SetConnectOptions(connectOptions
, true, false);
895 AddToKadList(pNewClient
); // This was a direct adding, but I like to check duplicates
896 //This method checks if this is a dup already.
897 AddClient(pNewClient
);
901 void CClientList::RequestBuddy(Kademlia::CContact
* contact
, uint8_t connectOptions
)
903 uint32_t nContactIP
= wxUINT32_SWAP_ALWAYS(contact
->GetIPAddress());
904 // Don't connect to ourself
905 if (theApp
->GetPublicIP() == nContactIP
&& thePrefs::GetPort() == contact
->GetTCPPort()) {
909 CUpDownClient
* pNewClient
= FindClientByIP(nContactIP
, contact
->GetTCPPort());
911 pNewClient
= new CUpDownClient(contact
->GetTCPPort(), contact
->GetIPAddress(), 0, 0, NULL
, false, true );
912 } else if (pNewClient
->GetKadState() != KS_NONE
) {
913 return; // already busy with this client in some way (probably fw stuff), don't mess with it
914 } else if (IsKadFirewallCheckIP(nContactIP
)) { // doing a kad firewall check with this IP, abort
915 AddDebugLogLineM(false, logKadMain
, wxT("Kad TCP firewallcheck / Buddy request collision for IP ") + Uint32toStringIP(nContactIP
));
919 //Add client to the lists to be processed.
920 pNewClient
->SetKadPort(contact
->GetUDPPort());
921 pNewClient
->SetKadState(KS_QUEUED_BUDDY
);
923 contact
->GetClientID().ToByteArray(ID
);
924 pNewClient
->SetUserHash(CMD4Hash(ID
));
925 pNewClient
->SetConnectOptions(connectOptions
, true, false);
926 AddToKadList(pNewClient
);
927 //This method checks if this is a dup already.
928 AddClient(pNewClient
);
931 bool CClientList::IncomingBuddy(Kademlia::CContact
* contact
, Kademlia::CUInt128
* buddyID
)
933 uint32_t nContactIP
= wxUINT32_SWAP_ALWAYS(contact
->GetIPAddress());
934 //If aMule already knows this client, abort this.. It could cause conflicts.
935 //Although the odds of this happening is very small, it could still happen.
936 if (FindClientByIP(nContactIP
, contact
->GetTCPPort())) {
938 } else if (IsKadFirewallCheckIP(nContactIP
)) { // doing a kad firewall check with this IP, abort
939 AddDebugLogLineM(false, logKadMain
, wxT("Kad TCP firewallcheck / Buddy request collision for IP ") + Uint32toStringIP(nContactIP
));
943 if (theApp
->GetPublicIP() == nContactIP
&& thePrefs::GetPort() == contact
->GetTCPPort()) {
944 return false; // don't connect ourself
947 //Add client to the lists to be processed.
948 CUpDownClient
* pNewClient
= new CUpDownClient(contact
->GetTCPPort(), contact
->GetIPAddress(), 0, 0, NULL
, false, true );
949 pNewClient
->SetKadPort(contact
->GetUDPPort());
950 pNewClient
->SetKadState(KS_INCOMING_BUDDY
);
952 contact
->GetClientID().ToByteArray(ID
);
953 pNewClient
->SetUserHash(CMD4Hash(ID
));
954 buddyID
->ToByteArray(ID
);
955 pNewClient
->SetBuddyID(ID
);
956 AddToKadList(pNewClient
);
957 AddClient(pNewClient
);
961 void CClientList::RemoveFromKadList(CUpDownClient
* torem
)
963 wxCHECK_RET(torem
, wxT("NULL pointer in RemoveFromKadList"));
965 if (m_KadSources
.erase(torem
)) {
966 if(torem
== m_pBuddy
) {
968 Notify_ServerUpdateED2KInfo();
973 void CClientList::AddToKadList(CUpDownClient
* toadd
)
975 wxCHECK_RET(toadd
, wxT("NULL pointer in AddToKadList"));
977 m_KadSources
.insert(toadd
); // This will take care of duplicates.
980 bool CClientList::DoRequestFirewallCheckUDP(const Kademlia::CContact
& contact
)
982 // first make sure we don't know this IP already from somewhere
983 if (IsIPAlreadyKnown(wxUINT32_SWAP_ALWAYS(contact
.GetIPAddress()))) {
986 // fine, just create the client object, set the state and wait
987 // TODO: We don't know the client's userhash, this means we cannot build an obfuscated connection, which
988 // again mean that the whole check won't work on "Require Obfuscation" setting, which is not a huge problem,
989 // but certainly not nice. Only somewhat acceptable way to solve this is to use the KadID instead.
990 CUpDownClient
* pNewClient
= new CUpDownClient(contact
.GetTCPPort(), contact
.GetIPAddress(), 0, 0, NULL
, false, true);
991 pNewClient
->SetKadState(KS_QUEUED_FWCHECK_UDP
);
992 AddDebugLogLineM(false, logClient
, wxT("Selected client for UDP Firewallcheck: ") + Uint32toStringIP(wxUINT32_SWAP_ALWAYS(contact
.GetIPAddress())));
993 AddToKadList(pNewClient
);
994 AddClient(pNewClient
);
995 wxASSERT(!pNewClient
->SupportsDirectUDPCallback());
999 void CClientList::CleanUpClientList()
1001 // We remove clients which are not needed any more by time
1002 // this check is also done on CUpDownClient::Disconnected, however it will not catch all
1003 // cases (if a client changes the state without beeing connected
1005 // Adding this check directly to every point where any state changes would be more effective,
1006 // is however not compatible with the current code, because there are points where a client has
1007 // no state for some code lines and the code is also not prepared that a client object gets
1008 // invalid while working with it (aka setting a new state)
1009 // so this way is just the easy and safe one to go (as long as amule is basically single threaded)
1010 const uint32 cur_tick
= ::GetTickCount();
1011 if (m_dwLastClientCleanUp
+ CLIENTLIST_CLEANUP_TIME
< cur_tick
){
1012 m_dwLastClientCleanUp
= cur_tick
;
1013 uint32 cDeleted
= 0;
1014 IDMap::iterator current_it
= m_clientList
.begin();
1015 while (current_it
!= m_clientList
.end()) {
1016 CUpDownClient
* pCurClient
= current_it
->second
;
1017 ++current_it
; // Won't be used till while loop again
1018 // Don't delete sources coming from source seeds for 10 mins,
1019 // to give them a chance to connect and become a useful source.
1020 if (pCurClient
->GetSourceFrom() == SF_SOURCE_SEEDS
&& cur_tick
- (uint32
)theStats::GetStartTime() < MIN2MS(10)) continue;
1021 if ((pCurClient
->GetUploadState() == US_NONE
|| (pCurClient
->GetUploadState() == US_BANNED
&& !pCurClient
->IsBanned()))
1022 && pCurClient
->GetDownloadState() == DS_NONE
1023 && pCurClient
->GetChatState() == MS_NONE
1024 && pCurClient
->GetKadState() == KS_NONE
1025 && pCurClient
->GetSocket() == NULL
)
1028 pCurClient
->Disconnected(wxT("Removed during ClientList cleanup."));
1029 pCurClient
->Safe_Delete();
1031 if (!(pCurClient
->GetUploadState() == US_NONE
|| (pCurClient
->GetUploadState() == US_BANNED
&& !pCurClient
->IsBanned()))) {
1032 AddDebugLogLineM(false, logProxy
,
1033 CFormat(wxT("Debug: Not deleted client %x with up state: %i "))
1034 % (long int)pCurClient
% pCurClient
->GetUploadState());
1036 if (!(pCurClient
->GetDownloadState() == DS_NONE
)) {
1037 AddDebugLogLineM(false, logProxy
,
1038 CFormat(wxT("Debug: Not deleted client %x with down state: %i "))
1039 % (long int)pCurClient
% pCurClient
->GetDownloadState());
1041 if (!(pCurClient
->GetChatState() == MS_NONE
)) {
1042 AddDebugLogLineM(false, logProxy
,
1043 CFormat(wxT("Debug: Not deleted client %x with chat state: %i "))
1044 % (long int)pCurClient
% pCurClient
->GetChatState());
1046 if (!(pCurClient
->GetKadState() == KS_NONE
)) {
1047 AddDebugLogLineM(false, logProxy
,
1048 CFormat(wxT("Debug: Not deleted client %x with kad state: %i ip: %s"))
1049 % (long int)pCurClient
% pCurClient
->GetKadState() % pCurClient
->GetFullIP());
1051 if (!(pCurClient
->GetSocket() == NULL
)) {
1052 AddDebugLogLineM(false, logProxy
,
1053 CFormat(wxT("Debug: Not deleted client %x: has socket")) % (long int)pCurClient
);
1055 AddDebugLogLineM(false, logProxy
,
1056 CFormat(wxT("Debug: Not deleted client %x with kad version: %i"))
1057 % (long int)pCurClient
% pCurClient
->GetKadVersion());
1060 AddDebugLogLineM(false, logClient
, wxString::Format(wxT("Cleaned ClientList, removed %i not used known clients"), cDeleted
));
1064 void CClientList::AddKadFirewallRequest(uint32 ip
)
1066 uint32 ticks
= ::GetTickCount();
1067 IpAndTicks add
= { ip
, ticks
};
1068 m_firewallCheckRequests
.push_front(add
);
1069 while (!m_firewallCheckRequests
.empty()) {
1070 if (ticks
- m_firewallCheckRequests
.back().inserted
> SEC2MS(180)) {
1071 m_firewallCheckRequests
.pop_back();
1078 bool CClientList::IsKadFirewallCheckIP(uint32 ip
) const
1080 uint32 ticks
= ::GetTickCount();
1081 for (IpAndTicksList::const_iterator it
= m_firewallCheckRequests
.begin(); it
!= m_firewallCheckRequests
.end(); ++it
) {
1082 if (it
->ip
== ip
&& ticks
- it
->inserted
< SEC2MS(180)) {
1089 void CClientList::AddDirectCallbackClient(CUpDownClient
* toAdd
)
1091 wxASSERT(toAdd
->GetDirectCallbackTimeout() != 0);
1092 for (DirectCallbackList::const_iterator it
= m_currentDirectCallbacks
.begin(); it
!= m_currentDirectCallbacks
.end(); ++it
) {
1094 wxFAIL
; // might happen very rarely on multiple connection tries, could be fixed in the client class, till then it's not much of a problem though
1098 m_currentDirectCallbacks
.push_back(toAdd
);
1101 void CClientList::ProcessDirectCallbackList()
1103 // we do check if any direct callbacks have timed out by now
1104 const uint32_t cur_tick
= ::GetTickCount();
1105 for (DirectCallbackList::iterator it
= m_currentDirectCallbacks
.begin(); it
!= m_currentDirectCallbacks
.end();) {
1106 DirectCallbackList::iterator it2
= it
++;
1107 CUpDownClient
* curClient
= *it2
;
1108 if (curClient
->GetDirectCallbackTimeout() < cur_tick
) {
1109 wxASSERT(curClient
->GetDirectCallbackTimeout() != 0);
1111 //DebugLog(_T("DirectCallback timed out (%s)"), pCurClient->DbgGetClientInfo());
1112 m_currentDirectCallbacks
.erase(it2
);
1113 if (curClient
->Disconnected(wxT("Direct Callback Timeout"))) {
1114 curClient
->Safe_Delete();
1120 void CClientList::AddTrackCallbackRequests(uint32_t ip
)
1122 uint32_t now
= ::GetTickCount();
1123 IpAndTicks add
= { ip
, now
};
1124 m_directCallbackRequests
.push_front(add
);
1125 while (!m_directCallbackRequests
.empty()) {
1126 if (now
- m_directCallbackRequests
.back().inserted
> MIN2MS(3)) {
1127 m_directCallbackRequests
.pop_back();
1134 bool CClientList::AllowCallbackRequest(uint32_t ip
) const
1136 uint32_t now
= ::GetTickCount();
1137 for (IpAndTicksList::const_iterator it
= m_directCallbackRequests
.begin(); it
!= m_directCallbackRequests
.end(); ++it
) {
1138 if (it
->ip
== ip
&& now
- it
->inserted
< MIN2MS(3)) {
1144 // File_checked_for_headers