123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- use crate::net::{Iface, NET_DEVICES};
- use alloc::sync::Arc;
- use system_error::SystemError::{self, *};
- pub mod port;
- pub use port::PortManager;
- #[allow(dead_code)]
- #[derive(Debug, Clone, Copy, PartialEq)]
- pub enum Types {
- Raw,
- Icmp,
- Udp,
- Tcp,
- Dhcpv4,
- Dns,
- }
- #[derive(Debug)]
- pub struct BoundInner {
- handle: smoltcp::iface::SocketHandle,
- iface: Arc<dyn Iface>,
-
-
- }
- impl BoundInner {
-
-
- pub fn bind<T>(
- socket: T,
-
- address: &smoltcp::wire::IpAddress,
- ) -> Result<Self, SystemError>
- where
- T: smoltcp::socket::AnySocket<'static>,
- {
- if address.is_unspecified() {
-
- let iface = NET_DEVICES
- .read_irqsave()
- .iter()
- .find_map(|(_, v)| {
- if v.common().is_default_iface() {
- Some(v.clone())
- } else {
- None
- }
- })
- .expect("No default interface");
- let handle = iface.sockets().lock_no_preempt().add(socket);
- return Ok(Self { handle, iface });
- } else {
- let iface = get_iface_to_bind(address).ok_or(ENODEV)?;
- let handle = iface.sockets().lock_no_preempt().add(socket);
- return Ok(Self { handle, iface });
- }
- }
- pub fn bind_ephemeral<T>(
- socket: T,
-
- remote: smoltcp::wire::IpAddress,
- ) -> Result<(Self, smoltcp::wire::IpAddress), SystemError>
- where
- T: smoltcp::socket::AnySocket<'static>,
- {
- let (iface, address) = get_ephemeral_iface(&remote);
-
- let handle = iface.sockets().lock_no_preempt().add(socket);
-
- Ok((Self { handle, iface }, address))
- }
- pub fn port_manager(&self) -> &PortManager {
- self.iface.port_manager()
- }
- pub fn with_mut<T: smoltcp::socket::AnySocket<'static>, R, F: FnMut(&mut T) -> R>(
- &self,
- mut f: F,
- ) -> R {
- f(self.iface.sockets().lock().get_mut::<T>(self.handle))
- }
- pub fn with<T: smoltcp::socket::AnySocket<'static>, R, F: Fn(&T) -> R>(&self, f: F) -> R {
- f(self.iface.sockets().lock().get::<T>(self.handle))
- }
- pub fn iface(&self) -> &Arc<dyn Iface> {
- &self.iface
- }
- pub fn release(&self) {
- self.iface.sockets().lock().remove(self.handle);
- }
- }
- #[inline]
- pub fn get_iface_to_bind(ip_addr: &smoltcp::wire::IpAddress) -> Option<Arc<dyn Iface>> {
-
-
- crate::net::NET_DEVICES
- .read_irqsave()
- .iter()
- .find(|(_, iface)| {
- let guard = iface.smol_iface().lock();
-
- return guard.has_ip_addr(*ip_addr);
- })
- .map(|(_, iface)| iface.clone())
- }
- fn get_ephemeral_iface(
- remote_ip_addr: &smoltcp::wire::IpAddress,
- ) -> (Arc<dyn Iface>, smoltcp::wire::IpAddress) {
- get_iface_to_bind(remote_ip_addr)
- .map(|iface| (iface, *remote_ip_addr))
- .or({
- let ifaces = NET_DEVICES.read_irqsave();
- ifaces.iter().find_map(|(_, iface)| {
- iface
- .smol_iface()
- .lock()
- .ip_addrs()
- .iter()
- .find(|cidr| cidr.contains_addr(remote_ip_addr))
- .map(|cidr| (iface.clone(), cidr.address()))
- })
- })
- .or({
- NET_DEVICES.read_irqsave().values().next().map(|iface| {
- (
- iface.clone(),
- iface.smol_iface().lock().ip_addrs()[0].address(),
- )
- })
- })
- .expect("No network interface")
- }
|