kademliaudplistener.cpp

来自「wxWidgets写的电驴」· C++ 代码 · 共 1,226 行 · 第 1/3 页

CPP
1,226
字号
//// This file is part of aMule Project//// Copyright (c) 2004-2006 Angel Vidal (Kry) ( kry@amule.org )// Copyright (c) 2004-2006 aMule Project ( http://www.amule-project.net )// 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, 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, pleasepost 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 knowingwhat 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 advertisethere client on the eMule forum..*/#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 "../io/IOException.h"#include "../../NetworkFunctions.h"#include "../../KnownFile.h"#include "../../KnownFileList.h"#include "../../OtherFunctions.h"#include "../kademlia/Indexed.h"#include "../kademlia/Tag.h"#include "../../OPCodes.h"#include "../kademlia/Defines.h"#include "../../amule.h"#include "../../ClientUDPSocket.h"#include "../../Packet.h"#include "../../ClientList.h"#include "../../Statistics.h"#include "../../MemFile.h"#include "../../updownclient.h"#include "../../ClientTCPSocket.h"#include "../../Logger.h"#include <common/Format.h>#include "../../Preferences.h"#include <wx/tokenzr.h>#include <wx/arrstr.h>#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif#ifndef AMULE_DAEMON#include "amuleDlg.h"#include "KadDlg.h"#endifextern wxChar* InvKadKeywordChars;////////////////////////////////////////using namespace Kademlia;////////////////////////////////////////void CKademliaUDPListener::bootstrap(uint32 ip, uint16 port){	wxASSERT(ip);	sendMyDetails(KADEMLIA_BOOTSTRAP_REQ, ip, port);}void CKademliaUDPListener::sendMyDetails(byte opcode, uint32 ip, uint16 port){	CMemFile bio(25);	bio.WriteUInt128(CKademlia::getPrefs()->getKadID());	bio.WriteUInt32(CKademlia::getPrefs()->getIPAddress());	bio.WriteUInt16(thePrefs::GetEffectiveUDPPort());	bio.WriteUInt16(thePrefs::GetPort());	bio.WriteUInt8(0);	sendPacket(&bio, opcode, ip, port);}void CKademliaUDPListener::firewalledCheck(uint32 ip, uint16 port){	CMemFile bio(2);	bio.WriteUInt16(thePrefs::GetPort());	sendPacket(&bio, KADEMLIA_FIREWALLED_REQ, ip, port);}void CKademliaUDPListener::sendNullPacket(byte opcode,uint32 ip, uint16 port){	CMemFile bio(0);	AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadNullPacket %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));	sendPacket(&bio, opcode, ip, port);}void CKademliaUDPListener::publishPacket(uint32 ip, uint16 port, const CUInt128 &targetID, const CUInt128 &contactID, const TagList& tags){	//We need to get the tag lists working with CMemFiles..	byte packet[1024];	CByteIO bio(packet, sizeof(packet));	bio.writeByte(OP_KADEMLIAHEADER);	bio.writeByte(KADEMLIA_PUBLISH_REQ);	bio.writeUInt128(targetID);	//We only use this for publishing sources now.. So we always send one here..	bio.writeUInt16(1);	bio.writeUInt128(contactID);	bio.writeTagList(tags);	uint32 len = sizeof(packet) - bio.getAvailable();	sendPacket(packet, len,  ip, port);}void CKademliaUDPListener::processPacket(const byte* data, uint32 lenData, uint32 ip, uint16 port){	//Update connection state only when it changes.	bool curCon = CKademlia::getPrefs()->hasHadContact();	CKademlia::getPrefs()->setLastContact();	if( curCon != CKademlia::getPrefs()->hasHadContact()) {		theApp.ShowConnectionState();	}	byte opcode = data[1];	const byte *packetData = data + 2;	uint32 lenPacket = lenData - 2;	switch (opcode) {		case KADEMLIA_BOOTSTRAP_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadBootstrapReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processBootstrapRequest(packetData, lenPacket, ip, port);			break;		case KADEMLIA_BOOTSTRAP_RES:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadBootstrapRes from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));						processBootstrapResponse(packetData, lenPacket, ip, port);			break;		case KADEMLIA_HELLO_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadHelloReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processHelloRequest(packetData, lenPacket, ip, port);			break;		case KADEMLIA_HELLO_RES:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadHelloRes from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));						processHelloResponse(packetData, lenPacket, ip, port);			break;		case KADEMLIA_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));						processKademliaRequest(packetData, lenPacket, ip, port);			break;		case KADEMLIA_RES:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadRes from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processKademliaResponse(packetData, lenPacket, ip, port);			break;		case KADEMLIA_SEARCH_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadSearchReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));						processSearchRequest(packetData, lenPacket, ip, port);			break;		case KADEMLIA_SEARCH_RES:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadSearchRes from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));						processSearchResponse(packetData, lenPacket, ip, port);			break;		case KADEMLIA_PUBLISH_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadPublishReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));						processPublishRequest(packetData, lenPacket, ip, port);			break;		case KADEMLIA_PUBLISH_RES:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadPublishRes from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));						processPublishResponse(packetData, lenPacket, ip, port);			break;		case KADEMLIA_SRC_NOTES_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadSearchNotesReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processSearchNotesRequest(packetData, lenPacket, ip, port);			break;		case KADEMLIA_SRC_NOTES_RES:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadSearchNotesRes from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processSearchNotesResponse(packetData, lenPacket, ip, port);			break;		case KADEMLIA_PUB_NOTES_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadPublishNotesReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processPublishNotesRequest(packetData, lenPacket, ip, port);			break;		case KADEMLIA_PUB_NOTES_RES:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadPublishNotesRes from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processPublishNotesResponse(packetData, lenPacket, ip, port);			break;		case KADEMLIA_FIREWALLED_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadFirewalledReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processFirewalledRequest(packetData, lenPacket, ip, port);			break;		case KADEMLIA_FIREWALLED_RES:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadFirewalledRes from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processFirewalledResponse(packetData, lenPacket, ip, port);			break;		case KADEMLIA_FIREWALLED_ACK:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadFirewalledAck from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processFirewalledResponse2(packetData, lenPacket, ip, port);			break;		case KADEMLIA_FINDBUDDY_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadFindBuddyReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processFindBuddyRequest(packetData, lenPacket, ip, port);			break;		case KADEMLIA_FINDBUDDY_RES:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadFindBuddyRes from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processFindBuddyResponse(packetData, lenPacket, ip, port);			break;		case KADEMLIA_CALLBACK_REQ:			AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadCallbackReq from %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));			processCallbackRequest(packetData, lenPacket, ip, port);			break;		default: {			throw wxString::Format(wxT("Unknown opcode %02x on CKademliaUDPListener::processPacket"), opcode);		}	}}void CKademliaUDPListener::addContact( const byte *data, uint32 lenData, uint32 ip, uint16 port, uint16 tport){	CMemFile bio((byte*)data, lenData);	CUInt128 id = bio.ReadUInt128();	bio.ReadUInt32();	bio.ReadUInt16();	if( tport ) {		bio.ReadUInt16();	} else {		tport = bio.ReadUInt16();	}	byte type = bio.ReadUInt8();	//AddDebugLogLineM(false, logKadMain, wxT("Adding a contact with ip ") + Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip),port));	// Look for existing client	CContact *contact = CKademlia::getRoutingZone()->getContact(id);	if (contact != NULL) {		contact->setIPAddress(ip);		contact->setUDPPort(port);		contact->setTCPPort(tport);	} else {		if(IsGoodIPPort(wxUINT32_SWAP_ALWAYS(ip),port)) {			// Ignore stated ip and port, use the address the packet came from			CKademlia::getRoutingZone()->add(id, ip, port, tport, type);		}	}}void CKademliaUDPListener::addContacts( const byte *data, uint32 lenData, uint16 numContacts){	CMemFile bio((byte*)data, lenData );	CRoutingZone *routingZone = CKademlia::getRoutingZone();	for (uint16 i=0; i<numContacts; i++) {		CUInt128 id = bio.ReadUInt128();		uint32 ip = bio.ReadUInt32();		ip = wxUINT32_SWAP_ALWAYS(ip);		uint16 port = bio.ReadUInt16();		uint16 tport = bio.ReadUInt16();		byte type = bio.ReadUInt8();		//AddDebugLogLineM(false, logKadMain, wxT("Adding contact(s) with ip ") + Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip),port));		if (IsGoodIPPort(wxUINT32_SWAP_ALWAYS(ip),port)) {			routingZone->add(id, ip, port, tport, type);		}	}}//KADEMLIA_BOOTSTRAP_REQvoid CKademliaUDPListener::processBootstrapRequest (const byte *packetData, uint32 lenPacket, uint32 ip, uint16 port){	// Verify packet is expected size	if (lenPacket != 25){		throw wxString::Format(wxT("***NOTE: Received wrong size (%u) packet in "), lenPacket) + wxString::FromAscii(__FUNCTION__);	}	// Add the sender to the list of contacts	addContact(packetData, lenPacket, ip, port);	// Get some contacts to return	ContactList contacts;	uint16 numContacts = 1 + (uint16)CKademlia::getRoutingZone()->getBootstrapContacts(&contacts, 20);	// Create response packet	//We only collect a max of 20 contacts here.. Max size is 527.	//2 + 25(20) + 15(1)	CMemFile bio(527);	// Write packet info	bio.WriteUInt16(numContacts);	CContact *contact;	ContactList::const_iterator it;	for (it = contacts.begin(); it != contacts.end(); it++) {		contact = *it;		bio.WriteUInt128(contact->getClientID());		bio.WriteUInt32(contact->getIPAddress());		bio.WriteUInt16(contact->getUDPPort());		bio.WriteUInt16(contact->getTCPPort());		bio.WriteUInt8(contact->getType());	}	bio.WriteUInt128(CKademlia::getPrefs()->getKadID());	bio.WriteUInt32(CKademlia::getPrefs()->getIPAddress());	bio.WriteUInt16(thePrefs::GetEffectiveUDPPort());	bio.WriteUInt16(thePrefs::GetPort());	bio.WriteUInt8(0);	// Send response	AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadBootstrapRes %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));	sendPacket(&bio, KADEMLIA_BOOTSTRAP_RES, ip, port);}//KADEMLIA_BOOTSTRAP_RESvoid CKademliaUDPListener::processBootstrapResponse (const byte *packetData, uint32 lenPacket, uint32 ip, uint16 port){	// Verify packet is expected size	if (lenPacket < 27){		throw wxString::Format(wxT("***NOTE: Received wrong size (%u) packet in "), lenPacket) + wxString::FromAscii(__FUNCTION__);	}	// How many contacts were given	CMemFile bio((byte*)packetData, lenPacket);	uint16 numContacts = bio.ReadUInt16();	// Verify packet is expected size	if (lenPacket != (uint32)(2 + 25*numContacts)) {		return;	}	// Add these contacts to the list.	addContacts(packetData+2, lenPacket-2, numContacts);	// Send sender to alive.	CKademlia::getRoutingZone()->setAlive(ip, port);}//KADEMLIA_HELLO_REQvoid CKademliaUDPListener::processHelloRequest (const byte *packetData, uint32 lenPacket, uint32 ip, uint16 port){	// Verify packet is expected size	if (lenPacket != 25){		throw wxString::Format(wxT("***NOTE: Received wrong size (%u) packet in "), lenPacket) + wxString::FromAscii(__FUNCTION__);	}	// Add the sender to the list of contacts	addContact(packetData, lenPacket, ip, port);	// Send response	AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadHelloRes %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));		sendMyDetails(KADEMLIA_HELLO_RES, ip, port);	// Check if firewalled	if(CKademlia::getPrefs()->getRecheckIP()) {		firewalledCheck(ip, port);	}}//KADEMLIA_HELLO_RESvoid CKademliaUDPListener::processHelloResponse (const byte *packetData, uint32 lenPacket, uint32 ip, uint16 port){	// Verify packet is expected size	if (lenPacket != 25){		throw wxString::Format(wxT("***NOTE: Received wrong size (%u) packet in "), lenPacket) + wxString::FromAscii(__FUNCTION__);	}	// Add or Update contact.	addContact(packetData, lenPacket, ip, port);	// Set contact to alive.	CKademlia::getRoutingZone()->setAlive(ip, port);}//KADEMLIA_REQvoid CKademliaUDPListener::processKademliaRequest (const byte *packetData, uint32 lenPacket, uint32 ip, uint16 port){	// Verify packet is expected size	if (lenPacket != 33){		throw wxString::Format(wxT("***NOTE: Received wrong size (%u) packet in "), lenPacket) + wxString::FromAscii(__FUNCTION__);	}	//RecheckIP and firewall status	if(CKademlia::getPrefs()->getRecheckIP())	{		firewalledCheck(ip, port);		AddDebugLogLineM(false, logClientKadUDP, CFormat(wxT("KadHelloReq %s")) % Uint32_16toStringIP_Port(wxUINT32_SWAP_ALWAYS(ip), port));		sendMyDetails(KADEMLIA_HELLO_REQ, ip, port);	}	// Get target and type	CMemFile bio((byte*)packetData, lenPacket);	byte type = bio.ReadUInt8();//		bool flag1 = (type >> 6); //Reserved//		bool flag2 = (type >> 7); //Reserved//		bool flag3 = (type >> 8); //Reserved	type = type & 0x1F;	if( type == 0 ) {		throw wxString::Format(wxT("***NOTE: Received wrong type (0x%02x) in "), type) + wxString::FromAscii(__FUNCTION__);	}	//This is the target node trying to be found.	CUInt128 target = bio.ReadUInt128();	CUInt128 distance(CKademlia::getPrefs()->getKadID());	distance.XOR(target);

⌨️ 快捷键说明

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