DHTMessageReceiver.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "DHTResponseMessage.h"
  40. #include "DHTUnknownMessage.h"
  41. #include "DHTMessageFactory.h"
  42. #include "DHTRoutingTable.h"
  43. #include "DHTNode.h"
  44. #include "DHTMessageCallback.h"
  45. #include "Dictionary.h"
  46. #include "Data.h"
  47. #include "MetaFileUtil.h"
  48. #include "DlAbortEx.h"
  49. #include "LogFactory.h"
  50. #include "Logger.h"
  51. #include "Util.h"
  52. #include <utility>
  53. namespace aria2 {
  54. DHTMessageReceiver::DHTMessageReceiver(const SharedHandle<DHTMessageTracker>& tracker):
  55. _tracker(tracker),
  56. _logger(LogFactory::getInstance())
  57. {}
  58. DHTMessageReceiver::~DHTMessageReceiver() {}
  59. SharedHandle<DHTMessage> DHTMessageReceiver::receiveMessage()
  60. {
  61. std::string remoteAddr;
  62. uint16_t remotePort;
  63. unsigned char data[64*1024];
  64. ssize_t length = _connection->receiveMessage(data, sizeof(data),
  65. remoteAddr,
  66. remotePort);
  67. if(length <= 0) {
  68. return SharedHandle<DHTMessage>();
  69. }
  70. try {
  71. bool isReply = false;
  72. MetaEntryHandle msgroot(MetaFileUtil::bdecoding(data, length));
  73. const Dictionary* d = dynamic_cast<const Dictionary*>(msgroot.get());
  74. if(d) {
  75. const Data* y = dynamic_cast<const Data*>(d->get(DHTMessage::Y));
  76. if(y) {
  77. if(y->toString() == DHTResponseMessage::R ||
  78. y->toString() == DHTUnknownMessage::E) {
  79. isReply = true;
  80. }
  81. } else {
  82. _logger->info("Malformed DHT message. Missing 'y' key. From:%s:%u", remoteAddr.c_str(), remotePort);
  83. return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
  84. }
  85. } else {
  86. _logger->info("Malformed DHT message. This is not a bencoded directory. From:%s:%u", remoteAddr.c_str(), remotePort);
  87. return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
  88. }
  89. SharedHandle<DHTMessage> message;
  90. SharedHandle<DHTMessageCallback> callback;
  91. if(isReply) {
  92. std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p = _tracker->messageArrived(d, remoteAddr, remotePort);
  93. message = p.first;
  94. callback = p.second;
  95. if(message.isNull()) {
  96. // timeout or malicious? message
  97. return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
  98. }
  99. } else {
  100. message = _factory->createQueryMessage(d, remoteAddr, remotePort);
  101. }
  102. _logger->info("Message received: %s", message->toString().c_str());
  103. message->validate();
  104. message->doReceivedAction();
  105. message->getRemoteNode()->markGood();
  106. message->getRemoteNode()->updateLastContact();
  107. _routingTable->addGoodNode(message->getRemoteNode());
  108. if(!callback.isNull()) {
  109. callback->onReceived(message);
  110. }
  111. return message;
  112. } catch(RecoverableException& e) {
  113. _logger->info("Exception thrown while receiving DHT message.", e);
  114. return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
  115. }
  116. }
  117. void DHTMessageReceiver::handleTimeout()
  118. {
  119. _tracker->handleTimeout();
  120. }
  121. SharedHandle<DHTMessage>
  122. DHTMessageReceiver::handleUnknownMessage(const unsigned char* data,
  123. size_t length,
  124. const std::string& remoteAddr,
  125. uint16_t remotePort)
  126. {
  127. SharedHandle<DHTMessage> m =
  128. _factory->createUnknownMessage(data, length, remoteAddr, remotePort);
  129. _logger->info("Message received: %s", m->toString().c_str());
  130. return m;
  131. }
  132. SharedHandle<DHTConnection> DHTMessageReceiver::getConnection() const
  133. {
  134. return _connection;
  135. }
  136. SharedHandle<DHTMessageTracker> DHTMessageReceiver::getMessageTracker() const
  137. {
  138. return _tracker;
  139. }
  140. void DHTMessageReceiver::setConnection(const SharedHandle<DHTConnection>& connection)
  141. {
  142. _connection = connection;
  143. }
  144. void DHTMessageReceiver::setMessageFactory(const SharedHandle<DHTMessageFactory>& factory)
  145. {
  146. _factory = factory;
  147. }
  148. void DHTMessageReceiver::setRoutingTable(const SharedHandle<DHTRoutingTable>& routingTable)
  149. {
  150. _routingTable = routingTable;
  151. }
  152. } // namespace aria2