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,364 +0,0 @@
|
|
|
1
|
-
#include "bt_messages.h"
|
|
2
|
-
#include <cstring>
|
|
3
|
-
|
|
4
|
-
namespace librats {
|
|
5
|
-
|
|
6
|
-
//=============================================================================
|
|
7
|
-
// Helpers
|
|
8
|
-
//=============================================================================
|
|
9
|
-
|
|
10
|
-
const char* message_type_to_string(BtMessageType type) {
|
|
11
|
-
switch (type) {
|
|
12
|
-
case BtMessageType::Choke: return "Choke";
|
|
13
|
-
case BtMessageType::Unchoke: return "Unchoke";
|
|
14
|
-
case BtMessageType::Interested: return "Interested";
|
|
15
|
-
case BtMessageType::NotInterested: return "NotInterested";
|
|
16
|
-
case BtMessageType::Have: return "Have";
|
|
17
|
-
case BtMessageType::Bitfield: return "Bitfield";
|
|
18
|
-
case BtMessageType::Request: return "Request";
|
|
19
|
-
case BtMessageType::Piece: return "Piece";
|
|
20
|
-
case BtMessageType::Cancel: return "Cancel";
|
|
21
|
-
case BtMessageType::Port: return "Port";
|
|
22
|
-
case BtMessageType::SuggestPiece: return "SuggestPiece";
|
|
23
|
-
case BtMessageType::HaveAll: return "HaveAll";
|
|
24
|
-
case BtMessageType::HaveNone: return "HaveNone";
|
|
25
|
-
case BtMessageType::RejectRequest: return "RejectRequest";
|
|
26
|
-
case BtMessageType::AllowedFast: return "AllowedFast";
|
|
27
|
-
case BtMessageType::Extended: return "Extended";
|
|
28
|
-
default: return "Unknown";
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
//=============================================================================
|
|
33
|
-
// Encoder Helper Methods
|
|
34
|
-
//=============================================================================
|
|
35
|
-
|
|
36
|
-
void BtMessageEncoder::write_uint32(std::vector<uint8_t>& buf, uint32_t value) {
|
|
37
|
-
buf.push_back(static_cast<uint8_t>((value >> 24) & 0xFF));
|
|
38
|
-
buf.push_back(static_cast<uint8_t>((value >> 16) & 0xFF));
|
|
39
|
-
buf.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
|
|
40
|
-
buf.push_back(static_cast<uint8_t>(value & 0xFF));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
void BtMessageEncoder::write_uint16(std::vector<uint8_t>& buf, uint16_t value) {
|
|
44
|
-
buf.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
|
|
45
|
-
buf.push_back(static_cast<uint8_t>(value & 0xFF));
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
//=============================================================================
|
|
49
|
-
// Core Protocol Encoding (BEP 3)
|
|
50
|
-
//=============================================================================
|
|
51
|
-
|
|
52
|
-
std::vector<uint8_t> BtMessageEncoder::encode_keepalive() {
|
|
53
|
-
std::vector<uint8_t> msg;
|
|
54
|
-
write_uint32(msg, 0); // length = 0
|
|
55
|
-
return msg;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
std::vector<uint8_t> BtMessageEncoder::encode_choke() {
|
|
59
|
-
std::vector<uint8_t> msg;
|
|
60
|
-
write_uint32(msg, 1); // length = 1
|
|
61
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Choke));
|
|
62
|
-
return msg;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
std::vector<uint8_t> BtMessageEncoder::encode_unchoke() {
|
|
66
|
-
std::vector<uint8_t> msg;
|
|
67
|
-
write_uint32(msg, 1);
|
|
68
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Unchoke));
|
|
69
|
-
return msg;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
std::vector<uint8_t> BtMessageEncoder::encode_interested() {
|
|
73
|
-
std::vector<uint8_t> msg;
|
|
74
|
-
write_uint32(msg, 1);
|
|
75
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Interested));
|
|
76
|
-
return msg;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
std::vector<uint8_t> BtMessageEncoder::encode_not_interested() {
|
|
80
|
-
std::vector<uint8_t> msg;
|
|
81
|
-
write_uint32(msg, 1);
|
|
82
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::NotInterested));
|
|
83
|
-
return msg;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
std::vector<uint8_t> BtMessageEncoder::encode_have(uint32_t piece_index) {
|
|
87
|
-
std::vector<uint8_t> msg;
|
|
88
|
-
write_uint32(msg, 5); // length = 1 (type) + 4 (piece index)
|
|
89
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Have));
|
|
90
|
-
write_uint32(msg, piece_index);
|
|
91
|
-
return msg;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
std::vector<uint8_t> BtMessageEncoder::encode_bitfield(const Bitfield& bitfield) {
|
|
95
|
-
std::vector<uint8_t> bf_bytes = bitfield.to_bytes();
|
|
96
|
-
|
|
97
|
-
std::vector<uint8_t> msg;
|
|
98
|
-
write_uint32(msg, static_cast<uint32_t>(1 + bf_bytes.size())); // length
|
|
99
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Bitfield));
|
|
100
|
-
msg.insert(msg.end(), bf_bytes.begin(), bf_bytes.end());
|
|
101
|
-
return msg;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
std::vector<uint8_t> BtMessageEncoder::encode_request(uint32_t piece_index,
|
|
105
|
-
uint32_t begin,
|
|
106
|
-
uint32_t length) {
|
|
107
|
-
std::vector<uint8_t> msg;
|
|
108
|
-
write_uint32(msg, 13); // length = 1 + 4 + 4 + 4
|
|
109
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Request));
|
|
110
|
-
write_uint32(msg, piece_index);
|
|
111
|
-
write_uint32(msg, begin);
|
|
112
|
-
write_uint32(msg, length);
|
|
113
|
-
return msg;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
std::vector<uint8_t> BtMessageEncoder::encode_request(const RequestMessage& req) {
|
|
117
|
-
return encode_request(req.piece_index, req.begin, req.length);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
std::vector<uint8_t> BtMessageEncoder::encode_piece(uint32_t piece_index,
|
|
121
|
-
uint32_t begin,
|
|
122
|
-
const uint8_t* data,
|
|
123
|
-
size_t length) {
|
|
124
|
-
std::vector<uint8_t> msg;
|
|
125
|
-
write_uint32(msg, static_cast<uint32_t>(9 + length)); // length = 1 + 4 + 4 + data
|
|
126
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Piece));
|
|
127
|
-
write_uint32(msg, piece_index);
|
|
128
|
-
write_uint32(msg, begin);
|
|
129
|
-
msg.insert(msg.end(), data, data + length);
|
|
130
|
-
return msg;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
std::vector<uint8_t> BtMessageEncoder::encode_piece(const PieceMessage& piece) {
|
|
134
|
-
return encode_piece(piece.piece_index, piece.begin,
|
|
135
|
-
piece.data.data(), piece.data.size());
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
std::vector<uint8_t> BtMessageEncoder::encode_cancel(uint32_t piece_index,
|
|
139
|
-
uint32_t begin,
|
|
140
|
-
uint32_t length) {
|
|
141
|
-
std::vector<uint8_t> msg;
|
|
142
|
-
write_uint32(msg, 13); // length = 1 + 4 + 4 + 4
|
|
143
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Cancel));
|
|
144
|
-
write_uint32(msg, piece_index);
|
|
145
|
-
write_uint32(msg, begin);
|
|
146
|
-
write_uint32(msg, length);
|
|
147
|
-
return msg;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
std::vector<uint8_t> BtMessageEncoder::encode_cancel(const RequestMessage& req) {
|
|
151
|
-
return encode_cancel(req.piece_index, req.begin, req.length);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
std::vector<uint8_t> BtMessageEncoder::encode_port(uint16_t port) {
|
|
155
|
-
std::vector<uint8_t> msg;
|
|
156
|
-
write_uint32(msg, 3); // length = 1 + 2
|
|
157
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Port));
|
|
158
|
-
write_uint16(msg, port);
|
|
159
|
-
return msg;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
//=============================================================================
|
|
163
|
-
// Fast Extension Encoding (BEP 6)
|
|
164
|
-
//=============================================================================
|
|
165
|
-
|
|
166
|
-
std::vector<uint8_t> BtMessageEncoder::encode_have_all() {
|
|
167
|
-
std::vector<uint8_t> msg;
|
|
168
|
-
write_uint32(msg, 1);
|
|
169
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::HaveAll));
|
|
170
|
-
return msg;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
std::vector<uint8_t> BtMessageEncoder::encode_have_none() {
|
|
174
|
-
std::vector<uint8_t> msg;
|
|
175
|
-
write_uint32(msg, 1);
|
|
176
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::HaveNone));
|
|
177
|
-
return msg;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
std::vector<uint8_t> BtMessageEncoder::encode_suggest_piece(uint32_t piece_index) {
|
|
181
|
-
std::vector<uint8_t> msg;
|
|
182
|
-
write_uint32(msg, 5);
|
|
183
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::SuggestPiece));
|
|
184
|
-
write_uint32(msg, piece_index);
|
|
185
|
-
return msg;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
std::vector<uint8_t> BtMessageEncoder::encode_reject_request(uint32_t piece_index,
|
|
189
|
-
uint32_t begin,
|
|
190
|
-
uint32_t length) {
|
|
191
|
-
std::vector<uint8_t> msg;
|
|
192
|
-
write_uint32(msg, 13);
|
|
193
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::RejectRequest));
|
|
194
|
-
write_uint32(msg, piece_index);
|
|
195
|
-
write_uint32(msg, begin);
|
|
196
|
-
write_uint32(msg, length);
|
|
197
|
-
return msg;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
std::vector<uint8_t> BtMessageEncoder::encode_allowed_fast(uint32_t piece_index) {
|
|
201
|
-
std::vector<uint8_t> msg;
|
|
202
|
-
write_uint32(msg, 5);
|
|
203
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::AllowedFast));
|
|
204
|
-
write_uint32(msg, piece_index);
|
|
205
|
-
return msg;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
//=============================================================================
|
|
209
|
-
// Extension Protocol Encoding (BEP 10)
|
|
210
|
-
//=============================================================================
|
|
211
|
-
|
|
212
|
-
std::vector<uint8_t> BtMessageEncoder::encode_extended(uint8_t extension_id,
|
|
213
|
-
const std::vector<uint8_t>& payload) {
|
|
214
|
-
std::vector<uint8_t> msg;
|
|
215
|
-
write_uint32(msg, static_cast<uint32_t>(2 + payload.size())); // length = 1 + 1 + payload
|
|
216
|
-
msg.push_back(static_cast<uint8_t>(BtMessageType::Extended));
|
|
217
|
-
msg.push_back(extension_id);
|
|
218
|
-
msg.insert(msg.end(), payload.begin(), payload.end());
|
|
219
|
-
return msg;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
//=============================================================================
|
|
223
|
-
// Decoder Helper Methods
|
|
224
|
-
//=============================================================================
|
|
225
|
-
|
|
226
|
-
uint32_t BtMessageDecoder::read_uint32(const uint8_t* data) {
|
|
227
|
-
return (static_cast<uint32_t>(data[0]) << 24) |
|
|
228
|
-
(static_cast<uint32_t>(data[1]) << 16) |
|
|
229
|
-
(static_cast<uint32_t>(data[2]) << 8) |
|
|
230
|
-
static_cast<uint32_t>(data[3]);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
uint16_t BtMessageDecoder::read_uint16(const uint8_t* data) {
|
|
234
|
-
return (static_cast<uint16_t>(data[0]) << 8) |
|
|
235
|
-
static_cast<uint16_t>(data[1]);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
//=============================================================================
|
|
239
|
-
// Message Decoding
|
|
240
|
-
//=============================================================================
|
|
241
|
-
|
|
242
|
-
size_t BtMessageDecoder::message_length(const uint8_t* data, size_t length) {
|
|
243
|
-
if (length < 4) {
|
|
244
|
-
return 0; // Not enough data to read length prefix
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
uint32_t msg_len = read_uint32(data);
|
|
248
|
-
size_t total_len = 4 + msg_len; // 4-byte length prefix + message
|
|
249
|
-
|
|
250
|
-
if (length < total_len) {
|
|
251
|
-
return 0; // Message not complete
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
return total_len;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
bool BtMessageDecoder::is_keepalive(const uint8_t* data, size_t length) {
|
|
258
|
-
if (length < 4) return false;
|
|
259
|
-
return read_uint32(data) == 0;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
std::optional<BtMessage> BtMessageDecoder::decode(const uint8_t* data,
|
|
263
|
-
size_t length,
|
|
264
|
-
uint32_t num_pieces) {
|
|
265
|
-
if (length < 4) {
|
|
266
|
-
return std::nullopt;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
uint32_t msg_len = read_uint32(data);
|
|
270
|
-
|
|
271
|
-
// Keep-alive: length = 0
|
|
272
|
-
if (msg_len == 0) {
|
|
273
|
-
return BtMessage(BtMessageType::Choke); // Treat as choke for simplicity
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (length < 4 + msg_len || msg_len < 1) {
|
|
277
|
-
return std::nullopt;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
const uint8_t* payload = data + 4;
|
|
281
|
-
uint8_t type_byte = payload[0];
|
|
282
|
-
|
|
283
|
-
BtMessage msg;
|
|
284
|
-
msg.type = static_cast<BtMessageType>(type_byte);
|
|
285
|
-
|
|
286
|
-
switch (msg.type) {
|
|
287
|
-
case BtMessageType::Choke:
|
|
288
|
-
case BtMessageType::Unchoke:
|
|
289
|
-
case BtMessageType::Interested:
|
|
290
|
-
case BtMessageType::NotInterested:
|
|
291
|
-
case BtMessageType::HaveAll:
|
|
292
|
-
case BtMessageType::HaveNone:
|
|
293
|
-
// No additional data
|
|
294
|
-
if (msg_len != 1) return std::nullopt;
|
|
295
|
-
break;
|
|
296
|
-
|
|
297
|
-
case BtMessageType::Have:
|
|
298
|
-
case BtMessageType::SuggestPiece:
|
|
299
|
-
case BtMessageType::AllowedFast:
|
|
300
|
-
// 4-byte piece index
|
|
301
|
-
if (msg_len != 5) return std::nullopt;
|
|
302
|
-
msg.have_piece = read_uint32(payload + 1);
|
|
303
|
-
break;
|
|
304
|
-
|
|
305
|
-
case BtMessageType::Port:
|
|
306
|
-
// 2-byte port
|
|
307
|
-
if (msg_len != 3) return std::nullopt;
|
|
308
|
-
msg.port = read_uint16(payload + 1);
|
|
309
|
-
break;
|
|
310
|
-
|
|
311
|
-
case BtMessageType::Bitfield: {
|
|
312
|
-
// Variable length
|
|
313
|
-
size_t bf_len = msg_len - 1;
|
|
314
|
-
size_t expected_bits = num_pieces > 0 ? num_pieces : bf_len * 8;
|
|
315
|
-
msg.bitfield = Bitfield::from_bytes(payload + 1, bf_len, expected_bits);
|
|
316
|
-
break;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
case BtMessageType::Request:
|
|
320
|
-
case BtMessageType::Cancel:
|
|
321
|
-
case BtMessageType::RejectRequest:
|
|
322
|
-
// 12 bytes: piece index (4) + begin (4) + length (4)
|
|
323
|
-
if (msg_len != 13) return std::nullopt;
|
|
324
|
-
msg.request = RequestMessage(
|
|
325
|
-
read_uint32(payload + 1),
|
|
326
|
-
read_uint32(payload + 5),
|
|
327
|
-
read_uint32(payload + 9)
|
|
328
|
-
);
|
|
329
|
-
break;
|
|
330
|
-
|
|
331
|
-
case BtMessageType::Piece: {
|
|
332
|
-
// Variable length: piece index (4) + begin (4) + data
|
|
333
|
-
if (msg_len < 9) return std::nullopt;
|
|
334
|
-
|
|
335
|
-
uint32_t piece_index = read_uint32(payload + 1);
|
|
336
|
-
uint32_t begin = read_uint32(payload + 5);
|
|
337
|
-
size_t data_len = msg_len - 9;
|
|
338
|
-
|
|
339
|
-
std::vector<uint8_t> piece_data(payload + 9, payload + 9 + data_len);
|
|
340
|
-
msg.piece = PieceMessage(piece_index, begin, std::move(piece_data));
|
|
341
|
-
break;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
case BtMessageType::Extended:
|
|
345
|
-
// 1-byte extension ID + variable payload
|
|
346
|
-
if (msg_len < 2) return std::nullopt;
|
|
347
|
-
msg.extension_id = payload[1];
|
|
348
|
-
msg.extension_payload.assign(payload + 2, payload + msg_len);
|
|
349
|
-
break;
|
|
350
|
-
|
|
351
|
-
default:
|
|
352
|
-
// Unknown message type
|
|
353
|
-
return std::nullopt;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
return msg;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
std::optional<BtMessage> BtMessageDecoder::decode(const std::vector<uint8_t>& data,
|
|
360
|
-
uint32_t num_pieces) {
|
|
361
|
-
return decode(data.data(), data.size(), num_pieces);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
} // namespace librats
|
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @file bt_messages.h
|
|
5
|
-
* @brief BitTorrent protocol message encoding and decoding
|
|
6
|
-
*
|
|
7
|
-
* Implements BEP 3 (BitTorrent Wire Protocol) message types.
|
|
8
|
-
* Also includes BEP 6 (Fast Extension) and BEP 10 (Extension Protocol).
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
#include "bt_types.h"
|
|
12
|
-
#include "bt_bitfield.h"
|
|
13
|
-
|
|
14
|
-
#include <vector>
|
|
15
|
-
#include <cstdint>
|
|
16
|
-
#include <optional>
|
|
17
|
-
#include <string>
|
|
18
|
-
|
|
19
|
-
namespace librats {
|
|
20
|
-
|
|
21
|
-
//=============================================================================
|
|
22
|
-
// Message Types (BEP 3)
|
|
23
|
-
//=============================================================================
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @brief BitTorrent message type IDs
|
|
27
|
-
*/
|
|
28
|
-
enum class BtMessageType : uint8_t {
|
|
29
|
-
// Core protocol (BEP 3)
|
|
30
|
-
Choke = 0,
|
|
31
|
-
Unchoke = 1,
|
|
32
|
-
Interested = 2,
|
|
33
|
-
NotInterested = 3,
|
|
34
|
-
Have = 4,
|
|
35
|
-
Bitfield = 5,
|
|
36
|
-
Request = 6,
|
|
37
|
-
Piece = 7,
|
|
38
|
-
Cancel = 8,
|
|
39
|
-
|
|
40
|
-
// DHT extension (BEP 5)
|
|
41
|
-
Port = 9,
|
|
42
|
-
|
|
43
|
-
// Fast extension (BEP 6)
|
|
44
|
-
SuggestPiece = 13,
|
|
45
|
-
HaveAll = 14,
|
|
46
|
-
HaveNone = 15,
|
|
47
|
-
RejectRequest = 16,
|
|
48
|
-
AllowedFast = 17,
|
|
49
|
-
|
|
50
|
-
// Extension protocol (BEP 10)
|
|
51
|
-
Extended = 20
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* @brief Convert message type to string for debugging
|
|
56
|
-
*/
|
|
57
|
-
const char* message_type_to_string(BtMessageType type);
|
|
58
|
-
|
|
59
|
-
//=============================================================================
|
|
60
|
-
// Message Structures
|
|
61
|
-
//=============================================================================
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @brief Request/Cancel message data
|
|
65
|
-
*/
|
|
66
|
-
struct RequestMessage {
|
|
67
|
-
uint32_t piece_index;
|
|
68
|
-
uint32_t begin;
|
|
69
|
-
uint32_t length;
|
|
70
|
-
|
|
71
|
-
RequestMessage() : piece_index(0), begin(0), length(0) {}
|
|
72
|
-
RequestMessage(uint32_t idx, uint32_t b, uint32_t len)
|
|
73
|
-
: piece_index(idx), begin(b), length(len) {}
|
|
74
|
-
|
|
75
|
-
bool operator==(const RequestMessage& other) const {
|
|
76
|
-
return piece_index == other.piece_index &&
|
|
77
|
-
begin == other.begin &&
|
|
78
|
-
length == other.length;
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* @brief Piece message data (block of data)
|
|
84
|
-
*/
|
|
85
|
-
struct PieceMessage {
|
|
86
|
-
uint32_t piece_index;
|
|
87
|
-
uint32_t begin;
|
|
88
|
-
std::vector<uint8_t> data;
|
|
89
|
-
|
|
90
|
-
PieceMessage() : piece_index(0), begin(0) {}
|
|
91
|
-
PieceMessage(uint32_t idx, uint32_t b, std::vector<uint8_t> d)
|
|
92
|
-
: piece_index(idx), begin(b), data(std::move(d)) {}
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @brief Parsed message from the wire
|
|
97
|
-
*/
|
|
98
|
-
struct BtMessage {
|
|
99
|
-
BtMessageType type;
|
|
100
|
-
|
|
101
|
-
// Data depending on type
|
|
102
|
-
union {
|
|
103
|
-
uint32_t have_piece; // Have
|
|
104
|
-
uint32_t port; // Port (DHT)
|
|
105
|
-
uint32_t suggest_piece; // SuggestPiece
|
|
106
|
-
uint32_t allowed_fast; // AllowedFast
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
// For complex message types (stored separately)
|
|
110
|
-
std::optional<Bitfield> bitfield;
|
|
111
|
-
std::optional<RequestMessage> request;
|
|
112
|
-
std::optional<PieceMessage> piece;
|
|
113
|
-
|
|
114
|
-
// For Extended messages
|
|
115
|
-
uint8_t extension_id;
|
|
116
|
-
std::vector<uint8_t> extension_payload;
|
|
117
|
-
|
|
118
|
-
BtMessage() : type(BtMessageType::Choke), have_piece(0), extension_id(0) {}
|
|
119
|
-
explicit BtMessage(BtMessageType t) : type(t), have_piece(0), extension_id(0) {}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
//=============================================================================
|
|
123
|
-
// Message Encoder
|
|
124
|
-
//=============================================================================
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* @brief Encodes BitTorrent protocol messages to wire format
|
|
128
|
-
*/
|
|
129
|
-
class BtMessageEncoder {
|
|
130
|
-
public:
|
|
131
|
-
/**
|
|
132
|
-
* @brief Encode a keep-alive message (length = 0)
|
|
133
|
-
*/
|
|
134
|
-
static std::vector<uint8_t> encode_keepalive();
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* @brief Encode a choke message
|
|
138
|
-
*/
|
|
139
|
-
static std::vector<uint8_t> encode_choke();
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* @brief Encode an unchoke message
|
|
143
|
-
*/
|
|
144
|
-
static std::vector<uint8_t> encode_unchoke();
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* @brief Encode an interested message
|
|
148
|
-
*/
|
|
149
|
-
static std::vector<uint8_t> encode_interested();
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* @brief Encode a not interested message
|
|
153
|
-
*/
|
|
154
|
-
static std::vector<uint8_t> encode_not_interested();
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* @brief Encode a have message
|
|
158
|
-
* @param piece_index Index of the piece we now have
|
|
159
|
-
*/
|
|
160
|
-
static std::vector<uint8_t> encode_have(uint32_t piece_index);
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* @brief Encode a bitfield message
|
|
164
|
-
* @param bitfield Our have bitfield
|
|
165
|
-
*/
|
|
166
|
-
static std::vector<uint8_t> encode_bitfield(const Bitfield& bitfield);
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* @brief Encode a request message
|
|
170
|
-
* @param piece_index Piece index
|
|
171
|
-
* @param begin Offset within piece
|
|
172
|
-
* @param length Length of block (usually 16384)
|
|
173
|
-
*/
|
|
174
|
-
static std::vector<uint8_t> encode_request(uint32_t piece_index,
|
|
175
|
-
uint32_t begin,
|
|
176
|
-
uint32_t length);
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* @brief Encode a request message from RequestMessage struct
|
|
180
|
-
*/
|
|
181
|
-
static std::vector<uint8_t> encode_request(const RequestMessage& req);
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* @brief Encode a piece message (block data)
|
|
185
|
-
* @param piece_index Piece index
|
|
186
|
-
* @param begin Offset within piece
|
|
187
|
-
* @param data Block data
|
|
188
|
-
*/
|
|
189
|
-
static std::vector<uint8_t> encode_piece(uint32_t piece_index,
|
|
190
|
-
uint32_t begin,
|
|
191
|
-
const uint8_t* data,
|
|
192
|
-
size_t length);
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* @brief Encode a piece message from PieceMessage struct
|
|
196
|
-
*/
|
|
197
|
-
static std::vector<uint8_t> encode_piece(const PieceMessage& piece);
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* @brief Encode a cancel message
|
|
201
|
-
*/
|
|
202
|
-
static std::vector<uint8_t> encode_cancel(uint32_t piece_index,
|
|
203
|
-
uint32_t begin,
|
|
204
|
-
uint32_t length);
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* @brief Encode a cancel message from RequestMessage struct
|
|
208
|
-
*/
|
|
209
|
-
static std::vector<uint8_t> encode_cancel(const RequestMessage& req);
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* @brief Encode a port message (DHT port)
|
|
213
|
-
*/
|
|
214
|
-
static std::vector<uint8_t> encode_port(uint16_t port);
|
|
215
|
-
|
|
216
|
-
//=========================================================================
|
|
217
|
-
// Fast Extension (BEP 6)
|
|
218
|
-
//=========================================================================
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* @brief Encode a have-all message
|
|
222
|
-
*/
|
|
223
|
-
static std::vector<uint8_t> encode_have_all();
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* @brief Encode a have-none message
|
|
227
|
-
*/
|
|
228
|
-
static std::vector<uint8_t> encode_have_none();
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* @brief Encode a suggest piece message
|
|
232
|
-
*/
|
|
233
|
-
static std::vector<uint8_t> encode_suggest_piece(uint32_t piece_index);
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* @brief Encode a reject request message
|
|
237
|
-
*/
|
|
238
|
-
static std::vector<uint8_t> encode_reject_request(uint32_t piece_index,
|
|
239
|
-
uint32_t begin,
|
|
240
|
-
uint32_t length);
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* @brief Encode an allowed fast message
|
|
244
|
-
*/
|
|
245
|
-
static std::vector<uint8_t> encode_allowed_fast(uint32_t piece_index);
|
|
246
|
-
|
|
247
|
-
//=========================================================================
|
|
248
|
-
// Extension Protocol (BEP 10)
|
|
249
|
-
//=========================================================================
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* @brief Encode an extended message
|
|
253
|
-
* @param extension_id Extension message ID (0 for handshake)
|
|
254
|
-
* @param payload Bencoded payload
|
|
255
|
-
*/
|
|
256
|
-
static std::vector<uint8_t> encode_extended(uint8_t extension_id,
|
|
257
|
-
const std::vector<uint8_t>& payload);
|
|
258
|
-
|
|
259
|
-
private:
|
|
260
|
-
/**
|
|
261
|
-
* @brief Write a 32-bit big-endian integer to buffer
|
|
262
|
-
*/
|
|
263
|
-
static void write_uint32(std::vector<uint8_t>& buf, uint32_t value);
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* @brief Write a 16-bit big-endian integer to buffer
|
|
267
|
-
*/
|
|
268
|
-
static void write_uint16(std::vector<uint8_t>& buf, uint16_t value);
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
//=============================================================================
|
|
272
|
-
// Message Decoder
|
|
273
|
-
//=============================================================================
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* @brief Decodes BitTorrent protocol messages from wire format
|
|
277
|
-
*/
|
|
278
|
-
class BtMessageDecoder {
|
|
279
|
-
public:
|
|
280
|
-
/**
|
|
281
|
-
* @brief Check if there's a complete message in the buffer
|
|
282
|
-
*
|
|
283
|
-
* @param data Buffer data
|
|
284
|
-
* @param length Buffer length
|
|
285
|
-
* @return Size of complete message (including 4-byte length prefix), or 0 if incomplete
|
|
286
|
-
*/
|
|
287
|
-
static size_t message_length(const uint8_t* data, size_t length);
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* @brief Check if buffer contains a keep-alive (length = 0)
|
|
291
|
-
*/
|
|
292
|
-
static bool is_keepalive(const uint8_t* data, size_t length);
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* @brief Decode a message from the buffer
|
|
296
|
-
*
|
|
297
|
-
* @param data Buffer data (starting with 4-byte length)
|
|
298
|
-
* @param length Buffer length
|
|
299
|
-
* @param num_pieces Number of pieces (for bitfield validation)
|
|
300
|
-
* @return Decoded message, or nullopt on error
|
|
301
|
-
*/
|
|
302
|
-
static std::optional<BtMessage> decode(const uint8_t* data,
|
|
303
|
-
size_t length,
|
|
304
|
-
uint32_t num_pieces = 0);
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* @brief Decode a message from vector
|
|
308
|
-
*/
|
|
309
|
-
static std::optional<BtMessage> decode(const std::vector<uint8_t>& data,
|
|
310
|
-
uint32_t num_pieces = 0);
|
|
311
|
-
|
|
312
|
-
private:
|
|
313
|
-
/**
|
|
314
|
-
* @brief Read a 32-bit big-endian integer from buffer
|
|
315
|
-
*/
|
|
316
|
-
static uint32_t read_uint32(const uint8_t* data);
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* @brief Read a 16-bit big-endian integer from buffer
|
|
320
|
-
*/
|
|
321
|
-
static uint16_t read_uint16(const uint8_t* data);
|
|
322
|
-
};
|
|
323
|
-
|
|
324
|
-
} // namespace librats
|