DHTMessageReceiver.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. _connection(0),
  55. _factory(0),
  56. _routingTable(0),
  57. _logger(LogFactory::getInstance())
  58. {}
  59. DHTMessageReceiver::~DHTMessageReceiver() {}
  60. SharedHandle<DHTMessage> DHTMessageReceiver::receiveMessage()
  61. {
  62. std::string remoteAddr;
  63. uint16_t remotePort;
  64. unsigned char data[64*1024];
  65. ssize_t length = _connection->receiveMessage(data, sizeof(data),
  66. remoteAddr,
  67. remotePort);
  68. if(length <= 0) {
  69. return 0;
  70. }
  71. try {
  72. bool isReply = false;
  73. MetaEntryHandle msgroot = MetaFileUtil::bdecoding(data, length);
  74. const Dictionary* d = dynamic_cast<const Dictionary*>(msgroot.get());
  75. if(d) {
  76. const Data* y = dynamic_cast<const Data*>(d->get("y"));
  77. if(y) {
  78. if(y->toString() == "r" || y->toString() == "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 = 0;
  90. SharedHandle<DHTMessageCallback> callback = 0;
  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. delete e;
  115. return handleUnknownMessage(data, sizeof(data), remoteAddr, remotePort);
  116. }
  117. }
  118. void DHTMessageReceiver::handleTimeout()
  119. {
  120. _tracker->handleTimeout();
  121. }
  122. SharedHandle<DHTMessage>
  123. DHTMessageReceiver::handleUnknownMessage(const unsigned char* data,
  124. size_t length,
  125. const std::string& remoteAddr,
  126. uint16_t remotePort)
  127. {
  128. SharedHandle<DHTMessage> m =
  129. _factory->createUnknownMessage(data, length, remoteAddr, remotePort);
  130. _logger->info("Message received: %s", m->toString().c_str());
  131. return m;
  132. }
  133. SharedHandle<DHTConnection> DHTMessageReceiver::getConnection() const
  134. {
  135. return _connection;
  136. }
  137. SharedHandle<DHTMessageTracker> DHTMessageReceiver::getMessageTracker() const
  138. {
  139. return _tracker;
  140. }
  141. void DHTMessageReceiver::setConnection(const SharedHandle<DHTConnection>& connection)
  142. {
  143. _connection = connection;
  144. }
  145. void DHTMessageReceiver::setMessageFactory(const SharedHandle<DHTMessageFactory>& factory)
  146. {
  147. _factory = factory;
  148. }
  149. void DHTMessageReceiver::setRoutingTable(const SharedHandle<DHTRoutingTable>& routingTable)
  150. {
  151. _routingTable = routingTable;
  152. }
  153. } // namespace aria2