Back to Site
Loading...
Searching...
No Matches
rats.h File Reference

The C ABI over Node — what every language binding is built on. More...

#include "librats/util/rats_export.h"
#include <stddef.h>
#include <stdint.h>
Include dependency graph for rats.h:

Go to the source code of this file.

Classes

struct  rats_config_t
struct  rats_peer_info_t
struct  rats_path_stats_t
struct  rats_file_progress_t
struct  rats_bt_config_t

Macros

#define RATS_ENUM_BASE
#define RATS_TRANSPORT_MASK_TCP   0x1u
#define RATS_TRANSPORT_MASK_UDP   0x2u
#define RATS_FALLBACK_NEVER   0xFFFFFFFFu
#define RATS_LIVE_MAX_LIFETIME_MS   30000u
#define RATS_LIVE_MAX_PAYLOAD   (4u * 1024u * 1024u)

Typedefs

typedef struct rats_node * rats_t
typedef void(* rats_peer_cb) (void *user, const char *peer_id)
typedef void(* rats_disconnect_cb) (void *user, const char *peer_id, rats_close_reason_t reason)
typedef void(* rats_dial_failed_cb) (void *user, const char *address)
typedef void(* rats_message_cb) (void *user, const char *peer_id, const void *data, size_t len)
typedef void(* rats_path_cb) (void *user, const char *peer_id, const rats_path_stats_t *stats)
typedef void(* rats_topic_cb) (void *user, const char *peer_id, const char *topic, const void *data, size_t len)
typedef void(* rats_json_cb) (void *user, const char *peer_id, const char *json)
typedef void(* rats_file_offer_cb) (void *user, const char *peer_id, uint64_t transfer_id, const char *name, uint64_t size, int is_directory)
typedef void(* rats_file_progress_cb) (void *user, const rats_file_progress_t *progress)
typedef void(* rats_file_complete_cb) (void *user, const char *peer_id, uint64_t transfer_id, int success, const char *path)

Enumerations

enum  rats_error_t {
  RATS_OK = 0 , RATS_ERR_INVALID_ARG = 1 , RATS_ERR_NOT_STARTED = 2 , RATS_ERR_ALREADY_STARTED = 3 ,
  RATS_ERR_NOT_ENABLED = 4 , RATS_ERR_NO_SUCH_PEER = 5 , RATS_ERR_NOT_FOUND = 6 , RATS_ERR_DECLINED = 7 ,
  RATS_ERR_BIND = 8 , RATS_ERR_INTERNAL = 9
}
enum  rats_security_t { RATS_SECURITY_NOISE = 0 , RATS_SECURITY_PLAINTEXT = 1 }
enum  rats_transport_t { RATS_TRANSPORT_UDP = 0 , RATS_TRANSPORT_TCP = 1 , RATS_TRANSPORT_RELAY = 2 }
enum  rats_congestion_t { RATS_CONGESTION_BBR = 0 , RATS_CONGESTION_RENO = 1 }
enum  rats_direction_t { RATS_DIRECTION_OUTBOUND = 0 , RATS_DIRECTION_INBOUND = 1 }
enum  rats_close_reason_t {
  RATS_CLOSE_LOCAL = 0 , RATS_CLOSE_PEER_CLOSED = 1 , RATS_CLOSE_PEER_RESET = 2 , RATS_CLOSE_CONNECT_FAILED = 3 ,
  RATS_CLOSE_HANDSHAKE_FAILED = 4 , RATS_CLOSE_PROTOCOL_ERROR = 5 , RATS_CLOSE_SLOW_CONSUMER = 6 , RATS_CLOSE_SHUTDOWN = 7 ,
  RATS_CLOSE_DUPLICATE = 8 , RATS_CLOSE_PEER_LIMIT = 9 , RATS_CLOSE_IDLE_TIMEOUT = 10 , RATS_CLOSE_DIAL_SUPERSEDED = 11
}
enum  rats_nat_mapping_t { RATS_NAT_UNKNOWN = 0 , RATS_NAT_OPEN = 1 , RATS_NAT_ENDPOINT_INDEPENDENT = 2 , RATS_NAT_ENDPOINT_DEPENDENT = 3 }
enum  rats_log_level_t { RATS_LOG_DEBUG = 0 , RATS_LOG_INFO = 1 , RATS_LOG_WARN = 2 , RATS_LOG_ERROR = 3 }
enum  rats_file_status_t {
  RATS_FILE_PENDING = 0 , RATS_FILE_ACTIVE = 1 , RATS_FILE_PAUSED = 2 , RATS_FILE_COMPLETED = 3 ,
  RATS_FILE_FAILED = 4 , RATS_FILE_CANCELLED = 5
}
enum  rats_bt_encryption_t { RATS_BT_ENC_ENABLED = 0 , RATS_BT_ENC_FORCED = 1 , RATS_BT_ENC_DISABLED = 2 }

