-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #101967 - jmillikin:linux-abstract-socket-addr, r=jos…
…htriplett Move `unix_socket_abstract` feature API to `SocketAddrExt`. The pre-stabilized API for abstract socket addresses exposes methods on `SocketAddr` that are only enabled for `cfg(any(target_os = "android", target_os = "linux"))`. Per discussion in <rust-lang/rust#85410>, moving these methods to an OS-specific extension trait is required before stabilization can be considered. This PR makes four changes: 1. The internal module `std::os::net` contains logic for the unstable feature `tcp_quickack` (rust-lang/rust#96256). I moved that code into `linux_ext/tcp.rs` and tried to adjust the module tree so it could accommodate a second unstable feature there. 2. Moves the public API out of `impl SocketAddr`, into `impl SocketAddrExt for SocketAddr` (the headline change). 3. The existing function names and docs for `unix_socket_abstract` refer to addresses as being created from abstract namespaces, but a more accurate description is that they create sockets in *the* abstract namespace. I adjusted the function signatures correspondingly and tried to update the docs to be clearer. 4. I also tweaked `from_abstract_name` so it takes an `AsRef<[u8]>` instead of `&[u8]`, allowing `b""` literals to be passed directly. Issues: 1. The public module `std::os::linux::net` is marked as part of `tcp_quickack`. I couldn't figure out how to mark a module as being part of two unstable features, so I just left the existing attributes in place. My hope is that this will be fixed as a side-effect of stabilizing either feature.
- Loading branch information
Showing
9 changed files
with
140 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
//! Linux and Android-specific definitions for socket options. | ||
//! Android-specific networking functionality. | ||
#![unstable(feature = "tcp_quickack", issue = "96256")] | ||
pub use crate::os::net::tcp::TcpStreamExt; | ||
|
||
#[unstable(feature = "unix_socket_abstract", issue = "85410")] | ||
pub use crate::os::net::linux_ext::addr::SocketAddrExt; | ||
|
||
#[unstable(feature = "tcp_quickack", issue = "96256")] | ||
pub use crate::os::net::linux_ext::tcp::TcpStreamExt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
//! Linux and Android-specific definitions for socket options. | ||
//! Linux-specific networking functionality. | ||
#![unstable(feature = "tcp_quickack", issue = "96256")] | ||
pub use crate::os::net::tcp::TcpStreamExt; | ||
|
||
#[unstable(feature = "unix_socket_abstract", issue = "85410")] | ||
pub use crate::os::net::linux_ext::addr::SocketAddrExt; | ||
|
||
#[unstable(feature = "tcp_quickack", issue = "96256")] | ||
pub use crate::os::net::linux_ext::tcp::TcpStreamExt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Linux and Android-specific extensions to socket addresses. | ||
use crate::os::unix::net::SocketAddr; | ||
use crate::sealed::Sealed; | ||
|
||
/// Platform-specific extensions to [`SocketAddr`]. | ||
#[unstable(feature = "unix_socket_abstract", issue = "85410")] | ||
pub trait SocketAddrExt: Sealed { | ||
/// Creates a Unix socket address in the abstract namespace. | ||
/// | ||
/// The abstract namespace is a Linux-specific extension that allows Unix | ||
/// sockets to be bound without creating an entry in the filesystem. | ||
/// Abstract sockets are unaffected by filesystem layout or permissions, | ||
/// and no cleanup is necessary when the socket is closed. | ||
/// | ||
/// An abstract socket address name may contain any bytes, including zero. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns an error if the name is longer than `SUN_LEN - 1`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(unix_socket_abstract)] | ||
/// use std::os::unix::net::{UnixListener, SocketAddr}; | ||
/// use std::os::linux::net::SocketAddrExt; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let addr = SocketAddr::from_abstract_name(b"hidden")?; | ||
/// let listener = match UnixListener::bind_addr(&addr) { | ||
/// Ok(sock) => sock, | ||
/// Err(err) => { | ||
/// println!("Couldn't bind: {err:?}"); | ||
/// return Err(err); | ||
/// } | ||
/// }; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
fn from_abstract_name<N>(name: &N) -> crate::io::Result<SocketAddr> | ||
where | ||
N: AsRef<[u8]>; | ||
|
||
/// Returns the contents of this address if it is in the abstract namespace. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(unix_socket_abstract)] | ||
/// use std::os::unix::net::{UnixListener, SocketAddr}; | ||
/// use std::os::linux::net::SocketAddrExt; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let name = b"hidden"; | ||
/// let name_addr = SocketAddr::from_abstract_name(name)?; | ||
/// let socket = UnixListener::bind_addr(&name_addr)?; | ||
/// let local_addr = socket.local_addr().expect("Couldn't get local address"); | ||
/// assert_eq!(local_addr.as_abstract_name(), Some(&name[..])); | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
fn as_abstract_name(&self) -> Option<&[u8]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//! Linux and Android-specific networking functionality. | ||
#![doc(cfg(any(target_os = "linux", target_os = "android")))] | ||
|
||
#[unstable(feature = "unix_socket_abstract", issue = "85410")] | ||
pub(crate) mod addr; | ||
|
||
#[unstable(feature = "tcp_quickack", issue = "96256")] | ||
pub(crate) mod tcp; | ||
|
||
#[cfg(test)] | ||
mod tests; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
//! Linux and Android-specific definitions for socket options. | ||
//! OS-specific networking functionality. | ||
#![unstable(feature = "tcp_quickack", issue = "96256")] | ||
#![doc(cfg(any(target_os = "linux", target_os = "android",)))] | ||
pub mod tcp; | ||
#[cfg(test)] | ||
mod tests; | ||
#[cfg(any(target_os = "linux", target_os = "android", doc))] | ||
pub(super) mod linux_ext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters