⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kademliaudplistener.cpp

📁 Kademlia---第第3代P2P原代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
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.
*/

// 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..
*/

#include "stdafx.h"
#include "KademliaUDPListener.h"
#include "../kademlia/Prefs.h"
#include "../kademlia/Kademlia.h"
#include "../kademlia/SearchManager.h"
#include "../routing/Contact.h"
#include "../routing/RoutingZone.h"
#include "../io/ByteIO.h"
#include "../utils/MiscUtils.h"
#include "otherfunctions.h"
#include "../kademlia/Indexed.h"
#include "../kademlia/tag.h"
#include "OpCodes.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


////////////////////////////////////////
using namespace Kademlia;
////////////////////////////////////////

PartialRequestHandler CKademliaUDPListener::partialRequestHandler = NULL;
PartialResponseHandler CKademliaUDPListener::partialResponseHandler = NULL;
RevConnectRequestHandler CKademliaUDPListener::revConnectRequestHandler = NULL;


void CKademliaUDPListener::bootstrap(const LPCSTR ip, const uint16 port)
{
//	CKademlia::debugMsg("Sent UDP OpCode KADEMLIA_CONNECT 0x0A (%u)", ip);
	sendMyDetails(KADEMLIA_BOOTSTRAP_REQ, nameToIP(ip), port);
}

void CKademliaUDPListener::bootstrap(const uint32 ip, const uint16 port)
{
//	CKademlia::debugMsg("Sent UDP OpCode KADEMLIA_CONNECT (%u)", ip);
	sendMyDetails(KADEMLIA_BOOTSTRAP_REQ, ip, port);
}

void CKademliaUDPListener::sendMyDetails(const byte opcode, const uint32 ip, const uint16 port)
{
	byte packet[27];
	CByteIO bio(packet, sizeof(packet));
	bio.writeByte(OP_KADEMLIAHEADER);
	bio.writeByte(opcode);
	CUInt128 id;
	CPrefs *prefs = CKademlia::getPrefs();
	ASSERT(prefs != NULL); 
	prefs->getClientID(&id);
	bio.writeUInt128(id);
	bio.writeUInt32(prefs->getIPAddress());
	bio.writeUInt16(prefs->getUDPPort());
	bio.writeUInt16(prefs->getTCPPort());
	bio.writeByte(0);
//	CKademlia::debugMsg("Sent UDP OpCode MyDetails (%u)", opcode);
	sendPacket(packet, sizeof(packet), ip, port);
}

void CKademliaUDPListener::firewalledCheck(const uint32 ip, const uint16 port)
{
	byte packet[4];
	CByteIO bio(packet, sizeof(packet));
	bio.writeByte(OP_KADEMLIAHEADER);
	bio.writeByte(KADEMLIA_FIREWALLED_REQ);
	CPrefs *prefs = CKademlia::getPrefs();
	ASSERT(prefs != NULL); 
	bio.writeUInt16(prefs->getTCPPort());
//	CKademlia::debugMsg("Sent UDP OpCode KADEMLIA_FIREWALLED_REQ (%u)", ip);
	sendPacket(packet, sizeof(packet), ip, port);
}

void CKademliaUDPListener::sendNullPacket(byte opcode,uint32 ip, uint16 port)
{
	byte packet[2];
	CByteIO bio(packet, sizeof(packet));
	bio.writeByte(OP_KADEMLIAHEADER);
	bio.writeByte(opcode);
//	CKademlia::debugMsg("Sent UDP OpCode NullPacket (%u)", ip);
	sendPacket(packet, sizeof packet, ip, port);
}

void CKademliaUDPListener::publishPacket(const uint32 ip, const uint16 port, const Kademlia::CUInt128 &targetID, const Kademlia::CUInt128 &contactID, const TagList& tags)
{
	byte packet[1024];
	CByteIO bio(packet, sizeof(packet));
	bio.writeByte(OP_KADEMLIAHEADER);
	bio.writeByte(KADEMLIA_PUBLISH_REQ);
	bio.writeUInt128(targetID);
	//This is for future use.. This is the count of items to publish within the packet.
	//This way if you have several items with the same key, you can send them all in one packet.
	//This will be even greater use when compression is added..
	bio.writeUInt16(1); //TODO: OPT???
	bio.writeUInt128(contactID);
	bio.writeTagList(tags);
	uint32 len = sizeof(packet) - bio.getAvailable();
	sendPacket(packet, len,  ip, port);
}

void CKademliaUDPListener::processPacket( byte *data, uint32 lenData, const sockaddr_in *senderAddress)
{
	try
	{
		CPrefs *prefs = CKademlia::getPrefs();
		ASSERT(prefs != NULL); 
		prefs->setLastContact();

		byte opcode = data[1];
		const byte *packetData = data + 2;
		uint32 lenPacket = lenData - 2;

#ifdef DEBUG
		CKademlia::debugMsg("Processing UDP Packet from %s port %ld : opcode %2X length %ld", inet_ntoa(senderAddress->sin_addr), ntohs(senderAddress->sin_port), opcode, lenPacket);
		CMiscUtils::debugHexDump(packetData, lenPacket);
#endif
		switch (opcode)
		{
			case KADEMLIA_BOOTSTRAP_REQ:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_BOOTSTRAP_REQ (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processBootstrapRequest(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_BOOTSTRAP_RES:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_BOOTSTRAP_RES (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processBootstrapResponse(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_HELLO_REQ:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_HELLO_REQ (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processHelloRequest(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_HELLO_RES:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_HELLO_RES_ACK (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processHelloResponse(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_REQ:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_REQ (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processKademliaRequest(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_RES:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_RES (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processKademliaResponse(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_SEARCH_REQ:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_SEARCH_REQ (%u)", ntohl(senderAddress->sin_addr.s_addr));
//				CMiscUtils::debugHexDump(packetData, lenPacket);
				processSearchRequest(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_SEARCH_RES:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_SEARCH_RES (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processSearchResponse(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_PUBLISH_REQ:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_PUBLISH_REQ (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processPublishRequest(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_PUBLISH_RES:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_PUBLISH_RES (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processPublishResponse(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_FIREWALLED_REQ:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_FIREWALED_REQ_REQ (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processFirewalledRequest(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_FIREWALLED_RES:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_FIREWALLED_RES (%u)", ntohl(senderAddress->sin_addr.s_addr));
				processFirewalledResponse(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_FIREWALLED_ACK:
//				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_FIREWALLED_ACK (%u)", ntohl(senderAddress->sin_addr.s_addr));
				this->processFirewalledResponse2(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_PARTIAL_REQ:
				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_PARTIAL_REQ (%u)", ntohl(senderAddress->sin_addr.s_addr));
				this->processPartialRequest(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_PARTIAL_RES:
				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_PARTIAL_RES (%u)", ntohl(senderAddress->sin_addr.s_addr));
				this->processPartialResponse(packetData, lenPacket, senderAddress);
				break;
			case KADEMLIA_REVCONNECT_REQ:
				CKademlia::debugMsg("Handled UDP OpCode KADEMLIA_REVCONNECTL_REQ (%u)", ntohl(senderAddress->sin_addr.s_addr));
				this->processRevConnectRequest(packetData, lenPacket, senderAddress);
				break;
			default:
				CKademlia::debugMsg("*************************");
				CKademlia::debugMsg("Unhandled UDP OpCode 0x%02X (%u)", opcode, ntohl(senderAddress->sin_addr.s_addr));
				CKademlia::debugMsg("*************************");
		}
	} catch (...) {TRACE("Exception is Kademlia Process Packet\n");}
}

void CKademliaUDPListener::addContact(const byte *data, const uint32 lenData, const uint32 ip, const uint16 port, uint16 tport)
{
	try
	{
		CByteIO bio(data, lenData);
		CRoutingZone *routingZone = CKademlia::getRoutingZone();
		ASSERT(routingZone != NULL); 
		CUInt128 id;
		bio.readUInt128(&id);
		bio.readUInt32();
		bio.readUInt16();
		if( tport )
			bio.readUInt16();
		else
			tport = bio.readUInt16();
		byte type = bio.readByte();
		CString key;
		id.toHexString(&key);
		// Look for existing client
		CContact *contact = routingZone->getContact(id);
		if (contact != NULL)
		{
			contact->setIPAddress(ip);
			contact->setUDPPort(port);
			contact->setTCPPort(tport);
			Kademlia::CKademlia::reportContactRef(contact);
		}
		else
		{
			if(::IsGoodIPPort(ntohl(ip),port))
			{
				// Ignore stated ip and port, use the address the packet came from
				routingZone->add(id, ip, port, tport, type);
			}
		}
	} catch (...) {TRACE("Exception in Kademlia Add Contact\n");}
}

void CKademliaUDPListener::addContacts(const byte *data, const uint32 lenData, const uint16 numContacts)
{
	try
	{
		CByteIO bio(data, lenData);
		CRoutingZone *routingZone = CKademlia::getRoutingZone();
		ASSERT(routingZone != NULL); 
		CUInt128 id;
		for (uint16 i=0; i<numContacts; i++)
		{
			bio.readUInt128(&id);
			uint32 ip = bio.readUInt32(); // Not LE
			uint16 port = bio.readUInt16();
			uint16 tport = bio.readUInt16();
			byte type = bio.readByte();
			if(::IsGoodIPPort(ntohl(ip),port))
			{
				routingZone->add(id, ip, port, tport, type);
			}
		}
	} catch (...) {TRACE("Exception in Kademlia Add Contacts\n");}
}

//KADEMLIA_BOOTSTRAP_REQ
void CKademliaUDPListener::processBootstrapRequest (const byte *packetData, const uint32 lenPacket, const sockaddr_in *senderAddress)
{
	try
	{
		// Verify packet is expected size
		if (lenPacket != 25){
			ASSERT(0);
			return;
		}

		//Used pointers.
		CRoutingZone *routingZone = CKademlia::getRoutingZone();
		ASSERT(routingZone != NULL); 
		CPrefs *prefs = CKademlia::getPrefs();
		ASSERT(prefs != NULL); 

		// Add the sender to the list of contacts
		addContact(packetData, lenPacket, ntohl(senderAddress->sin_addr.s_addr), ntohs(senderAddress->sin_port));

		// Get some contacts to return
		ContactList contacts;
		uint16 numContacts = 1 + (uint16)routingZone->getBootstrapContacts(&contacts, 20);

		// Create response packet
		uint32 lenResponse = 4 + numContacts * 25;
		byte *response = new byte[lenResponse];
		CByteIO bio(response, lenResponse);
		CUInt128 id;

		// Write packet info
		bio.writeByte(OP_KADEMLIAHEADER);
		bio.writeByte(KADEMLIA_BOOTSTRAP_RES);
		bio.writeUInt16(numContacts);//TODO: OPT??? ... (16+4+2+2+1)*255 = 6375 max.
		CContact *contact;
		ContactList::const_iterator it;
		for (it = contacts.begin(); it != contacts.end(); it++)
		{
			contact = *it;
			contact->getClientID(&id);
			bio.writeUInt128(id);
			bio.writeUInt32(contact->getIPAddress()); // Not LE
			bio.writeUInt16(contact->getUDPPort());
			bio.writeUInt16(contact->getTCPPort());
			bio.writeByte(contact->getType());
		}
		prefs->getClientID(&id);
		bio.writeUInt128(id);
		bio.writeUInt32(prefs->getIPAddress()); // Not LE
		bio.writeUInt16(prefs->getUDPPort());
		bio.writeUInt16(prefs->getTCPPort());
		bio.writeByte(0);

		// Send response
//		CKademlia::debugMsg("Sent UDP OpCode KADEMLIA_BOOTSTRAP_RES (%u)", ntohl(senderAddress->sin_addr.s_addr));
		sendPacket(response, lenResponse, ntohl(senderAddress->sin_addr.s_addr), ntohs(senderAddress->sin_port));

		// Finished with memory
		delete [] response;
	} catch (...) {TRACE("Exception in Kademlia Bootstrap Requests\n");}
}

//KADEMLIA_BOOTSTRAP_RES
void CKademliaUDPListener::processBootstrapResponse (const byte *packetData, const uint32 lenPacket, const sockaddr_in *senderAddress)
{
	try
	{
		// Verify packet is expected size
		if (lenPacket < 27){
			ASSERT(0);
			return;
		}

		//Used Pointers
		CRoutingZone *routingZone = CKademlia::getRoutingZone();
		ASSERT(routingZone != NULL); 

		// How many contacts were given
		CByteIO bio(packetData, lenPacket);
		uint16 numContacts = bio.readUInt16();

		// Verify packet is expected size
		if (lenPacket != (2 + 25*numContacts))
			return;

		// Add these contacts to the list.
		addContacts(packetData+2, lenPacket-2, numContacts);
		// Send sender to alive.
		routingZone->setAlive(ntohl(senderAddress->sin_addr.s_addr), ntohs(senderAddress->sin_port));

	} catch (...) {TRACE("Exception in Kademlia Bootstrap Response\n");}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -