veilid-core 0.5.3

Core library used to create a Veilid node and operate it as part of an application
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use super::*;

impl_veilid_log_facility!("rpc");

/// Routing configuration for destination
#[derive(Debug, Clone)]
pub(crate) struct UnsafeRoutingInfo {
    pub opt_node: Option<NodeRef>,
    pub opt_routing_domain: Option<RoutingDomain>,
}

/// Where to send an RPC message
#[derive(Debug, Clone)]
pub(crate) enum Destination {
    /// Send to node directly
    Direct {
        /// The node to send to
        node: FilteredNodeRef,
        /// Require safety route or not
        safety_selection: SafetySelection,
    },
    /// Send to node for relay purposes
    Relay {
        /// The relay dial info to send to
        relay_di: DialInfo,
        /// The final destination the relay should send to
        node: NodeRef,
    },
    /// Send to private route
    PrivateRoute {
        /// A private route to send to
        private_route: Arc<PrivateRoute>,
        /// Require safety route or not
        safety_selection: SafetySelection,
    },
}

impl PartialEq for Destination {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (
                Destination::Direct {
                    node: self_node,
                    safety_selection: self_safety_selection,
                },
                Destination::Direct {
                    node: other_node,
                    safety_selection: other_safety_selection,
                },
            ) => {
                self_node.equivalent(other_node) && self_safety_selection == other_safety_selection
            }
            (
                Destination::Relay {
                    relay_di: self_relay_di,
                    node: self_node,
                },
                Destination::Relay {
                    relay_di: other_relay_di,
                    node: other_node,
                },
            ) => self_relay_di == other_relay_di && self_node.equivalent(other_node),
            (
                Destination::PrivateRoute {
                    private_route: self_private_route,
                    safety_selection: self_safety_selection,
                },
                Destination::PrivateRoute {
                    private_route: other_private_route,
                    safety_selection: other_safety_selection,
                },
            ) => {
                self_private_route.public_key == other_private_route.public_key
                    && self_safety_selection == other_safety_selection
            }
            _ => false,
        }
    }
}

impl Eq for Destination {}

impl core::hash::Hash for Destination {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        core::mem::discriminant(self).hash(state);
        match self {
            Destination::Direct {
                node,
                safety_selection,
            } => {
                node.hash(state);
                safety_selection.hash(state);
            }
            Destination::Relay { relay_di, node } => {
                relay_di.hash(state);
                node.hash(state);
            }
            Destination::PrivateRoute {
                private_route,
                safety_selection,
            } => {
                private_route.public_key.hash(state);
                safety_selection.hash(state);
            }
        }
    }
}

impl Destination {
    pub fn direct(node: FilteredNodeRef, opt_safety_selection: Option<SafetySelection>) -> Self {
        let sequencing = node.sequencing();
        Self::Direct {
            node,
            safety_selection: opt_safety_selection
                .unwrap_or_else(|| SafetySelection::Unsafe(sequencing)),
        }
    }
    pub fn relay(relay_di: DialInfo, node: NodeRef) -> Self {
        Self::Relay { relay_di, node }
    }
    pub fn private_route(
        private_route: Arc<PrivateRoute>,
        safety_selection: SafetySelection,
    ) -> Self {
        Self::PrivateRoute {
            private_route,
            safety_selection,
        }
    }

    pub fn get_safety_selection(&self) -> SafetySelection {
        match self {
            Destination::Direct {
                node: _,
                safety_selection,
            } => safety_selection.clone(),
            Destination::Relay { relay_di: _, node } => {
                // Relayed dialinfo is always sent directly
                SafetySelection::Unsafe(node.sequencing())
            }
            Destination::PrivateRoute {
                private_route: _,
                safety_selection,
            } => safety_selection.clone(),
        }
    }

    pub fn get_target_node_ids(&self) -> Option<NodeIdGroup> {
        match self {
            Destination::Direct {
                node,
                safety_selection: _,
            } => Some(node.node_ids()),
            Destination::Relay { relay_di: _, node } => Some(node.node_ids()),
            Destination::PrivateRoute {
                private_route: _,
                safety_selection: _,
            } => None,
        }
    }

