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.
- package/README.md +145 -331
- package/binding.gyp +16 -3
- package/lib/index.d.ts +288 -696
- package/lib/index.js +407 -44
- package/native-src/3rdparty/android/ifaddrs-android.c +1 -0
- package/native-src/3rdparty/android/ifaddrs-android.h +1 -0
- package/native-src/CMakeLists.txt +404 -179
- package/native-src/LICENSE +1 -1
- package/native-src/src/librats/bindings/rats.cpp +762 -0
- package/native-src/src/librats/bindings/rats.h +380 -0
- package/native-src/src/librats/bittorrent/bencode.cpp +437 -0
- package/native-src/src/librats/bittorrent/bencode.h +176 -0
- package/native-src/src/librats/bittorrent/bitfield.cpp +97 -0
- package/native-src/src/librats/bittorrent/bitfield.h +76 -0
- package/native-src/src/librats/bittorrent/byte_io.h +58 -0
- package/native-src/src/librats/bittorrent/choker.cpp +25 -0
- package/native-src/src/librats/bittorrent/choker.h +46 -0
- package/native-src/src/librats/bittorrent/client.cpp +413 -0
- package/native-src/src/librats/bittorrent/client.h +227 -0
- package/native-src/src/librats/bittorrent/disk_io.cpp +209 -0
- package/native-src/src/librats/bittorrent/disk_io.h +150 -0
- package/native-src/src/librats/bittorrent/extensions.cpp +191 -0
- package/native-src/src/librats/bittorrent/extensions.h +94 -0
- package/native-src/src/librats/bittorrent/file_storage.cpp +77 -0
- package/native-src/src/librats/bittorrent/file_storage.h +79 -0
- package/native-src/src/librats/bittorrent/log.h +42 -0
- package/native-src/src/librats/bittorrent/magnet_uri.cpp +98 -0
- package/native-src/src/librats/bittorrent/magnet_uri.h +35 -0
- package/native-src/src/librats/bittorrent/peer_connection.cpp +502 -0
- package/native-src/src/librats/bittorrent/peer_connection.h +194 -0
- package/native-src/src/librats/bittorrent/peer_list.cpp +68 -0
- package/native-src/src/librats/bittorrent/peer_list.h +75 -0
- package/native-src/src/librats/bittorrent/piece_picker.cpp +352 -0
- package/native-src/src/librats/bittorrent/piece_picker.h +201 -0
- package/native-src/src/librats/bittorrent/reactor.cpp +97 -0
- package/native-src/src/librats/bittorrent/reactor.h +89 -0
- package/native-src/src/librats/bittorrent/resume_data.cpp +72 -0
- package/native-src/src/librats/bittorrent/resume_data.h +41 -0
- package/native-src/src/librats/bittorrent/store_buffer.cpp +48 -0
- package/native-src/src/librats/bittorrent/store_buffer.h +47 -0
- package/native-src/src/librats/bittorrent/torrent.cpp +870 -0
- package/native-src/src/librats/bittorrent/torrent.h +260 -0
- package/native-src/src/librats/bittorrent/torrent_creator.cpp +129 -0
- package/native-src/src/librats/bittorrent/torrent_creator.h +58 -0
- package/native-src/src/librats/bittorrent/torrent_info.cpp +314 -0
- package/native-src/src/librats/bittorrent/torrent_info.h +118 -0
- package/native-src/src/librats/bittorrent/tracker.cpp +374 -0
- package/native-src/src/librats/bittorrent/tracker.h +108 -0
- package/native-src/src/librats/bittorrent/types.cpp +206 -0
- package/native-src/src/librats/bittorrent/types.h +86 -0
- package/native-src/src/librats/core/address.cpp +35 -0
- package/native-src/src/librats/core/address.h +78 -0
- package/native-src/src/librats/core/bytes.h +69 -0
- package/native-src/src/librats/core/chained_send_buffer.cpp +172 -0
- package/native-src/src/librats/core/chained_send_buffer.h +183 -0
- package/native-src/src/librats/core/endpoint_parse.cpp +41 -0
- package/native-src/src/librats/core/endpoint_parse.h +31 -0
- package/native-src/src/librats/core/event_bus.h +70 -0
- package/native-src/src/librats/core/host_endpoint.h +56 -0
- package/native-src/src/{io_poller.cpp → librats/core/io_poller.cpp} +520 -65
- package/native-src/src/{io_poller.h → librats/core/io_poller.h} +12 -6
- package/native-src/src/librats/core/ip_address.cpp +120 -0
- package/native-src/src/librats/core/ip_address.h +109 -0
- package/native-src/src/librats/core/mpsc_queue.h +47 -0
- package/native-src/src/librats/core/notifier.h +74 -0
- package/native-src/src/librats/core/receive_buffer.cpp +219 -0
- package/native-src/src/librats/core/receive_buffer.h +171 -0
- package/native-src/src/librats/core/service_registry.h +58 -0
- package/native-src/src/{socket.cpp → librats/core/socket.cpp} +625 -118
- package/native-src/src/librats/core/socket.h +496 -0
- package/native-src/src/librats/core/timer_queue.h +105 -0
- package/native-src/src/librats/core/types.cpp +43 -0
- package/native-src/src/librats/core/types.h +103 -0
- package/native-src/src/librats/core/wakeup_pipe.h +83 -0
- package/native-src/src/{crypto → librats/crypto}/blake2_endian.h +21 -23
- package/native-src/src/{crypto → librats/crypto}/blake2b.c +34 -33
- package/native-src/src/{crypto → librats/crypto}/blake2b.h +7 -6
- package/native-src/src/{crypto → librats/crypto}/blake2s.c +55 -54
- package/native-src/src/{crypto → librats/crypto}/blake2s.h +13 -12
- package/native-src/src/{crypto → librats/crypto}/chacha.c +22 -21
- package/native-src/src/{crypto → librats/crypto}/chacha.h +14 -13
- package/native-src/src/{crypto → librats/crypto}/chachapoly.c +56 -56
- package/native-src/src/{crypto → librats/crypto}/chachapoly.h +24 -17
- package/native-src/src/{crc32.cpp → librats/crypto/crc32.cpp} +1 -1
- package/native-src/src/{crc32.h → librats/crypto/crc32.h} +3 -1
- package/native-src/src/{crypto → librats/crypto}/curve25519.c +6 -4
- package/native-src/src/{crypto → librats/crypto}/curve25519.h +6 -3
- package/native-src/src/librats/crypto/hkdf.c +266 -0
- package/native-src/src/{crypto → librats/crypto}/hkdf.h +19 -19
- package/native-src/src/{noise.cpp → librats/crypto/noise.cpp} +84 -73
- package/native-src/src/{noise.h → librats/crypto/noise.h} +18 -8
- package/native-src/src/{crypto → librats/crypto}/poly1305.c +47 -46
- package/native-src/src/librats/crypto/poly1305.h +37 -0
- package/native-src/src/{sha1.cpp → librats/crypto/sha1.cpp} +33 -1
- package/native-src/src/{sha1.h → librats/crypto/sha1.h} +14 -6
- package/native-src/src/{crypto → librats/crypto}/sha256.c +15 -14
- package/native-src/src/{crypto → librats/crypto}/sha256.h +8 -7
- package/native-src/src/{crypto → librats/crypto}/sha512.c +15 -14
- package/native-src/src/{crypto → librats/crypto}/sha512.h +8 -7
- package/native-src/src/librats/dht/announce.cpp +37 -0
- package/native-src/src/librats/dht/announce.h +41 -0
- package/native-src/src/librats/dht/bep42.cpp +109 -0
- package/native-src/src/librats/dht/bep42.h +48 -0
- package/native-src/src/librats/dht/dht.cpp +501 -0
- package/native-src/src/librats/dht/dht.h +119 -0
- package/native-src/src/librats/dht/dht_runner.cpp +103 -0
- package/native-src/src/librats/dht/dht_runner.h +71 -0
- package/native-src/src/librats/dht/dos_blocker.cpp +42 -0
- package/native-src/src/librats/dht/dos_blocker.h +47 -0
- package/native-src/src/librats/dht/find_peers.cpp +52 -0
- package/native-src/src/librats/dht/find_peers.h +73 -0
- package/native-src/src/librats/dht/id.h +167 -0
- package/native-src/src/{krpc.cpp → librats/dht/krpc.cpp} +32 -81
- package/native-src/src/{krpc.h → librats/dht/krpc.h} +19 -23
- package/native-src/src/librats/dht/log.h +38 -0
- package/native-src/src/librats/dht/node.cpp +473 -0
- package/native-src/src/librats/dht/node.h +164 -0
- package/native-src/src/librats/dht/node_entry.h +81 -0
- package/native-src/src/librats/dht/observer.h +72 -0
- package/native-src/src/librats/dht/persistence.cpp +90 -0
- package/native-src/src/librats/dht/persistence.h +32 -0
- package/native-src/src/librats/dht/routing_table.cpp +559 -0
- package/native-src/src/librats/dht/routing_table.h +185 -0
- package/native-src/src/librats/dht/rpc_manager.cpp +127 -0
- package/native-src/src/librats/dht/rpc_manager.h +77 -0
- package/native-src/src/librats/dht/storage.cpp +92 -0
- package/native-src/src/librats/dht/storage.h +74 -0
- package/native-src/src/librats/dht/transport.h +27 -0
- package/native-src/src/librats/dht/traversal.cpp +326 -0
- package/native-src/src/librats/dht/traversal.h +120 -0
- package/native-src/src/librats/dht/udp_transport.cpp +49 -0
- package/native-src/src/librats/dht/udp_transport.h +51 -0
- package/native-src/src/librats/mdns/log.h +22 -0
- package/native-src/src/{mdns.cpp → librats/mdns/mdns.cpp} +75 -40
- package/native-src/src/{mdns.h → librats/mdns/mdns.h} +9 -8
- package/native-src/src/{natpmp.cpp → librats/nat/natpmp.cpp} +12 -9
- package/native-src/src/{natpmp.h → librats/nat/natpmp.h} +3 -3
- package/native-src/src/{port_mapping.h → librats/nat/port_mapping.h} +3 -2
- package/native-src/src/{stun.cpp → librats/nat/stun.cpp} +4 -4
- package/native-src/src/{stun.h → librats/nat/stun.h} +1 -1
- package/native-src/src/{upnp.cpp → librats/nat/upnp.cpp} +6 -6
- package/native-src/src/{upnp.h → librats/nat/upnp.h} +2 -2
- package/native-src/src/librats/node/circuit_service.h +84 -0
- package/native-src/src/librats/node/config.h +110 -0
- package/native-src/src/librats/node/dial_service.h +54 -0
- package/native-src/src/librats/node/dialer.cpp +264 -0
- package/native-src/src/librats/node/dialer.h +188 -0
- package/native-src/src/librats/node/host_events.h +26 -0
- package/native-src/src/librats/node/identify.cpp +130 -0
- package/native-src/src/librats/node/identify.h +71 -0
- package/native-src/src/librats/node/nat_status.cpp +103 -0
- package/native-src/src/librats/node/nat_status.h +118 -0
- package/native-src/src/librats/node/node.cpp +865 -0
- package/native-src/src/librats/node/node.h +344 -0
- package/native-src/src/librats/node/node_context.h +33 -0
- package/native-src/src/librats/node/peer_network.h +91 -0
- package/native-src/src/librats/peer/peer.h +49 -0
- package/native-src/src/librats/peer/peer_book.cpp +181 -0
- package/native-src/src/librats/peer/peer_book.h +88 -0
- package/native-src/src/librats/peer/peer_id.cpp +72 -0
- package/native-src/src/librats/peer/peer_id.h +62 -0
- package/native-src/src/librats/peer/peer_info.h +37 -0
- package/native-src/src/librats/peer/peer_table.cpp +170 -0
- package/native-src/src/librats/peer/peer_table.h +148 -0
- package/native-src/src/librats/security/handshaker.h +66 -0
- package/native-src/src/librats/security/identity.h +43 -0
- package/native-src/src/librats/security/noise_security.cpp +122 -0
- package/native-src/src/librats/security/noise_security.h +37 -0
- package/native-src/src/librats/security/plaintext_security.h +106 -0
- package/native-src/src/librats/security/session.h +37 -0
- package/native-src/src/{storage.cpp → librats/storage/storage.cpp} +369 -522
- package/native-src/src/{storage.h → librats/storage/storage.h} +135 -299
- package/native-src/src/librats/subsystems/bittorrent.cpp +211 -0
- package/native-src/src/librats/subsystems/bittorrent.h +136 -0
- package/native-src/src/librats/subsystems/dht_discovery.cpp +202 -0
- package/native-src/src/librats/subsystems/dht_discovery.h +123 -0
- package/native-src/src/librats/subsystems/dht_service.h +36 -0
- package/native-src/src/librats/subsystems/file_transfer.cpp +972 -0
- package/native-src/src/librats/subsystems/file_transfer.h +367 -0
- package/native-src/src/librats/subsystems/hole_punch.cpp +605 -0
- package/native-src/src/librats/subsystems/hole_punch.h +290 -0
- package/native-src/src/librats/subsystems/hole_punch_service.h +38 -0
- package/native-src/src/librats/subsystems/mdns_discovery.cpp +66 -0
- package/native-src/src/librats/subsystems/mdns_discovery.h +55 -0
- package/native-src/src/librats/subsystems/message_json.cpp +112 -0
- package/native-src/src/librats/subsystems/message_json.h +88 -0
- package/native-src/src/librats/subsystems/peer_exchange.cpp +241 -0
- package/native-src/src/librats/subsystems/peer_exchange.h +136 -0
- package/native-src/src/librats/subsystems/ping_service.cpp +98 -0
- package/native-src/src/librats/subsystems/ping_service.h +66 -0
- package/native-src/src/librats/subsystems/port_mapping_service.cpp +192 -0
- package/native-src/src/librats/subsystems/port_mapping_service.h +84 -0
- package/native-src/src/librats/subsystems/pubsub.cpp +567 -0
- package/native-src/src/librats/subsystems/pubsub.h +175 -0
- package/native-src/src/librats/subsystems/reconnection.cpp +239 -0
- package/native-src/src/librats/subsystems/reconnection.h +126 -0
- package/native-src/src/librats/subsystems/relay.cpp +1142 -0
- package/native-src/src/librats/subsystems/relay.h +211 -0
- package/native-src/src/librats/subsystems/relay_service.h +46 -0
- package/native-src/src/librats/transport/connection.cpp +343 -0
- package/native-src/src/librats/transport/connection.h +283 -0
- package/native-src/src/librats/transport/link.h +96 -0
- package/native-src/src/librats/transport/reactor.cpp +588 -0
- package/native-src/src/librats/transport/reactor.h +262 -0
- package/native-src/src/librats/transport/reactor_pool.h +81 -0
- package/native-src/src/librats/transport/relay_link.cpp +208 -0
- package/native-src/src/librats/transport/relay_link.h +303 -0
- package/native-src/src/librats/transport/tcp_link.cpp +49 -0
- package/native-src/src/librats/transport/tcp_link.h +43 -0
- package/native-src/src/librats/transport/udp_mux.cpp +617 -0
- package/native-src/src/librats/transport/udp_mux.h +363 -0
- package/native-src/src/librats/transport/udp_packet.cpp +121 -0
- package/native-src/src/librats/transport/udp_packet.h +190 -0
- package/native-src/src/librats/transport/udp_stream.cpp +1194 -0
- package/native-src/src/librats/transport/udp_stream.h +614 -0
- package/native-src/src/librats/util/features.h.in +51 -0
- package/native-src/src/{fs.cpp → librats/util/fs.cpp} +51 -3
- package/native-src/src/librats/util/fs.h +136 -0
- package/native-src/src/librats/util/json.cpp +1002 -0
- package/native-src/src/librats/util/json.h +444 -0
- package/native-src/src/{logger.cpp → librats/util/logger.cpp} +1 -1
- package/native-src/src/{logger.h → librats/util/logger.h} +43 -31
- package/native-src/src/{network_monitor.cpp → librats/util/network_monitor.cpp} +12 -4
- package/native-src/src/{network_monitor.h → librats/util/network_monitor.h} +2 -1
- package/native-src/src/{network_utils.cpp → librats/util/network_utils.cpp} +38 -23
- package/native-src/src/{network_utils.h → librats/util/network_utils.h} +15 -8
- package/native-src/src/{os.cpp → librats/util/os.cpp} +48 -18
- package/native-src/src/librats/util/rats_export.h +69 -0
- package/native-src/src/{version.cpp → librats/util/version.cpp} +2 -2
- package/native-src/src/{version.h.in → librats/util/version.h.in} +1 -1
- package/native-src/src/librats/wire/frame.cpp +76 -0
- package/native-src/src/librats/wire/frame.h +111 -0
- package/native-src/src/librats/wire/message_router.cpp +45 -0
- package/native-src/src/librats/wire/message_router.h +47 -0
- package/package.json +5 -4
- package/scripts/build-librats.js +1 -0
- package/scripts/postinstall.js +3 -3
- package/scripts/prepare-package.js +4 -4
- package/scripts/verify-installation.js +63 -105
- package/src/librats_node.cpp +1067 -1323
- package/native-src/src/bencode.cpp +0 -485
- package/native-src/src/bencode.h +0 -145
- package/native-src/src/bittorrent.cpp +0 -14
- package/native-src/src/bittorrent.h +0 -74
- package/native-src/src/bt_bitfield.cpp +0 -372
- package/native-src/src/bt_bitfield.h +0 -316
- package/native-src/src/bt_choker.cpp +0 -228
- package/native-src/src/bt_choker.h +0 -147
- package/native-src/src/bt_client.cpp +0 -1047
- package/native-src/src/bt_client.h +0 -445
- package/native-src/src/bt_create_torrent.cpp +0 -677
- package/native-src/src/bt_create_torrent.h +0 -473
- package/native-src/src/bt_extension.cpp +0 -469
- package/native-src/src/bt_extension.h +0 -309
- package/native-src/src/bt_file_storage.cpp +0 -261
- package/native-src/src/bt_file_storage.h +0 -298
- package/native-src/src/bt_handshake.cpp +0 -134
- package/native-src/src/bt_handshake.h +0 -157
- package/native-src/src/bt_messages.cpp +0 -364
- package/native-src/src/bt_messages.h +0 -324
- package/native-src/src/bt_network.cpp +0 -1007
- package/native-src/src/bt_network.h +0 -417
- package/native-src/src/bt_peer_connection.cpp +0 -742
- package/native-src/src/bt_peer_connection.h +0 -592
- package/native-src/src/bt_piece_picker.cpp +0 -786
- package/native-src/src/bt_piece_picker.h +0 -473
- package/native-src/src/bt_resume_data.cpp +0 -410
- package/native-src/src/bt_resume_data.h +0 -249
- package/native-src/src/bt_torrent.cpp +0 -2120
- package/native-src/src/bt_torrent.h +0 -641
- package/native-src/src/bt_torrent_info.cpp +0 -659
- package/native-src/src/bt_torrent_info.h +0 -418
- package/native-src/src/bt_types.h +0 -621
- package/native-src/src/chained_send_buffer.cpp +0 -75
- package/native-src/src/chained_send_buffer.h +0 -137
- package/native-src/src/crypto/hkdf.c +0 -266
- package/native-src/src/crypto/poly1305.h +0 -36
- package/native-src/src/dht.cpp +0 -3311
- package/native-src/src/dht.h +0 -717
- package/native-src/src/disk_io.cpp +0 -632
- package/native-src/src/disk_io.h +0 -315
- package/native-src/src/file_transfer.cpp +0 -1415
- package/native-src/src/file_transfer.h +0 -286
- package/native-src/src/fs.h +0 -108
- package/native-src/src/gossipsub.cpp +0 -1139
- package/native-src/src/gossipsub.h +0 -403
- package/native-src/src/ice.cpp +0 -893
- package/native-src/src/ice.h +0 -559
- package/native-src/src/json.hpp +0 -25526
- package/native-src/src/librats.cpp +0 -2378
- package/native-src/src/librats.h +0 -2324
- package/native-src/src/librats_bittorrent.cpp +0 -601
- package/native-src/src/librats_c.cpp +0 -1557
- package/native-src/src/librats_c.h +0 -323
- package/native-src/src/librats_discovery.cpp +0 -402
- package/native-src/src/librats_encryption.cpp +0 -275
- package/native-src/src/librats_file_transfer.cpp +0 -144
- package/native-src/src/librats_gossipsub.cpp +0 -289
- package/native-src/src/librats_ice.cpp +0 -213
- package/native-src/src/librats_log_macros.h +0 -36
- package/native-src/src/librats_logging.cpp +0 -173
- package/native-src/src/librats_mdns.cpp +0 -166
- package/native-src/src/librats_persistence.cpp +0 -796
- package/native-src/src/librats_portmap.cpp +0 -419
- package/native-src/src/librats_reconnection.cpp +0 -218
- package/native-src/src/librats_statistic.cpp +0 -105
- package/native-src/src/librats_storage.cpp +0 -189
- package/native-src/src/rats_export.h +0 -17
- package/native-src/src/receive_buffer.cpp +0 -82
- package/native-src/src/receive_buffer.h +0 -127
- package/native-src/src/socket.h +0 -228
- package/native-src/src/threadmanager.cpp +0 -105
- package/native-src/src/threadmanager.h +0 -53
- package/native-src/src/tracker.cpp +0 -1264
- package/native-src/src/tracker.h +0 -319
- package/native-src/src/turn.cpp +0 -762
- package/native-src/src/turn.h +0 -460
- package/native-src/src/wakeup_pipe.h +0 -60
- /package/native-src/src/{os.h → librats/util/os.h} +0 -0
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
#include "krpc.h"
|
|
2
|
-
#include "
|
|
3
|
-
#include "logger.h"
|
|
4
|
-
#include <random>
|
|
5
|
-
#include <chrono>
|
|
1
|
+
#include "librats/dht/krpc.h"
|
|
2
|
+
#include "librats/util/logger.h"
|
|
6
3
|
#include <sstream>
|
|
7
4
|
#include <iomanip>
|
|
8
5
|
#include <cstring>
|
|
@@ -16,16 +13,15 @@
|
|
|
16
13
|
#include <netinet/in.h>
|
|
17
14
|
#endif
|
|
18
15
|
|
|
19
|
-
// KRPC module logging macros
|
|
20
|
-
|
|
21
|
-
#define
|
|
22
|
-
#define
|
|
23
|
-
#define
|
|
16
|
+
// KRPC module logging macros. Tagged "dht.krpc" to sit under the DHT dot-namespace
|
|
17
|
+
// alongside dht.find / dht.route / dht.rpc — the codec is used only by the DHT.
|
|
18
|
+
#define LOG_KRPC_DEBUG(message) LOG_DEBUG("dht.krpc", message)
|
|
19
|
+
#define LOG_KRPC_INFO(message) LOG_INFO("dht.krpc", message)
|
|
20
|
+
#define LOG_KRPC_WARN(message) LOG_WARN("dht.krpc", message)
|
|
21
|
+
#define LOG_KRPC_ERROR(message) LOG_ERROR("dht.krpc", message)
|
|
24
22
|
|
|
25
23
|
namespace librats {
|
|
26
24
|
|
|
27
|
-
std::atomic<uint32_t> KrpcProtocol::transaction_counter_ = 0;
|
|
28
|
-
|
|
29
25
|
KrpcProtocol::KrpcProtocol() {}
|
|
30
26
|
|
|
31
27
|
KrpcProtocol::~KrpcProtocol() {}
|
|
@@ -91,7 +87,7 @@ KrpcMessage KrpcProtocol::create_find_node_response(const std::string& transacti
|
|
|
91
87
|
return message;
|
|
92
88
|
}
|
|
93
89
|
|
|
94
|
-
KrpcMessage KrpcProtocol::create_get_peers_response(const std::string& transaction_id, const NodeId& response_id, const std::vector<
|
|
90
|
+
KrpcMessage KrpcProtocol::create_get_peers_response(const std::string& transaction_id, const NodeId& response_id, const std::vector<Address>& peers, const std::string& token) {
|
|
95
91
|
KrpcMessage message;
|
|
96
92
|
message.type = KrpcMessageType::Response;
|
|
97
93
|
message.transaction_id = transaction_id;
|
|
@@ -213,7 +209,7 @@ BencodeValue KrpcProtocol::encode_response(const KrpcMessage& message) {
|
|
|
213
209
|
std::string compact_nodes_v4;
|
|
214
210
|
std::string compact_nodes_v6;
|
|
215
211
|
for (const auto& node : message.nodes) {
|
|
216
|
-
if (
|
|
212
|
+
if (node.ip.is_v6()) {
|
|
217
213
|
compact_nodes_v6 += compact_node_info(node);
|
|
218
214
|
} else {
|
|
219
215
|
compact_nodes_v4 += compact_node_info(node);
|
|
@@ -245,8 +241,8 @@ BencodeValue KrpcProtocol::encode_response(const KrpcMessage& message) {
|
|
|
245
241
|
|
|
246
242
|
// BEP 42: echo the requester's external address as a top-level "ip" field
|
|
247
243
|
// (compact 6-byte IPv4 / 18-byte IPv6 ip+port).
|
|
248
|
-
if (!message.external_ip.
|
|
249
|
-
std::string compact = compact_peer_info(
|
|
244
|
+
if (!message.external_ip.is_unspecified()) {
|
|
245
|
+
std::string compact = compact_peer_info(Address(message.external_ip, message.external_port));
|
|
250
246
|
if (!compact.empty()) {
|
|
251
247
|
root["ip"] = BencodeValue(compact);
|
|
252
248
|
}
|
|
@@ -450,22 +446,6 @@ std::unique_ptr<KrpcMessage> KrpcProtocol::decode_error(const BencodeValue& data
|
|
|
450
446
|
}
|
|
451
447
|
|
|
452
448
|
// Utility functions
|
|
453
|
-
std::string KrpcProtocol::generate_transaction_id() {
|
|
454
|
-
// A predictable transaction ID lets an off-path attacker forge responses to our queries and
|
|
455
|
-
// poison lookups. We make the ID unpredictable (2 random bytes) while keeping it collision-free
|
|
456
|
-
// (2 monotonic counter bytes guarantee uniqueness among any 65536 consecutive/outstanding
|
|
457
|
-
// transactions). The result is a 4-byte opaque binary string echoed back verbatim in KRPC.
|
|
458
|
-
static thread_local std::mt19937 rng(std::random_device{}());
|
|
459
|
-
uint32_t counter = transaction_counter_.fetch_add(1, std::memory_order_relaxed);
|
|
460
|
-
uint16_t rnd = static_cast<uint16_t>(rng() & 0xFFFF);
|
|
461
|
-
char tid[4];
|
|
462
|
-
tid[0] = static_cast<char>((rnd >> 8) & 0xFF);
|
|
463
|
-
tid[1] = static_cast<char>(rnd & 0xFF);
|
|
464
|
-
tid[2] = static_cast<char>((counter >> 8) & 0xFF);
|
|
465
|
-
tid[3] = static_cast<char>(counter & 0xFF);
|
|
466
|
-
return std::string(tid, sizeof(tid));
|
|
467
|
-
}
|
|
468
|
-
|
|
469
449
|
std::string KrpcProtocol::node_id_to_string(const NodeId& id) {
|
|
470
450
|
return std::string(id.begin(), id.end());
|
|
471
451
|
}
|
|
@@ -481,56 +461,27 @@ NodeId KrpcProtocol::string_to_node_id(const std::string& str) {
|
|
|
481
461
|
return id;
|
|
482
462
|
}
|
|
483
463
|
|
|
484
|
-
// Append a compact
|
|
485
|
-
// Returns the number of address bytes written, or 0
|
|
486
|
-
static size_t append_compact_address(const
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
out.append(reinterpret_cast<const char*>(addr6.s6_addr), 16);
|
|
491
|
-
return 16;
|
|
492
|
-
}
|
|
493
|
-
return 0;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
struct in_addr addr;
|
|
497
|
-
if (inet_pton(AF_INET, ip.c_str(), &addr) == 1) {
|
|
498
|
-
uint32_t v = ntohl(addr.s_addr);
|
|
499
|
-
out += static_cast<char>((v >> 24) & 0xFF);
|
|
500
|
-
out += static_cast<char>((v >> 16) & 0xFF);
|
|
501
|
-
out += static_cast<char>((v >> 8) & 0xFF);
|
|
502
|
-
out += static_cast<char>(v & 0xFF);
|
|
464
|
+
// Append a numeric IpAddress in compact form (its 4 or 16 raw bytes) to `out`.
|
|
465
|
+
// Returns the number of address bytes written, or 0 for an unspecified address.
|
|
466
|
+
static size_t append_compact_address(const IpAddress& ip, std::string& out) {
|
|
467
|
+
const ByteView b = ip.bytes();
|
|
468
|
+
if (b.empty()) { // unspecified — emit 0.0.0.0 to keep the record length valid
|
|
469
|
+
out.append(4, '\x00');
|
|
503
470
|
return 4;
|
|
504
471
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
return 4;
|
|
472
|
+
out.append(reinterpret_cast<const char*>(b.data()), b.size());
|
|
473
|
+
return b.size();
|
|
508
474
|
}
|
|
509
475
|
|
|
510
476
|
// Read a compact IP address of `addr_len` bytes (4=IPv4, 16=IPv6) at compact_info[offset]
|
|
511
|
-
// into a
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
char ip_str[INET6_ADDRSTRLEN];
|
|
517
|
-
inet_ntop(AF_INET6, &addr6, ip_str, INET6_ADDRSTRLEN);
|
|
518
|
-
return std::string(ip_str);
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
struct in_addr addr;
|
|
522
|
-
uint32_t ip = 0;
|
|
523
|
-
ip |= (static_cast<uint8_t>(compact_info[offset]) << 24);
|
|
524
|
-
ip |= (static_cast<uint8_t>(compact_info[offset + 1]) << 16);
|
|
525
|
-
ip |= (static_cast<uint8_t>(compact_info[offset + 2]) << 8);
|
|
526
|
-
ip |= static_cast<uint8_t>(compact_info[offset + 3]);
|
|
527
|
-
addr.s_addr = htonl(ip);
|
|
528
|
-
char ip_str[INET_ADDRSTRLEN];
|
|
529
|
-
inet_ntop(AF_INET, &addr, ip_str, INET_ADDRSTRLEN);
|
|
530
|
-
return std::string(ip_str);
|
|
477
|
+
// straight into an IpAddress — a memcpy of the raw bytes, no inet_ntop. The caller
|
|
478
|
+
// has already validated that [offset, offset+addr_len) is in bounds.
|
|
479
|
+
static IpAddress read_compact_ip(const std::string& compact_info, size_t offset, size_t addr_len) {
|
|
480
|
+
const auto* p = reinterpret_cast<const uint8_t*>(compact_info.data()) + offset;
|
|
481
|
+
return IpAddress::from_bytes(ByteView(p, addr_len)).value_or(IpAddress{});
|
|
531
482
|
}
|
|
532
483
|
|
|
533
|
-
std::string KrpcProtocol::compact_peer_info(const
|
|
484
|
+
std::string KrpcProtocol::compact_peer_info(const Address& peer) {
|
|
534
485
|
std::string result;
|
|
535
486
|
result.reserve(18);
|
|
536
487
|
|
|
@@ -561,8 +512,8 @@ std::string KrpcProtocol::compact_node_info(const KrpcNode& node) {
|
|
|
561
512
|
return result;
|
|
562
513
|
}
|
|
563
514
|
|
|
564
|
-
std::vector<
|
|
565
|
-
std::vector<
|
|
515
|
+
std::vector<Address> KrpcProtocol::parse_compact_peer_info(const std::string& compact_info) {
|
|
516
|
+
std::vector<Address> peers;
|
|
566
517
|
|
|
567
518
|
// BEP 5/BEP 7: each "values" entry is a single compact peer: 6 bytes (IPv4) or 18 bytes (IPv6).
|
|
568
519
|
// Detect the family from the entry length. An exact length of 18 is treated as one IPv6 peer;
|
|
@@ -578,13 +529,13 @@ std::vector<Peer> KrpcProtocol::parse_compact_peer_info(const std::string& compa
|
|
|
578
529
|
}
|
|
579
530
|
|
|
580
531
|
for (size_t i = 0; i + record_size <= compact_info.size(); i += record_size) {
|
|
581
|
-
|
|
532
|
+
IpAddress ip = read_compact_ip(compact_info, i, addr_len);
|
|
582
533
|
|
|
583
534
|
uint16_t port = 0;
|
|
584
535
|
port |= (static_cast<uint8_t>(compact_info[i + addr_len]) << 8);
|
|
585
536
|
port |= static_cast<uint8_t>(compact_info[i + addr_len + 1]);
|
|
586
537
|
|
|
587
|
-
peers.emplace_back(
|
|
538
|
+
peers.emplace_back(ip, port);
|
|
588
539
|
}
|
|
589
540
|
|
|
590
541
|
return peers;
|
|
@@ -608,14 +559,14 @@ std::vector<KrpcNode> KrpcProtocol::parse_compact_node_info(const std::string& c
|
|
|
608
559
|
std::copy_n(compact_info.begin() + i, 20, node_id.begin());
|
|
609
560
|
|
|
610
561
|
// Extract IP address (4 or 16 bytes)
|
|
611
|
-
|
|
562
|
+
IpAddress ip = read_compact_ip(compact_info, i + 20, addr_len);
|
|
612
563
|
|
|
613
564
|
// Extract port (2 bytes)
|
|
614
565
|
uint16_t port = 0;
|
|
615
566
|
port |= (static_cast<uint8_t>(compact_info[i + 20 + addr_len]) << 8);
|
|
616
567
|
port |= static_cast<uint8_t>(compact_info[i + 20 + addr_len + 1]);
|
|
617
568
|
|
|
618
|
-
nodes.emplace_back(node_id,
|
|
569
|
+
nodes.emplace_back(node_id, ip, port);
|
|
619
570
|
}
|
|
620
571
|
|
|
621
572
|
return nodes;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
|
-
#include "bencode.h"
|
|
4
|
-
#include "
|
|
3
|
+
#include "librats/bittorrent/bencode.h"
|
|
4
|
+
#include "librats/core/ip_address.h"
|
|
5
|
+
#include "librats/core/socket.h"
|
|
5
6
|
#include <string>
|
|
6
7
|
#include <vector>
|
|
7
8
|
#include <array>
|
|
8
9
|
#include <memory>
|
|
9
|
-
#include <atomic>
|
|
10
10
|
|
|
11
11
|
namespace librats {
|
|
12
12
|
|
|
@@ -46,12 +46,12 @@ enum class KrpcErrorCode {
|
|
|
46
46
|
* KRPC Node information
|
|
47
47
|
*/
|
|
48
48
|
struct KrpcNode {
|
|
49
|
-
NodeId
|
|
50
|
-
|
|
51
|
-
uint16_t
|
|
52
|
-
|
|
49
|
+
NodeId id;
|
|
50
|
+
IpAddress ip;
|
|
51
|
+
uint16_t port;
|
|
52
|
+
|
|
53
53
|
KrpcNode() : id(), port(0) {}
|
|
54
|
-
KrpcNode(const NodeId& node_id, const
|
|
54
|
+
KrpcNode(const NodeId& node_id, const IpAddress& ip_addr, uint16_t port_num)
|
|
55
55
|
: id(node_id), ip(ip_addr), port(port_num) {}
|
|
56
56
|
};
|
|
57
57
|
|
|
@@ -78,14 +78,16 @@ struct KrpcMessage {
|
|
|
78
78
|
// For responses
|
|
79
79
|
NodeId response_id;
|
|
80
80
|
std::vector<KrpcNode> nodes; // may mix IPv4/IPv6 nodes; encoder splits into nodes/nodes6
|
|
81
|
-
std::vector<
|
|
81
|
+
std::vector<Address> peers;
|
|
82
82
|
|
|
83
83
|
// BEP 42: top-level "ip" field (compact ip+port).
|
|
84
84
|
// On responses we SEND: the requester's external address, so they can learn how the
|
|
85
85
|
// network sees them and derive a compliant node ID.
|
|
86
86
|
// On responses we RECEIVE: our own external address as observed by the responder.
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
// Kept as a numeric IpAddress (not text): it comes off the wire as raw bytes and is
|
|
88
|
+
// consumed as raw bytes, so no round-trip through a string is needed.
|
|
89
|
+
IpAddress external_ip;
|
|
90
|
+
uint16_t external_port = 0;
|
|
89
91
|
|
|
90
92
|
// For errors
|
|
91
93
|
KrpcErrorCode error_code;
|
|
@@ -112,7 +114,7 @@ public:
|
|
|
112
114
|
|
|
113
115
|
static KrpcMessage create_ping_response(const std::string& transaction_id, const NodeId& response_id);
|
|
114
116
|
static KrpcMessage create_find_node_response(const std::string& transaction_id, const NodeId& response_id, const std::vector<KrpcNode>& nodes);
|
|
115
|
-
static KrpcMessage create_get_peers_response(const std::string& transaction_id, const NodeId& response_id, const std::vector<
|
|
117
|
+
static KrpcMessage create_get_peers_response(const std::string& transaction_id, const NodeId& response_id, const std::vector<Address>& peers, const std::string& token);
|
|
116
118
|
static KrpcMessage create_get_peers_response_with_nodes(const std::string& transaction_id, const NodeId& response_id, const std::vector<KrpcNode>& nodes, const std::string& token);
|
|
117
119
|
static KrpcMessage create_announce_peer_response(const std::string& transaction_id, const NodeId& response_id);
|
|
118
120
|
|
|
@@ -123,22 +125,19 @@ public:
|
|
|
123
125
|
*/
|
|
124
126
|
static std::vector<uint8_t> encode_message(const KrpcMessage& message);
|
|
125
127
|
static std::unique_ptr<KrpcMessage> decode_message(const std::vector<uint8_t>& data);
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Generate transaction ID
|
|
129
|
-
*/
|
|
130
|
-
static std::string generate_transaction_id();
|
|
131
|
-
|
|
128
|
+
|
|
132
129
|
/**
|
|
133
130
|
* Utility functions
|
|
134
131
|
*/
|
|
135
132
|
static std::string node_id_to_string(const NodeId& id);
|
|
136
133
|
static NodeId string_to_node_id(const std::string& str);
|
|
134
|
+
// The KRPC method name ("ping" / "find_node" / "get_peers" / "announce_peer"); used for logging.
|
|
135
|
+
static std::string query_type_to_string(KrpcQueryType type);
|
|
137
136
|
// Compact encodings auto-select IPv4 (6/26 bytes) or IPv6 (18/38 bytes) based on the address.
|
|
138
|
-
static std::string compact_peer_info(const
|
|
137
|
+
static std::string compact_peer_info(const Address& peer);
|
|
139
138
|
static std::string compact_node_info(const KrpcNode& node);
|
|
140
139
|
// parse_compact_peer_info detects the family from the string length (one peer per string).
|
|
141
|
-
static std::vector<
|
|
140
|
+
static std::vector<Address> parse_compact_peer_info(const std::string& compact_info);
|
|
142
141
|
// parse_compact_node_info reads fixed-size records; pass ipv6=true for 38-byte ("nodes6") records.
|
|
143
142
|
static std::vector<KrpcNode> parse_compact_node_info(const std::string& compact_info, bool ipv6 = false);
|
|
144
143
|
|
|
@@ -152,9 +151,6 @@ private:
|
|
|
152
151
|
static std::unique_ptr<KrpcMessage> decode_error(const BencodeValue& data);
|
|
153
152
|
|
|
154
153
|
static KrpcQueryType string_to_query_type(const std::string& str);
|
|
155
|
-
static std::string query_type_to_string(KrpcQueryType type);
|
|
156
|
-
|
|
157
|
-
static std::atomic<uint32_t> transaction_counter_;
|
|
158
154
|
};
|
|
159
155
|
|
|
160
156
|
} // namespace librats
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file log.h
|
|
5
|
+
* @brief Small logging helpers shared across the DHT modules.
|
|
6
|
+
*
|
|
7
|
+
* Formatting sugar on top of util/logger.h — nothing more. The DHT logs under a set of
|
|
8
|
+
* dot-namespaced tags so each subsystem gets its own stable colour and is easy to grep:
|
|
9
|
+
*
|
|
10
|
+
* "dht" — client facade + runner lifecycle (start/stop/bootstrap/external-ip)
|
|
11
|
+
* "dht.find" — lookups (find_peers / announce) and their progress
|
|
12
|
+
* "dht.route" — routing table (bucket splits, evictions, table summary)
|
|
13
|
+
* "dht.rpc" — outgoing/incoming queries, timeouts, anti-spoof drops
|
|
14
|
+
* "dht.krpc" — the KRPC wire codec (encode/decode); defined in krpc.cpp
|
|
15
|
+
*
|
|
16
|
+
* Endpoints render via Address::to_string(); ids render via short_hex() below.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#include "librats/dht/id.h"
|
|
20
|
+
#include "librats/util/logger.h"
|
|
21
|
+
|
|
22
|
+
#include <cstddef>
|
|
23
|
+
#include <string>
|
|
24
|
+
|
|
25
|
+
namespace librats {
|
|
26
|
+
namespace dht {
|
|
27
|
+
|
|
28
|
+
// A compact, log-friendly rendering of a 160-bit id: its leading `chars` hex digits
|
|
29
|
+
// (default 8, like a git short hash). The full 40-char id is unreadable in a log line
|
|
30
|
+
// and its leading bytes are more than enough to follow one contact across lines. Reach
|
|
31
|
+
// for to_hex(id) only where the complete id genuinely matters (e.g. our own id at start).
|
|
32
|
+
inline std::string short_hex(const NodeId& id, std::size_t chars = 8) {
|
|
33
|
+
const std::string full = to_hex(id);
|
|
34
|
+
return chars >= full.size() ? full : full.substr(0, chars);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
} // namespace dht
|
|
38
|
+
} // namespace librats
|