2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 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
29 #include "DeadSourceList.h" // Needed for CDeadSourceList
30 #include "ClientRef.h"
36 class CClientTCPSocket
;
52 #define BAN_CLEANUP_TIME 1200000 // 20 min
56 * This class takes care of managing existing clients.
58 * This class tracks a number of attributes related to existing and deleted
59 * clients. Among other things, it keeps track of existing, banned, dead and
60 * dying clients, as well as offers support for matching new client-instances
61 * against already exist clients to avoid duplicates.
78 * Adds a client to the global list of clients.
80 * @param toadd The new client.
82 void AddClient( CUpDownClient
* toadd
);
85 * Removes a client from the client lists.
87 * @param client The client to be removed.
89 * To be called from CUpDownClient::Safe_Delete only.
91 void RemoveClient( CUpDownClient
* client
);
95 * Updates the recorded IP of the specified client.
97 * @param client The client to have its entry updated.
98 * @param newIP The new IP adress of the client.
100 * This function is to be called before the client actually changes its
101 * IP-address, and will update the old entry with the new value. There
102 * will only be added an entry if the new IP isn't zero.
104 void UpdateClientIP( CUpDownClient
* client
, uint32 newIP
);
107 * Updates the recorded ID of the specified client.
109 * @param client The client to have its entry updated.
110 * @param newID The new ID of the client.
112 * This function is to be called before the client actually changes its
113 * ID, and will update the old entry with the new value. Unlike the other
114 * two functions, this function will always ensure that there is an entry
115 * for the client, regardless of the value of newID.
117 void UpdateClientID( CUpDownClient
* client
, uint32 newID
);
120 * Updates the recorded hash of the specified client.
122 * @param client The client to have its entry updated.
123 * @param newHash The new user-hash.
125 * This function is to be called before the client actually changes its
126 * user-hash, and will update the old entry with the new value. There will
127 * only be added an entry if the new hash is valid.
129 void UpdateClientHash( CUpDownClient
* client
, const CMD4Hash
& newHash
);
133 * Returns the number of listed clients.
135 uint32
GetClientCount() const;
139 * Deletes all tracked clients.
145 * Replaces a new client-instance with the an already existing client, if one such exist.
147 * @param client A pointer to the pointer of the new instance.
148 * @param sender The socket assosiated with the new instance.
150 * Call this function when a new client-instance has been created. This function will then
151 * compare it against all existing clients and see if we already have an instance matching
152 * the new one. If that is the case, it will delete the new instance and set the pointer to
155 bool AttachToAlreadyKnown( CUpDownClient
** client
, CClientTCPSocket
* sender
);
159 * Finds a client with the specified ip and port.
161 * @param clientip The IP of the client to find.
162 * @param port The port used by the client.
164 CUpDownClient
* FindClientByIP( uint32 clientip
, uint16 port
);
168 * Finds a client with the specified ip.
170 * @param clientip The IP of the client to find.
172 * Returns the first client found if there are several with same ip.
174 CUpDownClient
* FindClientByIP( uint32 clientip
);
178 * Finds a client with the specified ECID.
180 * @param clientip The IP of the client to find.
183 CUpDownClient
* FindClientByECID(uint32 ecid
) const;
186 //! The list-type used to store clients IPs and other information
187 typedef std::map
<uint32
, uint32
> ClientMap
;
191 * Adds a client to the list of tracked clients.
193 * @param toadd The client to track.
195 * This function is used to keep track of clients after they
196 * have been deleted and makes it possible to spot port or hash
199 void AddTrackClient(CUpDownClient
* toadd
);
202 * Returns the number of tracked client.
204 * @param dwIP The IP-adress which of the clients.
205 * @return The number of clients tracked at the specifed IP.
207 uint16
GetClientsFromIP(uint32 dwIP
);
210 * Checks if a client has changed its user-hash.
212 * @param dwIP The IP of the client.
213 * @param nPort The port of the client.
214 * @param pNewHash The userhash assosiated with the client.
217 bool ComparePriorUserhash( uint32 dwIP
, uint16 nPort
, void* pNewHash
);
221 * Bans an IP address for 2 hours.
223 * @param dwIP The IP from which all clients will be banned.
225 void AddBannedClient(uint32 dwIP
);
228 * Checks if a client has been banned.
230 * @param dwIP The IP to check.
231 * @return True if the IP is banned, false otherwise.
233 bool IsBannedClient(uint32 dwIP
);
236 * Unbans an IP address, if it has been banned.
238 * @param dwIP The IP address to unban.
240 void RemoveBannedClient(uint32 dwIP
);
246 * This function takes care of cleaning the various lists and deleting
247 * pending clients on the deletion-queue.
253 * This function removes all clients filtered by the current IPFilter.
255 * Call this function after changing the current IPFiler list, to ensure
256 * that no client-connections to illegal IPs exist. These would otherwise
257 * be allowed to exist, bypassing the IPFilter.
262 //! The type of the list used to store client-pointers for a couple of tasks.
263 typedef std::deque
<CClientRef
> SourceList
;
267 * Returns a list of clients with the specified user-hash.
269 * @param hash The userhash to search for.
271 * This function will return a list of clients with the specified userhash,
272 * provided that the hash is a valid non-empty userhash. Empty hashes will
273 * simply result in nothing being found.
275 SourceList
GetClientsByHash( const CMD4Hash
& hash
);
278 * Returns a list of clients with the specified IP.
280 * @param ip The IP-address to search for.
282 * This function will return a list of clients with the specified IP,
283 * provided that the IP is a non-zero value. A value of zero will not
284 * result in any results.
286 SourceList
GetClientsByIP( unsigned long ip
);
289 //! The type of the lists used to store IPs and IDs.
290 typedef std::multimap
<uint32
, CClientRef
> IDMap
;
291 //! The pairs of the IP/ID list.
292 typedef std::pair
<uint32
, CClientRef
> IDMapPair
;
296 * Returns a list of all clients.
298 * @return The complete list of clients.
300 const IDMap
& GetClientList();
304 * Adds a source to the list of dead sources.
306 * @param client The source to be recorded as dead.
308 void AddDeadSource(const CUpDownClient
* client
);
311 * Checks if a source is recorded as being dead.
313 * @param client The client to evaluate.
314 * @return True if dead, false otherwise.
316 * Sources that are dead are not to be considered valid
317 * sources and should not be added to partfiles.
319 bool IsDeadSource(const CUpDownClient
* client
);
322 * Sends a message to a client, identified by a GUI_ID
326 bool SendChatMessage(uint64 client_id
, const wxString
& message
);
329 * Stops a chat session with a client.
332 void SetChatState(uint64 client_id
, uint8 state
);
334 uint8
GetBuddyStatus() const {return m_nBuddyStatus
;}
335 // This must be used on CreateKadSourceLink and if we ever add the columns
336 // on shared files control.
337 CUpDownClient
* GetBuddy() { return m_pBuddy
.GetClient(); }
339 uint16
GetBuddyPort();
340 bool RequestTCP(Kademlia::CContact
* contact
, uint8_t connectOptions
);
341 void RequestBuddy(Kademlia::CContact
* contact
, uint8_t connectOptions
);
342 bool IncomingBuddy(Kademlia::CContact
* contact
, Kademlia::CUInt128
* buddyID
);
343 void RemoveFromKadList(CUpDownClient
* torem
);
344 void AddToKadList(CUpDownClient
* toadd
);
345 bool DoRequestFirewallCheckUDP(const Kademlia::CContact
& contact
);
347 void AddKadFirewallRequest(uint32 ip
);
348 bool IsKadFirewallCheckIP(uint32 ip
) const;
350 // Direct Callback list
351 void AddDirectCallbackClient(CUpDownClient
*toAdd
);
352 void RemoveDirectCallback(CUpDownClient
*toRemove
) { m_currentDirectCallbacks
.remove(CCLIENTREF(toRemove
, wxEmptyString
)); }
353 void AddTrackCallbackRequests(uint32_t ip
);
354 bool AllowCallbackRequest(uint32_t ip
) const;
358 * Avoids unwanted clients to be forever in the client list
360 void CleanUpClientList();
362 void ProcessDirectCallbackList();
366 * Helperfunction which finds a client matching the specified client.
368 * @param client The client to search for.
369 * @return The matching client or NULL.
371 * This functions searches through the list of clients and finds the first match
372 * using the same checks as CUpDownClient::Compare, but without the overhead.
374 CUpDownClient
* FindMatchingClient( CUpDownClient
* client
);
378 * Check if we already know this IP.
380 * This function is used to determine if the given IP address
383 * @param ip The IP address to check.
385 bool IsIPAlreadyKnown(uint32_t ip
);
389 * Helperfunction which removes the client from the IP-list.
391 void RemoveIPFromList( CUpDownClient
* client
);
393 * Helperfunction which removes the client from the ID-list.
395 bool RemoveIDFromList( CUpDownClient
* client
);
397 * Helperfunction which removes the client from the hash-list.
399 void RemoveHashFromList( CUpDownClient
* client
);
402 //! The type of the list used to store user-hashes.
403 typedef std::multimap
<CMD4Hash
, CClientRef
> HashMap
;
404 //! The pairs of the Hash-list.
405 typedef std::pair
<CMD4Hash
, CClientRef
> HashMapPair
;
408 //! The map of clients with valid hashes
411 //! The map of clients with valid IPs
414 //! The full lists of clients
417 //! This is the map of banned clients.
418 ClientMap m_bannedList
;
419 //! This variable is used to keep track of the last time the banned-list was pruned.
420 uint32 m_dwLastBannCleanUp
;
422 //! This is the map of tracked clients.
423 std::map
<uint32
, CDeletedClient
*> m_trackedClientsList
;
424 //! This keeps track of the last time the tracked-list was pruned.
425 uint32 m_dwLastTrackedCleanUp
;
427 //! This keeps track of the last time the client-list was pruned.
428 uint32 m_dwLastClientCleanUp
;
430 //! List of unusable sources.
431 CDeadSourceList m_deadSources
;
434 CClientRefSet m_KadSources
;
436 uint8 m_nBuddyStatus
;
442 typedef std::list
<IpAndTicks
> IpAndTicksList
;
443 IpAndTicksList m_firewallCheckRequests
;
445 typedef CClientRefList DirectCallbackList
;
446 DirectCallbackList m_currentDirectCallbacks
;
447 IpAndTicksList m_directCallbackRequests
;
451 // File_checked_for_headers