Process the Icmpv4Repr in IcmpSocket::process
- Use the Icmpv4Repr in IcmpSocket::process instead of the raw payload. - Update the IcmpSocket tests.
This commit is contained in:
parent
26b8052ee9
commit
888b098dca
|
@ -460,7 +460,7 @@ impl<'b, 'c> InterfaceInner<'b, 'c> {
|
||||||
for mut icmp_socket in _sockets.iter_mut().filter_map(IcmpSocket::downcast) {
|
for mut icmp_socket in _sockets.iter_mut().filter_map(IcmpSocket::downcast) {
|
||||||
if !icmp_socket.accepts(&ip_repr, &icmp_repr, &checksum_caps) { continue }
|
if !icmp_socket.accepts(&ip_repr, &icmp_repr, &checksum_caps) { continue }
|
||||||
|
|
||||||
match icmp_socket.process(&ip_repr, ip_payload) {
|
match icmp_socket.process(&ip_repr, &icmp_repr, &checksum_caps) {
|
||||||
// The packet is valid and handled by socket.
|
// The packet is valid and handled by socket.
|
||||||
Ok(()) => handled_by_icmp_socket = true,
|
Ok(()) => handled_by_icmp_socket = true,
|
||||||
// The socket buffer is full.
|
// The socket buffer is full.
|
||||||
|
|
|
@ -313,12 +313,14 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn process(&mut self, ip_repr: &IpRepr, ip_payload: &[u8]) -> Result<()> {
|
pub(crate) fn process(&mut self, ip_repr: &IpRepr, icmp_repr: &Icmpv4Repr,
|
||||||
let packet_buf = self.rx_buffer.enqueue_one_with(|buf| buf.resize(ip_payload.len()))?;
|
cksum: &ChecksumCapabilities) -> Result<()> {
|
||||||
packet_buf.as_mut().copy_from_slice(ip_payload);
|
let packet_buf = self.rx_buffer.enqueue_one_with(|buf| buf.resize(icmp_repr.buffer_len()))?;
|
||||||
packet_buf.endpoint = ip_repr.src_addr();
|
packet_buf.endpoint = ip_repr.src_addr();
|
||||||
net_trace!("{}:{}: receiving {} octets",
|
net_trace!("{}:{}: receiving {} octets",
|
||||||
self.meta.handle, packet_buf.endpoint, packet_buf.size);
|
self.meta.handle, packet_buf.endpoint, packet_buf.size);
|
||||||
|
let mut packet = Icmpv4Packet::new(packet_buf.as_mut());
|
||||||
|
icmp_repr.emit(&mut packet, cksum);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -367,7 +369,7 @@ mod test {
|
||||||
fn buffer(packets: usize) -> SocketBuffer<'static, 'static> {
|
fn buffer(packets: usize) -> SocketBuffer<'static, 'static> {
|
||||||
let mut storage = vec![];
|
let mut storage = vec![];
|
||||||
for _ in 0..packets {
|
for _ in 0..packets {
|
||||||
storage.push(PacketBuffer::new(vec![0; 24]))
|
storage.push(PacketBuffer::new(vec![0; 46]))
|
||||||
}
|
}
|
||||||
SocketBuffer::new(storage)
|
SocketBuffer::new(storage)
|
||||||
}
|
}
|
||||||
|
@ -432,7 +434,7 @@ mod test {
|
||||||
Err(Error::Exhausted));
|
Err(Error::Exhausted));
|
||||||
|
|
||||||
// This buffer is too long
|
// This buffer is too long
|
||||||
assert_eq!(socket.send_slice(&[0xff; 25], REMOTE_IP), Err(Error::Truncated));
|
assert_eq!(socket.send_slice(&[0xff; 47], REMOTE_IP), Err(Error::Truncated));
|
||||||
assert!(socket.can_send());
|
assert!(socket.can_send());
|
||||||
|
|
||||||
let mut bytes = [0xff; 24];
|
let mut bytes = [0xff; 24];
|
||||||
|
@ -494,19 +496,20 @@ mod test {
|
||||||
|
|
||||||
let caps = DeviceCapabilities::default();
|
let caps = DeviceCapabilities::default();
|
||||||
|
|
||||||
let mut bytes = [0xff; 20];
|
let mut bytes = [0xff; 24];
|
||||||
let mut packet = Icmpv4Packet::new(&mut bytes);
|
let mut packet = Icmpv4Packet::new(&mut bytes);
|
||||||
ECHO_REPR.emit(&mut packet, &caps.checksum);
|
ECHO_REPR.emit(&mut packet, &caps.checksum);
|
||||||
let data = &packet.into_inner()[..];
|
let data = &packet.into_inner()[..];
|
||||||
|
|
||||||
assert!(socket.accepts(&REMOTE_IP_REPR, &ECHO_REPR, &caps.checksum));
|
assert!(socket.accepts(&REMOTE_IP_REPR, &ECHO_REPR, &caps.checksum));
|
||||||
assert_eq!(socket.process(&REMOTE_IP_REPR, &data[..]),
|
assert_eq!(socket.process(&REMOTE_IP_REPR, &ECHO_REPR, &caps.checksum),
|
||||||
Ok(()));
|
Ok(()));
|
||||||
assert!(socket.can_recv());
|
assert!(socket.can_recv());
|
||||||
|
|
||||||
assert!(socket.accepts(&REMOTE_IP_REPR, &ECHO_REPR, &caps.checksum));
|
assert!(socket.accepts(&REMOTE_IP_REPR, &ECHO_REPR, &caps.checksum));
|
||||||
assert_eq!(socket.process(&REMOTE_IP_REPR, &data[..]),
|
assert_eq!(socket.process(&REMOTE_IP_REPR, &ECHO_REPR, &caps.checksum),
|
||||||
Err(Error::Exhausted));
|
Err(Error::Exhausted));
|
||||||
|
|
||||||
assert_eq!(socket.recv(), Ok((&data[..], REMOTE_IP)));
|
assert_eq!(socket.recv(), Ok((&data[..], REMOTE_IP)));
|
||||||
assert!(!socket.can_recv());
|
assert!(!socket.can_recv());
|
||||||
}
|
}
|
||||||
|
@ -568,8 +571,14 @@ mod test {
|
||||||
// Ensure we can accept ICMP error response to the bound
|
// Ensure we can accept ICMP error response to the bound
|
||||||
// UDP port
|
// UDP port
|
||||||
assert!(socket.accepts(&ip_repr, &icmp_repr, &caps.checksum));
|
assert!(socket.accepts(&ip_repr, &icmp_repr, &caps.checksum));
|
||||||
assert_eq!(socket.process(&ip_repr, &data[..]),
|
assert_eq!(socket.process(&ip_repr, &icmp_repr, &caps.checksum),
|
||||||
Ok(()));
|
Ok(()));
|
||||||
assert!(socket.can_recv());
|
assert!(socket.can_recv());
|
||||||
|
|
||||||
|
let mut bytes = [0x00; 46];
|
||||||
|
let mut packet = Icmpv4Packet::new(&mut bytes[..]);
|
||||||
|
icmp_repr.emit(&mut packet, &caps.checksum);
|
||||||
|
assert_eq!(socket.recv(), Ok((&packet.into_inner()[..], REMOTE_IP)));
|
||||||
|
assert!(!socket.can_recv());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue