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
package/native-src/src/turn.h
DELETED
|
@@ -1,460 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @file turn.h
|
|
5
|
-
* @brief TURN (Traversal Using Relays around NAT) Client Implementation
|
|
6
|
-
*
|
|
7
|
-
* Implements RFC 5766 - TURN protocol for NAT traversal via relay.
|
|
8
|
-
* Provides functionality to allocate relay addresses and relay data
|
|
9
|
-
* when direct peer-to-peer connectivity is not possible.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
#include "stun.h"
|
|
13
|
-
#include <memory>
|
|
14
|
-
#include <mutex>
|
|
15
|
-
#include <unordered_map>
|
|
16
|
-
#include <chrono>
|
|
17
|
-
#include <functional>
|
|
18
|
-
#include <thread>
|
|
19
|
-
#include <atomic>
|
|
20
|
-
|
|
21
|
-
namespace librats {
|
|
22
|
-
|
|
23
|
-
// ============================================================================
|
|
24
|
-
// TURN Constants
|
|
25
|
-
// ============================================================================
|
|
26
|
-
|
|
27
|
-
/// Default TURN port
|
|
28
|
-
constexpr uint16_t TURN_DEFAULT_PORT = 3478;
|
|
29
|
-
|
|
30
|
-
/// Default TURN TLS port
|
|
31
|
-
constexpr uint16_t TURNS_DEFAULT_PORT = 5349;
|
|
32
|
-
|
|
33
|
-
/// Default allocation lifetime (seconds)
|
|
34
|
-
constexpr uint32_t TURN_DEFAULT_LIFETIME = 600;
|
|
35
|
-
|
|
36
|
-
/// Minimum channel number (RFC 5766)
|
|
37
|
-
constexpr uint16_t TURN_CHANNEL_MIN = 0x4000;
|
|
38
|
-
|
|
39
|
-
/// Maximum channel number (RFC 5766)
|
|
40
|
-
constexpr uint16_t TURN_CHANNEL_MAX = 0x7FFF;
|
|
41
|
-
|
|
42
|
-
/// Channel header size
|
|
43
|
-
constexpr size_t TURN_CHANNEL_HEADER_SIZE = 4;
|
|
44
|
-
|
|
45
|
-
/// UDP transport protocol number
|
|
46
|
-
constexpr uint8_t TURN_TRANSPORT_UDP = 17;
|
|
47
|
-
|
|
48
|
-
/// TCP transport protocol number
|
|
49
|
-
constexpr uint8_t TURN_TRANSPORT_TCP = 6;
|
|
50
|
-
|
|
51
|
-
// ============================================================================
|
|
52
|
-
// TURN Data Structures
|
|
53
|
-
// ============================================================================
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* TURN allocation state
|
|
57
|
-
*/
|
|
58
|
-
enum class TurnAllocationState {
|
|
59
|
-
None, ///< No allocation
|
|
60
|
-
Allocating, ///< Allocation in progress
|
|
61
|
-
Allocated, ///< Successfully allocated
|
|
62
|
-
Refreshing, ///< Refreshing allocation
|
|
63
|
-
Failed ///< Allocation failed
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* TURN allocation information
|
|
68
|
-
*/
|
|
69
|
-
struct TurnAllocation {
|
|
70
|
-
StunMappedAddress relay_address; ///< Relay address assigned by server
|
|
71
|
-
StunMappedAddress mapped_address; ///< Our reflexive address as seen by server
|
|
72
|
-
uint32_t lifetime; ///< Remaining lifetime in seconds
|
|
73
|
-
std::chrono::steady_clock::time_point allocated_at; ///< When allocation was made
|
|
74
|
-
std::chrono::steady_clock::time_point expires_at; ///< When allocation expires
|
|
75
|
-
|
|
76
|
-
TurnAllocation() : lifetime(0) {}
|
|
77
|
-
|
|
78
|
-
bool is_valid() const {
|
|
79
|
-
return relay_address.is_valid() && lifetime > 0;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
bool is_expired() const {
|
|
83
|
-
return std::chrono::steady_clock::now() >= expires_at;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
uint32_t remaining_lifetime() const {
|
|
87
|
-
auto now = std::chrono::steady_clock::now();
|
|
88
|
-
if (now >= expires_at) return 0;
|
|
89
|
-
return static_cast<uint32_t>(
|
|
90
|
-
std::chrono::duration_cast<std::chrono::seconds>(expires_at - now).count());
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* TURN permission for a peer
|
|
96
|
-
*/
|
|
97
|
-
struct TurnPermission {
|
|
98
|
-
std::string peer_address; ///< Peer IP address (without port)
|
|
99
|
-
std::chrono::steady_clock::time_point expires_at;
|
|
100
|
-
|
|
101
|
-
bool is_expired() const {
|
|
102
|
-
return std::chrono::steady_clock::now() >= expires_at;
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* TURN channel binding
|
|
108
|
-
*/
|
|
109
|
-
struct TurnChannelBinding {
|
|
110
|
-
uint16_t channel_number;
|
|
111
|
-
StunMappedAddress peer_address;
|
|
112
|
-
std::chrono::steady_clock::time_point expires_at;
|
|
113
|
-
|
|
114
|
-
bool is_expired() const {
|
|
115
|
-
return std::chrono::steady_clock::now() >= expires_at;
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* TURN client configuration
|
|
121
|
-
*/
|
|
122
|
-
struct TurnClientConfig {
|
|
123
|
-
std::string server; ///< TURN server hostname/IP
|
|
124
|
-
uint16_t port = TURN_DEFAULT_PORT; ///< TURN server port
|
|
125
|
-
std::string username; ///< Long-term credential username
|
|
126
|
-
std::string password; ///< Long-term credential password
|
|
127
|
-
std::string realm; ///< Authentication realm (usually discovered)
|
|
128
|
-
uint32_t requested_lifetime = TURN_DEFAULT_LIFETIME; ///< Requested allocation lifetime
|
|
129
|
-
int timeout_ms = 5000; ///< Request timeout in milliseconds
|
|
130
|
-
bool auto_refresh = true; ///< Automatically refresh allocation
|
|
131
|
-
std::string software = "librats"; ///< Software attribute value
|
|
132
|
-
|
|
133
|
-
TurnClientConfig() = default;
|
|
134
|
-
TurnClientConfig(const std::string& srv, const std::string& user, const std::string& pass)
|
|
135
|
-
: server(srv), username(user), password(pass) {}
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* TURN operation result
|
|
140
|
-
*/
|
|
141
|
-
struct TurnResult {
|
|
142
|
-
bool success = false;
|
|
143
|
-
std::optional<StunError> error;
|
|
144
|
-
std::string error_message;
|
|
145
|
-
|
|
146
|
-
TurnResult() = default;
|
|
147
|
-
explicit TurnResult(bool s) : success(s) {}
|
|
148
|
-
|
|
149
|
-
static TurnResult Success() { return TurnResult(true); }
|
|
150
|
-
static TurnResult Error(const std::string& msg) {
|
|
151
|
-
TurnResult r;
|
|
152
|
-
r.error_message = msg;
|
|
153
|
-
return r;
|
|
154
|
-
}
|
|
155
|
-
static TurnResult Error(StunErrorCode code, const std::string& msg = "") {
|
|
156
|
-
TurnResult r;
|
|
157
|
-
r.error = StunError(code, msg);
|
|
158
|
-
r.error_message = msg;
|
|
159
|
-
return r;
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Callback for received data via TURN relay
|
|
165
|
-
*/
|
|
166
|
-
using TurnDataCallback = std::function<void(const StunMappedAddress& peer,
|
|
167
|
-
const std::vector<uint8_t>& data)>;
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Callback for allocation state changes
|
|
171
|
-
*/
|
|
172
|
-
using TurnStateCallback = std::function<void(TurnAllocationState state)>;
|
|
173
|
-
|
|
174
|
-
// ============================================================================
|
|
175
|
-
// TURN Client
|
|
176
|
-
// ============================================================================
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* TURN Client for NAT traversal via relay
|
|
180
|
-
*
|
|
181
|
-
* Provides:
|
|
182
|
-
* - Allocation management with automatic refresh
|
|
183
|
-
* - Permission creation for peers
|
|
184
|
-
* - Channel binding for efficient data transfer
|
|
185
|
-
* - Data relay via Send/Data indications or channels
|
|
186
|
-
*
|
|
187
|
-
* Example usage:
|
|
188
|
-
* @code
|
|
189
|
-
* TurnClientConfig config;
|
|
190
|
-
* config.server = "turn.example.com";
|
|
191
|
-
* config.username = "user";
|
|
192
|
-
* config.password = "pass";
|
|
193
|
-
*
|
|
194
|
-
* TurnClient client(config);
|
|
195
|
-
*
|
|
196
|
-
* auto alloc_result = client.allocate();
|
|
197
|
-
* if (alloc_result.success) {
|
|
198
|
-
* auto& alloc = client.get_allocation();
|
|
199
|
-
* std::cout << "Relay: " << alloc.relay_address.to_string() << std::endl;
|
|
200
|
-
*
|
|
201
|
-
* // Create permission for a peer
|
|
202
|
-
* client.create_permission("192.168.1.100");
|
|
203
|
-
*
|
|
204
|
-
* // Send data to peer via relay
|
|
205
|
-
* std::vector<uint8_t> data = {...};
|
|
206
|
-
* client.send_data(StunMappedAddress(StunAddressFamily::IPv4, "192.168.1.100", 5000), data);
|
|
207
|
-
* }
|
|
208
|
-
* @endcode
|
|
209
|
-
*/
|
|
210
|
-
class TurnClient {
|
|
211
|
-
public:
|
|
212
|
-
TurnClient();
|
|
213
|
-
explicit TurnClient(const TurnClientConfig& config);
|
|
214
|
-
~TurnClient();
|
|
215
|
-
|
|
216
|
-
// Non-copyable, movable
|
|
217
|
-
TurnClient(const TurnClient&) = delete;
|
|
218
|
-
TurnClient& operator=(const TurnClient&) = delete;
|
|
219
|
-
TurnClient(TurnClient&&) noexcept;
|
|
220
|
-
TurnClient& operator=(TurnClient&&) noexcept;
|
|
221
|
-
|
|
222
|
-
// =========================================================================
|
|
223
|
-
// Configuration
|
|
224
|
-
// =========================================================================
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Set client configuration
|
|
228
|
-
*/
|
|
229
|
-
void set_config(const TurnClientConfig& config);
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Get current configuration
|
|
233
|
-
*/
|
|
234
|
-
const TurnClientConfig& config() const { return config_; }
|
|
235
|
-
|
|
236
|
-
// =========================================================================
|
|
237
|
-
// Allocation Management
|
|
238
|
-
// =========================================================================
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Request allocation from TURN server
|
|
242
|
-
* @return Result of allocation request
|
|
243
|
-
*/
|
|
244
|
-
TurnResult allocate();
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Refresh existing allocation
|
|
248
|
-
* @param lifetime New lifetime (0 = use configured default)
|
|
249
|
-
* @return Result of refresh request
|
|
250
|
-
*/
|
|
251
|
-
TurnResult refresh(uint32_t lifetime = 0);
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Release allocation (set lifetime to 0)
|
|
255
|
-
* @return Result of release request
|
|
256
|
-
*/
|
|
257
|
-
TurnResult release();
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Get current allocation state
|
|
261
|
-
*/
|
|
262
|
-
TurnAllocationState get_state() const { return state_; }
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Get allocation information
|
|
266
|
-
*/
|
|
267
|
-
const TurnAllocation& get_allocation() const { return allocation_; }
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Check if allocation is active
|
|
271
|
-
*/
|
|
272
|
-
bool is_allocated() const {
|
|
273
|
-
return state_ == TurnAllocationState::Allocated && !allocation_.is_expired();
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
// =========================================================================
|
|
277
|
-
// Permission Management
|
|
278
|
-
// =========================================================================
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Create permission for a peer address
|
|
282
|
-
* @param peer_address Peer IP address (port is ignored)
|
|
283
|
-
* @return Result of permission request
|
|
284
|
-
*/
|
|
285
|
-
TurnResult create_permission(const std::string& peer_address);
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Create permission for a peer
|
|
289
|
-
* @param peer Peer address (port is ignored)
|
|
290
|
-
* @return Result of permission request
|
|
291
|
-
*/
|
|
292
|
-
TurnResult create_permission(const StunMappedAddress& peer);
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Create permissions for multiple peers
|
|
296
|
-
* @param peer_addresses List of peer IP addresses
|
|
297
|
-
* @return Result of permission request
|
|
298
|
-
*/
|
|
299
|
-
TurnResult create_permissions(const std::vector<std::string>& peer_addresses);
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Check if permission exists for a peer
|
|
303
|
-
*/
|
|
304
|
-
bool has_permission(const std::string& peer_address) const;
|
|
305
|
-
|
|
306
|
-
// =========================================================================
|
|
307
|
-
// Channel Binding
|
|
308
|
-
// =========================================================================
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Bind a channel to a peer for efficient data transfer
|
|
312
|
-
* @param peer Peer address to bind
|
|
313
|
-
* @return Channel number on success, 0 on failure
|
|
314
|
-
*/
|
|
315
|
-
uint16_t bind_channel(const StunMappedAddress& peer);
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Get channel number for a peer (if bound)
|
|
319
|
-
* @return Channel number or 0 if not bound
|
|
320
|
-
*/
|
|
321
|
-
uint16_t get_channel(const StunMappedAddress& peer) const;
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Get peer address for a channel
|
|
325
|
-
* @return Peer address or empty if channel not bound
|
|
326
|
-
*/
|
|
327
|
-
std::optional<StunMappedAddress> get_channel_peer(uint16_t channel) const;
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Refresh channel binding
|
|
331
|
-
*/
|
|
332
|
-
TurnResult refresh_channel(uint16_t channel);
|
|
333
|
-
|
|
334
|
-
// =========================================================================
|
|
335
|
-
// Data Transfer
|
|
336
|
-
// =========================================================================
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Send data to a peer via TURN relay
|
|
340
|
-
* Uses channel if bound, otherwise Send indication
|
|
341
|
-
* @param peer Destination peer
|
|
342
|
-
* @param data Data to send
|
|
343
|
-
* @return true if sent successfully
|
|
344
|
-
*/
|
|
345
|
-
bool send_data(const StunMappedAddress& peer, const std::vector<uint8_t>& data);
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* Send data to a peer via Send indication (creates permission if needed)
|
|
349
|
-
* @param peer Destination peer
|
|
350
|
-
* @param data Data to send
|
|
351
|
-
* @return true if sent successfully
|
|
352
|
-
*/
|
|
353
|
-
bool send_indication(const StunMappedAddress& peer, const std::vector<uint8_t>& data);
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* Send data via channel (more efficient, requires channel binding)
|
|
357
|
-
* @param channel Channel number
|
|
358
|
-
* @param data Data to send
|
|
359
|
-
* @return true if sent successfully
|
|
360
|
-
*/
|
|
361
|
-
bool send_channel_data(uint16_t channel, const std::vector<uint8_t>& data);
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Receive data from TURN server
|
|
365
|
-
* Call this periodically or when socket is readable
|
|
366
|
-
* @param timeout_ms Timeout in milliseconds (0 = non-blocking)
|
|
367
|
-
* @return Received data with peer info, or empty if no data
|
|
368
|
-
*/
|
|
369
|
-
std::optional<std::pair<StunMappedAddress, std::vector<uint8_t>>> receive_data(int timeout_ms = 0);
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Set callback for received data
|
|
373
|
-
*/
|
|
374
|
-
void set_data_callback(TurnDataCallback callback);
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Set callback for state changes
|
|
378
|
-
*/
|
|
379
|
-
void set_state_callback(TurnStateCallback callback);
|
|
380
|
-
|
|
381
|
-
// =========================================================================
|
|
382
|
-
// Socket Access
|
|
383
|
-
// =========================================================================
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* Get the UDP socket used for TURN communication
|
|
387
|
-
* Useful for select/poll integration
|
|
388
|
-
*/
|
|
389
|
-
socket_t get_socket() const { return socket_; }
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* Process incoming data on socket
|
|
393
|
-
* Should be called when socket becomes readable
|
|
394
|
-
*/
|
|
395
|
-
void process_incoming();
|
|
396
|
-
|
|
397
|
-
// =========================================================================
|
|
398
|
-
// Lifecycle
|
|
399
|
-
// =========================================================================
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Start automatic refresh thread
|
|
403
|
-
*/
|
|
404
|
-
void start_refresh_thread();
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* Stop automatic refresh thread
|
|
408
|
-
*/
|
|
409
|
-
void stop_refresh_thread();
|
|
410
|
-
|
|
411
|
-
/**
|
|
412
|
-
* Close client and release resources
|
|
413
|
-
*/
|
|
414
|
-
void close();
|
|
415
|
-
|
|
416
|
-
private:
|
|
417
|
-
// Configuration
|
|
418
|
-
TurnClientConfig config_;
|
|
419
|
-
|
|
420
|
-
// Network
|
|
421
|
-
socket_t socket_ = INVALID_SOCKET_VALUE;
|
|
422
|
-
StunClient stun_client_;
|
|
423
|
-
|
|
424
|
-
// State
|
|
425
|
-
TurnAllocationState state_ = TurnAllocationState::None;
|
|
426
|
-
TurnAllocation allocation_;
|
|
427
|
-
std::string nonce_; // Server nonce for authentication
|
|
428
|
-
std::string realm_; // Discovered realm
|
|
429
|
-
|
|
430
|
-
// Permissions and channels
|
|
431
|
-
mutable std::mutex mutex_;
|
|
432
|
-
std::unordered_map<std::string, TurnPermission> permissions_;
|
|
433
|
-
std::unordered_map<uint16_t, TurnChannelBinding> channels_;
|
|
434
|
-
std::unordered_map<std::string, uint16_t> peer_to_channel_;
|
|
435
|
-
uint16_t next_channel_ = TURN_CHANNEL_MIN;
|
|
436
|
-
|
|
437
|
-
// Callbacks
|
|
438
|
-
TurnDataCallback data_callback_;
|
|
439
|
-
TurnStateCallback state_callback_;
|
|
440
|
-
|
|
441
|
-
// Refresh thread
|
|
442
|
-
std::atomic<bool> refresh_running_{false};
|
|
443
|
-
std::thread refresh_thread_;
|
|
444
|
-
|
|
445
|
-
// Internal methods
|
|
446
|
-
bool ensure_socket();
|
|
447
|
-
std::vector<uint8_t> compute_message_integrity_key() const;
|
|
448
|
-
TurnResult send_allocate_request(bool with_credentials);
|
|
449
|
-
TurnResult send_refresh_request(uint32_t lifetime);
|
|
450
|
-
TurnResult send_create_permission_request(const std::vector<StunMappedAddress>& peers);
|
|
451
|
-
TurnResult send_channel_bind_request(uint16_t channel, const StunMappedAddress& peer);
|
|
452
|
-
void handle_response(const StunMessage& response);
|
|
453
|
-
void handle_data_indication(const StunMessage& indication);
|
|
454
|
-
void handle_channel_data(const std::vector<uint8_t>& data);
|
|
455
|
-
void set_state(TurnAllocationState new_state);
|
|
456
|
-
void refresh_loop();
|
|
457
|
-
std::string peer_key(const StunMappedAddress& peer) const;
|
|
458
|
-
};
|
|
459
|
-
|
|
460
|
-
} // namespace librats
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @file wakeup_pipe.h
|
|
5
|
-
* @brief Loopback-UDP self-pipe for interrupting a blocking select()/receive.
|
|
6
|
-
*
|
|
7
|
-
* A worker thread that blocks in receive_udp_data() can only react to a stop
|
|
8
|
-
* request after its socket timeout expires. WakeupPipe provides a second socket
|
|
9
|
-
* to add to that select() set: stop() sends a one-byte datagram to it, which
|
|
10
|
-
* wakes the select() immediately so the worker can observe stop_requested_ and
|
|
11
|
-
* exit without waiting out the timeout.
|
|
12
|
-
*
|
|
13
|
-
* UDP loopback is used (rather than a pipe) because it is selectable on every
|
|
14
|
-
* platform, including Windows. If socket creation fails the pipe degrades
|
|
15
|
-
* gracefully: fd() returns an invalid socket (treated as "no interrupt") and
|
|
16
|
-
* signal() is a no-op, so callers simply fall back to timeout-based wakeups.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
#include "socket.h"
|
|
20
|
-
|
|
21
|
-
#include <vector>
|
|
22
|
-
|
|
23
|
-
namespace librats {
|
|
24
|
-
|
|
25
|
-
class WakeupPipe {
|
|
26
|
-
public:
|
|
27
|
-
WakeupPipe() {
|
|
28
|
-
sock_ = create_udp_socket(0, "127.0.0.1", AddressFamily::IPv4);
|
|
29
|
-
if (is_valid_socket(sock_)) {
|
|
30
|
-
sockaddr_in addr;
|
|
31
|
-
socklen_t len = sizeof(addr);
|
|
32
|
-
if (getsockname(sock_, reinterpret_cast<sockaddr*>(&addr), &len) == 0) {
|
|
33
|
-
port_ = ntohs(addr.sin_port);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
~WakeupPipe() {
|
|
39
|
-
if (is_valid_socket(sock_)) close_socket(sock_);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
WakeupPipe(const WakeupPipe&) = delete;
|
|
43
|
-
WakeupPipe& operator=(const WakeupPipe&) = delete;
|
|
44
|
-
|
|
45
|
-
/// Socket to add to a select() set (pass as receive_udp_data's interrupt_fd).
|
|
46
|
-
socket_t fd() const { return sock_; }
|
|
47
|
-
|
|
48
|
-
/// Wake any select() watching fd(). Idempotent; only meant to be called from stop().
|
|
49
|
-
void signal() {
|
|
50
|
-
if (is_valid_socket(sock_) && port_ != 0) {
|
|
51
|
-
send_udp_data(sock_, std::vector<uint8_t>{1}, "127.0.0.1", port_, AddressFamily::IPv4);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
private:
|
|
56
|
-
socket_t sock_ = INVALID_SOCKET_VALUE;
|
|
57
|
-
uint16_t port_ = 0;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
} // namespace librats
|
|
File without changes
|