2017-07-27 22:53:06 +08:00
|
|
|
use core::cmp::min;
|
2017-01-10 19:04:00 +08:00
|
|
|
use managed::Managed;
|
|
|
|
|
2017-07-27 21:51:02 +08:00
|
|
|
use {Error, Result};
|
2017-08-28 12:50:57 +08:00
|
|
|
use wire::{IpProtocol, IpEndpoint, UdpRepr};
|
2017-08-28 11:50:40 +08:00
|
|
|
use socket::{Socket, IpRepr};
|
2017-06-18 18:11:21 +08:00
|
|
|
use storage::{Resettable, RingBuffer};
|
2016-12-15 01:39:44 +08:00
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
/// A buffered UDP packet.
|
2016-12-17 12:15:12 +08:00
|
|
|
#[derive(Debug)]
|
2016-12-20 21:56:59 +08:00
|
|
|
pub struct PacketBuffer<'a> {
|
2016-12-20 21:54:11 +08:00
|
|
|
endpoint: IpEndpoint,
|
2016-12-16 01:07:56 +08:00
|
|
|
size: usize,
|
2016-12-17 12:15:12 +08:00
|
|
|
payload: Managed<'a, [u8]>
|
2016-12-16 01:07:56 +08:00
|
|
|
}
|
2016-12-15 01:39:44 +08:00
|
|
|
|
2016-12-20 21:56:59 +08:00
|
|
|
impl<'a> PacketBuffer<'a> {
|
2016-12-16 01:07:56 +08:00
|
|
|
/// Create a buffered packet.
|
2016-12-20 21:56:59 +08:00
|
|
|
pub fn new<T>(payload: T) -> PacketBuffer<'a>
|
2016-12-17 12:15:12 +08:00
|
|
|
where T: Into<Managed<'a, [u8]>> {
|
2016-12-20 21:56:59 +08:00
|
|
|
PacketBuffer {
|
2016-12-28 08:12:15 +08:00
|
|
|
endpoint: IpEndpoint::default(),
|
2016-12-16 01:07:56 +08:00
|
|
|
size: 0,
|
2016-12-17 12:15:12 +08:00
|
|
|
payload: payload.into()
|
2016-12-16 01:07:56 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-17 13:12:45 +08:00
|
|
|
|
|
|
|
fn as_ref<'b>(&'b self) -> &'b [u8] {
|
|
|
|
&self.payload[..self.size]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_mut<'b>(&'b mut self) -> &'b mut [u8] {
|
|
|
|
&mut self.payload[..self.size]
|
|
|
|
}
|
2017-07-27 22:53:06 +08:00
|
|
|
|
|
|
|
fn resize<'b>(&'b mut self, size: usize) -> Result<&'b mut Self> {
|
|
|
|
if self.payload.len() >= size {
|
|
|
|
self.size = size;
|
|
|
|
Ok(self)
|
|
|
|
} else {
|
|
|
|
Err(Error::Truncated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Resettable for PacketBuffer<'a> {
|
|
|
|
fn reset(&mut self) {
|
|
|
|
self.endpoint = Default::default();
|
|
|
|
self.size = 0;
|
|
|
|
}
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-19 03:40:11 +08:00
|
|
|
/// An UDP packet ring buffer.
|
2017-06-18 18:11:21 +08:00
|
|
|
pub type SocketBuffer<'a, 'b : 'a> = RingBuffer<'a, PacketBuffer<'b>>;
|
2016-12-15 01:39:44 +08:00
|
|
|
|
|
|
|
/// An User Datagram Protocol socket.
|
2016-12-15 13:15:00 +08:00
|
|
|
///
|
|
|
|
/// An UDP socket is bound to a specific endpoint, and owns transmit and receive
|
|
|
|
/// packet buffers.
|
2017-01-12 12:28:34 +08:00
|
|
|
#[derive(Debug)]
|
2016-12-17 14:27:08 +08:00
|
|
|
pub struct UdpSocket<'a, 'b: 'a> {
|
2017-07-28 06:55:30 +08:00
|
|
|
debug_id: usize,
|
2016-12-20 21:54:11 +08:00
|
|
|
endpoint: IpEndpoint,
|
2016-12-20 21:56:59 +08:00
|
|
|
rx_buffer: SocketBuffer<'a, 'b>,
|
2017-01-17 07:35:21 +08:00
|
|
|
tx_buffer: SocketBuffer<'a, 'b>,
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-17 14:27:08 +08:00
|
|
|
impl<'a, 'b> UdpSocket<'a, 'b> {
|
2016-12-15 01:39:44 +08:00
|
|
|
/// Create an UDP socket with the given buffers.
|
2017-01-11 13:25:54 +08:00
|
|
|
pub fn new(rx_buffer: SocketBuffer<'a, 'b>,
|
|
|
|
tx_buffer: SocketBuffer<'a, 'b>) -> Socket<'a, 'b> {
|
2016-12-23 15:30:57 +08:00
|
|
|
Socket::Udp(UdpSocket {
|
2017-07-28 06:55:30 +08:00
|
|
|
debug_id: 0,
|
2017-01-11 12:44:42 +08:00
|
|
|
endpoint: IpEndpoint::default(),
|
2016-12-15 01:39:44 +08:00
|
|
|
rx_buffer: rx_buffer,
|
2017-01-17 07:35:21 +08:00
|
|
|
tx_buffer: tx_buffer,
|
2016-12-17 13:12:45 +08:00
|
|
|
})
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2017-01-17 07:35:21 +08:00
|
|
|
/// Return the debug identifier.
|
2017-07-05 02:46:36 +08:00
|
|
|
#[inline]
|
2017-01-17 07:35:21 +08:00
|
|
|
pub fn debug_id(&self) -> usize {
|
|
|
|
self.debug_id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the debug identifier.
|
|
|
|
///
|
|
|
|
/// The debug identifier is a number printed in socket trace messages.
|
|
|
|
/// It could as well be used by the user code.
|
|
|
|
pub fn set_debug_id(&mut self, id: usize) {
|
|
|
|
self.debug_id = id
|
|
|
|
}
|
|
|
|
|
2017-01-11 12:44:42 +08:00
|
|
|
/// Return the bound endpoint.
|
|
|
|
#[inline]
|
|
|
|
pub fn endpoint(&self) -> IpEndpoint {
|
|
|
|
self.endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Bind the socket to the given endpoint.
|
2017-07-27 22:53:06 +08:00
|
|
|
///
|
2017-07-30 09:17:48 +08:00
|
|
|
/// This function returns `Err(Error::Illegal)` if the socket was open
|
|
|
|
/// (see [is_open](#method.is_open)), and `Err(Error::Unaddressable)`
|
|
|
|
/// if the port in the given endpoint is zero.
|
2017-07-27 22:53:06 +08:00
|
|
|
pub fn bind<T: Into<IpEndpoint>>(&mut self, endpoint: T) -> Result<()> {
|
|
|
|
let endpoint = endpoint.into();
|
|
|
|
if endpoint.port == 0 { return Err(Error::Unaddressable) }
|
|
|
|
|
2017-07-30 09:17:48 +08:00
|
|
|
if self.is_open() { return Err(Error::Illegal) }
|
2017-07-27 22:53:06 +08:00
|
|
|
|
|
|
|
self.endpoint = endpoint;
|
|
|
|
Ok(())
|
2017-01-11 12:44:42 +08:00
|
|
|
}
|
|
|
|
|
2017-07-30 09:17:48 +08:00
|
|
|
/// Check whether the socket is open.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_open(&self) -> bool {
|
|
|
|
self.endpoint.port != 0
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:33:51 +08:00
|
|
|
/// Check whether the transmit buffer is full.
|
2017-07-05 02:46:36 +08:00
|
|
|
#[inline]
|
2017-01-11 14:33:51 +08:00
|
|
|
pub fn can_send(&self) -> bool {
|
2017-09-09 07:23:40 +08:00
|
|
|
!self.tx_buffer.is_full()
|
2017-01-11 14:33:51 +08:00
|
|
|
}
|
|
|
|
|
2017-01-14 14:51:29 +08:00
|
|
|
/// Check whether the receive buffer is not empty.
|
2017-07-05 02:46:36 +08:00
|
|
|
#[inline]
|
2017-01-14 14:51:29 +08:00
|
|
|
pub fn can_recv(&self) -> bool {
|
2017-09-09 07:23:40 +08:00
|
|
|
!self.rx_buffer.is_empty()
|
2017-01-14 14:51:29 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
/// Enqueue a packet to be sent to a given remote endpoint, and return a pointer
|
|
|
|
/// to its payload.
|
2016-12-15 01:39:44 +08:00
|
|
|
///
|
2017-07-27 22:53:06 +08:00
|
|
|
/// 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
|
|
|
|
/// size, and `Err(Error::Unaddressable)` if local or remote port, or remote address,
|
|
|
|
/// are unspecified.
|
2017-07-27 21:51:02 +08:00
|
|
|
pub fn send(&mut self, size: usize, endpoint: IpEndpoint) -> Result<&mut [u8]> {
|
2017-07-27 22:53:06 +08:00
|
|
|
if self.endpoint.port == 0 { return Err(Error::Unaddressable) }
|
|
|
|
if !endpoint.is_specified() { return Err(Error::Unaddressable) }
|
|
|
|
|
2017-09-08 05:17:31 +08:00
|
|
|
let packet_buf = self.tx_buffer.enqueue_one_with(|buf| buf.resize(size))?;
|
2016-12-16 01:07:56 +08:00
|
|
|
packet_buf.endpoint = endpoint;
|
2017-01-17 07:35:21 +08:00
|
|
|
net_trace!("[{}]{}:{}: buffer to send {} octets",
|
2017-07-27 22:53:06 +08:00
|
|
|
self.debug_id, self.endpoint, packet_buf.endpoint, size);
|
2016-12-17 13:12:45 +08:00
|
|
|
Ok(&mut packet_buf.as_mut()[..size])
|
|
|
|
}
|
|
|
|
|
2016-12-26 22:50:12 +08:00
|
|
|
/// Enqueue a packet to be sent to a given remote endpoint, and fill it from a slice.
|
2016-12-17 13:12:45 +08:00
|
|
|
///
|
|
|
|
/// See also [send](#method.send).
|
2017-07-29 16:19:32 +08:00
|
|
|
pub fn send_slice(&mut self, data: &[u8], endpoint: IpEndpoint) -> Result<()> {
|
2017-07-27 22:53:06 +08:00
|
|
|
self.send(data.len(), endpoint)?.copy_from_slice(data);
|
2017-07-29 16:19:32 +08:00
|
|
|
Ok(())
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
/// Dequeue a packet received from a remote endpoint, and return the endpoint as well
|
|
|
|
/// as a pointer to the payload.
|
2016-12-15 01:39:44 +08:00
|
|
|
///
|
2017-07-27 21:51:02 +08:00
|
|
|
/// This function returns `Err(Error::Exhausted)` if the receive buffer is empty.
|
|
|
|
pub fn recv(&mut self) -> Result<(&[u8], IpEndpoint)> {
|
2017-09-08 05:17:31 +08:00
|
|
|
let packet_buf = self.rx_buffer.dequeue_one()?;
|
2017-01-17 07:35:21 +08:00
|
|
|
net_trace!("[{}]{}:{}: receive {} buffered octets",
|
|
|
|
self.debug_id, self.endpoint,
|
|
|
|
packet_buf.endpoint, packet_buf.size);
|
2017-07-27 22:53:06 +08:00
|
|
|
Ok((&packet_buf.as_ref(), packet_buf.endpoint))
|
2016-12-17 13:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Dequeue a packet received from a remote endpoint, and return the endpoint as well
|
|
|
|
/// as copy the payload into the given slice.
|
|
|
|
///
|
2017-01-12 12:18:18 +08:00
|
|
|
/// See also [recv](#method.recv).
|
2017-07-27 21:51:02 +08:00
|
|
|
pub fn recv_slice(&mut self, data: &mut [u8]) -> Result<(usize, IpEndpoint)> {
|
2017-06-25 00:34:32 +08:00
|
|
|
let (buffer, endpoint) = self.recv()?;
|
2017-07-27 22:53:06 +08:00
|
|
|
let length = min(data.len(), buffer.len());
|
|
|
|
data[..length].copy_from_slice(&buffer[..length]);
|
|
|
|
Ok((length, endpoint))
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
2016-12-15 13:15:00 +08:00
|
|
|
|
2017-09-01 05:44:21 +08:00
|
|
|
pub(crate) fn accepts(&self, ip_repr: &IpRepr, repr: &UdpRepr) -> bool {
|
|
|
|
if self.endpoint.port != repr.dst_port { return false }
|
2017-08-01 19:08:48 +08:00
|
|
|
if !self.endpoint.addr.is_unspecified() &&
|
2017-09-01 05:44:21 +08:00
|
|
|
self.endpoint.addr != ip_repr.dst_addr() { return false }
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn process(&mut self, ip_repr: &IpRepr, repr: &UdpRepr) -> Result<()> {
|
|
|
|
debug_assert!(self.accepts(ip_repr, repr));
|
2016-12-15 13:15:00 +08:00
|
|
|
|
2017-09-08 05:17:31 +08:00
|
|
|
let packet_buf = self.rx_buffer.enqueue_one_with(|buf| buf.resize(repr.payload.len()))?;
|
2017-07-27 22:53:06 +08:00
|
|
|
packet_buf.as_mut().copy_from_slice(repr.payload);
|
2017-08-01 19:08:48 +08:00
|
|
|
packet_buf.endpoint = IpEndpoint { addr: ip_repr.src_addr(), port: repr.src_port };
|
2017-01-17 07:35:21 +08:00
|
|
|
net_trace!("[{}]{}:{}: receiving {} octets",
|
|
|
|
self.debug_id, self.endpoint,
|
|
|
|
packet_buf.endpoint, packet_buf.size);
|
2016-12-16 01:07:56 +08:00
|
|
|
Ok(())
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2017-08-28 12:09:17 +08:00
|
|
|
pub(crate) fn dispatch<F>(&mut self, emit: F) -> Result<()>
|
2017-08-28 11:50:40 +08:00
|
|
|
where F: FnOnce((IpRepr, UdpRepr)) -> Result<()> {
|
2017-08-30 03:35:09 +08:00
|
|
|
let debug_id = self.debug_id;
|
|
|
|
let endpoint = self.endpoint;
|
2017-09-08 05:17:31 +08:00
|
|
|
self.tx_buffer.dequeue_one_with(|packet_buf| {
|
2017-08-30 03:35:09 +08:00
|
|
|
net_trace!("[{}]{}:{}: sending {} octets",
|
|
|
|
debug_id, endpoint,
|
|
|
|
packet_buf.endpoint, packet_buf.size);
|
|
|
|
|
|
|
|
let repr = UdpRepr {
|
|
|
|
src_port: endpoint.port,
|
|
|
|
dst_port: packet_buf.endpoint.port,
|
|
|
|
payload: &packet_buf.as_ref()[..]
|
|
|
|
};
|
|
|
|
let ip_repr = IpRepr::Unspecified {
|
|
|
|
src_addr: endpoint.addr,
|
|
|
|
dst_addr: packet_buf.endpoint.addr,
|
|
|
|
protocol: IpProtocol::Udp,
|
|
|
|
payload_len: repr.buffer_len()
|
|
|
|
};
|
|
|
|
emit((ip_repr, repr))
|
|
|
|
})
|
|
|
|
}
|
2017-07-27 22:53:06 +08:00
|
|
|
|
2017-08-30 03:35:09 +08:00
|
|
|
pub(crate) fn poll_at(&self) -> Option<u64> {
|
2017-09-09 07:23:40 +08:00
|
|
|
if self.tx_buffer.is_empty() {
|
2017-08-30 03:35:09 +08:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(0)
|
|
|
|
}
|
2016-12-15 13:15:00 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-27 22:53:06 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use wire::{IpAddress, Ipv4Address, IpRepr, Ipv4Repr, UdpRepr};
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn buffer(packets: usize) -> SocketBuffer<'static, 'static> {
|
|
|
|
let mut storage = vec![];
|
|
|
|
for _ in 0..packets {
|
|
|
|
storage.push(PacketBuffer::new(vec![0; 16]))
|
|
|
|
}
|
|
|
|
SocketBuffer::new(storage)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn socket(rx_buffer: SocketBuffer<'static, 'static>,
|
|
|
|
tx_buffer: SocketBuffer<'static, 'static>)
|
|
|
|
-> UdpSocket<'static, 'static> {
|
|
|
|
match UdpSocket::new(rx_buffer, tx_buffer) {
|
|
|
|
Socket::Udp(socket) => socket,
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const LOCAL_IP: IpAddress = IpAddress::Ipv4(Ipv4Address([10, 0, 0, 1]));
|
|
|
|
const REMOTE_IP: IpAddress = IpAddress::Ipv4(Ipv4Address([10, 0, 0, 2]));
|
|
|
|
const LOCAL_PORT: u16 = 53;
|
|
|
|
const REMOTE_PORT: u16 = 49500;
|
|
|
|
const LOCAL_END: IpEndpoint = IpEndpoint { addr: LOCAL_IP, port: LOCAL_PORT };
|
|
|
|
const REMOTE_END: IpEndpoint = IpEndpoint { addr: REMOTE_IP, port: REMOTE_PORT };
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bind_unaddressable() {
|
|
|
|
let mut socket = socket(buffer(0), buffer(0));
|
|
|
|
assert_eq!(socket.bind(0), Err(Error::Unaddressable));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bind_twice() {
|
|
|
|
let mut socket = socket(buffer(0), buffer(0));
|
|
|
|
assert_eq!(socket.bind(1), Ok(()));
|
|
|
|
assert_eq!(socket.bind(2), Err(Error::Illegal));
|
|
|
|
}
|
|
|
|
|
|
|
|
const LOCAL_IP_REPR: IpRepr = IpRepr::Unspecified {
|
|
|
|
src_addr: LOCAL_IP,
|
|
|
|
dst_addr: REMOTE_IP,
|
|
|
|
protocol: IpProtocol::Udp,
|
|
|
|
payload_len: 8 + 6
|
|
|
|
};
|
|
|
|
const LOCAL_UDP_REPR: UdpRepr = UdpRepr {
|
|
|
|
src_port: LOCAL_PORT,
|
|
|
|
dst_port: REMOTE_PORT,
|
|
|
|
payload: b"abcdef"
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send_unaddressable() {
|
|
|
|
let mut socket = socket(buffer(0), buffer(1));
|
|
|
|
assert_eq!(socket.send_slice(b"abcdef", REMOTE_END), Err(Error::Unaddressable));
|
2017-07-31 15:59:01 +08:00
|
|
|
assert_eq!(socket.bind(LOCAL_PORT), Ok(()));
|
2017-07-27 22:53:06 +08:00
|
|
|
assert_eq!(socket.send_slice(b"abcdef",
|
|
|
|
IpEndpoint { addr: IpAddress::Unspecified, ..REMOTE_END }),
|
|
|
|
Err(Error::Unaddressable));
|
|
|
|
assert_eq!(socket.send_slice(b"abcdef",
|
|
|
|
IpEndpoint { port: 0, ..REMOTE_END }),
|
|
|
|
Err(Error::Unaddressable));
|
2017-07-29 16:19:32 +08:00
|
|
|
assert_eq!(socket.send_slice(b"abcdef", REMOTE_END), Ok(()));
|
2017-07-27 22:53:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send_truncated() {
|
|
|
|
let mut socket = socket(buffer(0), buffer(1));
|
2017-07-31 15:59:01 +08:00
|
|
|
assert_eq!(socket.bind(LOCAL_END), Ok(()));
|
|
|
|
|
2017-07-27 22:53:06 +08:00
|
|
|
assert_eq!(socket.send_slice(&[0; 32][..], REMOTE_END), Err(Error::Truncated));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send_dispatch() {
|
|
|
|
let mut socket = socket(buffer(0), buffer(1));
|
2017-07-31 15:59:01 +08:00
|
|
|
assert_eq!(socket.bind(LOCAL_END), Ok(()));
|
2017-07-27 22:53:06 +08:00
|
|
|
|
|
|
|
assert!(socket.can_send());
|
2017-08-28 12:09:17 +08:00
|
|
|
assert_eq!(socket.dispatch(|_| unreachable!()),
|
2017-08-28 11:50:40 +08:00
|
|
|
Err(Error::Exhausted));
|
2017-07-27 22:53:06 +08:00
|
|
|
|
2017-07-29 16:19:32 +08:00
|
|
|
assert_eq!(socket.send_slice(b"abcdef", REMOTE_END), Ok(()));
|
2017-07-27 22:53:06 +08:00
|
|
|
assert_eq!(socket.send_slice(b"123456", REMOTE_END), Err(Error::Exhausted));
|
|
|
|
assert!(!socket.can_send());
|
|
|
|
|
2017-08-28 12:09:17 +08:00
|
|
|
assert_eq!(socket.dispatch(|(ip_repr, udp_repr)| {
|
2017-08-28 11:50:40 +08:00
|
|
|
assert_eq!(ip_repr, LOCAL_IP_REPR);
|
|
|
|
assert_eq!(udp_repr, LOCAL_UDP_REPR);
|
2017-07-27 22:53:06 +08:00
|
|
|
Err(Error::Unaddressable)
|
2017-08-28 11:50:40 +08:00
|
|
|
}), Err(Error::Unaddressable));
|
2017-08-30 03:35:09 +08:00
|
|
|
assert!(!socket.can_send());
|
2017-07-27 22:53:06 +08:00
|
|
|
|
2017-08-28 12:09:17 +08:00
|
|
|
assert_eq!(socket.dispatch(|(ip_repr, udp_repr)| {
|
2017-08-28 11:50:40 +08:00
|
|
|
assert_eq!(ip_repr, LOCAL_IP_REPR);
|
|
|
|
assert_eq!(udp_repr, LOCAL_UDP_REPR);
|
2017-07-27 22:53:06 +08:00
|
|
|
Ok(())
|
2017-08-30 03:35:09 +08:00
|
|
|
}), Ok(()));
|
2017-07-27 22:53:06 +08:00
|
|
|
assert!(socket.can_send());
|
|
|
|
}
|
|
|
|
|
|
|
|
const REMOTE_IP_REPR: IpRepr = IpRepr::Ipv4(Ipv4Repr {
|
|
|
|
src_addr: Ipv4Address([10, 0, 0, 2]),
|
|
|
|
dst_addr: Ipv4Address([10, 0, 0, 1]),
|
|
|
|
protocol: IpProtocol::Udp,
|
|
|
|
payload_len: 8 + 6
|
|
|
|
});
|
|
|
|
const REMOTE_UDP_REPR: UdpRepr = UdpRepr {
|
|
|
|
src_port: REMOTE_PORT,
|
|
|
|
dst_port: LOCAL_PORT,
|
|
|
|
payload: b"abcdef"
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_recv_process() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(0));
|
2017-07-31 15:59:01 +08:00
|
|
|
assert_eq!(socket.bind(LOCAL_PORT), Ok(()));
|
2017-07-27 22:53:06 +08:00
|
|
|
|
2017-07-31 15:59:01 +08:00
|
|
|
assert!(!socket.can_recv());
|
2017-07-27 22:53:06 +08:00
|
|
|
assert_eq!(socket.recv(), Err(Error::Exhausted));
|
2017-07-31 15:59:01 +08:00
|
|
|
|
2017-09-01 05:44:21 +08:00
|
|
|
assert!(socket.accepts(&REMOTE_IP_REPR, &REMOTE_UDP_REPR));
|
2017-08-28 12:50:57 +08:00
|
|
|
assert_eq!(socket.process(&REMOTE_IP_REPR, &REMOTE_UDP_REPR),
|
2017-07-27 22:53:06 +08:00
|
|
|
Ok(()));
|
|
|
|
assert!(socket.can_recv());
|
|
|
|
|
2017-09-01 05:44:21 +08:00
|
|
|
assert!(socket.accepts(&REMOTE_IP_REPR, &REMOTE_UDP_REPR));
|
2017-08-28 12:50:57 +08:00
|
|
|
assert_eq!(socket.process(&REMOTE_IP_REPR, &REMOTE_UDP_REPR),
|
2017-07-27 22:53:06 +08:00
|
|
|
Err(Error::Exhausted));
|
|
|
|
assert_eq!(socket.recv(), Ok((&b"abcdef"[..], REMOTE_END)));
|
|
|
|
assert!(!socket.can_recv());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_recv_truncated_slice() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(0));
|
2017-07-31 15:59:01 +08:00
|
|
|
assert_eq!(socket.bind(LOCAL_PORT), Ok(()));
|
2017-07-27 22:53:06 +08:00
|
|
|
|
2017-09-01 05:44:21 +08:00
|
|
|
assert!(socket.accepts(&REMOTE_IP_REPR, &REMOTE_UDP_REPR));
|
2017-08-28 12:50:57 +08:00
|
|
|
assert_eq!(socket.process(&REMOTE_IP_REPR, &REMOTE_UDP_REPR),
|
|
|
|
Ok(()));
|
2017-07-27 22:53:06 +08:00
|
|
|
|
|
|
|
let mut slice = [0; 4];
|
|
|
|
assert_eq!(socket.recv_slice(&mut slice[..]), Ok((4, REMOTE_END)));
|
|
|
|
assert_eq!(&slice, b"abcd");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_recv_truncated_packet() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(0));
|
2017-07-31 15:59:01 +08:00
|
|
|
assert_eq!(socket.bind(LOCAL_PORT), Ok(()));
|
2017-07-27 22:53:06 +08:00
|
|
|
|
|
|
|
let udp_repr = UdpRepr { payload: &[0; 100][..], ..REMOTE_UDP_REPR };
|
2017-09-01 05:44:21 +08:00
|
|
|
assert!(socket.accepts(&REMOTE_IP_REPR, &udp_repr));
|
2017-08-28 12:50:57 +08:00
|
|
|
assert_eq!(socket.process(&REMOTE_IP_REPR, &udp_repr),
|
2017-07-27 22:53:06 +08:00
|
|
|
Err(Error::Truncated));
|
|
|
|
}
|
2017-09-01 05:44:21 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_doesnt_accept_wrong_port() {
|
|
|
|
let mut socket = socket(buffer(1), buffer(0));
|
|
|
|
assert_eq!(socket.bind(LOCAL_PORT), Ok(()));
|
|
|
|
|
|
|
|
let mut udp_repr = REMOTE_UDP_REPR;
|
|
|
|
assert!(socket.accepts(&REMOTE_IP_REPR, &udp_repr));
|
|
|
|
udp_repr.dst_port += 1;
|
|
|
|
assert!(!socket.accepts(&REMOTE_IP_REPR, &udp_repr));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_doesnt_accept_wrong_ip() {
|
|
|
|
let ip_repr = IpRepr::Ipv4(Ipv4Repr {
|
|
|
|
src_addr: Ipv4Address([10, 0, 0, 2]),
|
|
|
|
dst_addr: Ipv4Address([10, 0, 0, 10]),
|
|
|
|
protocol: IpProtocol::Udp,
|
|
|
|
payload_len: 8 + 6
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut port_bound_socket = socket(buffer(1), buffer(0));
|
|
|
|
assert_eq!(port_bound_socket.bind(LOCAL_PORT), Ok(()));
|
|
|
|
assert!(port_bound_socket.accepts(&ip_repr, &REMOTE_UDP_REPR));
|
|
|
|
|
|
|
|
let mut ip_bound_socket = socket(buffer(1), buffer(0));
|
|
|
|
assert_eq!(ip_bound_socket.bind(LOCAL_END), Ok(()));
|
|
|
|
assert!(!ip_bound_socket.accepts(&ip_repr, &REMOTE_UDP_REPR));
|
|
|
|
}
|
2017-07-27 22:53:06 +08:00
|
|
|
}
|