overnet_core/
labels.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fidl_fuchsia_overnet_protocol::TRANSFER_KEY_LENGTH;
6use rand::Rng;
7
8/// Labels a node with a mesh-unique address
9#[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash, Debug)]
10pub struct NodeId(pub u64);
11
12/// Identifies whether an endpoint is a client or server
13#[derive(Copy, Clone, PartialEq, Eq, Debug)]
14pub enum Endpoint {
15    /// Endpoint is a client
16    Client,
17    /// Endpoint is a server
18    Server,
19}
20
21impl NodeId {
22    /// Makes a string node ID for use with the circuit protocol.
23    pub fn circuit_string(&self) -> String {
24        format!("{:x}", self.0)
25    }
26
27    /// Turns a string node id from the circuit protocol into a `NodeId`
28    pub fn from_circuit_string(id: &str) -> Result<Self, ()> {
29        Ok(NodeId(u64::from_str_radix(id, 16).map_err(|_| ())?))
30    }
31}
32
33impl From<u64> for NodeId {
34    fn from(id: u64) -> Self {
35        NodeId(id)
36    }
37}
38
39impl From<NodeId> for fidl_fuchsia_overnet_protocol::NodeId {
40    fn from(id: NodeId) -> Self {
41        Self { id: id.0 }
42    }
43}
44
45impl From<&NodeId> for fidl_fuchsia_overnet_protocol::NodeId {
46    fn from(id: &NodeId) -> Self {
47        Self { id: id.0 }
48    }
49}
50
51impl From<fidl_fuchsia_overnet_protocol::NodeId> for NodeId {
52    fn from(id: fidl_fuchsia_overnet_protocol::NodeId) -> Self {
53        id.id.into()
54    }
55}
56
57/// Labels a link with a node-unique identifier
58#[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, Hash)]
59pub struct NodeLinkId(pub u64);
60
61impl From<u64> for NodeLinkId {
62    fn from(id: u64) -> Self {
63        NodeLinkId(id)
64    }
65}
66
67pub(crate) type TransferKey = [u8; TRANSFER_KEY_LENGTH as usize];
68
69pub(crate) fn generate_transfer_key() -> TransferKey {
70    rand::thread_rng().gen::<TransferKey>()
71}