Skip to content

Commit

Permalink
Add serde derive macros where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
daviessm authored and JulianSchmid committed Dec 18, 2022
1 parent f63c91d commit ace6da1
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exclude = [

[dependencies]
arrayvec = "0.7.2"
serde = { version = "1.0", optional = true, features = ["derive"] }

[dev-dependencies]
assert_matches = "1.5.0"
Expand Down
1 change: 1 addition & 0 deletions src/internet/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::super::*;
///Internet protocol headers version 4 & 6
#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(clippy::large_enum_variant)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum IpHeader {
Version4(Ipv4Header, Ipv4Extensions),
Version6(Ipv6Header, Ipv6Extensions)
Expand Down
8 changes: 8 additions & 0 deletions src/internet/ip_authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub type IPv6AuthenticationHeader = IpAuthenticationHeader;

/// IP Authentication Header (rfc4302)
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct IpAuthenticationHeader {
/// IP protocol number specifying the next header or transport layer protocol.
///
Expand All @@ -27,6 +28,7 @@ pub struct IpAuthenticationHeader {
raw_icv_len: u8,
/// Buffer containing the "Encoded Integrity Check Value-ICV" (variable).
/// The length of the used data can be set via the `variable` (must be a multiple of 4 bytes).
#[cfg_attr(feature = "serde", serde(skip), serde(default = "default_raw_icv_buffer"))]
raw_icv_buffer: [u8;0xfe*4],
}

Expand Down Expand Up @@ -326,3 +328,9 @@ impl<'a> IpAuthenticationHeaderSlice<'a> {
).unwrap()
}
}

#[feature("serde")]
/// Used to create an empty buffer when deserializing using `serde`
fn default_raw_icv_buffer() -> [u8;0xfe*4] {
[0;0xfe*4]
}
8 changes: 8 additions & 0 deletions src/internet/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::slice::from_raw_parts;

/// IPv4 header without options.
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ipv4Header {
pub differentiated_services_code_point: u8,
pub explicit_congestion_notification: u8,
Expand All @@ -28,6 +29,7 @@ pub struct Ipv4Header {
pub destination: [u8;4],
/// Length of the options in the options_buffer in bytes.
options_len: u8,
#[cfg_attr(feature = "serde", serde(skip), serde(default = "default_options_buffer"))]
options_buffer: [u8;40]
}

Expand Down Expand Up @@ -729,3 +731,9 @@ impl<'a> Ipv4HeaderSlice<'a> {
}
}
}

#[feature("serde")]
/// Used to create an empty buffer when deserializing using `serde`
fn default_options_buffer() -> [u8;40] {
[0;40]
}
1 change: 1 addition & 0 deletions src/internet/ipv4_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::super::*;
/// Currently not supported:
/// - Encapsulating Security Payload Header (ESP)
#[derive(Clone, Debug, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ipv4Extensions {
pub auth: Option<IpAuthenticationHeader>,
}
Expand Down
1 change: 1 addition & 0 deletions src/internet/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::slice::from_raw_parts;

