Samuel Dai 69dde46586
fix(net): misc of resources release (#1096)
* fix: TCP socket miss activation after close

* fix: TCP socket miss activation after close (#1085)

* fix: loopback, udp resource aquire
- remove tcp useless status update
- enable smoltcp medium-ip feature
- change loopback device use ip for addressing, avoid arp procedure
- fix udp couldn't close bug
- fix udp resource aquire didn't lock port
- remove useless Timer in network initialization

* fmt: format

* fix: loopback and udp resource problem (#1086)

* fix: loopback, udp resource aquire
- remove tcp useless status update
- enable smoltcp medium-ip feature
- change loopback device use ip for addressing, avoid arp procedure
- fix udp couldn't close bug
- fix udp resource aquire didn't lock port
- remove useless Timer in network initialization

* fix(net): Unix 资源释放 (#1087)

* unix socket 相关资源释放 #991
* 完善streamsocket资源释放
* 解决inode和id不匹配

* fix TCP socketset release (#1095)

* fix: TCP socket miss activation after close

* fix: loopback, udp resource aquire
- remove tcp useless status update
- enable smoltcp medium-ip feature
- change loopback device use ip for addressing, avoid arp procedure
- fix udp couldn't close bug
- fix udp resource aquire didn't lock port
- remove useless Timer in network initialization

---------

Co-authored-by: YuLong Huang <139891737+LINGLUO00@users.noreply.github.com>
2025-03-10 12:58:39 +08:00

134 lines
3.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use core::{default, sync::atomic::AtomicU8};
use system_error::SystemError;
bitflags! {
/// @brief 用于指定socket的关闭类型
/// 参考https://code.dragonos.org.cn/xref/linux-6.1.9/include/net/sock.h?fi=SHUTDOWN_MASK#1573
pub struct ShutdownBit: u8 {
const SHUT_RD = 0;
const SHUT_WR = 1;
const SHUT_RDWR = 2;
}
}
const RCV_SHUTDOWN: u8 = 0x01;
const SEND_SHUTDOWN: u8 = 0x02;
const SHUTDOWN_MASK: u8 = 0x03;
#[derive(Debug, Default)]
pub struct Shutdown {
bit: AtomicU8,
}
impl From<ShutdownBit> for Shutdown {
fn from(shutdown_bit: ShutdownBit) -> Self {
match shutdown_bit {
ShutdownBit::SHUT_RD => Shutdown {
bit: AtomicU8::new(RCV_SHUTDOWN),
},
ShutdownBit::SHUT_WR => Shutdown {
bit: AtomicU8::new(SEND_SHUTDOWN),
},
ShutdownBit::SHUT_RDWR => Shutdown {
bit: AtomicU8::new(SHUTDOWN_MASK),
},
_ => Shutdown::default(),
}
}
}
impl Shutdown {
pub fn new() -> Self {
Self {
bit: AtomicU8::new(0),
}
}
pub fn recv_shutdown(&self) {
self.bit
.fetch_or(RCV_SHUTDOWN, core::sync::atomic::Ordering::SeqCst);
}
pub fn send_shutdown(&self) {
self.bit
.fetch_or(SEND_SHUTDOWN, core::sync::atomic::Ordering::SeqCst);
}
pub fn is_recv_shutdown(&self) -> bool {
self.bit.load(core::sync::atomic::Ordering::SeqCst) & RCV_SHUTDOWN != 0
}
pub fn is_send_shutdown(&self) -> bool {
self.bit.load(core::sync::atomic::Ordering::SeqCst) & SEND_SHUTDOWN != 0
}
pub fn is_both_shutdown(&self) -> bool {
self.bit.load(core::sync::atomic::Ordering::SeqCst) & SHUTDOWN_MASK == SHUTDOWN_MASK
}
pub fn is_empty(&self) -> bool {
self.bit.load(core::sync::atomic::Ordering::SeqCst) == 0
}
pub fn from_how(how: usize) -> Self {
Self::from(ShutdownBit::from_bits_truncate(how as u8))
}
pub fn get(&self) -> ShutdownTemp {
ShutdownTemp {
bit: self.bit.load(core::sync::atomic::Ordering::SeqCst),
}
}
}
pub struct ShutdownTemp {
bit: u8,
}
impl ShutdownTemp {
pub fn is_recv_shutdown(&self) -> bool {
self.bit & RCV_SHUTDOWN != 0
}
pub fn is_send_shutdown(&self) -> bool {
self.bit & SEND_SHUTDOWN != 0
}
pub fn is_both_shutdown(&self) -> bool {
self.bit & SHUTDOWN_MASK == SHUTDOWN_MASK
}
pub fn is_empty(&self) -> bool {
self.bit == 0
}
pub fn bits(&self) -> ShutdownBit {
ShutdownBit { bits: self.bit }
}
}
impl From<ShutdownBit> for ShutdownTemp {
fn from(shutdown_bit: ShutdownBit) -> Self {
match shutdown_bit {
ShutdownBit::SHUT_RD => Self { bit: RCV_SHUTDOWN },
ShutdownBit::SHUT_WR => Self { bit: SEND_SHUTDOWN },
ShutdownBit::SHUT_RDWR => Self { bit: SHUTDOWN_MASK },
_ => Self { bit: 0 },
}
}
}
impl TryFrom<usize> for ShutdownTemp {
type Error = SystemError;
fn try_from(value: usize) -> Result<Self, Self::Error> {
match value {
0..2 => Ok(ShutdownTemp {
bit: value as u8 + 1,
}),
_ => Err(SystemError::EINVAL),
}
}
}