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 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 // We want a UDP firewallcheck from this client and are just waiting to get connected to send the request
614 case KS_CONNECTED_FWCHECK
:
615 //We successfully connected to the client.
616 //We now send a ack to let them know.
617 if (cur_client
->GetKadVersion() >= 7) {
618 // The result is now sent per TCP instead of UDP, because this will fail if our intern port is unreachable.
619 // But we want the TCP testresult regardless if UDP is firewalled, the new UDP state and test takes care of the rest
620 wxASSERT(cur_client
->IsConnected());
621 //AddDebugLogLineM(false, logClient, wxT("Sent OP_KAD_FWTCPCHECK_ACK"));
622 CPacket
*packet
= new CPacket(OP_KAD_FWTCPCHECK_ACK
, 0, OP_EMULEPROT
);
623 cur_client
->SafeSendPacket(packet
);
625 DebugSend(KadFirewalledAckRes
, wxUINT32_SWAP_ALWAYS(cur_client
->GetIP()), cur_client
->GetKadPort());
626 Kademlia::CKademlia::GetUDPListener()->SendNullPacket(KADEMLIA_FIREWALLED_ACK_RES
, wxUINT32_SWAP_ALWAYS(cur_client
->GetIP()), cur_client
->GetKadPort(), 0, NULL
);
628 //We are done with this client. Set Kad status to KS_NONE and it will be removed in the next cycle.
629 cur_client
->SetKadState(KS_NONE
);
632 case KS_INCOMING_BUDDY
:
633 //A firewalled client wants us to be his buddy.
634 //If we already have a buddy, we set Kad state to KS_NONE and it's removed in the next cycle.
635 //If not, this client will change to KS_CONNECTED_BUDDY when it connects.
636 if( m_nBuddyStatus
== Connected
) {
637 cur_client
->SetKadState(KS_NONE
);
641 case KS_QUEUED_BUDDY
:
642 //We are firewalled and want to request this client to be a buddy.
643 //But first we check to make sure we are not already trying another client.
644 //If we are not already trying. We try to connect to this client.
645 //If we are already connected to a buddy, we set this client to KS_NONE and it's removed next cycle.
646 //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.
647 if( m_nBuddyStatus
== Disconnected
) {
649 m_nBuddyStatus
= Connecting
;
650 cur_client
->SetKadState(KS_CONNECTING_BUDDY
);
651 cur_client
->TryToConnect(true);
652 Notify_ServerUpdateED2KInfo();
654 if( m_nBuddyStatus
== Connected
) {
655 cur_client
->SetKadState(KS_NONE
);
660 case KS_CONNECTING_BUDDY
:
661 //We are trying to connect to this client.
662 //Although it should NOT happen, we make sure we are not already connected to a buddy.
663 //If we are we set to KS_NONE and it's removed next cycle.
664 //But if we are not already connected, make sure we set the flag to connecting so we know
665 //things are working correctly.
666 if( m_nBuddyStatus
== Connected
) {
667 cur_client
->SetKadState(KS_NONE
);
669 wxASSERT( m_nBuddyStatus
== Connecting
);
674 case KS_CONNECTED_BUDDY
:
675 //A potential connected buddy client wanting to me in the Kad network
676 //We set our flag to connected to make sure things are still working correctly.
679 //If m_nBuddyStatus is not connected already, we set this client as our buddy!
680 if( m_nBuddyStatus
!= Connected
) {
681 m_pBuddy
= cur_client
;
682 m_nBuddyStatus
= Connected
;
683 Notify_ServerUpdateED2KInfo();
685 if( m_pBuddy
== cur_client
&& theApp
->IsFirewalled() && cur_client
->SendBuddyPingPong() ) {
686 cur_client
->SendBuddyPing();
691 RemoveFromKadList(cur_client
);
695 //We either never had a buddy, or lost our buddy..
696 if( buddy
== Disconnected
) {
697 if( m_nBuddyStatus
!= Disconnected
|| m_pBuddy
) {
698 if( Kademlia::CKademlia::IsRunning() && theApp
->IsFirewalled() && Kademlia::CUDPFirewallTester::IsFirewalledUDP(true) ) {
699 //We are a lowID client and we just lost our buddy.
700 //Go ahead and instantly try to find a new buddy.
701 Kademlia::CKademlia::GetPrefs()->SetFindBuddy();
704 m_nBuddyStatus
= Disconnected
;
705 Notify_ServerUpdateED2KInfo();
709 if ( Kademlia::CKademlia::IsConnected() ) {
710 // we only need a buddy if direct callback is not available
711 if(Kademlia::CKademlia::IsFirewalled() && Kademlia::CUDPFirewallTester::IsFirewalledUDP(true)) {
712 // TODO: Kad buddies won't work with RequireCrypt, so it is disabled for now, but should (and will)
713 // be fixed in later version
714 if(m_nBuddyStatus
== Disconnected
&& Kademlia::CKademlia::GetPrefs()->GetFindBuddy() && !thePrefs::IsClientCryptLayerRequired()) {
715 //We are a firewalled client with no buddy. We have also waited a set time
716 //to try to avoid a false firewalled status.. So lets look for a buddy..
717 if (!Kademlia::CSearchManager::PrepareLookup(Kademlia::CSearch::FINDBUDDY
, true, Kademlia::CUInt128(true).XOR(Kademlia::CKademlia::GetPrefs()->GetKadID()))) {
718 //This search ID was already going. Most likely reason is that
719 //we found and lost our buddy very quickly and the last search hadn't
720 //had time to be removed yet. Go ahead and set this to happen again
722 Kademlia::CKademlia::GetPrefs()->SetFindBuddy();
727 //Lets make sure that if we have a buddy, they are firewalled!
728 //If they are also not firewalled, then someone must have fixed their firewall or stopped saturating their line..
729 //We just set the state of this buddy to KS_NONE and things will be cleared up with the next cycle.
730 if( !m_pBuddy
->HasLowID() ) {
731 m_pBuddy
->SetKadState(KS_NONE
);
737 //We are not connected anymore. Just set this buddy to KS_NONE and things will be cleared out on next cycle.
738 m_pBuddy
->SetKadState(KS_NONE
);
743 ProcessDirectCallbackList();
747 void CClientList::AddBannedClient(uint32 dwIP
)
749 m_bannedList
[dwIP
] = ::GetTickCount();
750 theStats::AddBannedClient();
754 bool CClientList::IsBannedClient(uint32 dwIP
)
756 ClientMap::iterator it
= m_bannedList
.find( dwIP
);
758 if ( it
!= m_bannedList
.end() ) {
759 if ( it
->second
+ CLIENTBANTIME
> ::GetTickCount() ) {
762 RemoveBannedClient(dwIP
);
769 void CClientList::RemoveBannedClient(uint32 dwIP
)
771 m_bannedList
.erase(dwIP
);
772 theStats::RemoveBannedClient();
776 void CClientList::FilterQueues()
778 // Filter client list
779 for ( IDMap::iterator it
= m_ipList
.begin(); it
!= m_ipList
.end(); ) {
780 IDMap::iterator tmp
= it
++; // Don't change this to a ++it!
782 if ( theApp
->ipfilter
->IsFiltered(tmp
->second
->GetConnectIP())) {
783 tmp
->second
->Disconnected(wxT("Filtered by IPFilter"));
784 tmp
->second
->Safe_Delete();
790 CClientList::SourceList
CClientList::GetClientsByHash( const CMD4Hash
& hash
)
794 // Find all items with the specified hash
795 std::pair
<HashMap::iterator
, HashMap::iterator
> range
= m_hashList
.equal_range( hash
);
797 for ( ; range
.first
!= range
.second
; ++range
.first
) {
798 results
.push_back( range
.first
->second
);
805 CClientList::SourceList
CClientList::GetClientsByIP( unsigned long ip
)
809 // Find all items with the specified hash
810 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range( ip
);
812 for ( ; range
.first
!= range
.second
; range
.first
++ ) {
813 results
.push_back( range
.first
->second
);
820 const CClientList::IDMap
& CClientList::GetClientList()
826 void CClientList::AddDeadSource(const CUpDownClient
* client
)
828 m_deadSources
.AddDeadSource( client
);
832 bool CClientList::IsDeadSource(const CUpDownClient
* client
)
834 return m_deadSources
.IsDeadSource( client
);
837 bool CClientList::SendMessage(uint64 client_id
, const wxString
& message
)
839 CUpDownClient
* client
= FindClientByIP(IP_FROM_GUI_ID(client_id
), PORT_FROM_GUI_ID(client_id
));
840 AddDebugLogLineM( false, logClient
, wxT("Trying to Send Message.") );
842 AddDebugLogLineM( false, logClient
, wxT("Sending.") );
844 AddDebugLogLineM( true, logClient
,
845 CFormat( wxT("No client (GUI_ID %lli [%s:%llu]) found in CClientList::SendMessage(). Creating") )
847 % Uint32toStringIP(IP_FROM_GUI_ID(client_id
))
848 % PORT_FROM_GUI_ID(client_id
) );
849 client
= new CUpDownClient(PORT_FROM_GUI_ID(client_id
),IP_FROM_GUI_ID(client_id
),0,0,NULL
, true, true);
852 return client
->SendMessage(message
);
855 void CClientList::SetChatState(uint64 client_id
, uint8 state
) {
856 CUpDownClient
* client
= FindClientByIP(IP_FROM_GUI_ID(client_id
), PORT_FROM_GUI_ID(client_id
));
858 client
->SetChatState(state
);
864 void CClientList::RequestTCP(Kademlia::CContact
* contact
, uint8_t connectOptions
)
866 uint32_t nContactIP
= wxUINT32_SWAP_ALWAYS(contact
->GetIPAddress());
867 // don't connect ourself
868 if (theApp
->GetPublicIP() == nContactIP
&& thePrefs::GetPort() == contact
->GetTCPPort()) {
872 CUpDownClient
* pNewClient
= FindClientByIP(nContactIP
, contact
->GetTCPPort());
875 //#warning Do we actually have to check friendstate here?
876 pNewClient
= new CUpDownClient(contact
->GetTCPPort(), contact
->GetIPAddress(), 0, 0, NULL
, false, true);
879 //Add client to the lists to be processed.
880 pNewClient
->SetKadPort(contact
->GetUDPPort());
881 pNewClient
->SetKadState(KS_QUEUED_FWCHECK
);
882 if (contact
->GetClientID() != 0) {
884 contact
->GetClientID().ToByteArray(ID
);
885 pNewClient
->SetUserHash(CMD4Hash(ID
));
886 pNewClient
->SetConnectOptions(connectOptions
, true, false);
888 AddToKadList(pNewClient
); // This was a direct adding, but I like to check duplicates
889 //This method checks if this is a dup already.
890 AddClient(pNewClient
);
893 void CClientList::RequestBuddy(Kademlia::CContact
* contact
, uint8_t connectOptions
)
895 uint32_t nContactIP
= wxUINT32_SWAP_ALWAYS(contact
->GetIPAddress());
896 // Don't connect to ourself
897 if (theApp
->GetPublicIP() == nContactIP
&& thePrefs::GetPort() == contact
->GetTCPPort()) {
901 CUpDownClient
* pNewClient
= FindClientByIP(nContactIP
, contact
->GetTCPPort());
903 pNewClient
= new CUpDownClient(contact
->GetTCPPort(), contact
->GetIPAddress(), 0, 0, NULL
, false, true );
906 //Add client to the lists to be processed.
907 pNewClient
->SetKadPort(contact
->GetUDPPort());
908 pNewClient
->SetKadState(KS_QUEUED_BUDDY
);
910 contact
->GetClientID().ToByteArray(ID
);
911 pNewClient
->SetUserHash(CMD4Hash(ID
));
912 pNewClient
->SetConnectOptions(connectOptions
, true, false);
913 AddToKadList(pNewClient
);
914 //This method checks if this is a dup already.
915 AddClient(pNewClient
);
918 void CClientList::IncomingBuddy(Kademlia::CContact
* contact
, Kademlia::CUInt128
* buddyID
)
920 uint32 nContactIP
= wxUINT32_SWAP_ALWAYS(contact
->GetIPAddress());
921 //If aMule already knows this client, abort this.. It could cause conflicts.
922 //Although the odds of this happening is very small, it could still happen.
923 if (FindClientByIP(nContactIP
, contact
->GetTCPPort())) {
927 // Don't connect ourself
928 if (theApp
->GetPublicIP() == nContactIP
&& thePrefs::GetPort() == contact
->GetTCPPort()) {
932 //Add client to the lists to be processed.
933 CUpDownClient
* pNewClient
= new CUpDownClient(contact
->GetTCPPort(), contact
->GetIPAddress(), 0, 0, NULL
, false, true );
934 pNewClient
->SetKadPort(contact
->GetUDPPort());
935 pNewClient
->SetKadState(KS_INCOMING_BUDDY
);
937 contact
->GetClientID().ToByteArray(ID
);
938 pNewClient
->SetUserHash(CMD4Hash(ID
));
939 buddyID
->ToByteArray(ID
);
940 pNewClient
->SetBuddyID(ID
);
941 AddToKadList(pNewClient
);
942 AddClient(pNewClient
);
945 void CClientList::RemoveFromKadList(CUpDownClient
* torem
)
947 wxCHECK_RET(torem
, wxT("NULL pointer in RemoveFromKadList"));
949 if (m_KadSources
.erase(torem
)) {
950 if(torem
== m_pBuddy
) {
952 Notify_ServerUpdateED2KInfo();
957 void CClientList::AddToKadList(CUpDownClient
* toadd
)
959 wxCHECK_RET(toadd
, wxT("NULL pointer in AddToKadList"));
961 m_KadSources
.insert(toadd
); // This will take care of duplicates.
964 bool CClientList::DoRequestFirewallCheckUDP(const Kademlia::CContact
& contact
)
966 // first make sure we don't know this IP already from somewhere
967 if (IsIPAlreadyKnown(wxUINT32_SWAP_ALWAYS(contact
.GetIPAddress()))) {
970 // fine, just create the client object, set the state and wait
971 // TODO: We don't know the client's userhash, this means we cannot build an obfuscated connection, which
972 // again mean that the whole check won't work on "Require Obfuscation" setting, which is not a huge problem,
973 // but certainly not nice. Only somewhat acceptable way to solve this is to use the KadID instead.
974 CUpDownClient
* pNewClient
= new CUpDownClient(contact
.GetTCPPort(), contact
.GetIPAddress(), 0, 0, NULL
, false, true);
975 pNewClient
->SetKadState(KS_QUEUED_FWCHECK_UDP
);
976 AddDebugLogLineM(false, logClient
, wxT("Selected client for UDP Firewallcheck: ") + Uint32toStringIP(wxUINT32_SWAP_ALWAYS(contact
.GetIPAddress())));
977 AddToKadList(pNewClient
);
978 AddClient(pNewClient
);
979 wxASSERT(!pNewClient
->SupportsDirectUDPCallback());
983 void CClientList::CleanUpClientList()
985 // We remove clients which are not needed any more by time
986 // this check is also done on CUpDownClient::Disconnected, however it will not catch all
987 // cases (if a client changes the state without beeing connected
989 // Adding this check directly to every point where any state changes would be more effective,
990 // is however not compatible with the current code, because there are points where a client has
991 // no state for some code lines and the code is also not prepared that a client object gets
992 // invalid while working with it (aka setting a new state)
993 // so this way is just the easy and safe one to go (as long as amule is basically single threaded)
994 const uint32 cur_tick
= ::GetTickCount();
995 if (m_dwLastClientCleanUp
+ CLIENTLIST_CLEANUP_TIME
< cur_tick
){
996 m_dwLastClientCleanUp
= cur_tick
;
998 IDMap::iterator current_it
= m_clientList
.begin();
999 while (current_it
!= m_clientList
.end()) {
1000 CUpDownClient
* pCurClient
= current_it
->second
;
1001 ++current_it
; // Won't be used till while loop again
1002 // Don't delete sources coming from source seeds for 10 mins,
1003 // to give them a chance to connect and become a useful source.
1004 if (pCurClient
->GetSourceFrom() == SF_SOURCE_SEEDS
&& cur_tick
- (uint32
)theStats::GetStartTime() < MIN2MS(10)) continue;
1005 if ((pCurClient
->GetUploadState() == US_NONE
|| pCurClient
->GetUploadState() == US_BANNED
&& !pCurClient
->IsBanned())
1006 && pCurClient
->GetDownloadState() == DS_NONE
1007 && pCurClient
->GetChatState() == MS_NONE
1008 && pCurClient
->GetKadState() == KS_NONE
1009 && pCurClient
->GetSocket() == NULL
)
1012 pCurClient
->Disconnected(wxT("Removed during ClientList cleanup."));
1013 pCurClient
->Safe_Delete();
1015 if (!(pCurClient
->GetUploadState() == US_NONE
|| pCurClient
->GetUploadState() == US_BANNED
&& !pCurClient
->IsBanned())) {
1016 AddDebugLogLineM(false, logProxy
,
1017 CFormat(wxT("Debug: Not deleted client %x with up state: %i "))
1018 % (long int)pCurClient
% pCurClient
->GetUploadState());
1020 if (!(pCurClient
->GetDownloadState() == DS_NONE
)) {
1021 AddDebugLogLineM(false, logProxy
,
1022 CFormat(wxT("Debug: Not deleted client %x with down state: %i "))
1023 % (long int)pCurClient
% pCurClient
->GetDownloadState());
1025 if (!(pCurClient
->GetChatState() == MS_NONE
)) {
1026 AddDebugLogLineM(false, logProxy
,
1027 CFormat(wxT("Debug: Not deleted client %x with chat state: %i "))
1028 % (long int)pCurClient
% pCurClient
->GetChatState());
1030 if (!(pCurClient
->GetKadState() == KS_NONE
)) {
1031 AddDebugLogLineM(false, logProxy
,
1032 CFormat(wxT("Debug: Not deleted client %x with kad state: %i ip: %s"))
1033 % (long int)pCurClient
% pCurClient
->GetKadState() % pCurClient
->GetFullIP());
1035 if (!(pCurClient
->GetSocket() == NULL
)) {
1036 AddDebugLogLineM(false, logProxy
,
1037 CFormat(wxT("Debug: Not deleted client %x: has socket")) % (long int)pCurClient
);
1039 AddDebugLogLineM(false, logProxy
,
1040 CFormat(wxT("Debug: Not deleted client %x with kad version: %i"))
1041 % (long int)pCurClient
% pCurClient
->GetKadVersion());
1044 AddDebugLogLineM(false, logClient
, wxString::Format(wxT("Cleaned ClientList, removed %i not used known clients"), cDeleted
));
1048 void CClientList::AddKadFirewallRequest(uint32 ip
)
1050 uint32 ticks
= ::GetTickCount();
1051 IpAndTicks add
= { ip
, ticks
};
1052 m_firewallCheckRequests
.push_front(add
);
1053 while (!m_firewallCheckRequests
.empty()) {
1054 if (ticks
- m_firewallCheckRequests
.back().inserted
> SEC2MS(180)) {
1055 m_firewallCheckRequests
.pop_back();
1062 bool CClientList::IsKadFirewallCheckIP(uint32 ip
) const
1064 uint32 ticks
= ::GetTickCount();
1065 for (IpAndTicksList::const_iterator it
= m_firewallCheckRequests
.begin(); it
!= m_firewallCheckRequests
.end(); ++it
) {
1066 if (it
->ip
== ip
&& ticks
- it
->inserted
< SEC2MS(180)) {
1073 void CClientList::AddDirectCallbackClient(CUpDownClient
* toAdd
)
1075 wxASSERT(toAdd
->GetDirectCallbackTimeout() != 0);
1076 for (DirectCallbackList::const_iterator it
= m_currentDirectCallbacks
.begin(); it
!= m_currentDirectCallbacks
.end(); ++it
) {
1078 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
1082 m_currentDirectCallbacks
.push_back(toAdd
);
1085 void CClientList::ProcessDirectCallbackList()
1087 // we do check if any direct callbacks have timed out by now
1088 const uint32_t cur_tick
= ::GetTickCount();
1089 for (DirectCallbackList::iterator it
= m_currentDirectCallbacks
.begin(); it
!= m_currentDirectCallbacks
.end();) {
1090 DirectCallbackList::iterator it2
= it
++;
1091 CUpDownClient
* curClient
= *it2
;
1092 if (curClient
->GetDirectCallbackTimeout() < cur_tick
) {
1093 wxASSERT(curClient
->GetDirectCallbackTimeout() != 0);
1095 //DebugLog(_T("DirectCallback timed out (%s)"), pCurClient->DbgGetClientInfo());
1096 m_currentDirectCallbacks
.erase(it2
);
1097 curClient
->Disconnected(wxT("Direct Callback Timeout"));
1102 void CClientList::AddTrackCallbackRequests(uint32_t ip
)
1104 uint32_t now
= ::GetTickCount();
1105 IpAndTicks add
= { ip
, now
};
1106 m_directCallbackRequests
.push_front(add
);
1107 while (!m_directCallbackRequests
.empty()) {
1108 if (now
- m_directCallbackRequests
.back().inserted
> MIN2MS(3)) {
1109 m_directCallbackRequests
.pop_back();
1116 bool CClientList::AllowCallbackRequest(uint32_t ip
) const
1118 uint32_t now
= ::GetTickCount();
1119 for (IpAndTicksList::const_iterator it
= m_directCallbackRequests
.begin(); it
!= m_directCallbackRequests
.end(); ++it
) {
1120 if (it
->ip
== ip
&& now
- it
->inserted
< MIN2MS(3)) {
1126 // File_checked_for_headers