Back to Site
Loading...
Searching...
No Matches
rats.h
Go to the documentation of this file.
1#pragma once
2
34
35#include "librats/util/rats_export.h"
36
37#include <stddef.h>
38#include <stdint.h>
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* C lets an enum hold any int, and a binding passes whatever int it has. C++
45 * gives an enum without a fixed underlying type only the range of its
46 * enumerators, so there loading another value — the very one a check exists to
47 * refuse — would be undefined. In C++ these enums are therefore int-based: the
48 * same size and calling convention as in C. */
49#ifdef __cplusplus
50#define RATS_ENUM_BASE : int
51#else
52#define RATS_ENUM_BASE
53#endif
54
55typedef struct rats_node* rats_t;
56
57/* ─── Results ─────────────────────────────────────────────────────────────── */
58
59typedef enum RATS_ENUM_BASE {
61 RATS_ERR_INVALID_ARG = 1, /* NULL or malformed argument (bad peer id, bad JSON, …) */
62 RATS_ERR_NOT_STARTED = 2, /* the call needs a started node */
63 RATS_ERR_ALREADY_STARTED = 3, /* only allowed before rats_start() */
64 RATS_ERR_NOT_ENABLED = 4, /* the subsystem was not enabled with its rats_enable_*() */
65 RATS_ERR_NO_SUCH_PEER = 5, /* the peer is not connected */
66 RATS_ERR_NOT_FOUND = 6, /* no such transfer (or other named object) */
67 RATS_ERR_DECLINED = 7, /* nothing to do or nothing to try with: the peer is
68 * already connected, an attempt is already running
69 * or cooling down, or no peer can help */
70 RATS_ERR_BIND = 8, /* rats_start() could not open its sockets */
73
75RATS_API const char* rats_error_str(rats_error_t err);
76
77/* ─── Enumerations ────────────────────────────────────────────────────────── */
78
79typedef enum RATS_ENUM_BASE {
80 RATS_SECURITY_NOISE = 0, /* Noise XX: encrypted and authenticated (default) */
81 RATS_SECURITY_PLAINTEXT = 1 /* no encryption; ids exchanged in the clear */
83
84/* Which wire a peer connection runs on. UDP and TCP carry the same protocol
85 * under the same encrypted handshake. */
86typedef enum RATS_ENUM_BASE {
87 RATS_TRANSPORT_UDP = 0, /* the library's own transport on one shared UDP socket */
88 RATS_TRANSPORT_TCP = 1, /* one kernel socket per peer */
89 /* Carried through another peer's connection (rats_enable_relay). Only ever
90 * reported, never chosen: a relay is not something a dial can try. */
93
94/* Bitmask of transports (rats_config_t::transports, rats_transports(),
95 * rats_peer_info_t::transports). */
96#define RATS_TRANSPORT_MASK_TCP 0x1u
97#define RATS_TRANSPORT_MASK_UDP 0x2u
98
99/* Congestion controller of every UDP connection. TCP is the kernel's and
100 * ignores it. */
101typedef enum RATS_ENUM_BASE {
102 RATS_CONGESTION_BBR = 0, /* BBRv3 (default): holds its rate through loss that is
103 * not congestion (Wi-Fi, mobile, long paths) */
104 RATS_CONGESTION_RENO = 1 /* NewReno with HyStart++: backs off on every loss */
106
107typedef enum RATS_ENUM_BASE {
108 RATS_DIRECTION_OUTBOUND = 0, /* we dialed it */
109 RATS_DIRECTION_INBOUND = 1 /* it dialed us */
111
112/* Why a peer connection ended (rats_on_peer_disconnected). */
113typedef enum RATS_ENUM_BASE {
114 RATS_CLOSE_LOCAL = 0, /* we disconnected it */
115 RATS_CLOSE_PEER_CLOSED = 1, /* the peer closed cleanly */
116 RATS_CLOSE_PEER_RESET = 2, /* connection reset / socket error */
117 RATS_CLOSE_CONNECT_FAILED = 3, /* the dial never completed */
118 RATS_CLOSE_HANDSHAKE_FAILED = 4, /* secure handshake failed or timed out */
119 RATS_CLOSE_PROTOCOL_ERROR = 5, /* malformed frame or decryption failure */
120 /* We kept sending while the peer's queue was already past its limit. The one
121 * reason the application causes and can prevent: stop when rats_send() is
122 * followed by rats_peer_writable() == 0 and resume on rats_on_peer_writable().
123 * It never means one message was too big — a message of any size is queued. */
125 RATS_CLOSE_SHUTDOWN = 7, /* the node is stopping */
126 RATS_CLOSE_DUPLICATE = 8, /* replaced by another connection to the same peer */
127 RATS_CLOSE_PEER_LIMIT = 9, /* inbound refused: max_peers reached */
128 RATS_CLOSE_IDLE_TIMEOUT = 10, /* a UDP connection went silent */
129 RATS_CLOSE_DIAL_SUPERSEDED = 11 /* a dial racing over the other transport won */
131
133RATS_API const char* rats_close_reason_str(rats_close_reason_t reason);
134
135/* What the mesh has shown about this node's own NAT (rats_nat_mapping). */
136typedef enum RATS_ENUM_BASE {
137 RATS_NAT_UNKNOWN = 0, /* not enough independent observations yet */
138 RATS_NAT_OPEN = 1, /* no NAT in the path */
139 RATS_NAT_ENDPOINT_INDEPENDENT = 2, /* one external port for every peer: punchable */
140 RATS_NAT_ENDPOINT_DEPENDENT = 3 /* a new mapping per peer (symmetric): not punchable */
142
149
150/* ─── Configuration and lifecycle ─────────────────────────────────────────── */
151
152/* transport_fallback_ms value that never races the second transport. */
153#define RATS_FALLBACK_NEVER 0xFFFFFFFFu
154
155/* Node configuration. Zero in any field is the default named beside it. Strings
156 * are copied by rats_create(); they need not outlive the call. */
157typedef struct {
158 /* — identity — */
159 const char* protocol; /* app id bound into the handshake, e.g. "myapp/1.0";
160 * peers with a different one cannot connect.
161 * NULL → "librats/1.0" */
162 const char* data_dir; /* persistent state (identity.key, DHT routing table,
163 * reconnect store). NULL → a new identity every run */
164 rats_security_t security; /* RATS_SECURITY_NOISE */
165
166 /* — listening — */
167 uint16_t listen_port; /* shared by both transports; 0 → ephemeral */
168 const char* bind_address; /* NULL → "::", dual-stack on every interface */
169 int dial_only; /* non-zero: no listener, outbound connections only */
170 size_t max_peers; /* cap on established peers, enforced on inbound;
171 * 0 → unlimited */
172
173 /* — transports — */
174 uint32_t transports; /* RATS_TRANSPORT_MASK_* to run; 0 → both */
175 rats_transport_t preferred_transport; /* tried first by a dial; RATS_TRANSPORT_UDP */
176 uint32_t transport_fallback_ms; /* the preferred transport dials alone this
177 * long before the other is raced; 0 → 1200,
178 * RATS_FALLBACK_NEVER → never */
179
180 /* — sending — */
181 size_t send_queue_limit; /* bytes a peer's queue may hold before a sender that
182 * keeps going has it dropped (RATS_CLOSE_SLOW_CONSUMER);
183 * rats_peer_writable() turns 0 at a quarter of it.
184 * Not a message size limit. 0 → 8 MiB */
185 uint32_t path_sample_ms; /* how often each peer's path is sampled for
186 * rats_path_stats(); 0 → only while a
187 * rats_on_peer_path() callback is registered, every 100 ms */
188
189 /* — UDP transport — */
190 rats_congestion_t congestion_control; /* RATS_CONGESTION_BBR */
191 size_t udp_receive_window; /* ceiling one connection's flow-control window grows
192 * to (it starts at 1 MiB); throughput is bounded by
193 * window / round trip. 0 → 16 MiB */
194 size_t udp_receive_budget; /* bytes all connections together may hold out of
195 * order. 0 → 64 MiB */
196 int disable_udp_offload; /* non-zero: no kernel segmentation offload
197 * (UDP_SEGMENT/GRO, USO/URO), for comparison or a
198 * host where it misbehaves */
199 uint32_t udp_idle_timeout_ms; /* silence from a peer after which its connection
200 * is closed as lost (RATS_CLOSE_IDLE_TIMEOUT);
201 * keep-alives go out four times within it.
202 * 0 → 45 s */
203
204 /* — runtime — */
205 size_t reactor_threads; /* I/O threads; 0 → 1, enough for thousands of peers */
206 int disable_network_monitor; /* non-zero: do not watch the host for interface and
207 * address changes (saves one mostly idle thread) */
209
214RATS_API rats_t rats_create(const rats_config_t* config);
215
217RATS_API void rats_destroy(rats_t node);
218
222
224RATS_API void rats_stop(rats_t node);
225
226/* ─── This node ───────────────────────────────────────────────────────────── */
227
229RATS_API const char* rats_local_id(rats_t node);
230
233RATS_API const char* rats_protocol(rats_t node);
234
236RATS_API uint16_t rats_listen_port(rats_t node);
237
241RATS_API uint32_t rats_transports(rats_t node);
242
246
250RATS_API char** rats_observed_addresses(rats_t node, size_t* count);
251
252/* ─── Peers ───────────────────────────────────────────────────────────────── */
253
256RATS_API rats_error_t rats_connect(rats_t node, const char* host, uint16_t port);
257
259RATS_API rats_error_t rats_disconnect(rats_t node, const char* peer_id);
260
261RATS_API size_t rats_peer_count(rats_t node);
262
265RATS_API char** rats_peer_ids(rats_t node, size_t* count);
266
267typedef struct {
268 rats_transport_t transport; /* the wire this connection runs on */
269 uint32_t transports; /* RATS_TRANSPORT_MASK_* the peer says it accepts;
270 * 0 → it did not say */
272 char address[64]; /* where the peer can be dialed, "ip:port" or
273 * "[ip]:port"; "" until known */
275
277RATS_API rats_error_t rats_peer_info(rats_t node, const char* peer_id, rats_peer_info_t* out);
278
280RATS_API void rats_set_max_peers(rats_t node, size_t max_peers);
281RATS_API size_t rats_max_peers(rats_t node);
282
283/* ─── Messaging ───────────────────────────────────────────────────────────── */
284
285/* Limits of a live message (lifetime_ms > 0). A longer lifetime is cut to the
286 * maximum; a larger payload is refused. */
287#define RATS_LIVE_MAX_LIFETIME_MS 30000u
288#define RATS_LIVE_MAX_PAYLOAD (4u * 1024u * 1024u)
289
303RATS_API rats_error_t rats_send(rats_t node, const char* peer_id, const char* channel,
304 const void* data, size_t len, uint32_t lifetime_ms);
305
307RATS_API rats_error_t rats_broadcast(rats_t node, const char* channel, const void* data,
308 size_t len, uint32_t lifetime_ms);
309
315RATS_API int rats_peer_writable(rats_t node, const char* peer_id);
316
317/* What the transport knows about the path to one peer, for a sender that sizes
318 * its output to the path (a live encoder). Rates are bits per second. A new
319 * message waits behind queued_bytes; queued_bytes * 8 / bandwidth_bps is that
320 * wait in seconds. Over TCP the path figures are the kernel's and exist on Linux
321 * only; elsewhere, and over a relay, only queued_bytes and the live counters are
322 * filled. */
323typedef struct {
324 uint64_t bandwidth_bps; /* the congestion controller's estimate; 0 = none yet */
325 int app_limited; /* 1: the sender was not filling the path, so the
326 * estimate is a floor, not the path's capacity */
327 uint64_t pacing_bps; /* current release rate; 0 = unpaced */
328 uint32_t srtt_us; /* smoothed round trip */
329 uint32_t min_rtt_us; /* lowest round trip seen: the path without queues */
330 uint64_t queued_bytes; /* accepted for the peer, not yet sent */
331 uint64_t bytes_in_flight; /* sent, not yet acknowledged */
332 double loss; /* share of bytes lost over the last interval, 0..1 */
333 uint64_t bytes_delivered; /* acknowledged since the connection opened */
334 uint64_t bytes_lost; /* declared lost since the connection opened */
335 uint64_t live_sent; /* live messages sent since the connection opened */
336 uint64_t live_expired; /* of those, dropped because their lifetime ran out */
337 uint64_t live_repair_bytes; /* forward-error-correction bytes sent with live
338 * messages on a lossy path, on top of the source's own */
340
344RATS_API rats_error_t rats_path_stats(rats_t node, const char* peer_id, rats_path_stats_t* out);
345
346/* ─── Callbacks (register before rats_start; run on a reactor thread) ────── */
347
348typedef void (*rats_peer_cb)(void* user, const char* peer_id);
349typedef void (*rats_disconnect_cb)(void* user, const char* peer_id, rats_close_reason_t reason);
350typedef void (*rats_dial_failed_cb)(void* user, const char* address);
351typedef void (*rats_message_cb)(void* user, const char* peer_id, const void* data, size_t len);
352typedef void (*rats_path_cb)(void* user, const char* peer_id, const rats_path_stats_t* stats);
353
354/* Each registration adds a handler; several may coexist. */
365RATS_API rats_error_t rats_on_peer_path(rats_t node, rats_path_cb cb, void* user);
367RATS_API rats_error_t rats_on(rats_t node, const char* channel, rats_message_cb cb, void* user);
368
369/* ─── Discovery (enable before start) ─────────────────────────────────────── */
370
374RATS_API rats_error_t rats_enable_dht(rats_t node, uint16_t dht_port, const char* discovery_key);
375
378
382
383/* ─── NAT traversal (enable before start) ─────────────────────────────────── */
384
387RATS_API rats_error_t rats_enable_port_mapping(rats_t node, int upnp, int natpmp);
388
394RATS_API rats_error_t rats_enable_hole_punch(rats_t node, int serve_rendezvous);
395
399RATS_API rats_error_t rats_punch_peer(rats_t node, const char* peer_id);
400
408RATS_API rats_error_t rats_enable_relay(rats_t node, int serve);
409
413RATS_API rats_error_t rats_connect_via_relay(rats_t node, const char* peer_id);
414
415/* ─── Pub/sub (GossipSub) ─────────────────────────────────────────────────── */
416
417typedef void (*rats_topic_cb)(void* user, const char* peer_id, const char* topic,
418 const void* data, size_t len);
419
423RATS_API rats_error_t rats_subscribe(rats_t node, const char* topic, rats_topic_cb cb, void* user);
424RATS_API rats_error_t rats_unsubscribe(rats_t node, const char* topic);
426RATS_API rats_error_t rats_publish(rats_t node, const char* topic, const void* data, size_t len);
427
428/* ─── JSON messaging (typed messages carrying a JSON document) ───────────── */
429
430/* `json` is compact JSON text. The sender id is the one authenticated by the
431 * handshake. */
432typedef void (*rats_json_cb)(void* user, const char* peer_id, const char* json);
433
437RATS_API rats_error_t rats_on_json(rats_t node, const char* type, rats_json_cb cb, void* user);
439RATS_API rats_error_t rats_once_json(rats_t node, const char* type, rats_json_cb cb, void* user);
441RATS_API rats_error_t rats_off_json(rats_t node, const char* type);
443RATS_API rats_error_t rats_send_json(rats_t node, const char* peer_id, const char* type,
444 const char* json);
445RATS_API rats_error_t rats_broadcast_json(rats_t node, const char* type, const char* json);
446
447/* ─── File transfer (push: the sender offers, the receiver accepts) ──────── */
448
457
458typedef struct {
459 uint64_t transfer_id;
460 const char* peer_id;
461 rats_direction_t direction; /* OUTBOUND = we send, INBOUND = we receive */
464 uint64_t total_bytes;
466 uint32_t total_files;
467 double rate_bytes_per_sec; /* recent, smoothed */
469
470typedef void (*rats_file_offer_cb)(void* user, const char* peer_id, uint64_t transfer_id,
471 const char* name, uint64_t size, int is_directory);
472typedef void (*rats_file_progress_cb)(void* user, const rats_file_progress_t* progress);
473typedef void (*rats_file_complete_cb)(void* user, const char* peer_id, uint64_t transfer_id,
474 int success, const char* path);
475
477RATS_API rats_error_t rats_enable_file_transfer(rats_t node, const char* temp_dir);
478
479/* One handler each, set before start; a second call replaces the first. */
483
487RATS_API rats_error_t rats_send_file(rats_t node, const char* peer_id, const char* path,
488 uint64_t* transfer_id);
489RATS_API rats_error_t rats_send_directory(rats_t node, const char* peer_id, const char* path,
490 uint64_t* transfer_id);
491
494RATS_API rats_error_t rats_accept_file(rats_t node, const char* peer_id, uint64_t transfer_id,
495 const char* dest_path);
496RATS_API rats_error_t rats_reject_file(rats_t node, const char* peer_id, uint64_t transfer_id);
497
500RATS_API rats_error_t rats_cancel_file(rats_t node, const char* peer_id, uint64_t transfer_id);
501RATS_API rats_error_t rats_pause_file(rats_t node, const char* peer_id, uint64_t transfer_id);
502RATS_API rats_error_t rats_resume_file(rats_t node, const char* peer_id, uint64_t transfer_id);
503
504/* ─── Ping (round-trip probing of every peer) ─────────────────────────────── */
505
510RATS_API int64_t rats_peer_rtt_ms(rats_t node, const char* peer_id);
511
512/* ─── Reconnection ────────────────────────────────────────────────────────── */
513
519RATS_API rats_error_t rats_add_reconnect(rats_t node, const char* host, uint16_t port);
521RATS_API rats_error_t rats_remove_reconnect(rats_t node, const char* host, uint16_t port);
527RATS_API rats_error_t rats_enable_reconnect_lost(rats_t node, uint32_t lost_for_ms);
528
529/* ─── BitTorrent ──────────────────────────────────────────────────────────────
530 * Available only in builds with RATS_SEARCH_FEATURES; elsewhere every call
531 * returns RATS_ERR_NOT_ENABLED. The client runs its own sockets; enable the DHT
532 * first and it shares the node's. */
533
534typedef enum RATS_ENUM_BASE {
535 RATS_BT_ENC_ENABLED = 0, /* obfuscate (MSE/PE) first, fall back to plaintext (default) */
536 RATS_BT_ENC_FORCED = 1, /* obfuscated connections only */
537 RATS_BT_ENC_DISABLED = 2 /* plaintext only */
539
540/* Zero in any field is the default named beside it. */
541typedef struct {
542 uint16_t listen_port; /* 0 → ephemeral */
543 const char* download_path; /* NULL → "." */
544 rats_bt_encryption_t outgoing_encryption; /* RATS_BT_ENC_ENABLED */
545 rats_bt_encryption_t incoming_encryption; /* RATS_BT_ENC_ENABLED */
546 int disable_outgoing_utp; /* non-zero: dial over TCP only */
547 int disable_incoming_utp; /* non-zero: never answer uTP */
549
553RATS_API rats_error_t rats_bt_add_magnet(rats_t node, const char* magnet_uri, const char* save_path);
555RATS_API rats_error_t rats_bt_add_torrent_file(rats_t node, const char* torrent_path,
556 const char* save_path);
558RATS_API rats_error_t rats_bt_remove_torrent(rats_t node, const char* info_hash_hex);
559
560/* ─── Process-wide ────────────────────────────────────────────────────────── */
561
564RATS_API void rats_set_log_file(const char* path);
565
567RATS_API const char* rats_version_string(void);
569RATS_API void rats_version(int* major, int* minor, int* patch, int* build);
571RATS_API const char* rats_git_describe(void);
573RATS_API uint32_t rats_abi(void);
574
576RATS_API void rats_strings_free(char** strings, size_t count);
577
578#undef RATS_ENUM_BASE
579
580#ifdef __cplusplus
581}
582#endif
rats_error_t rats_enable_port_mapping(rats_t node, int upnp, int natpmp)
Ask the router to forward the listen port, over UPnP IGD and/or NAT-PMP (non-zero enables each; both ...
void(* rats_peer_cb)(void *user, const char *peer_id)
Definition rats.h:348
rats_error_t rats_subscribe(rats_t node, const char *topic, rats_topic_cb cb, void *user)
Subscribe to a topic; one handler per topic, a second subscribe replaces it.
rats_nat_mapping_t rats_nat_mapping(rats_t node)
What the mesh has shown about this node's NAT, from where UDP peers report seeing its socket.
const char * rats_protocol(rats_t node)
The protocol bound into the handshake.
rats_error_t rats_start(rats_t node)
Open the sockets, start the I/O threads and every enabled subsystem.
rats_congestion_t
Definition rats.h:101
@ RATS_CONGESTION_RENO
Definition rats.h:104
@ RATS_CONGESTION_BBR
Definition rats.h:102
rats_file_status_t
Definition rats.h:449
@ RATS_FILE_ACTIVE
Definition rats.h:451
@ RATS_FILE_CANCELLED
Definition rats.h:455
@ RATS_FILE_PAUSED
Definition rats.h:452
@ RATS_FILE_COMPLETED
Definition rats.h:453
@ RATS_FILE_FAILED
Definition rats.h:454
@ RATS_FILE_PENDING
Definition rats.h:450
rats_direction_t
Definition rats.h:107
@ RATS_DIRECTION_OUTBOUND
Definition rats.h:108
@ RATS_DIRECTION_INBOUND
Definition rats.h:109
rats_security_t
Definition rats.h:79
@ RATS_SECURITY_NOISE
Definition rats.h:80
@ RATS_SECURITY_PLAINTEXT
Definition rats.h:81
char ** rats_observed_addresses(rats_t node, size_t *count)
Our own addresses as peers report seeing us ("ip:port", our listen port).
rats_error_t rats_off_json(rats_t node, const char *type)
Remove every handler of type.
rats_error_t rats_send_directory(rats_t node, const char *peer_id, const char *path, uint64_t *transfer_id)
rats_error_t rats_on_peer_writable(rats_t node, rats_peer_cb cb, void *user)
A peer whose queue had filled has drained back under its mark.
rats_error_t rats_on_peer_disconnected(rats_t node, rats_disconnect_cb cb, void *user)
void rats_version(int *major, int *minor, int *patch, int *build)
Version components; any pointer may be NULL.
void rats_set_log_file(const char *path)
Also write logs to path; NULL or "" stops file logging.
const char * rats_close_reason_str(rats_close_reason_t reason)
Static name of a close reason, e.g.
rats_error_t rats_broadcast_json(rats_t node, const char *type, const char *json)
void(* rats_path_cb)(void *user, const char *peer_id, const rats_path_stats_t *stats)
Definition rats.h:352
rats_error_t rats_enable_json(rats_t node)
Enable before start.
rats_error_t rats_enable_reconnect_lost(rats_t node, uint32_t lost_for_ms)
rats_enable_reconnect(), and a peer lost — its connection closed by neither side: gone quiet past the...
rats_error_t rats_on_dial_failed(rats_t node, rats_dial_failed_cb cb, void *user)
A dial to a numeric address failed over every transport tried — one made by rats_connect() or by a di...
void(* rats_message_cb)(void *user, const char *peer_id, const void *data, size_t len)
Definition rats.h:351
struct rats_node * rats_t
Definition rats.h:55
rats_error_t rats_unsubscribe(rats_t node, const char *topic)
rats_nat_mapping_t
Definition rats.h:136
@ RATS_NAT_ENDPOINT_INDEPENDENT
Definition rats.h:139
@ RATS_NAT_OPEN
Definition rats.h:138
@ RATS_NAT_ENDPOINT_DEPENDENT
Definition rats.h:140
@ RATS_NAT_UNKNOWN
Definition rats.h:137
uint16_t rats_listen_port(rats_t node)
The bound listen port (the real one when 0 was asked for); 0 before start.
size_t rats_max_peers(rats_t node)
rats_log_level_t
Definition rats.h:143
@ RATS_LOG_ERROR
Definition rats.h:147
@ RATS_LOG_INFO
Definition rats.h:145
@ RATS_LOG_DEBUG
Definition rats.h:144
@ RATS_LOG_WARN
Definition rats.h:146
rats_error_t rats_enable_hole_punch(rats_t node, int serve_rendezvous)
UDP hole punching: reach a peer behind a NAT by having a peer both sides share arrange for the two to...
rats_error_t rats_resume_file(rats_t node, const char *peer_id, uint64_t transfer_id)
rats_close_reason_t
Definition rats.h:113
@ RATS_CLOSE_LOCAL
Definition rats.h:114
@ RATS_CLOSE_PEER_LIMIT
Definition rats.h:127
@ RATS_CLOSE_DUPLICATE
Definition rats.h:126
@ RATS_CLOSE_CONNECT_FAILED
Definition rats.h:117
@ RATS_CLOSE_SLOW_CONSUMER
Definition rats.h:124
@ RATS_CLOSE_PEER_CLOSED
Definition rats.h:115
@ RATS_CLOSE_HANDSHAKE_FAILED
Definition rats.h:118
@ RATS_CLOSE_DIAL_SUPERSEDED
Definition rats.h:129
@ RATS_CLOSE_PROTOCOL_ERROR
Definition rats.h:119
@ RATS_CLOSE_SHUTDOWN
Definition rats.h:125
@ RATS_CLOSE_IDLE_TIMEOUT
Definition rats.h:128
@ RATS_CLOSE_PEER_RESET
Definition rats.h:116
rats_error_t rats_on_file_progress(rats_t node, rats_file_progress_cb cb, void *user)
rats_error_t rats_remove_reconnect(rats_t node, const char *host, uint16_t port)
Stop keeping an address connected and forget it.
rats_error_t rats_on_peer_path(rats_t node, rats_path_cb cb, void *user)
A path sample of every connected peer, every path_sample_ms (100 ms if 0).
void(* rats_topic_cb)(void *user, const char *peer_id, const char *topic, const void *data, size_t len)
Definition rats.h:417
rats_error_t rats_on_json(rats_t node, const char *type, rats_json_cb cb, void *user)
Add a handler for messages of type.
rats_bt_encryption_t
Definition rats.h:534
@ RATS_BT_ENC_FORCED
Definition rats.h:536
@ RATS_BT_ENC_ENABLED
Definition rats.h:535
@ RATS_BT_ENC_DISABLED
Definition rats.h:537
void(* rats_file_complete_cb)(void *user, const char *peer_id, uint64_t transfer_id, int success, const char *path)
Definition rats.h:473
rats_error_t rats_on_peer_connected(rats_t node, rats_peer_cb cb, void *user)
rats_error_t rats_send_file(rats_t node, const char *peer_id, const char *path, uint64_t *transfer_id)
Offer a file or a directory tree to a connected peer; the transfer id is written to *transfer_id (whi...
rats_error_t rats_add_reconnect(rats_t node, const char *host, uint16_t port)
Keep an address connected.
rats_error_t rats_send(rats_t node, const char *peer_id, const char *channel, const void *data, size_t len, uint32_t lifetime_ms)
Queue bytes for one peer on a named channel.
rats_error_t rats_enable_dht(rats_t node, uint16_t dht_port, const char *discovery_key)
Mainline DHT: announce and search a key, dialing the peers found.
int64_t rats_peer_rtt_ms(rats_t node, const char *peer_id)
Last measured round trip to a peer in ms; -1 if unknown (ping not enabled, no answer yet).
rats_error_t rats_on(rats_t node, const char *channel, rats_message_cb cb, void *user)
Messages arriving on a named channel.
rats_error_t rats_path_stats(rats_t node, const char *peer_id, rats_path_stats_t *out)
The latest path sample for a peer.
char ** rats_peer_ids(rats_t node, size_t *count)
Ids of the connected peers: an array of *count strings, NULL when there are none.
rats_error_t rats_accept_file(rats_t node, const char *peer_id, uint64_t transfer_id, const char *dest_path)
Answer an offer.
rats_error_t rats_once_json(rats_t node, const char *type, rats_json_cb cb, void *user)
Like rats_on_json(); the handler is removed after its first message.
void(* rats_file_offer_cb)(void *user, const char *peer_id, uint64_t transfer_id, const char *name, uint64_t size, int is_directory)
Definition rats.h:470
rats_error_t rats_peer_info(rats_t node, const char *peer_id, rats_peer_info_t *out)
Fill out for a connected peer.
void(* rats_disconnect_cb)(void *user, const char *peer_id, rats_close_reason_t reason)
Definition rats.h:349
rats_error_t
Definition rats.h:59
@ RATS_ERR_DECLINED
Definition rats.h:67
@ RATS_ERR_ALREADY_STARTED
Definition rats.h:63
@ RATS_ERR_NO_SUCH_PEER
Definition rats.h:65
@ RATS_OK
Definition rats.h:60
@ RATS_ERR_BIND
Definition rats.h:70
@ RATS_ERR_NOT_STARTED
Definition rats.h:62
@ RATS_ERR_NOT_ENABLED
Definition rats.h:64
@ RATS_ERR_INVALID_ARG
Definition rats.h:61
@ RATS_ERR_INTERNAL
Definition rats.h:71
@ RATS_ERR_NOT_FOUND
Definition rats.h:66
rats_error_t rats_publish(rats_t node, const char *topic, const void *data, size_t len)
Publish to every subscriber of the topic.
rats_t rats_create(const rats_config_t *config)
Create a node.
rats_error_t rats_enable_peer_exchange(rats_t node)
Peer exchange: connected peers trade the addresses of peers they hold, so a node needs one contact to...
void(* rats_file_progress_cb)(void *user, const rats_file_progress_t *progress)
Definition rats.h:472
rats_error_t rats_disconnect(rats_t node, const char *peer_id)
Close the connection to a peer.
rats_error_t rats_on_file_offer(rats_t node, rats_file_offer_cb cb, void *user)
rats_error_t rats_bt_add_magnet(rats_t node, const char *magnet_uri, const char *save_path)
Start a download from a magnet link (metadata is fetched from peers).
const char * rats_version_string(void)
Library version with the build number, e.g.
rats_error_t rats_enable_ping(rats_t node)
Enable before start.
rats_error_t rats_punch_peer(rats_t node, const char *peer_id)
Start punching to a peer.
rats_error_t rats_enable_bittorrent(rats_t node, const rats_bt_config_t *config)
Enable before start.
#define RATS_ENUM_BASE
Definition rats.h:52
rats_error_t rats_enable_relay(rats_t node, int serve)
Relaying: reach a peer nothing else can, through a node both ends are connected to.
rats_error_t rats_enable_mdns(rats_t node)
mDNS discovery on the local network.
uint32_t rats_abi(void)
(major << 16) | (minor << 8) | patch.
rats_transport_t
Definition rats.h:86
@ RATS_TRANSPORT_TCP
Definition rats.h:88
@ RATS_TRANSPORT_UDP
Definition rats.h:87
@ RATS_TRANSPORT_RELAY
Definition rats.h:91
rats_error_t rats_enable_pubsub(rats_t node)
Enable before start.
rats_error_t rats_on_file_complete(rats_t node, rats_file_complete_cb cb, void *user)
int rats_peer_writable(rats_t node, const char *peer_id)
1 while the peer's send queue has room, 0 when it does not or the peer is not connected.
const char * rats_error_str(rats_error_t err)
Static name of an error code, e.g.
const char * rats_local_id(rats_t node)
Our peer id as hex.
void rats_strings_free(char **strings, size_t count)
Free an array from rats_peer_ids() / rats_observed_addresses().
rats_error_t rats_enable_file_transfer(rats_t node, const char *temp_dir)
Enable before start.
rats_error_t rats_pause_file(rats_t node, const char *peer_id, uint64_t transfer_id)
uint32_t rats_transports(rats_t node)
Transports actually running, as RATS_TRANSPORT_MASK_* bits.
void rats_stop(rats_t node)
Close every connection and stop.
void(* rats_json_cb)(void *user, const char *peer_id, const char *json)
Definition rats.h:432
rats_error_t rats_connect_via_relay(rats_t node, const char *peer_id)
Start a relayed connection to a peer.
void rats_set_log_level(rats_log_level_t level)
rats_error_t rats_bt_add_torrent_file(rats_t node, const char *torrent_path, const char *save_path)
Start a download from a .torrent file.
rats_error_t rats_send_json(rats_t node, const char *peer_id, const char *type, const char *json)
json must be valid JSON text (else RATS_ERR_INVALID_ARG).
rats_error_t rats_bt_remove_torrent(rats_t node, const char *info_hash_hex)
Remove a torrent by its 40-char hex info-hash.
rats_error_t rats_connect(rats_t node, const char *host, uint16_t port)
Dial host:port (a literal or a hostname).
size_t rats_peer_count(rats_t node)
rats_error_t rats_broadcast(rats_t node, const char *channel, const void *data, size_t len, uint32_t lifetime_ms)
rats_send() to every connected peer.
void(* rats_dial_failed_cb)(void *user, const char *address)
Definition rats.h:350
rats_error_t rats_cancel_file(rats_t node, const char *peer_id, uint64_t transfer_id)
Control a running transfer from either side.
rats_error_t rats_enable_reconnect(rats_t node)
Re-dial dropped peers with exponential backoff.
const char * rats_git_describe(void)
git describe of the build, e.g.
rats_error_t rats_reject_file(rats_t node, const char *peer_id, uint64_t transfer_id)
void rats_set_max_peers(rats_t node, size_t max_peers)
Cap on established peers (0 = unlimited).
void rats_destroy(rats_t node)
Stop the node if running and free it.
int disable_outgoing_utp
Definition rats.h:546
rats_bt_encryption_t outgoing_encryption
Definition rats.h:544
uint16_t listen_port
Definition rats.h:542
int disable_incoming_utp
Definition rats.h:547
rats_bt_encryption_t incoming_encryption
Definition rats.h:545
const char * download_path
Definition rats.h:543
rats_transport_t preferred_transport
Definition rats.h:175
int disable_udp_offload
Definition rats.h:196
size_t max_peers
Definition rats.h:170
size_t reactor_threads
Definition rats.h:205
uint32_t udp_idle_timeout_ms
Definition rats.h:199
const char * protocol
Definition rats.h:159
uint16_t listen_port
Definition rats.h:167
size_t udp_receive_window
Definition rats.h:191
int disable_network_monitor
Definition rats.h:206
rats_congestion_t congestion_control
Definition rats.h:190
rats_security_t security
Definition rats.h:164
size_t send_queue_limit
Definition rats.h:181
size_t udp_receive_budget
Definition rats.h:194
uint32_t path_sample_ms
Definition rats.h:185
const char * bind_address
Definition rats.h:168
uint32_t transports
Definition rats.h:174
uint32_t transport_fallback_ms
Definition rats.h:176
int dial_only
Definition rats.h:169
const char * data_dir
Definition rats.h:162
const char * peer_id
Definition rats.h:460
uint32_t total_files
Definition rats.h:466
uint64_t transfer_id
Definition rats.h:459
uint64_t bytes_transferred
Definition rats.h:463
rats_direction_t direction
Definition rats.h:461
uint64_t total_bytes
Definition rats.h:464
double rate_bytes_per_sec
Definition rats.h:467
rats_file_status_t status
Definition rats.h:462
uint32_t files_completed
Definition rats.h:465
double loss
Definition rats.h:332
uint64_t live_repair_bytes
Definition rats.h:337
uint64_t pacing_bps
Definition rats.h:327
uint32_t srtt_us
Definition rats.h:328
uint64_t bytes_delivered
Definition rats.h:333
uint64_t live_expired
Definition rats.h:336
uint64_t bytes_in_flight
Definition rats.h:331
uint64_t queued_bytes
Definition rats.h:330
uint64_t bytes_lost
Definition rats.h:334
uint64_t live_sent
Definition rats.h:335
uint64_t bandwidth_bps
Definition rats.h:324
uint32_t min_rtt_us
Definition rats.h:329
uint32_t transports
Definition rats.h:269
char address[64]
Definition rats.h:272
rats_transport_t transport
Definition rats.h:268
rats_direction_t direction
Definition rats.h:271