    pub fn get_target(&self, routing_table: &RoutingTable) -> Result<Target, RPCError> {
        match self {
            Destination::Direct {
                node,
                safety_selection: _,
            } => {
                Ok(Target::NodeId(node.best_node_id().ok_or_else(|| {
                    RPCError::protocol("no supported node id")
                })?))
            }
            Destination::Relay { relay_di: _, node } => {
                Ok(Target::NodeId(node.best_node_id().ok_or_else(|| {
                    RPCError::protocol("no supported node id")
                })?))
            }
            Destination::PrivateRoute {
                private_route,
                safety_selection: _,
            } => {
                // Add the remote private route if we're going to keep the id
                let route_id = routing_table
                    .route_spec_store()
                    .import_single_remote_route(private_route.clone())
                    .map_err(RPCError::protocol)?;

                Ok(Target::RouteId(route_id))
            }
        }
    }

    pub fn get_unsafe_routing_info(
        &self,
        routing_table: &RoutingTable,
    ) -> Option<UnsafeRoutingInfo> {
        // If there's a safety route in use, the safety route will be responsible for the routing
        match self.get_safety_selection() {
            SafetySelection::Unsafe(_) => {}
            SafetySelection::Safe(_) => {
                return None;
            }
        }

        // Get:
        // * The target node (possibly relayed)
        // * The routing domain we are sending to if we can determine it
        let (opt_node, opt_routing_domain) = match self {
            Destination::Direct {
                node,
                safety_selection: _,
            } => {
                let opt_routing_domain = node.best_routing_domain();
                if opt_routing_domain.is_none() {
                    // No routing domain for target, no node info
                    // Only a stale connection or no connection exists
                    veilid_log!(node warn "No routing domain for node: node={}", node);
                };
                (Some(node.unfiltered()), opt_routing_domain)
            }
            Destination::Relay { relay_di, node } => {
                let opt_routing_domain =
                    routing_table.routing_domain_for_address(relay_di.address());

                (Some(node.clone()), opt_routing_domain)
            }
            Destination::PrivateRoute {
                private_route: _,
                safety_selection: _,
            } => (None, Some(RoutingDomain::PublicInternet)),
        };

        Some(UnsafeRoutingInfo {
            opt_node,
            opt_routing_domain,
        })
    }

    pub fn destination_key(&self) -> Result<PublicKey, RPCError> {
        match self {
            Destination::Direct {
                node,
                safety_selection: _,
            } => {
                let routing_domain = node
                    .best_routing_domain()
                    .ok_or_else(|| RPCError::internal("no reachable routing domain"))?;
                let public_key = node
                    .best_public_key(routing_domain)
                    .ok_or_else(|| RPCError::internal("no public key in routing domain"))?;
                Ok(public_key)
            }
            Destination::Relay { relay_di: _, node } => {
                let routing_domain = node
                    .best_routing_domain()
                    .ok_or_else(|| RPCError::internal("no reachable routing domain"))?;
                let public_key = node
                    .best_public_key(routing_domain)
                    .ok_or_else(|| RPCError::internal("no public key in routing domain"))?;
                Ok(public_key)
            }
            Destination::PrivateRoute {
                private_route,
                safety_selection: _,
            } => Ok(private_route.public_key.clone()),
        }
    }
}

impl fmt::Display for Destination {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Destination::Direct {
                node,
                safety_selection,
            } => {
                let sr = if matches!(safety_selection, SafetySelection::Safe(_)) {
                    "%SR"
                } else {
                    ""
                };

                write!(f, "{}{}", node, sr)
            }
            Destination::Relay { relay_di, node } => {
                write!(f, "{}@{}", node, relay_di)
            }
            Destination::PrivateRoute {
                private_route,
                safety_selection,
            } => {
                let sr = if matches!(safety_selection, SafetySelection::Safe(_)) {
                    "%SR"
                } else {
                    ""
                };

                write!(f, "PR%{}{}", private_route.public_key, sr)
            }
        }
    }
}

