-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.rs
185 lines (162 loc) · 5.34 KB
/
common.rs
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
use octseq::{Octets, OctetsBuilder, Parser};
use crate::util::parser::ParseError;
use inetnum::addr::Prefix;
use super::afisafi::Afi;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::fmt;
//------------ Types ---------------------------------------------------------
/// Path Identifier for BGP Multiple Paths (RFC7911).
///
/// Used in all AddpathNlri variants.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PathId(pub u32);
impl fmt::Display for PathId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
//------------ Helper functions ----------------------------------------------
#[deprecated = "use AFI specific methods"]
#[allow(dead_code)]
pub(super) fn parse_prefix<R: Octets>(parser: &mut Parser<'_, R>, afi: Afi)
-> Result<Prefix, ParseError>
{
let prefix_bits = parser.parse_u8()?;
parse_prefix_for_len(parser, prefix_bits, afi)
}
pub fn parse_prefix_for_len<R: Octets>(
parser: &mut Parser<'_, R>,
prefix_bits: u8,
afi: Afi
)
-> Result<Prefix, ParseError>
{
let prefix_bytes = prefix_bits_to_bytes(prefix_bits);
let res = match afi {
Afi::Ipv4 => {
let mut b = [0u8; 4];
//b[..prefix_bytes].copy_from_slice(parser.peek(prefix_bytes)?);
parser.parse_buf(&mut b[..prefix_bytes])?;
Prefix::new_v4(Ipv4Addr::from(b), prefix_bits).map_err(|e|
ParseError::form_error(e.static_description())
)?
},
Afi::Ipv6 => {
let mut b = [0u8; 16];
//b[..prefix_bytes].copy_from_slice(parser.peek(prefix_bytes)?);
parser.parse_buf(&mut b[..prefix_bytes])?;
Prefix::new_v6(Ipv6Addr::from(b), prefix_bits).map_err(|e|
ParseError::form_error(e.static_description())
)?
},
_ => { return Err(ParseError::form_error("unknown prefix format")); }
};
Ok(res)
}
pub(crate) fn parse_v4_prefix<R: Octets>(parser: &mut Parser<'_, R>)
-> Result<Prefix, ParseError>
{
let prefix_bits = parser.parse_u8()?;
parse_v4_prefix_for_len(parser, prefix_bits)
}
pub(super) fn parse_v4_prefix_for_len<R: Octets>(
parser: &mut Parser<'_, R>,
prefix_bits: u8,
) -> Result<Prefix, ParseError>
{
let prefix_bytes = prefix_bits_to_bytes(prefix_bits);
if prefix_bytes > 4 {
return Err(ParseError::form_error("illegal prefix length"));
}
let mut b = [0u8; 4];
parser.parse_buf(&mut b[..prefix_bytes])?;
Prefix::new_v4(Ipv4Addr::from(b), prefix_bits).map_err(|e|
ParseError::form_error(e.static_description())
)
}
pub(crate) fn parse_v6_prefix<R: Octets>(parser: &mut Parser<'_, R>)
-> Result<Prefix, ParseError>
{
let prefix_bits = parser.parse_u8()?;
parse_v6_prefix_for_len(parser, prefix_bits)
}
pub(super) fn parse_v6_prefix_for_len<R: Octets>(
parser: &mut Parser<'_, R>,
prefix_bits: u8,
) -> Result<Prefix, ParseError>
{
let prefix_bytes = prefix_bits_to_bytes(prefix_bits);
if prefix_bytes > 16 {
return Err(ParseError::form_error("illegal prefix length"));
}
let mut b = [0u8; 16];
parser.parse_buf(&mut b[..prefix_bytes])?;
Prefix::new_v6(Ipv6Addr::from(b), prefix_bits).map_err(|e|
ParseError::form_error(e.static_description())
)
}
pub(crate) const fn prefix_bits_to_bytes(bits: u8) -> usize {
((bits as usize) + 8 - 1) / 8
}
pub(super) fn compose_len_prefix(prefix: Prefix) -> usize {
prefix_bits_to_bytes(prefix.len())
}
pub(super) fn compose_prefix<Target: OctetsBuilder>(
prefix: Prefix,
target: &mut Target
) -> Result<(), Target::AppendError> {
let len = prefix.len();
target.append_slice(&[len])?;
let prefix_bytes = prefix_bits_to_bytes(len);
match prefix.addr() {
std::net::IpAddr::V4(a) => {
target.append_slice(&a.octets()[..prefix_bytes])?
}
std::net::IpAddr::V6(a) => {
target.append_slice(&a.octets()[..prefix_bytes])?
}
}
Ok(())
}
pub(super) fn compose_prefix_without_len<Target: OctetsBuilder>(
prefix: Prefix,
target: &mut Target
) -> Result<(), Target::AppendError> {
let prefix_bytes = prefix_bits_to_bytes(prefix.len());
match prefix.addr() {
std::net::IpAddr::V4(a) => {
target.append_slice(&a.octets()[..prefix_bytes])?
}
std::net::IpAddr::V6(a) => {
target.append_slice(&a.octets()[..prefix_bytes])?
}
}
Ok(())
}
//--- Skip functions
#[allow(dead_code)]
fn skip_prefix<R: Octets>(parser: &mut Parser<'_, R>)
-> Result<(), ParseError>
{
let prefix_bits = parser.parse_u8()?;
let prefix_bytes = prefix_bits_to_bytes(prefix_bits);
Ok(parser.advance(prefix_bytes)?)
}
#[allow(dead_code)]
fn skip_prefix_for_len<R: Octets>(parser: &mut Parser<'_, R>, prefix_bits: u8)
-> Result<(), ParseError>
{
let prefix_bytes = prefix_bits_to_bytes(prefix_bits);
Ok(parser.advance(prefix_bytes)?)
}
#[allow(dead_code)]
fn skip_prefix_addpath<R: Octets>(parser: &mut Parser<'_, R>)
-> Result<(), ParseError>
{
parser.advance(4)?;
let prefix_bits = parser.parse_u8()?;
let prefix_bytes = prefix_bits_to_bytes(prefix_bits);
Ok(parser.advance(prefix_bytes)?)
}