2016-12-15 01:39:44 +08:00
|
|
|
use Error;
|
2016-12-17 12:15:12 +08:00
|
|
|
use Managed;
|
2016-12-20 21:54:11 +08:00
|
|
|
use wire::{IpAddress, IpProtocol, IpEndpoint};
|
2016-12-15 13:15:00 +08:00
|
|
|
use wire::{UdpPacket, UdpRepr};
|
|
|
|
use socket::{Socket, PacketRepr};
|
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-21 03:51:52 +08:00
|
|
|
endpoint: IpEndpoint::UNSPECIFIED,
|
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]
|
|
|
|
}
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-19 03:40:11 +08:00
|
|
|
/// An UDP packet ring buffer.
|
2016-12-16 01:07:56 +08:00
|
|
|
#[derive(Debug)]
|
2016-12-20 21:56:59 +08:00
|
|
|
pub struct SocketBuffer<'a, 'b: 'a> {
|
|
|
|
storage: Managed<'a, [PacketBuffer<'b>]>,
|
2016-12-16 01:07:56 +08:00
|
|
|
read_at: usize,
|
2016-12-17 12:15:12 +08:00
|
|
|
length: usize
|
2016-12-16 01:07:56 +08:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:56:59 +08:00
|
|
|
impl<'a, 'b> SocketBuffer<'a, 'b> {
|
2016-12-16 01:07:56 +08:00
|
|
|
/// Create a packet buffer with the given storage.
|
2016-12-20 21:56:59 +08:00
|
|
|
pub fn new<T>(storage: T) -> SocketBuffer<'a, 'b>
|
|
|
|
where T: Into<Managed<'a, [PacketBuffer<'b>]>> {
|
2016-12-17 12:15:12 +08:00
|
|
|
let mut storage = storage.into();
|
|
|
|
for elem in storage.iter_mut() {
|
2016-12-16 01:07:56 +08:00
|
|
|
elem.endpoint = Default::default();
|
|
|
|
elem.size = 0;
|
|
|
|
}
|
2016-12-15 01:39:44 +08:00
|
|
|
|
2016-12-20 21:56:59 +08:00
|
|
|
SocketBuffer {
|
2016-12-16 01:07:56 +08:00
|
|
|
storage: storage,
|
|
|
|
read_at: 0,
|
2016-12-17 12:15:12 +08:00
|
|
|
length: 0
|
2016-12-16 01:07:56 +08:00
|
|
|
}
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
fn mask(&self, index: usize) -> usize {
|
2016-12-17 12:15:12 +08:00
|
|
|
index % self.storage.len()
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
fn incr(&self, index: usize) -> usize {
|
|
|
|
self.mask(index + 1)
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
fn empty(&self) -> bool {
|
|
|
|
self.length == 0
|
|
|
|
}
|
2016-12-15 01:39:44 +08:00
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
fn full(&self) -> bool {
|
2016-12-17 12:15:12 +08:00
|
|
|
self.length == self.storage.len()
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
/// Enqueue an element into the buffer, and return a pointer to it, or return
|
|
|
|
/// `Err(Error::Exhausted)` if the buffer is full.
|
2016-12-20 21:56:59 +08:00
|
|
|
pub fn enqueue(&mut self) -> Result<&mut PacketBuffer<'b>, Error> {
|
2016-12-16 01:07:56 +08:00
|
|
|
if self.full() {
|
|
|
|
Err(Error::Exhausted)
|
|
|
|
} else {
|
|
|
|
let index = self.mask(self.read_at + self.length);
|
2016-12-19 03:40:02 +08:00
|
|
|
let result = &mut self.storage[index];
|
2016-12-16 01:07:56 +08:00
|
|
|
self.length += 1;
|
|
|
|
Ok(result)
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
/// Dequeue an element from the buffer, and return a pointer to it, or return
|
|
|
|
/// `Err(Error::Exhausted)` if the buffer is empty.
|
2016-12-20 21:56:59 +08:00
|
|
|
pub fn dequeue(&mut self) -> Result<&PacketBuffer<'b>, Error> {
|
2016-12-16 01:07:56 +08:00
|
|
|
if self.empty() {
|
|
|
|
Err(Error::Exhausted)
|
|
|
|
} else {
|
|
|
|
self.length -= 1;
|
2016-12-17 12:15:12 +08:00
|
|
|
let result = &self.storage[self.read_at];
|
2016-12-16 01:07:56 +08:00
|
|
|
self.read_at = self.incr(self.read_at);
|
|
|
|
Ok(result)
|
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.
|
2016-12-17 14:27:08 +08:00
|
|
|
pub struct UdpSocket<'a, 'b: 'a> {
|
2016-12-20 21:54:11 +08:00
|
|
|
endpoint: IpEndpoint,
|
2016-12-20 21:56:59 +08:00
|
|
|
rx_buffer: SocketBuffer<'a, 'b>,
|
|
|
|
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.
|
2016-12-20 21:56:59 +08:00
|
|
|
pub fn new(endpoint: IpEndpoint,
|
|
|
|
rx_buffer: SocketBuffer<'a, 'b>, tx_buffer: SocketBuffer<'a, 'b>)
|
2016-12-17 14:27:08 +08:00
|
|
|
-> Socket<'a, 'b> {
|
2016-12-21 06:57:21 +08:00
|
|
|
Socket::UdpSocket(UdpSocket {
|
2016-12-15 01:39:44 +08:00
|
|
|
endpoint: endpoint,
|
|
|
|
rx_buffer: rx_buffer,
|
|
|
|
tx_buffer: tx_buffer
|
2016-12-17 13:12:45 +08:00
|
|
|
})
|
2016-12-15 01:39:44 +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
|
|
|
///
|
2016-12-16 01:07:56 +08:00
|
|
|
/// This function returns `Err(Error::Exhausted)` if the size is greater than what
|
|
|
|
/// the transmit buffer can accomodate.
|
2016-12-20 21:54:11 +08:00
|
|
|
pub fn send(&mut self, endpoint: IpEndpoint, size: usize) -> Result<&mut [u8], Error> {
|
2016-12-16 01:07:56 +08:00
|
|
|
let packet_buf = try!(self.tx_buffer.enqueue());
|
|
|
|
packet_buf.endpoint = endpoint;
|
|
|
|
packet_buf.size = size;
|
2016-12-17 13:12:45 +08:00
|
|
|
Ok(&mut packet_buf.as_mut()[..size])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enqueue a packete to be sent to a given remote endpoint, and fill it from a slice.
|
|
|
|
///
|
|
|
|
/// See also [send](#method.send).
|
2016-12-20 21:54:11 +08:00
|
|
|
pub fn send_slice(&mut self, endpoint: IpEndpoint, data: &[u8]) -> Result<(), Error> {
|
2016-12-17 13:12:45 +08:00
|
|
|
let buffer = try!(self.send(endpoint, data.len()));
|
|
|
|
Ok(buffer.copy_from_slice(data))
|
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
|
|
|
///
|
2016-12-16 01:07:56 +08:00
|
|
|
/// This function returns `Err(Error::Exhausted)` if the receive buffer is empty.
|
2016-12-20 21:54:11 +08:00
|
|
|
pub fn recv(&mut self) -> Result<(IpEndpoint, &[u8]), Error> {
|
2016-12-16 01:07:56 +08:00
|
|
|
let packet_buf = try!(self.rx_buffer.dequeue());
|
2016-12-17 13:12:45 +08:00
|
|
|
Ok((packet_buf.endpoint, &packet_buf.as_ref()[..packet_buf.size]))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dequeue a packet received from a remote endpoint, and return the endpoint as well
|
|
|
|
/// as copy the payload into the given slice.
|
|
|
|
///
|
|
|
|
/// This function returns `Err(Error::Exhausted)` if the received packet has payload
|
|
|
|
/// larger than the provided slice. See also [recv](#method.recv).
|
2016-12-20 21:54:11 +08:00
|
|
|
pub fn recv_slice(&mut self, data: &mut [u8]) -> Result<(IpEndpoint, usize), Error> {
|
2016-12-17 13:12:45 +08:00
|
|
|
let (endpoint, buffer) = try!(self.recv());
|
|
|
|
if data.len() < buffer.len() { return Err(Error::Exhausted) }
|
|
|
|
data[..buffer.len()].copy_from_slice(buffer);
|
|
|
|
Ok((endpoint, buffer.len()))
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
2016-12-15 13:15:00 +08:00
|
|
|
|
2016-12-17 13:12:45 +08:00
|
|
|
/// See [Socket::collect](enum.Socket.html#method.collect).
|
2016-12-20 21:54:11 +08:00
|
|
|
pub fn collect(&mut self, src_addr: &IpAddress, dst_addr: &IpAddress,
|
|
|
|
protocol: IpProtocol, payload: &[u8])
|
2016-12-15 13:15:00 +08:00
|
|
|
-> Result<(), Error> {
|
2016-12-20 21:54:11 +08:00
|
|
|
if protocol != IpProtocol::Udp { return Err(Error::Rejected) }
|
2016-12-15 13:15:00 +08:00
|
|
|
|
|
|
|
let packet = try!(UdpPacket::new(payload));
|
|
|
|
let repr = try!(UdpRepr::parse(&packet, src_addr, dst_addr));
|
2016-12-15 01:39:44 +08:00
|
|
|
|
|
|
|
if repr.dst_port != self.endpoint.port { return Err(Error::Rejected) }
|
|
|
|
if !self.endpoint.addr.is_unspecified() {
|
2016-12-15 13:15:00 +08:00
|
|
|
if self.endpoint.addr != *dst_addr { return Err(Error::Rejected) }
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
2016-12-15 13:15:00 +08:00
|
|
|
|
2016-12-16 01:07:56 +08:00
|
|
|
let packet_buf = try!(self.rx_buffer.enqueue());
|
2016-12-20 21:54:11 +08:00
|
|
|
packet_buf.endpoint = IpEndpoint { addr: *src_addr, port: repr.src_port };
|
2016-12-16 01:07:56 +08:00
|
|
|
packet_buf.size = repr.payload.len();
|
2016-12-17 13:12:45 +08:00
|
|
|
packet_buf.as_mut()[..repr.payload.len()].copy_from_slice(repr.payload);
|
2016-12-16 01:07:56 +08:00
|
|
|
Ok(())
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-17 13:12:45 +08:00
|
|
|
/// See [Socket::dispatch](enum.Socket.html#method.dispatch).
|
2016-12-20 21:54:11 +08:00
|
|
|
pub fn dispatch(&mut self, f: &mut FnMut(&IpAddress, &IpAddress,
|
|
|
|
IpProtocol, &PacketRepr) -> Result<(), Error>)
|
2016-12-15 13:15:00 +08:00
|
|
|
-> Result<(), Error> {
|
2016-12-16 01:07:56 +08:00
|
|
|
let packet_buf = try!(self.tx_buffer.dequeue());
|
|
|
|
f(&self.endpoint.addr,
|
|
|
|
&packet_buf.endpoint.addr,
|
2016-12-20 21:54:11 +08:00
|
|
|
IpProtocol::Udp,
|
2016-12-16 01:07:56 +08:00
|
|
|
&UdpRepr {
|
|
|
|
src_port: self.endpoint.port,
|
|
|
|
dst_port: packet_buf.endpoint.port,
|
2016-12-17 13:12:45 +08:00
|
|
|
payload: &packet_buf.as_ref()[..]
|
2016-12-16 01:07:56 +08:00
|
|
|
})
|
2016-12-15 01:39:44 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-15 13:15:00 +08:00
|
|
|
|
|
|
|
impl<'a> PacketRepr for UdpRepr<'a> {
|
2016-12-20 07:50:04 +08:00
|
|
|
fn buffer_len(&self) -> usize {
|
|
|
|
self.buffer_len()
|
2016-12-15 13:15:00 +08:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:11 +08:00
|
|
|
fn emit(&self, src_addr: &IpAddress, dst_addr: &IpAddress, payload: &mut [u8]) {
|
|
|
|
let mut packet = UdpPacket::new(payload).expect("undersized payload");
|
2016-12-15 13:15:00 +08:00
|
|
|
self.emit(&mut packet, src_addr, dst_addr)
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 01:07:56 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_buffer() {
|
|
|
|
let mut storage = vec![];
|
|
|
|
for _ in 0..5 {
|
2016-12-20 21:56:59 +08:00
|
|
|
storage.push(PacketBuffer::new(vec![0]))
|
2016-12-16 01:07:56 +08:00
|
|
|
}
|
2016-12-20 21:56:59 +08:00
|
|
|
let mut buffer = SocketBuffer::new(&mut storage[..]);
|
2016-12-16 01:07:56 +08:00
|
|
|
|
|
|
|
assert_eq!(buffer.empty(), true);
|
|
|
|
assert_eq!(buffer.full(), false);
|
|
|
|
buffer.enqueue().unwrap().size = 1;
|
|
|
|
assert_eq!(buffer.empty(), false);
|
|
|
|
assert_eq!(buffer.full(), false);
|
|
|
|
buffer.enqueue().unwrap().size = 2;
|
|
|
|
buffer.enqueue().unwrap().size = 3;
|
|
|
|
assert_eq!(buffer.dequeue().unwrap().size, 1);
|
|
|
|
assert_eq!(buffer.dequeue().unwrap().size, 2);
|
|
|
|
buffer.enqueue().unwrap().size = 4;
|
|
|
|
buffer.enqueue().unwrap().size = 5;
|
|
|
|
buffer.enqueue().unwrap().size = 6;
|
|
|
|
buffer.enqueue().unwrap().size = 7;
|
|
|
|
assert_eq!(buffer.enqueue().unwrap_err(), Error::Exhausted);
|
|
|
|
assert_eq!(buffer.empty(), false);
|
|
|
|
assert_eq!(buffer.full(), true);
|
|
|
|
assert_eq!(buffer.dequeue().unwrap().size, 3);
|
|
|
|
assert_eq!(buffer.dequeue().unwrap().size, 4);
|
|
|
|
assert_eq!(buffer.dequeue().unwrap().size, 5);
|
|
|
|
assert_eq!(buffer.dequeue().unwrap().size, 6);
|
|
|
|
assert_eq!(buffer.dequeue().unwrap().size, 7);
|
|
|
|
assert_eq!(buffer.dequeue().unwrap_err(), Error::Exhausted);
|
|
|
|
assert_eq!(buffer.empty(), true);
|
|
|
|
assert_eq!(buffer.full(), false);
|
|
|
|
}
|
|
|
|
}
|