Functions

const char * rats_error_str (rats_error_t err)
 Static name of an error code, e.g.
const char * rats_close_reason_str (rats_close_reason_t reason)
 Static name of a close reason, e.g.
rats_t rats_create (const rats_config_t *config)
 Create a node.
void rats_destroy (rats_t node)
 Stop the node if running and free it.
rats_error_t rats_start (rats_t node)
 Open the sockets, start the I/O threads and every enabled subsystem.
void rats_stop (rats_t node)
 Close every connection and stop.
const char * rats_local_id (rats_t node)
 Our peer id as hex.
const char * rats_protocol (rats_t node)
 The protocol bound into the handshake.
uint16_t rats_listen_port (rats_t node)
 The bound listen port (the real one when 0 was asked for); 0 before start.
uint32_t rats_transports (rats_t node)
 Transports actually running, as RATS_TRANSPORT_MASK_* bits.
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.
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_connect (rats_t node, const char *host, uint16_t port)
 Dial host:port (a literal or a hostname).
rats_error_t rats_disconnect (rats_t node, const char *peer_id)
 Close the connection to a peer.
size_t rats_peer_count (rats_t node)
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_peer_info (rats_t node, const char *peer_id, rats_peer_info_t *out)
 Fill out for a connected peer.
void rats_set_max_peers (rats_t node, size_t max_peers)
 Cap on established peers (0 = unlimited).
size_t rats_max_peers (rats_t node)
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_broadcast (rats_t node, const char *channel, const void *data, size_t len, uint32_t lifetime_ms)
 rats_send() to every connected peer.
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.
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.
rats_error_t rats_on_peer_connected (rats_t node, rats_peer_cb cb, void *user)
rats_error_t rats_on_peer_disconnected (rats_t node, rats_disconnect_cb cb, void *user)
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 discovery subsystem.
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_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).
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_enable_dht (rats_t node, uint16_t dht_port, const char *discovery_key)
 Mainline DHT: announce and search a key, dialing the peers found.
rats_error_t rats_enable_mdns (rats_t node)
 mDNS discovery on the local network.
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 find the rest of the mesh.
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 run in parallel).
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 dial each other at the same moment.
rats_error_t rats_punch_peer (rats_t node, const char *peer_id)
 Start punching to a peer.
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_connect_via_relay (rats_t node, const char *peer_id)
 Start a relayed connection to a peer.
rats_error_t rats_enable_pubsub (rats_t node)
 Enable before start.
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_error_t rats_unsubscribe (rats_t node, const char *topic)
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_error_t rats_enable_json (rats_t node)
 Enable before start.
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_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.
rats_error_t rats_off_json (rats_t node, const char *type)
 Remove every handler of type.
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_broadcast_json (rats_t node, const char *type, const char *json)
rats_error_t rats_enable_file_transfer (rats_t node, const char *temp_dir)
 Enable before start.
