Upstream tarball 9882
[amule.git] / src / ClientList.h
blobd8d0b939645f130ba669ec5cc23bc8e66a3da0d4
1 //
2 // This file is part of the aMule Project.
3 //
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 )
6 //
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
9 // respective authors.
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.
20 //
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 #ifndef CLIENTLIST_H
27 #define CLIENTLIST_H
29 #include "DeadSourceList.h" // Needed for CDeadSourceList
31 #include <deque>
32 #include <set>
34 class CUpDownClient;
35 class CClientTCPSocket;
36 class CDeletedClient;
37 class CMD4Hash;
38 namespace Kademlia {
39 class CContact;
40 class CUInt128;
43 enum buddyState
45 Disconnected,
46 Connecting,
47 Connected
51 #define BAN_CLEANUP_TIME 1200000 // 20 min
54 /**
55 * This class takes care of managing existing clients.
57 * This class tracks a number of attributes related to existing and deleted
58 * clients. Among other things, it keeps track of existing, banned, dead and
59 * dying clients, as well as offers support for matching new client-instances
60 * against already exist clients to avoid duplicates.
62 class CClientList
64 public:
65 /**
66 * Constructor.
68 CClientList();
70 /**
71 * Destructor.
73 ~CClientList();
76 /**
77 * Adds a client to the global list of clients.
79 * @param toadd The new client.
81 void AddClient( CUpDownClient* toadd );
83 /**
84 * Schedules a client for deletion.
86 * @param client The client to be deleted.
88 * Call this function whenever a client is to be deleted, rather than
89 * directly deleting the client. If the client is on the global client-
90 * list, then it will be scheduled for deletion, otherwise it will be
91 * deleted immediatly. Please check CUpDownClient::Safe_Delete for the
92 * proper way to do this.
94 void AddToDeleteQueue( CUpDownClient* client );
97 /**
98 * Updates the recorded IP of the specified client.
100 * @param client The client to have its entry updated.
101 * @param newIP The new IP adress of the client.
103 * This function is to be called before the client actually changes its
104 * IP-address, and will update the old entry with the new value. There
105 * will only be added an entry if the new IP isn't zero.
107 void UpdateClientIP( CUpDownClient* client, uint32 newIP );
110 * Updates the recorded ID of the specified client.
112 * @param client The client to have its entry updated.
113 * @param newID The new ID of the client.
115 * This function is to be called before the client actually changes its
116 * ID, and will update the old entry with the new value. Unlike the other
117 * two functions, this function will always ensure that there is an entry
118 * for the client, regardless of the value of newID.
120 void UpdateClientID( CUpDownClient* client, uint32 newID );
123 * Updates the recorded hash of the specified client.
125 * @param client The client to have its entry updated.
126 * @param newHash The new user-hash.
128 * This function is to be called before the client actually changes its
129 * user-hash, and will update the old entry with the new value. There will
130 * only be added an entry if the new hash is valid.
132 void UpdateClientHash( CUpDownClient* client, const CMD4Hash& newHash );
136 * Returns the number of listed clients.
138 uint32 GetClientCount() const;
142 * Deletes all tracked clients.
144 void DeleteAll();
148 * Replaces a new client-instance with the an already existing client, if one such exist.
150 * @param client A pointer to the pointer of the new instance.
151 * @param sender The socket assosiated with the new instance.
153 * Call this function when a new client-instance has been created. This function will then
154 * compare it against all existing clients and see if we already have an instance matching
155 * the new one. If that is the case, it will delete the new instance and set the pointer to
156 * the existing one.
158 bool AttachToAlreadyKnown( CUpDownClient** client, CClientTCPSocket* sender );
162 * Finds a client with the specified ip and port.
164 * @param clientip The IP of the client to find.
165 * @param port The port used by the client.
167 CUpDownClient* FindClientByIP( uint32 clientip, uint16 port );
171 * Finds a client with the specified ip.
173 * @param clientip The IP of the client to find.
175 * Returns the first client found if there are several with same ip.
177 CUpDownClient* FindClientByIP( uint32 clientip );
180 //! The list-type used to store clients IPs and other information
181 typedef std::map<uint32, uint32> ClientMap;
185 * Adds a client to the list of tracked clients.
187 * @param toadd The client to track.
189 * This function is used to keep track of clients after they
190 * have been deleted and makes it possible to spot port or hash
191 * changes.
193 void AddTrackClient(CUpDownClient* toadd);
196 * Returns the number of tracked client.
198 * @param dwIP The IP-adress which of the clients.
199 * @return The number of clients tracked at the specifed IP.
201 uint16 GetClientsFromIP(uint32 dwIP);
204 * Checks if a client has changed its user-hash.
206 * @param dwIP The IP of the client.
207 * @param nPort The port of the client.
208 * @param pNewHash The userhash assosiated with the client.
211 bool ComparePriorUserhash( uint32 dwIP, uint16 nPort, void* pNewHash );
215 * Bans an IP address for 2 hours.
217 * @param dwIP The IP from which all clients will be banned.
219 void AddBannedClient(uint32 dwIP);
222 * Checks if a client has been banned.
224 * @param dwIP The IP to check.
225 * @return True if the IP is banned, false otherwise.
227 bool IsBannedClient(uint32 dwIP);
230 * Unbans an IP address, if it has been banned.
232 * @param dwIP The IP address to unban.
234 void RemoveBannedClient(uint32 dwIP);
238 * Main loop.
240 * This function takes care of cleaning the various lists and deleting
241 * pending clients on the deletion-queue.
243 void Process();
247 * Deletes clients previously queued for deletion
249 * This function takes care of deleting pending clients on the
250 * deletion-queue.
252 void ProcessDeleteQueue();
256 * This function removes all clients filtered by the current IPFilter.
258 * Call this function after changing the current IPFiler list, to ensure
259 * that no client-connections to illegal IPs exist. These would otherwise
260 * be allowed to exist, bypassing the IPFilter.
262 void FilterQueues();
265 //! The type of the list used to store client-pointers for a couple of tasks.
266 typedef std::deque<CUpDownClient*> SourceList;
270 * Returns a list of clients with the specified user-hash.
272 * @param hash The userhash to search for.
274 * This function will return a list of clients with the specified userhash,
275 * provided that the hash is a valid non-empty userhash. Empty hashes will
276 * simply result in nothing being found.
278 SourceList GetClientsByHash( const CMD4Hash& hash );
281 * Returns a list of clients with the specified IP.
283 * @param ip The IP-address to search for.
285 * This function will return a list of clients with the specified IP,
286 * provided that the IP is a non-zero value. A value of zero will not
287 * result in any results.
289 SourceList GetClientsByIP( unsigned long ip );
292 //! The type of the lists used to store IPs and IDs.
293 typedef std::multimap<uint32, CUpDownClient*> IDMap;
294 //! The pairs of the IP/ID list.
295 typedef std::pair<uint32, CUpDownClient*> IDMapPair;
299 * Returns a list of all clients.
301 * @return The complete list of clients.
303 const IDMap& GetClientList();
307 * Adds a source to the list of dead sources.
309 * @param client The source to be recorded as dead.
311 void AddDeadSource(const CUpDownClient* client);
314 * Checks if a source is recorded as being dead.
316 * @param client The client to evaluate.
317 * @return True if dead, false otherwise.
319 * Sources that are dead are not to be considered valid
320 * sources and should not be added to partfiles.
322 bool IsDeadSource(const CUpDownClient* client);
325 * Sends a message to a client, identified by a GUI_ID
327 * @return Success
329 bool SendChatMessage(uint64 client_id, const wxString& message);
332 * Stops a chat session with a client.
335 void SetChatState(uint64 client_id, uint8 state);
337 uint8 GetBuddyStatus() const {return m_nBuddyStatus;}
338 // This must be used on CreateKadSourceLink and if we ever add the columns
339 // on shared files control.
340 CUpDownClient* GetBuddy() const { return m_pBuddy; }
341 bool RequestTCP(Kademlia::CContact* contact, uint8_t connectOptions);
342 void RequestBuddy(Kademlia::CContact* contact, uint8_t connectOptions);
343 bool IncomingBuddy(Kademlia::CContact* contact, Kademlia::CUInt128* buddyID);
344 void RemoveFromKadList(CUpDownClient* torem);
345 void AddToKadList(CUpDownClient* toadd);
346 bool DoRequestFirewallCheckUDP(const Kademlia::CContact& contact);
348 void AddKadFirewallRequest(uint32 ip);
349 bool IsKadFirewallCheckIP(uint32 ip) const;
351 // Direct Callback list
352 void AddDirectCallbackClient(CUpDownClient *toAdd);
353 void RemoveDirectCallback(CUpDownClient *toRemove) { m_currentDirectCallbacks.remove(toRemove); }
354 void AddTrackCallbackRequests(uint32_t ip);
355 bool AllowCallbackRequest(uint32_t ip) const;
357 protected:
359 * Avoids unwanted clients to be forever in the client list
361 void CleanUpClientList();
363 void ProcessDirectCallbackList();
365 private:
367 * Helperfunction which finds a client matching the specified client.
369 * @param client The client to search for.
370 * @return The matching client or NULL.
372 * This functions searches through the list of clients and finds the first match
373 * using the same checks as CUpDownClient::Compare, but without the overhead.
375 CUpDownClient* FindMatchingClient( CUpDownClient* client );
379 * Check if we already know this IP.
381 * This function is used to determine if the given IP address
382 * is already known.
384 * @param ip The IP address to check.
386 bool IsIPAlreadyKnown(uint32_t ip);
390 * Helperfunction which removes the client from the IP-list.
392 void RemoveIPFromList( CUpDownClient* client );
394 * Helperfunction which removes the client from the ID-list.
396 bool RemoveIDFromList( CUpDownClient* client );
398 * Helperfunction which removes the client from the hash-list.
400 void RemoveHashFromList( CUpDownClient* client );
403 //! The type of the list used to store user-hashes.
404 typedef std::multimap<CMD4Hash, CUpDownClient*> HashMap;
405 //! The pairs of the Hash-list.
406 typedef std::pair<CMD4Hash, CUpDownClient*> HashMapPair;
409 //! The map of clients with valid hashes
410 HashMap m_hashList;
412 //! The map of clients with valid IPs
413 IDMap m_ipList;
415 //! The full lists of clients
416 IDMap m_clientList;
418 //! This is the lists of clients that should be deleted
419 SourceList m_delete_queue;
420 #ifdef __WXDEBUG__
421 bool m_delete_queue_closed;
422 #endif
424 //! This is the map of banned clients.
425 ClientMap m_bannedList;
426 //! This variable is used to keep track of the last time the banned-list was pruned.
427 uint32 m_dwLastBannCleanUp;
429 //! This is the map of tracked clients.
430 std::map<uint32, CDeletedClient*> m_trackedClientsList;
431 //! This keeps track of the last time the tracked-list was pruned.
432 uint32 m_dwLastTrackedCleanUp;
434 //! This keeps track of the last time the client-list was pruned.
435 uint32 m_dwLastClientCleanUp;
437 //! List of unusable sources.
438 CDeadSourceList m_deadSources;
440 /* Kad Stuff */
441 std::set<CUpDownClient*> m_KadSources;
442 CUpDownClient* m_pBuddy;
443 uint8 m_nBuddyStatus;
445 typedef struct {
446 uint32 ip;
447 uint32 inserted;
448 } IpAndTicks;
449 typedef std::list<IpAndTicks> IpAndTicksList;
450 IpAndTicksList m_firewallCheckRequests;
452 typedef std::list<CUpDownClient *> DirectCallbackList;
453 DirectCallbackList m_currentDirectCallbacks;
454 IpAndTicksList m_directCallbackRequests;
457 #endif
458 // File_checked_for_headers