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
@@ -0,0 +1,103 @@
1
+ #pragma once
2
+
3
+ /**
4
+ * @file types.h
5
+ * @brief Core transport identifiers and enums shared across the reactor layer.
6
+ */
7
+
8
+ #include "librats/util/rats_export.h"
9
+
10
+ #include <cstdint>
11
+
12
+ namespace librats {
13
+
14
+ /// Stable-for-lifetime connection handle. Assigned by the owning Reactor and
15
+ /// unique within it; never reused while the connection is alive.
16
+ using ConnId = uint64_t;
17
+ constexpr ConnId kInvalidConnId = 0;
18
+
19
+ /// Opaque timer handle returned by Reactor::schedule(), usable to cancel().
20
+ using TimerId = uint64_t;
21
+ constexpr TimerId kInvalidTimerId = 0;
22
+
23
+ /// Who initiated the connection.
24
+ enum class ConnRole {
25
+ Inbound, ///< We accepted it from a listener.
26
+ Outbound, ///< We dialed out to a remote address.
27
+ };
28
+
29
+ /// Which wire a connection runs over. Tcp and Udp are first-class equals: they
30
+ /// carry the exact same block/frame protocol and the same secure handshake, and
31
+ /// differ only in how an ordered, reliable byte stream is obtained — the kernel's
32
+ /// TCP stack, or the library's own reliability layer on top of datagrams (see
33
+ /// transport/udp_stream.h). Relay is a third way of obtaining that same stream —
34
+ /// out of another peer's connection rather than out of a socket — and is a last
35
+ /// resort rather than an equal (see below).
36
+ enum class TransportKind {
37
+ Tcp, ///< One kernel socket per peer.
38
+ Udp, ///< Reliable ordered stream over the shared UDP socket (NAT-friendly).
39
+ /// Carried inside another peer's connection: the byte stream is chopped into
40
+ /// messages a third node forwards between the two ends (see subsystems/relay.h).
41
+ /// Not a wire of its own — it is one of the two above, one hop further away —
42
+ /// but it behaves differently enough to be worth naming: it costs the relay
43
+ /// bandwidth, it cannot be dialed, and it must always lose to a direct link
44
+ /// (see PeerTable::add). Never a value connect() or the Dialer chooses.
45
+ Relay,
46
+ };
47
+
48
+ /// How hard an outbound datagram dial tries before it is called failed.
49
+ ///
50
+ /// The default is the ordinary dial: three Syns at a doubling 500 ms timeout, so a
51
+ /// path that swallows UDP gives up in about three and a half seconds and the dialer
52
+ /// falls back to TCP promptly.
53
+ ///
54
+ /// A hole punch wants the opposite shape. There the FIRST Syn is expected to die on
55
+ /// the peer's NAT — its job is to open our own mapping and filter — and what
56
+ /// actually connects is a later one, sent after the peer's Syn has crossed ours. So
57
+ /// a punch trades the long patient tail for a dense burst: more attempts, closer
58
+ /// together, no backoff. Everything else about the stream is unchanged; this only
59
+ /// governs the Syn, and once a stream is connected its timeout comes from measured
60
+ /// round trips as usual.
61
+ struct DialProfile {
62
+ int syn_attempts = 3; ///< transmissions of the Syn before the dial fails
63
+ uint32_t syn_rto_ms = 500; ///< interval before the first retransmission
64
+ bool syn_backoff = true; ///< double the interval after each attempt
65
+
66
+ /// The punch shape: eight probes 200 ms apart, ~1.6 s of dense punching.
67
+ static DialProfile punch() noexcept { return DialProfile{8, 200, false}; }
68
+ };
69
+
70
+ /// Connection lifecycle. A connection moves strictly forward through these.
71
+ enum class ConnState {
72
+ Connecting, ///< Outbound transport connect in flight (TCP connect / UDP SYN).
73
+ Handshaking, ///< Transport up; secure-channel handshake in progress.
74
+ Established, ///< Handshake done; application frames may flow.
75
+ Closing, ///< Marked for teardown; no further frames accepted.
76
+ Closed, ///< Removed from the reactor.
77
+ };
78
+
79
+ /// Why a connection was torn down. Surfaced to delegates/observers.
80
+ enum class CloseReason {
81
+ LocalClose, ///< Application asked to disconnect.
82
+ PeerClosed, ///< Remote sent FIN (clean close).
83
+ PeerReset, ///< Connection reset / socket error.
84
+ ConnectFailed, ///< Outbound transport connect never completed.
85
+ HandshakeFailed, ///< Secure-channel handshake failed or timed out.
86
+ ProtocolError, ///< Malformed frame / decryption failure on the wire.
87
+ SlowConsumer, ///< Send buffer exceeded its high-water mark.
88
+ ReactorShutdown, ///< Reactor is stopping.
89
+ DuplicateConn, ///< Redundant connection to a peer we already hold; superseded.
90
+ PeerLimit, ///< Inbound rejected: the configured peer limit is reached.
91
+ IdleTimeout, ///< Datagram link went silent past its idle deadline.
92
+ DialSuperseded, ///< A racing dial over the other transport won; this one is redundant.
93
+ };
94
+
95
+ const char* to_string(ConnState) noexcept;
96
+ const char* to_string(CloseReason) noexcept;
97
+ /// Exported, unlike its siblings above: TransportKind is part of the public
98
+ /// surface (NodeConfig::preferred_transport, PeerInfo::transport), so a consumer
99
+ /// of the shared build needs to be able to render one. ConnState/CloseReason only
100
+ /// ever appear on Connection, which does not cross the library boundary.
101
+ RATS_API const char* to_string(TransportKind) noexcept;
102
+
103
+ } // namespace librats
@@ -0,0 +1,83 @@
1
+ #pragma once
2
+
3
+ /**
4
+ * @file wakeup_pipe.h
5
+ * @brief Loopback-UDP self-pipe for interrupting a blocking select()/receive.
6
+ *
7
+ * A worker thread that blocks in receive_udp_data() can only react to a stop
8
+ * request (or newly posted work) after its socket timeout expires. WakeupPipe
9
+ * provides a second socket to add to that select() set: signal() sends a one-byte
10
+ * datagram to it, which wakes the select() immediately so the worker can react
11
+ * without waiting out the timeout.
12
+ *
13
+ * Two usage patterns are supported:
14
+ * - one-shot (NAT-PMP/UPnP): signal() once on stop(), then tear down. No drain().
15
+ * - repeated (DHT runner): signal() on every posted task. The worker MUST call
16
+ * drain() once per wake-up, otherwise the unread byte keeps select() readable
17
+ * and the loop spins. The socket is non-blocking so drain() never stalls.
18
+ *
19
+ * UDP loopback is used (rather than a pipe) because it is selectable on every
20
+ * platform, including Windows. If socket creation fails the pipe degrades
21
+ * gracefully: fd() returns an invalid socket (treated as "no interrupt"), signal()
22
+ * is a no-op and drain() is a no-op, so callers fall back to timeout-based wakeups.
23
+ */
24
+
25
+ #include "librats/core/socket.h"
26
+
27
+ #include <vector>
28
+
29
+ namespace librats {
30
+
31
+ class WakeupPipe {
32
+ public:
33
+ WakeupPipe() {
34
+ sock_ = create_udp_socket(0, "127.0.0.1", AddressFamily::IPv4);
35
+ if (is_valid_socket(sock_)) {
36
+ sockaddr_in addr;
37
+ socklen_t len = sizeof(addr);
38
+ if (getsockname(sock_, reinterpret_cast<sockaddr*>(&addr), &len) == 0) {
39
+ port_ = ntohs(addr.sin_port);
40
+ }
41
+ set_socket_nonblocking(sock_); // so drain() reads until empty without blocking
42
+ }
43
+ }
44
+
45
+ ~WakeupPipe() {
46
+ if (is_valid_socket(sock_)) close_socket(sock_);
47
+ }
48
+
49
+ WakeupPipe(const WakeupPipe&) = delete;
50
+ WakeupPipe& operator=(const WakeupPipe&) = delete;
51
+
52
+ /// Socket to add to a select() set (pass as receive_udp_data's interrupt_fd).
53
+ socket_t fd() const { return sock_; }
54
+
55
+ /// Wake any select() watching fd(). Safe to call from any thread (a one-byte UDP
56
+ /// send), and as often as needed — drain() consumes the accumulated bytes.
57
+ void signal() {
58
+ if (is_valid_socket(sock_) && port_ != 0) {
59
+ send_udp_data(sock_, std::vector<uint8_t>{1}, "127.0.0.1", port_, AddressFamily::IPv4);
60
+ }
61
+ }
62
+
63
+ /// Discard all pending wakeup bytes so the next select() blocks again. Call once
64
+ /// after each wake-up, from the waiting thread only. No-op if the pipe is degraded.
65
+ void drain() {
66
+ if (!is_valid_socket(sock_)) return;
67
+ uint8_t buf[64];
68
+ sockaddr_in from;
69
+ socklen_t len;
70
+ for (;;) {
71
+ len = sizeof(from);
72
+ const int n = recvfrom(sock_, reinterpret_cast<char*>(buf), sizeof(buf), 0,
73
+ reinterpret_cast<sockaddr*>(&from), &len);
74
+ if (n <= 0) break; // EWOULDBLOCK / drained (socket is non-blocking)
75
+ }
76
+ }
77
+
78
+ private:
79
+ socket_t sock_ = RATS_INVALID_SOCKET;
80
+ uint16_t port_ = 0;
81
+ };
82
+
83
+ } // namespace librats
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: MIT */
1
2
  /*
2
3
  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3
4
  *
@@ -23,35 +24,32 @@
23
24
  #ifndef LIBRATS_BLAKE2_ENDIAN_H
24
25
  #define LIBRATS_BLAKE2_ENDIAN_H
25
26
 
26
- #if defined(__WIN32__) || defined(WIN32) || defined(_WIN32)
27
- #ifndef __BIG_ENDIAN
28
- #define __BIG_ENDIAN 4321
29
- #endif
30
- #ifndef __LITTLE_ENDIAN
31
- #define __LITTLE_ENDIAN 1234
32
- #endif
33
- #ifndef __BYTE_ORDER
34
- #define __BYTE_ORDER __LITTLE_ENDIAN
35
- #endif
27
+ /*
28
+ * Defines RATS_BLAKE2_LITTLE_ENDIAN when the target is little-endian, and
29
+ * nothing else. This header is installed, so it must NOT define __BYTE_ORDER /
30
+ * __LITTLE_ENDIAN / __BIG_ENDIAN: those names are reserved to the platform, and
31
+ * defining them here leaks into every consumer translation unit that includes
32
+ * us. Detect the byte order instead of asserting it.
33
+ */
34
+
35
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
36
+ /* GCC and Clang predefine these on every target — no system header needed. */
37
+ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
38
+ #define RATS_BLAKE2_LITTLE_ENDIAN 1
39
+ #endif
40
+ #elif defined(_WIN32)
41
+ /* MSVC predefines no byte-order macro; every Windows target it ships is LE. */
42
+ #define RATS_BLAKE2_LITTLE_ENDIAN 1
36
43
  #elif defined(__APPLE__)
