LpdMessageReceiver.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_.reset(new 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_, 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. SharedHandle<LpdMessage> LpdMessageReceiver::receiveMessage()
  76. {
  77. SharedHandle<LpdMessage> msg;
  78. try {
  79. unsigned char buf[200];
  80. std::pair<std::string, uint16_t> peerAddr;
  81. ssize_t length = socket_->readDataFrom(buf, sizeof(buf), peerAddr);
  82. if(length == 0) {
  83. return msg;
  84. }
  85. HttpHeaderProcessor proc;
  86. proc.update(buf, length);
  87. if(!proc.eoh()) {
  88. msg.reset(new LpdMessage());
  89. return msg;
  90. }
  91. SharedHandle<HttpHeader> header = proc.getHttpRequestHeader();
  92. std::string infoHashString = header->getFirst("Infohash");
  93. uint16_t port = header->getFirstAsUInt("Port");
  94. A2_LOG_INFO(fmt("LPD message received infohash=%s, port=%u from %s",
  95. infoHashString.c_str(),
  96. port,
  97. peerAddr.first.c_str()));
  98. std::string infoHash;
  99. if(infoHashString.size() != 40 ||
  100. (infoHash = util::fromHex(infoHashString)).empty() ||
  101. port == 0) {
  102. A2_LOG_INFO(fmt("LPD bad request. infohash=%s", infoHashString.c_str()));
  103. msg.reset(new LpdMessage());
  104. return msg;
  105. }
  106. SharedHandle<Peer> peer(new Peer(peerAddr.first, port, false));
  107. if(util::inPrivateAddress(peerAddr.first)) {
  108. peer->setLocalPeer(true);
  109. }
  110. msg.reset(new LpdMessage(peer, infoHash));
  111. return msg;
  112. } catch(RecoverableException& e) {
  113. A2_LOG_INFO_EX("Failed to receive LPD message.", e);
  114. msg.reset(new LpdMessage());
  115. return msg;
  116. }
  117. }
  118. } // namespace aria2