rats_error_t rats_on_file_offer (rats_t node, rats_file_offer_cb cb, void *user)
rats_error_t rats_on_file_progress (rats_t node, rats_file_progress_cb cb, void *user)
rats_error_t rats_on_file_complete (rats_t node, rats_file_complete_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 (which may be NULL).
rats_error_t rats_send_directory (rats_t node, const char *peer_id, const char *path, uint64_t *transfer_id)
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_reject_file (rats_t node, const char *peer_id, uint64_t transfer_id)
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_pause_file (rats_t node, const char *peer_id, uint64_t transfer_id)
rats_error_t rats_resume_file (rats_t node, const char *peer_id, uint64_t transfer_id)
rats_error_t rats_enable_ping (rats_t node)
 Enable before start.
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_enable_reconnect (rats_t node)
 Re-dial dropped peers with exponential backoff.
rats_error_t rats_add_reconnect (rats_t node, const char *host, uint16_t port)
 Keep an address connected.
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_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 idle timeout, or stopped answering — dialed again where it was last known for lost_for_ms, until it is back over any route.
rats_error_t rats_enable_bittorrent (rats_t node, const rats_bt_config_t *config)
 Enable before start.
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).
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_bt_remove_torrent (rats_t node, const char *info_hash_hex)
 Remove a torrent by its 40-char hex info-hash.
void rats_set_log_level (rats_log_level_t level)
void rats_set_log_file (const char *path)
 Also write logs to path; NULL or "" stops file logging.
const char * rats_version_string (void)
 Library version with the build number, e.g.
void rats_version (int *major, int *minor, int *patch, int *build)
 Version components; any pointer may be NULL.
const char * rats_git_describe (void)
 git describe of the build, e.g.
uint32_t rats_abi (void)
 (major << 16) | (minor << 8) | patch.
void rats_strings_free (char **strings, size_t count)
 Free an array from rats_peer_ids() / rats_observed_addresses().

Detailed Description

The C ABI over Node — what every language binding is built on.

A rats_t is an opaque handle to one node. Peer ids cross the ABI as 64-char lowercase hex of the peer's self-certifying PeerId.

Configuration: rats_config_t is zero-initialisable — every field's zero is the library default — so rats_config_t cfg = {0}; (or a designated initialiser naming only what you change) is a complete config, and rats_create(NULL) is a node with all defaults.

Errors: a fallible call returns rats_error_t, RATS_OK (0) on success; rats_error_str() names a code. Sending is asynchronous: RATS_OK from rats_send()/rats_publish() means the message was queued, not that it arrived.

Subsystems are opt-in. Discovery, NAT traversal, pub/sub, JSON messaging, file transfer, ping and reconnection each need their rats_enable_*() BEFORE rats_start(). Using one that was not enabled returns RATS_ERR_NOT_ENABLED; enabling after start returns RATS_ERR_ALREADY_STARTED.

Threading: every call may be made from any thread. Callbacks run on an internal reactor thread — keep them short and never block in them. Register them before rats_start(); the node-level ones refuse afterwards (RATS_ERR_ALREADY_STARTED). Strings and structs handed to a callback are valid only for the duration of the call.

Memory: the string arrays rats_peer_ids() and rats_observed_addresses() return are released with rats_strings_free(). Every const char* the library returns is its own and must not be freed.

Definition in file rats.h.

Macro Definition Documentation

◆ RATS_ENUM_BASE

#define RATS_ENUM_BASE

Definition at line 52 of file rats.h.

◆ RATS_FALLBACK_NEVER

#define RATS_FALLBACK_NEVER   0xFFFFFFFFu

Definition at line 153 of file rats.h.

◆ RATS_LIVE_MAX_LIFETIME_MS

#define RATS_LIVE_MAX_LIFETIME_MS   30000u

Definition at line 287 of file rats.h.

◆ RATS_LIVE_MAX_PAYLOAD

#define RATS_LIVE_MAX_PAYLOAD   (4u * 1024u * 1024u)

Definition at line 288 of file rats.h.

◆ RATS_TRANSPORT_MASK_TCP

#define RATS_TRANSPORT_MASK_TCP   0x1u

Definition at line 96 of file rats.h.

◆ RATS_TRANSPORT_MASK_UDP

#define RATS_TRANSPORT_MASK_UDP   0x2u

Definition at line 97 of file rats.h.

Typedef Documentation

◆ rats_dial_failed_cb

