mod.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. use crate::net::{Iface, NET_DEVICES};
  2. use alloc::sync::Arc;
  3. use system_error::SystemError::{self, *};
  4. pub mod port;
  5. pub use port::PortManager;
  6. #[allow(dead_code)]
  7. #[derive(Debug, Clone, Copy, PartialEq)]
  8. pub enum Types {
  9. Raw,
  10. Icmp,
  11. Udp,
  12. Tcp,
  13. Dhcpv4,
  14. Dns,
  15. }
  16. /**
  17. * 目前,以下设计仍然没有考虑多网卡的listen问题,仅只解决了socket在绑定单网卡下的问题。
  18. */
  19. #[derive(Debug)]
  20. pub struct BoundInner {
  21. handle: smoltcp::iface::SocketHandle,
  22. iface: Arc<dyn Iface>,
  23. // inner: Vec<(smoltcp::iface::SocketHandle, Arc<dyn Iface>)>
  24. // address: smoltcp::wire::IpAddress,
  25. }
  26. impl BoundInner {
  27. /// # `bind`
  28. /// 将socket绑定到指定的地址上,置入指定的网络接口中
  29. pub fn bind<T>(
  30. socket: T,
  31. // socket_type: Types,
  32. address: &smoltcp::wire::IpAddress,
  33. ) -> Result<Self, SystemError>
  34. where
  35. T: smoltcp::socket::AnySocket<'static>,
  36. {
  37. if address.is_unspecified() {
  38. // 强绑VirtualIO
  39. let iface = NET_DEVICES
  40. .read_irqsave()
  41. .iter()
  42. .find_map(|(_, v)| {
  43. if v.common().is_default_iface() {
  44. Some(v.clone())
  45. } else {
  46. None
  47. }
  48. })
  49. .expect("No default interface");
  50. let handle = iface.sockets().lock_no_preempt().add(socket);
  51. return Ok(Self { handle, iface });
  52. } else {
  53. let iface = get_iface_to_bind(address).ok_or(ENODEV)?;
  54. let handle = iface.sockets().lock_no_preempt().add(socket);
  55. return Ok(Self { handle, iface });
  56. }
  57. }
  58. pub fn bind_ephemeral<T>(
  59. socket: T,
  60. // socket_type: Types,
  61. remote: smoltcp::wire::IpAddress,
  62. ) -> Result<(Self, smoltcp::wire::IpAddress), SystemError>
  63. where
  64. T: smoltcp::socket::AnySocket<'static>,
  65. {
  66. let (iface, address) = get_ephemeral_iface(&remote);
  67. // let bound_port = iface.port_manager().bind_ephemeral_port(socket_type)?;
  68. let handle = iface.sockets().lock_no_preempt().add(socket);
  69. // let endpoint = smoltcp::wire::IpEndpoint::new(local_addr, bound_port);
  70. Ok((Self { handle, iface }, address))
  71. }
  72. pub fn port_manager(&self) -> &PortManager {
  73. self.iface.port_manager()
  74. }
  75. pub fn with_mut<T: smoltcp::socket::AnySocket<'static>, R, F: FnMut(&mut T) -> R>(
  76. &self,
  77. mut f: F,
  78. ) -> R {
  79. f(self.iface.sockets().lock().get_mut::<T>(self.handle))
  80. }
  81. pub fn with<T: smoltcp::socket::AnySocket<'static>, R, F: Fn(&T) -> R>(&self, f: F) -> R {
  82. f(self.iface.sockets().lock().get::<T>(self.handle))
  83. }
  84. pub fn iface(&self) -> &Arc<dyn Iface> {
  85. &self.iface
  86. }
  87. pub fn release(&self) {
  88. self.iface.sockets().lock().remove(self.handle);
  89. }
  90. }
  91. #[inline]
  92. pub fn get_iface_to_bind(ip_addr: &smoltcp::wire::IpAddress) -> Option<Arc<dyn Iface>> {
  93. // log::debug!("get_iface_to_bind: {:?}", ip_addr);
  94. // if ip_addr.is_unspecified()
  95. crate::net::NET_DEVICES
  96. .read_irqsave()
  97. .iter()
  98. .find(|(_, iface)| {
  99. let guard = iface.smol_iface().lock();
  100. // log::debug!("iface name: {}, ip: {:?}", iface.iface_name(), guard.ip_addrs());
  101. return guard.has_ip_addr(*ip_addr);
  102. })
  103. .map(|(_, iface)| iface.clone())
  104. }
  105. /// Get a suitable iface to deal with sendto/connect request if the socket is not bound to an iface.
  106. /// If the remote address is the same as that of some iface, we will use the iface.
  107. /// Otherwise, we will use a default interface.
  108. fn get_ephemeral_iface(
  109. remote_ip_addr: &smoltcp::wire::IpAddress,
  110. ) -> (Arc<dyn Iface>, smoltcp::wire::IpAddress) {
  111. get_iface_to_bind(remote_ip_addr)
  112. .map(|iface| (iface, *remote_ip_addr))
  113. .or({
  114. let ifaces = NET_DEVICES.read_irqsave();
  115. ifaces.iter().find_map(|(_, iface)| {
  116. iface
  117. .smol_iface()
  118. .lock()
  119. .ip_addrs()
  120. .iter()
  121. .find(|cidr| cidr.contains_addr(remote_ip_addr))
  122. .map(|cidr| (iface.clone(), cidr.address()))
  123. })
  124. })
  125. .or({
  126. NET_DEVICES.read_irqsave().values().next().map(|iface| {
  127. (
  128. iface.clone(),
  129. iface.smol_iface().lock().ip_addrs()[0].address(),
  130. )
  131. })
  132. })
  133. .expect("No network interface")
  134. }