1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
|
/*
* Copyright (C) 2009-2010 Big Muscle, http://strongdc.sourceforge.net/
* Copyright (C) 2019 Boris Pek <tehnick-8@yandex.ru>
*
* 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, see <https://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "DHT.h"
#include "BootstrapManager.h"
#include "ConnectionManager.h"
#include "IndexManager.h"
#include "SearchManager.h"
#include "TaskManager.h"
#include "Utils.h"
#include "dcpp/AdcCommand.h"
#include "dcpp/ChatMessage.h"
#include "dcpp/CID.h"
#include "dcpp/ClientManager.h"
#include "dcpp/CryptoManager.h"
#include "dcpp/File.h"
#include "dcpp/LogManager.h"
#include "dcpp/SettingsManager.h"
#include "dcpp/ShareManager.h"
#include "dcpp/ThrottleManager.h"
#include "dcpp/UploadManager.h"
#include "dcpp/User.h"
#include "dcpp/version.h"
namespace dht
{
DHT::DHT(void)
: bucket(nullptr)
, lastExternalIP(Util::getLocalIp()) // hack
, lastPacket(0)
, firewalled(true)
, requestFWCheck(true)
, dirty(false)
{
type = ClientBase::DHT;
IndexManager::newInstance();
}
DHT::~DHT(void) throw()
{
// when DHT is disabled, we shouldn't try to perform exit cleanup
if(bucket == nullptr) {
IndexManager::deleteInstance();
return;
}
stop(true);
IndexManager::deleteInstance();
}
/*
* Starts DHT.
*/
void DHT::start()
{
if(!BOOLSETTING(USE_DHT))
return;
// start with global firewalled status
firewalled = !ClientManager::getInstance()->isActive(Util::emptyString);
requestFWCheck = true;
if(!bucket)
{
if(!BOOLSETTING(NO_IP_OVERRIDE))
SettingsManager::getInstance()->set(SettingsManager::EXTERNAL_IP, Util::emptyString);
bucket = new KBucket();
BootstrapManager::newInstance();
SearchManager::newInstance();
TaskManager::newInstance();
ConnectionManager::newInstance();
loadData();
}
socket.listen();
BootstrapManager::getInstance()->bootstrap();
}
void DHT::stop(bool exiting)
{
if(!bucket)
return;
socket.disconnect();
if(!BOOLSETTING(USE_DHT) || exiting)
{
saveData();
lastPacket = 0;
ConnectionManager::deleteInstance();
TaskManager::deleteInstance();
SearchManager::deleteInstance();
BootstrapManager::deleteInstance();
delete bucket;
bucket = nullptr;
}
}
/*
* Process incoming command
*/
void DHT::dispatch(const string& aLine, const string& ip, const string& port, bool isUdpKeyValid)
{
// check node's IP address
if(!Utils::isGoodIPPort(ip, port))
{
//socket.send(AdcCommand(AdcCommand::SEV_FATAL, AdcCommand::ERROR_BAD_IP, "Your client supplied invalid IP: " + ip, AdcCommand::TYPE_UDP), ip, port);
return; // invalid ip/port supplied
}
try
{
AdcCommand cmd(aLine);
// flood protection
if(!Utils::checkFlood(ip, cmd))
return;
string cid = cmd.getParam(0);
if(cid.size() != 39)
return;
// ignore message from myself
if(CID(cid) == ClientManager::getInstance()->getMe()->getCID() || ip == lastExternalIP)
return;
lastPacket = GET_TICK();
// don't add node here, because it may block verified nodes when table becomes full of unverified nodes
Node::Ptr node = createNode(CID(cid), ip, port, true, isUdpKeyValid);
// all communication to this node will be encrypted with this key
string udpKey;
if(cmd.getParam("UK", 1, udpKey))
{
node->setUdpKey(CID(udpKey));
}
// node is requiring FW check
string internalUdpPort;
if(cmd.getParam("FW", 1, internalUdpPort))
{
bool firewalled = (internalUdpPort != port);
if(firewalled)
node->getUser()->setFlag(User::PASSIVE);
// send him his external ip and port
AdcCommand cmd(AdcCommand::SEV_SUCCESS, AdcCommand::SUCCESS, !firewalled ? "UDP port opened" : "UDP port closed", AdcCommand::TYPE_UDP);
cmd.addParam("FC", "FWCHECK");
cmd.addParam("I4", ip);
cmd.addParam("U4", port);
send(cmd, ip, port, node->getUser()->getCID(), node->getUdpKey());
}
#define C(n) case AdcCommand::CMD_##n: handle(AdcCommand::n(), node, cmd); break;
switch(cmd.getCommand())
{
C(INF); // user's info
C(SCH); // search request
C(RES); // response to SCH
C(PUB); // request to publish file
C(CTM); // connection request
C(RCM); // reverse connection request
C(STA); // status message
C(PSR); // partial file request
C(MSG); // private message
C(GET); // get some data
C(SND); // response to GET
default:
dcdebug("Unknown ADC command: %.50s\n", aLine.c_str());
break;
#undef C
}
}
catch(const ParseException&)
{
dcdebug("Invalid ADC command: %.50s\n", aLine.c_str());
}
}
/*
* Sends command to ip and port
*/
void DHT::send(AdcCommand& cmd, const string& ip, const string& port, const CID& targetCID, const CID& udpKey)
{
{
// FW check
Lock l(fwCheckCs);
if(requestFWCheck && (firewalledWanted.size() + firewalledChecks.size() < FW_RESPONSES))
{
if(firewalledWanted.count(ip) == 0) // only when not requested from this node yet
{
cmd.addParam("FW", getPort());
firewalledWanted.insert(ip);
}
}
}
socket.send(cmd, ip, port, targetCID, udpKey);
}
/*
* Creates new (or update existing) node which is NOT added to our routing table
*/
Node::Ptr DHT::createNode(const CID& cid, const string& ip, const string& port, bool update, bool isUdpKeyValid)
{
// create user as offline (only TCP connected users will be online)
UserPtr u = ClientManager::getInstance()->getUser(cid);
Lock l(cs);
return bucket->createNode(u, ip, port, update, isUdpKeyValid);
}
/*
* Adds node to routing table
*/
bool DHT::addNode(const Node::Ptr& node, bool makeOnline)
{
bool isAcceptable = true;
if(!node->isOnline())
{
{
Lock l(cs);
isAcceptable = bucket->insert(node); // insert node to our routing table
}
if(makeOnline)
{
// put him online so we can make a connection with him
node->inc();
node->setOnline(true);
ClientManager::getInstance()->putOnline(node.get());
}
}
return isAcceptable;
}
/*
* Finds "max" closest nodes and stores them to the list
*/
void DHT::getClosestNodes(const CID& cid, std::map<CID, Node::Ptr>& closest, unsigned int max, uint8_t maxType)
{
Lock l(cs);
bucket->getClosestNodes(cid, closest, max, maxType);
}
/*
* Removes dead nodes
*/
void DHT::checkExpiration(uint64_t aTick)
{
{
Lock l(cs);
if(bucket->checkExpiration(aTick))
setDirty();
}
{
Lock l(fwCheckCs);
firewalledWanted.clear();
}
}
/*
* Finds the file in the network
*/
void DHT::findFile(const string& tth, const string& token)
{
if(isConnected())
SearchManager::getInstance()->findFile(tth, token);
}
/*
* Sends our info to specified ip:port
*/
void DHT::info(const string& ip, const string& port, uint32_t type, const CID& targetCID, const CID& udpKey)
{
// TODO: what info is needed?
AdcCommand cmd(AdcCommand::CMD_INF, AdcCommand::TYPE_UDP);
cmd.addParam("TY", Util::toString(type));
cmd.addParam("VE", fullADCVersionString);
cmd.addParam("NI", SETTING(NICK));
cmd.addParam("SL", Util::toString(UploadManager::getInstance()->getSlots()));
int limit = ThrottleManager::getInstance()->getUpLimit();
if (SETTING(THROTTLE_ENABLE) && limit > 0) {
cmd.addParam("US", Util::toString(limit*1024));
} else {
cmd.addParam("US", Util::toString((long)(Util::toDouble(SETTING(UPLOAD_SPEED))*1024*1024/8)));
}
string su;
if(CryptoManager::getInstance()->TLSOk())
su += ADCS_FEATURE ",";
// TCP status according to global status
if(ClientManager::getInstance()->isActive(Util::emptyString))
su += TCP4_FEATURE ",";
// UDP status according to UDP status check
if(!isFirewalled())
su += UDP4_FEATURE ",";
if(!su.empty()) {
su.erase(su.size() - 1);
}
cmd.addParam("SU", su);
send(cmd, ip, port, targetCID, udpKey);
}
/*
* Sends Connect To Me request to online node
*/
void DHT::connect(const OnlineUser& ou, const string& token)
{
// this is DHT's node, so we can cast ou to Node
ConnectionManager::getInstance()->connect((Node*)&ou, token);
}
/*
* Sends private message to online node
*/
void DHT::privateMessage(const OnlineUser& /*ou*/, const string& /*aMessage*/, bool /*thirdPerson*/)
{
//AdcCommand cmd(AdcCommand::CMD_MSG, AdcCommand::TYPE_UDP);
//cmd.addParam(aMessage);
//if(thirdPerson)
// cmd.addParam("ME", "1");
//
//send(cmd, ou.getIdentity().getIp(), static_cast<uint16_t>(Util::toInt(ou.getIdentity().getUdpPort())));
}
/*
* Loads network information from XML file
*/
void DHT::loadData()
{
try
{
dcpp::File f(Util::getPath(Util::PATH_USER_CONFIG) + DHT_FILE, dcpp::File::READ, dcpp::File::OPEN);
SimpleXML xml;
xml.fromXML(f.read());
xml.stepIn();
// load nodes; when file is older than 7 days, bootstrap from database later
if(f.getLastModified() > time(nullptr) - 7 * 24 * 60 * 60)
bucket->loadNodes(xml);
// load indexes
IndexManager::getInstance()->loadIndexes(xml);
xml.stepOut();
}
catch(Exception& e)
{
dcdebug("%s\n", e.getError().c_str());
}
}
/*
* Finds "max" closest nodes and stores them to the list
*/
void DHT::saveData()
{
if(!dirty)
return;
Lock l(cs);
SimpleXML xml;
xml.addTag("DHT");
xml.stepIn();
// save nodes
bucket->saveNodes(xml);
// save foreign published files
IndexManager::getInstance()->saveIndexes(xml);
xml.stepOut();
try
{
dcpp::File file(Util::getPath(Util::PATH_USER_CONFIG) + DHT_FILE + ".tmp", dcpp::File::WRITE, dcpp::File::CREATE | dcpp::File::TRUNCATE);
BufferedOutputStream<false> bos(&file);
bos.write(SimpleXML::utf8Header);
xml.toXML(&bos);
bos.flush();
file.close();
dcpp::File::deleteFile(Util::getPath(Util::PATH_USER_CONFIG) + DHT_FILE);
dcpp::File::renameFile(Util::getPath(Util::PATH_USER_CONFIG) + DHT_FILE + ".tmp", Util::getPath(Util::PATH_USER_CONFIG) + DHT_FILE);
}
catch(const FileException&)
{
}
}
/*
* Message processing
*/
// user's info
void DHT::handle(AdcCommand::INF, const Node::Ptr& node, AdcCommand& c) throw()
{
//string ip = node->getIdentity().getIp();
string udpPort = node->getIdentity().getUdpPort();
InfType it = NONE;
for(StringIterC i = c.getParameters().begin() + 1; i != c.getParameters().end(); ++i)
{
if(i->length() < 2)
continue;
string parameter_name = i->substr(0, 2);
if(parameter_name == "TY")
it = (InfType)Util::toInt(i->substr(2));
else if((parameter_name != "I4") && (parameter_name != "U4") && (parameter_name != "UK")) // avoid IP+port spoofing + don't store key into map
node->getIdentity().set(i->c_str(), i->substr(2));
}
if(node->getIdentity().supports(ADCS_FEATURE))
{
node->getUser()->setFlag(User::TLS);
}
if(!node->getIdentity().get("US").empty()) {
node->getIdentity().setConnection(Util::formatBytes(node->getIdentity().get("US")) + "/s");
}
// add node to our routing table and put him online if it is required
addNode(node, true);
// do we wait for any search results from this user?
SearchManager::getInstance()->processSearchResults(node->getUser(), Util::toInt(node->getIdentity().get("SL")));
if(it & PING)
{
// remove ping flag to avoid ping-pong-ping-pong-ping...
info(node->getIdentity().getIp(), udpPort, it & ~PING, node->getUser()->getCID(), node->getUdpKey());
}
}
// incoming search request
void DHT::handle(AdcCommand::SCH, const Node::Ptr& node, AdcCommand& c) throw()
{
SearchManager::getInstance()->processSearchRequest(node, c);
}
// incoming search result
void DHT::handle(AdcCommand::RES, const Node::Ptr& node, AdcCommand& c) throw()
{
SearchManager::getInstance()->processSearchResult(node, c);
}
// incoming publish request
void DHT::handle(AdcCommand::PUB, const Node::Ptr& node, AdcCommand& c) throw()
{
if(!isFirewalled()) // we should index this entry only if our UDP port is opened
IndexManager::getInstance()->processPublishSourceRequest(node, c);
}
// connection request
void DHT::handle(AdcCommand::CTM, const Node::Ptr& node, AdcCommand& c) throw()
{
ConnectionManager::getInstance()->connectToMe(node, c);
}
// reverse connection request
void DHT::handle(AdcCommand::RCM, const Node::Ptr& node, AdcCommand& c) throw()
{
ConnectionManager::getInstance()->revConnectToMe(node, c);
}
// status message
void DHT::handle(AdcCommand::STA, const Node::Ptr& node, AdcCommand& c) throw()
{
if(c.getParameters().size() < 3)
return;
string fromIP = node->getIdentity().getIp();
int code = Util::toInt(c.getParam(1).substr(1));
if(code == 0)
{
string resTo;
if(!c.getParam("FC", 2, resTo))
return;
if(resTo == "PUB")
{
#ifdef _DEBUG
// don't do anything
string tth;
if(!c.getParam("TR", 1, tth))
return;
try
{
string fileName = Util::getFileName(ShareManager::getInstance()->toVirtual(TTHValue(tth)));
LogManager::getInstance()->message(str(F_("DHT (%1%): File published: %2%") % fromIP % fileName));
}
catch(ShareException&)
{
// published non-shared file??? Maybe partial file
LogManager::getInstance()->message(str(F_("DHT (%1%): Partial file published: %2%") % fromIP % tth));
}
#endif
}
else if(resTo == "FWCHECK")
{
Lock l(fwCheckCs);
if(!firewalledWanted.count(fromIP))
return; // we didn't requested firewall check from this node
firewalledWanted.erase(fromIP);
if(firewalledChecks.count(fromIP))
return; // already received firewall check from this node
string externalIP;
string externalUdpPort;
if(!c.getParam("I4", 1, externalIP) || !c.getParam("U4", 1, externalUdpPort))
return; // no IP and port in response
firewalledChecks.insert(std::make_pair(fromIP, std::make_pair(externalIP, externalUdpPort)));
if(firewalledChecks.size() == FW_RESPONSES)
{
// when we received more firewalled statuses, we will be firewalled
int fw = 0; string lastIP;
for(const auto &i : firewalledChecks)
{
const string ip = i.second.first;
const string udpPort = i.second.second;
if(udpPort != getPort())
fw++;
else
fw--;
if(lastIP.empty())
{
externalIP = ip;
lastIP = ip;
}
//If the last check matches this one, reset our current IP.
//If the last check does not match, wait for our next incoming IP.
//This happens for one reason.. a client responsed with a bad IP.
if(ip == lastIP)
externalIP = ip;
else
lastIP = ip;
}
if(fw >= 0)
{
// we are probably firewalled, so our internal UDP port is unaccessible
if(externalIP != lastExternalIP || !firewalled)
LogManager::getInstance()->message(str(F_("DHT: Firewalled UDP status set (IP: %1%)") % externalIP));
firewalled = true;
}
else
{
if(externalIP != lastExternalIP || firewalled)
LogManager::getInstance()->message(str(F_("DHT: Our UDP port seems to be opened (IP: %1%)") % externalIP));
firewalled = false;
}
if(!BOOLSETTING(NO_IP_OVERRIDE))
SettingsManager::getInstance()->set(SettingsManager::EXTERNAL_IP, externalIP);
firewalledChecks.clear();
firewalledWanted.clear();
lastExternalIP = externalIP;
requestFWCheck = false;
}
}
return;
}
// display message in all other cases
//string msg = c.getParam(2);
//if(!msg.empty())
// LogManager::getInstance()->message("DHT (" + fromIP + "): " + msg);
}
// partial file request
void DHT::handle(AdcCommand::PSR, const Node::Ptr& node, AdcCommand& c) throw()
{
c.getParameters().erase(c.getParameters().begin()); // remove CID from UDP command
dcpp::SearchManager::getInstance()->onPSR(c, node->getUser(), node->getIdentity().getIp());
}
// private message
void DHT::handle(AdcCommand::MSG, const Node::Ptr& /*node*/, AdcCommand& /*c*/) throw()
{
// not implemented yet
//fire(ClientListener::PrivateMessage(), this, *node, to, node, c.getParam(0), c.hasFlag("ME", 1));
//privateMessage(*node, "Sorry, private messages aren't supported yet!", false);
}
void DHT::handle(AdcCommand::GET, const Node::Ptr& node, AdcCommand& c) throw()
{
if(c.getParam(1) == "nodes" && c.getParam(2) == "dht.xml")
{
AdcCommand cmd(AdcCommand::CMD_SND, AdcCommand::TYPE_UDP);
cmd.addParam(c.getParam(1));
cmd.addParam(c.getParam(2));
SimpleXML xml;
xml.addTag("Nodes");
xml.stepIn();
// get 20 random contacts
Node::Map nodes;
DHT::getInstance()->getClosestNodes(CID::generate(), nodes, 20, 2);
// add nodelist in XML format
for(Node::Map::const_iterator i = nodes.begin(); i != nodes.end(); ++i)
{
xml.addTag("Node");
xml.addChildAttrib("CID", i->second->getUser()->getCID().toBase32());
xml.addChildAttrib("I4", i->second->getIdentity().getIp());
xml.addChildAttrib("U4", i->second->getIdentity().getUdpPort());
}
xml.stepOut();
string nodesXML;
StringRefOutputStream sos(nodesXML);
//sos.write(SimpleXML::utf8Header);
xml.toXML(&sos);
cmd.addParam(Utils::compressXML(nodesXML));
send(cmd, node->getIdentity().getIp(), node->getIdentity().getUdpPort(), node->getUser()->getCID(), node->getUdpKey());
}
}
void DHT::handle(AdcCommand::SND, const Node::Ptr& node, AdcCommand& c) throw()
{
if(c.getParam(1) == "nodes" && c.getParam(2) == "dht.xml")
{
// add node to our routing table
if(node->isIpVerified())
addNode(node, false);
try
{
SimpleXML xml;
xml.fromXML(c.getParam(3));
xml.stepIn();
// extract bootstrap nodes
unsigned int n = 20;
while(xml.findChild("Node") && n-- > 0)
{
CID cid = CID(xml.getChildAttrib("CID"));
if(!cid)
continue;
// don't bother with myself
if(ClientManager::getInstance()->getMe()->getCID() == cid)
continue;
const string i4 = xml.getChildAttrib("I4");
const string u4 = Util::toString(xml.getIntChildAttrib("U4"));
// don't bother with private IPs
if(!Utils::isGoodIPPort(i4, u4))
continue;
// create verified node, it's not big risk here and allows faster bootstrapping
// if this node already exists in our routing table, don't update it's ip/port for security reasons
Node::Ptr node = DHT::getInstance()->createNode(cid, i4, u4, false, true);
DHT::getInstance()->addNode(node, false);
}
xml.stepOut();
}
catch(const SimpleXMLException&)
{
// malformed node list
}
}
}
}
|