2017-10-30 08:19:34 +08:00
|
|
|
use core::cmp;
|
|
|
|
|
|
|
|
use {Error, Result};
|
|
|
|
use phy::{ChecksumCapabilities, DeviceCapabilities};
|
2017-11-22 11:50:09 +08:00
|
|
|
use socket::{Socket, SocketMeta, SocketHandle};
|
2018-02-22 20:23:35 +08:00
|
|
|
use storage::{PacketBuffer, PacketMetadata};
|
2018-02-11 00:32:41 +08:00
|
|
|
use time::Instant;
|
2017-10-30 08:19:34 +08:00
|
|
|
use wire::{IpAddress, IpEndpoint, IpProtocol, IpRepr};
|
2018-05-09 21:27:51 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "proto-ipv4")]
|
|
|
|
use wire::{Ipv4Address, Ipv4Repr, Icmpv4Packet, Icmpv4Repr};
|
|
|
|
#[cfg(feature = "proto-ipv6")]
|
|
|
|
use wire::{Ipv6Address, Ipv6Repr, Icmpv6Packet, Icmpv6Repr};
|
|
|
|
use wire::IcmpRepr;
|
2017-10-30 08:19:34 +08:00
|
|
|
use wire::{UdpPacket, UdpRepr};
|
|
|
|
|
|
|
|
/// Type of endpoint to bind the ICMP socket to. See [IcmpSocket::bind] for
|
|
|
|
/// more details.
|
|
|
|
///
|
|
|
|
/// [IcmpSocket::bind]: struct.IcmpSocket.html#method.bind
|
|
|
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
|
|
|
pub enum Endpoint {
|
|
|
|
Unspecified,
|
|
|
|
Ident(u16),
|
|
|
|
Udp(IpEndpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Endpoint {
|
|
|
|
pub fn is_specified(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Endpoint::Ident(_) => true,
|
|
|
|
Endpoint::Udp(endpoint) => endpoint.port != 0,
|
|
|
|
Endpoint::Unspecified => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Endpoint {
|
|
|
|
fn default() -> Endpoint { Endpoint::Unspecified }
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:23:35 +08:00
|
|
|
/// An ICMPv4 packet metadata.
|
|
|
|
pub type IcmpPacketMetadata = PacketMetadata<IpAddress>;
|
2017-10-30 08:19:34 +08:00
|
|
|
|
|
|
|
/// An ICMPv4 packet ring buffer.
|
2018-02-22 20:23:35 +08:00
|
|
|
pub type IcmpSocketBuffer<'a, 'b> = PacketBuffer<'a, 'b, IpAddress>;
|
2017-10-30 08:19:34 +08:00
|
|
|
|
|
|
|
/// An ICMPv4 socket
|
|
|
|
///
|
|
|
|
/// An ICMPv4 socket is bound to a specific [IcmpEndpoint] which may
|
|
|
|
/// be a sepecific UDP port to listen for ICMP error messages related
|
|
|
|
/// to the port or a specific ICMP identifier value. See [bind] for
|
|
|
|
/// more details.
|
|
|
|
///
|
|
|
|
/// [IcmpEndpoint]: enum.IcmpEndpoint.html
|
|
|
|
/// [bind]: #method.bind
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct IcmpSocket<'a, 'b: 'a> {
|
2017-11-22 11:50:09 +08:00
|
|
|
pub(crate) meta: SocketMeta,
|
2018-02-22 20:23:35 +08:00
|
|
|
rx_buffer: IcmpSocketBuffer<'a, 'b>,
|
|
|
|
tx_buffer: IcmpSocketBuffer<'a, 'b>,
|
2017-10-30 08:19:34 +08:00
|
|
|
/// The endpoint this socket is communicating with
|
|
|
|
endpoint: Endpoint,
|
|
|
|
/// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
2017-12-10 11:09:50 +08:00
|
|
|
hop_limit: Option<u8>
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> IcmpSocket<'a, 'b> {
|
|
|
|
/// Create an ICMPv4 socket with the given buffers.
|
2018-02-22 20:23:35 +08:00
|
|
|
pub fn new(rx_buffer: IcmpSocketBuffer<'a, 'b>,
|
|
|
|
tx_buffer: IcmpSocketBuffer<'a, 'b>) -> IcmpSocket<'a, 'b> {
|
2018-01-28 22:36:23 +08:00
|
|
|
IcmpSocket {
|
2017-11-22 11:50:09 +08:00
|
|
|
meta: SocketMeta::default(),
|
2017-10-30 08:19:34 +08:00
|
|
|
rx_buffer: rx_buffer,
|
|
|
|
tx_buffer: tx_buffer,
|
|
|
|
endpoint: Endpoint::default(),
|
2017-12-10 11:09:50 +08:00
|
|
|
hop_limit: None
|
2018-01-28 22:36:23 +08:00
|
|
|
}
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the socket handle.
|
|
|
|
#[inline]
|
|
|
|
pub fn handle(&self) -> SocketHandle {
|
2017-11-22 11:50:09 +08:00
|
|
|
self.meta.handle
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
|
|
|
///
|
2017-12-10 11:09:50 +08:00
|
|
|
/// See also the [set_hop_limit](#method.set_hop_limit) method
|
|
|
|
pub fn hop_limit(&self) -> Option<u8> {
|
|
|
|
self.hop_limit
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
|
|
|
///
|
2017-12-10 11:09:50 +08:00
|
|
|
/// A socket without an explicitly set hop limit value uses the default [IANA recommended]
|
2017-10-30 08:19:34 +08:00
|
|
|
/// value (64).
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2017-12-10 11:09:50 +08:00
|
|
|
/// This function panics if a hop limit value of 0 is given. See [RFC 1122 § 3.2.1.7].
|
2017-10-30 08:19:34 +08:00
|
|
|
///
|
|
|
|
/// [IANA recommended]: https://www.iana.org/assignments/ip-parameters/ip-parameters.xhtml
|
|
|
|
/// [RFC 1122 § 3.2.1.7]: https://tools.ietf.org/html/rfc1122#section-3.2.1.7
|
2017-12-10 11:09:50 +08:00
|
|
|
pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) {
|
|
|
|
// A host MUST NOT send a datagram with a hop limit value of 0
|
|
|
|
if let Some(0) = hop_limit {
|
2017-10-30 08:19:34 +08:00
|
|
|
panic!("the time-to-live value of a packet must not be zero")
|
|
|
|
}
|
|
|
|
|
2017-12-10 11:09:50 +08:00
|
|
|
self.hop_limit = hop_limit
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Bind the socket to the given endpoint.
|
|
|
|
///
|
|
|
|
/// This function returns `Err(Error::Illegal)` if the socket was open
|
|
|
|
/// (see [is_open](#method.is_open)), and `Err(Error::Unaddressable)`
|
|
|
|
/// if `endpoint` is unspecified (see [is_specified]).
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ## Bind to ICMP Error messages associated with a specific UDP port:
|
|
|
|
///
|
|
|
|
/// To [recv] ICMP error messages that are associated with a specific local
|
|
|
|
/// UDP port, the socket may be bound to a given port using [IcmpEndpoint::Udp].
|
|
|
|
/// This may be useful for applications using UDP attempting to detect and/or
|
|
|
|
/// diagnose connection problems.
|
|
|
|
///
|
|
|
|
/// ```
|
2018-02-22 20:23:35 +08:00
|
|
|
/// # use smoltcp::socket::{Socket, IcmpSocket, IcmpSocketBuffer, IcmpPacketMetadata};
|
2018-03-24 11:21:26 +08:00
|
|
|
/// # let rx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 20]);
|
|
|
|
/// # let tx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 20]);
|
2017-10-30 08:19:34 +08:00
|
|
|
/// use smoltcp::wire::IpEndpoint;
|
2017-11-09 09:59:27 +08:00
|
|
|
/// use smoltcp::socket::IcmpEndpoint;
|
|
|
|
///
|
|
|
|
/// let mut icmp_socket = // ...
|
2018-01-28 22:36:23 +08:00
|
|
|
/// # IcmpSocket::new(rx_buffer, tx_buffer);
|
2017-11-09 09:59:27 +08:00
|
|
|
///
|
2017-10-30 08:19:34 +08:00
|
|
|
/// // Bind to ICMP error responses for UDP packets sent from port 53.
|
|
|
|
/// let endpoint = IpEndpoint::from(53);
|
|
|
|
/// icmp_socket.bind(IcmpEndpoint::Udp(endpoint)).unwrap();
|
|
|
|
/// ```
|
|
|
|
///
|
2017-11-13 12:54:49 +08:00
|
|
|
/// ## Bind to a specific ICMP identifier:
|
2017-10-30 08:19:34 +08:00
|
|
|
///
|
|
|
|
/// To [send] and [recv] ICMP packets that are not associated with a specific UDP
|
2017-11-13 12:54:49 +08:00
|
|
|
/// port, the socket may be bound to a specific ICMP identifier using
|
2017-10-30 08:19:34 +08:00
|
|
|
/// [IcmpEndpoint::Ident]. This is useful for sending and receiving Echo Request/Reply
|
|
|
|
/// messages.
|
|
|
|
///
|
|
|
|
/// ```
|
2018-02-22 20:23:35 +08:00
|
|
|
/// # use smoltcp::socket::{Socket, IcmpSocket, IcmpSocketBuffer, IcmpPacketMetadata};
|
2018-03-24 11:21:26 +08:00
|
|
|
/// # let rx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 20]);
|
|
|
|
/// # let tx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 20]);
|
2017-11-09 09:59:27 +08:00
|
|
|
/// use smoltcp::socket::IcmpEndpoint;
|
|
|
|
///
|
|
|
|
/// let mut icmp_socket = // ...
|
2018-01-28 22:36:23 +08:00
|
|
|
/// # IcmpSocket::new(rx_buffer, tx_buffer);
|
2017-11-09 09:59:27 +08:00
|
|
|
///
|
2017-11-13 12:54:49 +08:00
|
|
|
/// // Bind to ICMP messages with the ICMP identifier 0x1234
|
2017-10-30 08:19:34 +08:00
|
|
|
/// icmp_socket.bind(IcmpEndpoint::Ident(0x1234)).unwrap();
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [is_specified]: enum.IcmpEndpoint.html#method.is_specified
|
2017-11-09 09:59:27 +08:00
|
|
|
/// [IcmpEndpoint::Ident]: enum.IcmpEndpoint.html#variant.Ident
|
|
|
|
/// [IcmpEndpoint::Udp]: enum.IcmpEndpoint.html#variant.Udp
|
2017-10-30 08:19:34 +08:00
|
|
|
/// [send]: #method.send
|
|
|
|
/// [recv]: #method.recv
|
|
|
|
pub fn bind<T: Into<Endpoint>>(&mut self, endpoint: T) -> Result<()> {
|
|
|
|
let endpoint = endpoint.into();
|
|
|
|
if !endpoint.is_specified() {
|
|
|
|
return Err(Error::Unaddressable);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_open() { return Err(Error::Illegal) }
|
|
|
|
|
|
|
|
self.endpoint = endpoint;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether the transmit buffer is full.
|
|
|
|
#[inline]
|
|
|
|
pub fn can_send(&self) -> bool {
|
|
|
|
!self.tx_buffer.is_full()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether the receive buffer is not empty.
|
|
|
|
#[inline]
|
|
|
|
pub fn can_recv(&self) -> bool {
|
|
|
|
!self.rx_buffer.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether the socket is open.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_open(&self) -> bool {
|
|
|
|
self.endpoint != Endpoint::Unspecified
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enqueue a packet to be sent to a given remote address, and return a pointer
|
|
|
|
/// to its payload.
|
|
|
|
///
|
|
|
|
/// This function returns `Err(Error::Exhausted)` if the transmit buffer is full,
|
|
|
|
/// `Err(Error::Truncated)` if the requested size is larger than the packet buffer
|
2017-11-09 09:59:27 +08:00
|
|
|
/// size, and `Err(Error::Unaddressable)` if the remote address is unspecified.
|
2017-10-30 08:19:34 +08:00
|
|
|
pub fn send(&mut self, size: usize, endpoint: IpAddress) -> Result<&mut [u8]> {
|
|
|
|
if endpoint.is_unspecified() {
|
|
|
|
return Err(Error::Unaddressable)
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:23:35 +08:00
|
|
|
let packet_buf = self.tx_buffer.enqueue(size, endpoint)?;
|
|
|
|
|
2017-10-30 08:19:34 +08:00
|
|
|
net_trace!("{}:{}: buffer to send {} octets",
|
2018-02-22 20:23:35 +08:00
|
|
|
self.meta.handle, endpoint, size);
|
|
|
|
Ok(packet_buf)
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Enqueue a packet to be sent to a given remote address, and fill it from a slice.
|
|
|
|
///
|
|
|
|
/// See also [send](#method.send).
|
|
|
|
pub fn send_slice(&mut self, data: &[u8], endpoint: IpAddress) -> Result<()> {
|
|
|
|
let packet_buf = self.send(data.len(), endpoint)?;
|
|
|
|
packet_buf.copy_from_slice(data);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dequeue a packet received from a remote endpoint, and return the `IpAddress` as well
|
|
|
|
/// as a pointer to the payload.
|
|
|
|
///
|
|
|
|
/// This function returns `Err(Error::Exhausted)` if the receive buffer is empty.
|
|
|
|
pub fn recv(&mut self) -> Result<(&[u8], IpAddress)> {
|
2018-02-22 20:23:35 +08:00
|
|
|
let (endpoint, packet_buf) = self.rx_buffer.dequeue()?;
|
|
|
|
|
2017-10-30 08:19:34 +08:00
|
|
|
net_trace!("{}:{}: receive {} buffered octets",
|
2018-02-22 20:23:35 +08:00
|
|
|
self.meta.handle, endpoint, packet_buf.len());
|
|
|
|
Ok((packet_buf, endpoint))
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Dequeue a packet received from a remote endpoint, copy the payload into the given slice,
|
|
|
|
/// and return the amount of octets copied as well as the `IpAddress`
|
|
|
|
///
|
|
|
|
/// See also [recv](#method.recv).
|
|
|
|
pub fn recv_slice(&mut self, data: &mut [u8]) -> Result<(usize, IpAddress)> {
|
|
|
|
let (buffer, endpoint) = self.recv()?;
|
|
|
|
let length = cmp::min(data.len(), buffer.len());
|
|
|
|
data[..length].copy_from_slice(&buffer[..length]);
|
|
|
|
Ok((length, endpoint))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Filter determining which packets received by the interface are appended to
|
|
|
|
/// the given sockets received buffer.
|
2018-05-09 21:27:51 +08:00
|
|
|
pub(crate) fn accepts(&self, ip_repr: &IpRepr, icmp_repr: &IcmpRepr,
|
2017-10-30 08:19:34 +08:00
|
|
|
cksum: &ChecksumCapabilities) -> bool {
|
2017-11-09 09:59:27 +08:00
|
|
|
match (&self.endpoint, icmp_repr) {
|
2017-10-30 08:19:34 +08:00
|
|
|
// If we are bound to ICMP errors associated to a UDP port, only
|
|
|
|
// accept Destination Unreachable messages with the data containing
|
|
|
|
// a UDP packet send from the local port we are bound to.
|
2018-05-09 21:27:51 +08:00
|
|
|
#[cfg(feature = "proto-ipv4")]
|
|
|
|
(&Endpoint::Udp(endpoint), &IcmpRepr::Ipv4(Icmpv4Repr::DstUnreachable { data, .. }))
|
|
|
|
if endpoint.addr.is_unspecified() || endpoint.addr == ip_repr.dst_addr() => {
|
|
|
|
let packet = UdpPacket::new(data);
|
|
|
|
match UdpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr(), cksum) {
|
|
|
|
Ok(repr) => endpoint.port == repr.src_port,
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "proto-ipv6")]
|
|
|
|
(&Endpoint::Udp(endpoint), &IcmpRepr::Ipv6(Icmpv6Repr::DstUnreachable { data, .. }))
|
2017-11-09 09:59:27 +08:00
|
|
|
if endpoint.addr.is_unspecified() || endpoint.addr == ip_repr.dst_addr() => {
|
|
|
|
let packet = UdpPacket::new(data);
|
|
|
|
match UdpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr(), cksum) {
|
|
|
|
Ok(repr) => endpoint.port == repr.src_port,
|
|
|
|
Err(_) => false,
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
2017-11-09 09:59:27 +08:00
|
|
|
}
|
2017-10-30 08:19:34 +08:00
|
|
|
// If we are bound to a specific ICMP identifier value, only accept an
|
|
|
|
// Echo Request/Reply with the identifier field matching the endpoint
|
|
|
|
// port.
|
2018-05-09 21:27:51 +08:00
|
|
|
#[cfg(feature = "proto-ipv4")]
|
|
|
|
(&Endpoint::Ident(bound_ident), &IcmpRepr::Ipv4(Icmpv4Repr::EchoRequest { ident, .. })) |
|
|
|
|
(&Endpoint::Ident(bound_ident), &IcmpRepr::Ipv4(Icmpv4Repr::EchoReply { ident, .. })) =>
|
|
|
|
ident == bound_ident,
|
|
|
|
#[cfg(feature = "proto-ipv6")]
|
|
|
|
(&Endpoint::Ident(bound_ident), &IcmpRepr::Ipv6(Icmpv6Repr::EchoRequest { ident, .. })) |
|
|
|
|
(&Endpoint::Ident(bound_ident), &IcmpRepr::Ipv6(Icmpv6Repr::EchoReply { ident, .. })) =>
|
2017-11-09 09:59:27 +08:00
|
|
|
ident == bound_ident,
|
2017-10-30 08:19:34 +08:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
pub(crate) fn process(&mut self, ip_repr: &IpRepr, icmp_repr: &IcmpRepr,
|
2018-02-22 20:23:35 +08:00
|
|
|
_cksum: &ChecksumCapabilities) -> Result<()> {
|
2018-05-09 21:27:51 +08:00
|
|
|
match icmp_repr {
|
|
|
|
#[cfg(feature = "proto-ipv4")]
|
|
|
|
&IcmpRepr::Ipv4(ref icmp_repr) => {
|
|
|
|
let packet_buf = self.rx_buffer.enqueue(icmp_repr.buffer_len(), ip_repr.src_addr())?;
|
|
|
|
icmp_repr.emit(&mut Icmpv4Packet::new(packet_buf), &ChecksumCapabilities::default());
|
|
|
|
|
|
|
|
net_trace!("{}:{}: receiving {} octets",
|
|
|
|
self.meta.handle, icmp_repr.buffer_len(), packet_buf.len());
|
|
|
|
},
|
|
|
|
#[cfg(feature = "proto-ipv6")]
|
|
|
|
&IcmpRepr::Ipv6(ref icmp_repr) => {
|
|
|
|
let packet_buf = self.rx_buffer.enqueue(icmp_repr.buffer_len(), ip_repr.src_addr())?;
|
|
|
|
icmp_repr.emit(&ip_repr.src_addr(), &ip_repr.dst_addr(),
|
|
|
|
&mut Icmpv6Packet::new(packet_buf), &ChecksumCapabilities::default());
|
|
|
|
|
|
|
|
net_trace!("{}:{}: receiving {} octets",
|
|
|
|
self.meta.handle, icmp_repr.buffer_len(), packet_buf.len());
|
|
|
|
},
|
|
|
|
}
|
2017-10-30 08:19:34 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:23:35 +08:00
|
|
|
pub(crate) fn dispatch<F>(&mut self, _caps: &DeviceCapabilities, emit: F) -> Result<()>
|
2018-05-09 21:27:51 +08:00
|
|
|
where F: FnOnce((IpRepr, IcmpRepr)) -> Result<()>
|
2017-11-09 09:59:27 +08:00
|
|
|
{
|
2018-02-22 20:23:35 +08:00
|
|
|
let handle = self.meta.handle;
|
2017-12-10 11:09:50 +08:00
|
|
|
let hop_limit = self.hop_limit.unwrap_or(64);
|
2018-02-22 20:23:35 +08:00
|
|
|
self.tx_buffer.dequeue_with(|remote_endpoint, packet_buf| {
|
2017-10-30 08:19:34 +08:00
|
|
|
net_trace!("{}:{}: sending {} octets",
|
2018-02-22 20:23:35 +08:00
|
|
|
handle, remote_endpoint, packet_buf.len());
|
|
|
|
match *remote_endpoint {
|
2018-05-09 21:27:51 +08:00
|
|
|
#[cfg(feature = "proto-ipv4")]
|
2017-10-30 08:19:34 +08:00
|
|
|
IpAddress::Ipv4(ipv4_addr) => {
|
2018-02-22 20:23:35 +08:00
|
|
|
let packet = Icmpv4Packet::new(&*packet_buf);
|
2018-05-09 21:27:51 +08:00
|
|
|
let repr = Icmpv4Repr::parse(&packet, &ChecksumCapabilities::ignored())?;
|
2017-10-30 08:19:34 +08:00
|
|
|
let ip_repr = IpRepr::Ipv4(Ipv4Repr {
|
|
|
|
src_addr: Ipv4Address::default(),
|
|
|
|
dst_addr: ipv4_addr,
|
|
|
|
protocol: IpProtocol::Icmp,
|
|
|
|
payload_len: repr.buffer_len(),
|
2017-12-10 11:09:50 +08:00
|
|
|
hop_limit: hop_limit,
|
2017-10-30 08:19:34 +08:00
|
|
|
});
|
2018-05-09 21:27:51 +08:00
|
|
|
emit((ip_repr, IcmpRepr::Ipv4(repr)))
|
|
|
|
},
|
|
|
|
#[cfg(feature = "proto-ipv6")]
|
|
|
|
IpAddress::Ipv6(ipv6_addr) => {
|
|
|
|
let packet = Icmpv6Packet::new(&*packet_buf);
|
|
|
|
let src_addr = Ipv6Address::default();
|
|
|
|
let repr = Icmpv6Repr::parse(&src_addr.into(), &ipv6_addr.into(), &packet, &ChecksumCapabilities::ignored())?;
|
|
|
|
let ip_repr = IpRepr::Ipv6(Ipv6Repr {
|
|
|
|
src_addr: src_addr,
|
|
|
|
dst_addr: ipv6_addr,
|
2018-05-14 02:22:18 +08:00
|
|
|
next_header: IpProtocol::Icmpv6,
|
2018-05-09 21:27:51 +08:00
|
|
|
payload_len: repr.buffer_len(),
|
|
|
|
hop_limit: hop_limit,
|
|
|
|
});
|
|
|
|
emit((ip_repr, IcmpRepr::Ipv6(repr)))
|
2017-10-30 08:19:34 +08:00
|
|
|
},
|
|
|
|
_ => Err(Error::Unaddressable)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-11 00:32:41 +08:00
|
|
|
pub(crate) fn poll_at(&self) -> Option<Instant> {
|
2017-10-30 08:19:34 +08:00
|
|
|
if self.tx_buffer.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2018-02-11 00:32:41 +08:00
|
|
|
Some(Instant::from_millis(0))
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 22:36:23 +08:00
|
|
|
impl<'a, 'b> Into<Socket<'a, 'b>> for IcmpSocket<'a, 'b> {
|
|
|
|
fn into(self) -> Socket<'a, 'b> {
|
|
|
|
Socket::Icmp(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 08:19:34 +08:00
|
|
|
#[cfg(test)]
|
2018-05-09 21:27:51 +08:00
|
|
|
mod tests_common {
|
|
|
|
pub use phy::DeviceCapabilities;
|
|
|
|
pub use wire::IpAddress;
|
|
|
|
pub use super::*;
|
2017-10-30 08:19:34 +08:00
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
pub fn buffer(packets: usize) -> IcmpSocketBuffer<'static, 'static> {
|
|
|
|
IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY; packets], vec![0; 66 * packets])
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
pub fn socket(rx_buffer: IcmpSocketBuffer<'static, 'static>,
|
2018-02-22 20:23:35 +08:00
|
|
|
tx_buffer: IcmpSocketBuffer<'static, 'static>) -> IcmpSocket<'static, 'static> {
|
2018-01-28 22:36:23 +08:00
|
|
|
IcmpSocket::new(rx_buffer, tx_buffer)
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
pub const LOCAL_PORT: u16 = 53;
|
|
|
|
|
|
|
|
pub static UDP_REPR: UdpRepr = UdpRepr {
|
|
|
|
src_port: 53,
|
|
|
|
dst_port: 9090,
|
|
|
|
payload: &[0xff; 10]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(test, feature = "proto-ipv4"))]
|
|
|
|
mod test_ipv4 {
|
|
|
|
use super::tests_common::*;
|
|
|
|
|
|
|
|
use wire::Icmpv4DstUnreachable;
|
|
|
|
|
2017-10-30 08:19:34 +08:00
|
|
|
const REMOTE_IPV4: Ipv4Address = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
|
2018-02-22 20:23:35 +08:00
|
|
|
const LOCAL_IPV4: Ipv4Address = Ipv4Address([0x7f, 0x00, 0x00, 0x01]);
|
2018-05-09 21:27:51 +08:00
|
|
|
const LOCAL_END_V4: IpEndpoint = IpEndpoint { addr: IpAddress::Ipv4(LOCAL_IPV4), port: LOCAL_PORT };
|
2017-10-30 08:19:34 +08:00
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
static ECHOV4_REPR: Icmpv4Repr = Icmpv4Repr::EchoRequest {
|
2017-10-30 08:19:34 +08:00
|
|
|
ident: 0x1234,
|
|
|
|
seq_no: 0x5678,
|
|
|
|
data: &[0xff; 16]
|
|
|
|
};
|
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
static LOCAL_IPV4_REPR: IpRepr = IpRepr::Ipv4(Ipv4Repr {
|
2017-10-30 08:19:34 +08:00
|
|
|
src_addr: Ipv4Address::UNSPECIFIED,
|
|
|
|
dst_addr: REMOTE_IPV4,
|
|
|
|
protocol: IpProtocol::Icmp,
|
|
|
|
payload_len: 24,
|
2017-12-10 11:09:50 +08:00
|
|
|
hop_limit: 0x40
|
2017-10-30 08:19:34 +08:00
|
|
|
});
|
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
static REMOTE_IPV4_REPR: IpRepr = IpRepr::Ipv4(Ipv4Repr {
|
2017-10-30 08:19:34 +08:00
|
|
|
src_addr: REMOTE_IPV4,
|
|
|
|
dst_addr: LOCAL_IPV4,
|
|
|
|
protocol: IpProtocol::Icmp,
|
|
|
|
payload_len: 24,
|
2017-12-10 11:09:50 +08:00
|
|
|
hop_limit: 0x40
|
2017-10-30 08:19:34 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send_unaddressable() {
|
|
|
|
let mut socket = socket(buffer(0), buffer(1));
|
|
|
|
assert_eq!(socket.send_slice(b"abcdef", IpAddress::default()),
|
|
|
|
Err(Error::Unaddressable));
|
2018-05-09 21:27:51 +08:00
|
|
|
assert_eq!(socket.send_slice(b"abcdef", REMOTE_IPV4.into()), Ok(()));
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send_dispatch() {
|
|
|
|
let mut socket = socket(buffer(0), buffer(1));
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
|
|
|
|
assert_eq!(socket.dispatch(&caps, |_| unreachable!()),
|
|
|
|
Err(Error::Exhausted));
|
|
|
|
|
|
|
|
// This buffer is too long
|
2018-05-09 21:27:51 +08:00
|
|
|
assert_eq!(socket.send_slice(&[0xff; 67], REMOTE_IPV4.into()), Err(Error::Truncated));
|
2017-10-30 08:19:34 +08:00
|
|
|
assert!(socket.can_send());
|
|
|
|
|
|
|
|
let mut bytes = [0xff; 24];
|
|
|
|
let mut packet = Icmpv4Packet::new(&mut bytes);
|
2018-05-09 21:27:51 +08:00
|
|
|
ECHOV4_REPR.emit(&mut packet, &caps.checksum);
|
2017-10-30 08:19:34 +08:00
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
assert_eq!(socket.send_slice(&packet.into_inner()[..], REMOTE_IPV4.into()), Ok(()));
|
|
|
|
assert_eq!(socket.send_slice(b"123456", REMOTE_IPV4.into()), Err(Error::Exhausted));
|
2017-10-30 08:19:34 +08:00
|
|
|
assert!(!socket.can_send());
|
|
|
|
|
|
|
|
assert_eq!(socket.dispatch(&caps, |(ip_repr, icmp_repr)| {
|
2018-05-09 21:27:51 +08:00
|
|
|
assert_eq!(ip_repr, LOCAL_IPV4_REPR);
|
|
|
|
assert_eq!(icmp_repr, ECHOV4_REPR.into());
|
2017-10-30 08:19:34 +08:00
|
|
|
Err(Error::Unaddressable)
|
|
|
|
}), Err(Error::Unaddressable));
|
|
|
|
// buffer is not taken off of the tx queue due to the error
|
|
|
|
assert!(!socket.can_send());
|
|
|
|
|
|
|
|
assert_eq!(socket.dispatch(&caps, |(ip_repr, icmp_repr)| {
|
2018-05-09 21:27:51 +08:00
|
|
|
assert_eq!(ip_repr, LOCAL_IPV4_REPR);
|
|
|
|
assert_eq!(icmp_repr, ECHOV4_REPR.into());
|
2017-10-30 08:19:34 +08:00
|
|
|
Ok(())
|
|
|
|
}), Ok(()));
|
|
|
|
// buffer is taken off of the queue this time
|
|
|
|
assert!(socket.can_send());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-05-09 21:27:51 +08:00
|
|
|
fn test_set_hop_limit_v4() {
|
2017-10-30 08:19:34 +08:00
|
|
|
let mut s = socket(buffer(0), buffer(1));
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
|
|
|
|
let mut bytes = [0xff; 24];
|
|
|
|
let mut packet = Icmpv4Packet::new(&mut bytes);
|
2018-05-09 21:27:51 +08:00
|
|
|
ECHOV4_REPR.emit(&mut packet, &caps.checksum);
|
2017-10-30 08:19:34 +08:00
|
|
|
|
2017-12-10 11:09:50 +08:00
|
|
|
s.set_hop_limit(Some(0x2a));
|
2017-10-30 08:19:34 +08:00
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
assert_eq!(s.send_slice(&packet.into_inner()[..], REMOTE_IPV4.into()), Ok(()));
|
2017-10-30 08:19:34 +08:00
|
|
|
assert_eq!(s.dispatch(&caps, |(ip_repr, _)| {
|
|
|
|
assert_eq!(ip_repr, IpRepr::Ipv4(Ipv4Repr {
|
|
|
|
src_addr: Ipv4Address::UNSPECIFIED,
|
|
|
|
dst_addr: REMOTE_IPV4,
|
|
|
|
protocol: IpProtocol::Icmp,
|
2018-05-09 21:27:51 +08:00
|
|
|
payload_len: ECHOV4_REPR.buffer_len(),
|
2017-12-10 11:09:50 +08:00
|
|
|
hop_limit: 0x2a,
|
2017-10-30 08:19:34 +08:00
|
|
|
}));
|
|
|
|
Ok(())
|
|
|
|
}), Ok(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_recv_process() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(1));
|
|
|
|
assert_eq!(socket.bind(Endpoint::Ident(0x1234)), Ok(()));
|
|
|
|
|
|
|
|
assert!(!socket.can_recv());
|
|
|
|
assert_eq!(socket.recv(), Err(Error::Exhausted));
|
|
|
|
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
|
2017-12-07 11:52:40 +08:00
|
|
|
let mut bytes = [0xff; 24];
|
2017-10-30 08:19:34 +08:00
|
|
|
let mut packet = Icmpv4Packet::new(&mut bytes);
|
2018-05-09 21:27:51 +08:00
|
|
|
ECHOV4_REPR.emit(&mut packet, &caps.checksum);
|
2017-10-30 08:19:34 +08:00
|
|
|
let data = &packet.into_inner()[..];
|
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
assert!(socket.accepts(&REMOTE_IPV4_REPR, &ECHOV4_REPR.into(), &caps.checksum));
|
|
|
|
assert_eq!(socket.process(&REMOTE_IPV4_REPR, &ECHOV4_REPR.into(), &caps.checksum),
|
2017-10-30 08:19:34 +08:00
|
|
|
Ok(()));
|
|
|
|
assert!(socket.can_recv());
|
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
assert!(socket.accepts(&REMOTE_IPV4_REPR, &ECHOV4_REPR.into(), &caps.checksum));
|
|
|
|
assert_eq!(socket.process(&REMOTE_IPV4_REPR, &ECHOV4_REPR.into(), &caps.checksum),
|
2017-10-30 08:19:34 +08:00
|
|
|
Err(Error::Exhausted));
|
2017-12-07 11:52:40 +08:00
|
|
|
|
2018-05-09 21:27:51 +08:00
|
|
|
assert_eq!(socket.recv(), Ok((&data[..], REMOTE_IPV4.into())));
|
2017-10-30 08:19:34 +08:00
|
|
|
assert!(!socket.can_recv());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_accept_bad_id() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(1));
|
|
|
|
assert_eq!(socket.bind(Endpoint::Ident(0x1234)), Ok(()));
|
|
|
|
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
let mut bytes = [0xff; 20];
|
|
|
|
let mut packet = Icmpv4Packet::new(&mut bytes);
|
|
|
|
let icmp_repr = Icmpv4Repr::EchoRequest {
|
|
|
|
ident: 0x4321,
|
|
|
|
seq_no: 0x5678,
|
|
|
|
data: &[0xff; 16]
|
|
|
|
};
|
|
|
|
icmp_repr.emit(&mut packet, &caps.checksum);
|
|
|
|
|
|
|
|
// Ensure that a packet with an identifier that isn't the bound
|
|
|
|
// ID is not accepted
|
2018-05-09 21:27:51 +08:00
|
|
|
assert!(!socket.accepts(&REMOTE_IPV4_REPR, &icmp_repr.into(), &caps.checksum));
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_accepts_udp() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(1));
|
2018-05-09 21:27:51 +08:00
|
|
|
assert_eq!(socket.bind(Endpoint::Udp(LOCAL_END_V4)), Ok(()));
|
2017-10-30 08:19:34 +08:00
|
|
|
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
|
|
|
|
let mut bytes = [0xff; 18];
|
|
|
|
let mut packet = UdpPacket::new(&mut bytes);
|
2018-05-09 21:27:51 +08:00
|
|
|
UDP_REPR.emit(&mut packet, &REMOTE_IPV4.into(), &LOCAL_IPV4.into(), &caps.checksum);
|
2017-10-30 08:19:34 +08:00
|
|
|
|
|
|
|
let data = &packet.into_inner()[..];
|
|
|
|
|
|
|
|
let icmp_repr = Icmpv4Repr::DstUnreachable {
|
|
|
|
reason: Icmpv4DstUnreachable::PortUnreachable,
|
|
|
|
header: Ipv4Repr {
|
|
|
|
src_addr: LOCAL_IPV4,
|
|
|
|
dst_addr: REMOTE_IPV4,
|
|
|
|
protocol: IpProtocol::Icmp,
|
|
|
|
payload_len: 12,
|
2017-12-10 11:09:50 +08:00
|
|
|
hop_limit: 0x40
|
2017-10-30 08:19:34 +08:00
|
|
|
},
|
|
|
|
data: data
|
|
|
|
};
|
|
|
|
let ip_repr = IpRepr::Unspecified {
|
2018-05-09 21:27:51 +08:00
|
|
|
src_addr: REMOTE_IPV4.into(),
|
|
|
|
dst_addr: LOCAL_IPV4.into(),
|
2017-10-30 08:19:34 +08:00
|
|
|
protocol: IpProtocol::Icmp,
|
|
|
|
payload_len: icmp_repr.buffer_len(),
|
2017-12-10 11:09:50 +08:00
|
|
|
hop_limit: 0x40
|
2017-10-30 08:19:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(!socket.can_recv());
|
|
|
|
|
|
|
|
// Ensure we can accept ICMP error response to the bound
|
|
|
|
// UDP port
|
2018-05-09 21:27:51 +08:00
|
|
|
assert!(socket.accepts(&ip_repr, &icmp_repr.into(), &caps.checksum));
|
|
|
|
assert_eq!(socket.process(&ip_repr, &icmp_repr.into(), &caps.checksum),
|
2017-10-30 08:19:34 +08:00
|
|
|
Ok(()));
|
|
|
|
assert!(socket.can_recv());
|
2017-12-07 11:52:40 +08:00
|
|
|
|
|
|
|
let mut bytes = [0x00; 46];
|
|
|
|
let mut packet = Icmpv4Packet::new(&mut bytes[..]);
|
|
|
|
icmp_repr.emit(&mut packet, &caps.checksum);
|
2018-05-09 21:27:51 +08:00
|
|
|
assert_eq!(socket.recv(), Ok((&packet.into_inner()[..], REMOTE_IPV4.into())));
|
|
|
|
assert!(!socket.can_recv());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(test, feature = "proto-ipv6"))]
|
|
|
|
mod test_ipv6 {
|
|
|
|
use super::tests_common::*;
|
|
|
|
|
|
|
|
use wire::Icmpv6DstUnreachable;
|
|
|
|
|
|
|
|
const REMOTE_IPV6: Ipv6Address = Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 1]);
|
|
|
|
const LOCAL_IPV6: Ipv6Address = Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 2]);
|
|
|
|
const LOCAL_END_V6: IpEndpoint = IpEndpoint { addr: IpAddress::Ipv6(LOCAL_IPV6), port: LOCAL_PORT };
|
|
|
|
static ECHOV6_REPR: Icmpv6Repr = Icmpv6Repr::EchoRequest {
|
|
|
|
ident: 0x1234,
|
|
|
|
seq_no: 0x5678,
|
|
|
|
data: &[0xff; 16]
|
|
|
|
};
|
|
|
|
|
|
|
|
static LOCAL_IPV6_REPR: IpRepr = IpRepr::Ipv6(Ipv6Repr {
|
|
|
|
src_addr: Ipv6Address::UNSPECIFIED,
|
|
|
|
dst_addr: REMOTE_IPV6,
|
2018-05-14 02:22:18 +08:00
|
|
|
next_header: IpProtocol::Icmpv6,
|
2018-05-09 21:27:51 +08:00
|
|
|
payload_len: 24,
|
|
|
|
hop_limit: 0x40
|
|
|
|
});
|
|
|
|
|
|
|
|
static REMOTE_IPV6_REPR: IpRepr = IpRepr::Ipv6(Ipv6Repr {
|
|
|
|
src_addr: REMOTE_IPV6,
|
|
|
|
dst_addr: LOCAL_IPV6,
|
2018-05-14 02:22:18 +08:00
|
|
|
next_header: IpProtocol::Icmpv6,
|
2018-05-09 21:27:51 +08:00
|
|
|
payload_len: 24,
|
|
|
|
hop_limit: 0x40
|
|
|
|
});
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send_unaddressable() {
|
|
|
|
let mut socket = socket(buffer(0), buffer(1));
|
|
|
|
assert_eq!(socket.send_slice(b"abcdef", IpAddress::default()),
|
|
|
|
Err(Error::Unaddressable));
|
|
|
|
assert_eq!(socket.send_slice(b"abcdef", REMOTE_IPV6.into()), Ok(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send_dispatch() {
|
|
|
|
let mut socket = socket(buffer(0), buffer(1));
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
|
|
|
|
assert_eq!(socket.dispatch(&caps, |_| unreachable!()),
|
|
|
|
Err(Error::Exhausted));
|
|
|
|
|
|
|
|
// This buffer is too long
|
|
|
|
assert_eq!(socket.send_slice(&[0xff; 67], REMOTE_IPV6.into()), Err(Error::Truncated));
|
|
|
|
assert!(socket.can_send());
|
|
|
|
|
|
|
|
let mut bytes = vec![0xff; 24];
|
|
|
|
let mut packet = Icmpv6Packet::new(&mut bytes);
|
|
|
|
ECHOV6_REPR.emit(&LOCAL_IPV6.into(), &REMOTE_IPV6.into(), &mut packet, &caps.checksum);
|
|
|
|
|
|
|
|
assert_eq!(socket.send_slice(&packet.into_inner()[..], REMOTE_IPV6.into()), Ok(()));
|
|
|
|
assert_eq!(socket.send_slice(b"123456", REMOTE_IPV6.into()), Err(Error::Exhausted));
|
|
|
|
assert!(!socket.can_send());
|
|
|
|
|
|
|
|
assert_eq!(socket.dispatch(&caps, |(ip_repr, icmp_repr)| {
|
|
|
|
assert_eq!(ip_repr, LOCAL_IPV6_REPR);
|
|
|
|
assert_eq!(icmp_repr, ECHOV6_REPR.into());
|
|
|
|
Err(Error::Unaddressable)
|
|
|
|
}), Err(Error::Unaddressable));
|
|
|
|
// buffer is not taken off of the tx queue due to the error
|
|
|
|
assert!(!socket.can_send());
|
|
|
|
|
|
|
|
assert_eq!(socket.dispatch(&caps, |(ip_repr, icmp_repr)| {
|
|
|
|
assert_eq!(ip_repr, LOCAL_IPV6_REPR);
|
|
|
|
assert_eq!(icmp_repr, ECHOV6_REPR.into());
|
|
|
|
Ok(())
|
|
|
|
}), Ok(()));
|
|
|
|
// buffer is taken off of the queue this time
|
|
|
|
assert!(socket.can_send());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_hop_limit() {
|
|
|
|
let mut s = socket(buffer(0), buffer(1));
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
|
|
|
|
let mut bytes = vec![0xff; 24];
|
|
|
|
let mut packet = Icmpv6Packet::new(&mut bytes);
|
|
|
|
ECHOV6_REPR.emit(&LOCAL_IPV6.into(), &REMOTE_IPV6.into(), &mut packet, &caps.checksum);
|
|
|
|
|
|
|
|
s.set_hop_limit(Some(0x2a));
|
|
|
|
|
|
|
|
assert_eq!(s.send_slice(&packet.into_inner()[..], REMOTE_IPV6.into()), Ok(()));
|
|
|
|
assert_eq!(s.dispatch(&caps, |(ip_repr, _)| {
|
|
|
|
assert_eq!(ip_repr, IpRepr::Ipv6(Ipv6Repr {
|
|
|
|
src_addr: Ipv6Address::UNSPECIFIED,
|
|
|
|
dst_addr: REMOTE_IPV6,
|
2018-05-14 02:22:18 +08:00
|
|
|
next_header: IpProtocol::Icmpv6,
|
2018-05-09 21:27:51 +08:00
|
|
|
payload_len: ECHOV6_REPR.buffer_len(),
|
|
|
|
hop_limit: 0x2a,
|
|
|
|
}));
|
|
|
|
Ok(())
|
|
|
|
}), Ok(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_recv_process() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(1));
|
|
|
|
assert_eq!(socket.bind(Endpoint::Ident(0x1234)), Ok(()));
|
|
|
|
|
|
|
|
assert!(!socket.can_recv());
|
|
|
|
assert_eq!(socket.recv(), Err(Error::Exhausted));
|
|
|
|
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
|
|
|
|
let mut bytes = [0xff; 24];
|
|
|
|
let mut packet = Icmpv6Packet::new(&mut bytes);
|
|
|
|
ECHOV6_REPR.emit(&LOCAL_IPV6.into(), &REMOTE_IPV6.into(), &mut packet, &caps.checksum);
|
|
|
|
let data = &packet.into_inner()[..];
|
|
|
|
|
|
|
|
assert!(socket.accepts(&REMOTE_IPV6_REPR, &ECHOV6_REPR.into(), &caps.checksum));
|
|
|
|
assert_eq!(socket.process(&REMOTE_IPV6_REPR, &ECHOV6_REPR.into(), &caps.checksum),
|
|
|
|
Ok(()));
|
|
|
|
assert!(socket.can_recv());
|
|
|
|
|
|
|
|
assert!(socket.accepts(&REMOTE_IPV6_REPR, &ECHOV6_REPR.into(), &caps.checksum));
|
|
|
|
assert_eq!(socket.process(&REMOTE_IPV6_REPR, &ECHOV6_REPR.into(), &caps.checksum),
|
|
|
|
Err(Error::Exhausted));
|
|
|
|
|
|
|
|
assert_eq!(socket.recv(), Ok((&data[..], REMOTE_IPV6.into())));
|
|
|
|
assert!(!socket.can_recv());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_accept_bad_id() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(1));
|
|
|
|
assert_eq!(socket.bind(Endpoint::Ident(0x1234)), Ok(()));
|
|
|
|
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
let mut bytes = [0xff; 20];
|
|
|
|
let mut packet = Icmpv6Packet::new(&mut bytes);
|
|
|
|
let icmp_repr = Icmpv6Repr::EchoRequest {
|
|
|
|
ident: 0x4321,
|
|
|
|
seq_no: 0x5678,
|
|
|
|
data: &[0xff; 16]
|
|
|
|
};
|
|
|
|
icmp_repr.emit(&LOCAL_IPV6.into(), &REMOTE_IPV6.into(), &mut packet, &caps.checksum);
|
|
|
|
|
|
|
|
// Ensure that a packet with an identifier that isn't the bound
|
|
|
|
// ID is not accepted
|
|
|
|
assert!(!socket.accepts(&REMOTE_IPV6_REPR, &icmp_repr.into(), &caps.checksum));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_accepts_udp() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(1));
|
|
|
|
assert_eq!(socket.bind(Endpoint::Udp(LOCAL_END_V6)), Ok(()));
|
|
|
|
|
|
|
|
let caps = DeviceCapabilities::default();
|
|
|
|
|
|
|
|
let mut bytes = [0xff; 18];
|
|
|
|
let mut packet = UdpPacket::new(&mut bytes);
|
|
|
|
UDP_REPR.emit(&mut packet, &REMOTE_IPV6.into(), &LOCAL_IPV6.into(), &caps.checksum);
|
|
|
|
|
|
|
|
let data = &packet.into_inner()[..];
|
|
|
|
|
|
|
|
let icmp_repr = Icmpv6Repr::DstUnreachable {
|
|
|
|
reason: Icmpv6DstUnreachable::PortUnreachable,
|
|
|
|
header: Ipv6Repr {
|
|
|
|
src_addr: LOCAL_IPV6,
|
|
|
|
dst_addr: REMOTE_IPV6,
|
2018-05-14 02:22:18 +08:00
|
|
|
next_header: IpProtocol::Icmpv6,
|
2018-05-09 21:27:51 +08:00
|
|
|
payload_len: 12,
|
|
|
|
hop_limit: 0x40
|
|
|
|
},
|
|
|
|
data: data
|
|
|
|
};
|
|
|
|
let ip_repr = IpRepr::Unspecified {
|
|
|
|
src_addr: REMOTE_IPV6.into(),
|
|
|
|
dst_addr: LOCAL_IPV6.into(),
|
2018-05-14 02:22:18 +08:00
|
|
|
protocol: IpProtocol::Icmpv6,
|
2018-05-09 21:27:51 +08:00
|
|
|
payload_len: icmp_repr.buffer_len(),
|
|
|
|
hop_limit: 0x40
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(!socket.can_recv());
|
|
|
|
|
|
|
|
// Ensure we can accept ICMP error response to the bound
|
|
|
|
// UDP port
|
|
|
|
assert!(socket.accepts(&ip_repr, &icmp_repr.into(), &caps.checksum));
|
|
|
|
assert_eq!(socket.process(&ip_repr, &icmp_repr.into(), &caps.checksum),
|
|
|
|
Ok(()));
|
|
|
|
assert!(socket.can_recv());
|
|
|
|
|
|
|
|
let mut bytes = [0x00; 66];
|
|
|
|
let mut packet = Icmpv6Packet::new(&mut bytes[..]);
|
|
|
|
icmp_repr.emit(&LOCAL_IPV6.into(), &REMOTE_IPV6.into(), &mut packet, &caps.checksum);
|
|
|
|
assert_eq!(socket.recv(), Ok((&packet.into_inner()[..], REMOTE_IPV6.into())));
|
2017-12-07 11:52:40 +08:00
|
|
|
assert!(!socket.can_recv());
|
2017-10-30 08:19:34 +08:00
|
|
|
}
|
|
|
|
}
|