typedef void(* rats_dial_failed_cb) (void *user, const char *address)

Definition at line 350 of file rats.h.

◆ rats_disconnect_cb

typedef void(* rats_disconnect_cb) (void *user, const char *peer_id, rats_close_reason_t reason)

Definition at line 349 of file rats.h.

◆ rats_file_complete_cb

typedef void(* rats_file_complete_cb) (void *user, const char *peer_id, uint64_t transfer_id, int success, const char *path)

Definition at line 473 of file rats.h.

◆ rats_file_offer_cb

typedef 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 at line 470 of file rats.h.

◆ rats_file_progress_cb

typedef void(* rats_file_progress_cb) (void *user, const rats_file_progress_t *progress)

Definition at line 472 of file rats.h.

◆ rats_json_cb

typedef void(* rats_json_cb) (void *user, const char *peer_id, const char *json)

Definition at line 432 of file rats.h.

◆ rats_message_cb

typedef void(* rats_message_cb) (void *user, const char *peer_id, const void *data, size_t len)

Definition at line 351 of file rats.h.

◆ rats_path_cb

typedef void(* rats_path_cb) (void *user, const char *peer_id, const rats_path_stats_t *stats)

Definition at line 352 of file rats.h.

◆ rats_peer_cb

typedef void(* rats_peer_cb) (void *user, const char *peer_id)

Definition at line 348 of file rats.h.

◆ rats_t

typedef struct rats_node* rats_t

Definition at line 55 of file rats.h.

◆ rats_topic_cb

typedef void(* rats_topic_cb) (void *user, const char *peer_id, const char *topic, const void *data, size_t len)

Definition at line 417 of file rats.h.

Enumeration Type Documentation

◆ rats_bt_encryption_t

Enumerator
RATS_BT_ENC_ENABLED 
RATS_BT_ENC_FORCED 
RATS_BT_ENC_DISABLED 

Definition at line 534 of file rats.h.

◆ rats_close_reason_t

Enumerator
RATS_CLOSE_LOCAL 
RATS_CLOSE_PEER_CLOSED 
RATS_CLOSE_PEER_RESET 
RATS_CLOSE_CONNECT_FAILED 
RATS_CLOSE_HANDSHAKE_FAILED 
RATS_CLOSE_PROTOCOL_ERROR 
RATS_CLOSE_SLOW_CONSUMER 
RATS_CLOSE_SHUTDOWN 
RATS_CLOSE_DUPLICATE 
RATS_CLOSE_PEER_LIMIT 
RATS_CLOSE_IDLE_TIMEOUT 
RATS_CLOSE_DIAL_SUPERSEDED 

Definition at line 113 of file rats.h.

◆ rats_congestion_t

Enumerator
RATS_CONGESTION_BBR 
RATS_CONGESTION_RENO 

Definition at line 101 of file rats.h.

◆ rats_direction_t

Enumerator
RATS_DIRECTION_OUTBOUND 
RATS_DIRECTION_INBOUND 

Definition at line 107 of file rats.h.

◆ rats_error_t

Enumerator
RATS_OK 
RATS_ERR_INVALID_ARG 
RATS_ERR_NOT_STARTED 
RATS_ERR_ALREADY_STARTED 
RATS_ERR_NOT_ENABLED 
RATS_ERR_NO_SUCH_PEER 
RATS_ERR_NOT_FOUND 
RATS_ERR_DECLINED 
RATS_ERR_BIND 
RATS_ERR_INTERNAL 

Definition at line 59 of file rats.h.

◆ rats_file_status_t

Enumerator
RATS_FILE_PENDING 
RATS_FILE_ACTIVE 
RATS_FILE_PAUSED 
RATS_FILE_COMPLETED 
RATS_FILE_FAILED 
RATS_FILE_CANCELLED 

Definition at line 449 of file rats.h.

◆ rats_log_level_t

Enumerator
RATS_LOG_DEBUG 
RATS_LOG_INFO 
RATS_LOG_WARN 
RATS_LOG_ERROR 

Definition at line 143 of file rats.h.

