-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathether_backup.rs
More file actions
291 lines (285 loc) · 8.6 KB
/
ether_backup.rs
File metadata and controls
291 lines (285 loc) · 8.6 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#![allow(missing_docs)]
#![allow(unused_parens)]
use crate::ether::{EtherAddr, EtherType};
use crate::{Buf, PktBuf, PktBufMut};
use crate::{Cursor, CursorMut};
/// A constant that defines the fixed byte length of the EtherFrame protocol header.
pub const ETHERFRAME_HEADER_LEN: usize = 14;
/// A fixed EtherFrame header.
pub const ETHERFRAME_HEADER_TEMPLATE: [u8; 14] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
];
#[derive(Debug, Clone, Copy)]
pub struct EtherFrame<T> {
buf: T,
}
impl<T: Buf> EtherFrame<T> {
#[inline]
pub fn parse_unchecked(buf: T) -> Self {
Self { buf }
}
#[inline]
pub fn buf(&self) -> &T {
&self.buf
}
#[inline]
pub fn release(self) -> T {
self.buf
}
#[inline]
pub fn parse(buf: T) -> Result<Self, T> {
let chunk_len = buf.chunk().len();
if chunk_len < 14 {
return Err(buf);
}
let container = Self { buf };
Ok(container)
}
#[inline]
pub fn fix_header_slice(&self) -> &[u8] {
&self.buf.chunk()[0..14]
}
#[inline]
pub fn dst_addr(&self) -> EtherAddr {
EtherAddr::from_bytes(&self.buf.chunk()[0..6])
}
#[inline]
pub fn src_addr(&self) -> EtherAddr {
EtherAddr::from_bytes(&self.buf.chunk()[6..12])
}
#[inline]
pub fn ethertype(&self) -> EtherType {
EtherType::from(u16::from_be_bytes(
(&self.buf.chunk()[12..14]).try_into().unwrap(),
))
}
}
impl<T: PktBuf> EtherFrame<T> {
#[inline]
pub fn payload(self) -> T {
let mut buf = self.buf;
buf.advance(14);
buf
}
}
impl<T: PktBufMut> EtherFrame<T> {
#[inline]
pub fn prepend_header<'a>(mut buf: T, header: &'a [u8; 14]) -> Self {
assert!(buf.chunk_headroom() >= 14);
buf.move_back(14);
(&mut buf.chunk_mut()[0..14]).copy_from_slice(&header.as_ref()[..]);
Self { buf }
}
#[inline]
pub fn set_dst_addr(&mut self, value: EtherAddr) {
(&mut self.buf.chunk_mut()[0..6]).copy_from_slice(value.as_bytes());
}
#[inline]
pub fn set_src_addr(&mut self, value: EtherAddr) {
(&mut self.buf.chunk_mut()[6..12]).copy_from_slice(value.as_bytes());
}
#[inline]
pub fn set_ethertype(&mut self, value: EtherType) {
(&mut self.buf.chunk_mut()[12..14]).copy_from_slice(&u16::from(value).to_be_bytes());
}
}
impl<'a> EtherFrame<Cursor<'a>> {
#[inline]
pub fn parse_from_cursor(buf: Cursor<'a>) -> Result<Self, Cursor<'a>> {
let remaining_len = buf.chunk().len();
if remaining_len < 14 {
return Err(buf);
}
let container = Self { buf };
Ok(container)
}
#[inline]
pub fn payload_as_cursor(&self) -> Cursor<'_> {
Cursor::new(&self.buf.chunk()[14..])
}
#[inline]
pub fn from_header_array(header_array: &'a [u8; 14]) -> Self {
Self {
buf: Cursor::new(header_array.as_slice()),
}
}
}
impl<'a> EtherFrame<CursorMut<'a>> {
#[inline]
pub fn parse_from_cursor_mut(buf: CursorMut<'a>) -> Result<Self, CursorMut<'a>> {
let remaining_len = buf.chunk().len();
if remaining_len < 14 {
return Err(buf);
}
let container = Self { buf };
Ok(container)
}
#[inline]
pub fn payload_as_cursor_mut(&mut self) -> CursorMut<'_> {
CursorMut::new(&mut self.buf.chunk_mut()[14..])
}
#[inline]
pub fn from_header_array_mut(header_array: &'a mut [u8; 14]) -> Self {
Self {
buf: CursorMut::new(header_array.as_mut_slice()),
}
}
}
/// A constant that defines the fixed byte length of the EtherFrameDot3 protocol header.
pub const ETHERFRAMEDOT3_HEADER_LEN: usize = 14;
/// A fixed EtherFrameDot3 header.
pub const ETHERFRAMEDOT3_HEADER_TEMPLATE: [u8; 14] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
];
#[derive(Debug, Clone, Copy)]
pub struct EtherFrameDot3<T> {
buf: T,
}
impl<T: Buf> EtherFrameDot3<T> {
#[inline]
pub fn parse_unchecked(buf: T) -> Self {
Self { buf }
}
#[inline]
pub fn buf(&self) -> &T {
&self.buf
}
#[inline]
pub fn release(self) -> T {
self.buf
}
#[inline]
pub fn parse(buf: T) -> Result<Self, T> {
let chunk_len = buf.chunk().len();
if chunk_len < 14 {
return Err(buf);
}
let container = Self { buf };
if (container.payload_len() as usize) + 14 > container.buf.remaining() {
return Err(container.buf);
}
Ok(container)
}
#[inline]
pub fn fix_header_slice(&self) -> &[u8] {
&self.buf.chunk()[0..14]
}
#[inline]
pub fn dst_addr(&self) -> EtherAddr {
EtherAddr::from_bytes(&self.buf.chunk()[0..6])
}
#[inline]
pub fn src_addr(&self) -> EtherAddr {
EtherAddr::from_bytes(&self.buf.chunk()[6..12])
}
#[inline]
pub fn payload_len(&self) -> u16 {
(u16::from_be_bytes((&self.buf.chunk()[12..14]).try_into().unwrap()))
}
}
impl<T: PktBuf> EtherFrameDot3<T> {
#[inline]
pub fn payload(self) -> T {
assert!(14 + self.payload_len() as usize <= self.buf.remaining());
let trim_size = self.buf.remaining() - (14 + self.payload_len() as usize);
let mut buf = self.buf;
if trim_size > 0 {
buf.trim_off(trim_size);
}
buf.advance(14);
buf
}
}
impl<T: PktBufMut> EtherFrameDot3<T> {
#[inline]
pub fn prepend_header<'a>(mut buf: T, header: &'a [u8; 14]) -> Self {
assert!(buf.chunk_headroom() >= 14);
let payload_len = buf.remaining();
assert!(payload_len <= 65535);
buf.move_back(14);
(&mut buf.chunk_mut()[0..14]).copy_from_slice(&header.as_ref()[..]);
let mut container = Self { buf };
container.set_payload_len(payload_len as u16);
container
}
#[inline]
pub fn set_dst_addr(&mut self, value: EtherAddr) {
(&mut self.buf.chunk_mut()[0..6]).copy_from_slice(value.as_bytes());
}
#[inline]
pub fn set_src_addr(&mut self, value: EtherAddr) {
(&mut self.buf.chunk_mut()[6..12]).copy_from_slice(value.as_bytes());
}
#[inline]
pub fn set_payload_len(&mut self, value: u16) {
(&mut self.buf.chunk_mut()[12..14]).copy_from_slice(&(value).to_be_bytes());
}
}
impl<'a> EtherFrameDot3<Cursor<'a>> {
#[inline]
pub fn parse_from_cursor(buf: Cursor<'a>) -> Result<Self, Cursor<'a>> {
let remaining_len = buf.chunk().len();
if remaining_len < 14 {
return Err(buf);
}
let container = Self { buf };
if (container.payload_len() as usize) + 14 > remaining_len {
return Err(container.buf);
}
Ok(container)
}
#[inline]
pub fn payload_as_cursor(&self) -> Cursor<'_> {
let payload_len = self.payload_len() as usize;
Cursor::new(&self.buf.chunk()[14..(14 + payload_len)])
}
#[inline]
pub fn from_header_array(header_array: &'a [u8; 14]) -> Self {
Self {
buf: Cursor::new(header_array.as_slice()),
}
}
}
impl<'a> EtherFrameDot3<CursorMut<'a>> {
#[inline]
pub fn parse_from_cursor_mut(buf: CursorMut<'a>) -> Result<Self, CursorMut<'a>> {
let remaining_len = buf.chunk().len();
if remaining_len < 14 {
return Err(buf);
}
let container = Self { buf };
if (container.payload_len() as usize) + 14 > remaining_len {
return Err(container.buf);
}
Ok(container)
}
#[inline]
pub fn payload_as_cursor_mut(&mut self) -> CursorMut<'_> {
let payload_len = self.payload_len() as usize;
CursorMut::new(&mut self.buf.chunk_mut()[14..(14 + payload_len)])
}
#[inline]
pub fn from_header_array_mut(header_array: &'a mut [u8; 14]) -> Self {
Self {
buf: CursorMut::new(header_array.as_mut_slice()),
}
}
}
#[derive(Debug)]
pub enum EtherFrameParser<T> {
EtherFrame_(EtherFrame<T>),
EtherFrameDot3_(EtherFrameDot3<T>),
}
impl<T: Buf> EtherFrameParser<T> {
pub fn group_parse(buf: T) -> Result<Self, T> {
if buf.chunk().len() < 14 {
return Err(buf);
}
let cond_value0 = u16::from_be_bytes((&buf.chunk()[12..14]).try_into().unwrap());
match cond_value0 {
1536..=65535 => EtherFrame::parse(buf).map(|pkt| EtherFrameParser::EtherFrame_(pkt)),
..=1500 => EtherFrameDot3::parse(buf).map(|pkt| EtherFrameParser::EtherFrameDot3_(pkt)),
_ => Err(buf),
}
}
}