///IPv6 header according to rfc8200.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ipv6Header {
pub traffic_class: u8,
///If non 0 serves as a hint to router and switches with multiple outbound paths that these packets should stay on the same path, so that they will not be reordered.
Expand Down
2 changes: 2 additions & 0 deletions src/internet/ipv6_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::slice::from_raw_parts;
/// * IP Mobility
/// * Site Multihoming by IPv6 Intermediation (SHIM6)
#[derive(Clone, Debug, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ipv6Extensions {
pub hop_by_hop_options: Option<Ipv6RawExtensionHeader>,
pub destination_options: Option<Ipv6RawExtensionHeader>,
Expand Down Expand Up @@ -656,6 +657,7 @@ impl Ipv6Extensions {
/// In case a route header is present it is also possible
/// to attach a "final destination" header.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ipv6RoutingExtensions {
pub routing: Ipv6RawExtensionHeader,
pub final_destination_options: Option<Ipv6RawExtensionHeader>
Expand Down
1 change: 1 addition & 0 deletions src/internet/ipv6_fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::slice::from_raw_parts;

/// IPv6 fragment header.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ipv6FragmentHeader {
/// IP protocol number specifying the next header or transport layer protocol.
///
Expand Down
8 changes: 8 additions & 0 deletions src/internet/ipv6_raw_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub const IPV6_MAX_NUM_HEADER_EXTENSIONS: usize = 12;
/// * Host Identity Protocol
/// * Shim6 Protocol
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ipv6RawExtensionHeader {
/// IP protocol number specifying the next header or transport layer protocol.
///
Expand All @@ -29,6 +30,7 @@ pub struct Ipv6RawExtensionHeader {
/// Length of the extension header in 8 octets (minus the first 8 octets).
header_length: u8,
//// The data contained in the extension header (excluding next_header & hdr length).
#[cfg_attr(feature = "serde", serde(skip), serde(default = "default_payload_buffer"))]
payload_buffer: [u8;0xff * 8 + 6],
}

Expand Down Expand Up @@ -288,3 +290,9 @@ impl<'a> Ipv6RawExtensionHeaderSlice<'a> {
).unwrap()
}
}