◆ rats_nat_mapping_t

Enumerator
RATS_NAT_UNKNOWN 
RATS_NAT_OPEN 
RATS_NAT_ENDPOINT_INDEPENDENT 
RATS_NAT_ENDPOINT_DEPENDENT 

Definition at line 136 of file rats.h.

◆ rats_security_t

Enumerator
RATS_SECURITY_NOISE 
RATS_SECURITY_PLAINTEXT 

Definition at line 79 of file rats.h.

◆ rats_transport_t

Enumerator
RATS_TRANSPORT_UDP 
RATS_TRANSPORT_TCP 
RATS_TRANSPORT_RELAY 

Definition at line 86 of file rats.h.

Function Documentation

◆ rats_abi()

uint32_t rats_abi ( void )

(major << 16) | (minor << 8) | patch.

◆ rats_accept_file()

rats_error_t rats_accept_file ( rats_t node,
const char * peer_id,
uint64_t transfer_id,
const char * dest_path )

Answer an offer.

dest_path is the file to write for a file, the directory to fill for a directory.

◆ rats_add_reconnect()

rats_error_t rats_add_reconnect ( rats_t node,
const char * host,
uint16_t port )

Keep an address connected.

May be called while running.

◆ rats_broadcast()

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.

◆ rats_broadcast_json()

rats_error_t rats_broadcast_json ( rats_t node,
const char * type,
const char * json )

◆ rats_bt_add_magnet()

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).

◆ rats_bt_add_torrent_file()

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_bt_remove_torrent()

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.

Downloaded files are kept.

◆ rats_cancel_file()

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_ERR_NOT_FOUND if there is no such transfer.

◆ rats_close_reason_str()

const char * rats_close_reason_str ( rats_close_reason_t reason)

Static name of a close reason, e.g.

"RATS_CLOSE_SLOW_CONSUMER" (never NULL).

◆ rats_connect()

rats_error_t rats_connect ( rats_t node,
const char * host,
uint16_t port )

Dial host:port (a literal or a hostname).

Non-blocking: success arrives as rats_on_peer_connected, failure as rats_on_dial_failed.

◆ rats_connect_via_relay()

rats_error_t rats_connect_via_relay ( rats_t node,
const char * peer_id )

Start a relayed connection to a peer.

Success arrives as rats_on_peer_connected. RATS_ERR_DECLINED: already connected, already trying or cooling down, or no connected peer could carry it.

◆ rats_create()

rats_t rats_create ( const rats_config_t * config)

Create a node.

config may be NULL for all defaults. Returns NULL when the config is invalid: no transport, an out-of-range enum, or TCP preferred while turned off. With a data_dir the identity is loaded from it, or created and saved there. No socket is opened until rats_start().

◆ rats_destroy()

void rats_destroy ( rats_t node)

Stop the node if running and free it.

The handle is invalid afterwards.

◆ rats_disconnect()

rats_error_t rats_disconnect ( rats_t node,
const char * peer_id )

Close the connection to a peer.

RATS_ERR_NO_SUCH_PEER if it is not connected.

◆ rats_enable_bittorrent()

rats_error_t rats_enable_bittorrent ( rats_t node,
const rats_bt_config_t * config )

Enable before start.

config may be NULL for the defaults.

◆ rats_enable_dht()

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.

dht_port 0 is ephemeral. discovery_key NULL → the node's protocol, so only nodes that could handshake find each other.

◆ rats_enable_file_transfer()

rats_error_t rats_enable_file_transfer ( rats_t node,
const char * temp_dir )

Enable before start.

temp_dir holds partial downloads (NULL → ".").

◆ rats_enable_hole_punch()

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 dial each other at the same moment.

Every node involved needs it enabled. serve_rendezvous non-zero also passes other peers' rendezvous along — a few dozen bytes per punch; a mesh where nobody does this cannot punch.

◆ rats_enable_json()

rats_error_t rats_enable_json ( rats_t node)

Enable before start.

◆ rats_enable_mdns()

rats_error_t rats_enable_mdns ( rats_t node)

mDNS discovery on the local network.

