DHTMessageReceiver.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "DHTMessageReceiver.h"
  36. #include "DHTMessageTracker.h"
  37. #include "DHTConnection.h"
  38. #include "DHTMessage.h"
  39. #include "DHTMessageFactory.h"
  40. #include "DHTRoutingTable.h"
  41. #include "DHTNode.h"
  42. #include "DHTMessageCallback.h"
  43. #include "Dictionary.h"
  44. #include "Data.h"
  45. #include "MetaFileUtil.h"
  46. #include "DlAbortEx.h"
  47. #include "LogFactory.h"
  48. #include "Logger.h"
  49. #include "Util.h"
  50. #include <utility>
  51. namespace aria2 {
  52. DHTMessageReceiver::DHTMessageReceiver(const SharedHandle<DHTMessageTracker>& tracker):
  53. _tracker(tracker),
  54. _logger(LogFactory::getInstance())
  55. {}
  56. DHTMessageReceiver::~DHTMessageReceiver() {}
  57. SharedHandle<DHTMessage> DHTMessageReceiver::receiveMessage()
  58. {
  59. std::string remoteAddr;
  60. uint16_t remotePort;
  61. unsigned char data[64*1024];
  62. ssize_t length = _connection->receiveMessage(data, sizeof(data),
  63. remoteAddr,
  64. remotePort);
  65. if(length <= 0) {
  66. return SharedHandle<DHTMessage>();
  67. }
  68. try {
  69. bool isReply = false;
  70. MetaEntryHandle msgroot(MetaFileUtil::bdecoding(data, length));
  71. const Dictionary* d = dynamic_cast<const Dictionary*>(msgroot.get());
  72. if(d) {
  73. const Data* y = dynamic_cast<const Data*>(d->get("y"));
  74. if(y) {
  75. if(y->toString() == "r" || y->toString() == "e") {
  76. isReply = true;
  77. }
  78. } else {
  79. _logger->info("Malformed DHT message. Missing 'y' key. From:%s:%u", remoteAddr.c_str(), remotePort);
  80. return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
  81. }
  82. } else {
  83. _logger->info("Malformed DHT message. This is not a bencoded directory. From:%s:%u", remoteAddr.c_str(), remotePort);
  84. return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
  85. }
  86. SharedHandle<DHTMessage> message;
  87. SharedHandle<DHTMessageCallback> callback;
  88. if(isReply) {
  89. std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p = _tracker->messageArrived(d, remoteAddr, remotePort);
  90. message = p.first;
  91. callback = p.second;
  92. if(message.isNull()) {
  93. // timeout or malicious? message
  94. return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
  95. }
  96. } else {
  97. message = _factory->createQueryMessage(d, remoteAddr, remotePort);
  98. }
  99. _logger->info("Message received: %s", message->toString().c_str());
  100. message->validate();
  101. message->doReceivedAction();
  102. message->getRemoteNode()->markGood();
  103. message->getRemoteNode()->updateLastContact();
  104. _routingTable->addGoodNode(message->getRemoteNode());
  105. if(!callback.isNull()) {
  106. callback->onReceived(message);
  107. }
  108. return message;
  109. } catch(RecoverableException& e) {
  110. _logger->info("Exception thrown while receiving DHT message.", e);
  111. return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
  112. }
  113. }
  114. void DHTMessageReceiver::handleTimeout()
  115. {
  116. _tracker->handleTimeout();
  117. }
  118. SharedHandle<DHTMessage>
  119. DHTMessageReceiver::handleUnknownMessage(const unsigned char* data,
  120. size_t length,
  121. const std::string& remoteAddr,
  122. uint16_t remotePort)
  123. {
  124. SharedHandle<DHTMessage> m =
  125. _factory->createUnknownMessage(data, length, remoteAddr, remotePort);
  126. _logger->info("Message received: %s", m->toString().c_str());
  127. return m;
  128. }
  129. SharedHandle<DHTConnection> DHTMessageReceiver::getConnection() const
  130. {
  131. return _connection;
  132. }
  133. SharedHandle<DHTMessageTracker> DHTMessageReceiver::getMessageTracker() const
  134. {
  135. return _tracker;
  136. }
  137. void DHTMessageReceiver::setConnection(const SharedHandle<DHTConnection>& connection)
  138. {
  139. _connection = connection;
  140. }
  141. void DHTMessageReceiver::setMessageFactory(const SharedHandle<DHTMessageFactory>& factory)
  142. {
  143. _factory = factory;
  144. }
  145. void DHTMessageReceiver::setRoutingTable(const SharedHandle<DHTRoutingTable>& routingTable)
  146. {
  147. _routingTable = routingTable;
  148. }
  149. } // namespace aria2