-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add trait to reduce duplicated code between socket address types, implement netlink addresses #1004
base: main
Are you sure you want to change the base?
Conversation
f694272
to
4c1c746
Compare
As a quick update here, I am looking forward to reviewing this; I've just been busy. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach, it allows me to very easily define a socket address not yet present in this crate, like so
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)] // fields are never read directly because they get memcp'd into another struct
#[repr(C)]
struct SocketAddressHci {
hci_family: rustix::net::AddressFamily,
hci_dev: u16,
hci_channel: u16,
}
unsafe impl SocketAddress for SocketAddressHci {
/// SAFETY: `SocketAddressHci` has the same layout as the type in the kernel and can therefore safely be passed in a syscall
type CSockAddr = Self;
fn encode(&self) -> Self::CSockAddr {
self.clone()
}
}
I left some comment about the code structure, most of it more nit than anything else. I am interested to here your opinion on using something like a Bow
for the return value of encode
tho. It would allow impl's to avoid the clone without having to reimplement write_sockaddr
and with_sockaddr
again :)
4c1c746
to
013792a
Compare
f2b3389
to
44051ea
Compare
44051ea
to
9b432db
Compare
Given that I also rebased to clean up the messy history for easier review. Full history archived here. |
#918
This adds a trait
SockAddr
, and implements it forSocketAddrV4
,SocketAddrV6
,SocketAddrUnix
,SocketAddrXdp
,SocketAddr
,SocketAddrAny
, and the newly-addedSocketAddrNetlink
. The syscalls that take asockaddr
are reimplemented in terms of this trait, removing hundreds of lines of duplicated code. It also simplifies the public interface, allowingrustix::net::{bind, connect, sendto, sendmsg_addr}
to take an(addr: &impl SockAddr)
, rather than needing separate functionsbind_v4
,bind_v6
,bind_unix
,bind_xdp
,bind_any
etc. All of those are left as wrappers around the generic version, but could be deprecated and removed.Open questions
SocketAddrAny
: So far this only changes the syscalls that take a socket address as an in-pointer. Those that return an address via an out-pointer currently returnSocketAddrAny
. If it is desirable to allow types outside of rustix to implement theSocketAddress
trait, it would be ideal forSocketAddrAny
to wrapsockaddr_storage
instead of being a Rust enum. This would allowSocketAddrAny
be fully generic and allow these syscalls to be used with any address type not known to rustix. Address types could use.into()
/.try_into()
to convert to / fromSocketAddrAny
. This would require a breaking change.