rns_core/transport/
queries.rs1use super::*;
2
3impl TransportEngine {
4 pub fn path_table_entries(&self) -> impl Iterator<Item = (&[u8; 16], &PathEntry)> {
5 self.path_table
6 .iter()
7 .filter_map(|(k, ps)| ps.primary().map(|e| (k, e)))
8 }
9
10 pub fn path_table_sets(&self) -> impl Iterator<Item = (&[u8; 16], &PathSet)> {
11 self.path_table.iter()
12 }
13
14 pub fn interface_count(&self) -> usize {
15 self.interfaces.len()
16 }
17
18 pub fn link_table_count(&self) -> usize {
19 self.link_table.len()
20 }
21
22 pub fn path_table_count(&self) -> usize {
23 self.path_table.len()
24 }
25
26 pub fn announce_table_count(&self) -> usize {
27 self.announce_table.len()
28 }
29
30 pub fn reverse_table_count(&self) -> usize {
31 self.reverse_table.len()
32 }
33
34 pub fn held_announces_count(&self) -> usize {
35 self.held_announces.len()
36 }
37
38 pub fn packet_hashlist_len(&self) -> usize {
39 self.packet_hashlist.len()
40 }
41
42 pub fn announce_sig_cache_len(&self) -> usize {
43 self.announce_sig_cache.len()
44 }
45
46 pub fn rate_limiter_count(&self) -> usize {
47 self.rate_limiter.len()
48 }
49
50 pub fn blackholed_count(&self) -> usize {
51 self.blackholed_identities.len()
52 }
53
54 pub fn tunnel_count(&self) -> usize {
55 self.tunnel_table.len()
56 }
57
58 pub fn discovery_pr_tags_count(&self) -> usize {
59 self.discovery_pr_tags.len()
60 }
61
62 #[cfg(test)]
63 pub(crate) fn has_discovery_pr_tag(&self, unique_tag: &[u8; 32]) -> bool {
64 self.discovery_pr_tag_set.contains(unique_tag)
65 }
66
67 pub fn discovery_path_requests_count(&self) -> usize {
68 self.discovery_path_requests.len()
69 }
70
71 pub fn announce_queue_count(&self) -> usize {
72 self.announce_queues.queue_count()
73 }
74
75 pub fn nonempty_announce_queue_count(&self) -> usize {
76 self.announce_queues.nonempty_queue_count()
77 }
78
79 pub fn queued_announce_count(&self) -> usize {
80 self.announce_queues.total_queued_announces()
81 }
82
83 pub fn queued_announce_bytes(&self) -> usize {
84 self.announce_queues.total_queued_bytes()
85 }
86
87 pub fn announce_queue_interface_cap_drop_count(&self) -> u64 {
88 self.announce_queues.interface_cap_drop_count()
89 }
90
91 pub fn local_destinations_count(&self) -> usize {
92 self.local_destinations.len()
93 }
94
95 pub fn rate_limiter(&self) -> &AnnounceRateLimiter {
96 &self.rate_limiter
97 }
98
99 pub fn interface_info(&self, id: &InterfaceId) -> Option<&InterfaceInfo> {
100 self.interfaces.get(id)
101 }
102
103 pub fn redirect_path(&mut self, dest_hash: &[u8; 16], interface: InterfaceId, now: f64) {
104 if let Some(entry) = self
105 .path_table
106 .get_mut(dest_hash)
107 .and_then(|ps| ps.primary_mut())
108 {
109 entry.receiving_interface = interface;
110 entry.hops = 1;
111 } else {
112 self.upsert_path_destination(
113 *dest_hash,
114 PathEntry {
115 timestamp: now,
116 next_hop: [0u8; 16],
117 hops: 1,
118 expires: now + 3600.0,
119 random_blobs: Vec::new(),
120 receiving_interface: interface,
121 packet_hash: [0u8; 32],
122 announce_raw: None,
123 },
124 now,
125 );
126 }
127 }
128
129 pub fn inject_path(&mut self, dest_hash: [u8; 16], entry: PathEntry) {
130 self.upsert_path_destination(dest_hash, entry.clone(), entry.timestamp);
131 }
132
133 pub fn drop_path(&mut self, dest_hash: &[u8; 16]) -> bool {
134 self.path_table.remove(dest_hash).is_some()
135 }
136
137 pub fn drop_all_via(&mut self, transport_hash: &[u8; 16]) -> usize {
138 let mut removed = 0usize;
139 for ps in self.path_table.values_mut() {
140 let before = ps.len();
141 ps.retain(|entry| &entry.next_hop != transport_hash);
142 removed += before - ps.len();
143 }
144 self.path_table.retain(|_, ps| !ps.is_empty());
145 removed
146 }
147
148 pub fn drop_paths_for_interface(&mut self, interface: InterfaceId) -> usize {
149 let mut removed = 0usize;
150 let mut cleared_destinations = Vec::new();
151 for (dest_hash, ps) in self.path_table.iter_mut() {
152 let before = ps.len();
153 ps.retain(|entry| entry.receiving_interface != interface);
154 if ps.is_empty() {
155 cleared_destinations.push(*dest_hash);
156 }
157 removed += before - ps.len();
158 }
159 self.path_table.retain(|_, ps| !ps.is_empty());
160 for dest_hash in cleared_destinations {
161 self.path_states.remove(&dest_hash);
162 }
163 removed
164 }
165
166 pub fn drop_reverse_for_interface(&mut self, interface: InterfaceId) -> usize {
167 let before = self.reverse_table.len();
168 self.reverse_table.retain(|_, entry| {
169 entry.receiving_interface != interface && entry.outbound_interface != interface
170 });
171 before - self.reverse_table.len()
172 }
173
174 pub fn drop_links_for_interface(&mut self, interface: InterfaceId) -> usize {
175 let before = self.link_table.len();
176 self.link_table.retain(|_, entry| {
177 entry.next_hop_interface != interface && entry.received_interface != interface
178 });
179 before - self.link_table.len()
180 }
181
182 pub fn drop_announce_queues(&mut self) {
183 self.announce_table.clear();
184 self.held_announces.clear();
185 self.announce_queues = AnnounceQueues::new(self.config.announce_queue_max_interfaces);
186 self.ingress_control.clear();
187 }
188
189 pub fn identity_hash(&self) -> Option<&[u8; 16]> {
190 self.config.identity_hash.as_ref()
191 }
192
193 pub fn transport_enabled(&self) -> bool {
194 self.config.transport_enabled
195 }
196
197 pub fn config(&self) -> &TransportConfig {
198 &self.config
199 }
200
201 pub fn set_packet_hashlist_max_entries(&mut self, max_entries: usize) {
202 self.config.packet_hashlist_max_entries = max_entries;
203 self.packet_hashlist = PacketHashlist::new(max_entries);
204 }
205
206 pub fn get_path_table(&self, max_hops: Option<u8>) -> Vec<PathTableRow> {
207 let mut result = Vec::new();
208 for (dest_hash, ps) in self.path_table.iter() {
209 if let Some(entry) = ps.primary() {
210 if let Some(max) = max_hops {
211 if entry.hops > max {
212 continue;
213 }
214 }
215 let iface_name = self
216 .interfaces
217 .get(&entry.receiving_interface)
218 .map(|i| i.name.clone())
219 .unwrap_or_else(|| {
220 alloc::format!("Interface({})", entry.receiving_interface.0)
221 });
222 result.push((
223 *dest_hash,
224 entry.timestamp,
225 entry.next_hop,
226 entry.hops,
227 entry.expires,
228 iface_name,
229 ));
230 }
231 }
232 result
233 }
234
235 pub fn get_rate_table(&self) -> Vec<RateTableRow> {
236 self.rate_limiter
237 .entries()
238 .map(|(hash, entry)| {
239 (
240 *hash,
241 entry.last,
242 entry.rate_violations,
243 entry.blocked_until,
244 entry.timestamps.clone(),
245 )
246 })
247 .collect()
248 }
249
250 pub fn get_blackholed(&self) -> Vec<([u8; 16], f64, f64, Option<alloc::string::String>)> {
251 self.blackholed_entries()
252 .map(|(hash, entry)| (*hash, entry.created, entry.expires, entry.reason.clone()))
253 .collect()
254 }
255
256 pub fn active_destination_hashes(&self) -> alloc::collections::BTreeSet<[u8; 16]> {
257 self.path_table.keys().copied().collect()
258 }
259
260 pub fn path_destination_cap_evict_count(&self) -> usize {
261 self.path_destination_cap_evict_count
262 }
263
264 pub fn active_packet_hashes(&self) -> Vec<[u8; 32]> {
265 self.path_table
266 .values()
267 .flat_map(|ps| ps.iter().map(|p| p.packet_hash))
268 .collect()
269 }
270
271 pub fn cull_rate_limiter(
272 &mut self,
273 active: &alloc::collections::BTreeSet<[u8; 16]>,
274 now: f64,
275 ttl_secs: f64,
276 ) -> usize {
277 self.rate_limiter.cull_stale(active, now, ttl_secs)
278 }
279
280 pub fn update_interface_freq(&mut self, id: InterfaceId, ia_freq: f64) {
281 if let Some(info) = self.interfaces.get_mut(&id) {
282 info.ia_freq = ia_freq;
283 }
284 }
285
286 pub fn held_announce_count(&self, interface: &InterfaceId) -> usize {
287 self.ingress_control.held_count(interface)
288 }
289
290 #[cfg(test)]
291 #[allow(dead_code)]
292 pub(crate) fn path_table(&self) -> &BTreeMap<[u8; 16], PathSet> {
293 &self.path_table
294 }
295
296 #[cfg(test)]
297 #[allow(dead_code)]
298 pub(crate) fn announce_table(&self) -> &BTreeMap<[u8; 16], AnnounceEntry> {
299 &self.announce_table
300 }
301
302 #[cfg(test)]
303 #[allow(dead_code)]
304 pub(crate) fn held_announces(&self) -> &BTreeMap<[u8; 16], AnnounceEntry> {
305 &self.held_announces
306 }
307
308 #[cfg(test)]
309 #[allow(dead_code)]
310 pub(crate) fn announce_retained_bytes(&self) -> usize {
311 self.announce_retained_bytes_total()
312 }
313
314 #[cfg(test)]
315 #[allow(dead_code)]
316 pub(crate) fn reverse_table(&self) -> &BTreeMap<[u8; 16], tables::ReverseEntry> {
317 &self.reverse_table
318 }
319}