librats 1.0.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (319) hide show
  1. package/README.md +145 -331
  2. package/binding.gyp +16 -3
  3. package/lib/index.d.ts +288 -696
  4. package/lib/index.js +407 -44
  5. package/native-src/3rdparty/android/ifaddrs-android.c +1 -0
  6. package/native-src/3rdparty/android/ifaddrs-android.h +1 -0
  7. package/native-src/CMakeLists.txt +404 -179
  8. package/native-src/LICENSE +1 -1
  9. package/native-src/src/librats/bindings/rats.cpp +762 -0
  10. package/native-src/src/librats/bindings/rats.h +380 -0
  11. package/native-src/src/librats/bittorrent/bencode.cpp +437 -0
  12. package/native-src/src/librats/bittorrent/bencode.h +176 -0
  13. package/native-src/src/librats/bittorrent/bitfield.cpp +97 -0
  14. package/native-src/src/librats/bittorrent/bitfield.h +76 -0
  15. package/native-src/src/librats/bittorrent/byte_io.h +58 -0
  16. package/native-src/src/librats/bittorrent/choker.cpp +25 -0
  17. package/native-src/src/librats/bittorrent/choker.h +46 -0
  18. package/native-src/src/librats/bittorrent/client.cpp +413 -0
  19. package/native-src/src/librats/bittorrent/client.h +227 -0
  20. package/native-src/src/librats/bittorrent/disk_io.cpp +209 -0
  21. package/native-src/src/librats/bittorrent/disk_io.h +150 -0
  22. package/native-src/src/librats/bittorrent/extensions.cpp +191 -0
  23. package/native-src/src/librats/bittorrent/extensions.h +94 -0
  24. package/native-src/src/librats/bittorrent/file_storage.cpp +77 -0
  25. package/native-src/src/librats/bittorrent/file_storage.h +79 -0
  26. package/native-src/src/librats/bittorrent/log.h +42 -0
  27. package/native-src/src/librats/bittorrent/magnet_uri.cpp +98 -0
  28. package/native-src/src/librats/bittorrent/magnet_uri.h +35 -0
  29. package/native-src/src/librats/bittorrent/peer_connection.cpp +502 -0
  30. package/native-src/src/librats/bittorrent/peer_connection.h +194 -0
  31. package/native-src/src/librats/bittorrent/peer_list.cpp +68 -0
  32. package/native-src/src/librats/bittorrent/peer_list.h +75 -0
  33. package/native-src/src/librats/bittorrent/piece_picker.cpp +352 -0
  34. package/native-src/src/librats/bittorrent/piece_picker.h +201 -0
  35. package/native-src/src/librats/bittorrent/reactor.cpp +97 -0
  36. package/native-src/src/librats/bittorrent/reactor.h +89 -0
  37. package/native-src/src/librats/bittorrent/resume_data.cpp +72 -0
  38. package/native-src/src/librats/bittorrent/resume_data.h +41 -0
  39. package/native-src/src/librats/bittorrent/store_buffer.cpp +48 -0
  40. package/native-src/src/librats/bittorrent/store_buffer.h +47 -0
  41. package/native-src/src/librats/bittorrent/torrent.cpp +870 -0
  42. package/native-src/src/librats/bittorrent/torrent.h +260 -0
  43. package/native-src/src/librats/bittorrent/torrent_creator.cpp +129 -0
  44. package/native-src/src/librats/bittorrent/torrent_creator.h +58 -0
  45. package/native-src/src/librats/bittorrent/torrent_info.cpp +314 -0
  46. package/native-src/src/librats/bittorrent/torrent_info.h +118 -0
  47. package/native-src/src/librats/bittorrent/tracker.cpp +374 -0
  48. package/native-src/src/librats/bittorrent/tracker.h +108 -0
  49. package/native-src/src/librats/bittorrent/types.cpp +206 -0
  50. package/native-src/src/librats/bittorrent/types.h +86 -0
  51. package/native-src/src/librats/core/address.cpp +35 -0
  52. package/native-src/src/librats/core/address.h +78 -0
  53. package/native-src/src/librats/core/bytes.h +69 -0
  54. package/native-src/src/librats/core/chained_send_buffer.cpp +172 -0
  55. package/native-src/src/librats/core/chained_send_buffer.h +183 -0
  56. package/native-src/src/librats/core/endpoint_parse.cpp +41 -0
  57. package/native-src/src/librats/core/endpoint_parse.h +31 -0
  58. package/native-src/src/librats/core/event_bus.h +70 -0
  59. package/native-src/src/librats/core/host_endpoint.h +56 -0
  60. package/native-src/src/{io_poller.cpp → librats/core/io_poller.cpp} +520 -65
  61. package/native-src/src/{io_poller.h → librats/core/io_poller.h} +12 -6
  62. package/native-src/src/librats/core/ip_address.cpp +120 -0
  63. package/native-src/src/librats/core/ip_address.h +109 -0
  64. package/native-src/src/librats/core/mpsc_queue.h +47 -0
  65. package/native-src/src/librats/core/notifier.h +74 -0
  66. package/native-src/src/librats/core/receive_buffer.cpp +219 -0
  67. package/native-src/src/librats/core/receive_buffer.h +171 -0
  68. package/native-src/src/librats/core/service_registry.h +58 -0
  69. package/native-src/src/{socket.cpp → librats/core/socket.cpp} +625 -118
  70. package/native-src/src/librats/core/socket.h +496 -0
  71. package/native-src/src/librats/core/timer_queue.h +105 -0
  72. package/native-src/src/librats/core/types.cpp +43 -0
  73. package/native-src/src/librats/core/types.h +103 -0
  74. package/native-src/src/librats/core/wakeup_pipe.h +83 -0
  75. package/native-src/src/{crypto → librats/crypto}/blake2_endian.h +21 -23
  76. package/native-src/src/{crypto → librats/crypto}/blake2b.c +34 -33
  77. package/native-src/src/{crypto → librats/crypto}/blake2b.h +7 -6
  78. package/native-src/src/{crypto → librats/crypto}/blake2s.c +55 -54
  79. package/native-src/src/{crypto → librats/crypto}/blake2s.h +13 -12
  80. package/native-src/src/{crypto → librats/crypto}/chacha.c +22 -21
  81. package/native-src/src/{crypto → librats/crypto}/chacha.h +14 -13
  82. package/native-src/src/{crypto → librats/crypto}/chachapoly.c +56 -56
  83. package/native-src/src/{crypto → librats/crypto}/chachapoly.h +24 -17
  84. package/native-src/src/{crc32.cpp → librats/crypto/crc32.cpp} +1 -1
  85. package/native-src/src/{crc32.h → librats/crypto/crc32.h} +3 -1
  86. package/native-src/src/{crypto → librats/crypto}/curve25519.c +6 -4
  87. package/native-src/src/{crypto → librats/crypto}/curve25519.h +6 -3
  88. package/native-src/src/librats/crypto/hkdf.c +266 -0
  89. package/native-src/src/{crypto → librats/crypto}/hkdf.h +19 -19
  90. package/native-src/src/{noise.cpp → librats/crypto/noise.cpp} +84 -73
  91. package/native-src/src/{noise.h → librats/crypto/noise.h} +18 -8
  92. package/native-src/src/{crypto → librats/crypto}/poly1305.c +47 -46
  93. package/native-src/src/librats/crypto/poly1305.h +37 -0
  94. package/native-src/src/{sha1.cpp → librats/crypto/sha1.cpp} +33 -1
  95. package/native-src/src/{sha1.h → librats/crypto/sha1.h} +14 -6
  96. package/native-src/src/{crypto → librats/crypto}/sha256.c +15 -14
  97. package/native-src/src/{crypto → librats/crypto}/sha256.h +8 -7
  98. package/native-src/src/{crypto → librats/crypto}/sha512.c +15 -14
  99. package/native-src/src/{crypto → librats/crypto}/sha512.h +8 -7
  100. package/native-src/src/librats/dht/announce.cpp +37 -0
  101. package/native-src/src/librats/dht/announce.h +41 -0
  102. package/native-src/src/librats/dht/bep42.cpp +109 -0
  103. package/native-src/src/librats/dht/bep42.h +48 -0
  104. package/native-src/src/librats/dht/dht.cpp +501 -0
  105. package/native-src/src/librats/dht/dht.h +119 -0
  106. package/native-src/src/librats/dht/dht_runner.cpp +103 -0
  107. package/native-src/src/librats/dht/dht_runner.h +71 -0
  108. package/native-src/src/librats/dht/dos_blocker.cpp +42 -0
  109. package/native-src/src/librats/dht/dos_blocker.h +47 -0
  110. package/native-src/src/librats/dht/find_peers.cpp +52 -0
  111. package/native-src/src/librats/dht/find_peers.h +73 -0
  112. package/native-src/src/librats/dht/id.h +167 -0
  113. package/native-src/src/{krpc.cpp → librats/dht/krpc.cpp} +32 -81
  114. package/native-src/src/{krpc.h → librats/dht/krpc.h} +19 -23
  115. package/native-src/src/librats/dht/log.h +38 -0
  116. package/native-src/src/librats/dht/node.cpp +473 -0
  117. package/native-src/src/librats/dht/node.h +164 -0
  118. package/native-src/src/librats/dht/node_entry.h +81 -0
  119. package/native-src/src/librats/dht/observer.h +72 -0
  120. package/native-src/src/librats/dht/persistence.cpp +90 -0
  121. package/native-src/src/librats/dht/persistence.h +32 -0
  122. package/native-src/src/librats/dht/routing_table.cpp +559 -0
  123. package/native-src/src/librats/dht/routing_table.h +185 -0
  124. package/native-src/src/librats/dht/rpc_manager.cpp +127 -0
  125. package/native-src/src/librats/dht/rpc_manager.h +77 -0
  126. package/native-src/src/librats/dht/storage.cpp +92 -0
  127. package/native-src/src/librats/dht/storage.h +74 -0
  128. package/native-src/src/librats/dht/transport.h +27 -0
  129. package/native-src/src/librats/dht/traversal.cpp +326 -0
  130. package/native-src/src/librats/dht/traversal.h +120 -0
  131. package/native-src/src/librats/dht/udp_transport.cpp +49 -0
  132. package/native-src/src/librats/dht/udp_transport.h +51 -0
  133. package/native-src/src/librats/mdns/log.h +22 -0
  134. package/native-src/src/{mdns.cpp → librats/mdns/mdns.cpp} +75 -40
  135. package/native-src/src/{mdns.h → librats/mdns/mdns.h} +9 -8
  136. package/native-src/src/{natpmp.cpp → librats/nat/natpmp.cpp} +12 -9
  137. package/native-src/src/{natpmp.h → librats/nat/natpmp.h} +3 -3
  138. package/native-src/src/{port_mapping.h → librats/nat/port_mapping.h} +3 -2
  139. package/native-src/src/{stun.cpp → librats/nat/stun.cpp} +4 -4
  140. package/native-src/src/{stun.h → librats/nat/stun.h} +1 -1
  141. package/native-src/src/{upnp.cpp → librats/nat/upnp.cpp} +6 -6
  142. package/native-src/src/{upnp.h → librats/nat/upnp.h} +2 -2
  143. package/native-src/src/librats/node/circuit_service.h +84 -0
  144. package/native-src/src/librats/node/config.h +110 -0
  145. package/native-src/src/librats/node/dial_service.h +54 -0
  146. package/native-src/src/librats/node/dialer.cpp +264 -0
  147. package/native-src/src/librats/node/dialer.h +188 -0
  148. package/native-src/src/librats/node/host_events.h +26 -0
  149. package/native-src/src/librats/node/identify.cpp +130 -0
  150. package/native-src/src/librats/node/identify.h +71 -0
  151. package/native-src/src/librats/node/nat_status.cpp +103 -0
  152. package/native-src/src/librats/node/nat_status.h +118 -0
  153. package/native-src/src/librats/node/node.cpp +865 -0
  154. package/native-src/src/librats/node/node.h +344 -0
  155. package/native-src/src/librats/node/node_context.h +33 -0
  156. package/native-src/src/librats/node/peer_network.h +91 -0
  157. package/native-src/src/librats/peer/peer.h +49 -0
  158. package/native-src/src/librats/peer/peer_book.cpp +181 -0
  159. package/native-src/src/librats/peer/peer_book.h +88 -0
  160. package/native-src/src/librats/peer/peer_id.cpp +72 -0
  161. package/native-src/src/librats/peer/peer_id.h +62 -0
  162. package/native-src/src/librats/peer/peer_info.h +37 -0
  163. package/native-src/src/librats/peer/peer_table.cpp +170 -0
  164. package/native-src/src/librats/peer/peer_table.h +148 -0
  165. package/native-src/src/librats/security/handshaker.h +66 -0
  166. package/native-src/src/librats/security/identity.h +43 -0
  167. package/native-src/src/librats/security/noise_security.cpp +122 -0
  168. package/native-src/src/librats/security/noise_security.h +37 -0
  169. package/native-src/src/librats/security/plaintext_security.h +106 -0
  170. package/native-src/src/librats/security/session.h +37 -0
  171. package/native-src/src/{storage.cpp → librats/storage/storage.cpp} +369 -522
  172. package/native-src/src/{storage.h → librats/storage/storage.h} +135 -299
  173. package/native-src/src/librats/subsystems/bittorrent.cpp +211 -0
  174. package/native-src/src/librats/subsystems/bittorrent.h +136 -0
  175. package/native-src/src/librats/subsystems/dht_discovery.cpp +202 -0
  176. package/native-src/src/librats/subsystems/dht_discovery.h +123 -0
  177. package/native-src/src/librats/subsystems/dht_service.h +36 -0
  178. package/native-src/src/librats/subsystems/file_transfer.cpp +972 -0
  179. package/native-src/src/librats/subsystems/file_transfer.h +367 -0
  180. package/native-src/src/librats/subsystems/hole_punch.cpp +605 -0
  181. package/native-src/src/librats/subsystems/hole_punch.h +290 -0
  182. package/native-src/src/librats/subsystems/hole_punch_service.h +38 -0
  183. package/native-src/src/librats/subsystems/mdns_discovery.cpp +66 -0
  184. package/native-src/src/librats/subsystems/mdns_discovery.h +55 -0
  185. package/native-src/src/librats/subsystems/message_json.cpp +112 -0
  186. package/native-src/src/librats/subsystems/message_json.h +88 -0
  187. package/native-src/src/librats/subsystems/peer_exchange.cpp +241 -0
  188. package/native-src/src/librats/subsystems/peer_exchange.h +136 -0
  189. package/native-src/src/librats/subsystems/ping_service.cpp +98 -0
  190. package/native-src/src/librats/subsystems/ping_service.h +66 -0
  191. package/native-src/src/librats/subsystems/port_mapping_service.cpp +192 -0
  192. package/native-src/src/librats/subsystems/port_mapping_service.h +84 -0
  193. package/native-src/src/librats/subsystems/pubsub.cpp +567 -0
  194. package/native-src/src/librats/subsystems/pubsub.h +175 -0
  195. package/native-src/src/librats/subsystems/reconnection.cpp +239 -0
  196. package/native-src/src/librats/subsystems/reconnection.h +126 -0
  197. package/native-src/src/librats/subsystems/relay.cpp +1142 -0
  198. package/native-src/src/librats/subsystems/relay.h +211 -0
  199. package/native-src/src/librats/subsystems/relay_service.h +46 -0
  200. package/native-src/src/librats/transport/connection.cpp +343 -0
  201. package/native-src/src/librats/transport/connection.h +283 -0
  202. package/native-src/src/librats/transport/link.h +96 -0
  203. package/native-src/src/librats/transport/reactor.cpp +588 -0
  204. package/native-src/src/librats/transport/reactor.h +262 -0
  205. package/native-src/src/librats/transport/reactor_pool.h +81 -0
  206. package/native-src/src/librats/transport/relay_link.cpp +208 -0
  207. package/native-src/src/librats/transport/relay_link.h +303 -0
  208. package/native-src/src/librats/transport/tcp_link.cpp +49 -0
  209. package/native-src/src/librats/transport/tcp_link.h +43 -0
  210. package/native-src/src/librats/transport/udp_mux.cpp +617 -0
  211. package/native-src/src/librats/transport/udp_mux.h +363 -0
  212. package/native-src/src/librats/transport/udp_packet.cpp +121 -0
  213. package/native-src/src/librats/transport/udp_packet.h +190 -0
  214. package/native-src/src/librats/transport/udp_stream.cpp +1194 -0
  215. package/native-src/src/librats/transport/udp_stream.h +614 -0
  216. package/native-src/src/librats/util/features.h.in +51 -0
  217. package/native-src/src/{fs.cpp → librats/util/fs.cpp} +51 -3
  218. package/native-src/src/librats/util/fs.h +136 -0
  219. package/native-src/src/librats/util/json.cpp +1002 -0
  220. package/native-src/src/librats/util/json.h +444 -0
  221. package/native-src/src/{logger.cpp → librats/util/logger.cpp} +1 -1
  222. package/native-src/src/{logger.h → librats/util/logger.h} +43 -31
  223. package/native-src/src/{network_monitor.cpp → librats/util/network_monitor.cpp} +12 -4
  224. package/native-src/src/{network_monitor.h → librats/util/network_monitor.h} +2 -1
  225. package/native-src/src/{network_utils.cpp → librats/util/network_utils.cpp} +38 -23
  226. package/native-src/src/{network_utils.h → librats/util/network_utils.h} +15 -8
  227. package/native-src/src/{os.cpp → librats/util/os.cpp} +48 -18
  228. package/native-src/src/librats/util/rats_export.h +69 -0
  229. package/native-src/src/{version.cpp → librats/util/version.cpp} +2 -2
  230. package/native-src/src/{version.h.in → librats/util/version.h.in} +1 -1
  231. package/native-src/src/librats/wire/frame.cpp +76 -0
  232. package/native-src/src/librats/wire/frame.h +111 -0
  233. package/native-src/src/librats/wire/message_router.cpp +45 -0
  234. package/native-src/src/librats/wire/message_router.h +47 -0
  235. package/package.json +5 -4
  236. package/scripts/build-librats.js +1 -0
  237. package/scripts/postinstall.js +3 -3
  238. package/scripts/prepare-package.js +4 -4
  239. package/scripts/verify-installation.js +63 -105
  240. package/src/librats_node.cpp +1067 -1323
  241. package/native-src/src/bencode.cpp +0 -485
  242. package/native-src/src/bencode.h +0 -145
  243. package/native-src/src/bittorrent.cpp +0 -14
  244. package/native-src/src/bittorrent.h +0 -74
  245. package/native-src/src/bt_bitfield.cpp +0 -372
  246. package/native-src/src/bt_bitfield.h +0 -316
  247. package/native-src/src/bt_choker.cpp +0 -228
  248. package/native-src/src/bt_choker.h +0 -147
  249. package/native-src/src/bt_client.cpp +0 -1047
  250. package/native-src/src/bt_client.h +0 -445
  251. package/native-src/src/bt_create_torrent.cpp +0 -677
  252. package/native-src/src/bt_create_torrent.h +0 -473
  253. package/native-src/src/bt_extension.cpp +0 -469
  254. package/native-src/src/bt_extension.h +0 -309
  255. package/native-src/src/bt_file_storage.cpp +0 -261
  256. package/native-src/src/bt_file_storage.h +0 -298
  257. package/native-src/src/bt_handshake.cpp +0 -134
  258. package/native-src/src/bt_handshake.h +0 -157
  259. package/native-src/src/bt_messages.cpp +0 -364
  260. package/native-src/src/bt_messages.h +0 -324
  261. package/native-src/src/bt_network.cpp +0 -1007
  262. package/native-src/src/bt_network.h +0 -417
  263. package/native-src/src/bt_peer_connection.cpp +0 -742
  264. package/native-src/src/bt_peer_connection.h +0 -592
  265. package/native-src/src/bt_piece_picker.cpp +0 -786
  266. package/native-src/src/bt_piece_picker.h +0 -473
  267. package/native-src/src/bt_resume_data.cpp +0 -410
  268. package/native-src/src/bt_resume_data.h +0 -249
  269. package/native-src/src/bt_torrent.cpp +0 -2120
  270. package/native-src/src/bt_torrent.h +0 -641
  271. package/native-src/src/bt_torrent_info.cpp +0 -659
  272. package/native-src/src/bt_torrent_info.h +0 -418
  273. package/native-src/src/bt_types.h +0 -621
  274. package/native-src/src/chained_send_buffer.cpp +0 -75
  275. package/native-src/src/chained_send_buffer.h +0 -137
  276. package/native-src/src/crypto/hkdf.c +0 -266
  277. package/native-src/src/crypto/poly1305.h +0 -36
  278. package/native-src/src/dht.cpp +0 -3311
  279. package/native-src/src/dht.h +0 -717
  280. package/native-src/src/disk_io.cpp +0 -632
  281. package/native-src/src/disk_io.h +0 -315
  282. package/native-src/src/file_transfer.cpp +0 -1415
  283. package/native-src/src/file_transfer.h +0 -286
  284. package/native-src/src/fs.h +0 -108
  285. package/native-src/src/gossipsub.cpp +0 -1139
  286. package/native-src/src/gossipsub.h +0 -403
  287. package/native-src/src/ice.cpp +0 -893
  288. package/native-src/src/ice.h +0 -559
  289. package/native-src/src/json.hpp +0 -25526
  290. package/native-src/src/librats.cpp +0 -2378
  291. package/native-src/src/librats.h +0 -2324
  292. package/native-src/src/librats_bittorrent.cpp +0 -601
  293. package/native-src/src/librats_c.cpp +0 -1557
  294. package/native-src/src/librats_c.h +0 -323
  295. package/native-src/src/librats_discovery.cpp +0 -402
  296. package/native-src/src/librats_encryption.cpp +0 -275
  297. package/native-src/src/librats_file_transfer.cpp +0 -144
  298. package/native-src/src/librats_gossipsub.cpp +0 -289
  299. package/native-src/src/librats_ice.cpp +0 -213
  300. package/native-src/src/librats_log_macros.h +0 -36
  301. package/native-src/src/librats_logging.cpp +0 -173
  302. package/native-src/src/librats_mdns.cpp +0 -166
  303. package/native-src/src/librats_persistence.cpp +0 -796
  304. package/native-src/src/librats_portmap.cpp +0 -419
  305. package/native-src/src/librats_reconnection.cpp +0 -218
  306. package/native-src/src/librats_statistic.cpp +0 -105
  307. package/native-src/src/librats_storage.cpp +0 -189
  308. package/native-src/src/rats_export.h +0 -17
  309. package/native-src/src/receive_buffer.cpp +0 -82
  310. package/native-src/src/receive_buffer.h +0 -127
  311. package/native-src/src/socket.h +0 -228
  312. package/native-src/src/threadmanager.cpp +0 -105
  313. package/native-src/src/threadmanager.h +0 -53
  314. package/native-src/src/tracker.cpp +0 -1264
  315. package/native-src/src/tracker.h +0 -319
  316. package/native-src/src/turn.cpp +0 -762
  317. package/native-src/src/turn.h +0 -460
  318. package/native-src/src/wakeup_pipe.h +0 -60
  319. /package/native-src/src/{os.h → librats/util/os.h} +0 -0