◆ rats_enable_peer_exchange()

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 find the rest of the mesh.

◆ rats_enable_ping()

rats_error_t rats_enable_ping ( rats_t node)

Enable before start.

◆ rats_enable_port_mapping()

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 run in parallel).

◆ rats_enable_pubsub()

rats_error_t rats_enable_pubsub ( rats_t node)

Enable before start.

◆ rats_enable_reconnect()

rats_error_t rats_enable_reconnect ( rats_t node)

Re-dial dropped peers with exponential backoff.

Every address dialed is remembered; with a data_dir the list survives restarts (peers.json). Enable before start — a node without it never redials on its own.

◆ rats_enable_reconnect_lost()

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 idle timeout, or stopped answering — dialed again where it was last known for lost_for_ms, until it is back over any route.

A peer that left is not. Enable before start, instead of rats_enable_reconnect().

◆ rats_enable_relay()

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.

Encryption stays end to end; rats_peer_info() reports RATS_TRANSPORT_RELAY. serve non-zero also carries OTHER peers' connections, which costs this node their bandwidth (each circuit is capped by bytes, time and count). With hole punching enabled too, a punch that cannot work falls back to a relay by itself, and a relayed peer is upgraded to a direct connection when a punch later succeeds.

◆ rats_error_str()

const char * rats_error_str ( rats_error_t err)

Static name of an error code, e.g.

"RATS_ERR_NO_SUCH_PEER" (never NULL).

◆ rats_git_describe()

const char * rats_git_describe ( void )

git describe of the build, e.g.

"v1.2.3-4-gabcdef".

◆ rats_listen_port()

uint16_t rats_listen_port ( rats_t node)

The bound listen port (the real one when 0 was asked for); 0 before start.

◆ rats_local_id()

const char * rats_local_id ( rats_t node)

Our peer id as hex.

Owned by the node, valid until rats_destroy().

◆ rats_max_peers()

size_t rats_max_peers ( rats_t node)

◆ rats_nat_mapping()

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.

◆ rats_observed_addresses()

char ** rats_observed_addresses ( rats_t node,
size_t * count )

Our own addresses as peers report seeing us ("ip:port", our listen port).

Returns an array of *count strings, NULL when there are none; release with rats_strings_free().

◆ rats_off_json()

rats_error_t rats_off_json ( rats_t node,
const char * type )

Remove every handler of type.

◆ rats_on()

rats_error_t rats_on ( rats_t node,
const char * channel,
rats_message_cb cb,
void * user )

Messages arriving on a named channel.

◆ rats_on_dial_failed()

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 discovery subsystem.

address is "ip:port"; a dial by hostname is not reported.

◆ rats_on_file_complete()

rats_error_t rats_on_file_complete ( rats_t node,
rats_file_complete_cb cb,
void * user )

◆ rats_on_file_offer()

rats_error_t rats_on_file_offer ( rats_t node,
rats_file_offer_cb cb,
void * user )

◆ rats_on_file_progress()

rats_error_t rats_on_file_progress ( rats_t node,
rats_file_progress_cb cb,
void * user )

◆ rats_on_json()

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.

May be called while running.

◆ rats_on_peer_connected()

rats_error_t rats_on_peer_connected ( rats_t node,
rats_peer_cb cb,
void * user )

◆ rats_on_peer_disconnected()

rats_error_t rats_on_peer_disconnected ( rats_t node,
rats_disconnect_cb cb,
void * user )

◆ rats_on_peer_path()

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).

Registering one turns sampling on. Runs once per peer per interval.

◆ rats_on_peer_writable()

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_once_json()

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.

◆ rats_path_stats()

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.

RATS_ERR_NO_SUCH_PEER if the peer is not connected or nothing was sampled yet (sampling runs only with path_sample_ms set or a rats_on_peer_path() callback registered).

◆ rats_pause_file()

rats_error_t rats_pause_file ( rats_t node,
const char * peer_id,
uint64_t transfer_id )

◆ rats_peer_count()

size_t rats_peer_count ( rats_t node)

◆ rats_peer_ids()

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.

