Communication over Reticulum networks is achieved by using a simple set of classes exposed by the RNS API.
This chapter lists and explains all classes exposed by the Reticulum Network Stack API, along with their method signatures and usage. It can be used as a reference while writing applications that utilise Reticulum, or it can be read in entirity to gain an understanding of the complete functionality of RNS from a developers perspective.
Recall identity for a destination or identity hash. By default, this function
will return the identity associated with a given *destination* hash. As an
example, if you know the `lxmf.delivery` destination hash of an endpoint,
this function will return the associated underlying identity. You can also
search for an identity from a known *identity hash*, by setting the
`from_identity_hash` argument.
* **Parameters:**
* **target_hash** – Destination or identity hash as *bytes*.
* **from_identity_hash** – Whether to search based on identity hash instead of destination hash as *bool*.
* **Returns:**
An [RNS.Identity](#api-identity) instance that can be used to create an outgoing [RNS.Destination](#api-destination), or *None* if the destination is unknown.
A class used to describe endpoints in a Reticulum Network. Destination
instances are used both to create outgoing and incoming endpoints. The
destination type will decide if encryption, and what type, is used in
communication with the endpoint. A destination can also announce its
presence on the network, which will distribute necessary keys for
encrypted communication with it.
* **Parameters:**
* **identity** – An instance of [RNS.Identity](#api-identity). Can hold only public keys for an outgoing destination, or holding private keys for an ingoing.
* **direction** –`RNS.Destination.IN` or `RNS.Destination.OUT`.
* **type** –`RNS.Destination.SINGLE`, `RNS.Destination.GROUP` or `RNS.Destination.PLAIN`.
* **app_name** – A string specifying the app name.
* **\*aspects** – Any non-zero number of string arguments.
#### RATCHET_COUNT *= 512*
The default number of generated ratchet keys a destination will retain, if it has ratchets enabled.
#### RATCHET_INTERVAL *= 1800*
The minimum interval between rotating ratchet keys, in seconds.
Creates an announce packet for this destination and broadcasts it on all
relevant interfaces. Application specific data can be added to the announce.
* **Parameters:**
* **app_data** –*bytes* containing the app_data.
* **path_response** – Internal flag used by [RNS.Transport](#api-transport). Ignore.
#### accepts_links(accepts=None)
Set or query whether the destination accepts incoming link requests.
* **Parameters:**
**accepts**– If `True` or `False`, this method sets whether the destination accepts incoming link requests. If not provided or `None`, the method returns whether the destination currently accepts link requests.
* **Returns:**
`True` or `False` depending on whether the destination accepts incoming link requests, if the *accepts* parameter is not provided or `None`.
#### set_link_established_callback(callback)
Registers a function to be called when a link has been established to
this destination.
* **Parameters:**
**callback**– A function or method with the signature *callback(link)* to be called when a new link is established with this destination.
#### set_packet_callback(callback)
Registers a function to be called when a packet has been received by
this destination.
* **Parameters:**
**callback**– A function or method with the signature *callback(data, packet)* to be called when this destination receives a packet.
#### set_proof_requested_callback(callback)
Registers a function to be called when a proof has been requested for
a packet sent to this destination. Allows control over when and if
proofs should be returned for received packets.
* **Parameters:**
**callback**– A function or method to with the signature *callback(packet)* be called when a packet that requests a proof is received. The callback must return one of True or False. If the callback returns True, a proof will be sent. If it returns False, a proof will not be sent.
#### set_proof_strategy(proof_strategy)
Sets the destinations proof strategy.
* **Parameters:**
**proof_strategy**– One of `RNS.Destination.PROVE_NONE`, `RNS.Destination.PROVE_ALL` or `RNS.Destination.PROVE_APP`. If `RNS.Destination.PROVE_APP` is set, the proof_requested_callback will be called to determine whether a proof should be sent or not.
* **path** – The path for the request handler to be registered.
* **response_generator** – A function or method with the signature *response_generator(path, data, request_id, link_id, remote_identity, requested_at)* to be called. Whatever this funcion returns will be sent as a response to the requester. If the function returns `None`, no response will be sent.
* **allow** – One of `RNS.Destination.ALLOW_NONE`, `RNS.Destination.ALLOW_ALL` or `RNS.Destination.ALLOW_LIST`. If `RNS.Destination.ALLOW_LIST` is set, the request handler will only respond to requests for identified peers in the supplied list.
* **allowed_list** – A list of *bytes-like* [RNS.Identity](#api-identity) hashes.
* **auto_compress** – If `True` or `False`, determines whether automatic compression of responses should be carried out. If set to an integer value, responses will only be auto-compressed if under this size in bytes. If omitted, the default compression settings will be followed.
* **Raises:**
`ValueError` if any of the supplied arguments are invalid.
#### deregister_request_handler(path)
Deregisters a request handler.
* **Parameters:**
**path**– The path for the request handler to be deregistered.
* **Returns:**
True if the handler was deregistered, otherwise False.
#### enable_ratchets(ratchets_path)
Enables ratchets on the destination. When ratchets are enabled, Reticulum will automatically rotate
the keys used to encrypt packets to this destination, and include the latest ratchet key in announces.
Enabling ratchets on a destination will provide forward secrecy for packets sent to that destination,
even when sent outside a `Link`. The normal Reticulum `Link` establishment procedure already performs
its own ephemeral key exchange for each link establishment, which means that ratchets are not necessary
to provide forward secrecy for links.
Enabling ratchets will have a small impact on announce size, adding 32 bytes to every sent announce.
* **Parameters:**
**ratchets_path**– The path to a file to store ratchet data in.
* **Returns:**
True if the operation succeeded, otherwise False.
#### enforce_ratchets()
When ratchet enforcement is enabled, this destination will never accept packets that use its
base Identity key for encryption, but only accept packets encrypted with one of the retained
ratchet keys.
#### set_retained_ratchets(retained_ratchets)
Sets the number of previously generated ratchet keys this destination will retain,
and try to use when decrypting incoming packets. Defaults to `Destination.RATCHET_COUNT`.
* **Parameters:**
**retained_ratchets**– The number of generated ratchets to retain.
* **Returns:**
True if the operation succeeded, False if not.
#### set_ratchet_interval(interval)
Sets the minimum interval in seconds between ratchet key rotation.
Defaults to `Destination.RATCHET_INTERVAL`.
* **Parameters:**
**interval**– The minimum interval in seconds.
* **Returns:**
True if the operation succeeded, False if not.
#### create_keys()
For a `RNS.Destination.GROUP` type destination, creates a new symmetric key.
* **Raises:**
`TypeError` if called on an incompatible type of destination.
#### get_private_key()
For a `RNS.Destination.GROUP` type destination, returns the symmetric private key.
* **Raises:**
`TypeError` if called on an incompatible type of destination.
#### load_private_key(key)
For a `RNS.Destination.GROUP` type destination, loads a symmetric private key.
* **Parameters:**
**key**– A *bytes-like* containing the symmetric key.
* **Raises:**
`TypeError` if called on an incompatible type of destination.
#### encrypt(plaintext)
Encrypts information for `RNS.Destination.SINGLE` or `RNS.Destination.GROUP` type destination.
* **Parameters:**
**plaintext**– A *bytes-like* containing the plaintext to be encrypted.
* **Raises:**
`ValueError` if destination does not hold a necessary key for encryption.
#### decrypt(ciphertext)
Decrypts information for `RNS.Destination.SINGLE` or `RNS.Destination.GROUP` type destination.
* **Parameters:**
**ciphertext**–*Bytes* containing the ciphertext to be decrypted.
* **Raises:**
`ValueError` if destination does not hold a necessary key for decryption.
#### sign(message)
Signs information for `RNS.Destination.SINGLE` type destination.
* **Parameters:**
**message**–*Bytes* containing the message to be signed.
* **Returns:**
A *bytes-like* containing the message signature, or *None* if the destination could not sign the message.
#### set_default_app_data(app_data=None)
Sets the default app_data for the destination. If set, the default
app_data will be included in every announce sent by the destination,
unless other app_data is specified in the *announce* method.
* **Parameters:**
**app_data**– A *bytes-like* containing the default app_data, or a *callable* returning a *bytes-like* containing the app_data.
#### clear_default_app_data()
Clears default app_data previously set for the destination.
The Packet class is used to create packet instances that can be sent
over a Reticulum network. Packets will automatically be encrypted if
they are addressed to a `RNS.Destination.SINGLE` destination,
`RNS.Destination.GROUP` destination or a [RNS.Link](#api-link).
For `RNS.Destination.GROUP` destinations, Reticulum will use the
pre-shared key configured for the destination. All packets to group
destinations are encrypted with the same AES-256 key.
For `RNS.Destination.SINGLE` destinations, Reticulum will use a newly
derived ephemeral AES-256 key for every packet.
For [RNS.Link](#api-link) destinations, Reticulum will use per-link
ephemeral keys, and offers **Forward Secrecy**.
* **Parameters:**
* **destination** – A [RNS.Destination](#api-destination) instance to which the packet will be sent.
* **data** – The data payload to be included in the packet as *bytes*.
* **create_receipt** – Specifies whether a [RNS.PacketReceipt](#api-packetreceipt) should be created when instantiating the packet.
#### ENCRYPTED_MDU *= 383*
The maximum size of the payload data in a single encrypted packet
#### PLAIN_MDU *= 464*
The maximum size of the payload data in a single unencrypted packet
#### send()
Sends the packet.
* **Returns:**
A [RNS.PacketReceipt](#api-packetreceipt) instance if *create_receipt* was set to *True* when the packet was instantiated, if not returns *None*. If the packet could not be sent *False* is returned.
#### resend()
Re-sends the packet.
* **Returns:**
A [RNS.PacketReceipt](#api-packetreceipt) instance if *create_receipt* was set to *True* when the packet was instantiated, if not returns *None*. If the packet could not be sent *False* is returned.
#### get_rssi()
* **Returns:**
The physical layer *Received Signal Strength Indication* if available, otherwise `None`.
#### get_snr()
* **Returns:**
The physical layer *Signal-to-Noise Ratio* if available, otherwise `None`.
#### get_q()
* **Returns:**
The physical layer *Link Quality* if available, otherwise `None`.
### *class* RNS.PacketReceipt
The PacketReceipt class is used to receive notifications about
[RNS.Packet](#api-packet) instances sent over the network. Instances
of this class are never created manually, but always returned from
the *send()* method of a [RNS.Packet](#api-packet) instance.
#### get_status()
* **Returns:**
The status of the associated [RNS.Packet](#api-packet) instance. Can be one of `RNS.PacketReceipt.SENT`, `RNS.PacketReceipt.DELIVERED`, `RNS.PacketReceipt.FAILED` or `RNS.PacketReceipt.CULLED`.
#### get_rtt()
* **Returns:**
The round-trip-time in seconds
#### set_timeout(timeout)
Sets a timeout in seconds
* **Parameters:**
**timeout**– The timeout in seconds.
#### set_delivery_callback(callback)
Sets a function that gets called if a successfull delivery has been proven.
* **Parameters:**
**callback**– A *callable* with the signature *callback(packet_receipt)*
#### set_timeout_callback(callback)
Sets a function that gets called if the delivery times out.
* **Parameters:**
**callback**– A *callable* with the signature *callback(packet_receipt)*
* **response_callback** – An optional function or method with the signature *response_callback(request_receipt)* to be called when a response is received. See the [Request Example](examples.md#example-request) for more info.
* **failed_callback** – An optional function or method with the signature *failed_callback(request_receipt)* to be called when a request fails. See the [Request Example](examples.md#example-request) for more info.
* **progress_callback** – An optional function or method with the signature *progress_callback(request_receipt)* to be called when progress is made receiving the response. Progress can be accessed as a float between 0.0 and 1.0 by the *request_receipt.progress* property.
* **timeout** – An optional timeout in seconds for the request. If *None* is supplied it will be calculated based on link RTT.
* **Returns:**
A [RNS.RequestReceipt](#api-requestreceipt) instance if the request was sent, or *False* if it was not.
#### track_phy_stats(track)
You can enable physical layer statistics on a per-link basis. If this is enabled,
and the link is running over an interface that supports reporting physical layer
statistics, you will be able to retrieve stats such as *RSSI*, *SNR* and physical
*Link Quality* for the link.
* **Parameters:**
**track**– Whether or not to keep track of physical layer statistics. Value must be `True` or `False`.
#### get_rssi()
* **Returns:**
The physical layer *Received Signal Strength Indication* if available, otherwise `None`. Physical layer statistics must be enabled on the link for this method to return a value.
#### get_snr()
* **Returns:**
The physical layer *Signal-to-Noise Ratio* if available, otherwise `None`. Physical layer statistics must be enabled on the link for this method to return a value.
#### get_q()
* **Returns:**
The physical layer *Link Quality* if available, otherwise `None`. Physical layer statistics must be enabled on the link for this method to return a value.
#### get_establishment_rate()
* **Returns:**
The data transfer rate at which the link establishment procedure ocurred, in bits per second.
#### get_mtu()
* **Returns:**
The MTU of an established link.
#### get_mdu()
* **Returns:**
The packet MDU of an established link.
#### get_expected_rate()
* **Returns:**
The packet expected in-flight data rate of an established link.
#### get_mode()
* **Returns:**
The mode of an established link.
#### get_age()
* **Returns:**
The time in seconds since this link was established.
#### no_inbound_for()
* **Returns:**
The time in seconds since last inbound packet on the link. This includes keepalive packets.
#### no_outbound_for()
* **Returns:**
The time in seconds since last outbound packet on the link. This includes keepalive packets.
#### no_data_for()
* **Returns:**
The time in seconds since payload data traversed the link. This excludes keepalive packets.
#### inactive_for()
* **Returns:**
The time in seconds since activity on the link. This includes keepalive packets.
#### get_remote_identity()
* **Returns:**
The identity of the remote peer, if it is known. Calling this method will not query the remote initiator to reveal its identity. Returns `None` if the link initiator has not already independently called the `identify(identity)` method.
#### teardown()
Closes the link and purges encryption keys. New keys will
be used if a new link to the same destination is established.
#### get_channel()
Get the `Channel` for this link.
* **Returns:**
`Channel` object
#### set_link_closed_callback(callback)
Registers a function to be called when a link has been
torn down.
* **Parameters:**
**callback**– A function or method with the signature *callback(link)* to be called.
#### set_packet_callback(callback)
Registers a function to be called when a packet has been
received over this link.
* **Parameters:**
**callback**– A function or method with the signature *callback(message, packet)* to be called.
#### set_resource_callback(callback)
Registers a function to be called when a resource has been
advertised over this link. If the function returns *True*
the resource will be accepted. If it returns *False* it will
be ignored.
* **Parameters:**
**callback**– A function or method with the signature *callback(resource)* to be called. Please note that only the basic information of the resource is available at this time, such as *get_transfer_size()*, *get_data_size()*, *get_parts()* and *is_compressed()*.
#### set_resource_started_callback(callback)
Registers a function to be called when a resource has begun
transferring over this link.
* **Parameters:**
**callback**– A function or method with the signature *callback(resource)* to be called.
#### set_resource_concluded_callback(callback)
Registers a function to be called when a resource has concluded
transferring over this link.
* **Parameters:**
**callback**– A function or method with the signature *callback(resource)* to be called.
#### set_remote_identified_callback(callback)
Registers a function to be called when an initiating peer has
identified over this link.
* **Parameters:**
**callback**– A function or method with the signature *callback(link, identity)* to be called.
#### set_resource_strategy(resource_strategy)
Sets the resource strategy for the link.
* **Parameters:**
**resource_strategy**– One of `RNS.Link.ACCEPT_NONE`, `RNS.Link.ACCEPT_ALL` or `RNS.Link.ACCEPT_APP`. If `RNS.Link.ACCEPT_APP` is set, the resource_callback will be called to determine whether the resource should be accepted or not.
* **Raises:**
*TypeError* if the resource strategy is unsupported.
### *class* RNS.RequestReceipt
An instance of this class is returned by the `request` method of `RNS.Link`
instances. It should never be instantiated manually. It provides methods to
check status, response time and response data when the request concludes.
#### get_request_id()
* **Returns:**
The request ID as *bytes*.
#### get_status()
* **Returns:**
The current status of the request, one of `RNS.RequestReceipt.FAILED`, `RNS.RequestReceipt.SENT`, `RNS.RequestReceipt.DELIVERED`, `RNS.RequestReceipt.READY`.
#### get_progress()
* **Returns:**
The progress of a response being received as a *float* between 0.0 and 1.0.
#### get_response()
* **Returns:**
The response as *bytes* if it is ready, otherwise *None*.
#### get_response_time()
* **Returns:**
The response time of the request in seconds.
#### concluded()
* **Returns:**
True if the associated request has concluded (successfully or with a failure), otherwise False.
The Resource class allows transferring arbitrary amounts
of data over a link. It will automatically handle sequencing,
compression, coordination and checksumming.
* **Parameters:**
* **data** – The data to be transferred. Can be *bytes* or an open *file handle*. See the [Filetransfer Example](examples.md#example-filetransfer) for details.
* **link** – The [RNS.Link](#api-link) instance on which to transfer the data.
* **advertise** – Optional. Whether to automatically advertise the resource. Can be *True* or *False*.
* **auto_compress** – Optional. Whether to auto-compress the resource. Can be *True* or *False*.
* **callback** – An optional *callable* with the signature *callback(resource)*. Will be called when the resource transfer concludes.
* **progress_callback** – An optional *callable* with the signature *callback(resource)*. Will be called whenever the resource transfer progress is updated.
#### advertise()
Advertise the resource. If the other end of the link accepts
the resource advertisement it will begin transferring.
#### cancel()
Cancels transferring the resource.
#### get_progress()
* **Returns:**
The current progress of the resource transfer as a *float* between 0.0 and 1.0.
#### get_transfer_size()
* **Returns:**
The number of bytes needed to transfer the resource.
#### get_data_size()
* **Returns:**
The total data size of the resource.
#### get_parts()
* **Returns:**
The number of parts the resource will be transferred in.
#### get_segments()
* **Returns:**
The number of segments the resource is divided into.
#### get_hash()
* **Returns:**
The hash of the resource.
#### is_compressed()
* **Returns:**
Whether the resource is compressed.
### *class* RNS.Channel.Channel
Provides reliable delivery of messages over
a link.
`Channel` differs from `Request` and
`Resource` in some important ways:
> **Continuous**
> : Messages can be sent or received as long as
> the `Link` is open.
> **Bi-directional**
> : Messages can be sent in either direction on
> the `Link`; neither end is the client or
> server.
> **Size-constrained**
> : Messages must be encoded into a single packet.
`Channel` is similar to `Packet`, except that it
provides reliable delivery (automatic retries) as well
as a structure for exchanging several types of
messages over the `Link`.
`Channel` is not instantiated directly, but rather
Requests a path to the destination from the network and
blocks until the path is available, or the timeout is reached.
* **Parameters:**
* **destination_hash** – A destination hash as *bytes*.
* **timeout** – An optional timeout in seconds.
* **on_interface** – If specified, the path request will only be sent on this interface. In normal use, Reticulum handles this automatically, and this parameter should not be used.
* **Returns:**
*True* if a path to the destination is found, otherwise *False*.
* **on_interface** – If specified, the path request will only be sent on this interface. In normal use, Reticulum handles this automatically, and this parameter should not be used.