37
44
  #include <machine/endian.h>
38
- #if !defined( __BYTE_ORDER) && defined(__DARWIN_BYTE_ORDER)
39
- #define __BYTE_ORDER __DARWIN_BYTE_ORDER
45
+ #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
46
+ #define RATS_BLAKE2_LITTLE_ENDIAN 1
40
47
  #endif
41
- #if !defined( __BIG_ENDIAN) && defined(__DARWIN_BIG_ENDIAN)
42
- #define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
43
- #endif
44
- #if !defined( __LITTLE_ENDIAN) && defined(__DARWIN_LITTLE_ENDIAN)
45
- #define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
46
- #endif
47
- #elif defined(__ANDROID__)
48
- #include <endian.h>
49
48
  #else
50
49
  #include <endian.h>
51
- #endif
52
-
53
50
  #if __BYTE_ORDER == __LITTLE_ENDIAN
54
- #define BLAKE2_LITTLE_ENDIAN 1
51
+ #define RATS_BLAKE2_LITTLE_ENDIAN 1
52
+ #endif
55
53
  #endif
56
54
 
57
55
  #endif /* LIBRATS_BLAKE2_ENDIAN_H */
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: MIT */
1
2
  /*
2
3
  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3
4
  *
@@ -26,30 +27,30 @@
26
27
  https://github.com/rweather/arduinolibs
27
28
  */
