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