LpdMessageReceiver.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * aria2 - The high speed download utility
  3. *
  4. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * In addition, as a special exception, the copyright holders give
  21. * permission to link the code of portions of this program with the
  22. * OpenSSL library under certain conditions as described in each
  23. * individual source file, and distribute linked combinations
  24. * including the two.
  25. * You must obey the GNU General Public License in all respects
  26. * for all of the code used other than OpenSSL. If you modify
  27. * file(s) with this exception, you may extend this exception to your
  28. * version of the file(s), but you are not obligated to do so. If you
  29. * do not wish to do so, delete this exception statement from your
  30. * version. If you delete this exception statement from all source
  31. * files in the program, then also delete it here.
  32. */
  33. #include "LpdMessageReceiver.h"
  34. #include "SocketCore.h"
  35. #include "Logger.h"
  36. #include "LogFactory.h"
  37. #include "HttpHeaderProcessor.h"
  38. #include "HttpHeader.h"
  39. #include "util.h"
  40. #include "LpdMessage.h"
  41. #include "RecoverableException.h"
  42. #include "Peer.h"
  43. #include "fmt.h"
  44. namespace aria2 {
  45. LpdMessageReceiver::LpdMessageReceiver
  46. (const std::string& multicastAddress, uint16_t multicastPort)
  47. : multicastAddress_(multicastAddress),
  48. multicastPort_(multicastPort)
  49. {}
  50. LpdMessageReceiver::~LpdMessageReceiver() {}
  51. bool LpdMessageReceiver::init(const std::string& localAddr)
  52. {
  53. try {
  54. socket_ = std::make_shared<SocketCore>(SOCK_DGRAM);
  55. #ifdef __MINGW32__
  56. // Binding multicast address fails under Windows.
  57. socket_->bindWithFamily(multicastPort_, AF_INET);
  58. #else // !__MINGW32__
  59. socket_->bind(multicastAddress_.c_str(), multicastPort_, AF_INET);
  60. #endif // !__MINGW32__
  61. A2_LOG_DEBUG(fmt("Joining multicast group. %s:%u, localAddr=%s",
  62. multicastAddress_.c_str(), multicastPort_,
  63. localAddr.c_str()));
  64. socket_->joinMulticastGroup(multicastAddress_, multicastPort_, localAddr);
  65. socket_->setNonBlockingMode();
  66. localAddress_ = localAddr;
  67. A2_LOG_INFO(fmt("Listening multicast group (%s:%u) packet",
  68. multicastAddress_.c_str(), multicastPort_));
  69. return true;
  70. } catch(RecoverableException& e) {
  71. A2_LOG_ERROR_EX("Failed to initialize LPD message receiver.", e);
  72. }
  73. return false;
  74. }
  75. std::unique_ptr<LpdMessage> LpdMessageReceiver::receiveMessage()
  76. {
  77. while(1) {
  78. unsigned char buf[200];
  79. std::pair<std::string, uint16_t> peerAddr;
  80. ssize_t length;
  81. try {
  82. length = socket_->readDataFrom(buf, sizeof(buf), peerAddr);
  83. if(length == 0) {
  84. return nullptr;
  85. }
  86. } catch(RecoverableException& e) {
  87. A2_LOG_INFO_EX("Failed to receive LPD message.", e);
  88. return nullptr;
  89. }
  90. HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  91. try {
  92. if(!proc.parse(buf, length)) {
  93. // UDP packet must contain whole HTTP header block.
  94. continue;
  95. }
  96. } catch(RecoverableException& e) {
  97. A2_LOG_INFO_EX("Failed to parse LPD message.", e);
  98. continue;
  99. }
  100. auto header = proc.getResult();
  101. const std::string& infoHashString = header->find(HttpHeader::INFOHASH);
  102. uint32_t port = 0;
  103. if(!util::parseUIntNoThrow(port, header->find(HttpHeader::PORT)) ||
  104. port > UINT16_MAX || port == 0) {
  105. A2_LOG_INFO(fmt("Bad LPD port=%u", port));
  106. continue;
  107. }
  108. A2_LOG_INFO(fmt("LPD message received infohash=%s, port=%u from %s",
  109. infoHashString.c_str(),
  110. port,
  111. peerAddr.first.c_str()));
  112. std::string infoHash;
  113. if(infoHashString.size() != 40 ||
  114. (infoHash = util::fromHex(infoHashString.begin(),
  115. infoHashString.end())).empty()) {
  116. A2_LOG_INFO(fmt("LPD bad request. infohash=%s",
  117. infoHashString.c_str()));
  118. continue;
  119. }
  120. auto peer = std::make_shared<Peer>(peerAddr.first, port, false);
  121. if(util::inPrivateAddress(peerAddr.first)) {
  122. peer->setLocalPeer(true);
  123. }
  124. return make_unique<LpdMessage>(peer, infoHash);
  125. }
  126. }
  127. } // namespace aria2