28
29
 
29
- #include "blake2b.h"
30
- #include "blake2_endian.h"
30
+ #include "librats/crypto/blake2b.h"
31
+ #include "librats/crypto/blake2_endian.h"
31
32
  #include <string.h>
32
33
 
33
34
  /* Initialization vectors for BLAKE2b */
34
- #define BLAKE2b_IV0 0x6a09e667f3bcc908ULL
35
- #define BLAKE2b_IV1 0xbb67ae8584caa73bULL
36
- #define BLAKE2b_IV2 0x3c6ef372fe94f82bULL
37
- #define BLAKE2b_IV3 0xa54ff53a5f1d36f1ULL
38
- #define BLAKE2b_IV4 0x510e527fade682d1ULL
39
- #define BLAKE2b_IV5 0x9b05688c2b3e6c1fULL
40
- #define BLAKE2b_IV6 0x1f83d9abfb41bd6bULL
41
- #define BLAKE2b_IV7 0x5be0cd19137e2179ULL
42
-
43
- void BLAKE2b_reset(BLAKE2b_context_t *context)
35
+ #define rats_blake2b_IV0 0x6a09e667f3bcc908ULL
36
+ #define rats_blake2b_IV1 0xbb67ae8584caa73bULL
37
+ #define rats_blake2b_IV2 0x3c6ef372fe94f82bULL
38
+ #define rats_blake2b_IV3 0xa54ff53a5f1d36f1ULL
39
+ #define rats_blake2b_IV4 0x510e527fade682d1ULL
40
+ #define rats_blake2b_IV5 0x9b05688c2b3e6c1fULL
41
+ #define rats_blake2b_IV6 0x1f83d9abfb41bd6bULL
42
+ #define rats_blake2b_IV7 0x5be0cd19137e2179ULL
43
+
44
+ void rats_blake2b_reset(rats_blake2b_context_t *context)
44
45
  {
45
- context->h[0] = BLAKE2b_IV0 ^ 0x01010040; /* Default output length of 64 */
46
- context->h[1] = BLAKE2b_IV1;
47
- context->h[2] = BLAKE2b_IV2;
48
- context->h[3] = BLAKE2b_IV3;
49
- context->h[4] = BLAKE2b_IV4;
50
- context->h[5] = BLAKE2b_IV5;
51
- context->h[6] = BLAKE2b_IV6;
52
- context->h[7] = BLAKE2b_IV7;
46
+ context->h[0] = rats_blake2b_IV0 ^ 0x01010040; /* Default output length of 64 */
47
+ context->h[1] = rats_blake2b_IV1;
48
+ context->h[2] = rats_blake2b_IV2;
49
+ context->h[3] = rats_blake2b_IV3;
50
+ context->h[4] = rats_blake2b_IV4;
51
+ context->h[5] = rats_blake2b_IV5;
52
+ context->h[6] = rats_blake2b_IV6;
53
+ context->h[7] = rats_blake2b_IV7;
53
54
  context->length = 0;
54
55
  context->posn = 0;
55
56
  }
