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,380 @@
1
+ #pragma once
2
+
3
+ /**
4
+ * @file rats.h
5
+ * @brief The canonical C ABI over Node — the foundation for all language bindings.
6
+ *
7
+ * Opaque-pointer style. A `rats_t` wraps a C++ Node. Strings returned by the
8
+ * library (e.g. peer ids) are heap-allocated and must be released with
9
+ * rats_string_free(). Peer ids are 64-char lowercase hex of the peer's
10
+ * self-certifying PeerId.
11
+ *
12
+ * Error model: fallible operations return a `rats_error_t` (RATS_OK == 0 on
13
+ * success). Pure getters return their value directly. Use rats_error_str() for a
14
+ * static human-readable name. Delivery of messages is asynchronous and
15
+ * best-effort: a RATS_OK from rats_send()/rats_publish() means the request was
16
+ * accepted and queued, not that a peer received it.
17
+ *
18
+ * Subsystems are explicit and opt-in: discovery, pub/sub, typed messaging, file
19
+ * transfer and ping must each be turned on with the matching rats_enable_*()
20
+ * BEFORE rats_start(). Calling a subsystem function before its enable returns
21
+ * RATS_ERR_NOT_ENABLED; calling an enable after start returns
22
+ * RATS_ERR_ALREADY_STARTED.
23
+ *
24
+ * Threading: callbacks fire on an internal reactor thread — do not block in them.
25
+ * Register callbacks and enable subsystems BEFORE rats_start().
26
+ */
27
+
28
+ #include "librats/util/rats_export.h"
29
+
30
+ #include <stddef.h>
31
+ #include <stdint.h>
32
+
33
+ #ifdef __cplusplus
34
+ extern "C" {
35
+ #endif
36
+
37
+ typedef void* rats_t;
38
+
39
+ typedef enum {
40
+ RATS_SECURITY_NOISE = 0, /* Noise XX, encrypted + authenticated (default) */
41
+ RATS_SECURITY_PLAINTEXT = 1 /* unencrypted, ids exchanged in the clear */
42
+ } rats_security_t;
43
+
44
+ /* Which wire a peer connection runs on. Both carry the identical protocol and
45
+ * the identical encrypted handshake; they differ only in how the ordered,
46
+ * reliable byte stream underneath is obtained. */
47
+ typedef enum {
48
+ RATS_TRANSPORT_TCP = 0, /* one kernel socket per peer */
49
+ RATS_TRANSPORT_UDP = 1, /* reliable stream over the shared UDP socket */
50
+ /* Carried inside another peer's connection (see rats_enable_relay). Reported
51
+ by rats_peer_transport(); never a value to put in preferred_transport,
52
+ which chooses what a DIAL tries and a relay is never dialed. */
53
+ RATS_TRANSPORT_RELAY = 2
54
+ } rats_transport_t;
55
+
56
+ /* Bitmask of transports (see rats_transports / rats_peer_transports). */
57
+ #define RATS_TRANSPORT_MASK_TCP 0x1u
58
+ #define RATS_TRANSPORT_MASK_UDP 0x2u
59
+
60
+ /* Result of a fallible operation. RATS_OK is 0; any non-zero value is an error.
61
+ * NOTE: this inverts the truthiness of the old int-returning calls — test against
62
+ * RATS_OK, not against zero/non-zero, e.g. `if (rats_start(n) != RATS_OK) …`. */
63
+ typedef enum {
64
+ RATS_OK = 0,
65
+ RATS_ERR_INVALID_ARG = 1, /* null/malformed argument (bad peer id, null ptr, bad json) */
66
+ RATS_ERR_NOT_STARTED = 2, /* operation requires a started node */
67
+ RATS_ERR_ALREADY_STARTED = 3, /* enable/attach called after rats_start() */
68
+ RATS_ERR_NOT_ENABLED = 4, /* subsystem not enabled — call the matching rats_enable_* */
69
+ RATS_ERR_NO_SUCH_PEER = 5, /* peer not connected, or transfer id not found */
70
+ RATS_ERR_BIND = 6, /* listen/bind failed during rats_start() */
71
+ RATS_ERR_INTERNAL = 7
72
+ } rats_error_t;
73
+
74
+ /** Static human-readable name of an error code (do not free). */
75
+ RATS_API const char* rats_error_str(rats_error_t err);
76
+
77
+ /* — construction / lifecycle — */
78
+
79
+ /** Full node configuration. Obtain a sane-defaults instance with
80
+ * rats_config_default(), set the fields you care about, then pass it to
81
+ * rats_create_config(). Zero-initialising this struct yourself is NOT safe
82
+ * (enable_listen would be 0 → a dial-only node); always start from the default.
83
+ * String fields are borrowed only for the duration of the create call; NULL on
84
+ * any of them selects the library default shown below. */
85
+ typedef struct {
86
+ uint16_t listen_port; /* inbound port; 0 = ephemeral */
87
+ int enable_listen; /* 0 = dial-only node (no listener) */
88
+ const char* bind_address; /* NULL → "::" dual-stack wildcard */
89
+ rats_security_t security; /* RATS_SECURITY_NOISE / _PLAINTEXT */
90
+ const char* data_dir; /* persistent state dir; NULL/"" → ephemeral identity each run */
91
+ const char* protocol; /* handshake app id (e.g. "myapp/1.0"); NULL → "librats/1.0" */
92
+ size_t max_peers; /* established-peer cap; 0 = unlimited */
93
+
94
+ /* Transports. Both are enabled by default and bind the SAME port, so one
95
+ * advertised address is dialable either way. */
96
+ int enable_tcp; /* accept and dial over TCP (default 1) */
97
+ int enable_udp; /* accept and dial over UDP (default 1) */
98
+ /* Both zero means "both enabled", not "neither": a node with no transport can
99
+ * neither dial nor accept, so nobody asks for one on purpose — while zeroed
100
+ * memory (rats_config_t cfg = {0};) asks for it by accident. Prefer
101
+ * rats_config_default() and change what you need. */
102
+ rats_transport_t preferred_transport; /* tried first when dialing (default UDP) */
103
+ uint32_t transport_fallback_ms; /* delay before racing the other transport;
104
+ * 0 = never fall back (default 1200) */
105
+ } rats_config_t;
106
+
107
+ /** A config pre-filled with the library defaults (listening, Noise, ephemeral
108
+ * identity, unlimited peers). Mutate the returned struct and pass to
109
+ * rats_create_config(). */
110
+ RATS_API rats_config_t rats_config_default(void);
111
+
112
+ /** Create a node from a full config (NULL → all defaults). When data_dir is set,
113
+ * the identity persists across restarts and subsystems (DHT routing table,
114
+ * reconnection store) co-locate their state there. */
115
+ RATS_API rats_t rats_create_config(const rats_config_t* config);
116
+
117
+ /** Create a listening node (Noise, dual-stack, ephemeral identity, port 0 = ephemeral). */
118
+ RATS_API rats_t rats_create(uint16_t listen_port);
119
+
120
+ /** Create with basic control. enable_listen=0 makes a dial-only node. For
121
+ * data_dir / protocol identity / max_peers use rats_create_config(). */
122
+ RATS_API rats_t rats_create_ex(uint16_t listen_port, int enable_listen,
123
+ const char* bind_address, rats_security_t security);
124
+
125
+ RATS_API void rats_destroy(rats_t node);
126
+ RATS_API rats_error_t rats_start(rats_t node); /* RATS_OK / RATS_ERR_ALREADY_STARTED / RATS_ERR_BIND */
127
+ RATS_API void rats_stop(rats_t node);
128
+ RATS_API uint16_t rats_listen_port(rats_t node);
129
+
130
+ /** Transports this node is actually running, as a RATS_TRANSPORT_MASK_* bitmask.
131
+ * May be narrower than the config asked for: a UDP socket that could not be
132
+ * bound leaves the node TCP-only rather than failing to start. 0 before
133
+ * rats_start() and after rats_stop(). */
134
+ RATS_API uint32_t rats_transports(rats_t node);
135
+
136
+ /** Our self-certifying peer id as hex. Caller frees with rats_string_free(). */
137
+ RATS_API char* rats_local_id(rats_t node);
138
+
139
+ /** Application protocol identity bound into the handshake (see rats_config_t).
140
+ * Two nodes whose protocol differs cannot complete a handshake. Caller frees
141
+ * the returned string with rats_string_free(). */
142
+ RATS_API char* rats_protocol(rats_t node);
143
+
144
+ /* — connections — */
145
+
146
+ RATS_API rats_error_t rats_connect(rats_t node, const char* host, uint16_t port);
147
+ RATS_API size_t rats_peer_count(rats_t node);
148
+
149
+ /** Cap on established peers (0 = unlimited). Guards inbound; our dials are honored.
150
+ * May be set before or after start(). */
151
+ RATS_API void rats_set_max_peers(rats_t node, size_t max_peers);
152
+ RATS_API size_t rats_max_peers(rats_t node);
153
+
154
+ /* — messaging (named application channel, raw bytes) — */
155
+
156
+ RATS_API rats_error_t rats_send(rats_t node, const char* peer_id_hex,
157
+ const char* channel, const void* data, size_t len);
158
+ RATS_API rats_error_t rats_broadcast(rats_t node, const char* channel,
159
+ const void* data, size_t len);
160
+
161
+ /* — callbacks (register before start; invoked on a reactor thread) — */
162
+
163
+ typedef void (*rats_peer_cb)(void* user, const char* peer_id_hex);
164
+ typedef void (*rats_message_cb)(void* user, const char* peer_id_hex, const void* data, size_t len);
165
+
166
+ RATS_API rats_error_t rats_on_peer_connected(rats_t node, rats_peer_cb cb, void* user);
167
+ RATS_API rats_error_t rats_on_peer_disconnected(rats_t node, rats_peer_cb cb, void* user);
168
+ RATS_API rats_error_t rats_on(rats_t node, const char* channel, rats_message_cb cb, void* user);
169
+
170
+ /* — optional subsystems (enable before start) — */
171
+
172
+ /** DHT discovery. dht_port 0 = ephemeral; discovery_key namespaces the app
173
+ * (NULL → the node's protocol, so only same-protocol peers discover each other). */
174
+ RATS_API rats_error_t rats_enable_dht(rats_t node, uint16_t dht_port, const char* discovery_key);
175
+ /** Local-network mDNS discovery. */
176
+ RATS_API rats_error_t rats_enable_mdns(rats_t node);
177
+ /** Automatic NAT port forwarding for the listen port (UPnP IGD + NAT-PMP).
178
+ * Pass non-zero to enable each backend; both run in parallel. */
179
+ RATS_API rats_error_t rats_enable_port_mapping(rats_t node, int enable_upnp, int enable_natpmp);
180
+
181
+ /** UDP hole punching: reach a peer that no port forwarding made reachable, by
182
+ * arranging with a peer both sides already have that the two dial each other at
183
+ * the same moment. Call before start(); both punching nodes must have it enabled,
184
+ * and so must the node that carries the rendezvous between them.
185
+ *
186
+ * `serve_as_relay` (non-zero) also carries other peers' rendezvous — a few dozen
187
+ * forwarded bytes per punch, only ever to peers this node already holds. A mesh in
188
+ * which nobody relays cannot punch at all. */
189
+ RATS_API rats_error_t rats_enable_hole_punch(rats_t node, int serve_as_relay);
190
+
191
+ /** Try to reach `peer_id_hex` by punching. Non-blocking: success arrives as an
192
+ * ordinary peer-connected callback. RATS_OK if a rendezvous was started;
193
+ * RATS_ERR_NOT_ENABLED if hole punching is off; RATS_ERR_NO_SUCH_PEER if there is
194
+ * nothing to do or nothing to try with — the peer is already connected, a punch to
195
+ * it is already running, or this node does not yet know an external endpoint of
196
+ * its own to advertise (it needs at least one datagram peer first). */
197
+ RATS_API rats_error_t rats_punch_peer(rats_t node, const char* peer_id_hex);
198
+
199
+ /** Relaying: reach a peer that neither port forwarding nor hole punching could
200
+ * make reachable, by routing the connection through a node both ends are already
201
+ * connected to. Call before start(). The peer that comes out is ordinary in every
202
+ * way — the same end-to-end encryption, the same channels — except that its bytes
203
+ * take a detour, which rats_peer_transport() reports as RATS_TRANSPORT_RELAY.
204
+ *
205
+ * `serve_as_relay` (non-zero) also carries OTHER peers' connections. Unlike a
206
+ * hole-punch rendezvous, that spends real bandwidth on somebody else's traffic, so
207
+ * it is off by default and opted into here; a mesh in which nobody serves cannot
208
+ * relay at all. A serving node forwards only between peers it already holds, never
209
+ * chains circuits, and caps each one by bytes, duration and count. */
210
+ RATS_API rats_error_t rats_enable_relay(rats_t node, int serve_as_relay);
211
+
212
+ /** Try to reach `peer_id_hex` through a relay. Non-blocking: success arrives as an
213
+ * ordinary peer-connected callback. RATS_OK if an attempt was started;
214
+ * RATS_ERR_NOT_ENABLED if relaying is off; RATS_ERR_NO_SUCH_PEER if there is
215
+ * nothing to do or nothing to try with — the peer is already connected, an attempt
216
+ * is already running, it is in cooldown, or this node has no peer that could carry
217
+ * the connection.
218
+ *
219
+ * Usually there is no need to call this: with hole punching enabled too, a punch
220
+ * that cannot work hands the target over by itself. */
221
+ RATS_API rats_error_t rats_connect_via_relay(rats_t node, const char* peer_id_hex);
222
+
223
+ /** What the mesh has shown about this node's own NAT, from the endpoints datagram
224
+ * peers report seeing its shared UDP socket at. One of the RATS_NAT_* values;
225
+ * RATS_NAT_ENDPOINT_DEPENDENT means punching cannot work from here. */
226
+ RATS_API int rats_nat_mapping(rats_t node);
227
+
228
+ enum {
229
+ RATS_NAT_UNKNOWN = 0, /* not enough independent observations yet */
230
+ RATS_NAT_OPEN = 1, /* no NAT in the path */
231
+ RATS_NAT_ENDPOINT_INDEPENDENT = 2, /* one external port for every peer — punchable */
232
+ RATS_NAT_ENDPOINT_DEPENDENT = 3 /* a fresh mapping per peer (symmetric) — not punchable */
233
+ };
234
+
235
+ /* — peer enumeration — */
236
+
237
+ /** Hex ids of currently-connected peers. Writes the count to *count and returns a
238
+ * heap array of `count` heap strings; free the whole thing with rats_free_peer_ids().
239
+ * Returns NULL (and *count = 0) when there are no peers. */
240
+ RATS_API char** rats_peer_ids(rats_t node, size_t* count);
241
+ RATS_API void rats_free_peer_ids(char** ids, size_t count);
242
+
243
+ /** Which transport a connected peer's link runs on, or -1 if not connected. */
244
+ RATS_API int rats_peer_transport(rats_t node, const char* peer_id_hex);
245
+
246
+ /** Transports a connected peer advertised in its identify message, as a
247
+ * RATS_TRANSPORT_MASK_* bitmask. 0 means the peer did not say (an older build),
248
+ * which is "no information", not "none". -1 if the peer is not connected. */
249
+ RATS_API int rats_peer_transports(rats_t node, const char* peer_id_hex);
250
+
251
+ /* — pub/sub (topic-based, raw bytes; enable + subscribe before start) — */
252
+
253
+ typedef void (*rats_topic_cb)(void* user, const char* peer_id_hex,
254
+ const char* topic, const void* data, size_t len);
255
+
256
+ /** Enable the pub/sub (GossipSub) subsystem. Call before start(). */
257
+ RATS_API rats_error_t rats_enable_pubsub(rats_t node);
258
+ /** Subscribe to `topic`; matching messages invoke `cb` on a reactor thread. */
259
+ RATS_API rats_error_t rats_subscribe(rats_t node, const char* topic, rats_topic_cb cb, void* user);
260
+ RATS_API rats_error_t rats_unsubscribe(rats_t node, const char* topic);
261
+ /** Publish `data` on `topic` to every subscribed peer (and local subscribers). */
262
+ RATS_API rats_error_t rats_publish(rats_t node, const char* topic, const void* data, size_t len);
263
+
264
+ /* — typed JSON messaging (named message type; payload is JSON text) — */
265
+
266
+ typedef void (*rats_json_cb)(void* user, const char* peer_id_hex, const char* json);
267
+
268
+ /** Enable the JSON-messaging subsystem (the C view of MessageJson). Call before start(). */
269
+ RATS_API rats_error_t rats_enable_json(rats_t node);
270
+ /** Register a handler for JSON messages of `type`. `json` is compact JSON text owned
271
+ * by the library (valid only for the duration of the call). Additive: multiple
272
+ * handlers may coexist. The sender id is the authenticated handshake PeerId. */
273
+ RATS_API rats_error_t rats_on_json(rats_t node, const char* type, rats_json_cb cb, void* user);
274
+ /** Like rats_on_json, but the handler is removed right after it fires once. */
275
+ RATS_API rats_error_t rats_once_json(rats_t node, const char* type, rats_json_cb cb, void* user);
276
+ RATS_API rats_error_t rats_off_json(rats_t node, const char* type);
277
+ /** Send/broadcast a JSON message. `json` must be valid JSON text (invalid → RATS_ERR_INVALID_ARG). */
278
+ RATS_API rats_error_t rats_send_json(rats_t node, const char* peer_id_hex, const char* type, const char* json);
279
+ RATS_API rats_error_t rats_broadcast_json(rats_t node, const char* type, const char* json);
280
+
281
+ /* — file transfer (push model; enable + register callbacks before start) — */
282
+
283
+ typedef void (*rats_file_offer_cb)(void* user, const char* peer_id_hex, uint64_t transfer_id,
284
+ const char* name, uint64_t size, int is_directory);
285
+ typedef void (*rats_file_progress_cb)(void* user, uint64_t transfer_id, const char* peer_id_hex,
286
+ uint64_t bytes_transferred, uint64_t total_bytes, int status);
287
+ typedef void (*rats_file_complete_cb)(void* user, uint64_t transfer_id, int success, const char* path);
288
+
289
+ /** Enable the file-transfer subsystem. `temp_dir` holds in-progress downloads
290
+ * (NULL → current directory). Call before start(). */
291
+ RATS_API rats_error_t rats_enable_file_transfer(rats_t node, const char* temp_dir);
292
+
293
+ RATS_API rats_error_t rats_on_file_offer(rats_t node, rats_file_offer_cb cb, void* user);
294
+ RATS_API rats_error_t rats_on_file_progress(rats_t node, rats_file_progress_cb cb, void* user);
295
+ RATS_API rats_error_t rats_on_file_complete(rats_t node, rats_file_complete_cb cb, void* user);
296
+
297
+ /** Offer a file / directory tree to a peer. Returns the transfer id (0 on failure,
298
+ * e.g. file transfer not enabled or bad peer id). */
299
+ RATS_API uint64_t rats_send_file(rats_t node, const char* peer_id_hex, const char* path);
300
+ RATS_API uint64_t rats_send_directory(rats_t node, const char* peer_id_hex, const char* dir_path);
301
+
302
+ /** Respond to an offer. For a single file, dest_path is the file path; for a
303
+ * directory, the destination directory. (peer_id, transfer_id) names the offer. */
304
+ RATS_API rats_error_t rats_accept_file(rats_t node, const char* peer_id_hex, uint64_t transfer_id, const char* dest_path);
305
+ RATS_API rats_error_t rats_reject_file(rats_t node, const char* peer_id_hex, uint64_t transfer_id);
306
+
307
+ /** Control a live transfer (either side). RATS_OK if the transfer was found and
308
+ * the action applied; RATS_ERR_NO_SUCH_PEER if no matching transfer. */
309
+ RATS_API rats_error_t rats_cancel_file(rats_t node, const char* peer_id_hex, uint64_t transfer_id);
310
+ RATS_API rats_error_t rats_pause_file(rats_t node, const char* peer_id_hex, uint64_t transfer_id);
311
+ RATS_API rats_error_t rats_resume_file(rats_t node, const char* peer_id_hex, uint64_t transfer_id);
312
+
313
+ /* — liveness (RTT probing) — */
314
+
315
+ /** Enable periodic ping/pong RTT probing of every peer. Call before start(). */
316
+ RATS_API rats_error_t rats_enable_ping(rats_t node);
317
+ /** Last measured round-trip time to a peer in milliseconds, or -1 if unknown
318
+ * (ping not enabled, or no pong received yet). */
319
+ RATS_API int64_t rats_peer_rtt_ms(rats_t node, const char* peer_id_hex);
320
+
321
+ /* — automatic reconnection — */
322
+
323
+ /** Enable the reconnection subsystem: re-dials dropped peers with exponential
324
+ * backoff. Dialed peers are remembered automatically; when the node has a
325
+ * data_dir, targets persist to "<data_dir>/peers.json" across restarts. Call
326
+ * before start(). A bare node never reconnects on its own. */
327
+ RATS_API rats_error_t rats_enable_reconnect(rats_t node);
328
+ /** Add an address to keep connected (re-dialed on drop). Persisted if a store is
329
+ * configured. May be called before or after start(). */
330
+ RATS_API rats_error_t rats_add_reconnect(rats_t node, const char* host, uint16_t port);
331
+ /** Stop reconnecting to an address and drop it from the store. */
332
+ RATS_API rats_error_t rats_remove_reconnect(rats_t node, const char* host, uint16_t port);
333
+
334
+ /* — BitTorrent —
335
+ * Only functional when the library is built with RATS_SEARCH_FEATURES; the calls
336
+ * otherwise return RATS_ERR_NOT_ENABLED. The client keeps its own transport and,
337
+ * when DHT is enabled on the node first, shares it. Enable before start(). */
338
+
339
+ /** Enable BitTorrent. listen_port 0 picks an ephemeral port; download_path is the
340
+ * default save directory (NULL = "."). Enable DHT first to share the node's DHT. */
341
+ RATS_API rats_error_t rats_enable_bittorrent(rats_t node, uint16_t listen_port, const char* download_path);
342
+ /** Start downloading a magnet link (metadata is fetched from peers). */
343
+ RATS_API rats_error_t rats_bt_add_magnet(rats_t node, const char* magnet_uri, const char* save_path);
344
+ /** Start a torrent from a .torrent file on disk. */
345
+ RATS_API rats_error_t rats_bt_add_torrent_file(rats_t node, const char* torrent_path, const char* save_path);
346
+ /** Remove a torrent by its 40-char hex info-hash (downloaded files are kept). */
347
+ RATS_API rats_error_t rats_bt_remove_torrent(rats_t node, const char* info_hash_hex);
348
+
349
+ /* — logging (process-global; no node required) — */
350
+
351
+ typedef enum {
352
+ RATS_LOG_DEBUG = 0,
353
+ RATS_LOG_INFO = 1,
354
+ RATS_LOG_WARN = 2,
355
+ RATS_LOG_ERROR = 3
356
+ } rats_log_level_t;
357
+
358
+ RATS_API void rats_set_log_level(rats_log_level_t level);
359
+ /** Mirror logs to `path` (NULL/empty disables file logging). */
360
+ RATS_API void rats_set_log_file(const char* path);
361
+
362
+ /* — library info (process-global; no node required) — */
363
+
364
+ /** Library version as a static string, e.g. "1.2.3" (do not free). */
365
+ RATS_API const char* rats_version_string(void);
366
+ /** Library version components. Any out-pointer may be NULL. */
367
+ RATS_API void rats_version(int* major, int* minor, int* patch, int* build);
368
+ /** Git describe of the build, e.g. "v1.2.3-4-gabcdef" (static; do not free). */
369
+ RATS_API const char* rats_git_describe(void);
370
+ /** Packed ABI id as (major<<16)|(minor<<8)|patch — MAJOR bumps on breaking
371
+ * changes, MINOR on additive ones. */
372
+ RATS_API uint32_t rats_abi(void);
373
+
374
+ /* — utility — */
375
+
376
+ RATS_API void rats_string_free(char* str);
377
+
378
+ #ifdef __cplusplus
379
+ }
380
+ #endif