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
use super::super::*;
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Ipv4Extensions {
pub auth: Option<IpAuthenticationHeader>,
}
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Ipv4ExtensionsSlice<'a> {
pub auth: Option<IpAuthenticationHeaderSlice<'a>>,
}
impl Ipv4Extensions {
pub fn from_slice(start_protocol: u8, slice: &[u8]) -> Result<(Ipv4Extensions, u8, &[u8]), ReadError> {
Ipv4ExtensionsSlice::from_slice(start_protocol, slice).map(
|v| (v.0.to_header(), v.1, v.2)
)
}
pub fn read<T: io::Read + io::Seek + Sized>(reader: &mut T, start_ip_number: u8) -> Result<(Ipv4Extensions, u8), ReadError> {
use ip_number::*;
if AUTH == start_ip_number {
let header = IpAuthenticationHeader::read(reader)?;
let next_ip_number = header.next_header;
Ok((
Ipv4Extensions{
auth: Some(header)
},
next_ip_number,
))
} else {
Ok((Default::default(), start_ip_number))
}
}
pub fn write<T: io::Write + Sized>(&self, writer: &mut T, start_ip_number: u8) -> Result<(), WriteError> {
use ip_number::*;
use IpNumber::*;
use ValueError::*;
match self.auth {
Some(ref header) => if AUTH == start_ip_number {
header.write(writer)
} else {
Err(Ipv4ExtensionNotReferenced(AuthenticationHeader).into())
},
None => Ok(())
}
}
pub fn header_len(&self) -> usize {
if let Some(ref header) = self.auth {
header.header_len()
} else {
0
}
}
pub fn set_next_headers(&mut self, last_protocol_number: u8) -> u8 {
use ip_number::*;
let mut next = last_protocol_number;
if let Some(ref mut header) = self.auth {
header.next_header = next;
next = AUTH;
}
next
}
pub fn next_header(&self, first_next_header: u8) -> Result<u8, ValueError> {
use ip_number::*;
if let Some(ref auth) = self.auth {
if first_next_header == AUTH {
Ok(auth.next_header)
} else {
Err(
ValueError::Ipv4ExtensionNotReferenced(
IpNumber::AuthenticationHeader
)
)
}
} else {
Ok(first_next_header)
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.auth.is_none()
}
}
impl<'a> Ipv4ExtensionsSlice<'a> {
pub fn from_slice(start_ip_number: u8, start_slice: &'a [u8]) -> Result<(Ipv4ExtensionsSlice, u8, &[u8]), ReadError> {
use ip_number::*;
if AUTH == start_ip_number {
let header = IpAuthenticationHeaderSlice::from_slice(start_slice)?;
let rest = &start_slice[header.slice().len()..];
let next_header = header.next_header();
Ok((
Ipv4ExtensionsSlice{
auth: Some(header)
},
next_header,
rest
))
} else {
Ok((Default::default(), start_ip_number, start_slice))
}
}
pub fn to_header(&self) -> Ipv4Extensions {
Ipv4Extensions {
auth: self.auth.as_ref().map(|v| v.to_header())
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.auth.is_none()
}
}