impl RPCProcessor {
    /// Convert a 'Target' into a 'Destination'
    pub async fn resolve_target_to_destination(
        &self,
        target: Target,
        safety_selection: SafetySelection,
    ) -> Result<rpc_processor::Destination, RPCError> {
        match target {
            Target::NodeId(node_id) => {
                // Resolve node
                let nr = match self.resolve_node(node_id, safety_selection.clone()).await? {
                    Some(nr) => nr,
                    None => {
                        return Err(RPCError::network("could not resolve node id"));
                    }
                };
                // Apply sequencing to match safety selection
                let nr = nr.sequencing_filtered(safety_selection.get_sequencing());

                Ok(rpc_processor::Destination::Direct {
                    node: nr,
                    safety_selection,
                })
            }
            Target::RouteId(rsid) => {
                // Get remote private route
                let Some(private_route) = self
                    .routing_table()
                    .route_spec_store()
                    .best_remote_private_route(&rsid)
                else {
                    return Err(RPCError::network("could not get remote private route"));
                };

                Ok(rpc_processor::Destination::PrivateRoute {
                    private_route,
                    safety_selection,
                })
            }
        }
    }

    /// Convert the 'Destination' into a 'RespondTo' for a response
    pub(super) async fn get_destination_respond_to(
        &self,
        dest: &Destination,
    ) -> RPCNetworkResult<RespondTo> {
        let routing_table = self.routing_table();
        let rss = routing_table.route_spec_store();

        match dest {
            Destination::Direct {
                node: target,
                safety_selection,
            } => match safety_selection {
                SafetySelection::Unsafe(_) => {
                    // Sent directly with no safety route, can respond directly
                    Ok(NetworkResult::value(RespondTo::Sender))
                }
                SafetySelection::Safe(safety_spec) => {
                    // Sent directly but with a safety route, respond to private route
                    let crypto_kind = target
                        .best_node_id()
                        .ok_or_else(|| RPCError::protocol("no supported node id"))?
                        .kind();
                    let RouteIdAndKeys {
                        route_id: _,
                        route_set_keys: public_keys,
                    } = network_result_try!(rss
                        .select_single_route(RouteSelectParams {
                            crypto_kind,
                            preferred_route: safety_spec.preferred_route.clone(),
                            hop_count: safety_spec.hop_count,
                            stability: safety_spec.stability,
                            sequencing: safety_spec.sequencing,
                            directions: Direction::In.into(),
                            avoid_nodes: target.node_ids().to_vec(),
                            is_destination_safe: false,
                        })
                        .await
                        .to_rpc_network_result()?);

                    let pr_key = public_keys.get(crypto_kind).unwrap_or_log();

                    // Get the assembled route for response
                    let private_route = network_result_try!(rss
                        .assemble_single_private_route(&pr_key, None)
                        .await
                        .to_rpc_network_result()?);

                    Ok(NetworkResult::Value(RespondTo::PrivateRoute(private_route)))
                }
            },
            Destination::Relay {
                relay_di: _,
                node: _,
            } => {
                // Sent directly via a relay, must respond directly
                Ok(NetworkResult::value(RespondTo::Sender))
            }
            Destination::PrivateRoute {
                private_route,
                safety_selection,
            } => {
                let Some(avoid_node_id) = private_route.first_hop_node_id() else {
                    return Err(RPCError::internal(
                        "destination private route must have first hop",
                    ));
                };

                let crypto_kind = private_route.public_key.kind();

                match safety_selection {
                    SafetySelection::Unsafe(_) => {
                        // Sent to a private route with no safety route, use a stub safety route for the response

                        let Some(published_peer_info) =
                            routing_table.get_published_peer_info(RoutingDomain::PublicInternet)
                        else {
                            return Ok(NetworkResult::service_unavailable(
                                "Own node info must be published to use private route",
                            ));
                        };

                        // Determine if we can use optimized nodeinfo
                        let route_node = if rss.has_remote_private_route_seen_our_node_info(
                            &private_route.public_key,
                            &published_peer_info,
                        ) {
                            RouteNode::NodeId(routing_table.node_id(crypto_kind))
                        } else {
                            RouteNode::PeerInfo(published_peer_info)
                        };

                        Ok(NetworkResult::value(RespondTo::PrivateRoute(Arc::new(
                            PrivateRoute::new_stub(
                                routing_table.public_key(crypto_kind),
                                route_node,
                            ),
                        ))))
                    }
                    SafetySelection::Safe(safety_spec) => {
                        // Sent to a private route via a safety route, respond to private route

                        // Check for loopback test
                        let opt_private_route_id =
                            rss.get_route_id_for_key(&private_route.public_key);
                        let pr_key = if opt_private_route_id.is_some()
                            && safety_spec.preferred_route == opt_private_route_id
                        {
                            // Private route is also safety route during loopback test
                            private_route.public_key.clone()
                        } else {
                            // Get the private route to respond to that matches the safety route spec we sent the request with
                            network_result_try!(rss
                                .select_single_route(RouteSelectParams {
                                    crypto_kind,
                                    preferred_route: safety_spec.preferred_route.clone(),
                                    hop_count: safety_spec.hop_count,
                                    stability: safety_spec.stability,
                                    sequencing: safety_spec.sequencing,
                                    directions: Direction::In.into(),
                                    avoid_nodes: vec![avoid_node_id],
                                    is_destination_safe: true,
                                })
                                .await
                                .to_rpc_network_result()?)
                            .route_set_keys
                            .get(crypto_kind)
                            .unwrap_or_log()
                        };

                        // Get the assembled route for response
                        let private_route = network_result_try!(rss
                            .assemble_single_private_route(&pr_key, None)
                            .await
                            .to_rpc_network_result()?);

                        Ok(NetworkResult::Value(RespondTo::PrivateRoute(private_route)))
                    }
                }
            }
        }
    }

