PeerListProcessor.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef _D_PEER_LIST_PROCESSOR_H_
  36. #define _D_PEER_LIST_PROCESSOR_H_
  37. #include "common.h"
  38. #include <cstring>
  39. #include "a2netcompat.h"
  40. #include "bencode.h"
  41. #include "Peer.h"
  42. #include "ValueBase.h"
  43. namespace aria2 {
  44. class PeerListProcessor {
  45. private:
  46. template<typename OutputIterator>
  47. class PeerListValueBaseVisitor:public ValueBaseVisitor {
  48. OutputIterator dest_;
  49. public:
  50. PeerListValueBaseVisitor(OutputIterator dest):dest_(dest) {}
  51. virtual void visit(const String& peerData)
  52. {
  53. size_t length = peerData.s().size();
  54. if(length%6 == 0) {
  55. const char* base = peerData.s().data();
  56. for(size_t i = 0; i < length; i += 6) {
  57. struct in_addr in;
  58. memcpy(&in.s_addr, base+i, sizeof(uint32_t));
  59. std::string ipaddr = inet_ntoa(in);
  60. uint16_t port_nworder;
  61. memcpy(&port_nworder, base+i+4, sizeof(uint16_t));
  62. uint16_t port = ntohs(port_nworder);
  63. *dest_ = SharedHandle<Peer>(new Peer(ipaddr, port));
  64. ++dest_;
  65. }
  66. }
  67. }
  68. virtual void visit(const Integer& v) {}
  69. virtual void visit(const List& peerData)
  70. {
  71. for(List::ValueType::const_iterator itr = peerData.begin(),
  72. eoi = peerData.end(); itr != eoi; ++itr) {
  73. const Dict* peerDict = asDict(*itr);
  74. if(!peerDict) {
  75. continue;
  76. }
  77. static const std::string IP = "ip";
  78. static const std::string PORT = "port";
  79. const String* ip = asString(peerDict->get(IP));
  80. const Integer* port = asInteger(peerDict->get(PORT));
  81. if(!ip || !port || !(0 < port->i() && port->i() < 65536)) {
  82. continue;
  83. }
  84. *dest_ = SharedHandle<Peer>(new Peer(ip->s(), port->i()));
  85. ++dest_;
  86. }
  87. }
  88. virtual void visit(const Dict& v) {}
  89. };
  90. public:
  91. template<typename OutputIterator>
  92. void extractPeer(const SharedHandle<ValueBase>& peerData, OutputIterator dest)
  93. {
  94. if(!peerData.isNull()) {
  95. PeerListValueBaseVisitor<OutputIterator> visitor(dest);
  96. peerData->accept(visitor);
  97. }
  98. }
  99. template<typename OutputIterator>
  100. void extractPeer(const BDE& peerData, OutputIterator dest)
  101. {
  102. if(peerData.isList()) {
  103. extractPeerFromList(peerData, dest);
  104. } else if(peerData.isString()) {
  105. extractPeerFromCompact(peerData, dest);
  106. }
  107. }
  108. template<typename OutputIterator>
  109. void extractPeerFromList(const BDE& peerData, OutputIterator dest)
  110. {
  111. for(BDE::List::const_iterator itr = peerData.listBegin(),
  112. eoi = peerData.listEnd(); itr != eoi; ++itr) {
  113. const BDE& peerDict = *itr;
  114. if(!peerDict.isDict()) {
  115. continue;
  116. }
  117. static const std::string IP = "ip";
  118. static const std::string PORT = "port";
  119. const BDE& ip = peerDict[IP];
  120. const BDE& port = peerDict[PORT];
  121. if(!ip.isString() || !port.isInteger() ||
  122. !(0 < port.i() && port.i() < 65536)) {
  123. continue;
  124. }
  125. *dest = SharedHandle<Peer>(new Peer(ip.s(), port.i()));
  126. ++dest;
  127. }
  128. }
  129. template<typename OutputIterator>
  130. void extractPeerFromCompact(const BDE& peerData, OutputIterator dest)
  131. {
  132. size_t length = peerData.s().size();
  133. if(length%6 == 0) {
  134. for(size_t i = 0; i < length; i += 6) {
  135. struct in_addr in;
  136. memcpy(&in.s_addr, peerData.s().c_str()+i, sizeof(uint32_t));
  137. std::string ipaddr = inet_ntoa(in);
  138. uint16_t port_nworder;
  139. memcpy(&port_nworder, peerData.s().c_str()+i+4, sizeof(uint16_t));
  140. uint16_t port = ntohs(port_nworder);
  141. *dest = SharedHandle<Peer>(new Peer(ipaddr, port));
  142. ++dest;
  143. }
  144. }
  145. }
  146. };
  147. } // namespace aria2
  148. #endif // _D_PEER_LIST_PROCESSOR_H_