📄 routingzone.cpp
字号:
/*
Copyright (C)2003 Barry Dunne (http://www.emule-project.net)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
This work is based on the java implementation of the Kademlia protocol.
Kademlia: Peer-to-peer routing based on the XOR metric
Copyright (C) 2002 Petar Maymounkov [petar@post.harvard.edu]
http://kademlia.scs.cs.nyu.edu
*/
// Note To Mods //
/*
Please do not change anything here and release it..
There is going to be a new forum created just for the Kademlia side of the client..
If you feel there is an error or a way to improve something, please
post it in the forum first and let us look at it.. If it is a real improvement,
it will be added to the offical client.. Changing something without knowing
what all it does can cause great harm to the network if released in mass form..
Any mod that changes anything within the Kademlia side will not be allowed to advertise
there client on the eMule forum..
*/
/**
* The *Zone* is just a node in a binary tree of *Zone*s.
* Each zone is either an internal node or a leaf node.
* Internal nodes have "bin == null" and "subZones[i] != null",
* leaf nodes have "subZones[i] == null" and "bin != null".
*
* All key unique id's are relative to the center (self), which
* is considered to be 000..000
*/
#include "stdafx.h"
#include <math.h>
#include "./RoutingZone.h"
#include "./RoutingBin.h"
#include "../utils/MiscUtils.h"
#include "../kademlia/Kademlia.h"
#include "../kademlia/Prefs.h"
#include "../kademlia/SearchManager.h"
#include "../kademlia/Defines.h"
#include "../net/KademliaUDPListener.h"
#include "../../Opcodes.h"
#include "../../emule.h"
#include "../../emuledlg.h"
#include "../../KadContactListCtrl.h"
#include "../../kademliawnd.h"
#include "../../SafeFile.h"
#include "../../Log.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace Kademlia;
void DebugSend(LPCTSTR pszMsg, uint32 uIP, uint16 uUDPPort);
CString CRoutingZone::m_sFilename;
CUInt128 CRoutingZone::uMe = (ULONG)0;
CRoutingZone::CRoutingZone()
{
// Can only create routing zone after prefs
// Set our KadID for creating the contact tree
CKademlia::GetPrefs()->GetKadID(&uMe);
// Set the preference file name.
m_sFilename = CMiscUtils::GetAppDir();
m_sFilename.Append(CONFIGFOLDER);
m_sFilename.Append(_T("nodes.dat"));
// Init our root node.
Init(NULL, 0, CUInt128((ULONG)0));
}
CRoutingZone::CRoutingZone(LPCSTR szFilename)
{
// Can only create routing zone after prefs
// Set our KadID for creating the contact tree
CKademlia::GetPrefs()->GetKadID(&uMe);
m_sFilename = szFilename;
// Init our root node.
Init(NULL, 0, CUInt128((ULONG)0));
}
CRoutingZone::CRoutingZone(CRoutingZone *pSuper_zone, int iLevel, const CUInt128 &uZone_index)
{
// Create a new leaf.
Init(pSuper_zone, iLevel, uZone_index);
}
void CRoutingZone::Init(CRoutingZone *pSuper_zone, int iLevel, const CUInt128 &uZone_index)
{
// Init all Zone vars
// Set this zones parent
m_pSuperZone = pSuper_zone;
// Set this zones level
m_uLevel = iLevel;
// Set this zones CUInt128 Index
m_uZoneIndex = uZone_index;
// Mark this zone has having now leafs.
m_pSubZones[0] = NULL;
m_pSubZones[1] = NULL;
// Create a new contact bin as this is a leaf.
m_pBin = new CRoutingBin();
// Set timer so that zones closer to the root are processed earlier.
m_tNextSmallTimer = time(NULL) + m_uZoneIndex.Get32BitChunk(3);
// Start this zone.
StartTimer();
// If we are initializing the root node, read in our saved contact list.
if ((m_pSuperZone == NULL) && (m_sFilename.GetLength() > 0))
ReadFile();
}
CRoutingZone::~CRoutingZone()
{
// Root node is processed first so that we can write our contact list and delete all branches.
if ((m_pSuperZone == NULL) && (m_sFilename.GetLength() > 0))
{
// Hide contacts in the GUI
theApp.emuledlg->kademliawnd->HideContacts();
WriteFile();
}
// If this zone is a leaf, delete our contact bin.
if (IsLeaf())
delete m_pBin;
else
{
// If this zone is branch, delete it's leafs.
delete m_pSubZones[0];
delete m_pSubZones[1];
}
// All branches are deleted, show the contact list in the GUI.
if (m_pSuperZone == NULL)
theApp.emuledlg->kademliawnd->ShowContacts();
}
void CRoutingZone::ReadFile()
{
// Read in the saved contact list.
try
{
// Hide contact list in the GUI
theApp.emuledlg->kademliawnd->HideContacts();
CSafeBufferedFile file;
CFileException fexp;
if (file.Open(m_sFilename, CFile::modeRead | CFile::osSequentialScan|CFile::typeBinary|CFile::shareDenyWrite, &fexp))
{
setvbuf(file.m_pStream, NULL, _IOFBF, 32768);
// Get how many contacts in the saved list.
// NOTE: Older clients put the number of contacts here..
// Newer clients always have 0 here to prevent older clients from reading it.
uint32 uNumContacts = file.ReadUInt32();
uint32 uVersion = 0;
if (uNumContacts == 0)
{
try
{
uVersion = file.ReadUInt32();
if(uVersion == 1)
uNumContacts = file.ReadUInt32();
}
catch(...)
{
AddDebugLogLine( false, GetResString(IDS_ERR_KADCONTACTS));
}
}
if (uNumContacts != 0)
{
uint32 uValidContacts = 0;
CUInt128 uID;
while ( uNumContacts )
{
file.ReadUInt128(&uID);
uint32 uIP = file.ReadUInt32();
uint16 uUDPPort = file.ReadUInt16();
uint16 uTCPPort = file.ReadUInt16();
uint8 uContactVersion = 0;
byte byType = 0;
if(uVersion == 1)
uContactVersion = file.ReadUInt8();
else
byType = file.ReadUInt8();
// IP Appears valid
if( byType < 4)
{
// This was not a dead contact, Inc counter if add was successful
if( Add(uID, uIP, uUDPPort, uTCPPort, uContactVersion) )
uValidContacts++;
}
uNumContacts--;
}
AddLogLine( false, GetResString(IDS_KADCONTACTSREAD), uValidContacts);
}
file.Close();
}
}
catch (CFileException* e)
{
e->Delete();
AddDebugLogLine(false, _T("CFileException in CRoutingZone::readFile"));
}
// Show contact list in GUI
theApp.emuledlg->kademliawnd->ShowContacts();
}
void CRoutingZone::WriteFile()
{
try
{
// Write a saved contact list.
CUInt128 uID;
CSafeBufferedFile file;
CFileException fexp;
if (file.Open(m_sFilename, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary|CFile::shareDenyWrite, &fexp))
{
setvbuf(file.m_pStream, NULL, _IOFBF, 32768);
// The bootstrap method gets a very nice sample of contacts to save.
ContactList listContacts;
GetBootstrapContacts(&listContacts, 200);
// Start file with 0 to prevent older clients from reading it.
file.WriteUInt32(0);
// Now tag it with a version which happens to be 1.
file.WriteUInt32(1);
file.WriteUInt32((uint32)listContacts.size());
for (ContactList::const_iterator itContactList = listContacts.begin(); itContactList != listContacts.end(); ++itContactList)
{
CContact* pContact = *itContactList;
pContact->GetClientID(&uID);
file.WriteUInt128(&uID);
file.WriteUInt32(pContact->GetIPAddress());
file.WriteUInt16(pContact->GetUDPPort());
file.WriteUInt16(pContact->GetTCPPort());
file.WriteUInt8(pContact->GetVersion());
}
file.Close();
AddDebugLogLine( false, _T("Wrote %ld contact%s to file."), listContacts.size(), ((listContacts.size() == 1) ? _T("") : _T("s")));
}
}
catch (CFileException* e)
{
e->Delete();
AddDebugLogLine(false, _T("CFileException in CRoutingZone::writeFile"));
}
}
bool CRoutingZone::CanSplit() const
{
// Max levels allowed.
if (m_uLevel >= 127)
return false;
// Check if this zone is allowed to split.
if ( (m_uZoneIndex < KK || m_uLevel < KBASE) && m_pBin->GetSize() == K)
return true;
return false;
}
bool CRoutingZone::Add(const CUInt128 &uID, uint32 uIP, uint16 uUDPPort, uint16 uTCPPort, uint8 uVersion )
{
if(::IsGoodIPPort(ntohl(uIP), uUDPPort))
{
if(uID != uMe)
{
// JOHNTODO -- How do these end up leaking at times?
CContact* pContact = new CContact(uID, uIP, uUDPPort, uTCPPort, uVersion);
if(Add(pContact))
return true;
delete pContact;
}
}
return false;
}
bool CRoutingZone::Add(CContact* pContact)
{
// If we are not a leaf, call add on the correct branch.
if (!IsLeaf())
return m_pSubZones[pContact->GetDistance().GetBitNumber(m_uLevel)]->Add(pContact);
else
{
// Do we already have a contact with this KadID?
if (m_pBin->GetContact(pContact->GetClientID()))
return false;
else if (m_pBin->GetRemaining())
{
// This bin is not full, so add the new contact.
if(m_pBin->AddContact(pContact))
{
// Add was successful, add to the GUI and let contact know it's listed in the gui.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -