Add peek APIs to std::net #38983

Open
wants to merge 12 commits into
from

Projects

None yet

5 participants

@APTy
Contributor
APTy commented Jan 11, 2017 edited

peek

Adds "peek" APIs to std::net sockets, including:

  • UdpSocket.peek()
  • UdpSocket.peek_from()
  • TcpStream.peek()

These methods enable socket reads without side-effects. That is, repeated calls to peek() return identical data. This is accomplished by providing the POSIX flag MSG_PEEK to the underlying socket read operations.

refactor

This also moves the current implementation of recv_from out of the platform-independent sys_common and into respective sys/windows and sys/unix implementations. This allows for more platform-dependent implementations where necessary.

Fixes #38980

@rust-highfive
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@aturon
Contributor
aturon commented Jan 11, 2017

cc @rust-lang/libs, new proposed unstable APIs for UDP.

@aturon aturon added the T-libs label Jan 11, 2017
@alexcrichton

For consistency should we also be sure to add this to the TCP streams as well?

src/libstd/sys/windows/net.rs
@@ -24,6 +24,8 @@ use sys_common::io::read_to_end_uninitialized;
use sys_common::net;
use time::Duration;
+const MSG_PEEK: c_int = 0x2;
@alexcrichton
alexcrichton Jan 11, 2017 Member

Can this be added to the the c module with other windows bindings?

@APTy
APTy Jan 12, 2017 Contributor

yes, agree

src/libstd/sys/windows/net.rs
-1 if c::WSAGetLastError() == c::WSAESHUTDOWN => Ok(0),
-1 => Err(last_error()),
n => Ok(n as usize)
}
}
}
+ pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
+ recv_with_flags(buf, 0)
@alexcrichton
alexcrichton Jan 11, 2017 Member

This may need to be self.recv_with_flags

src/libstd/sys_common/net.rs
@@ -53,6 +53,8 @@ use libc::MSG_NOSIGNAL;
target_os = "haiku", target_os = "bitrig")))]
const MSG_NOSIGNAL: c_int = 0x0;
+const MSG_PEEK: c_int = 0x2;
@alexcrichton
alexcrichton Jan 11, 2017 Member

This seems duplicated with the Windows definition? If this is the unix definition can it be pulled from libc?

@APTy
APTy Jan 12, 2017 Contributor

Yes, let's use libc if possible. I've added the flag only very recently in rust-lang/libc#492. How is best to make this transition? Should we hold off on this PR until the next libc release?

@alexcrichton
alexcrichton Jan 12, 2017 Member

Oh you can just update the submodule in the repository

src/libstd/sys/unix/ext/net.rs
@@ -39,6 +39,8 @@ use libc::MSG_NOSIGNAL;
target_os = "haiku", target_os = "bitrig")))]
const MSG_NOSIGNAL: libc::c_int = 0x0;
+const MSG_PEEK: libc::c_int = 0x2;
@alexcrichton
alexcrichton Jan 11, 2017 Member

Can this be pulled from the libc crate?

src/libstd/net/udp.rs
+ /// Successive calls return the same data.
+ ///
+ /// On success, returns the number of bytes read and the address from
+ /// whence the data came.
@alexcrichton
alexcrichton Jan 11, 2017 Member

Could this documentation also elaborate on how this relates to MSG_PEEK?

@sfackler
Member

Unix sockets as well.

@APTy APTy changed the title from [WIP] Add peek APIs to std::net::UdpSocket to [WIP] Add peek APIs to std::net Jan 12, 2017
@APTy
Contributor
APTy commented Jan 13, 2017 edited

Removed the sys/unix/ext and sys/redox partial implementations, since they seem unnecessary at the moment, but let me know if you feel otherwise.

Last note from me: do we need to change #[unstable(feature = "peek", issue = "38980")] before merge? I'm not familiar with the unstable/stable APIs process.

@APTy APTy changed the title from [WIP] Add peek APIs to std::net to Add peek APIs to std::net Jan 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment