Skip to content

Commit 7a0c6d3

Browse files
add datalink trait for ipv4
1 parent 2420db1 commit 7a0c6d3

File tree

13 files changed

+255
-229
lines changed

13 files changed

+255
-229
lines changed

core/src/packets/arp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ impl<E: Datalink, H: HardwareAddr, P: ProtocolAddr> Arp<E, H, P> {
114114
}
115115

116116
/// Returns the protocol type.
117-
///
118-
/// [IANA] assigned Protocol type numbers share the same space as
117+
///
118+
/// [IANA] assigned Protocol type numbers share the same space as
119119
/// [`EtherTypes`].
120-
///
120+
///
121121
/// [IANA]: https://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml#arp-parameters-3
122122
/// [`EtherTypes`]: crate::packets::ethernet::EtherTypes
123123
#[inline]

core/src/packets/icmp/v4/echo_reply.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
use crate::packets::icmp::v4::{Icmpv4, Icmpv4Message, Icmpv4Packet, Icmpv4Type, Icmpv4Types};
20+
use crate::packets::ip::v4::Ipv4Packet;
2021
use crate::packets::types::u16be;
2122
use crate::packets::{Internal, Packet, SizeOf};
2223
use anyhow::Result;
@@ -46,12 +47,12 @@ use std::ptr::NonNull;
4647
///
4748
/// [IETF RFC 792]: https://tools.ietf.org/html/rfc792
4849
#[derive(Icmpv4Packet)]
49-
pub struct EchoReply {
50-
icmp: Icmpv4,
50+
pub struct EchoReply<E: Ipv4Packet> {
51+
icmp: Icmpv4<E>,
5152
body: NonNull<EchoReplyBody>,
5253
}
5354

54-
impl EchoReply {
55+
impl<E: Ipv4Packet> EchoReply<E> {
5556
#[inline]
5657
fn body(&self) -> &EchoReplyBody {
5758
unsafe { self.body.as_ref() }
@@ -127,7 +128,7 @@ impl EchoReply {
127128
}
128129
}
129130

130-
impl fmt::Debug for EchoReply {
131+
impl<E: Ipv4Packet> fmt::Debug for EchoReply<E> {
131132
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132133
f.debug_struct("EchoReply")
133134
.field("type", &format!("{}", self.msg_type()))
@@ -142,24 +143,26 @@ impl fmt::Debug for EchoReply {
142143
}
143144
}
144145

145-
impl Icmpv4Message for EchoReply {
146+
impl<E: Ipv4Packet> Icmpv4Message for EchoReply<E> {
147+
type Envelope = E;
148+
146149
#[inline]
147150
fn msg_type() -> Icmpv4Type {
148151
Icmpv4Types::EchoReply
149152
}
150153

151154
#[inline]
152-
fn icmp(&self) -> &Icmpv4 {
155+
fn icmp(&self) -> &Icmpv4<Self::Envelope> {
153156
&self.icmp
154157
}
155158

156159
#[inline]
157-
fn icmp_mut(&mut self) -> &mut Icmpv4 {
160+
fn icmp_mut(&mut self) -> &mut Icmpv4<Self::Envelope> {
158161
&mut self.icmp
159162
}
160163

161164
#[inline]
162-
fn into_icmp(self) -> Icmpv4 {
165+
fn into_icmp(self) -> Icmpv4<Self::Envelope> {
163166
self.icmp
164167
}
165168

@@ -178,7 +181,7 @@ impl Icmpv4Message for EchoReply {
178181
/// Returns an error if the payload does not have sufficient data for
179182
/// the echo reply message body.
180183
#[inline]
181-
fn try_parse(icmp: Icmpv4, _internal: Internal) -> Result<Self> {
184+
fn try_parse(icmp: Icmpv4<Self::Envelope>, _internal: Internal) -> Result<Self> {
182185
let mbuf = icmp.mbuf();
183186
let offset = icmp.payload_offset();
184187
let body = mbuf.read_data(offset)?;
@@ -193,7 +196,7 @@ impl Icmpv4Message for EchoReply {
193196
///
194197
/// Returns an error if the buffer does not have enough free space.
195198
#[inline]
196-
fn try_push(mut icmp: Icmpv4, _internal: Internal) -> Result<Self> {
199+
fn try_push(mut icmp: Icmpv4<Self::Envelope>, _internal: Internal) -> Result<Self> {
197200
let offset = icmp.payload_offset();
198201
let mbuf = icmp.mbuf_mut();
199202

@@ -219,7 +222,7 @@ struct EchoReplyBody {
219222
mod tests {
220223
use super::*;
221224
use crate::packets::ethernet::Ethernet;
222-
use crate::packets::ip::v4::Ipv4;
225+
use crate::packets::ip::v4::Ip4;
223226
use crate::packets::Mbuf;
224227

225228
#[test]
@@ -231,8 +234,8 @@ mod tests {
231234
fn push_and_set_echo_reply() {
232235
let packet = Mbuf::new().unwrap();
233236
let ethernet = packet.push::<Ethernet>().unwrap();
234-
let ipv4 = ethernet.push::<Ipv4>().unwrap();
235-
let mut echo = ipv4.push::<EchoReply>().unwrap();
237+
let ip4 = ethernet.push::<Ip4>().unwrap();
238+
let mut echo = ip4.push::<EchoReply<Ip4>>().unwrap();
236239

237240
assert_eq!(4, echo.header_len());
238241
assert_eq!(EchoReplyBody::size_of(), echo.payload_len());

core/src/packets/icmp/v4/echo_request.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
use crate::packets::icmp::v4::{Icmpv4, Icmpv4Message, Icmpv4Packet, Icmpv4Type, Icmpv4Types};
20+
use crate::packets::ip::v4::Ipv4Packet;
2021
use crate::packets::types::u16be;
2122
use crate::packets::{Internal, Packet, SizeOf};
2223
use anyhow::Result;
@@ -47,12 +48,12 @@ use std::ptr::NonNull;
4748
///
4849
/// [IETF RFC 792]: https://tools.ietf.org/html/rfc792
4950
#[derive(Icmpv4Packet)]
50-
pub struct EchoRequest {
51-
icmp: Icmpv4,
51+
pub struct EchoRequest<E: Ipv4Packet> {
52+
icmp: Icmpv4<E>,
5253
body: NonNull<EchoRequestBody>,
5354
}
5455

55-
impl EchoRequest {
56+
impl<E: Ipv4Packet> EchoRequest<E> {
5657
#[inline]
5758
fn body(&self) -> &EchoRequestBody {
5859
unsafe { self.body.as_ref() }
@@ -128,7 +129,7 @@ impl EchoRequest {
128129
}
129130
}
130131

131-
impl fmt::Debug for EchoRequest {
132+
impl<E: Ipv4Packet> fmt::Debug for EchoRequest<E> {
132133
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133134
f.debug_struct("EchoRequest")
134135
.field("type", &format!("{}", self.msg_type()))
@@ -143,24 +144,26 @@ impl fmt::Debug for EchoRequest {
143144
}
144145
}
145146

146-
impl Icmpv4Message for EchoRequest {
147+
impl<E: Ipv4Packet> Icmpv4Message for EchoRequest<E> {
148+
type Envelope = E;
149+
147150
#[inline]
148151
fn msg_type() -> Icmpv4Type {
149152
Icmpv4Types::EchoRequest
150153
}
151154

152155
#[inline]
153-
fn icmp(&self) -> &Icmpv4 {
156+
fn icmp(&self) -> &Icmpv4<Self::Envelope> {
154157
&self.icmp
155158
}
156159

157160
#[inline]
158-
fn icmp_mut(&mut self) -> &mut Icmpv4 {
161+
fn icmp_mut(&mut self) -> &mut Icmpv4<Self::Envelope> {
159162
&mut self.icmp
160163
}
161164

162165
#[inline]
163-
fn into_icmp(self) -> Icmpv4 {
166+
fn into_icmp(self) -> Icmpv4<Self::Envelope> {
164167
self.icmp
165168
}
166169

@@ -179,7 +182,7 @@ impl Icmpv4Message for EchoRequest {
179182
/// Returns an error if the payload does not have sufficient data for
180183
/// the echo request message body.
181184
#[inline]
182-
fn try_parse(icmp: Icmpv4, _internal: Internal) -> Result<Self> {
185+
fn try_parse(icmp: Icmpv4<Self::Envelope>, _internal: Internal) -> Result<Self> {
183186
let mbuf = icmp.mbuf();
184187
let offset = icmp.payload_offset();
185188
let body = mbuf.read_data(offset)?;
@@ -194,7 +197,7 @@ impl Icmpv4Message for EchoRequest {
194197
///
195198
/// Returns an error if the buffer does not have enough free space.
196199
#[inline]
197-
fn try_push(mut icmp: Icmpv4, _internal: Internal) -> Result<Self> {
200+
fn try_push(mut icmp: Icmpv4<Self::Envelope>, _internal: Internal) -> Result<Self> {
198201
let offset = icmp.payload_offset();
199202
let mbuf = icmp.mbuf_mut();
200203

@@ -220,7 +223,7 @@ struct EchoRequestBody {
220223
mod tests {
221224
use super::*;
222225
use crate::packets::ethernet::Ethernet;
223-
use crate::packets::ip::v4::Ipv4;
226+
use crate::packets::ip::v4::Ip4;
224227
use crate::packets::Mbuf;
225228

226229
#[test]
@@ -232,8 +235,8 @@ mod tests {
232235
fn push_and_set_echo_request() {
233236
let packet = Mbuf::new().unwrap();
234237
let ethernet = packet.push::<Ethernet>().unwrap();
235-
let ipv4 = ethernet.push::<Ipv4>().unwrap();
236-
let mut echo = ipv4.push::<EchoRequest>().unwrap();
238+
let ip4 = ethernet.push::<Ip4>().unwrap();
239+
let mut echo = ip4.push::<EchoRequest<Ip4>>().unwrap();
237240

238241
assert_eq!(4, echo.header_len());
239242
assert_eq!(EchoRequestBody::size_of(), echo.payload_len());

0 commit comments

Comments
 (0)