@@ -87,7 +88,7 @@ static const uint8_t sigma[12][16] = {
87
88
  } while (0)
88
89
 
89
90
  static void blake2b_transform
90
- (BLAKE2b_context_t *context, const uint8_t *data, uint64_t f0)
91
+ (rats_blake2b_context_t *context, const uint8_t *data, uint64_t f0)
91
92
  {
92
93
  uint8_t index;
93
94
  uint64_t m[16];
@@ -95,7 +96,7 @@ static void blake2b_transform
95
96
  const uint8_t *sigma_row;
96
97
 
97
98
  /* Unpack the input data from little-endian */
98
- #if BLAKE2_LITTLE_ENDIAN
99
+ #if RATS_BLAKE2_LITTLE_ENDIAN
99
100
  memcpy(m, data, sizeof(m));
100
101
  #else
101
102
  for (index = 0; index < 16; ++index, data += 8) {
@@ -112,14 +113,14 @@ static void blake2b_transform
112
113
 
113
114
  /* Format the block to be hashed */
114
115
  memcpy(v, context->h, sizeof(context->h));
115
- v[8] = BLAKE2b_IV0;
116
- v[9] = BLAKE2b_IV1;
117
- v[10] = BLAKE2b_IV2;
118
- v[11] = BLAKE2b_IV3;
119
- v[12] = BLAKE2b_IV4 ^ context->length;
120
- v[13] = BLAKE2b_IV5;
121
- v[14] = BLAKE2b_IV6 ^ f0;
122
- v[15] = BLAKE2b_IV7;
116
+ v[8] = rats_blake2b_IV0;
117
+ v[9] = rats_blake2b_IV1;
118
+ v[10] = rats_blake2b_IV2;
119
+ v[11] = rats_blake2b_IV3;
120
+ v[12] = rats_blake2b_IV4 ^ context->length;
121
+ v[13] = rats_blake2b_IV5;
122
+ v[14] = rats_blake2b_IV6 ^ f0;
123
+ v[15] = rats_blake2b_IV7;
123
124
 
124
125
  /* Perform the 12 BLAKE2b rounds */
125
126
  sigma_row = sigma[0];
@@ -142,7 +143,7 @@ static void blake2b_transform
142
143
  context->h[index] ^= (v[index] ^ v[index + 8]);
143
144
  }
144
145
 
145
- void BLAKE2b_update(BLAKE2b_context_t *context, const void *data, size_t size)
146
+ void rats_blake2b_update(rats_blake2b_context_t *context, const void *data, size_t size)
146
147
  {
147
148
  /* Break the input up into 1024-bit chunks and process each in turn */
148
149
  const uint8_t *d = (const uint8_t *)data;
@@ -174,14 +175,14 @@ void BLAKE2b_update(BLAKE2b_context_t *context, const void *data, size_t size)
174
175
  }
175
176
  }
