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(const std::string& multicastAddress,
  46. uint16_t multicastPort)
  47. : multicastAddress_(multicastAddress), multicastPort_(multicastPort)
  48. {
  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. }
  71. catch (RecoverableException& e) {
  72. A2_LOG_ERROR_EX("Failed to initialize LPD message receiver.", e);
  73. }
  74. return false;
  75. }
  76. std::unique_ptr<LpdMessage> LpdMessageReceiver::receiveMessage()
  77. {
  78. while (1) {
  79. unsigned char buf[200];
  80. Endpoint remoteEndpoint;
  81. ssize_t length;
  82. try {
  83. length = socket_->readDataFrom(buf, sizeof(buf), remoteEndpoint);
  84. if (length == 0) {
  85. return nullptr;
  86. }
  87. }
  88. catch (RecoverableException& e) {
  89. A2_LOG_INFO_EX("Failed to receive LPD message.", e);
  90. return nullptr;
  91. }
  92. HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  93. try {
  94. if (!proc.parse(buf, length)) {
  95. // UDP packet must contain whole HTTP header block.
  96. continue;
  97. }
  98. }
  99. catch (RecoverableException& e) {
  100. A2_LOG_INFO_EX("Failed to parse LPD message.", e);
  101. continue;
  102. }
  103. auto header = proc.getResult();
  104. const std::string& infoHashString = header->find(HttpHeader::INFOHASH);
  105. uint32_t port = 0;
  106. if (!util::parseUIntNoThrow(port, header->find(HttpHeader::PORT)) ||
  107. port > UINT16_MAX || port == 0) {
  108. A2_LOG_INFO(fmt("Bad LPD port=%u", port));
  109. continue;
  110. }
  111. A2_LOG_INFO(fmt("LPD message received infohash=%s, port=%u from %s",
  112. infoHashString.c_str(), port, remoteEndpoint.addr.c_str()));
  113. std::string infoHash;
  114. if (infoHashString.size() != 40 ||
  115. (infoHash = util::fromHex(infoHashString.begin(), infoHashString.end()))
  116. .empty()) {
  117. A2_LOG_INFO(fmt("LPD bad request. infohash=%s", infoHashString.c_str()));
  118. continue;
  119. }
  120. auto peer = std::make_shared<Peer>(remoteEndpoint.addr, port, false);
  121. if (util::inPrivateAddress(remoteEndpoint.addr)) {
  122. peer->setLocalPeer(true);
  123. }
  124. return make_unique<LpdMessage>(peer, infoHash);
  125. }
  126. }
  127. } // namespace aria2