bittorrent_helper.h 8.6 KB

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