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
use std::fmt::{Display, Formatter};
use std::net::{Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use ipnet::IpNet;
use serde::{Serialize, Serializer, Deserialize};
use crate::err::BgpModelsError;
#[derive(Debug, Clone, Serialize, Copy)]
pub struct AddrMeta {
pub afi: Afi,
pub asn_len: AsnLength,
}
#[derive(Debug, Clone, Serialize, Copy, Deserialize, PartialEq, Eq)]
pub enum AsnLength {
Bits16,
Bits32,
}
#[derive(Debug, Clone, Copy, Eq)]
pub struct Asn {
pub asn: u32,
pub len: AsnLength,
}
impl PartialEq for Asn {
fn eq(&self, other: &Self) -> bool {
self.asn==other.asn
}
}
impl PartialEq<i32> for Asn {
fn eq(&self, other: &i32) -> bool {
self.asn as i32==*other
}
}
impl PartialEq<u32> for Asn {
fn eq(&self, other: &u32) -> bool {
self.asn==*other
}
}
impl From<u32> for Asn {
fn from(v: u32) -> Self {
Asn{asn:v, len: AsnLength::Bits32}
}
}
impl From<i32> for Asn {
fn from(v: i32) -> Self {
Asn{asn:v as u32, len: AsnLength::Bits32}
}
}
impl Into<i32> for Asn {
fn into(self) -> i32 {
self.asn as i32
}
}
impl Into<u32> for Asn {
fn into(self) -> u32 {
self.asn
}
}
impl Serialize for Asn {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
Ok( serializer.serialize_u32(self.asn)?)
}
}
#[derive(Debug, PartialEq, Primitive, Clone, Copy, Serialize, Eq)]
pub enum Afi {
Ipv4 = 1,
Ipv6 = 2,
}
#[derive(Debug, PartialEq, Primitive, Clone, Copy, Serialize, Eq)]
pub enum Safi {
Unicast = 1,
Multicast = 2,
UnicastMulticast = 3,
}
#[derive(Debug, PartialEq, Copy, Clone, Serialize, Eq)]
pub enum NextHopAddress {
Ipv4(Ipv4Addr),
Ipv6(Ipv6Addr),
Ipv6LinkLocal(Ipv6Addr, Ipv6Addr),
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct NetworkPrefix {
pub prefix: IpNet,
pub path_id: u32,
}
impl Serialize for NetworkPrefix {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
serializer.serialize_str(self.to_string().as_str())
}
}
impl FromStr for NetworkPrefix {
type Err = BgpModelsError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let prefix = IpNet::from_str(s)?;
Ok(
NetworkPrefix{
prefix,
path_id: 0,
}
)
}
}
impl NetworkPrefix {
pub fn new(prefix: IpNet, path_id: u32) -> NetworkPrefix {
NetworkPrefix { prefix, path_id }
}
}
impl Display for NetworkPrefix {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.prefix)
}
}
impl Display for Asn {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.asn)
}
}