CompactPeerListProcessor.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * aria2 - The high speed download utility
  3. *
  4. * Copyright (C) 2006 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. /* copyright --> */
  34. #include "CompactPeerListProcessor.h"
  35. #include "a2netcompat.h"
  36. #include "bencode.h"
  37. #include "Peer.h"
  38. namespace aria2 {
  39. CompactPeerListProcessor::CompactPeerListProcessor() {}
  40. CompactPeerListProcessor::~CompactPeerListProcessor() {}
  41. bool CompactPeerListProcessor::canHandle(const bencode::BDE& peerData) const
  42. {
  43. return peerData.isString();
  44. }
  45. void CompactPeerListProcessor::extractPeer
  46. (std::deque<SharedHandle<Peer> >& peers, const bencode::BDE& peerData)
  47. {
  48. if(!canHandle(peerData)) {
  49. return;
  50. }
  51. size_t length = peerData.s().size();
  52. if(length%6 == 0) {
  53. for(size_t i = 0; i < length; i += 6) {
  54. struct in_addr in;
  55. in.s_addr = *(uint32_t*)(peerData.s().c_str()+i);
  56. std::string ipaddr = inet_ntoa(in);
  57. uint16_t port = ntohs(*(uint16_t*)(peerData.s().c_str()+i+4));
  58. PeerHandle peer(new Peer(ipaddr, port));
  59. peers.push_back(peer);
  60. }
  61. }
  62. }
  63. } // namespace aria2