Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{getter_be, setter_be};
/// The ARP header is typically found after the Ethernet header and is used to
/// map a network protocol address (like an IPv4 address) to a hardware
/// address (like a MAC address).
#[repr(C)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone, Default)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct ArpHdr {
Expand Down
2 changes: 1 addition & 1 deletion src/geneve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{getter_be, setter_be};
/// Represents a Geneve (Generic Network Virtualization Encapsulation) header, according to RFC 8926.
/// Geneve is an encapsulation protocol designed for network virtualization.
///
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone, Default)]

pub struct GeneveHdr {
Expand Down
24 changes: 12 additions & 12 deletions src/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum IcmpError {
///
/// The `data` field contains type-specific data such as echo identifiers/sequence numbers,
/// redirect gateway addresses, or pointers to errors in received packets.
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpHdr {
Expand Down Expand Up @@ -442,7 +442,7 @@ impl IcmpHdr {
/// - `traceroute`: Used for Traceroute messages (Type 30) to hold ID number
/// - `photuris`: Used for PHOTURIS security messages (Type 40) to hold SPI and error pointer
/// - `reserved`: Generic 4-byte field for types not covered by other variants
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub union IcmpHdrUn {
Expand Down Expand Up @@ -479,7 +479,7 @@ impl core::fmt::Debug for IcmpHdrUn {
/// - 18: Address Mask Reply (deprecated)
/// - 37: Domain Name Request (deprecated)
/// - 38: Domain Name Reply (deprecated)
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpEcho {
Expand Down Expand Up @@ -540,7 +540,7 @@ impl IcmpEcho {
/// For ICMP Type 3 "Destination Unreachable" Message (RFC 792) with support for PMTUD (RFC 1191)
/// Contains 2 unused bytes followed by a Next-Hop MTU field indicating the maximum transmission unit
/// of the next-hop network on which fragmentation is required.
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpDstUnreachable {
Expand Down Expand Up @@ -569,7 +569,7 @@ impl IcmpDstUnreachable {
/// For ICMP Type 12 "Parameter Problem" Message (RFC 792)
/// Contains a pointer to the byte in the original datagram that caused the error
/// and 3 bytes of unused padding to make the field a total of 4 bytes.
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpParamProblem {
Expand All @@ -581,7 +581,7 @@ pub struct IcmpParamProblem {
/// Contains 2 "Reserved" bytes followed by the Security Parameters Index used
/// for a security association between two peers. Also includes a 2-byte pointer
/// field indicating where in the message the error was detected.
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpHdrPhoturis {
Expand Down Expand Up @@ -615,7 +615,7 @@ impl IcmpHdrPhoturis {
/// Contains a 16-bit ID Number field used by the source to match responses to outgoing requests
/// followed by 2 unused bytes to make a total of 4 bytes. The ID Number helps match Reply messages
/// (type 31) to their corresponding Requests.
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpTraceroute {
Expand Down Expand Up @@ -720,7 +720,7 @@ impl IcmpTraceroute {
/// Ok(0)
/// }
/// ```
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpTimestampMsgPart {
Expand Down Expand Up @@ -828,7 +828,7 @@ impl IcmpTimestampMsgPart {
/// Ok(0)
/// }
/// ```
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpTracerouteMsgPart {
Expand Down Expand Up @@ -917,7 +917,7 @@ impl IcmpTracerouteMsgPart {
///
/// The `data` field contains type-specific data such as echo identifiers/sequence numbers,
/// MTU values, or pointers to errors in received packets.
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpV6Hdr {
Expand All @@ -933,7 +933,7 @@ pub struct IcmpV6Hdr {
/// - `packet_too_big_mtu`: Used in Packet Too Big messages (Type 2) to indicate next-hop MTU
/// - `param_problem_pointer`: Used in Parameter Problem messages (Type 4) to point to error location
/// - `reserved`: Generic 4-byte field for unused/reserved data in other message types
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub union IcmpV6HdrUn {
Expand Down Expand Up @@ -978,7 +978,7 @@ impl core::fmt::Debug for IcmpV6HdrUn {
}
}

#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct IcmpV6Redirect {
Expand Down
4 changes: 2 additions & 2 deletions src/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum IpHdr {
}

/// IPv4 header, which is present after the Ethernet header.
#[repr(C)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct Ipv4Hdr {
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Ipv4Hdr {
}

/// IPv6 header, which is present after the Ethernet header.
#[repr(C)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct Ipv6Hdr {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ pub mod vxlan;
/// # Safety
///
/// Caller needs to ensure that the provided field name is in bounds of the struct.
/// Caller needs to ensure that the targeted field is aligned
#[cfg(target_endian = "big")]
#[macro_export]
macro_rules! getter_be {
($self:expr, $field:ident, $ty:ty) => {
::core::ptr::read_unaligned(
::core::ptr::read(
(($self as *const Self as usize) + ::memoffset::offset_of!(Self, $field)) as *const $ty,
)
};
Expand All @@ -36,7 +37,7 @@ macro_rules! getter_be {
#[macro_export]
macro_rules! getter_be {
($self:expr, $field:ident, $ty:ty) => {
::core::ptr::read_unaligned(
::core::ptr::read(
(($self as *const Self as usize) + ::memoffset::offset_of!(Self, $field)) as *const $ty,
)
.swap_bytes()
Expand Down
2 changes: 1 addition & 1 deletion src/llc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::mem;

/// Represents Logical Link Control according to ISO/IEC 8802-2 Definition
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct LlcHdr {
Expand Down
2 changes: 1 addition & 1 deletion src/mpls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::mem;
/// https://www.rfc-editor.org/rfc/rfc3032.html.
/// This header format applies to all MPLS messages.
/// 20 bits for Label - 3 for TC - 1 for S - 8 for TTL
#[repr(C, packed)]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct Mpls {
Expand Down
2 changes: 1 addition & 1 deletion src/sctp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::mem;

#[repr(C)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct SctpHdr {
Expand Down
2 changes: 1 addition & 1 deletion src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::bitfield::BitfieldUnit;
pub const TCP_HDR_LEN: usize = mem::size_of::<TcpHdr>();

/// TCP header, which is present after the IP header.
#[repr(C)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct TcpHdr {
Expand Down
2 changes: 1 addition & 1 deletion src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{getter_be, setter_be};
/// udp_header.set_len(28); // 8 bytes header + 20 bytes payload
/// udp_header.set_checksum(0); // Checksum calculation would be done separately
/// ```
#[repr(C)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct UdpHdr {
Expand Down
2 changes: 1 addition & 1 deletion src/vxlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::mem;
/// Uses a 24-bit VXLAN Network Identifier (VNI) for traffic segregation.
/// Header length: 8 bytes.
/// Reference: RFC 7348.
#[repr(C, packed)]
#[repr(C, align(4))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct VxlanHdr {
Expand Down