    /// Convert the 'RespondTo' into a 'Destination' for a response
    pub(super) fn get_respond_to_destination(
        &self,
        request: &Message,
    ) -> NetworkResult<Destination> {
        // Get the question 'respond to'
        let respond_to = match request.operation.kind() {
            RPCOperationKind::Question(q) => q.respond_to(),
            _ => {
                panic!("not a question");
            }
        };

        // To where should we respond?
        match respond_to {
            RespondTo::Sender => {
                // Parse out the header detail from the question
                let detail = match &request.header.detail {
                    RPCMessageHeaderDetail::Direct(detail) => detail,
                    RPCMessageHeaderDetail::SafetyRouted(_)
                    | RPCMessageHeaderDetail::PrivateRouted(_) => {
                        // If this was sent via a private route, we don't know what the sender was, so drop this
                        return NetworkResult::invalid_message(
                            "can't respond directly to non-direct question",
                        );
                    }
                };

                // Get the filtered noderef of the sender
                let sender_noderef = detail.sender_noderef.clone();
                NetworkResult::value(Destination::direct(sender_noderef, None))
            }
            RespondTo::PrivateRoute(pr) => {
                match &request.header.detail {
                    RPCMessageHeaderDetail::Direct(_) => {
                        // If this was sent directly, we should only ever respond directly
                        NetworkResult::invalid_message(
                            "not responding to private route from direct question",
                        )
                    }
                    RPCMessageHeaderDetail::SafetyRouted(detail) => {
                        // If this was sent via a safety route, but not received over our private route, don't respond with a safety route,
                        // it would give away which safety routes belong to this node
                        NetworkResult::value(Destination::private_route(
                            pr.clone(),
                            SafetySelection::Unsafe(detail.sequencing),
                        ))
                    }
                    RPCMessageHeaderDetail::PrivateRouted(detail) => {
                        // If this was received over our private route, it's okay to respond to a private route via our safety route
                        NetworkResult::value(Destination::private_route(
                            pr.clone(),
                            SafetySelection::Safe(detail.safety_spec.clone()),
                        ))
                    }
                }
            }
        }
    }
}