1//								-*- C++ -*-
2// This file is part of the aMule Project.
3//
4// Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
5// Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6// Copyright (c) 2003-2011 Barry Dunne (http://www.emule-project.net)
7//
8// Any parts of this program derived from the xMule, lMule or eMule project,
9// or contributed by third-party developers are copyrighted by their
10// respective authors.
11//
12// This program is free software; you can redistribute it and/or modify
13// it under the terms of the GNU General Public License as published by
14// the Free Software Foundation; either version 2 of the License, or
15// (at your option) any later version.
16//
17// This program is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with this program; if not, write to the Free Software
24// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
25//
26
27// Note To Mods //
28/*
29Please do not change anything here and release it..
30There is going to be a new forum created just for the Kademlia side of the client..
31If you feel there is an error or a way to improve something, please
32post it in the forum first and let us look at it.. If it is a real improvement,
33it will be added to the offical client.. Changing something without knowing
34what all it does can cause great harm to the network if released in mass form..
35Any mod that changes anything within the Kademlia side will not be allowed to advertise
36there client on the eMule forum..
37*/
38
39#ifndef __ROUTING_BIN__
40#define __ROUTING_BIN__
41
42#include "Maps.h"
43#include "../../Types.h"
44#include "../kademlia/Defines.h"
45#include "Contact.h"
46
47////////////////////////////////////////
48namespace Kademlia {
49////////////////////////////////////////
50
51class CUInt128;
52
53class CRoutingBin
54{
55public:
56	CRoutingBin()
57		: m_dontDeleteContacts(false)
58	{}
59	~CRoutingBin();
60
61	bool	  AddContact(CContact *contact);
62	void	  SetAlive(CContact *contact);
63	void	  SetTCPPort(uint32_t ip, uint16_t port, uint16_t tcpPort);
64	void	  RemoveContact(CContact *contact, bool noTrackingAdjust = false)	{ if (!noTrackingAdjust) AdjustGlobalTracking(contact->GetIPAddress(), false); m_entries.remove(contact); }
65	CContact *GetContact(const CUInt128 &id) const throw();
66	CContact *GetContact(uint32_t ip, uint16_t port, bool tcpPort) const throw();
67	CContact *GetOldest() const throw()		{ return m_entries.size() ? m_entries.front() : NULL; }
68
69	uint32_t  GetSize() const throw()		{ return m_entries.size(); }
70	void	  GetNumContacts(uint32_t& nInOutContacts, uint32_t& nInOutFilteredContacts, uint8_t minVersion) const throw();
71	uint32_t  GetRemaining() const throw()		{ return K - m_entries.size(); }
72	void	  GetEntries(ContactList *result, bool emptyFirst = true) const;
73	void	  GetClosestTo(uint32_t maxType, const CUInt128 &target, uint32_t maxRequired, ContactMap *result, bool emptyFirst = true, bool setInUse = false) const;
74	bool	  ChangeContactIPAddress(CContact *contact, uint32_t newIP);
75	void	  PushToBottom(CContact *contact); // puts an existing contact from X to the end of the list
76	CContact *GetRandomContact(uint32_t maxType, uint32_t minKadVersion) const;
77	void	  SetAllContactsVerified();
78	bool	  HasOnlyLANNodes() const throw();
79
80	static bool	CheckGlobalIPLimits(uint32_t ip, uint16_t port);
81
82	bool	m_dontDeleteContacts;
83
84protected:
85	static void AdjustGlobalTracking(uint32_t ip, bool increase);
86
87private:
88	ContactList m_entries;
89
90	typedef std::map<uint32_t, uint32_t>	GlobalTrackingMap;
91
92	static GlobalTrackingMap	s_globalContactIPs;
93	static GlobalTrackingMap	s_globalContactSubnets;
94};
95
96} // End namespace
97
98#endif // __ROUTING_BIN__
99// File_checked_for_headers
100