-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: move UDP protocol structs into
udp.rs
Signed-off-by: Anhad Singh <[email protected]>
- Loading branch information
1 parent
a2e4f27
commit 1a3f153
Showing
2 changed files
with
55 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use byte_endian::BigEndian; | ||
use static_assertions::const_assert_eq; | ||
|
||
use crate::network::Ipv4; | ||
use crate::{Parsable, PointerExtension, Protocol, Stack, Stacked, StackingAnchor}; | ||
|
||
#[repr(C, packed)] | ||
pub struct Udp { | ||
src_port: BigEndian<u16>, | ||
dst_port: BigEndian<u16>, | ||
len: BigEndian<u16>, | ||
crc: BigEndian<u16>, | ||
} | ||
|
||
impl Udp { | ||
crate::impl_stack!(@getter src_port: BigEndian<u16> as u16, dst_port: BigEndian<u16> as u16); | ||
|
||
pub fn new(src_port: u16, dst_port: u16) -> Self { | ||
Self { | ||
src_port: src_port.into(), | ||
dst_port: dst_port.into(), | ||
len: 0.into(), | ||
crc: 0.into(), | ||
} | ||
} | ||
} | ||
|
||
const_assert_eq!(core::mem::size_of::<Udp>(), 8); | ||
|
||
unsafe impl StackingAnchor<Udp> for Udp {} | ||
unsafe impl<U: Protocol> StackingAnchor<Udp> for Stacked<U, Udp> {} | ||
|
||
impl<U: StackingAnchor<Ipv4>> Stack<U> for Udp { | ||
type Output = Stacked<U, Self>; | ||
|
||
fn stack(self, lhs: U) -> Self::Output { | ||
Self::Output { | ||
upper: lhs, | ||
lower: self, | ||
} | ||
} | ||
} | ||
|
||
crate::impl_stack!(@make Udp { | ||
fn write_stage2(&self, mem: NonNull<u8>, payload_len: usize) { | ||
use crate::checksum::{self, PseudoHeader}; | ||
|
||
let udp = unsafe { mem.cast::<Udp>().as_mut() }; | ||
let ipv4 = unsafe { mem.cast::<Ipv4>().sub(1).as_ref() }; | ||
let pseudo_header = PseudoHeader::new(ipv4); | ||
|
||
udp.len = (payload_len as u16).into(); | ||
udp.crc = checksum::make_combine(&[checksum::calculate(&pseudo_header), checksum::calculate_with_len(udp, payload_len)]); | ||
} | ||
}); |