bittorrent_helper.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 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_BITTORRENT_HELPER_H_
  36. #define _D_BITTORRENT_HELPER_H_
  37. #include "common.h"
  38. #include <string>
  39. #include <vector>
  40. #include <deque>
  41. #include <utility>
  42. #include "SharedHandle.h"
  43. #include "AnnounceTier.h"
  44. #include "BDE.h"
  45. #include "util.h"
  46. namespace aria2 {
  47. class DownloadContext;
  48. class Randomizer;
  49. namespace bittorrent {
  50. extern const std::string INFO_HASH;
  51. extern const std::string MODE;
  52. extern const std::string PRIVATE;
  53. extern const std::string ANNOUNCE_LIST;
  54. extern const std::string NODES;
  55. extern const std::string HOSTNAME;
  56. extern const std::string PORT;
  57. extern const std::string NAME;
  58. extern const std::string URL_LIST;
  59. extern const std::string CREATION_DATE;
  60. extern const std::string COMMENT;
  61. extern const std::string CREATED_BY;
  62. extern const std::string SINGLE;
  63. extern const std::string MULTI;
  64. extern const std::string BITTORRENT;
  65. extern const std::string METADATA_SIZE;
  66. extern const std::string METADATA;
  67. void load(const std::string& torrentFile,
  68. const SharedHandle<DownloadContext>& ctx,
  69. const std::string& overrideName = "");
  70. void load(const std::string& torrentFile,
  71. const SharedHandle<DownloadContext>& ctx,
  72. const std::deque<std::string>& uris,
  73. const std::string& overrideName = "");
  74. void loadFromMemory(const unsigned char* content, size_t length,
  75. const SharedHandle<DownloadContext>& ctx,
  76. const std::string& defaultName,
  77. const std::string& overrideName = "");
  78. void loadFromMemory(const unsigned char* content, size_t length,
  79. const SharedHandle<DownloadContext>& ctx,
  80. const std::deque<std::string>& uris,
  81. const std::string& defaultName,
  82. const std::string& overrideName = "");
  83. void loadFromMemory(const std::string& context,
  84. const SharedHandle<DownloadContext>& ctx,
  85. const std::string& defaultName,
  86. const std::string& overrideName = "");
  87. void loadFromMemory(const std::string& context,
  88. const SharedHandle<DownloadContext>& ctx,
  89. const std::deque<std::string>& uris,
  90. const std::string& defaultName,
  91. const std::string& overrideName = "");
  92. // Parses BitTorrent Magnet URI and returns BDE::dict() which includes
  93. // infoHash, name and announceList. If parsing operation failed, an
  94. // RecoverableException will be thrown. infoHash and name are string
  95. // and announceList is a list of list of announce URI.
  96. //
  97. // magnet:?xt=urn:btih:<info-hash>&dn=<name>&tr=<tracker-url>
  98. // <info-hash> comes in 2 flavors: 40bytes hexadecimal ascii string,
  99. // or 32bytes Base32 encoded string.
  100. BDE parseMagnet(const std::string& magnet);
  101. // Parses BitTorrent Magnet URI and set them in ctx as a
  102. // bittorrent::BITTORRENT attibute. If parsing operation failed, an
  103. // RecoverableException will be thrown.
  104. void loadMagnet
  105. (const std::string& magnet, const SharedHandle<DownloadContext>& ctx);
  106. // Generates Peer ID. BitTorrent specification says Peer ID is 20-byte
  107. // length. This function uses peerIdPrefix as a Peer ID and it is
  108. // less than 20bytes, random bytes are generated and appened to it. If
  109. // peerIdPrefix is larger than 20bytes, first 20bytes are used.
  110. std::string generatePeerId(const std::string& peerIdPrefix);
  111. // Generates Peer ID and stores it in static variable. This function
  112. // uses generatePeerId(peerIdPrefix) to produce Peer ID.
  113. const std::string& generateStaticPeerId(const std::string& peerIdPrefix);
  114. // Returns Peer ID statically stored by generateStaticPeerId(). If
  115. // Peer ID is not stored yet, this function calls
  116. // generateStaticPeerId("aria2-")
  117. const unsigned char* getStaticPeerId();
  118. // Set newPeerId as a static Peer ID. newPeerId must be 20-byte
  119. // length.
  120. void setStaticPeerId(const std::string& newPeerId);
  121. // Computes fast set index and stores them in fastset.
  122. void computeFastSet
  123. (std::vector<size_t>& fastSet, const std::string& ipaddr,
  124. size_t numPieces, const unsigned char* infoHash, size_t fastSetSize);
  125. // Writes the detailed information about torrent loaded in dctx.
  126. void print(std::ostream& o, const SharedHandle<DownloadContext>& dctx);
  127. // Returns the value associated with INFO_HASH key in BITTORRENT
  128. // attribute.
  129. const unsigned char*
  130. getInfoHash(const SharedHandle<DownloadContext>& downloadContext);
  131. std::string
  132. getInfoHashString(const SharedHandle<DownloadContext>& downloadContext);
  133. // Returns 4bytes unsigned integer located at offset pos. The integer
  134. // in msg is network byte order. This function converts it into host
  135. // byte order and returns it.
  136. uint32_t getIntParam(const unsigned char* msg, size_t pos);
  137. // Returns 2bytes unsigned integer located at offset pos. The integer
  138. // in msg is network byte order. This function converts it into host
  139. // byte order and returns it.
  140. uint16_t getShortIntParam(const unsigned char* msg, size_t pos);
  141. // Put param at location pointed by dest. param is converted into
  142. // network byte order.
  143. void setIntParam(unsigned char* dest, uint32_t param);
  144. // Put param at location pointed by dest. param is converted into
  145. // network byte order.
  146. void setShortIntParam(unsigned char* dest, uint16_t param);
  147. // Returns message ID located at first byte:msg[0]
  148. uint8_t getId(const unsigned char* msg);
  149. void checkIndex(size_t index, size_t pieces);
  150. void checkBegin(uint32_t begin, size_t pieceLength);
  151. void checkLength(size_t length);
  152. void checkRange(uint32_t begin, size_t length, size_t pieceLength);
  153. void checkBitfield
  154. (const unsigned char* bitfield, size_t bitfieldLength, size_t pieces);
  155. // Initialize msg with 0 and set payloadLength and messageId.
  156. void createPeerMessageString
  157. (unsigned char* msg, size_t msgLength, size_t payloadLength, uint8_t messageId);
  158. /**
  159. * Creates compact tracker format(6bytes for ipv4 address and port)
  160. * and stores the results in compact.
  161. * compact must be at least 6 bytes and pre-allocated.
  162. * Returns true if creation is successful, otherwise returns false.
  163. * The example of failure reason is that addr is not numbers-and-dots
  164. * notation.
  165. */
  166. bool createcompact
  167. (unsigned char* compact, const std::string& addr, uint16_t port);
  168. // Unpack compact into pair of IPv4 address and port.
  169. std::pair<std::string, uint16_t> unpackcompact(const unsigned char* compact);
  170. // Throws exception if threshold >= actual
  171. void assertPayloadLengthGreater
  172. (size_t threshold, size_t actual, const std::string& msgName);
  173. // Throws exception if expected != actual
  174. void assertPayloadLengthEqual
  175. (size_t expected, size_t actual, const std::string& msgName);
  176. // Throws exception if expected is not equal to id from data.
  177. void assertID
  178. (uint8_t expected, const unsigned char* data, const std::string& msgName);
  179. // Converts attrs into torrent data. attrs must be a BDE::dict. This
  180. // function does not guarantee the returned string is valid torrent
  181. // data.
  182. std::string metadata2Torrent(const std::string& metadata, const BDE& attrs);
  183. // Constructs BitTorrent Magnet URI using attrs. attrs must be a
  184. // BDE::dict.
  185. std::string torrent2Magnet(const BDE& attrs);
  186. } // namespace bittorrent
  187. } // namespace aria2
  188. #endif // _D_BITTORRENT_HELPER_H_