📄 kademliaudplistener.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.
*/
// 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 "../io/IOException.h"
#include "../utils/MiscUtils.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 "emule.h"
#include "ClientUDPSocket.h"
#include "Packets.h"
#include "emuledlg.h"
#include "KadContactListCtrl.h"
#include "kademliawnd.h"
#include "clientlist.h"
#include "UploadQueue.h"
#include "SafeFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
////////////////////////////////////////
using namespace Kademlia;
////////////////////////////////////////
void DebugSend(LPCTSTR pszOpcode, uint32 ip, uint16 port)
{
Debug(_T(">>> %-20s to %-15s:%u\n"), pszOpcode, ipstr(ntohl(ip)), port);
}
void DebugSendF(LPCTSTR pszOpcode, uint32 ip, uint16 port, LPCTSTR pszMsg, ...)
{
va_list args;
va_start(args, pszMsg);
CString str;
str.Format(_T(">>> %-20s to %-15s:%-5u; "), pszOpcode, ipstr(ntohl(ip)), port);
str.AppendFormatV(pszMsg, args);
va_end(args);
Debug(_T("%s\n"), str);
}
void DebugRecv(LPCTSTR pszOpcode, uint32 ip, uint16 port)
{
Debug(_T("%-24s from %-15s:%u\n"), pszOpcode, ipstr(ntohl(ip)), port);
}
void CKademliaUDPListener::bootstrap(LPCSTR host, uint16 port)
{
uint32 retVal = 0;
if (isalpha(host[0]))
{
hostent *hp = gethostbyname(host);
if (hp == NULL)
return;
memcpy (&retVal, hp->h_addr, sizeof(retVal));
}
else
retVal = inet_addr(host);
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KadBootstrapReq", ntohl(retVal), port);
sendMyDetails(KADEMLIA_BOOTSTRAP_REQ, ntohl(retVal), port);
}
void CKademliaUDPListener::bootstrap(uint32 ip, uint16 port)
{
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KadBootstrapReq", ip, port);
sendMyDetails(KADEMLIA_BOOTSTRAP_REQ, ip, port);
}
void CKademliaUDPListener::sendMyDetails(byte opcode, uint32 ip, uint16 port)
{
ASSERT( CKademlia::getPrefs() != NULL );
CSafeMemFile bio(25);
bio.WriteUInt128(&CKademlia::getPrefs()->getClientID());
bio.WriteUInt32(CKademlia::getPrefs()->getIPAddress());
bio.WriteUInt16(thePrefs.GetUDPPort());
bio.WriteUInt16(thePrefs.GetPort());
bio.WriteUInt8(0);
sendPacket(&bio, opcode, ip, port);
}
void CKademliaUDPListener::firewalledCheck(uint32 ip, uint16 port)
{
CSafeMemFile bio(2);
bio.WriteUInt16(thePrefs.GetPort());
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KadFirewalledReq", ip, port);
sendPacket(&bio, KADEMLIA_FIREWALLED_REQ, ip, port);
}
void CKademliaUDPListener::sendNullPacket(byte opcode,uint32 ip, uint16 port)
{
CSafeMemFile bio(0);
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KadNullPacket", 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 CSafeMemFiles..
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();
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KadPublishReq", ip, port);
sendPacket(packet, len, ip, port);
}
void CKademliaUDPListener::processPacket(const byte* data, uint32 lenData, uint32 ip, uint16 port)
{
CPrefs *prefs = CKademlia::getPrefs();
ASSERT(prefs != NULL);
//Update constate only when it changes.
bool curCon = prefs->getLastContact();
prefs->setLastContact();
if( curCon != prefs->getLastContact())
theApp.emuledlg->ShowConnectionState();
byte opcode = data[1];
const byte *packetData = data + 2;
uint32 lenPacket = lenData - 2;
// CKademlia::debugMsg("Processing UDP Packet from %s port %ld : opcode length %ld", inet_ntoa(senderAddress->sin_addr), ntohs(senderAddress->sin_port), lenPacket);
// CMiscUtils::debugHexDump(packetData, lenPacket);
switch (opcode)
{
case KADEMLIA_BOOTSTRAP_REQ:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadBootstrapReq", ip, port);
processBootstrapRequest(packetData, lenPacket, ip, port);
break;
case KADEMLIA_BOOTSTRAP_RES:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadBootstrapRes", ip, port);
processBootstrapResponse(packetData, lenPacket, ip, port);
break;
case KADEMLIA_HELLO_REQ:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadHelloReq", ip, port);
processHelloRequest(packetData, lenPacket, ip, port);
break;
case KADEMLIA_HELLO_RES:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadHelloRes", ip, port);
processHelloResponse(packetData, lenPacket, ip, port);
break;
case KADEMLIA_REQ:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadReq", ip, port);
processKademliaRequest(packetData, lenPacket, ip, port);
break;
case KADEMLIA_RES:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadRes", ip, port);
processKademliaResponse(packetData, lenPacket, ip, port);
break;
case KADEMLIA_SEARCH_REQ:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadSearchReq", ip, port);
processSearchRequest(packetData, lenPacket, ip, port);
break;
case KADEMLIA_SEARCH_RES:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadSearchRes", ip, port);
processSearchResponse(packetData, lenPacket, ip, port);
break;
case KADEMLIA_PUBLISH_REQ:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadPublishReq", ip, port);
processPublishRequest(packetData, lenPacket, ip, port);
break;
case KADEMLIA_PUBLISH_RES:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadPublishRes", ip, port);
processPublishResponse(packetData, lenPacket, ip, port);
break;
case KADEMLIA_FIREWALLED_REQ:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadFirewalledReq", ip, port);
processFirewalledRequest(packetData, lenPacket, ip, port);
break;
case KADEMLIA_FIREWALLED_RES:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadFirewalledRes", ip, port);
processFirewalledResponse(packetData, lenPacket, ip, port);
break;
case KADEMLIA_FIREWALLED_ACK:
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugRecv("KadFirewalledAck", ip, port);
processFirewalledResponse2(packetData, lenPacket, ip, port);
break;
default:
{
CString strError;
strError.Format("Unknown opcode %02x", opcode);
throw strError;
}
}
}
void CKademliaUDPListener::addContact( const byte *data, uint32 lenData, uint32 ip, uint16 port, uint16 tport)
{
CSafeMemFile 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.ReadUInt8();
// Look for existing client
CContact *contact = routingZone->getContact(id);
if (contact != NULL)
{
contact->setIPAddress(ip);
contact->setUDPPort(port);
contact->setTCPPort(tport);
theApp.emuledlg->kademliawnd->contactList->ContactRef(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);
}
}
}
void CKademliaUDPListener::addContacts( const byte *data, uint32 lenData, uint16 numContacts)
{
CSafeMemFile 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();
uint16 port = bio.ReadUInt16();
uint16 tport = bio.ReadUInt16();
byte type = bio.ReadUInt8();
if(::IsGoodIPPort(ntohl(ip),port))
{
routingZone->add(id, ip, port, tport, type);
}
}
}
//KADEMLIA_BOOTSTRAP_REQ
void CKademliaUDPListener::processBootstrapRequest (const byte *packetData, uint32 lenPacket, uint32 ip, uint16 port)
{
// Verify packet is expected size
if (lenPacket != 25){
CString strError;
strError.Format("***NOTE: Received wrong size (%u) packet in %s", lenPacket, __FUNCTION__);
throw strError;
}
//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, ip, port);
// Get some contacts to return
ContactList contacts;
uint16 numContacts = 1 + (uint16)routingZone->getBootstrapContacts(&contacts, 20);
// Create response packet
//We only collect a max of 20 contacts here.. Max size is 527.
//2 + 25(20) + 15(1)
CSafeMemFile bio(527);
CUInt128 id;
// Write packet info
bio.WriteUInt16(numContacts);
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());
bio.WriteUInt16(contact->getUDPPort());
bio.WriteUInt16(contact->getTCPPort());
bio.WriteUInt8(contact->getType());
}
prefs->getClientID(&id);
bio.WriteUInt128(&id);
bio.WriteUInt32(prefs->getIPAddress());
bio.WriteUInt16(thePrefs.GetUDPPort());
bio.WriteUInt16(thePrefs.GetPort());
bio.WriteUInt8(0);
// Send response
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KadBootstrapRes", ip, port);
sendPacket(&bio, KADEMLIA_BOOTSTRAP_RES, ip, port);
}
//KADEMLIA_BOOTSTRAP_RES
void CKademliaUDPListener::processBootstrapResponse (const byte *packetData, uint32 lenPacket, uint32 ip, uint16 port)
{
// Verify packet is expected size
if (lenPacket < 27){
CString strError;
strError.Format("***NOTE: Received wrong size (%u) packet in %s", lenPacket, __FUNCTION__);
throw strError;
}
//Used Pointers
CRoutingZone *routingZone = CKademlia::getRoutingZone();
ASSERT(routingZone != NULL);
// How many contacts were given
CSafeMemFile bio( packetData, lenPacket);
uint16 numContacts = bio.ReadUInt16();
// Verify packet is expected size
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -