Skip to content

Update to socket2 v0.6 #329

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tokio = { version = "1.2", features = ["net", "rt", "sync"] }
slab = "0.4.2"
libc = "0.2.80"
io-uring = "0.6.0"
socket2 = { version = "0.4.4", features = ["all"] }
socket2 = { version = "0.6.0", features = ["all"] }
bytes = { version = "1.0", optional = true }
futures-util = { version = "0.3.26", default-features = false, features = ["std"] }

Expand Down
6 changes: 4 additions & 2 deletions src/io/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ impl Completable for Accept {
let fd = SharedFd::new(fd as i32);
let socket = Socket { fd };
let (_, addr) = unsafe {
socket2::SockAddr::init(move |addr_storage, len| {
self.socketaddr.0.clone_into(&mut *addr_storage);
socket2::SockAddr::try_init(move |addr_storage, len| {
// SAFETY: socket2::SockAddrStorage contains
// libc::sockaddr_storage, so this cast is safe.
self.socketaddr.0.clone_into(&mut *addr_storage.cast());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We added a SockAddrStorage::view_as() method for exactly this kind of pointer cast.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that as well, but SockAddr::try_init uses a raw pointer view_as uses a mutable reference. We could dereference first, then view though if you prefer.

*len = self.socketaddr.1;
Ok(())
})?
Expand Down
2 changes: 1 addition & 1 deletion src/io/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Op<Connect> {
|connect| {
opcode::Connect::new(
types::Fd(connect.fd.raw_fd()),
connect.socket_addr.as_ptr(),
connect.socket_addr.as_ptr().cast(),
connect.socket_addr.len(),
)
.build()
Expand Down
2 changes: 1 addition & 1 deletion src/io/recv_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<T: BoundedBufMut> Op<RecvFrom<T>> {
std::slice::from_raw_parts_mut(buf.stable_mut_ptr(), buf.bytes_total())
})];

let socket_addr = Box::new(unsafe { SockAddr::init(|_, _| Ok(()))?.1 });
let socket_addr = Box::new(unsafe { SockAddr::try_init(|_, _| Ok(()))?.1 });

let mut msghdr: Box<libc::msghdr> = Box::new(unsafe { std::mem::zeroed() });
msghdr.msg_iov = io_slices.as_mut_ptr().cast();
Expand Down
2 changes: 1 addition & 1 deletion src/io/recvmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<T: BoundedBufMut> Op<RecvMsg<T>> {
}));
}

let socket_addr = Box::new(unsafe { SockAddr::init(|_, _| Ok(()))?.1 });
let socket_addr = Box::new(unsafe { SockAddr::try_init(|_, _| Ok(()))?.1 });

let mut msghdr: Box<libc::msghdr> = Box::new(unsafe { std::mem::zeroed() });
msghdr.msg_iov = io_slices.as_mut_ptr().cast();
Expand Down
8 changes: 7 additions & 1 deletion src/io/shared_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::future::poll_fn;
use std::{
cell::RefCell,
io,
os::unix::io::{FromRawFd, RawFd},
os::unix::io::{BorrowedFd, FromRawFd, RawFd},
rc::Rc,
task::Waker,
};
Expand Down Expand Up @@ -58,6 +58,12 @@ impl SharedFd {
self.inner.fd
}

pub(crate) fn fd(&self) -> BorrowedFd {
// SAFETY: we're ensuring the fd stays open as long as SharedFd is
// alive.
unsafe { BorrowedFd::borrow_raw(self.inner.fd) }
}

/// An FD cannot be closed until all in-flight operation have completed.
/// This prevents bugs where in-flight reads could operate on the incorrect
/// file descriptor.
Expand Down
10 changes: 8 additions & 2 deletions src/io/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
use std::{
io,
net::SocketAddr,
os::unix::io::{AsRawFd, IntoRawFd, RawFd},
os::unix::io::{AsFd, AsRawFd, BorrowedFd, IntoRawFd, RawFd},
path::Path,
};

Expand Down Expand Up @@ -276,7 +276,7 @@ impl Socket {
/// small packets.
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
let socket_ref = socket2::SockRef::from(self);
socket_ref.set_nodelay(nodelay)
socket_ref.set_tcp_nodelay(nodelay)
}
}

Expand All @@ -285,3 +285,9 @@ impl AsRawFd for Socket {
self.fd.raw_fd()
}
}

impl AsFd for Socket {
fn as_fd(&self) -> BorrowedFd {
self.fd.fd()
}
}