Fix language getting reset to system default on saving preferences
[amule.git] / src / ClientList.h
blob0174f55fdba0d0745026ed6d800b46c1b356e0d0
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 );
181 * Finds a client with the specified ECID.
183 * @param clientip The IP of the client to find.
185 * Returns the first client found if there are several with same ip.
187 CUpDownClient* FindClientByECID(uint32 ecid) const;
190 //! The list-type used to store clients IPs and other information
191 typedef std::map<uint32, uint32> ClientMap;
195 * Adds a client to the list of tracked clients.
197 * @param toadd The client to track.
199 * This function is used to keep track of clients after they
200 * have been deleted and makes it possible to spot port or hash
201 * changes.
203 void AddTrackClient(CUpDownClient* toadd);
206 * Returns the number of tracked client.
208 * @param dwIP The IP-adress which of the clients.
209 * @return The number of clients tracked at the specifed IP.
211 uint16 GetClientsFromIP(uint32 dwIP);
214 * Checks if a client has changed its user-hash.
216 * @param dwIP The IP of the client.
217 * @param nPort The port of the client.
218 * @param pNewHash The userhash assosiated with the client.
221 bool ComparePriorUserhash( uint32 dwIP, uint16 nPort, void* pNewHash );
225 * Bans an IP address for 2 hours.
227 * @param dwIP The IP from which all clients will be banned.
229 void AddBannedClient(uint32 dwIP);
232 * Checks if a client has been banned.
234 * @param dwIP The IP to check.
235 * @return True if the IP is banned, false otherwise.
237 bool IsBannedClient(uint32 dwIP);
240 * Unbans an IP address, if it has been banned.
242 * @param dwIP The IP address to unban.
244 void RemoveBannedClient(uint32 dwIP);
248 * Main loop.
250 * This function takes care of cleaning the various lists and deleting
251 * pending clients on the deletion-queue.
253 void Process();
257 * Deletes clients previously queued for deletion
259 * This function takes care of deleting pending clients on the
260 * deletion-queue.
262 void ProcessDeleteQueue();
266 * This function removes all clients filtered by the current IPFilter.
268 * Call this function after changing the current IPFiler list, to ensure
269 * that no client-connections to illegal IPs exist. These would otherwise
270 * be allowed to exist, bypassing the IPFilter.
272 void FilterQueues();
275 //! The type of the list used to store client-pointers for a couple of tasks.
276 typedef std::deque<CUpDownClient*> SourceList;
280 * Returns a list of clients with the specified user-hash.
282 * @param hash The userhash to search for.
284 * This function will return a list of clients with the specified userhash,
285 * provided that the hash is a valid non-empty userhash. Empty hashes will
286 * simply result in nothing being found.
288 SourceList GetClientsByHash( const CMD4Hash& hash );
291 * Returns a list of clients with the specified IP.
293 * @param ip The IP-address to search for.
295 * This function will return a list of clients with the specified IP,
296 * provided that the IP is a non-zero value. A value of zero will not
297 * result in any results.
299 SourceList GetClientsByIP( unsigned long ip );
302 //! The type of the lists used to store IPs and IDs.
303 typedef std::multimap<uint32, CUpDownClient*> IDMap;
304 //! The pairs of the IP/ID list.
305 typedef std::pair<uint32, CUpDownClient*> IDMapPair;
309 * Returns a list of all clients.
311 * @return The complete list of clients.
313 const IDMap& GetClientList();
317 * Adds a source to the list of dead sources.
319 * @param client The source to be recorded as dead.
321 void AddDeadSource(const CUpDownClient* client);
324 * Checks if a source is recorded as being dead.
326 * @param client The client to evaluate.
327 * @return True if dead, false otherwise.
329 * Sources that are dead are not to be considered valid
330 * sources and should not be added to partfiles.
332 bool IsDeadSource(const CUpDownClient* client);
335 * Sends a message to a client, identified by a GUI_ID
337 * @return Success
339 bool SendChatMessage(uint64 client_id, const wxString& message);
342 * Stops a chat session with a client.
345 void SetChatState(uint64 client_id, uint8 state);
347 uint8 GetBuddyStatus() const {return m_nBuddyStatus;}
348 // This must be used on CreateKadSourceLink and if we ever add the columns
349 // on shared files control.
350 CUpDownClient* GetBuddy() const { return m_pBuddy; }
351 bool RequestTCP(Kademlia::CContact* contact, uint8_t connectOptions);
352 void RequestBuddy(Kademlia::CContact* contact, uint8_t connectOptions);
353 bool IncomingBuddy(Kademlia::CContact* contact, Kademlia::CUInt128* buddyID);
354 void RemoveFromKadList(CUpDownClient* torem);
355 void AddToKadList(CUpDownClient* toadd);
356 bool DoRequestFirewallCheckUDP(const Kademlia::CContact& contact);
358 void AddKadFirewallRequest(uint32 ip);
359 bool IsKadFirewallCheckIP(uint32 ip) const;
361 // Direct Callback list
362 void AddDirectCallbackClient(CUpDownClient *toAdd);
363 void RemoveDirectCallback(CUpDownClient *toRemove) { m_currentDirectCallbacks.remove(toRemove); }
364 void AddTrackCallbackRequests(uint32_t ip);
365 bool AllowCallbackRequest(uint32_t ip) const;
367 protected:
369 * Avoids unwanted clients to be forever in the client list
371 void CleanUpClientList();
373 void ProcessDirectCallbackList();
375 private:
377 * Helperfunction which finds a client matching the specified client.
379 * @param client The client to search for.
380 * @return The matching client or NULL.
382 * This functions searches through the list of clients and finds the first match
383 * using the same checks as CUpDownClient::Compare, but without the overhead.
385 CUpDownClient* FindMatchingClient( CUpDownClient* client );
389 * Check if we already know this IP.
391 * This function is used to determine if the given IP address
392 * is already known.
394 * @param ip The IP address to check.
396 bool IsIPAlreadyKnown(uint32_t ip);
400 * Helperfunction which removes the client from the IP-list.
402 void RemoveIPFromList( CUpDownClient* client );
404 * Helperfunction which removes the client from the ID-list.
406 bool RemoveIDFromList( CUpDownClient* client );
408 * Helperfunction which removes the client from the hash-list.
410 void RemoveHashFromList( CUpDownClient* client );
413 //! The type of the list used to store user-hashes.
414 typedef std::multimap<CMD4Hash, CUpDownClient*> HashMap;
415 //! The pairs of the Hash-list.
416 typedef std::pair<CMD4Hash, CUpDownClient*> HashMapPair;
419 //! The map of clients with valid hashes
420 HashMap m_hashList;
422 //! The map of clients with valid IPs
423 IDMap m_ipList;
425 //! The full lists of clients
426 IDMap m_clientList;
428 //! This is the lists of clients that should be deleted
429 SourceList m_delete_queue;
430 #ifdef __WXDEBUG__
431 bool m_delete_queue_closed;
432 #endif
434 //! This is the map of banned clients.
435 ClientMap m_bannedList;
436 //! This variable is used to keep track of the last time the banned-list was pruned.
437 uint32 m_dwLastBannCleanUp;
439 //! This is the map of tracked clients.
440 std::map<uint32, CDeletedClient*> m_trackedClientsList;
441 //! This keeps track of the last time the tracked-list was pruned.
442 uint32 m_dwLastTrackedCleanUp;
444 //! This keeps track of the last time the client-list was pruned.
445 uint32 m_dwLastClientCleanUp;
447 //! List of unusable sources.
448 CDeadSourceList m_deadSources;
450 /* Kad Stuff */
451 std::set<CUpDownClient*> m_KadSources;
452 CUpDownClient* m_pBuddy;
453 uint8 m_nBuddyStatus;
455 typedef struct {
456 uint32 ip;
457 uint32 inserted;
458 } IpAndTicks;
459 typedef std::list<IpAndTicks> IpAndTicksList;
460 IpAndTicksList m_firewallCheckRequests;
462 typedef std::list<CUpDownClient *> DirectCallbackList;
463 DirectCallbackList m_currentDirectCallbacks;
464 IpAndTicksList m_directCallbackRequests;
467 #endif
468 // File_checked_for_headers