Release with rats_strings_free().

◆ rats_peer_info()

rats_error_t rats_peer_info ( rats_t node,
const char * peer_id,
rats_peer_info_t * out )

Fill out for a connected peer.

RATS_ERR_NO_SUCH_PEER if not connected.

◆ rats_peer_rtt_ms()

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_peer_writable()

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.

0 means stop: what you sent was queued, but keep adding and the peer is dropped with RATS_CLOSE_SLOW_CONSUMER. Resume on rats_on_peer_writable(), or poll this from a thread of your own. It counts bytes just handed to rats_send() too, so asking right after a send is exact.

◆ rats_protocol()

const char * rats_protocol ( rats_t node)

The protocol bound into the handshake.

Owned by the node, valid until rats_destroy().

◆ rats_publish()

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_punch_peer()

rats_error_t rats_punch_peer ( rats_t node,
const char * peer_id )

Start punching to a peer.

Success arrives as rats_on_peer_connected. RATS_ERR_DECLINED: already connected, already punching, or no external endpoint of our own known yet (that takes at least one UDP peer).

◆ rats_reject_file()

rats_error_t rats_reject_file ( rats_t node,
const char * peer_id,
uint64_t transfer_id )

◆ rats_remove_reconnect()

rats_error_t rats_remove_reconnect ( rats_t node,
const char * host,
uint16_t port )

Stop keeping an address connected and forget it.

◆ rats_resume_file()

rats_error_t rats_resume_file ( rats_t node,
const char * peer_id,
uint64_t transfer_id )

◆ rats_send()

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_ERR_NO_SUCH_PEER if the peer is not connected.

lifetime_ms 0 is reliable: the message arrives, in order with everything else this node sends on its channels. Non-zero makes it a live message, worth delivering only within that many ms of this call: repaired while it can still arrive in time, dropped whole once it cannot, never delivered partly or twice, and ordered with nothing (number them if order matters). Made for audio and video frames. The receiver needs nothing special — it arrives at rats_on() like any message.

If you send in bulk, ask rats_peer_writable() after each send: that is the only warning before the peer is dropped as a slow consumer.

◆ rats_send_directory()

rats_error_t rats_send_directory ( rats_t node,
const char * peer_id,
const char * path,
uint64_t * transfer_id )

◆ rats_send_file()

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 (which may be NULL).

RATS_ERR_NO_SUCH_PEER if the peer is not connected, RATS_ERR_NOT_FOUND if the path cannot be read.

◆ rats_send_json()

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_set_log_file()

void rats_set_log_file ( const char * path)

Also write logs to path; NULL or "" stops file logging.

◆ rats_set_log_level()

void rats_set_log_level ( rats_log_level_t level)

◆ rats_set_max_peers()

void rats_set_max_peers ( rats_t node,
size_t max_peers )

Cap on established peers (0 = unlimited).

Can be changed while running.

◆ rats_start()

rats_error_t rats_start ( rats_t node)

Open the sockets, start the I/O threads and every enabled subsystem.

RATS_ERR_ALREADY_STARTED, or RATS_ERR_BIND if the listen port is taken.

◆ rats_stop()

void rats_stop ( rats_t node)

Close every connection and stop.

Idempotent.

◆ rats_strings_free()

void rats_strings_free ( char ** strings,
size_t count )

Free an array from rats_peer_ids() / rats_observed_addresses().

NULL is a no-op.

◆ rats_subscribe()

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_transports()

uint32_t rats_transports ( rats_t node)

Transports actually running, as RATS_TRANSPORT_MASK_* bits.

Can be narrower than the config asked for: a UDP socket that cannot be bound leaves the node TCP-only rather than failing to start. 0 when not running.

◆ rats_unsubscribe()

rats_error_t rats_unsubscribe ( rats_t node,
const char * topic )

◆ rats_version()

void rats_version ( int * major,
int * minor,
int * patch,
int * build )

Version components; any pointer may be NULL.

◆ rats_version_string()

const char * rats_version_string ( void )

Library version with the build number, e.g.

"2.3.7.884".