Skip to main content

rns_net/common/
callbacks.rs

1//! Application callback trait for driver events.
2
3use crate::{DestHash, IdentityHash, InterfaceId, LinkId, PacketHash, TeardownReason};
4
5/// Names a packet queued with `RnsNode::send_packet_deferred` until
6/// [`Callbacks::on_packet_sent`] reports its hash.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct SendTicket(pub u64);
9
10pub trait Callbacks: Send {
11    fn on_announce(&mut self, announced: crate::common::destination::AnnouncedIdentity);
12
13    fn on_path_updated(&mut self, dest_hash: DestHash, hops: u8);
14
15    fn on_local_delivery(&mut self, dest_hash: DestHash, raw: Vec<u8>, packet_hash: PacketHash);
16
17    /// Called when an interface comes online.
18    fn on_interface_up(&mut self, _id: InterfaceId) {}
19
20    /// Called when an interface goes offline.
21    fn on_interface_down(&mut self, _id: InterfaceId) {}
22
23    /// Called when a link is fully established.
24    fn on_link_established(
25        &mut self,
26        _link_id: LinkId,
27        _dest_hash: DestHash,
28        _rtt: f64,
29        _is_initiator: bool,
30    ) {
31    }
32
33    /// Called when a link is closed.
34    fn on_link_closed(&mut self, _link_id: LinkId, _reason: Option<TeardownReason>) {}
35
36    /// Called when a remote peer identifies on a link.
37    fn on_remote_identified(
38        &mut self,
39        _link_id: LinkId,
40        _identity_hash: IdentityHash,
41        _public_key: [u8; 64],
42    ) {
43    }
44
45    /// Called when a resource transfer delivers data.
46    fn on_resource_received(
47        &mut self,
48        _link_id: LinkId,
49        _data: Vec<u8>,
50        _metadata: Option<Vec<u8>>,
51    ) {
52    }
53
54    /// Called when a resource transfer delivers data, with the application
55    /// flag the sender set in the advertisement. The default ignores the flag
56    /// and forwards to [`Callbacks::on_resource_received`].
57    fn on_resource_received_with_app_flag(
58        &mut self,
59        link_id: LinkId,
60        data: Vec<u8>,
61        metadata: Option<Vec<u8>>,
62        _app_flag: bool,
63    ) {
64        self.on_resource_received(link_id, data, metadata);
65    }
66
67    /// Called when a resource transfer completes (sender-side proof validated).
68    fn on_resource_completed(&mut self, _link_id: LinkId) {}
69
70    /// Called when a resource transfer fails.
71    fn on_resource_failed(&mut self, _link_id: LinkId, _error: String) {}
72
73    /// Called with resource transfer progress updates.
74    fn on_resource_progress(&mut self, _link_id: LinkId, _received: usize, _total: usize) {}
75
76    /// Called when a streaming Resource completes on the sender side.
77    fn on_resource_stream_completed(
78        &mut self,
79        _link_id: LinkId,
80        _transfer_id: crate::resource::ResourceTransferId,
81    ) {
82    }
83
84    /// Called when a streaming Resource fails.
85    fn on_resource_stream_failed(
86        &mut self,
87        _link_id: LinkId,
88        _transfer_id: crate::resource::ResourceTransferId,
89        _error: crate::resource::ResourceTransferError,
90    ) {
91    }
92
93    /// Called with logical-byte progress for a streaming Resource.
94    fn on_resource_stream_progress(
95        &mut self,
96        _link_id: LinkId,
97        _transfer_id: crate::resource::ResourceTransferId,
98        _transferred: u64,
99        _total: u64,
100    ) {
101    }
102
103    /// Called with each verified segment of an incoming Resource received
104    /// in [`ResourceReceiveMode::Stream`](crate::resource::ResourceReceiveMode::Stream),
105    /// in order: `offset` is where `data` starts in the Resource.
106    fn on_resource_segment(
107        &mut self,
108        _link_id: LinkId,
109        _original_hash: [u8; 32],
110        _offset: u64,
111        _data: Vec<u8>,
112    ) {
113    }
114
115    /// Called when an incoming Resource received in
116    /// [`ResourceReceiveMode::Stream`](crate::resource::ResourceReceiveMode::Stream)
117    /// has been delivered in full.
118    fn on_resource_stream_received(
119        &mut self,
120        _link_id: LinkId,
121        _resource: crate::resource::ReceivedResourceStream,
122    ) {
123    }
124
125    /// Called when a disk-backed incoming Resource is ready.
126    fn on_resource_file_received(
127        &mut self,
128        _link_id: LinkId,
129        _resource: crate::resource::ReceivedResourceFile,
130    ) {
131    }
132
133    /// Called to ask whether to accept an incoming resource (for AcceptApp strategy).
134    /// Return true to accept, false to reject.
135    fn on_resource_accept_query(
136        &mut self,
137        _link_id: LinkId,
138        _resource_hash: Vec<u8>,
139        _transfer_size: u64,
140        _has_metadata: bool,
141    ) -> bool {
142        false
143    }
144
145    /// Called to ask whether to accept an incoming resource (for AcceptApp
146    /// strategy), with the application flag set in its advertisement
147    /// (Python `ResourceAdvertisement.app_flag_set()`). The default ignores
148    /// the flag and forwards to [`Callbacks::on_resource_accept_query`].
149    fn on_resource_accept_query_with_app_flag(
150        &mut self,
151        link_id: LinkId,
152        resource_hash: Vec<u8>,
153        transfer_size: u64,
154        has_metadata: bool,
155        _app_flag: bool,
156    ) -> bool {
157        self.on_resource_accept_query(link_id, resource_hash, transfer_size, has_metadata)
158    }
159
160    /// Called when a channel message is received on a link.
161    fn on_channel_message(&mut self, _link_id: LinkId, _msgtype: u16, _payload: Vec<u8>) {}
162
163    /// Called when generic link data is received.
164    fn on_link_data(&mut self, _link_id: LinkId, _context: u8, _data: Vec<u8>) {}
165
166    /// Called when a response is received on a link.
167    fn on_response(&mut self, _link_id: LinkId, _request_id: [u8; 16], _data: Vec<u8>) {}
168
169    /// Called when a response is received on a link, including resource metadata when present.
170    fn on_response_with_metadata(
171        &mut self,
172        link_id: LinkId,
173        request_id: [u8; 16],
174        data: Vec<u8>,
175        _metadata: Option<Vec<u8>>,
176    ) {
177        self.on_response(link_id, request_id, data);
178    }
179
180    /// Called when a request response is rejected.
181    fn on_request_failed(
182        &mut self,
183        _link_id: LinkId,
184        _request_id: [u8; 16],
185        _reason: crate::link_manager::RequestFailure,
186    ) {
187    }
188
189    /// Called for a packet queued with `RnsNode::send_packet_deferred` once it
190    /// is encrypted, before it is sent: its hash, which a proof of it will
191    /// carry, or `None` when it could not be encrypted or the node is
192    /// shutting down, and it was not sent.
193    fn on_packet_sent(&mut self, _ticket: SendTicket, _packet_hash: Option<PacketHash>) {}
194
195    /// Called with the plaintext of a single packet for a destination that
196    /// delivers decrypted (`RnsNode::deliver_decrypted`), in place of
197    /// [`on_local_delivery`](Self::on_local_delivery). Packets that do not
198    /// decrypt are dropped, and not proved. Decrypted on several threads,
199    /// packets may be reported out of arrival order.
200    fn on_packet_received(
201        &mut self,
202        _dest_hash: DestHash,
203        _data: Vec<u8>,
204        _packet_hash: PacketHash,
205    ) {
206    }
207
208    /// Called when a delivery proof is received for a packet we sent.
209    /// `rtt` is the round-trip time in seconds.
210    fn on_proof(&mut self, _dest_hash: DestHash, _packet_hash: PacketHash, _rtt: f64) {}
211
212    /// Called for ProveApp strategy: should we prove this incoming packet?
213    /// Return true to generate and send a proof, false to skip.
214    fn on_proof_requested(&mut self, _dest_hash: DestHash, _packet_hash: PacketHash) -> bool {
215        true
216    }
217
218    /// Called when a direct connection is proposed by a peer (for AskApp policy).
219    /// Return true to accept, false to reject.
220    fn on_direct_connect_proposed(
221        &mut self,
222        _link_id: LinkId,
223        _peer_identity: Option<IdentityHash>,
224    ) -> bool {
225        false
226    }
227
228    /// Called when a direct P2P connection is established via hole punching.
229    fn on_direct_connect_established(&mut self, _link_id: LinkId, _interface_id: InterfaceId) {}
230
231    /// Called when a direct connection attempt fails.
232    fn on_direct_connect_failed(&mut self, _link_id: LinkId, _reason: u8) {}
233}