LpdMessageReceiver.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. namespace aria2 {
  43. LpdMessageReceiver::LpdMessageReceiver
  44. (const std::string& multicastAddress, uint16_t multicastPort):
  45. multicastAddress_(multicastAddress),
  46. multicastPort_(multicastPort),
  47. logger_(LogFactory::getInstance()) {}
  48. bool LpdMessageReceiver::init(const std::string& localAddr)
  49. {
  50. try {
  51. socket_.reset(new SocketCore(SOCK_DGRAM));
  52. #ifdef __MINGW32__
  53. // Binding multicast address fails under Windows.
  54. socket_->bindWithFamily(multicastPort_, AF_INET);
  55. #else // !__MINGW32__
  56. socket_->bind(multicastAddress_, multicastPort_, AF_INET);
  57. #endif // !__MINGW32__
  58. if(logger_->debug()) {
  59. logger_->debug("Joining multicast group. %s:%u, localAddr=%s",
  60. multicastAddress_.c_str(), multicastPort_,
  61. localAddr.c_str());
  62. }
  63. socket_->joinMulticastGroup(multicastAddress_, multicastPort_, localAddr);
  64. socket_->setNonBlockingMode();
  65. localAddress_ = localAddr;
  66. logger_->info("Listening multicast group (%s:%u) packet",
  67. multicastAddress_.c_str(), multicastPort_);
  68. return true;
  69. } catch(RecoverableException& e) {
  70. logger_->error("Failed to initialize LPD message receiver.", e);
  71. }
  72. return false;
  73. }
  74. SharedHandle<LpdMessage> LpdMessageReceiver::receiveMessage()
  75. {
  76. SharedHandle<LpdMessage> msg;
  77. try {
  78. unsigned char buf[200];
  79. std::pair<std::string, uint16_t> peerAddr;
  80. ssize_t length = socket_->readDataFrom(buf, sizeof(buf), peerAddr);
  81. if(length == 0) {
  82. return msg;
  83. }
  84. HttpHeaderProcessor proc;
  85. proc.update(buf, length);
  86. if(!proc.eoh()) {
  87. msg.reset(new LpdMessage());
  88. return msg;
  89. }
  90. SharedHandle<HttpHeader> header = proc.getHttpRequestHeader();
  91. std::string infoHashString = header->getFirst("Infohash");
  92. uint16_t port = header->getFirstAsUInt("Port");
  93. logger_->info("LPD message received infohash=%s, port=%u from %s",
  94. infoHashString.c_str(), port, peerAddr.first.c_str());
  95. std::string infoHash;
  96. if(infoHashString.size() != 40 ||
  97. (infoHash = util::fromHex(infoHashString)).empty() ||
  98. port == 0) {
  99. logger_->info("LPD bad request. infohash=%s", infoHashString.c_str());
  100. msg.reset(new LpdMessage());
  101. return msg;
  102. }
  103. SharedHandle<Peer> peer(new Peer(peerAddr.first, port, false));
  104. if(util::inPrivateAddress(peerAddr.first)) {
  105. peer->setLocalPeer(true);
  106. }
  107. msg.reset(new LpdMessage(peer, infoHash));
  108. return msg;
  109. } catch(RecoverableException& e) {
  110. logger_->info("Failed to receive LPD message.", e);
  111. msg.reset(new LpdMessage());
  112. return msg;
  113. }
  114. }
  115. } // namespace aria2