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 DeleteContents(m_trackedClientsList
);
104 wxASSERT(m_clientList
.empty());
105 wxASSERT(m_delete_queue
.empty());
109 void CClientList::AddClient( CUpDownClient
* toadd
)
111 // Ensure that only new clients can be added to the list
112 if ( toadd
->GetClientState() == CS_NEW
) {
113 // Update the client-state
114 toadd
->m_clientState
= CS_LISTED
;
116 //Notify_ClientCtrlAddClient( toadd );
118 // We always add the ID/ptr pair, regardles of the actual ID value
119 m_clientList
.insert( IDMapPair( toadd
->GetUserIDHybrid(), toadd
) );
121 // We only add the IP if it is valid
122 if ( toadd
->GetIP() ) {
123 m_ipList
.insert( IDMapPair( toadd
->GetIP(), toadd
) );
126 // We only add the hash if it is valid
127 if ( toadd
->HasValidHash() ) {
128 m_hashList
.insert( HashMapPair( toadd
->GetUserHash(), toadd
) );
131 toadd
->UpdateStats();
136 void CClientList::AddToDeleteQueue(CUpDownClient
* client
)
138 RemoveFromKadList( client
);
139 RemoveDirectCallback( client
);
141 // We have to remove the client from the list immediatly, to avoit it getting
142 // found by functions such as AttachToAlreadyKnown and GetClientsFromIP,
143 // however, if the client isn't on the clientlist, then it is safe to delete
144 // it right now. Otherwise, push it onto the queue.
145 if ( RemoveIDFromList( client
) ) {
146 // Also remove the ip and hash entries
147 RemoveIPFromList( client
);
148 RemoveHashFromList( client
);
150 wxASSERT(!m_delete_queue_closed
);
151 m_delete_queue
.push_back( client
);
158 void CClientList::UpdateClientID( CUpDownClient
* client
, uint32 newID
)
161 if ( ( client
->GetClientState() != CS_LISTED
) || ( client
->GetUserIDHybrid() == newID
) )
164 // First remove the ID entry
165 RemoveIDFromList( client
);
168 m_clientList
.insert( IDMapPair( newID
, client
) );
172 void CClientList::UpdateClientIP( CUpDownClient
* client
, uint32 newIP
)
175 if ( ( client
->GetClientState() != CS_LISTED
) || ( client
->GetIP() == newIP
) )
178 // Remove the old IP entry
179 RemoveIPFromList( client
);
182 m_ipList
.insert( IDMapPair( newIP
, client
) );
187 void CClientList::UpdateClientHash( CUpDownClient
* client
, const CMD4Hash
& newHash
)
190 if ( ( client
->GetClientState() != CS_LISTED
) || ( client
->GetUserHash() == newHash
) )
194 // Remove the old entry
195 RemoveHashFromList( client
);
197 // And add the new one if valid
198 if ( !newHash
.IsEmpty() ) {
199 m_hashList
.insert( HashMapPair( newHash
, client
) );
204 bool CClientList::RemoveIDFromList( CUpDownClient
* client
)
208 // First remove the ID entry
209 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_clientList
.equal_range( client
->GetUserIDHybrid() );
211 for ( ; range
.first
!= range
.second
; ++range
.first
) {
212 if ( client
== range
.first
->second
) {
213 /* erase() will invalidate the iterator, but we're not using it anymore
214 anyway (notice the break;) */
215 m_clientList
.erase( range
.first
);
226 void CClientList::RemoveIPFromList( CUpDownClient
* client
)
228 // Check if we need to look for the IP entry
229 if ( !client
->GetIP() ) {
233 // Remove the IP entry
234 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range( client
->GetIP() );
236 for ( ; range
.first
!= range
.second
; ++range
.first
) {
237 if ( client
== range
.first
->second
) {
238 /* erase() will invalidate the iterator, but we're not using it anymore
239 anyway (notice the break;) */
240 m_ipList
.erase( range
.first
);
246 void CClientList::RemoveHashFromList( CUpDownClient
* client
)
249 if ( !client
->HasValidHash() ) {
253 // Find all items with the specified hash
254 std::pair
<HashMap::iterator
, HashMap::iterator
> range
= m_hashList
.equal_range( client
->GetUserHash() );
256 for ( ; range
.first
!= range
.second
; ++range
.first
) {
257 if ( client
== range
.first
->second
) {
258 /* erase() will invalidate the iterator, but we're not using it anymore
259 anyway (notice the break;) */
260 m_hashList
.erase( range
.first
);
267 CUpDownClient
* CClientList::FindMatchingClient( CUpDownClient
* client
)
269 typedef std::pair
<IDMap::const_iterator
, IDMap::const_iterator
> IDMapIteratorPair
;
270 wxCHECK(client
, NULL
);
272 const uint32 userIP
= client
->GetIP();
273 const uint32 userID
= client
->GetUserIDHybrid();
274 const uint16 userPort
= client
->GetUserPort();
275 const uint16 userKadPort
= client
->GetKadPort();
278 // LowID clients need a different set of checks
279 if (client
->HasLowID()) {
280 // User is firewalled ... Must do two checks.
281 if (userIP
&& (userPort
|| userKadPort
)) {
282 IDMapIteratorPair range
= m_ipList
.equal_range(userIP
);
284 for ( ; range
.first
!= range
.second
; ++range
.first
) {
285 CUpDownClient
* other
= range
.first
->second
;
286 wxASSERT(userIP
== other
->GetIP());
288 if (userPort
&& (userPort
== other
->GetUserPort())) {
290 } else if (userKadPort
&& (userKadPort
== other
->GetKadPort())) {
296 const uint32 serverIP
= client
->GetServerIP();
297 const uint32 serverPort
= client
->GetServerPort();
298 if (userID
&& serverIP
&& serverPort
) {
299 IDMapIteratorPair range
= m_clientList
.equal_range(userID
);
301 for (; range
.first
!= range
.second
; ++range
.first
) {
302 CUpDownClient
* other
= range
.first
->second
;
303 wxASSERT(userID
== other
->GetUserIDHybrid());
305 // For lowid, we also have to check the server
306 if (serverIP
== other
->GetServerIP()) {
307 if (serverPort
== other
->GetServerPort()) {
313 } else if (userPort
|| userKadPort
) {
314 // Check by IP first, then by ID
315 struct { const IDMap
& map
; uint32 value
; } toCheck
[] = {
316 { m_ipList
, userIP
}, { m_clientList
, userID
}
319 for (size_t i
= 0; i
< itemsof(toCheck
); ++i
) {
320 if (toCheck
[i
].value
== 0) {
321 // We may not have both (or any) of these values.
325 IDMapIteratorPair range
= toCheck
[i
].map
.equal_range(toCheck
[i
].value
);
328 IDMap::const_iterator it
= range
.first
;
329 for (; it
!= range
.second
; ++it
) {
330 if (userPort
== it
->second
->GetUserPort()) {
337 IDMap::const_iterator it
= range
.first
;
338 for (; it
!= range
.second
; ++it
) {
339 if (userKadPort
== it
->second
->GetKadPort()) {
348 // If anything else fails, then we look at hashes
349 if ( client
->HasValidHash() ) {
350 // Find all items with the specified hash
351 std::pair
<HashMap::iterator
, HashMap::iterator
> range
= m_hashList
.equal_range( client
->GetUserHash() );
353 // Just return the first item if any
354 if ( range
.first
!= range
.second
) {
355 return range
.first
->second
;
359 // Nothing found, must be a new client
364 uint32
CClientList::GetClientCount() const
366 return m_clientList
.size();
370 void CClientList::DeleteAll()
375 while ( !m_clientList
.empty() ) {
376 IDMap::iterator it
= m_clientList
.begin();
378 // Will call the removal of the item on this same class
379 it
->second
->Disconnected(wxT("Removed while deleting all from ClientList."));
380 it
->second
->Safe_Delete();
383 // Clean up the clients now queued for deletion
385 m_delete_queue_closed
= true;
387 ProcessDeleteQueue();
391 bool CClientList::AttachToAlreadyKnown(CUpDownClient
** client
, CClientTCPSocket
* sender
)
393 CUpDownClient
* tocheck
= (*client
);
395 CUpDownClient
* found_client
= FindMatchingClient( tocheck
);
397 if ( tocheck
== found_client
) {
398 // We found the same client instance (client may have sent more than one OP_HELLO). do not delete that client!
402 if (found_client
!= NULL
){
404 if (found_client
->GetSocket()) {
405 if (found_client
->IsConnected()
406 && (found_client
->GetIP() != tocheck
->GetIP() || found_client
->GetUserPort() != tocheck
->GetUserPort() ) )
408 // if found_client is connected and has the IS_IDENTIFIED, it's safe to say that the other one is a bad guy
409 if (found_client
->IsIdentified()){
410 AddDebugLogLineM( false, logClient
, wxT("Client: ") + tocheck
->GetUserName() + wxT("(") + tocheck
->GetFullIP() + wxT("), Banreason: Userhash invalid"));
415 AddDebugLogLineM( false, logClient
, wxT("WARNING! Found matching client, to a currently connected client: ")
416 + tocheck
->GetUserName() + wxT("(") + tocheck
->GetFullIP()
417 + wxT(") and ") + found_client
->GetUserName() + wxT("(") + found_client
->GetFullIP() + wxT(")"));
420 found_client
->GetSocket()->Safe_Delete();
422 found_client
->SetSocket( sender
);
423 tocheck
->SetSocket( NULL
);
426 tocheck
->Safe_Delete();
427 *client
= found_client
;
435 CUpDownClient
* CClientList::FindClientByIP( uint32 clientip
, uint16 port
)
437 // Find all items with the specified ip
438 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range( clientip
);
440 for ( ; range
.first
!= range
.second
; ++range
.first
) {
441 CUpDownClient
* cur_client
= range
.first
->second
;
442 // Check if it's actually the client we want
443 if ( cur_client
->GetUserPort() == port
) {
452 CUpDownClient
* CClientList::FindClientByIP( uint32 clientip
)
454 // Find all items with the specified ip
455 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range( clientip
);
457 return (range
.first
!= range
.second
) ? range
.first
->second
: NULL
;
461 bool CClientList::IsIPAlreadyKnown(uint32_t ip
)
463 // Find all items with the specified ip
464 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range(ip
);
465 return range
.first
!= range
.second
;
469 bool CClientList::ComparePriorUserhash(uint32 dwIP
, uint16 nPort
, void* pNewHash
)
471 std::map
<uint32
, CDeletedClient
*>::iterator it
= m_trackedClientsList
.find( dwIP
);
473 if ( it
!= m_trackedClientsList
.end() ) {
474 CDeletedClient
* pResult
= it
->second
;
476 CDeletedClient::PaHList::iterator it2
= pResult
->m_ItemsList
.begin();
477 for ( ; it2
!= pResult
->m_ItemsList
.end(); ++it2
) {
478 if ( it2
->nPort
== nPort
) {
479 if ( it2
->pHash
!= pNewHash
) {
491 void CClientList::AddTrackClient(CUpDownClient
* toadd
)
493 std::map
<uint32
, CDeletedClient
*>::iterator it
= m_trackedClientsList
.find( toadd
->GetIP() );
495 if ( it
!= m_trackedClientsList
.end() ) {
496 CDeletedClient
* pResult
= it
->second
;
498 pResult
->m_dwInserted
= ::GetTickCount();
500 CDeletedClient::PaHList::iterator it2
= pResult
->m_ItemsList
.begin();
501 for ( ; it2
!= pResult
->m_ItemsList
.end(); ++it2
) {
502 if ( it2
->nPort
== toadd
->GetUserPort() ) {
503 // already tracked, update
504 it2
->pHash
= toadd
->GetCreditsHash();
509 // New client for that IP, add an entry
510 CDeletedClient::PortAndHash porthash
= { toadd
->GetUserPort(), toadd
->GetCreditsHash()};
511 pResult
->m_ItemsList
.push_back(porthash
);
513 m_trackedClientsList
[ toadd
->GetIP() ] = new CDeletedClient(toadd
);
518 uint16
CClientList::GetClientsFromIP(uint32 dwIP
)
520 std::map
<uint32
, CDeletedClient
*>::iterator it
= m_trackedClientsList
.find( dwIP
);
522 if ( it
!= m_trackedClientsList
.end() ) {
523 return it
->second
->m_ItemsList
.size();
530 void CClientList::ProcessDeleteQueue()
532 // Delete pending clients
533 while ( !m_delete_queue
.empty() ) {
534 CUpDownClient
* toremove
= m_delete_queue
.front();
535 m_delete_queue
.pop_front();
537 // Doing what RemoveClient used to do. Just to be sure...
538 theApp
->uploadqueue
->RemoveFromUploadQueue( toremove
);
539 theApp
->uploadqueue
->RemoveFromWaitingQueue( toremove
);
540 theApp
->downloadqueue
->RemoveSource( toremove
);
542 //Notify_ClientCtrlRemoveClient( toremove );
549 void CClientList::Process()
551 const uint32 cur_tick
= ::GetTickCount();
553 ProcessDeleteQueue();
555 if (m_dwLastBannCleanUp
+ BAN_CLEANUP_TIME
< cur_tick
) {
556 m_dwLastBannCleanUp
= cur_tick
;
558 ClientMap::iterator it
= m_bannedList
.begin();
559 while ( it
!= m_bannedList
.end() ) {
560 if ( it
->second
+ CLIENTBANTIME
< cur_tick
) {
561 ClientMap::iterator tmp
= it
++;
563 m_bannedList
.erase( tmp
);
564 theStats::RemoveBannedClient();
572 if ( m_dwLastTrackedCleanUp
+ TRACKED_CLEANUP_TIME
< cur_tick
) {
573 m_dwLastTrackedCleanUp
= cur_tick
;
575 std::map
<uint32
, CDeletedClient
*>::iterator it
= m_trackedClientsList
.begin();
576 while ( it
!= m_trackedClientsList
.end() ) {
577 std::map
<uint32
, CDeletedClient
*>::iterator cur_src
= it
++;
579 if ( cur_src
->second
->m_dwInserted
+ KEEPTRACK_TIME
< cur_tick
) {
580 delete cur_src
->second
;
581 m_trackedClientsList
.erase( cur_src
);
586 //We need to try to connect to the clients in m_KadList
587 //If connected, remove them from the list and send a message back to Kad so we can send a ACK.
588 //If we don't connect, we need to remove the client..
589 //The sockets timeout should delete this object.
591 // buddy is just a flag that is used to make sure we are still connected or connecting to a buddy.
592 buddyState buddy
= Disconnected
;
594 std::set
<CUpDownClient
*>::iterator current_it
= m_KadSources
.begin();
595 while (current_it
!= m_KadSources
.end()) {
596 CUpDownClient
* cur_client
= *current_it
;
597 ++current_it
; // Won't be used anymore till while loop
598 if( !Kademlia::CKademlia::IsRunning() ) {
599 //Clear out this list if we stop running Kad.
600 //Setting the Kad state to KS_NONE causes it to be removed in the switch below.
601 cur_client
->SetKadState(KS_NONE
);
603 switch (cur_client
->GetKadState()) {
604 case KS_QUEUED_FWCHECK
:
605 case KS_QUEUED_FWCHECK_UDP
:
606 //Another client asked us to try to connect to them to check their firewalled status.
607 cur_client
->TryToConnect(true);
610 case KS_CONNECTING_FWCHECK
:
611 //Ignore this state as we are just waiting for results.
615 case KS_CONNECTING_FWCHECK_UDP
:
616 // We want a UDP firewallcheck from this client and are just waiting to get connected to send the request
619 case KS_CONNECTED_FWCHECK
:
620 //We successfully connected to the client.
621 //We now send a ack to let them know.
622 if (cur_client
->GetKadVersion() >= 7) {
623 // The result is now sent per TCP instead of UDP, because this will fail if our intern port is unreachable.
624 // But we want the TCP testresult regardless if UDP is firewalled, the new UDP state and test takes care of the rest
625 wxASSERT(cur_client
->IsConnected());
626 //AddDebugLogLineM(false, logClient, wxT("Sent OP_KAD_FWTCPCHECK_ACK"));
627 CPacket
*packet
= new CPacket(OP_KAD_FWTCPCHECK_ACK
, 0, OP_EMULEPROT
);
628 cur_client
->SafeSendPacket(packet
);
630 DebugSend(KadFirewalledAckRes
, wxUINT32_SWAP_ALWAYS(cur_client
->GetIP()), cur_client
->GetKadPort());
631 Kademlia::CKademlia::GetUDPListener()->SendNullPacket(KADEMLIA_FIREWALLED_ACK_RES
, wxUINT32_SWAP_ALWAYS(cur_client
->GetIP()), cur_client
->GetKadPort(), 0, NULL
);
633 //We are done with this client. Set Kad status to KS_NONE and it will be removed in the next cycle.
634 cur_client
->SetKadState(KS_NONE
);
637 case KS_INCOMING_BUDDY
:
638 //A firewalled client wants us to be his buddy.
639 //If we already have a buddy, we set Kad state to KS_NONE and it's removed in the next cycle.
640 //If not, this client will change to KS_CONNECTED_BUDDY when it connects.
641 if( m_nBuddyStatus
== Connected
) {
642 cur_client
->SetKadState(KS_NONE
);
646 case KS_QUEUED_BUDDY
:
647 //We are firewalled and want to request this client to be a buddy.
648 //But first we check to make sure we are not already trying another client.
649 //If we are not already trying. We try to connect to this client.
650 //If we are already connected to a buddy, we set this client to KS_NONE and it's removed next cycle.
651 //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.
652 if( m_nBuddyStatus
== Disconnected
) {
654 m_nBuddyStatus
= Connecting
;
655 cur_client
->SetKadState(KS_CONNECTING_BUDDY
);
656 cur_client
->TryToConnect(true);
657 Notify_ServerUpdateED2KInfo();
659 if( m_nBuddyStatus
== Connected
) {
660 cur_client
->SetKadState(KS_NONE
);
665 case KS_CONNECTING_BUDDY
:
666 //We are trying to connect to this client.
667 //Although it should NOT happen, we make sure we are not already connected to a buddy.
668 //If we are we set to KS_NONE and it's removed next cycle.
669 //But if we are not already connected, make sure we set the flag to connecting so we know
670 //things are working correctly.
671 if( m_nBuddyStatus
== Connected
) {
672 cur_client
->SetKadState(KS_NONE
);
674 wxASSERT( m_nBuddyStatus
== Connecting
);
679 case KS_CONNECTED_BUDDY
:
680 //A potential connected buddy client wanting to me in the Kad network
681 //We set our flag to connected to make sure things are still working correctly.
684 //If m_nBuddyStatus is not connected already, we set this client as our buddy!
685 if( m_nBuddyStatus
!= Connected
) {
686 m_pBuddy
= cur_client
;
687 m_nBuddyStatus
= Connected
;
688 Notify_ServerUpdateED2KInfo();
690 if( m_pBuddy
== cur_client
&& theApp
->IsFirewalled() && cur_client
->SendBuddyPingPong() ) {
691 cur_client
->SendBuddyPing();
696 RemoveFromKadList(cur_client
);
700 //We either never had a buddy, or lost our buddy..
701 if( buddy
== Disconnected
) {
702 if( m_nBuddyStatus
!= Disconnected
|| m_pBuddy
) {
703 if( Kademlia::CKademlia::IsRunning() && theApp
->IsFirewalled() && Kademlia::CUDPFirewallTester::IsFirewalledUDP(true) ) {
704 //We are a lowID client and we just lost our buddy.
705 //Go ahead and instantly try to find a new buddy.
706 Kademlia::CKademlia::GetPrefs()->SetFindBuddy();
709 m_nBuddyStatus
= Disconnected
;
710 Notify_ServerUpdateED2KInfo();
714 if ( Kademlia::CKademlia::IsConnected() ) {
715 // we only need a buddy if direct callback is not available
716 if(Kademlia::CKademlia::IsFirewalled() && Kademlia::CUDPFirewallTester::IsFirewalledUDP(true)) {
717 // TODO: Kad buddies won't work with RequireCrypt, so it is disabled for now, but should (and will)
718 // be fixed in later version
719 // Update: buddy connections themselves support obfuscation properly since eMule 0.49a and aMule SVN 2008-05-09
720 // (this makes it work fine if our buddy uses require crypt), however callback requests don't support it yet so we
721 // wouldn't be able to answer callback requests with RequireCrypt, protocolchange intended for eMule 0.49b
722 if(m_nBuddyStatus
== Disconnected
&& Kademlia::CKademlia::GetPrefs()->GetFindBuddy() && !thePrefs::IsClientCryptLayerRequired()) {
723 AddDebugLogLineM(false, logKadMain
, wxT("Starting BuddySearch"));
724 //We are a firewalled client with no buddy. We have also waited a set time
725 //to try to avoid a false firewalled status.. So lets look for a buddy..
726 if (!Kademlia::CSearchManager::PrepareLookup(Kademlia::CSearch::FINDBUDDY
, true, Kademlia::CUInt128(true).XOR(Kademlia::CKademlia::GetPrefs()->GetKadID()))) {
727 //This search ID was already going. Most likely reason is that
728 //we found and lost our buddy very quickly and the last search hadn't
729 //had time to be removed yet. Go ahead and set this to happen again
731 Kademlia::CKademlia::GetPrefs()->SetFindBuddy();
736 //Lets make sure that if we have a buddy, they are firewalled!
737 //If they are also not firewalled, then someone must have fixed their firewall or stopped saturating their line..
738 //We just set the state of this buddy to KS_NONE and things will be cleared up with the next cycle.
739 if( !m_pBuddy
->HasLowID() ) {
740 m_pBuddy
->SetKadState(KS_NONE
);
746 //We are not connected anymore. Just set this buddy to KS_NONE and things will be cleared out on next cycle.
747 m_pBuddy
->SetKadState(KS_NONE
);
752 ProcessDirectCallbackList();
756 void CClientList::AddBannedClient(uint32 dwIP
)
758 m_bannedList
[dwIP
] = ::GetTickCount();
759 theStats::AddBannedClient();
763 bool CClientList::IsBannedClient(uint32 dwIP
)
765 ClientMap::iterator it
= m_bannedList
.find( dwIP
);
767 if ( it
!= m_bannedList
.end() ) {
768 if ( it
->second
+ CLIENTBANTIME
> ::GetTickCount() ) {
771 RemoveBannedClient(dwIP
);
778 void CClientList::RemoveBannedClient(uint32 dwIP
)
780 m_bannedList
.erase(dwIP
);
781 theStats::RemoveBannedClient();
785 void CClientList::FilterQueues()
787 // Filter client list
788 for ( IDMap::iterator it
= m_ipList
.begin(); it
!= m_ipList
.end(); ) {
789 IDMap::iterator tmp
= it
++; // Don't change this to a ++it!
791 if ( theApp
->ipfilter
->IsFiltered(tmp
->second
->GetConnectIP())) {
792 tmp
->second
->Disconnected(wxT("Filtered by IPFilter"));
793 tmp
->second
->Safe_Delete();
799 CClientList::SourceList
CClientList::GetClientsByHash( const CMD4Hash
& hash
)
803 // Find all items with the specified hash
804 std::pair
<HashMap::iterator
, HashMap::iterator
> range
= m_hashList
.equal_range( hash
);
806 for ( ; range
.first
!= range
.second
; ++range
.first
) {
807 results
.push_back( range
.first
->second
);
814 CClientList::SourceList
CClientList::GetClientsByIP( unsigned long ip
)
818 // Find all items with the specified hash
819 std::pair
<IDMap::iterator
, IDMap::iterator
> range
= m_ipList
.equal_range( ip
);
821 for ( ; range
.first
!= range
.second
; range
.first
++ ) {
822 results
.push_back( range
.first
->second
);
829 const CClientList::IDMap
& CClientList::GetClientList()
835 void CClientList::AddDeadSource(const CUpDownClient
* client
)
837 m_deadSources
.AddDeadSource( client
);
841 bool CClientList::IsDeadSource(const CUpDownClient
* client
)
843 return m_deadSources
.IsDeadSource( client
);
846 bool CClientList::SendChatMessage(uint64 client_id
, const wxString
& message
)
848 CUpDownClient
* client
= FindClientByIP(IP_FROM_GUI_ID(client_id
), PORT_FROM_GUI_ID(client_id
));
849 AddDebugLogLineM( false, logClient
, wxT("Trying to Send Message.") );
851 AddDebugLogLineM( false, logClient
, wxT("Sending.") );
853 AddDebugLogLineM( true, logClient
,
854 CFormat( wxT("No client (GUI_ID %lli [%s:%llu]) found in CClientList::SendChatMessage(). Creating") )
856 % Uint32toStringIP(IP_FROM_GUI_ID(client_id
))
857 % PORT_FROM_GUI_ID(client_id
) );
858 client
= new CUpDownClient(PORT_FROM_GUI_ID(client_id
),IP_FROM_GUI_ID(client_id
),0,0,NULL
, true, true);
861 return client
->SendChatMessage(message
);
864 void CClientList::SetChatState(uint64 client_id
, uint8 state
) {
865 CUpDownClient
* client
= FindClientByIP(IP_FROM_GUI_ID(client_id
), PORT_FROM_GUI_ID(client_id
));
867 client
->SetChatState(state
);
873 bool CClientList::RequestTCP(Kademlia::CContact
* contact
, uint8_t connectOptions
)
875 uint32_t nContactIP
= wxUINT32_SWAP_ALWAYS(contact
->GetIPAddress());
876 // don't connect ourself
877 if (theApp
->GetPublicIP() == nContactIP
&& thePrefs::GetPort() == contact
->GetTCPPort()) {
881 CUpDownClient
* pNewClient
= FindClientByIP(nContactIP
, contact
->GetTCPPort());
884 //#warning Do we actually have to check friendstate here?
885 pNewClient
= new CUpDownClient(contact
->GetTCPPort(), contact
->GetIPAddress(), 0, 0, NULL
, false, true);
886 } else if (pNewClient
->GetKadState() != KS_NONE
) {
887 return false; // already busy with this client in some way (probably buddy stuff), don't mess with it
890 //Add client to the lists to be processed.
891 pNewClient
->SetKadPort(contact
->GetUDPPort());
892 pNewClient
->SetKadState(KS_QUEUED_FWCHECK
);
893 if (contact
->GetClientID() != 0) {
895 contact
->GetClientID().ToByteArray(ID
);
896 pNewClient
->SetUserHash(CMD4Hash(ID
));
897 pNewClient
->SetConnectOptions(connectOptions
, true, false);
899 AddToKadList(pNewClient
); // This was a direct adding, but I like to check duplicates
900 //This method checks if this is a dup already.
901 AddClient(pNewClient
);
905 void CClientList::RequestBuddy(Kademlia::CContact
* contact
, uint8_t connectOptions
)
907 uint32_t nContactIP
= wxUINT32_SWAP_ALWAYS(contact
->GetIPAddress());
908 // Don't connect to ourself
909 if (theApp
->GetPublicIP() == nContactIP
&& thePrefs::GetPort() == contact
->GetTCPPort()) {
913 CUpDownClient
* pNewClient
= FindClientByIP(nContactIP
, contact
->GetTCPPort());
915 pNewClient
= new CUpDownClient(contact
->GetTCPPort(), contact
->GetIPAddress(), 0, 0, NULL
, false, true );
916 } else if (pNewClient
->GetKadState() != KS_NONE
) {
917 return; // already busy with this client in some way (probably fw stuff), don't mess with it
918 } else if (IsKadFirewallCheckIP(nContactIP
)) { // doing a kad firewall check with this IP, abort
919 AddDebugLogLineM(false, logKadMain
, wxT("Kad TCP firewallcheck / Buddy request collision for IP ") + Uint32toStringIP(nContactIP
));
923 //Add client to the lists to be processed.
924 pNewClient
->SetKadPort(contact
->GetUDPPort());
925 pNewClient
->SetKadState(KS_QUEUED_BUDDY
);
927 contact
->GetClientID().ToByteArray(ID
);
928 pNewClient
->SetUserHash(CMD4Hash(ID
));
929 pNewClient
->SetConnectOptions(connectOptions
, true, false);
930 AddToKadList(pNewClient
);
931 //This method checks if this is a dup already.
932 AddClient(pNewClient
);
935 bool CClientList::IncomingBuddy(Kademlia::CContact
* contact
, Kademlia::CUInt128
* buddyID
)
937 uint32_t nContactIP
= wxUINT32_SWAP_ALWAYS(contact
->GetIPAddress());
938 //If aMule already knows this client, abort this.. It could cause conflicts.
939 //Although the odds of this happening is very small, it could still happen.
940 if (FindClientByIP(nContactIP
, contact
->GetTCPPort())) {
942 } else if (IsKadFirewallCheckIP(nContactIP
)) { // doing a kad firewall check with this IP, abort
943 AddDebugLogLineM(false, logKadMain
, wxT("Kad TCP firewallcheck / Buddy request collision for IP ") + Uint32toStringIP(nContactIP
));
947 if (theApp
->GetPublicIP() == nContactIP
&& thePrefs::GetPort() == contact
->GetTCPPort()) {
948 return false; // don't connect ourself
951 //Add client to the lists to be processed.
952 CUpDownClient
* pNewClient
= new CUpDownClient(contact
->GetTCPPort(), contact
->GetIPAddress(), 0, 0, NULL
, false, true );
953 pNewClient
->SetKadPort(contact
->GetUDPPort());
954 pNewClient
->SetKadState(KS_INCOMING_BUDDY
);
956 contact
->GetClientID().ToByteArray(ID
);
957 pNewClient
->SetUserHash(CMD4Hash(ID
));
958 buddyID
->ToByteArray(ID
);
959 pNewClient
->SetBuddyID(ID
);
960 AddToKadList(pNewClient
);
961 AddClient(pNewClient
);
965 void CClientList::RemoveFromKadList(CUpDownClient
* torem
)
967 wxCHECK_RET(torem
, wxT("NULL pointer in RemoveFromKadList"));
969 if (m_KadSources
.erase(torem
)) {
970 if(torem
== m_pBuddy
) {
972 m_nBuddyStatus
= Disconnected
;
973 Notify_ServerUpdateED2KInfo();
978 void CClientList::AddToKadList(CUpDownClient
* toadd
)
980 wxCHECK_RET(toadd
, wxT("NULL pointer in AddToKadList"));
982 m_KadSources
.insert(toadd
); // This will take care of duplicates.
985 bool CClientList::DoRequestFirewallCheckUDP(const Kademlia::CContact
& contact
)
987 // first make sure we don't know this IP already from somewhere
988 if (IsIPAlreadyKnown(wxUINT32_SWAP_ALWAYS(contact
.GetIPAddress()))) {
991 // fine, just create the client object, set the state and wait
992 // TODO: We don't know the client's userhash, this means we cannot build an obfuscated connection, which
993 // again mean that the whole check won't work on "Require Obfuscation" setting, which is not a huge problem,
994 // but certainly not nice. Only somewhat acceptable way to solve this is to use the KadID instead.
995 CUpDownClient
* pNewClient
= new CUpDownClient(contact
.GetTCPPort(), contact
.GetIPAddress(), 0, 0, NULL
, false, true);
996 pNewClient
->SetKadState(KS_QUEUED_FWCHECK_UDP
);
997 AddDebugLogLineM(false, logClient
, wxT("Selected client for UDP Firewallcheck: ") + Uint32toStringIP(wxUINT32_SWAP_ALWAYS(contact
.GetIPAddress())));
998 AddToKadList(pNewClient
);
999 AddClient(pNewClient
);
1000 wxASSERT(!pNewClient
->SupportsDirectUDPCallback());
1004 void CClientList::CleanUpClientList()
1006 // We remove clients which are not needed any more by time
1007 // this check is also done on CUpDownClient::Disconnected, however it will not catch all
1008 // cases (if a client changes the state without beeing connected
1010 // Adding this check directly to every point where any state changes would be more effective,
1011 // is however not compatible with the current code, because there are points where a client has
1012 // no state for some code lines and the code is also not prepared that a client object gets
1013 // invalid while working with it (aka setting a new state)
1014 // so this way is just the easy and safe one to go (as long as amule is basically single threaded)
1015 const uint32 cur_tick
= ::GetTickCount();
1016 if (m_dwLastClientCleanUp
+ CLIENTLIST_CLEANUP_TIME
< cur_tick
){
1017 m_dwLastClientCleanUp
= cur_tick
;
1018 uint32 cDeleted
= 0;
1019 IDMap::iterator current_it
= m_clientList
.begin();
1020 while (current_it
!= m_clientList
.end()) {
1021 CUpDownClient
* pCurClient
= current_it
->second
;
1022 ++current_it
; // Won't be used till while loop again
1023 // Don't delete sources coming from source seeds for 10 mins,
1024 // to give them a chance to connect and become a useful source.
1025 if (pCurClient
->GetSourceFrom() == SF_SOURCE_SEEDS
&& cur_tick
- (uint32
)theStats::GetStartTime() < MIN2MS(10)) continue;
1026 if ((pCurClient
->GetUploadState() == US_NONE
|| (pCurClient
->GetUploadState() == US_BANNED
&& !pCurClient
->IsBanned()))
1027 && pCurClient
->GetDownloadState() == DS_NONE
1028 && pCurClient
->GetChatState() == MS_NONE
1029 && pCurClient
->GetKadState() == KS_NONE
1030 && pCurClient
->GetSocket() == NULL
)
1033 pCurClient
->Disconnected(wxT("Removed during ClientList cleanup."));
1034 pCurClient
->Safe_Delete();
1036 if (!(pCurClient
->GetUploadState() == US_NONE
|| (pCurClient
->GetUploadState() == US_BANNED
&& !pCurClient
->IsBanned()))) {
1037 AddDebugLogLineM(false, logProxy
,
1038 CFormat(wxT("Debug: Not deleted client %x with up state: %i "))
1039 % (long int)pCurClient
% pCurClient
->GetUploadState());
1041 if (!(pCurClient
->GetDownloadState() == DS_NONE
)) {
1042 AddDebugLogLineM(false, logProxy
,
1043 CFormat(wxT("Debug: Not deleted client %x with down state: %i "))
1044 % (long int)pCurClient
% pCurClient
->GetDownloadState());
1046 if (!(pCurClient
->GetChatState() == MS_NONE
)) {
1047 AddDebugLogLineM(false, logProxy
,
1048 CFormat(wxT("Debug: Not deleted client %x with chat state: %i "))
1049 % (long int)pCurClient
% pCurClient
->GetChatState());
1051 if (!(pCurClient
->GetKadState() == KS_NONE
)) {
1052 AddDebugLogLineM(false, logProxy
,
1053 CFormat(wxT("Debug: Not deleted client %x with kad state: %i ip: %s"))
1054 % (long int)pCurClient
% pCurClient
->GetKadState() % pCurClient
->GetFullIP());
1056 if (!(pCurClient
->GetSocket() == NULL
)) {
1057 AddDebugLogLineM(false, logProxy
,
1058 CFormat(wxT("Debug: Not deleted client %x: has socket")) % (long int)pCurClient
);
1060 AddDebugLogLineM(false, logProxy
,
1061 CFormat(wxT("Debug: Not deleted client %x with kad version: %i"))
1062 % (long int)pCurClient
% pCurClient
->GetKadVersion());
1065 AddDebugLogLineM(false, logClient
, wxString::Format(wxT("Cleaned ClientList, removed %i not used known clients"), cDeleted
));
1069 void CClientList::AddKadFirewallRequest(uint32 ip
)
1071 uint32 ticks
= ::GetTickCount();
1072 IpAndTicks add
= { ip
, ticks
};
1073 m_firewallCheckRequests
.push_front(add
);
1074 while (!m_firewallCheckRequests
.empty()) {
1075 if (ticks
- m_firewallCheckRequests
.back().inserted
> SEC2MS(180)) {
1076 m_firewallCheckRequests
.pop_back();
1083 bool CClientList::IsKadFirewallCheckIP(uint32 ip
) const
1085 uint32 ticks
= ::GetTickCount();
1086 for (IpAndTicksList::const_iterator it
= m_firewallCheckRequests
.begin(); it
!= m_firewallCheckRequests
.end(); ++it
) {
1087 if (it
->ip
== ip
&& ticks
- it
->inserted
< SEC2MS(180)) {
1094 void CClientList::AddDirectCallbackClient(CUpDownClient
* toAdd
)
1096 wxASSERT(toAdd
->GetDirectCallbackTimeout() != 0);
1097 if (toAdd
->HasBeenDeleted()) {
1100 for (DirectCallbackList::const_iterator it
= m_currentDirectCallbacks
.begin(); it
!= m_currentDirectCallbacks
.end(); ++it
) {
1102 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
1106 m_currentDirectCallbacks
.push_back(toAdd
);
1109 void CClientList::ProcessDirectCallbackList()
1111 // we do check if any direct callbacks have timed out by now
1112 const uint32_t cur_tick
= ::GetTickCount();
1113 for (DirectCallbackList::iterator it
= m_currentDirectCallbacks
.begin(); it
!= m_currentDirectCallbacks
.end();) {
1114 DirectCallbackList::iterator it2
= it
++;
1115 CUpDownClient
* curClient
= *it2
;
1116 if (curClient
->GetDirectCallbackTimeout() < cur_tick
) {
1117 wxASSERT(curClient
->GetDirectCallbackTimeout() != 0);
1119 //DebugLog(_T("DirectCallback timed out (%s)"), pCurClient->DbgGetClientInfo());
1120 m_currentDirectCallbacks
.erase(it2
);
1121 if (curClient
->Disconnected(wxT("Direct Callback Timeout"))) {
1122 curClient
->Safe_Delete();
1128 void CClientList::AddTrackCallbackRequests(uint32_t ip
)
1130 uint32_t now
= ::GetTickCount();
1131 IpAndTicks add
= { ip
, now
};
1132 m_directCallbackRequests
.push_front(add
);
1133 while (!m_directCallbackRequests
.empty()) {
1134 if (now
- m_directCallbackRequests
.back().inserted
> MIN2MS(3)) {
1135 m_directCallbackRequests
.pop_back();
1142 bool CClientList::AllowCallbackRequest(uint32_t ip
) const
1144 uint32_t now
= ::GetTickCount();
1145 for (IpAndTicksList::const_iterator it
= m_directCallbackRequests
.begin(); it
!= m_directCallbackRequests
.end(); ++it
) {
1146 if (it
->ip
== ip
&& now
- it
->inserted
< MIN2MS(3)) {
1152 // File_checked_for_headers