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
use std::hash;
use std::cmp::Ordering;
use std::time::Duration;
use routecore::addr::MaxLenPrefix;
use routecore::asn::Asn;
use routecore::bgpsec::KeyIdentifier;
use super::pdu::RouterKeyInfo;
#[derive(Clone, Copy, Debug)]
pub struct RouteOrigin {
pub prefix: MaxLenPrefix,
pub asn: Asn,
}
impl RouteOrigin {
pub fn new(prefix: MaxLenPrefix, asn: Asn) -> Self {
RouteOrigin { prefix, asn }
}
}
impl PartialEq for RouteOrigin {
fn eq(&self, other: &Self) -> bool {
self.prefix.prefix() == other.prefix.prefix()
&& self.prefix.resolved_max_len() == other.prefix.resolved_max_len()
&& self.asn == other.asn
}
}
impl Eq for RouteOrigin { }
impl PartialOrd for RouteOrigin {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for RouteOrigin {
fn cmp(&self, other: &Self) -> Ordering {
match self.prefix.prefix().cmp(&other.prefix.prefix()) {
Ordering::Equal => { }
other => return other
}
match self.prefix.resolved_max_len().cmp(
&other.prefix.resolved_max_len()
) {
Ordering::Equal => { }
other => return other
}
self.asn.cmp(&other.asn)
}
}
impl hash::Hash for RouteOrigin {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.prefix.prefix().hash(state);
self.prefix.resolved_max_len().hash(state);
self.asn.hash(state);
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct RouterKey {
pub key_identifier: KeyIdentifier,
pub asn: Asn,
pub key_info: RouterKeyInfo,
}
impl RouterKey {
pub fn new(
key_identifier: KeyIdentifier, asn: Asn, key_info: RouterKeyInfo
) -> Self {
RouterKey { key_identifier, asn, key_info }
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Payload {
Origin(RouteOrigin),
RouterKey(RouterKey),
}
impl Payload {
pub fn origin(prefix: MaxLenPrefix, asn: Asn) -> Self {
Payload::Origin(RouteOrigin::new(prefix, asn))
}
pub fn router_key(
key_identifier: KeyIdentifier, asn: Asn, key_info: RouterKeyInfo
) -> Self {
Payload::RouterKey(RouterKey::new(key_identifier, asn, key_info))
}
pub fn to_origin(&self) -> Option<RouteOrigin> {
match *self {
Payload::Origin(origin) => Some(origin),
_ => None
}
}
pub fn as_router_key(&self) -> Option<&RouterKey> {
match *self {
Payload::RouterKey(ref key) => Some(key),
_ => None
}
}
}
impl From<RouteOrigin> for Payload {
fn from(src: RouteOrigin) -> Self {
Payload::Origin(src)
}
}
impl From<RouterKey> for Payload {
fn from(src: RouterKey) -> Self {
Payload::RouterKey(src)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Action {
Announce,
Withdraw,
}
impl Action {
pub fn is_announce(self) -> bool {
matches!(self, Action::Announce)
}
pub fn is_withdraw(self) -> bool {
matches!(self, Action::Withdraw)
}
pub fn from_flags(flags: u8) -> Self {
if flags & 1 == 1 {
Action::Announce
}
else {
Action::Withdraw
}
}
pub fn into_flags(self) -> u8 {
match self {
Action::Announce => 1,
Action::Withdraw => 0
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Timing {
pub refresh: u32,
pub retry: u32,
pub expire: u32
}
impl Timing {
pub fn refresh_duration(self) -> Duration {
Duration::from_secs(u64::from(self.refresh))
}
}
impl Default for Timing {
fn default() -> Self {
Timing {
refresh: 3600,
retry: 600,
expire: 7200
}
}
}