@@ -1,641 +0,0 @@
1
- #pragma once
2
-
3
- /**
4
- * @file bt_torrent.h
5
- * @brief Active torrent management
6
- *
7
- * Manages the state of a torrent being downloaded or seeded,
8
- * including peer connections, piece management, and I/O.
9
- */
10
-
11
- #include "bt_types.h"
12
- #include "bt_bitfield.h"
13
- #include "bt_torrent_info.h"
14
- #include "bt_piece_picker.h"
15
- #include "bt_peer_connection.h"
16
- #include "bt_choker.h"
17
- #include "disk_io.h"
18
- #include "socket.h" // For Peer struct
19
-
20
- // Forward declaration for resume data
21
- namespace librats { struct TorrentResumeData; }
22
-
23
- #include <memory>
24
- #include <vector>
25
- #include <unordered_map>
26
- #include <functional>
27
- #include <mutex>
28
- #include <atomic>
29
- #include <chrono>
30
-
31
- namespace librats {
32
-
33
- //=============================================================================
34
- // Torrent State
35
- //=============================================================================
36
-
37
- /**
38
- * @brief Current state of a torrent
39
- */
40
- enum class TorrentState : uint8_t {
41
- Stopped, ///< Not active
42
- CheckingFiles, ///< Verifying existing data
43
- DownloadingMetadata,///< Getting metadata via ut_metadata
44
- Downloading, ///< Downloading pieces
45
- Seeding, ///< Complete, uploading to others
46
- Paused, ///< Paused by user
47
- Error ///< Error state
48
- };
49
-
50
- /**
51
- * @brief Convert state to string
52
- */
53
- const char* torrent_state_to_string(TorrentState state);
54
-
55
- //=============================================================================
56
- // Torrent Statistics
57
- //=============================================================================
58
-
59
- /**
60
- * @brief Statistics for a torrent
61
- */
62
- struct TorrentStats {
63
- // Progress
64
- uint64_t total_size;
65
- uint64_t bytes_done;
66
- uint32_t pieces_total;
67
- uint32_t pieces_done;
68
- float progress; ///< 0.0 to 1.0
69
-
70
- // Transfer rates
71
- uint64_t download_rate; ///< Bytes/sec
72
- uint64_t upload_rate; ///< Bytes/sec
73
- uint64_t total_downloaded;
74
- uint64_t total_uploaded;
75
-
76
- // Peers
77
- uint32_t peers_connected;
78
- uint32_t peers_total; ///< Known peers
79
- uint32_t seeders;
80
- uint32_t leechers;
81
-
82
- // Time
83
- std::chrono::steady_clock::time_point started_at;
84
- std::chrono::seconds eta; ///< Estimated time remaining
85
-
86
- TorrentStats()
87
- : total_size(0), bytes_done(0)
88
- , pieces_total(0), pieces_done(0), progress(0.0f)
89
- , download_rate(0), upload_rate(0)
90
- , total_downloaded(0), total_uploaded(0)
91
- , peers_connected(0), peers_total(0)
92
- , seeders(0), leechers(0)
93
- , eta(0) {}
94
- };
95
-
96
- //=============================================================================
97
- // Torrent Configuration
98
- //=============================================================================
99
-
100
- /**
101
- * @brief Configuration for a torrent
102
- */
103
- struct TorrentConfig {
104
- std::string save_path; ///< Directory to save files
105
- std::string resume_data_path; ///< Directory for resume data (if empty, uses save_path)
106
- size_t max_connections; ///< Max peer connections (0 = unlimited)
107
- size_t max_uploads; ///< Max upload slots
108
- uint64_t download_limit; ///< Bytes/sec (0 = unlimited)
109
- uint64_t upload_limit; ///< Bytes/sec (0 = unlimited)
110
- bool sequential_download; ///< Download pieces in order
111
- bool seed_mode; ///< Assume files are complete
112
-
113
- TorrentConfig()
114
- : max_connections(50)
115
- , max_uploads(4)
116
- , download_limit(0)
117
- , upload_limit(0)
118
- , sequential_download(false)
119
- , seed_mode(false) {}
120
-
121
- /// Get the path to use for resume data (resume_data_path if set, otherwise save_path)
122
- const std::string& get_resume_path() const {
123
- return resume_data_path.empty() ? save_path : resume_data_path;
124
- }
125
- };
126
-
127
- //=============================================================================
128
- // Torrent
129
- //=============================================================================
130
-
131
- /**
132
- * @brief Manages an active torrent
133
- *
134
- * This class coordinates all aspects of downloading/seeding a torrent:
135
- * - Peer connection management
136
- * - Piece selection and requesting
137
- * - Disk I/O for reading/writing pieces
138
- * - Tracker and DHT announces
139
- * - Choking algorithm
140
- *
141
- * Thread-safe: Most methods are protected by a mutex.
142
- */
143
- class Torrent : public std::enable_shared_from_this<Torrent> {
144
- public:
145
- //=========================================================================
146
- // Types
147
- //=========================================================================
148
-
149
- using Ptr = std::shared_ptr<Torrent>;
150
-
151
- /// Callback for state changes
152
- using StateCallback = std::function<void(Torrent*, TorrentState)>;
153
-
154
- /// Callback for piece completion
155
- using PieceCallback = std::function<void(Torrent*, uint32_t piece)>;
156
-
157
- /// Callback for completion
158
- using CompleteCallback = std::function<void(Torrent*)>;
159
-
160
- /// Callback for errors
161
- using ErrorCallback = std::function<void(Torrent*, const std::string&)>;
162
-
163
- /// Callback for metadata received (for magnet links)
164
- using MetadataCallback = std::function<void(Torrent*, bool success)>;
165
-
166
- /// Callback for progress updates (downloaded bytes, total bytes, percentage)
167
- using ProgressCallback = std::function<void(uint64_t downloaded, uint64_t total, double percentage)>;
168
-
169
- /// Callback for torrent completion (name)
170
- using TorrentCompleteCallback = std::function<void(const std::string& name)>;
171
-
172
- /// Callback for metadata received with TorrentInfo (for rats-search compatibility)
173
- using MetadataCompleteCallback = std::function<void(const TorrentInfo& info)>;
174
-
175
- /// Callback for peer connected (peer info)
176
- using PeerConnectedCallback = std::function<void(const Peer& peer)>;
177
-
178
- /// Callback for peer disconnected (peer info)
179
- using PeerDisconnectedCallback = std::function<void(const Peer& peer)>;
180
-
181
- //=========================================================================
182
- // Construction
183
- //=========================================================================
184
-
185
- /**
186
- * @brief Create a torrent from TorrentInfo
187
- *
188
- * @param info Torrent metadata
189
- * @param config Configuration
190
- * @param our_peer_id Our peer ID
191
- */
192
- Torrent(const TorrentInfo& info,
193
- const TorrentConfig& config,
194
- const PeerID& our_peer_id);
195
-
196
- /**
197
- * @brief Create a torrent from magnet link (no metadata yet)
198
- *
199
- * @param info_hash Info hash from magnet
200
- * @param name Display name (may be empty)
201
- * @param config Configuration
202
- * @param our_peer_id Our peer ID
203
- */
204
- Torrent(const BtInfoHash& info_hash,
205
- const std::string& name,
206
- const TorrentConfig& config,
207
- const PeerID& our_peer_id);
208
-
209
- ~Torrent();
210
-
211
- // Non-copyable
212
- Torrent(const Torrent&) = delete;
213
- Torrent& operator=(const Torrent&) = delete;
214
-
215
- //=========================================================================
216
- // Lifecycle
217
- //=========================================================================
218
-
219
- /**
220
- * @brief Start the torrent
221
- */
222
- void start();
223
-
224
- /**
225
- * @brief Stop the torrent
226
- */
227
- void stop();
228
-
229
- /**
230
- * @brief Pause the torrent
231
- */
232
- void pause();
233
-
234
- /**
235
- * @brief Resume a paused torrent
236
- */
237
- void resume();
238
-
239
- /**
240
- * @brief Force a recheck of files
241
- */
242
- void recheck();
243
-
244
- //=========================================================================
245
- // State
246
- //=========================================================================
247
-
248
- /**
249
- * @brief Get current state
250
- */
251
- TorrentState state() const { return state_; }
252
-
253
- /**
254
- * @brief Check if torrent is active (downloading or seeding)
255
- */
256
- bool is_active() const;
257
-
258
- /**
259
- * @brief Check if download is complete
260
- */
261
- bool is_complete() const;
262
-
263
- /**
264
- * @brief Check if we have metadata
265
- */
266
- bool has_metadata() const;
267
-
268
- //=========================================================================
269
- // Info
270
- //=========================================================================
271
-
272
- /**
273
- * @brief Get info hash
274
- */
275
- const BtInfoHash& info_hash() const { return info_hash_; }
276
-
277
- /**
278
- * @brief Get info hash as hex
279
- */
280
- std::string info_hash_hex() const { return info_hash_to_hex(info_hash_); }
281
-
282
- /**
283
- * @brief Get torrent name
284
- */
285
- const std::string& name() const { return name_; }
286
-
287
- /**
288
- * @brief Get torrent info (metadata)
289
- */
290
- const TorrentInfo* info() const { return info_.get(); }
291
-
292
- /**
293
- * @brief Get torrent info reference (for rats-search compatibility)
294
- * @note Returns a static empty TorrentInfo if metadata not available
295
- */
296
- const TorrentInfo& get_torrent_info() const;
297
-
298
- /**
299
- * @brief Get save path
300
- */
301
- const std::string& save_path() const { return config_.save_path; }
302
-
303
- //=========================================================================
304
- // Progress and Speed Accessors (rats-search compatibility)
305
- //=========================================================================
306
-
307
- /**
308
- * @brief Get total downloaded bytes
309
- */
310
- uint64_t downloaded_bytes() const;
311
-
312
- /**
313
- * @brief Get download progress as percentage (0-100)
314
- */
315
- double progress_percentage() const;
316
-
317
- /**
318
- * @brief Get current download speed in bytes/sec
319
- */
320
- double download_speed() const;
321
-
322
- //=========================================================================
323
- // Statistics
324
- //=========================================================================
325
-
326
- /**
327
- * @brief Get current statistics
328
- */
329
- TorrentStats stats() const;
330
-
331
- /**
332
- * @brief Get have bitfield
333
- */
334
- Bitfield get_have_bitfield() const;
335
-
336
- //=========================================================================
337
- // Peer Management
338
- //=========================================================================
339
-
340
- /**
341
- * @brief Add a peer to connect to
342
- *
343
- * @param ip Peer IP address
344
- * @param port Peer port
345
- */
346
- void add_peer(const std::string& ip, uint16_t port);
347
-
348
- /**
349
- * @brief Add peers from tracker response
350
- *
351
- * @param peers List of (ip, port) pairs
352
- */
353
- void add_peers(const std::vector<std::pair<std::string, uint16_t>>& peers);
354
-
355
- /**
356
- * @brief Get number of connected peers
357
- */
358
- size_t num_peers() const;
359
-
360
- /**
361
- * @brief Get list of peer connections
362
- */
363
- std::vector<BtPeerConnection*> peers() const;
364
-
365
- /**
366
- * @brief Get pending peers to connect to
367
- * @return List of (ip, port) pairs
368
- */
369
- std::vector<std::pair<std::string, uint16_t>> get_pending_peers() const;
370
-
371
- /**
372
- * @brief Clear pending peers
373
- */
374
- void clear_pending_peers();
375
-
376
- /**
377
- * @brief Add an established connection
378
- * Called by BtClient when network manager establishes a connection
379
- */
380
- void add_connection(std::shared_ptr<BtPeerConnection> connection);
381
-
382
- /**
383
- * @brief Remove a connection by pointer
384
- */
385
- void remove_connection(BtPeerConnection* connection);
386
-
387
- //=========================================================================
388
- // Configuration
389
- //=========================================================================
390
-
391
- /**
392
- * @brief Set sequential download mode
393
- */
394
- void set_sequential(bool sequential);
395
-
396
- /**
397
- * @brief Set download rate limit
398
- */
399
- void set_download_limit(uint64_t bytes_per_sec);
400
-
401
- /**
402
- * @brief Set upload rate limit
403
- */
404
- void set_upload_limit(uint64_t bytes_per_sec);
405
-
406
- /**
407
- * @brief Get current config
408
- */
409
- const TorrentConfig& config() const { return config_; }
410
-
411
- //=========================================================================
412
- // Callbacks
413
- //=========================================================================
414
-
415
- void set_state_callback(StateCallback cb) { on_state_change_ = std::move(cb); }
416
- void set_piece_callback(PieceCallback cb) { on_piece_complete_ = std::move(cb); }
417
- void set_complete_callback(CompleteCallback cb) { on_complete_ = std::move(cb); }
418
- void set_error_callback(ErrorCallback cb) { on_error_ = std::move(cb); }
419
- void set_metadata_callback(MetadataCallback cb) { on_metadata_received_ = std::move(cb); }
420
-
421
- // Extended callbacks (rats-search compatibility)
422
- void set_progress_callback(ProgressCallback cb) { on_progress_ = std::move(cb); }
423
- void set_torrent_complete_callback(TorrentCompleteCallback cb) { on_torrent_complete_ = std::move(cb); }
424
- void set_metadata_complete_callback(MetadataCompleteCallback cb) { on_metadata_complete_ = std::move(cb); }
425
- void set_peer_connected_callback(PeerConnectedCallback cb) { on_peer_connected_ext_ = std::move(cb); }
426
- void set_peer_disconnected_callback(PeerDisconnectedCallback cb) { on_peer_disconnected_ext_ = std::move(cb); }
427
-
428
- //=========================================================================
429
- // Metadata (for magnet links)
430
- //=========================================================================
431
-
432
- /**
433
- * @brief Set metadata received from peers
434
- *
435
- * @param metadata Raw info dict bytes
436
- * @return true if valid
437
- */
438
- bool set_metadata(const std::vector<uint8_t>& metadata);
439
-
440
- //=========================================================================
441
- // Resume Data (Fast Resume)
442
- //=========================================================================
443
-
444
- /**
445
- * @brief Generate resume data for this torrent
446
- *
447
- * Resume data can be saved to disk and used to quickly resume
448
- * downloading without re-verifying existing pieces.
449
- *
450
- * @return Resume data structure
451
- */
452
- TorrentResumeData generate_resume_data() const;
453
-
454
- /**
455
- * @brief Save resume data to the default path
456
- *
457
- * Saves to {save_path}/.resume/{info_hash}.resume
458
- *
459
- * @return true on success
460
- */
461
- bool save_resume_data();
462
-
463
- /**
464
- * @brief Save resume data to a specific path
465
- *
466
- * @param path Path to save the resume file
467
- * @return true on success
468
- */
469
- bool save_resume_data(const std::string& path);
470
-
471
- /**
472
- * @brief Load resume data and apply it to this torrent
473
- *
474
- * This should be called before start() to apply saved state.
475
- * Will set have_pieces_ bitfield and restore unfinished pieces.
476
- *
477
- * @param resume_data Resume data to apply
478
- * @return true if resume data was valid and applied
479
- */
480
- bool load_resume_data(const TorrentResumeData& resume_data);
481
-
482
- /**
483
- * @brief Try to load resume data from the default path
484
- *
485
- * Looks for {save_path}/.resume/{info_hash}.resume
486
- *
487
- * @return true if resume data was found and applied
488
- */
489
- bool try_load_resume_data();
490
-
491
- //=========================================================================
492
- // File Verification (Recheck)
493
- //=========================================================================
494
-
495
- /**
496
- * @brief Check which pieces are already complete on disk
497
- *
498
- * Reads and verifies hashes for all pieces. This is called during
499
- * recheck() or when resuming without resume data.
500
- *
501
- * @param progress_callback Optional callback for progress updates
502
- * Parameters: (current_piece, total_pieces)
503
- */
504
- void check_files(std::function<void(uint32_t, uint32_t)> progress_callback = nullptr);
505
-
506
- /**
507
- * @brief Check files asynchronously
508
- *
509
- * @param completion_callback Called when checking is complete
510
- * Parameters: (num_pieces_have, num_pieces_total)
511
- * @param progress_callback Optional callback for progress updates
512
- */
513
- void check_files_async(
514
- std::function<void(uint32_t, uint32_t)> completion_callback,
515
- std::function<void(uint32_t, uint32_t)> progress_callback = nullptr);
516
-
517
- //=========================================================================
518
- // Tick (called periodically)
519
- //=========================================================================
520
-
521
- /**
522
- * @brief Process periodic tasks
523
- *
524
- * Should be called regularly (e.g., every 100ms)
525
- */
526
- void tick();
527
-
528
- private:
529
- //=========================================================================
530
- // Internal Methods
531
- //=========================================================================
532
-
533
- void set_state(TorrentState new_state);
534
-
535
- // Unlocked versions (call when mutex is already held)
536
- bool is_complete_unlocked() const;
537
- bool has_metadata_unlocked() const;
538
- void on_peer_connected(BtPeerConnection* peer);
539
- void on_peer_disconnected(BtPeerConnection* peer);
540
- void on_peer_message(BtPeerConnection* peer, const BtMessage& msg);
541
- void on_piece_received(uint32_t piece, uint32_t begin, const std::vector<uint8_t>& data);
542
- void on_piece_verified(uint32_t piece, bool valid);
543
- void request_pieces();
544
- void request_blocks_from_peer(BtPeerConnection* peer);
545
- void run_choker();
546
- void update_stats();
547
-
548
- // Disk I/O helpers
549
- std::vector<FileMappingInfo> get_file_mappings() const;
550
- void write_block_to_disk(const BlockInfo& block, const std::vector<uint8_t>& data);
551
- void read_piece_from_disk(uint32_t piece, BtPeerConnection* peer,
552
- uint32_t begin, uint32_t length);
553
- void verify_piece_hash(uint32_t piece);
554
-
555
- // Extension protocol helpers
556
- void send_extension_handshake(BtPeerConnection* peer);
557
- void on_extension_message(BtPeerConnection* peer, uint8_t ext_id,
558
- const std::vector<uint8_t>& payload);
559
- void on_extension_handshake(BtPeerConnection* peer,
560
- const std::vector<uint8_t>& payload);
561
- void request_metadata(BtPeerConnection* peer);
562
- void on_metadata_message(BtPeerConnection* peer,
563
- const std::vector<uint8_t>& payload);
564
- void on_metadata_complete();
565
- size_t find_bencode_end(const std::vector<uint8_t>& data);
566
-
567
- // Resume/recheck helpers
568
- void verify_piece_hash_sync(uint32_t piece, bool& valid);
569
- void on_file_check_complete(uint32_t pieces_have);
570
-
571
- //=========================================================================
572
- // Data Members
573
- //=========================================================================
574
-
575
- mutable std::mutex mutex_;
576
-
577
- // Identity
578
- BtInfoHash info_hash_;
579
- std::string name_;
580
- PeerID our_peer_id_;
581
-
582
- // Metadata
583
- std::unique_ptr<TorrentInfo> info_;
584
-
585
- // Configuration
586
- TorrentConfig config_;
587
-
588
- // State
589
- std::atomic<TorrentState> state_;
590
- std::string error_message_;
591
-
592
- // Piece management
593
- std::unique_ptr<PiecePicker> picker_;
594
- Bitfield have_pieces_;
595
- // Note: Blocks are written directly to disk as they arrive (no in-memory buffering)
596
- // This prevents memory bloat when downloading from many peers simultaneously
597
-
598
- // Peers
599
- std::vector<std::shared_ptr<BtPeerConnection>> connections_;
600
- std::vector<std::pair<std::string, uint16_t>> pending_peers_;
601
-
602
- // Choking
603
- Choker choker_;
604
-
605
- // Statistics
606
- TorrentStats stats_;
607
- std::chrono::steady_clock::time_point last_stats_update_;
608
- std::chrono::steady_clock::time_point last_choker_run_;
609
-
610
- // Time tracking for resume data
611
- int64_t added_time_ = 0; ///< When torrent was added (Unix time)
612
- int64_t completed_time_ = 0; ///< When download completed (Unix time)
613
- int64_t active_time_ = 0; ///< Seconds spent actively downloading
614
- int64_t seeding_time_ = 0; ///< Seconds spent seeding
615
- std::chrono::steady_clock::time_point last_activity_check_;
616
-
617
- // Callbacks
618
- StateCallback on_state_change_;
619
- PieceCallback on_piece_complete_;
620
- CompleteCallback on_complete_;
621
- ErrorCallback on_error_;
622
- MetadataCallback on_metadata_received_;
623
-
624
- // Extended callbacks (rats-search compatibility)
625
- ProgressCallback on_progress_;
626
- TorrentCompleteCallback on_torrent_complete_;
627
- MetadataCompleteCallback on_metadata_complete_;
628
- PeerConnectedCallback on_peer_connected_ext_;
629
- PeerDisconnectedCallback on_peer_disconnected_ext_;
630
-
631
- // Metadata exchange (for magnet links)
632
- std::vector<uint8_t> metadata_buffer_;
633
- std::vector<bool> metadata_pieces_received_;
634
- std::vector<bool> metadata_pieces_requested_;
635
- std::unordered_map<BtPeerConnection*, size_t> peer_metadata_size_;
636
- std::unordered_map<BtPeerConnection*, uint8_t> peer_ut_metadata_id_;
637
- std::chrono::steady_clock::time_point metadata_download_started_;
638
- static constexpr int METADATA_TIMEOUT_SECONDS = 120; // 2 minutes timeout
639
- };
640
-
641
- } // namespace librats