176
177
 
177
- void BLAKE2b_finish(BLAKE2b_context_t *context, uint8_t *hash)
178
+ void rats_blake2b_finish(rats_blake2b_context_t *context, uint8_t *hash)
178
179
  {
179
180
  /* Pad the last chunk and hash it with f0 set to all-ones */
180
181
  memset(context->m + context->posn, 0, 128 - context->posn);
181
182
  blake2b_transform(context, context->m, 0xFFFFFFFFFFFFFFFFULL);
182
183
 
183
184
  /* Copy the hash to the caller's return buffer in little-endian */
184
- #if BLAKE2_LITTLE_ENDIAN
185
+ #if RATS_BLAKE2_LITTLE_ENDIAN
185
186
  memcpy(hash, context->h, sizeof(context->h));
186
187
  #else
187
188
  {
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: MIT */
1
2
  /*
2
3
  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3
4
  *
@@ -30,8 +31,8 @@
30
31
  extern "C" {
31
32
  #endif
32
33
 
33
- #define BLAKE2B_HASH_SIZE 64
34
- #define BLAKE2B_BLOCK_SIZE 128
34
+ #define RATS_BLAKE2B_HASH_SIZE 64
35
+ #define RATS_BLAKE2B_BLOCK_SIZE 128
35
36
 
36
37
  typedef struct
37
38
  {
@@ -40,11 +41,11 @@ typedef struct
40
41
  uint64_t length; /* Limited to 2^64 - 1 bytes */
41
42
  uint8_t posn;
42
43
 
43
- } BLAKE2b_context_t;
44
+ } rats_blake2b_context_t;
44
45
 
45
- void BLAKE2b_reset(BLAKE2b_context_t *context);
46
- void BLAKE2b_update(BLAKE2b_context_t *context, const void *data, size_t size);
47
- void BLAKE2b_finish(BLAKE2b_context_t *context, uint8_t *hash);
46
+ void rats_blake2b_reset(rats_blake2b_context_t *context);
47
+ void rats_blake2b_update(rats_blake2b_context_t *context, const void *data, size_t size);
48
+ void rats_blake2b_finish(rats_blake2b_context_t *context, uint8_t *hash);
48
49
 
49
50
  #ifdef __cplusplus
50
51
  }
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: MIT */
1
2
  /*
2
3
  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3
4
  *
@@ -27,36 +28,36 @@
27
28
  https://github.com/rweather/arduinolibs
28
29
  */
29
30
 
30
- #include "blake2s.h"
31
- #include "blake2_endian.h"
31
+ #include "librats/crypto/blake2s.h"
32
+ #include "librats/crypto/blake2_endian.h"
32
33
  #include <string.h>
33
34
 
34
35
  /* Initialization vectors for BLAKE2s */
35
- #define BLAKE2s_IV0 0x6A09E667
36
- #define BLAKE2s_IV1 0xBB67AE85
37
- #define BLAKE2s_IV2 0x3C6EF372
38
- #define BLAKE2s_IV3 0xA54FF53A
39
- #define BLAKE2s_IV4 0x510E527F
40
- #define BLAKE2s_IV5 0x9B05688C
41
- #define BLAKE2s_IV6 0x1F83D9AB
42
- #define BLAKE2s_IV7 0x5BE0CD19
36
+ #define rats_blake2s_IV0 0x6A09E667
37
+ #define rats_blake2s_IV1 0xBB67AE85
38
+ #define rats_blake2s_IV2 0x3C6EF372
39
+ #define rats_blake2s_IV3 0xA54FF53A
40
+ #define rats_blake2s_IV4 0x510E527F
41
+ #define rats_blake2s_IV5 0x9B05688C
42
+ #define rats_blake2s_IV6 0x1F83D9AB
43
+ #define rats_blake2s_IV7 0x5BE0CD19
43
44
 
