CompactPeerListProcessor.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 "Data.h"
  36. #include <netinet/in.h>
  37. bool CompactPeerListProcessor::canHandle(const MetaEntry* peersEntry) const {
  38. return dynamic_cast<const Data*>(peersEntry) != 0;
  39. }
  40. Peers CompactPeerListProcessor::extractPeer(const MetaEntry* peersEntry) {
  41. Peers peers;
  42. const Data* peersData = (const Data*)peersEntry;
  43. if(peersData->getLen() > 0) {
  44. for(int i = 0; i < peersData->getLen(); i += 6) {
  45. unsigned int ipaddr1 = (unsigned char)*(peersData->getData()+i);
  46. unsigned int ipaddr2 = (unsigned char)*(peersData->getData()+i+1);
  47. unsigned int ipaddr3 = (unsigned char)*(peersData->getData()+i+2);
  48. unsigned int ipaddr4 = (unsigned char)*(peersData->getData()+i+3);
  49. unsigned int port = ntohs(*(unsigned short int*)(peersData->getData()+i+4));
  50. char ipaddr[16];
  51. snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d",
  52. ipaddr1, ipaddr2, ipaddr3, ipaddr4);
  53. PeerHandle peer =
  54. PeerHandle(new Peer(ipaddr, port, pieceLength, totalLength));
  55. peers.push_back(peer);
  56. }
  57. }
  58. return peers;
  59. }