mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-23 09:23:25 +00:00
Record the original bound name
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
6b50d28ba1
commit
f499f54cf5
@ -5,14 +5,14 @@ use crate::{fs::path::Dentry, net::socket::util::socket_addr::SocketAddr, prelud
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum UnixSocketAddr {
|
||||
Unnamed,
|
||||
Path(String),
|
||||
Abstract(Vec<u8>),
|
||||
Path(Arc<str>),
|
||||
Abstract(Arc<[u8]>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(super) enum UnixSocketAddrBound {
|
||||
Path(Arc<Dentry>),
|
||||
Abstract(Vec<u8>),
|
||||
Path(Arc<str>, Arc<Dentry>),
|
||||
Abstract(Arc<[u8]>),
|
||||
}
|
||||
|
||||
impl TryFrom<SocketAddr> for UnixSocketAddr {
|
||||
@ -29,10 +29,7 @@ impl TryFrom<SocketAddr> for UnixSocketAddr {
|
||||
impl From<UnixSocketAddrBound> for UnixSocketAddr {
|
||||
fn from(value: UnixSocketAddrBound) -> Self {
|
||||
match value {
|
||||
UnixSocketAddrBound::Path(dentry) => {
|
||||
let abs_path = dentry.abs_path();
|
||||
Self::Path(abs_path)
|
||||
}
|
||||
UnixSocketAddrBound::Path(path, _) => Self::Path(path),
|
||||
UnixSocketAddrBound::Abstract(name) => Self::Abstract(name),
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ impl Init {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn bind(&mut self, addr_to_bind: &UnixSocketAddr) -> Result<()> {
|
||||
pub(super) fn bind(&mut self, addr_to_bind: UnixSocketAddr) -> Result<()> {
|
||||
if self.addr.is_some() {
|
||||
return_errno_with_message!(Errno::EINVAL, "the socket is already bound");
|
||||
}
|
||||
@ -35,8 +35,8 @@ impl Init {
|
||||
UnixSocketAddr::Unnamed => todo!(),
|
||||
UnixSocketAddr::Abstract(_) => todo!(),
|
||||
UnixSocketAddr::Path(path) => {
|
||||
let dentry = create_socket_file(path)?;
|
||||
UnixSocketAddrBound::Path(dentry)
|
||||
let dentry = create_socket_file(&path)?;
|
||||
UnixSocketAddrBound::Path(path, dentry)
|
||||
}
|
||||
};
|
||||
self.addr = Some(bound_addr);
|
||||
|
@ -82,7 +82,7 @@ impl BacklogTable {
|
||||
|
||||
fn add_backlog(&self, addr: &UnixSocketAddrBound, backlog: usize) -> Result<()> {
|
||||
let inode = {
|
||||
let UnixSocketAddrBound::Path(dentry) = addr else {
|
||||
let UnixSocketAddrBound::Path(_, dentry) = addr else {
|
||||
todo!()
|
||||
};
|
||||
create_keyable_inode(dentry)
|
||||
@ -99,7 +99,7 @@ impl BacklogTable {
|
||||
|
||||
fn get_backlog(&self, addr: &UnixSocketAddrBound) -> Result<Arc<Backlog>> {
|
||||
let inode = {
|
||||
let UnixSocketAddrBound::Path(dentry) = addr else {
|
||||
let UnixSocketAddrBound::Path(_, dentry) = addr else {
|
||||
todo!()
|
||||
};
|
||||
create_keyable_inode(dentry)
|
||||
@ -134,7 +134,7 @@ impl BacklogTable {
|
||||
}
|
||||
|
||||
fn remove_backlog(&self, addr: &UnixSocketAddrBound) {
|
||||
let UnixSocketAddrBound::Path(dentry) = addr else {
|
||||
let UnixSocketAddrBound::Path(_, dentry) = addr else {
|
||||
todo!()
|
||||
};
|
||||
|
||||
|
@ -198,7 +198,7 @@ impl Socket for UnixStreamSocket {
|
||||
let addr = UnixSocketAddr::try_from(socket_addr)?;
|
||||
|
||||
match &mut *self.state.write() {
|
||||
State::Init(init) => init.bind(&addr),
|
||||
State::Init(init) => init.bind(addr),
|
||||
_ => return_errno_with_message!(
|
||||
Errno::EINVAL,
|
||||
"cannot bind a listening or connected socket"
|
||||
@ -217,7 +217,7 @@ impl Socket for UnixStreamSocket {
|
||||
}
|
||||
UnixSocketAddr::Path(path) => {
|
||||
let dentry = lookup_socket_file(&path)?;
|
||||
UnixSocketAddrBound::Path(dentry)
|
||||
UnixSocketAddrBound::Path(path, dentry)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -91,16 +91,16 @@ pub(super) fn from_c_bytes(bytes: &[u8]) -> Result<UnixSocketAddr> {
|
||||
}
|
||||
|
||||
if sun_path[0] == 0 {
|
||||
return Ok(UnixSocketAddr::Abstract(Vec::from(&sun_path[1..])));
|
||||
return Ok(UnixSocketAddr::Abstract(Arc::from(&sun_path[1..])));
|
||||
}
|
||||
|
||||
// Again, Linux always appends a null terminator to the pathname if none is supplied. So we
|
||||
// need to deal with the case where `CStr::from_bytes_until_nul` fails.
|
||||
if let Ok(c_str) = CStr::from_bytes_until_nul(sun_path) {
|
||||
Ok(UnixSocketAddr::Path(c_str.to_string_lossy().to_string()))
|
||||
Ok(UnixSocketAddr::Path(Arc::from(c_str.to_string_lossy())))
|
||||
} else {
|
||||
Ok(UnixSocketAddr::Path(
|
||||
String::from_utf8_lossy(sun_path).to_string(),
|
||||
))
|
||||
Ok(UnixSocketAddr::Path(Arc::from(String::from_utf8_lossy(
|
||||
sun_path,
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
@ -88,12 +88,12 @@ static int sk_accepted;
|
||||
#define UNNAMED_ADDRLEN PATH_OFFSET
|
||||
|
||||
#define BOUND_ADDR \
|
||||
((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "/tmp/B0" })
|
||||
#define BOUND_ADDRLEN (PATH_OFFSET + 8)
|
||||
((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "//tmp/B0" })
|
||||
#define BOUND_ADDRLEN (PATH_OFFSET + 9)
|
||||
|
||||
#define LISTEN_ADDR \
|
||||
((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "/tmp/L0" })
|
||||
#define LISTEN_ADDRLEN (PATH_OFFSET + 8)
|
||||
((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "/tmp//L0" })
|
||||
#define LISTEN_ADDRLEN (PATH_OFFSET + 9)
|
||||
|
||||
FN_SETUP(unbound)
|
||||
{
|
||||
|
Reference in New Issue
Block a user