44
- void BLAKE2s_reset(BLAKE2s_context_t *context)
45
+ void rats_blake2s_reset(rats_blake2s_context_t *context)
45
46
  {
46
- #if BLAKE2S_USE_VECTOR_MATH
47
- context->h[0] = (BlakeVectorUInt32){BLAKE2s_IV0 ^ 0x01010020,
48
- BLAKE2s_IV1, BLAKE2s_IV2, BLAKE2s_IV3};
49
- context->h[1] = (BlakeVectorUInt32){BLAKE2s_IV4, BLAKE2s_IV5,
50
- BLAKE2s_IV6, BLAKE2s_IV7};
47
+ #if RATS_BLAKE2S_USE_VECTOR_MATH
48
+ context->h[0] = (RatsBlakeVectorUInt32){rats_blake2s_IV0 ^ 0x01010020,
49
+ rats_blake2s_IV1, rats_blake2s_IV2, rats_blake2s_IV3};
50
+ context->h[1] = (RatsBlakeVectorUInt32){rats_blake2s_IV4, rats_blake2s_IV5,
51
+ rats_blake2s_IV6, rats_blake2s_IV7};
51
52
  #else
52
- context->h[0] = BLAKE2s_IV0 ^ 0x01010020; /* Default output length of 32 */
53
- context->h[1] = BLAKE2s_IV1;
54
- context->h[2] = BLAKE2s_IV2;
55
- context->h[3] = BLAKE2s_IV3;
56
- context->h[4] = BLAKE2s_IV4;
57
- context->h[5] = BLAKE2s_IV5;
58
- context->h[6] = BLAKE2s_IV6;
59
- context->h[7] = BLAKE2s_IV7;
53
+ context->h[0] = rats_blake2s_IV0 ^ 0x01010020; /* Default output length of 32 */
54
+ context->h[1] = rats_blake2s_IV1;
55
+ context->h[2] = rats_blake2s_IV2;
56
+ context->h[3] = rats_blake2s_IV3;
57
+ context->h[4] = rats_blake2s_IV4;
58
+ context->h[5] = rats_blake2s_IV5;
59
+ context->h[6] = rats_blake2s_IV6;
60
+ context->h[7] = rats_blake2s_IV7;
60
61
  #endif
61
62
  context->length = 0;
62
63
  context->posn = 0;
@@ -92,11 +93,11 @@ static const uint8_t sigma[10][16] = {
92
93
  (b) = rightRotate((b) ^ (c), 7); \
93
94
  } while (0)
94
95
 
95
- #if BLAKE2S_USE_VECTOR_MATH
96
+ #if RATS_BLAKE2S_USE_VECTOR_MATH
96
97
 
97
- #define shuffleLeft1(x) (BlakeVectorUInt32){(x)[1], (x)[2], (x)[3], (x)[0]}
98
- #define shuffleLeft2(x) (BlakeVectorUInt32){(x)[2], (x)[3], (x)[0], (x)[1]}
99
- #define shuffleLeft3(x) (BlakeVectorUInt32){(x)[3], (x)[0], (x)[1], (x)[2]}
98
+ #define shuffleLeft1(x) (RatsBlakeVectorUInt32){(x)[1], (x)[2], (x)[3], (x)[0]}
99
+ #define shuffleLeft2(x) (RatsBlakeVectorUInt32){(x)[2], (x)[3], (x)[0], (x)[1]}
100
+ #define shuffleLeft3(x) (RatsBlakeVectorUInt32){(x)[3], (x)[0], (x)[1], (x)[2]}
100
101
 
101
102
  /* Perform a BLAKE2s quarter round operation with vector math */
102
103
  #define quarterRoundVec(a, b, c, d, mv0, mv1) \
@@ -114,31 +115,31 @@ static const uint8_t sigma[10][16] = {
114
115
  #endif
115
116
 
116
117
  static void blake2s_transform
117
- (BLAKE2s_context_t *context, const uint8_t *data, uint32_t f0)
118
+ (rats_blake2s_context_t *context, const uint8_t *data, uint32_t f0)
118
119
  {
119
- #if BLAKE2S_USE_VECTOR_MATH
120
+ #if RATS_BLAKE2S_USE_VECTOR_MATH
120
121
  /* Assumption: CPU is little-endian and supports unaligned 32-bit loads */
121
122
  uint8_t index;
122
123
  const uint32_t *m = (const uint32_t *)data;
123
- BlakeVectorUInt32 v0, v1, v2, v3, mv0, mv1;
124
+ RatsBlakeVectorUInt32 v0, v1, v2, v3, mv0, mv1;
124
125
  const uint8_t *sigma_row;
125
126
 
126
127
  /* Format the block to be hashed */
127
128
  v0 = context->h[0];
128
129
  v1 = context->h[1];
129
- v2 = (BlakeVectorUInt32){BLAKE2s_IV0, BLAKE2s_IV1,
130
- BLAKE2s_IV2, BLAKE2s_IV3};
131
- v3 = (BlakeVectorUInt32){BLAKE2s_IV4 ^ (uint32_t)(context->length),
132
- BLAKE2s_IV5 ^ (uint32_t)(context->length >> 32),
133
- BLAKE2s_IV6 ^ f0, BLAKE2s_IV7};
130
+ v2 = (RatsBlakeVectorUInt32){rats_blake2s_IV0, rats_blake2s_IV1,
131
+ rats_blake2s_IV2, rats_blake2s_IV3};
132
+ v3 = (RatsBlakeVectorUInt32){rats_blake2s_IV4 ^ (uint32_t)(context->length),
133
+ rats_blake2s_IV5 ^ (uint32_t)(context->length >> 32),
134
+ rats_blake2s_IV6 ^ f0, rats_blake2s_IV7};
134
135
 
135
136
  /* Perform the 10 BLAKE2s rounds */
136
137
  sigma_row = sigma[0];
137
138
  for (index = 0; index < 10; ++index, sigma_row += 16) {
138
139
  /* Column round */
139
- mv0 = (BlakeVectorUInt32){m[sigma_row[0]], m[sigma_row[2]],
140
+ mv0 = (RatsBlakeVectorUInt32){m[sigma_row[0]], m[sigma_row[2]],
140
141
  m[sigma_row[4]], m[sigma_row[6]]};
141
- mv1 = (BlakeVectorUInt32){m[sigma_row[1]], m[sigma_row[3]],
142
+ mv1 = (RatsBlakeVectorUInt32){m[sigma_row[1]], m[sigma_row[3]],
142
143
  m[sigma_row[5]], m[sigma_row[7]]};
143
144
  quarterRoundVec(v0, v1, v2, v3, mv0, mv1);
144
145
  v1 = shuffleLeft1(v1);
@@ -146,9 +147,9 @@ static void blake2s_transform
146
147
  v3 = shuffleLeft3(v3);
147
148
 
148
149
  /* Diagonal round */
149
- mv0 = (BlakeVectorUInt32){m[sigma_row[8]], m[sigma_row[10]],
150
+ mv0 = (RatsBlakeVectorUInt32){m[sigma_row[8]], m[sigma_row[10]],
150
151
  m[sigma_row[12]], m[sigma_row[14]]};
151
- mv1 = (BlakeVectorUInt32){m[sigma_row[9]], m[sigma_row[11]],
152
+ mv1 = (RatsBlakeVectorUInt32){m[sigma_row[9]], m[sigma_row[11]],
152
153
  m[sigma_row[13]], m[sigma_row[15]]};
153
154
  quarterRoundVec(v0, v1, v2, v3, mv0, mv1);
154
155
  v1 = shuffleLeft3(v1);
@@ -159,14 +160,14 @@ static void blake2s_transform
159
160
  /* Combine the new and old hash values */
160
161
  context->h[0] ^= v0 ^ v2;
161
162
  context->h[1] ^= v1 ^ v3;
162
- #else /* !BLAKE2S_USE_VECTOR_MATH */
163
+ #else /* !RATS_BLAKE2S_USE_VECTOR_MATH */
163
164
  uint8_t index;
164
165
  uint32_t m[16];
165
166
  uint32_t v[16];
166
167
  const uint8_t *sigma_row;
167
168
 
168
169
  /* Unpack the input data from little-endian */
169
- #if BLAKE2_LITTLE_ENDIAN
170
+ #if RATS_BLAKE2_LITTLE_ENDIAN
170
171
  memcpy(m, data, sizeof(m));
171
172
  #else
172
173
  for (index = 0; index < 16; ++index, data += 4) {
@@ -179,14 +180,14 @@ static void blake2s_transform
179
180
 
180
181
  /* Format the block to be hashed */
181
182
  memcpy(v, context->h, sizeof(context->h));
182
- v[8] = BLAKE2s_IV0;
183
- v[9] = BLAKE2s_IV1;
184
- v[10] = BLAKE2s_IV2;
185
- v[11] = BLAKE2s_IV3;
186
- v[12] = BLAKE2s_IV4 ^ (uint32_t)(context->length);
187
- v[13] = BLAKE2s_IV5 ^ (uint32_t)(context->length >> 32);
188
- v[14] = BLAKE2s_IV6 ^ f0;
189
- v[15] = BLAKE2s_IV7;
183
+ v[8] = rats_blake2s_IV0;
184
+ v[9] = rats_blake2s_IV1;
185
+ v[10] = rats_blake2s_IV2;
186
+ v[11] = rats_blake2s_IV3;
187
+ v[12] = rats_blake2s_IV4 ^ (uint32_t)(context->length);
188
+ v[13] = rats_blake2s_IV5 ^ (uint32_t)(context->length >> 32);
189
+ v[14] = rats_blake2s_IV6 ^ f0;
190
+ v[15] = rats_blake2s_IV7;
190
191
 
191
192
  /* Perform the 10 BLAKE2s rounds */
192
193
  sigma_row = sigma[0];
@@ -207,10 +208,10 @@ static void blake2s_transform
207
208
  /* Combine the new and old hash values */
208
209
  for (index = 0; index < 8; ++index)
209
210
  context->h[index] ^= (v[index] ^ v[index + 8]);
210
- #endif /* !BLAKE2S_USE_VECTOR_MATH */
211
+ #endif /* !RATS_BLAKE2S_USE_VECTOR_MATH */
211
212
  }
212
213
 
213
- void BLAKE2s_update(BLAKE2s_context_t *context, const void *data, size_t size)
214
+ void rats_blake2s_update(rats_blake2s_context_t *context, const void *data, size_t size)
214
215
  {
215
216
  /* Break the input up into 512-bit chunks and process each in turn */
216
217
  const uint8_t *d = (const uint8_t *)data;
@@ -242,14 +243,14 @@ void BLAKE2s_update(BLAKE2s_context_t *context, const void *data, size_t size)
242
243
  }
243
244
  }
244
245
 
245
- void BLAKE2s_finish(BLAKE2s_context_t *context, uint8_t *hash)
246
+ void rats_blake2s_finish(rats_blake2s_context_t *context, uint8_t *hash)
246
247
  {
247
248
  /* Pad the last chunk and hash it with f0 set to all-ones */
248
249
  memset(context->m + context->posn, 0, 64 - context->posn);
249
250
  blake2s_transform(context, context->m, 0xFFFFFFFF);
250
251
 
251
252
  /* Copy the hash to the caller's return buffer in little-endian */
252
- #if BLAKE2_LITTLE_ENDIAN
253
+ #if RATS_BLAKE2_LITTLE_ENDIAN
253
254
  memcpy(hash, context->h, sizeof(context->h));
254
255
  #else
255
256
  {
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: MIT */
1
2
  /*
2
3
  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3
4
  *
@@ -30,24 +31,24 @@
30
31
  extern "C" {
31
32
  #endif
32
33
 
33
- #define BLAKE2S_HASH_SIZE 32
34
- #define BLAKE2S_BLOCK_SIZE 64
34
+ #define RATS_BLAKE2S_HASH_SIZE 32
35
+ #define RATS_BLAKE2S_BLOCK_SIZE 64
35
36
 
36
37
  #if defined(__SSE2__) && defined(__GNUC__) && __GNUC__ >= 4
37
- #define BLAKE2S_USE_VECTOR_MATH 1
38
+ #define RATS_BLAKE2S_USE_VECTOR_MATH 1
38
39
  #ifdef __clang__
39
- typedef uint32_t BlakeVectorUInt32 __attribute__((ext_vector_type(4)));
40
+ typedef uint32_t RatsBlakeVectorUInt32 __attribute__((ext_vector_type(4)));
40
41
  #else
41
- typedef uint32_t BlakeVectorUInt32 __attribute__((__vector_size__(16)));
42
+ typedef uint32_t RatsBlakeVectorUInt32 __attribute__((__vector_size__(16)));
42
43
  #endif
43
44
  #else
44
- #undef BLAKE2S_USE_VECTOR_MATH
45
+ #undef RATS_BLAKE2S_USE_VECTOR_MATH
45
46
  #endif
46
47
 
47
48
  typedef struct
48
49
  {
49
- #if BLAKE2S_USE_VECTOR_MATH
50
- BlakeVectorUInt32 h[2];
50
+ #if RATS_BLAKE2S_USE_VECTOR_MATH
51
+ RatsBlakeVectorUInt32 h[2];
51
52
  #else
52
53
  uint32_t h[8];
53
54
  #endif
@@ -55,11 +56,11 @@ typedef struct
55
56
  uint64_t length;
56
57
  uint8_t posn;
57
58
 
58
- } BLAKE2s_context_t;
59
+ } rats_blake2s_context_t;
59
60
 
60
- void BLAKE2s_reset(BLAKE2s_context_t *context);
61
- void BLAKE2s_update(BLAKE2s_context_t *context, const void *data, size_t size);
62
- void BLAKE2s_finish(BLAKE2s_context_t *context, uint8_t *hash);
61
+ void rats_blake2s_reset(rats_blake2s_context_t *context);
62
+ void rats_blake2s_update(rats_blake2s_context_t *context, const void *data, size_t size);
63
+ void rats_blake2s_finish(rats_blake2s_context_t *context, uint8_t *hash);
63
64
 
64
65
  #ifdef __cplusplus
65
66
  }