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,298 +0,0 @@
1
- #pragma once
2
-
3
- /**
4
- * @file bt_file_storage.h
5
- * @brief File storage layout for BitTorrent torrents
6
- *
7
- * Handles mapping between pieces/blocks and files in a torrent.
8
- * Supports both single-file and multi-file torrents.
9
- */
10
-
11
- #include <string>
12
- #include <vector>
13
- #include <cstdint>
14
-
15
- namespace librats {
16
-
17
- /**
18
- * @brief Information about a single file in the torrent
19
- */
20
- struct FileEntry {
21
- std::string path; ///< Relative path of the file
22
- int64_t size; ///< Size in bytes
23
- int64_t offset; ///< Offset from start of torrent data
24
- bool pad_file; ///< True if this is a padding file (not real data)
25
- bool executable; ///< Executable attribute (Unix)
26
- bool hidden; ///< Hidden attribute (Windows)
27
-
28
- FileEntry()
29
- : size(0), offset(0), pad_file(false), executable(false), hidden(false) {}
30
-
31
- FileEntry(const std::string& p, int64_t s, int64_t off = 0)
32
- : path(p), size(s), offset(off), pad_file(false), executable(false), hidden(false) {}
33
- };
34
-
35
- /**
36
- * @brief A slice of a file that corresponds to part of a piece
37
- */
38
- struct FileSlice {
39
- size_t file_index; ///< Index of the file in FileStorage
40
- int64_t offset; ///< Offset within the file
41
- int64_t size; ///< Number of bytes in this slice
42
-
43
- FileSlice() : file_index(0), offset(0), size(0) {}
44
- FileSlice(size_t idx, int64_t off, int64_t sz)
45
- : file_index(idx), offset(off), size(sz) {}
46
- };
47
-
48
- /**
49
- * @brief Result of mapping a file position to piece/block
50
- */
51
- struct PiecePosition {
52
- uint32_t piece; ///< Piece index
53
- uint32_t offset; ///< Offset within piece
54
- uint32_t length; ///< Length of data
55
-
56
- PiecePosition() : piece(0), offset(0), length(0) {}
57
- PiecePosition(uint32_t p, uint32_t off, uint32_t len)
58
- : piece(p), offset(off), length(len) {}
59
- };
60
-
61
- /**
62
- * @brief Manages file layout and piece-to-file mapping for a torrent
63
- *
64
- * This class stores information about all files in a torrent and provides
65
- * efficient mapping between:
66
- * - Piece/block positions and file positions
67
- * - File positions and piece positions
68
- *
69
- * Thread-safe for read operations after construction.
70
- */
71
- class FileStorage {
72
- public:
73
- /**
74
- * @brief Default constructor - creates empty storage
75
- */
76
- FileStorage();
77
-
78
- /**
79
- * @brief Constructor with piece length
80
- * @param piece_length Length of each piece in bytes
81
- */
82
- explicit FileStorage(uint32_t piece_length);
83
-
84
- /**
85
- * @brief Copy constructor
86
- */
87
- FileStorage(const FileStorage& other) = default;
88
-
89
- /**
90
- * @brief Move constructor
91
- */
92
- FileStorage(FileStorage&& other) noexcept = default;
93
-
94
- /**
95
- * @brief Copy assignment
96
- */
97
- FileStorage& operator=(const FileStorage& other) = default;
98
-
99
- /**
100
- * @brief Move assignment
101
- */
102
- FileStorage& operator=(FileStorage&& other) noexcept = default;
103
-
104
- //=========================================================================
105
- // File Management
106
- //=========================================================================
107
-
108
- /**
109
- * @brief Add a file to the storage
110
- *
111
- * Files are added in order and their offsets are computed automatically.
112
- *
113
- * @param path Relative path of the file
114
- * @param size Size of the file in bytes
115
- * @param pad_file True if this is a padding file
116
- * @param executable True if file has executable attribute
117
- * @param hidden True if file has hidden attribute
118
- */
119
- void add_file(const std::string& path, int64_t size,
120
- bool pad_file = false, bool executable = false, bool hidden = false);
121
-
122
- /**
123
- * @brief Add a FileEntry directly
124
- * @param entry File entry to add (offset will be computed)
125
- */
126
- void add_file(const FileEntry& entry);
127
-
128
- /**
129
- * @brief Reserve space for files (optimization)
130
- * @param num_files Expected number of files
131
- */
132
- void reserve(size_t num_files);
133
-
134
- /**
135
- * @brief Set the piece length
136
- * @param length Piece length in bytes
137
- */
138
- void set_piece_length(uint32_t length);
139
-
140
- /**
141
- * @brief Finalize the storage after all files are added
142
- *
143
- * Computes the total number of pieces based on total size and piece length.
144
- * Must be called before using mapping functions.
145
- */
146
- void finalize();
147
-
148
- //=========================================================================
149
- // Accessors
150
- //=========================================================================
151
-
152
- /**
153
- * @brief Get number of files
154
- */
155
- size_t num_files() const { return files_.size(); }
156
-
157
- /**
158
- * @brief Get total size of all files
159
- */
160
- int64_t total_size() const { return total_size_; }
161
-
162
- /**
163
- * @brief Get piece length
164
- */
165
- uint32_t piece_length() const { return piece_length_; }
166
-
167
- /**
168
- * @brief Get total number of pieces
169
- */
170
- uint32_t num_pieces() const { return num_pieces_; }
171
-
172
- /**
173
- * @brief Get size of a specific piece
174
- *
175
- * All pieces have the same size except possibly the last one.
176
- *
177
- * @param piece_index Index of the piece
178
- * @return Size of the piece in bytes
179
- */
180
- uint32_t piece_size(uint32_t piece_index) const;
181
-
182
- /**
183
- * @brief Get file entry by index
184
- * @param index File index
185
- * @return Reference to file entry
186
- */
187
- const FileEntry& file_at(size_t index) const { return files_[index]; }
188
-
189
- /**
190
- * @brief Get all file entries
191
- */
192
- const std::vector<FileEntry>& files() const { return files_; }
193
-
194
- /**
195
- * @brief Check if storage is empty
196
- */
197
- bool empty() const { return files_.empty(); }
198
-
199
- /**
200
- * @brief Check if storage is finalized
201
- */
202
- bool is_finalized() const { return finalized_; }
203
-
204
- //=========================================================================
205
- // Piece-to-File Mapping
206
- //=========================================================================
207
-
208
- /**
209
- * @brief Map a piece/block to file slices
210
- *
211
- * Given a piece index, offset within piece, and size, returns the list
212
- * of file slices that need to be read/written.
213
- *
214
- * @param piece Piece index
215
- * @param offset Offset within the piece
216
- * @param size Number of bytes
217
- * @return Vector of file slices
218
- */
219
- std::vector<FileSlice> map_block(uint32_t piece, uint32_t offset, uint32_t size) const;
220
-
221
- /**
222
- * @brief Find which file contains a given byte offset
223
- * @param torrent_offset Absolute offset from start of torrent data
224
- * @return File index, or num_files() if offset is out of range
225
- */
226
- size_t file_at_offset(int64_t torrent_offset) const;
227
-
228
- /**
229
- * @brief Get the file index at a given piece
230
- * @param piece Piece index
231
- * @return File index of the first file that starts in or spans this piece
232
- */
233
- size_t file_at_piece(uint32_t piece) const;
234
-
235
- //=========================================================================
236
- // File-to-Piece Mapping
237
- //=========================================================================
238
-
239
- /**
240
- * @brief Map a file position to piece position
241
- *
242
- * @param file_index Index of the file
243
- * @param file_offset Offset within the file
244
- * @param size Number of bytes
245
- * @return Piece position
246
- */
247
- PiecePosition map_file(size_t file_index, int64_t file_offset, uint32_t size) const;
248
-
249
- /**
250
- * @brief Get the first piece that contains data from a file
251
- * @param file_index File index
252
- * @return First piece index
253
- */
254
- uint32_t file_first_piece(size_t file_index) const;
255
-
256
- /**
257
- * @brief Get the last piece that contains data from a file
258
- * @param file_index File index
259
- * @return Last piece index
260
- */
261
- uint32_t file_last_piece(size_t file_index) const;
262
-
263
- /**
264
- * @brief Get number of pieces a file spans
265
- * @param file_index File index
266
- * @return Number of pieces
267
- */
268
- uint32_t file_num_pieces(size_t file_index) const;
269
-
270
- //=========================================================================
271
- // Torrent Name
272
- //=========================================================================
273
-
274
- /**
275
- * @brief Set the torrent name (root directory for multi-file)
276
- */
277
- void set_name(const std::string& name) { name_ = name; }
278
-
279
- /**
280
- * @brief Get the torrent name
281
- */
282
- const std::string& name() const { return name_; }
283
-
284
- private:
285
- std::vector<FileEntry> files_;
286
- std::string name_;
287
- int64_t total_size_;
288
- uint32_t piece_length_;
289
- uint32_t num_pieces_;
290
- bool finalized_;
291
-
292
- /**
293
- * @brief Binary search to find file at offset (helper)
294
- */
295
- size_t find_file_at_offset(int64_t offset) const;
296
- };
297
-
298
- } // namespace librats
@@ -1,134 +0,0 @@
1
- #include "bt_handshake.h"
2
- #include <cstring>
3
-
4
- namespace librats {
5
-
6
- //=============================================================================
7
- // ExtensionFlags
8
- //=============================================================================
9
-
10
- std::array<uint8_t, 8> ExtensionFlags::to_reserved() const {
11
- std::array<uint8_t, 8> reserved{};
12
-
13
- // BEP 10: Extension protocol - reserved[5] bit 4
14
- if (extension_protocol) {
15
- reserved[5] |= 0x10;
16
- }
17
-
18
- // BEP 5: DHT - reserved[7] bit 0
19
- if (dht) {
20
- reserved[7] |= 0x01;
21
- }
22
-
23
- // BEP 6: Fast extension - reserved[7] bit 2
24
- if (fast) {
25
- reserved[7] |= 0x04;
26
- }
27
-
28
- return reserved;
29
- }
30
-
31
- ExtensionFlags ExtensionFlags::from_reserved(const uint8_t* reserved) {
32
- ExtensionFlags flags;
33
-
34
- // BEP 10: Extension protocol - reserved[5] bit 4
35
- flags.extension_protocol = (reserved[5] & 0x10) != 0;
36
-
37
- // BEP 5: DHT - reserved[7] bit 0
38
- flags.dht = (reserved[7] & 0x01) != 0;
39
-
40
- // BEP 6: Fast extension - reserved[7] bit 2
41
- flags.fast = (reserved[7] & 0x04) != 0;
42
-
43
- return flags;
44
- }
45
-
46
- //=============================================================================
47
- // BtHandshake
48
- //=============================================================================
49
-
50
- std::vector<uint8_t> BtHandshake::encode(const BtInfoHash& info_hash,
51
- const PeerID& peer_id,
52
- const ExtensionFlags& extensions) {
53
- std::vector<uint8_t> handshake;
54
- handshake.reserve(BT_HANDSHAKE_SIZE);
55
-
56
- // Protocol string length (1 byte)
57
- handshake.push_back(static_cast<uint8_t>(BT_PROTOCOL_STRING_LEN));
58
-
59
- // Protocol string (19 bytes)
60
- handshake.insert(handshake.end(),
61
- BT_PROTOCOL_STRING,
62
- BT_PROTOCOL_STRING + BT_PROTOCOL_STRING_LEN);
63
-
64
- // Reserved bytes (8 bytes)
65
- auto reserved = extensions.to_reserved();
66
- handshake.insert(handshake.end(), reserved.begin(), reserved.end());
67
-
68
- // Info hash (20 bytes)
69
- handshake.insert(handshake.end(), info_hash.begin(), info_hash.end());
70
-
71
- // Peer ID (20 bytes)
72
- handshake.insert(handshake.end(), peer_id.begin(), peer_id.end());
73
-
74
- return handshake;
75
- }
76
-
77
- std::vector<uint8_t> BtHandshake::encode_with_extensions(const BtInfoHash& info_hash,
78
- const PeerID& peer_id) {
79
- ExtensionFlags flags;
80
- flags.enable_all();
81
- return encode(info_hash, peer_id, flags);
82
- }
83
-
84
- bool BtHandshake::is_complete(const uint8_t* data, size_t length) {
85
- return length >= BT_HANDSHAKE_SIZE;
86
- }
87
-
88
- std::optional<Handshake> BtHandshake::decode(const uint8_t* data, size_t length) {
89
- if (length < BT_HANDSHAKE_SIZE) {
90
- return std::nullopt;
91
- }
92
-
93
- // Validate protocol string length
94
- if (data[0] != BT_PROTOCOL_STRING_LEN) {
95
- return std::nullopt;
96
- }
97
-
98
- // Validate protocol string
99
- if (std::memcmp(data + 1, BT_PROTOCOL_STRING, BT_PROTOCOL_STRING_LEN) != 0) {
100
- return std::nullopt;
101
- }
102
-
103
- Handshake hs;
104
-
105
- // Parse reserved bytes (offset 20)
106
- std::memcpy(hs.reserved.data(), data + 20, 8);
107
- hs.extensions = ExtensionFlags::from_reserved(data + 20);
108
-
109
- // Parse info hash (offset 28)
110
- std::memcpy(hs.info_hash.data(), data + 28, BT_INFO_HASH_SIZE);
111
-
112
- // Parse peer ID (offset 48)
113
- std::memcpy(hs.peer_id.data(), data + 48, BT_PEER_ID_SIZE);
114
-
115
- return hs;
116
- }
117
-
118
- std::optional<Handshake> BtHandshake::decode(const std::vector<uint8_t>& data) {
119
- return decode(data.data(), data.size());
120
- }
121
-
122
- bool BtHandshake::validate_protocol(const uint8_t* data, size_t length) {
123
- if (length < 20) {
124
- return false;
125
- }
126
-
127
- if (data[0] != BT_PROTOCOL_STRING_LEN) {
128
- return false;
129
- }
130
-
131
- return std::memcmp(data + 1, BT_PROTOCOL_STRING, BT_PROTOCOL_STRING_LEN) == 0;
132
- }
133
-
134
- } // namespace librats
@@ -1,157 +0,0 @@
1
- #pragma once
2
-
3
- /**
4
- * @file bt_handshake.h
5
- * @brief BitTorrent handshake handling
6
- *
7
- * Implements the BitTorrent handshake protocol:
8
- * <pstrlen><pstr><reserved><info_hash><peer_id>
9
- *
10
- * Where:
11
- * - pstrlen: 1 byte, length of pstr (19 for "BitTorrent protocol")
12
- * - pstr: 19 bytes, protocol string
13
- * - reserved: 8 bytes, extension flags
14
- * - info_hash: 20 bytes, SHA-1 hash of the info dictionary
15
- * - peer_id: 20 bytes, unique client identifier
16
- */
17
-
18
- #include "bt_types.h"
19
-
20
- #include <vector>
21
- #include <cstdint>
22
- #include <optional>
23
- #include <array>
24
-
25
- namespace librats {
26
-
27
- //=============================================================================
28
- // Extension Flags
29
- //=============================================================================
30
-
31
- /**
32
- * @brief Extension flags in reserved bytes
33
- */
34
- struct ExtensionFlags {
35
- bool dht; ///< BEP 5: DHT support (reserved[7] & 0x01)
36
- bool fast; ///< BEP 6: Fast extension (reserved[7] & 0x04)
37
- bool extension_protocol;///< BEP 10: Extension protocol (reserved[5] & 0x10)
38
-
39
- ExtensionFlags() : dht(false), fast(false), extension_protocol(false) {}
40
-
41
- /**
42
- * @brief Set all standard extensions
43
- */
44
- void enable_all() {
45
- dht = true;
46
- fast = true;
47
- extension_protocol = true;
48
- }
49
-
50
- /**
51
- * @brief Convert to 8-byte reserved field
52
- */
53
- std::array<uint8_t, 8> to_reserved() const;
54
-
55
- /**
56
- * @brief Parse from 8-byte reserved field
57
- */
58
- static ExtensionFlags from_reserved(const uint8_t* reserved);
59
- };
60
-
61
- //=============================================================================
62
- // Handshake Data
63
- //=============================================================================
64
-
65
- /**
66
- * @brief Parsed handshake data
67
- */
68
- struct Handshake {
69
- BtInfoHash info_hash; ///< 20-byte info hash
70
- PeerID peer_id; ///< 20-byte peer ID
71
- ExtensionFlags extensions; ///< Parsed extension flags
72
- std::array<uint8_t, 8> reserved;///< Raw reserved bytes
73
-
74
- Handshake() : info_hash{}, peer_id{}, reserved{} {}
75
-
76
- /**
77
- * @brief Check if handshake is valid
78
- */
79
- bool is_valid() const { return !is_zero_hash(info_hash); }
80
-
81
- /**
82
- * @brief Get peer ID as string
83
- */
84
- std::string peer_id_string() const { return peer_id_to_string(peer_id); }
85
-
86
- /**
87
- * @brief Get info hash as hex string
88
- */
89
- std::string info_hash_hex() const { return info_hash_to_hex(info_hash); }
90
- };
91
-
92
- //=============================================================================
93
- // Handshake Encoder/Decoder
94
- //=============================================================================
95
-
96
- /**
97
- * @brief Encode and decode BitTorrent handshakes
98
- */
99
- class BtHandshake {
100
- public:
101
- /**
102
- * @brief Create a handshake message
103
- *
104
- * @param info_hash 20-byte info hash
105
- * @param peer_id 20-byte peer ID
106
- * @param extensions Extension flags to advertise
107
- * @return 68-byte handshake message
108
- */
109
- static std::vector<uint8_t> encode(const BtInfoHash& info_hash,
110
- const PeerID& peer_id,
111
- const ExtensionFlags& extensions = ExtensionFlags());
112
-
113
- /**
114
- * @brief Create a handshake message with all extensions enabled
115
- */
116
- static std::vector<uint8_t> encode_with_extensions(const BtInfoHash& info_hash,
117
- const PeerID& peer_id);
118
-
119
- /**
120
- * @brief Check if buffer contains a complete handshake
121
- *
122
- * @param data Buffer data
123
- * @param length Buffer length
124
- * @return true if at least 68 bytes available
125
- */
126
- static bool is_complete(const uint8_t* data, size_t length);
127
-
128
- /**
129
- * @brief Decode a handshake message
130
- *
131
- * @param data Buffer data (at least 68 bytes)
132
- * @param length Buffer length
133
- * @return Decoded handshake, or nullopt on error
134
- */
135
- static std::optional<Handshake> decode(const uint8_t* data, size_t length);
136
-
137
- /**
138
- * @brief Decode from vector
139
- */
140
- static std::optional<Handshake> decode(const std::vector<uint8_t>& data);
141
-
142
- /**
143
- * @brief Validate handshake protocol string
144
- *
145
- * @param data Buffer data
146
- * @param length Buffer length
147
- * @return true if starts with valid protocol string
148
- */
149
- static bool validate_protocol(const uint8_t* data, size_t length);
150
-
151
- /**
152
- * @brief Get size of handshake in bytes
153
- */
154
- static constexpr size_t size() { return BT_HANDSHAKE_SIZE; }
155
- };
156
-
157
- } // namespace librats