make tun dependency optional (#142)

* remove log crates
* remove udp/raw of smoltcp
* make tun as optional dependancy, compile for freebsd works
This commit is contained in:
Sijie.Sun
2024-06-11 09:09:32 +08:00
committed by GitHub
parent 8aa57ebc22
commit 34f832bbad
26 changed files with 310 additions and 488 deletions

View File

@@ -18,9 +18,9 @@ pub use smoltcp;
use smoltcp::{
iface::{Config, Interface, Routes},
time::{Duration, Instant},
wire::{HardwareAddress, IpAddress, IpCidr, IpProtocol, IpVersion},
wire::{HardwareAddress, IpAddress, IpCidr},
};
pub use socket::{RawSocket, TcpListener, TcpStream, UdpSocket};
pub use socket::{TcpListener, TcpStream};
pub use socket_allocator::BufferSize;
use tokio::sync::Notify;
@@ -155,19 +155,6 @@ impl Net {
)
.await
}
/// This function will create a new UDP socket and attempt to bind it to the `addr` provided.
pub async fn udp_bind(&self, addr: SocketAddr) -> io::Result<UdpSocket> {
let addr = self.set_address(addr);
UdpSocket::new(self.reactor.clone(), addr.into()).await
}
/// Creates a new raw socket.
pub async fn raw_socket(
&self,
ip_version: IpVersion,
ip_protocol: IpProtocol,
) -> io::Result<RawSocket> {
RawSocket::new(self.reactor.clone(), ip_version, ip_protocol).await
}
fn set_address(&self, mut addr: SocketAddr) -> SocketAddr {
if addr.ip().is_unspecified() {
addr.set_ip(match self.ip_addr.address() {

View File

@@ -153,8 +153,6 @@ impl Drop for Reactor {
for (_, socket) in self.socket_allocator.sockets().lock().iter_mut() {
match socket {
Socket::Tcp(tcp) => tcp.close(),
Socket::Raw(_) => {}
Socket::Udp(udp) => udp.close(),
#[allow(unreachable_patterns)]
_ => {}
}

View File

@@ -1,8 +1,8 @@
use super::{reactor::Reactor, socket_allocator::SocketHandle};
use futures::future::{self, poll_fn};
use futures::{ready, Stream};
pub use smoltcp::socket::{raw, tcp, udp};
use smoltcp::wire::{IpAddress, IpEndpoint, IpProtocol, IpVersion};
pub use smoltcp::socket::tcp;
use smoltcp::wire::{IpAddress, IpEndpoint};
use std::mem::replace;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::{
@@ -231,147 +231,3 @@ impl AsyncWrite for TcpStream {
Poll::Pending
}
}
/// A UDP socket.
pub struct UdpSocket {
handle: SocketHandle,
reactor: Arc<Reactor>,
local_addr: SocketAddr,
}
impl UdpSocket {
pub(super) async fn new(
reactor: Arc<Reactor>,
local_endpoint: IpEndpoint,
) -> io::Result<UdpSocket> {
let handle = reactor.socket_allocator().new_udp_socket();
{
let mut socket = reactor.get_socket::<udp::Socket>(*handle);
socket.bind(local_endpoint).map_err(map_err)?;
}
let local_addr = ep2sa(&local_endpoint);
Ok(UdpSocket {
handle,
reactor,
local_addr,
})
}
/// Note that on multiple calls to a poll_* method in the send direction, only the Waker from the Context passed to the most recent call will be scheduled to receive a wakeup.
pub fn poll_send_to(
&self,
cx: &mut Context<'_>,
buf: &[u8],
target: SocketAddr,
) -> Poll<io::Result<usize>> {
let mut socket = self.reactor.get_socket::<udp::Socket>(*self.handle);
let target_ip: IpEndpoint = target.into();
match socket.send_slice(buf, target_ip) {
// the buffer is full
Err(udp::SendError::BufferFull) => {}
r => {
r.map_err(map_err)?;
self.reactor.notify();
return Poll::Ready(Ok(buf.len()));
}
}
socket.register_send_waker(cx.waker());
Poll::Pending
}
/// See note on `poll_send_to`
pub async fn send_to(&self, buf: &[u8], target: SocketAddr) -> io::Result<usize> {
poll_fn(|cx| self.poll_send_to(cx, buf, target)).await
}
/// Note that on multiple calls to a poll_* method in the recv direction, only the Waker from the Context passed to the most recent call will be scheduled to receive a wakeup.
pub fn poll_recv_from(
&self,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<(usize, SocketAddr)>> {
let mut socket = self.reactor.get_socket::<udp::Socket>(*self.handle);
match socket.recv_slice(buf) {
// the buffer is empty
Err(udp::RecvError::Exhausted) => {}
r => {
let (size, metadata) = r.map_err(map_err)?;
self.reactor.notify();
return Poll::Ready(Ok((size, ep2sa(&metadata.endpoint))));
}
}
socket.register_recv_waker(cx.waker());
Poll::Pending
}
/// See note on `poll_recv_from`
pub async fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
poll_fn(|cx| self.poll_recv_from(cx, buf)).await
}
pub fn local_addr(&self) -> io::Result<SocketAddr> {
Ok(self.local_addr)
}
}
/// A raw socket.
pub struct RawSocket {
handle: SocketHandle,
reactor: Arc<Reactor>,
}
impl RawSocket {
pub(super) async fn new(
reactor: Arc<Reactor>,
ip_version: IpVersion,
ip_protocol: IpProtocol,
) -> io::Result<RawSocket> {
let handle = reactor
.socket_allocator()
.new_raw_socket(ip_version, ip_protocol);
Ok(RawSocket { handle, reactor })
}
/// Note that on multiple calls to a poll_* method in the send direction, only the Waker from the Context passed to the most recent call will be scheduled to receive a wakeup.
pub fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
let mut socket = self.reactor.get_socket::<raw::Socket>(*self.handle);
match socket.send_slice(buf) {
// the buffer is full
Err(raw::SendError::BufferFull) => {}
r => {
r.map_err(map_err)?;
self.reactor.notify();
return Poll::Ready(Ok(buf.len()));
}
}
socket.register_send_waker(cx.waker());
Poll::Pending
}
/// See note on `poll_send`
pub async fn send(&self, buf: &[u8]) -> io::Result<usize> {
poll_fn(|cx| self.poll_send(cx, buf)).await
}
/// Note that on multiple calls to a poll_* method in the recv direction, only the Waker from the Context passed to the most recent call will be scheduled to receive a wakeup.
pub fn poll_recv(&self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
let mut socket = self.reactor.get_socket::<raw::Socket>(*self.handle);
match socket.recv_slice(buf) {
// the buffer is empty
Err(raw::RecvError::Exhausted) => {}
r => {
let size = r.map_err(map_err)?;
return Poll::Ready(Ok(size));
}
}
socket.register_recv_waker(cx.waker());
Poll::Pending
}
/// See note on `poll_recv`
pub async fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
poll_fn(|cx| self.poll_recv(cx, buf)).await
}
}

View File

@@ -1,8 +1,7 @@
use parking_lot::Mutex;
use smoltcp::{
iface::{SocketHandle as InnerSocketHandle, SocketSet},
socket::{raw, tcp, udp},
wire::{IpProtocol, IpVersion},
socket::tcp,
};
use std::{
ops::{Deref, DerefMut},
@@ -14,14 +13,6 @@ use std::{
pub struct BufferSize {
pub tcp_rx_size: usize,
pub tcp_tx_size: usize,
pub udp_rx_size: usize,
pub udp_tx_size: usize,
pub udp_rx_meta_size: usize,
pub udp_tx_meta_size: usize,
pub raw_rx_size: usize,
pub raw_tx_size: usize,
pub raw_rx_meta_size: usize,
pub raw_tx_meta_size: usize,
}
impl Default for BufferSize {
@@ -29,14 +20,6 @@ impl Default for BufferSize {
BufferSize {
tcp_rx_size: 8192,
tcp_tx_size: 8192,
udp_rx_size: 8192,
udp_tx_size: 8192,
udp_rx_meta_size: 32,
udp_tx_meta_size: 32,
raw_rx_size: 8192,
raw_tx_size: 8192,
raw_rx_meta_size: 32,
raw_tx_meta_size: 32,
}
}
}
@@ -65,16 +48,6 @@ impl SocketAlloctor {
let handle = set.add(self.alloc_tcp_socket());
SocketHandle::new(handle, self.sockets.clone())
}
pub fn new_udp_socket(&self) -> SocketHandle {
let mut set = self.sockets.lock();
let handle = set.add(self.alloc_udp_socket());
SocketHandle::new(handle, self.sockets.clone())
}
pub fn new_raw_socket(&self, ip_version: IpVersion, ip_protocol: IpProtocol) -> SocketHandle {
let mut set = self.sockets.lock();
let handle = set.add(self.alloc_raw_socket(ip_version, ip_protocol));
SocketHandle::new(handle, self.sockets.clone())
}
fn alloc_tcp_socket(&self) -> tcp::Socket<'static> {
let rx_buffer = tcp::SocketBuffer::new(vec![0; self.buffer_size.tcp_rx_size]);
let tx_buffer = tcp::SocketBuffer::new(vec![0; self.buffer_size.tcp_tx_size]);
@@ -83,36 +56,6 @@ impl SocketAlloctor {
tcp
}
fn alloc_udp_socket(&self) -> udp::Socket<'static> {
let rx_buffer = udp::PacketBuffer::new(
vec![udp::PacketMetadata::EMPTY; self.buffer_size.udp_rx_meta_size],
vec![0; self.buffer_size.udp_rx_size],
);
let tx_buffer = udp::PacketBuffer::new(
vec![udp::PacketMetadata::EMPTY; self.buffer_size.udp_tx_meta_size],
vec![0; self.buffer_size.udp_tx_size],
);
let udp = udp::Socket::new(rx_buffer, tx_buffer);
udp
}
fn alloc_raw_socket(
&self,
ip_version: IpVersion,
ip_protocol: IpProtocol,
) -> raw::Socket<'static> {
let rx_buffer = raw::PacketBuffer::new(
vec![raw::PacketMetadata::EMPTY; self.buffer_size.raw_rx_meta_size],
vec![0; self.buffer_size.raw_rx_size],
);
let tx_buffer = raw::PacketBuffer::new(
vec![raw::PacketMetadata::EMPTY; self.buffer_size.raw_tx_meta_size],
vec![0; self.buffer_size.raw_tx_size],
);
let raw = raw::Socket::new(ip_version, ip_protocol, rx_buffer, tx_buffer);
raw
}
}
pub struct SocketHandle(InnerSocketHandle, SharedSocketSet);