#[feature("serde")]
/// Used to create an empty buffer when deserializing using `serde`
fn default_payload_buffer() -> [u8;0xff * 8 + 6] {
[0;0xff * 8 + 6]
}
1 change: 1 addition & 0 deletions src/link/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub mod ether_type {

///Ethernet II header.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ethernet2Header {
pub source: [u8;6],
pub destination: [u8;6],
Expand Down
3 changes: 3 additions & 0 deletions src/link/vlan_tagging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::slice::from_raw_parts;

/// IEEE 802.1Q VLAN Tagging Header (can be single or double tagged).
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum VlanHeader {
/// IEEE 802.1Q VLAN Tagging Header
Single(SingleVlanHeader),
Expand Down Expand Up @@ -63,6 +64,7 @@ impl<'a> VlanSlice<'a> {

/// IEEE 802.1Q VLAN Tagging Header
#[derive(Clone, Debug, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SingleVlanHeader {
/// A 3 bit number which refers to the IEEE 802.1p class of service and maps to the frame priority level.
pub priority_code_point: u8,
Expand Down Expand Up @@ -178,6 +180,7 @@ impl SingleVlanHeader {

/// IEEE 802.1Q double VLAN Tagging Header
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DoubleVlanHeader {
/// The outer vlan tagging header
pub outer: SingleVlanHeader,
Expand Down
1 change: 1 addition & 0 deletions src/transport/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/// originating Echo Requests and receiving Echo Replies, for diagnostic
/// purposes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct IcmpEchoHeader {
/// An identifier to aid in matching Echo Replies to Echo Requests. May be zero.
pub id: u16,
Expand Down
8 changes: 8 additions & 0 deletions src/transport/icmpv4_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub mod icmpv4 {
/// Codes 0, 1, 4, and 5 may be received from a gateway. Codes 2 and
/// 3 may be received from a host.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DestUnreachableHeader {
/// Network unreachable error.
Network,
Expand Down Expand Up @@ -231,6 +232,7 @@ pub mod icmpv4 {

/// Code value in an ICMPv4 Redirect message.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum RedirectCode {
/// Redirect Datagram for the Network (or subnet)
RedirectForNetwork = 0,
Expand Down Expand Up @@ -266,6 +268,7 @@ pub mod icmpv4 {
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RedirectHeader {
pub code: RedirectCode,
pub gateway_internet_address: [u8; 4],
Expand All @@ -279,6 +282,7 @@ pub mod icmpv4 {

/// Code values for ICMPv4 time exceeded message.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TimeExceededCode {
/// Time-to-live exceeded in transit.
TtlExceededInTransit = 0,
Expand Down Expand Up @@ -309,6 +313,7 @@ pub mod icmpv4 {

/// A ICMPv4 timestamp or timestamp response message.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TimestampMessage {
pub id: u16,
pub seq: u16,
Expand Down Expand Up @@ -345,6 +350,7 @@ pub mod icmpv4 {
/// The header of an ICMPv4 Parameter Problems (contents up to
/// the offending ip header).
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ParameterProblemHeader {
/// Identifies the octet where an error was detected.
///
Expand Down Expand Up @@ -378,6 +384,7 @@ use icmpv4::*;

/// Starting contents of an ICMPv4 packet without the checksum.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Icmpv4Type {
/// In case of an unknown ICMP type and code combination is received the
/// header elements are stored raw in this enum value. The `Unknown` value can
Expand Down Expand Up @@ -724,6 +731,7 @@ impl Icmpv4Type {
/// and code. But usually the static sized elements are part
/// of the header.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Icmpv4Header {
/// Type & type specific values & code.
pub icmp_type: Icmpv4Type,
Expand Down
6 changes: 6 additions & 0 deletions src/transport/icmpv6_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub mod icmpv6 {
/// than congestion. (An ICMPv6 message MUST NOT be generated if a
/// packet is dropped due to congestion.)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DestUnreachableCode {
/// No route to destination
NoRoute = 0,
Expand Down Expand Up @@ -179,6 +180,7 @@ pub mod icmpv6 {

/// Code values for ICMPv6 time exceeded message.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TimeExceededCode {
/// "hop limit exceeded in transit"
HopLimitExceeded = 0,
Expand Down Expand Up @@ -247,6 +249,7 @@ pub mod icmpv6 {
///
/// Source: <https://www.iana.org/assignments/icmpv6-parameters/icmpv6-parameters.xhtml#icmpv6-parameters-codes-5>
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ParameterProblemCode {
/// Erroneous header field encountered (from [RFC 4443](https://tools.ietf.org/html/rfc4443))
ErroneousHeaderField = 0,
Expand Down Expand Up @@ -308,6 +311,7 @@ pub mod icmpv6 {

/// ICMPv6 parameter problem header.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ParameterProblemHeader {
/// The code can offer additional informations about what kind of parameter
/// problem caused the error.
Expand Down Expand Up @@ -407,6 +411,7 @@ use icmpv6::*;
/// # }
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Icmpv6Type {
/// In case of an unknown icmp type is received the header elements of
/// the first 8 bytes/octets are stored raw in this enum value.
Expand Down Expand Up @@ -830,6 +835,7 @@ impl Icmpv6Type {

/// The statically sized data at the start of an ICMPv6 packet (at least the first 8 bytes of an ICMPv6 packet).
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Icmpv6Header {
/// Type & type specific values & code.
pub icmp_type: Icmpv6Type,
Expand Down
1 change: 1 addition & 0 deletions src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::io;

///The possible headers on the transport layer
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TransportHeader {
Udp(udp::UdpHeader),
Tcp(tcp::TcpHeader),
Expand Down
8 changes: 8 additions & 0 deletions src/transport/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub const TCP_MAXIMUM_DATA_OFFSET: u8 = 0xf;
///
///Field descriptions copied from RFC 793 page 15++
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TcpHeader {
///The source port number.
pub source_port: u16,
Expand Down Expand Up @@ -70,9 +71,16 @@ pub struct TcpHeader {
///the URG control bit set.
pub urgent_pointer: u16,
///Buffer containing the options of the header (note that the data_offset defines the actual length). Use the options() method if you want to get a slice that has the actual length of the options.
#[cfg_attr(feature = "serde", serde(skip), serde(default = "default_options_buffer"))]
options_buffer: [u8;40]
}

#[feature("serde")]
/// Used to create an empty buffer when deserializing using `serde`
fn default_options_buffer() -> [u8;40] {
[0;40]
}

impl TcpHeader {

///Creates a TcpHeader with the given values and the rest initialized with default values.
Expand Down
1 change: 1 addition & 0 deletions src/transport/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::slice::from_raw_parts;

///Udp header according to rfc768.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct UdpHeader {
///Source port of the packet (optional).
pub source_port: u16,
Expand Down

0 comments on commit ace6da1

Please sign in to comment.