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
// Copyright 2021 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use serde::{Deserialize, Serialize};
use xor_name::{Prefix, XorName};

type SocketId = XorName;

/// The planned route of a message.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub struct Itinerary {
    /// Source
    pub src: SrcLocation,
    /// Destionation
    pub dst: DstLocation,
    /// Wether this will be aggregated, and where.
    pub aggregation: Aggregation,
}

impl Itinerary {
    /// Elders will send their signed message, where recipients aggregate.
    pub fn aggregate_at_dst(&self) -> bool {
        matches!(self.aggregation, Aggregation::AtDestination)
    }

    /// Elders will aggregate a group sig before they each send one copy of it to dst.
    pub fn aggregate_at_src(&self) -> bool {
        matches!(self.aggregation, Aggregation::AtSource)
    }

    /// Name of the source
    pub fn src_name(&self) -> XorName {
        self.src.name()
    }

    /// Name of the destionation
    pub fn dst_name(&self) -> Option<XorName> {
        self.dst.name()
    }
}

/// Aggregation scheme
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum Aggregation {
    /// No aggregation is made, eg. when the payload contains full authority.
    None,
    /// Elders will aggregate a group sig before they each send one copy of it to dst.
    AtSource,
    /// Elders will send their signed message, where recipients aggregate.
    AtDestination,
}

/// An EndUser is represented by the name
/// it's proxied through, and its socket id.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub struct EndUser {
    /// The name it's proxied through
    pub xorname: XorName,
    /// This maps to the SocketAddr at the Elders where the EndUser is proxied through.
    pub socket_id: SocketId,
}

/// Message source location.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum SrcLocation {
    /// An EndUser.
    EndUser(EndUser),
    /// A single node with the given name.
    Node(XorName),
    /// A section close to a name.
    Section(XorName),
}

impl SrcLocation {
    /// Returns whether this location is a section.
    pub fn is_section(&self) -> bool {
        matches!(self, Self::Section(_))
    }

    /// Returns whether this location is an end user.
    pub fn is_user(&self) -> bool {
        matches!(self, Self::EndUser(_))
    }

    /// Returns whether the given name is part of this location
    pub fn equals(&self, name: &XorName) -> bool {
        match self {
            Self::EndUser(user) => &user.xorname == name,
            Self::Node(self_name) => name == self_name,
            Self::Section(some_name) => name == some_name,
        }
    }

    /// Returns the name of this location.
    pub fn name(&self) -> XorName {
        match self {
            Self::EndUser(user) => user.xorname,
            Self::Node(name) => *name,
            Self::Section(name) => *name,
        }
    }

    /// Returns this location as `DstLocation`
    pub fn to_dst(&self) -> DstLocation {
        match self {
            Self::EndUser(user) => DstLocation::EndUser(*user),
            Self::Node(name) => DstLocation::Node(*name),
            Self::Section(name) => DstLocation::Section(*name),
        }
    }
}

/// Message destination location.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum DstLocation {
    /// An EndUser.
    EndUser(EndUser),
    /// Destination is a single node with the given name.
    Node(XorName),
    /// Destination are the nodes of the section whose prefix matches the given name.
    Section(XorName),
    /// Destination is a specific node. To be directly connected to, and so the message is unrouted. `ConnectionInfo` is used to determine the target SocketAdrr for the message.
    DirectAndUnrouted,
}

impl DstLocation {
    /// Returns whether this location is a section.
    pub fn is_section(&self) -> bool {
        matches!(self, Self::Section(_))
    }

    /// Returns whether this location is an end user.
    pub fn is_user(&self) -> bool {
        matches!(self, Self::EndUser(_))
    }

    /// Returns whether the given name of the given prefix is part of this location.
    ///
    /// Returns None if `prefix` does not match `name`.
    pub fn contains(&self, name: &XorName, prefix: &Prefix) -> bool {
        if !prefix.matches(name) {
            return false;
        }

        match self {
            Self::EndUser(user) => prefix.matches(&user.xorname),
            Self::Node(self_name) => name == self_name,
            Self::Section(self_name) => prefix.matches(self_name),
            Self::DirectAndUnrouted => true,
        }
    }

    /// Returns the name of this location, or `None` if it is `Direct`.
    pub fn name(&self) -> Option<XorName> {
        match self {
            Self::EndUser(user) => Some(user.xorname),
            Self::Node(name) => Some(*name),
            Self::Section(name) => Some(*name),
            Self::DirectAndUnrouted => None,
        }
    }
}