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,137 +0,0 @@
1
- #pragma once
2
-
3
- /**
4
- * @file chained_send_buffer.h
5
- * @brief Zero-copy chained send buffer for efficient network sending
6
- *
7
- * Stores a chain of buffers to be sent. Supports:
8
- * - Appending without copying (move semantics)
9
- * - Efficient partial sends via pop_front()
10
- * - Direct access to front chunk for send()
11
- *
12
- * Zero-copy chained buffer for efficient sending.
13
- */
14
-
15
- #include <vector>
16
- #include <deque>
17
- #include <cstdint>
18
- #include <cstddef>
19
-
20
- namespace librats {
21
-
22
- /**
23
- * @brief A chunk of data in the send chain
24
- */
25
- struct SendChunk {
26
- std::vector<uint8_t> data; ///< Owned data
27
- size_t offset = 0; ///< Current read offset (for partial sends)
28
-
29
- SendChunk() = default;
30
- explicit SendChunk(std::vector<uint8_t> d) : data(std::move(d)), offset(0) {}
31
-
32
- /// Bytes remaining to send
33
- size_t remaining() const { return data.size() - offset; }
34
-
35
- /// Pointer to current data position
36
- const uint8_t* current() const { return data.data() + offset; }
37
- };
38
-
39
- /**
40
- * @brief Zero-copy chained send buffer
41
- *
42
- * Stores a chain of buffers to be sent. Supports:
43
- * - Appending without copying (move semantics)
44
- * - Efficient partial sends via pop_front()
45
- * - Direct access to front chunk for send()
46
- *
47
- * Zero-copy chained buffer for efficient sending.
48
- *
49
- * Usage:
50
- * 1. append(data) - queue data for sending
51
- * 2. send(socket, front_data(), front_size())
52
- * 3. pop_front(bytes_sent) - remove sent data
53
- */
54
- class ChainedSendBuffer {
55
- public:
56
- ChainedSendBuffer() = default;
57
-
58
- //=========================================================================
59
- // Append interface
60
- //=========================================================================
61
-
62
- /**
63
- * @brief Append data by moving (zero-copy)
64
- */
65
- void append(std::vector<uint8_t> data);
66
-
67
- /**
68
- * @brief Append data by copying
69
- */
70
- void append(const uint8_t* data, size_t length);
71
-
72
- //=========================================================================
73
- // Send interface
74
- //=========================================================================
75
-
76
- /**
77
- * @brief Get pointer to front chunk data (for send())
78
- *
79
- * Returns nullptr if buffer is empty.
80
- */
81
- const uint8_t* front_data() const;
82
-
83
- /**
84
- * @brief Get size of front chunk (for send())
85
- *
86
- * Returns 0 if buffer is empty.
87
- */
88
- size_t front_size() const;
89
-
90
- /**
91
- * @brief Remove bytes from front after successful send
92
- */
93
- void pop_front(size_t bytes);
94
-
95
- //=========================================================================
96
- // Buffer state
97
- //=========================================================================
98
-
99
- /**
100
- * @brief Get total bytes pending across all chunks
101
- */
102
- size_t size() const { return total_bytes_; }
103
-
104
- /**
105
- * @brief Check if buffer is empty
106
- */
107
- bool empty() const { return total_bytes_ == 0; }
108
-
109
- /**
110
- * @brief Clear all pending data
111
- */
112
- void clear();
113
-
114
- /**
115
- * @brief Get number of chunks
116
- */
117
- size_t chunk_count() const { return chunks_.size(); }
118
-
119
- //=========================================================================
120
- // Advanced: Gather I/O support
121
- //=========================================================================
122
-
123
- /**
124
- * @brief Copy data to a contiguous buffer (for legacy APIs)
125
- *
126
- * @param buffer Output buffer
127
- * @param max_bytes Maximum bytes to copy
128
- * @return Actual bytes copied
129
- */
130
- size_t copy_to(uint8_t* buffer, size_t max_bytes) const;
131
-
132
- private:
133
- std::deque<SendChunk> chunks_;
134
- size_t total_bytes_ = 0;
135
- };
136
-
137
- } // namespace librats
@@ -1,266 +0,0 @@
1
- /*
2
- * HKDF-SHA256 implementation for Noise Protocol
3
- * Based on RFC 5869
4
- */
5
-
6
- #include "hkdf.h"
7
- #include "sha256.h"
8
- #include <string.h>
9
-
10
- #define SHA256_BLOCK_SIZE 64
11
- #define SHA256_HASH_SIZE 32
12
-
13
- /* Zero memory securely */
14
- static void secure_zero(void *ptr, size_t len) {
15
- volatile uint8_t *p = (volatile uint8_t *)ptr;
16
- while (len--) {
17
- *p++ = 0;
18
- }
19
- }
20
-
21
- void hmac_sha256(
22
- const uint8_t *key, size_t key_len,
23
- const uint8_t *data, size_t data_len,
24
- uint8_t output[HKDF_SHA256_HASH_LEN]
25
- ) {
26
- sha256_context_t ctx;
27
- uint8_t k_pad[SHA256_BLOCK_SIZE];
28
- uint8_t temp_key[SHA256_HASH_SIZE];
29
- const uint8_t *k;
30
- size_t k_len;
31
- size_t i;
32
-
33
- /* If key is longer than block size, hash it first */
34
- if (key_len > SHA256_BLOCK_SIZE) {
35
- sha256_hash(temp_key, key, key_len);
36
- k = temp_key;
37
- k_len = SHA256_HASH_SIZE;
38
- } else {
39
- k = key;
40
- k_len = key_len;
41
- }
42
-
43
- /* Prepare inner padding (key XOR 0x36) */
44
- memset(k_pad, 0x36, SHA256_BLOCK_SIZE);
45
- for (i = 0; i < k_len; i++) {
46
- k_pad[i] ^= k[i];
47
- }
48
-
49
- /* Inner hash: H(K XOR ipad || data) */
50
- sha256_reset(&ctx);
51
- sha256_update(&ctx, k_pad, SHA256_BLOCK_SIZE);
52
- sha256_update(&ctx, data, data_len);
53
- sha256_finish(&ctx, output);
54
-
55
- /* Prepare outer padding (key XOR 0x5c) */
56
- memset(k_pad, 0x5c, SHA256_BLOCK_SIZE);
57
- for (i = 0; i < k_len; i++) {
58
- k_pad[i] ^= k[i];
59
- }
60
-
61
- /* Outer hash: H(K XOR opad || inner_hash) */
62
- sha256_reset(&ctx);
63
- sha256_update(&ctx, k_pad, SHA256_BLOCK_SIZE);
64
- sha256_update(&ctx, output, SHA256_HASH_SIZE);
65
- sha256_finish(&ctx, output);
66
-
67
- /* Clean up */
68
- secure_zero(k_pad, sizeof(k_pad));
69
- secure_zero(temp_key, sizeof(temp_key));
70
- }
71
-
72
- void hmac_sha256_2(
73
- const uint8_t *key, size_t key_len,
74
- const uint8_t *data1, size_t data1_len,
75
- const uint8_t *data2, size_t data2_len,
76
- uint8_t output[HKDF_SHA256_HASH_LEN]
77
- ) {
78
- sha256_context_t ctx;
79
- uint8_t k_pad[SHA256_BLOCK_SIZE];
80
- uint8_t temp_key[SHA256_HASH_SIZE];
81
- const uint8_t *k;
82
- size_t k_len;
83
- size_t i;
84
-
85
- /* If key is longer than block size, hash it first */
86
- if (key_len > SHA256_BLOCK_SIZE) {
87
- sha256_hash(temp_key, key, key_len);
88
- k = temp_key;
89
- k_len = SHA256_HASH_SIZE;
90
- } else {
91
- k = key;
92
- k_len = key_len;
93
- }
94
-
95
- /* Prepare inner padding (key XOR 0x36) */
96
- memset(k_pad, 0x36, SHA256_BLOCK_SIZE);
97
- for (i = 0; i < k_len; i++) {
98
- k_pad[i] ^= k[i];
99
- }
100
-
101
- /* Inner hash: H(K XOR ipad || data1 || data2) */
102
- sha256_reset(&ctx);
103
- sha256_update(&ctx, k_pad, SHA256_BLOCK_SIZE);
104
- sha256_update(&ctx, data1, data1_len);
105
- sha256_update(&ctx, data2, data2_len);
106
- sha256_finish(&ctx, output);
107
-
108
- /* Prepare outer padding (key XOR 0x5c) */
109
- memset(k_pad, 0x5c, SHA256_BLOCK_SIZE);
110
- for (i = 0; i < k_len; i++) {
111
- k_pad[i] ^= k[i];
112
- }
113
-
114
- /* Outer hash: H(K XOR opad || inner_hash) */
115
- sha256_reset(&ctx);
116
- sha256_update(&ctx, k_pad, SHA256_BLOCK_SIZE);
117
- sha256_update(&ctx, output, SHA256_HASH_SIZE);
118
- sha256_finish(&ctx, output);
119
-
120
- /* Clean up */
121
- secure_zero(k_pad, sizeof(k_pad));
122
- secure_zero(temp_key, sizeof(temp_key));
123
- }
124
-
125
- void hkdf_sha256_extract(
126
- const uint8_t *salt, size_t salt_len,
127
- const uint8_t *ikm, size_t ikm_len,
128
- uint8_t prk[HKDF_SHA256_HASH_LEN]
129
- ) {
130
- static const uint8_t default_salt[HKDF_SHA256_HASH_LEN] = {0};
131
-
132
- if (salt == NULL || salt_len == 0) {
133
- salt = default_salt;
134
- salt_len = HKDF_SHA256_HASH_LEN;
135
- }
136
-
137
- hmac_sha256(salt, salt_len, ikm, ikm_len, prk);
138
- }
139
-
140
- void hkdf_sha256_expand(
141
- const uint8_t prk[HKDF_SHA256_HASH_LEN],
142
- const uint8_t *info, size_t info_len,
143
- uint8_t *okm, size_t okm_len
144
- ) {
145
- sha256_context_t ctx;
146
- uint8_t k_pad[SHA256_BLOCK_SIZE];
147
- uint8_t T[SHA256_HASH_SIZE];
148
- uint8_t counter;
149
- size_t t_len = 0;
150
- size_t copy_len;
151
- size_t i;
152
-
153
- /* Key is always 32 bytes (hash length) */
154
- /* Prepare inner padding template */
155
- memset(k_pad, 0x36, SHA256_BLOCK_SIZE);
156
- for (i = 0; i < HKDF_SHA256_HASH_LEN; i++) {
157
- k_pad[i] ^= prk[i];
158
- }
159
-
160
- counter = 1;
161
- while (okm_len > 0) {
162
- uint8_t outer_result[SHA256_HASH_SIZE];
163
- uint8_t k_pad_outer[SHA256_BLOCK_SIZE];
164
-
165
- /* Inner hash: H(K XOR ipad || T || info || counter) */
166
- sha256_reset(&ctx);
167
- sha256_update(&ctx, k_pad, SHA256_BLOCK_SIZE);
168
- if (t_len > 0) {
169
- sha256_update(&ctx, T, t_len);
170
- }
171
- if (info_len > 0) {
172
- sha256_update(&ctx, info, info_len);
173
- }
174
- sha256_update(&ctx, &counter, 1);
175
- sha256_finish(&ctx, T);
176
-
177
- /* Prepare outer padding */
178
- memset(k_pad_outer, 0x5c, SHA256_BLOCK_SIZE);
179
- for (i = 0; i < HKDF_SHA256_HASH_LEN; i++) {
180
- k_pad_outer[i] ^= prk[i];
181
- }
182
-
183
- /* Outer hash: H(K XOR opad || inner_hash) */
184
- sha256_reset(&ctx);
185
- sha256_update(&ctx, k_pad_outer, SHA256_BLOCK_SIZE);
186
- sha256_update(&ctx, T, SHA256_HASH_SIZE);
187
- sha256_finish(&ctx, T);
188
-
189
- /* Copy to output */
190
- copy_len = okm_len < SHA256_HASH_SIZE ? okm_len : SHA256_HASH_SIZE;
191
- memcpy(okm, T, copy_len);
192
- okm += copy_len;
193
- okm_len -= copy_len;
194
-
195
- t_len = SHA256_HASH_SIZE;
196
- counter++;
197
-
198
- secure_zero(k_pad_outer, sizeof(k_pad_outer));
199
- }
200
-
201
- secure_zero(k_pad, sizeof(k_pad));
202
- secure_zero(T, sizeof(T));
203
- }
204
-
205
- void hkdf_sha256(
206
- const uint8_t *salt, size_t salt_len,
207
- const uint8_t *ikm, size_t ikm_len,
208
- const uint8_t *info, size_t info_len,
209
- uint8_t *okm, size_t okm_len
210
- ) {
211
- uint8_t prk[HKDF_SHA256_HASH_LEN];
212
-
213
- hkdf_sha256_extract(salt, salt_len, ikm, ikm_len, prk);
214
- hkdf_sha256_expand(prk, info, info_len, okm, okm_len);
215
-
216
- secure_zero(prk, sizeof(prk));
217
- }
218
-
219
- void noise_hkdf_2(
220
- const uint8_t chaining_key[HKDF_SHA256_HASH_LEN],
221
- const uint8_t *input_key, size_t input_len,
222
- uint8_t output1[HKDF_SHA256_HASH_LEN],
223
- uint8_t output2[HKDF_SHA256_HASH_LEN]
224
- ) {
225
- uint8_t temp_key[HKDF_SHA256_HASH_LEN];
226
- uint8_t counter1 = 0x01;
227
- uint8_t counter2 = 0x02;
228
-
229
- /* temp_key = HMAC(ck, input) */
230
- hmac_sha256(chaining_key, HKDF_SHA256_HASH_LEN, input_key, input_len, temp_key);
231
-
232
- /* output1 = HMAC(temp_key, 0x01) */
233
- hmac_sha256(temp_key, HKDF_SHA256_HASH_LEN, &counter1, 1, output1);
234
-
235
- /* output2 = HMAC(temp_key, output1 || 0x02) */
236
- hmac_sha256_2(temp_key, HKDF_SHA256_HASH_LEN, output1, HKDF_SHA256_HASH_LEN, &counter2, 1, output2);
237
-
238
- secure_zero(temp_key, sizeof(temp_key));
239
- }
240
-
241
- void noise_hkdf_3(
242
- const uint8_t chaining_key[HKDF_SHA256_HASH_LEN],
243
- const uint8_t *input_key, size_t input_len,
244
- uint8_t output1[HKDF_SHA256_HASH_LEN],
245
- uint8_t output2[HKDF_SHA256_HASH_LEN],
246
- uint8_t output3[HKDF_SHA256_HASH_LEN]
247
- ) {
248
- uint8_t temp_key[HKDF_SHA256_HASH_LEN];
249
- uint8_t counter1 = 0x01;
250
- uint8_t counter2 = 0x02;
251
- uint8_t counter3 = 0x03;
252
-
253
- /* temp_key = HMAC(ck, input) */
254
- hmac_sha256(chaining_key, HKDF_SHA256_HASH_LEN, input_key, input_len, temp_key);
255
-
256
- /* output1 = HMAC(temp_key, 0x01) */
257
- hmac_sha256(temp_key, HKDF_SHA256_HASH_LEN, &counter1, 1, output1);
258
-
259
- /* output2 = HMAC(temp_key, output1 || 0x02) */
260
- hmac_sha256_2(temp_key, HKDF_SHA256_HASH_LEN, output1, HKDF_SHA256_HASH_LEN, &counter2, 1, output2);
261
-
262
- /* output3 = HMAC(temp_key, output2 || 0x03) */
263
- hmac_sha256_2(temp_key, HKDF_SHA256_HASH_LEN, output2, HKDF_SHA256_HASH_LEN, &counter3, 1, output3);
264
-
265
- secure_zero(temp_key, sizeof(temp_key));
266
- }
@@ -1,36 +0,0 @@
1
- /*
2
- * poly1305-donna
3
- * https://github.com/floodyberry/poly1305-donna
4
- * MIT license / public domain
5
- */
6
-
7
- #ifndef LIBRATS_POLY1305_H
8
- #define LIBRATS_POLY1305_H
9
-
10
- #include <stddef.h>
11
-
12
- #ifdef __cplusplus
13
- extern "C" {
14
- #endif
15
-
16
- #define POLY1305_KEY_SIZE 32
17
- #define POLY1305_TAG_SIZE 16
18
-
19
- typedef struct poly1305_context {
20
- size_t aligner;
21
- unsigned char opaque[136];
22
- } poly1305_context;
23
-
24
- void poly1305_init(poly1305_context *ctx, const unsigned char key[32]);
25
- void poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes);
26
- void poly1305_finish(poly1305_context *ctx, unsigned char mac[16]);
27
- void poly1305_auth(unsigned char mac[16], const unsigned char *m, size_t bytes, const unsigned char key[32]);
28
-
29
- int poly1305_verify(const unsigned char mac1[16], const unsigned char mac2[16]);
30
- int poly1305_power_on_self_test(void);
31
-
32
- #ifdef __cplusplus
33
- }
34
- #endif
35
-
36
- #endif /* LIBRATS_POLY1305_H */