Skip to content

Commit ace6da1

Browse files
daviessmJulianSchmid
authored andcommitted
Add serde derive macros where applicable
1 parent f63c91d commit ace6da1

17 files changed

Lines changed: 60 additions & 0 deletions

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exclude = [
2020

2121
[dependencies]
2222
arrayvec = "0.7.2"
23+
serde = { version = "1.0", optional = true, features = ["derive"] }
2324

2425
[dev-dependencies]
2526
assert_matches = "1.5.0"

src/internet/ip.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::super::*;
33
///Internet protocol headers version 4 & 6
44
#[derive(Clone, Debug, Eq, PartialEq)]
55
#[allow(clippy::large_enum_variant)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67
pub enum IpHeader {
78
Version4(Ipv4Header, Ipv4Extensions),
89
Version6(Ipv6Header, Ipv6Extensions)

src/internet/ip_authentication.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub type IPv6AuthenticationHeader = IpAuthenticationHeader;
1212

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

@@ -326,3 +328,9 @@ impl<'a> IpAuthenticationHeaderSlice<'a> {
326328
).unwrap()
327329
}
328330
}
331+
332+
#[feature("serde")]
333+
/// Used to create an empty buffer when deserializing using `serde`
334+
fn default_raw_icv_buffer() -> [u8;0xfe*4] {
335+
[0;0xfe*4]
336+
}

src/internet/ipv4.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::slice::from_raw_parts;
66

77
/// IPv4 header without options.
88
#[derive(Clone)]
9+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
910
pub struct Ipv4Header {
1011
pub differentiated_services_code_point: u8,
1112
pub explicit_congestion_notification: u8,
@@ -28,6 +29,7 @@ pub struct Ipv4Header {
2829
pub destination: [u8;4],
2930
/// Length of the options in the options_buffer in bytes.
3031
options_len: u8,
32+
#[cfg_attr(feature = "serde", serde(skip), serde(default = "default_options_buffer"))]
3133
options_buffer: [u8;40]
3234
}
3335

@@ -729,3 +731,9 @@ impl<'a> Ipv4HeaderSlice<'a> {
729731
}
730732
}
731733
}
734+
735+
#[feature("serde")]
736+
/// Used to create an empty buffer when deserializing using `serde`
737+
fn default_options_buffer() -> [u8;40] {
738+
[0;40]
739+
}

src/internet/ipv4_extensions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::super::*;
88
/// Currently not supported:
99
/// - Encapsulating Security Payload Header (ESP)
1010
#[derive(Clone, Debug, Eq, PartialEq, Default)]
11+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1112
pub struct Ipv4Extensions {
1213
pub auth: Option<IpAuthenticationHeader>,
1314
}

src/internet/ipv6.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::slice::from_raw_parts;
55

66
///IPv6 header according to rfc8200.
77
#[derive(Clone, Debug, Eq, PartialEq, Default)]
8+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
89
pub struct Ipv6Header {
910
pub traffic_class: u8,
1011
///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.

src/internet/ipv6_extensions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::slice::from_raw_parts;
1919
/// * IP Mobility
2020
/// * Site Multihoming by IPv6 Intermediation (SHIM6)
2121
#[derive(Clone, Debug, Eq, PartialEq, Default)]
22+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2223
pub struct Ipv6Extensions {
2324
pub hop_by_hop_options: Option<Ipv6RawExtensionHeader>,
2425
pub destination_options: Option<Ipv6RawExtensionHeader>,
@@ -656,6 +657,7 @@ impl Ipv6Extensions {
656657
/// In case a route header is present it is also possible
657658
/// to attach a "final destination" header.
658659
#[derive(Clone, Debug, Eq, PartialEq)]
660+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
659661
pub struct Ipv6RoutingExtensions {
660662
pub routing: Ipv6RawExtensionHeader,
661663
pub final_destination_options: Option<Ipv6RawExtensionHeader>

src/internet/ipv6_fragment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::slice::from_raw_parts;
44

55
/// IPv6 fragment header.
66
#[derive(Clone, Debug, Eq, PartialEq)]
7+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
78
pub struct Ipv6FragmentHeader {
89
/// IP protocol number specifying the next header or transport layer protocol.
910
///

src/internet/ipv6_raw_extension.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub const IPV6_MAX_NUM_HEADER_EXTENSIONS: usize = 12;
2121
/// * Host Identity Protocol
2222
/// * Shim6 Protocol
2323
#[derive(Clone)]
24+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2425
pub struct Ipv6RawExtensionHeader {
2526
/// IP protocol number specifying the next header or transport layer protocol.
2627
///
@@ -29,6 +30,7 @@ pub struct Ipv6RawExtensionHeader {
2930
/// Length of the extension header in 8 octets (minus the first 8 octets).
3031
header_length: u8,
3132
//// The data contained in the extension header (excluding next_header & hdr length).
33+
#[cfg_attr(feature = "serde", serde(skip), serde(default = "default_payload_buffer"))]
3234
payload_buffer: [u8;0xff * 8 + 6],
3335
}
3436

@@ -288,3 +290,9 @@ impl<'a> Ipv6RawExtensionHeaderSlice<'a> {
288290
).unwrap()
289291
}
290292
}
293+
294+
#[feature("serde")]
295+
/// Used to create an empty buffer when deserializing using `serde`
296+
fn default_payload_buffer() -> [u8;0xff * 8 + 6] {
297+
[0;0xff * 8 + 6]
298+
}

src/link/ethernet.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub mod ether_type {
5858

5959
///Ethernet II header.
6060
#[derive(Clone, Debug, Eq, PartialEq, Default)]
61+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6162
pub struct Ethernet2Header {
6263
pub source: [u8;6],
6364
pub destination: [u8;6],

0 commit comments

Comments
 (0)