From d5e375487f617152ff2f8d44f9ee4886ad806e72 Mon Sep 17 00:00:00 2001 From: kleinmarb Date: Fri, 9 Aug 2024 21:41:41 +0200 Subject: [PATCH 1/8] Add SO_DONTROUTE setter and getter for unix-based systems --- .idea/.gitignore | 5 +++++ .idea/modules.xml | 8 ++++++++ .idea/socket2.iml | 12 ++++++++++++ .idea/vcs.xml | 6 ++++++ src/sys/unix.rs | 31 +++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/socket2.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..b58b603f --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..c4d1e7ad --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/socket2.iml b/.idea/socket2.iml new file mode 100644 index 00000000..bbe0a70f --- /dev/null +++ b/.idea/socket2.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 3a898bc3..2df2b1a5 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -1555,6 +1555,37 @@ impl crate::Socket { } } + /// Get the value of the `SO_DONTROUTE` option on this socket. + /// + /// For more information about this option, see [`set_dont_route`]. + /// + /// [`set_dont_route`]: crate::Socket::set_dont_route + #[cfg(all(feature = "all", not(target_os = "redox")))] + pub fn dont_route(&self) -> io::Result { + unsafe { + getsockopt::(self.as_raw(), libc::SOL_SOCKET, libc::SO_DONTROUTE) + .map(|reuse| reuse != 0) + } + } + + /// Set the value of the `SO_DONTROUTE` option on this socket. + /// + /// If set, it instructs the operating system's network stack to bypass the normal + /// routing table for outgoing packets on that socket. Instead of using the routing + /// table to determine the path to the destination, packets are sent directly to + /// the network interface that is connected to the destination network. + #[cfg(all(feature = "all", not(target_os = "redox")))] + pub fn set_dont_route(&self, dont_route: bool) -> io::Result<()> { + unsafe { + setsockopt::( + self.as_raw(), + libc::SOL_SOCKET, + libc::SO_DONTROUTE, + dont_route as c_int, + ) + } + } + /// Gets the value of the `TCP_MAXSEG` option on this socket. /// /// For more information about this option, see [`set_mss`]. From 29bdc530225d6bd24c6d5ceea870b86bf4843b0b Mon Sep 17 00:00:00 2001 From: kleinmarb Date: Fri, 9 Aug 2024 21:43:36 +0200 Subject: [PATCH 2/8] Add .idea to .gitignore --- .gitignore | 1 + .idea/.gitignore | 5 ----- .idea/modules.xml | 8 -------- .idea/socket2.iml | 12 ------------ .idea/vcs.xml | 6 ------ 5 files changed, 1 insertion(+), 31 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/modules.xml delete mode 100644 .idea/socket2.iml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 75c73087..b1980d9d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Cargo.lock .vscode/ +.idea/ diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index b58b603f..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index c4d1e7ad..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/socket2.iml b/.idea/socket2.iml deleted file mode 100644 index bbe0a70f..00000000 --- a/.idea/socket2.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddf..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 4b632a002fa48611e9eb56de8730fb63edf6d7d8 Mon Sep 17 00:00:00 2001 From: kleinmarb Date: Fri, 9 Aug 2024 21:53:13 +0200 Subject: [PATCH 3/8] Add SO_DEBUG setter and getter for unix-based systems --- src/sys/unix.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 2df2b1a5..e70062c9 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -1586,6 +1586,38 @@ impl crate::Socket { } } + /// Get the value of the `SO_DEBUG` option on this socket. + /// + /// For more information about this option, see [`set_debug`]. + /// + /// [`set_debug`]: crate::Socket::set_debug + #[cfg(all(feature = "all", not(target_os = "redox")))] + pub fn debug(&self) -> io::Result { + unsafe { + getsockopt::(self.as_raw(), libc::SOL_SOCKET, libc::SO_DEBUG) + .map(|reuse| reuse != 0) + } + } + + /// Set the value of the `SO_DEBUG` option on this socket. + /// + /// If set, the operating system may generate additional diagnostic or debugging + /// information related to the socket's operations. + /// However, the actual behavior and utility of `SO_DEBUG` vary significantly + /// between different operating systems, and in many cases, + /// it is either minimally implemented or largely deprecated. + #[cfg(all(feature = "all", not(target_os = "redox")))] + pub fn set_debug(&self, debug: bool) -> io::Result<()> { + unsafe { + setsockopt::( + self.as_raw(), + libc::SOL_SOCKET, + libc::SO_DEBUG, + debug as c_int, + ) + } + } + /// Gets the value of the `TCP_MAXSEG` option on this socket. /// /// For more information about this option, see [`set_mss`]. From bb893f54f914909cbbf10d604f2edd5953dbe6cc Mon Sep 17 00:00:00 2001 From: kleinmarb Date: Fri, 9 Aug 2024 22:29:21 +0200 Subject: [PATCH 4/8] Add SO_RCVLOWAT and SO_SNDLOWAT setters and getters for unix-based systems --- src/sys/unix.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/sys/unix.rs b/src/sys/unix.rs index e70062c9..c84438ca 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -1618,6 +1618,68 @@ impl crate::Socket { } } + /// Get the value of the `SO_RCVLOWAT` option on this socket. + /// + /// For more information about this option, see [`set_rcv_lowat`]. + /// + /// [`set_rcv_lowat`]: crate::Socket::set_rcv_lowat + // TODO: add Redox support, but I'm not sure if Redox supports SO_RCVLOWAT + #[cfg(all(feature = "all", not(target_os = "redox")))] + pub fn rcv_lowat(&self) -> io::Result { + unsafe { + getsockopt::(self.as_raw(), libc::SOL_SOCKET, libc::SO_RCVLOWAT) + .map(|rcv_lowat| { rcv_lowat as u32}) + } + } + + /// Set the value of the `SO_RCVLOWAT` option on this socket. + /// + /// This option sets the minimum number of bytes that must be available in the + /// receive buffer before a recv() or read() call will return. + // TODO: add Redox support, but I'm not sure if Redox supports SO_RCVLOWAT + #[cfg(all(feature = "all", not(target_os = "redox")))] + pub fn set_rcv_lowat(&self, rcv_lowat: u32) -> io::Result<()> { + unsafe { + setsockopt::( + self.as_raw(), + libc::SOL_SOCKET, + libc::SO_RCVLOWAT, + rcv_lowat as c_int, + ) + } + } + + /// Get the value of the `SO_SNDLOWAT` option on this socket. + /// + /// For more information about this option, see [`set_snd_lowat`]. + /// + /// [`set_snd_lowat`]: crate::Socket::set_snd_lowat + // TODO: add Redox support, but I'm not sure if Redox supports SO_SNDLOWAT + #[cfg(all(feature = "all", not(target_os = "redox")))] + pub fn snd_lowat(&self) -> io::Result { + unsafe { + getsockopt::(self.as_raw(), libc::SOL_SOCKET, libc::SO_RCVLOWAT) + .map(|snd_lowat| { snd_lowat as u32}) + } + } + + /// Set the value of the `SO_SNDLOWAT` option on this socket. + /// + /// This option sets the minimum number of bytes that must be available in the send buffer + /// before a send() or write() call will return. + // TODO: add Redox support, but I'm not sure if Redox supports SO_SNDLOWAT + #[cfg(all(feature = "all", not(target_os = "redox")))] + pub fn set_snd_lowat(&self, snd_lowat: u32) -> io::Result<()> { + unsafe { + setsockopt::( + self.as_raw(), + libc::SOL_SOCKET, + libc::SO_RCVLOWAT, + snd_lowat as c_int, + ) + } + } + /// Gets the value of the `TCP_MAXSEG` option on this socket. /// /// For more information about this option, see [`set_mss`]. From df834da9104fb7a1c2adcf8c0bf5607e0ec3f73c Mon Sep 17 00:00:00 2001 From: kleinmarb Date: Fri, 9 Aug 2024 22:53:57 +0200 Subject: [PATCH 5/8] Fix some typos and lift return statement up in match statements --- CHANGELOG.md | 18 +++++++++--------- CONTRIBUTING.md | 4 ++-- src/lib.rs | 4 ++-- src/sockaddr.rs | 24 ++++++++++++------------ src/socket.rs | 22 +++++++++++----------- src/sys/unix.rs | 22 +++++++++++----------- src/sys/windows.rs | 8 ++++---- tests/socket.rs | 17 +++++++---------- 8 files changed, 58 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b90e67c2..a6f2b910 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,7 +46,7 @@ (https://github.com/rust-lang/socket2/pull/448). * Added `Socket::protocol` for Windows (using `WSAPROTOCOL_INFOW`) (https://github.com/rust-lang/socket2/pull/470). -* `From` for `SockAddr ` nows sets `ss_len` on platforms that +* `From` for `SockAddr ` now sets `ss_len` on platforms that have the fields (most BSDs) (https://github.com/rust-lang/socket2/pull/469). * Change Windows to use `ADDRESS_FAMILY` for `sa_family_t`, this shouldn't @@ -90,7 +90,7 @@ ## Fixed -* Generatation of documentation on docs.rs +* Generation of documentation on docs.rs (https://github.com/rust-lang/socket2/pull/398). # 0.5.0 @@ -204,7 +204,7 @@ This release was broken for Windows. * Reverted back to the `winapi` dependency as switch to `windows-sys` was a breaking change (https://github.com/rust-lang/socket2/pull/340). - Note that we'll will switch to `windows-sys` in v0.5 . + Note that we'll switch to `windows-sys` in v0.5 . * Disable RECVTOS on OpenBSD (https://github.com/rust-lang/socket2/pull/307). * Derive Clone for SockAddr @@ -360,7 +360,7 @@ This release was broken for Windows. * `Protocol::tcp` => `Protocol::TCP`. * `Protocol::udp` => `Protocol::UDP`. * **BREAKING:** Changed the signature of `Socket::recv`, `Socket::recv_vectored` - and related methods to accept unitialised buffers. The `Read` implementation + and related methods to accept uninitialized buffers. The `Read` implementation can be used to read into initialised buffers. * **BREAKING:** Renamed `SockAddr::as_std` to `as_socket`. * **BREAKING:** Renamed `SockAddr::as_inet` to `as_socket_ipv4`. @@ -374,7 +374,7 @@ This release was broken for Windows. * Split the `impl` block for the `Socket` type to create groupings for setting and getting different level socket options using `setsockopt(2)`/`getsockopt(2)`. -* Updated `winapi` depdency to version 0.3.9 and dropped unused features. +* Updated `winapi` dependency to version 0.3.9 and dropped unused features. ## Removed @@ -389,17 +389,17 @@ This release was broken for Windows. * `Socket::into_unix_listener` => `UnixListener::from(socket)`. * `Socket::into_unix_datagram` => `UnixDatagram::from(socket)`. * Removed `cfg-if` dependency. -* Remove `redox_syscall` depdency. +* Remove `redox_syscall` dependency. ## Fixes -* Fixes the Andoid, Fuchsia, Haiku, iOS, illumos, NetBSD and Redox (nightly +* Fixes the Android, Fuchsia, Haiku, iOS, illumos, NetBSD and Redox (nightly only) targets. * Correctly call `recv_from` in `Socket::recv_from_with_flags` (called `recv` previously). * Correctly call `send_to` in `Socket::send_to_with_flags` (called `recv` previously). -* Use correct inmutable references in `Socket::send_with_flags` and +* Use correct immutable references in `Socket::send_with_flags` and `Socket::send_out_of_band`. * Use `IPPROTO_IPV6` in `Socket::join_multicast_v6` on Windows. * Use `c_int` instead of `i32` where appropriate. @@ -426,4 +426,4 @@ This release was broken for Windows. # 0.3.16 * Don't assume the memory layout of `std::net::SocketAddr`. -* Other changes omited +* Other changes omitted diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 97c87acc..db39e5e3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -46,7 +46,7 @@ a collection of system calls for creating and using the socket, e.g. for getting and setting socket options on various levels, e.g. `SOL_SOCKET`, where each block contains a single level. The methods in these block are sorted based on the option name, e.g. `IP_ADD_MEMBERSHIP` rather than -`join_multicast_v4`. Finally the last block contains platforms specific methods +`join_multicast_v4`. Finally, the last block contains platforms specific methods such as `Socket::freebind` which is (at the time of writing) only available on Android, Linux and Fuchsia, which is defined in the `src/sys/*.rs` files. @@ -59,7 +59,7 @@ Other types are mostly defined in `src/lib.rs`, except for `SockAddr` and Testing Socket2 is as simple as running `cargo test --all-features`. -However Socket2 supports a good number of OSs and features. If you want to +However, Socket2 supports a good number of OSs and features. If you want to test/check all those combinations it's easier to use the [Makefile]. Using `make test_all` it will check all supported OS targets and all combinations of supported features. Note that this requires [cargo-hack] and various rustup diff --git a/src/lib.rs b/src/lib.rs index 8f593163..c5c7559b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -200,7 +200,7 @@ pub use socket::InterfaceIndexOrAddress; /// /// This is a newtype wrapper around an integer which provides a nicer API in /// addition to an injection point for documentation. Convenience constants such -/// as [`Domain::IPV4`], [`Domain::IPV6`], etc, are provided to avoid reaching +/// as [`Domain::IPV4`], [`Domain::IPV6`], etc., are provided to avoid reaching /// into libc for various constants. /// /// This type is freely interconvertible with C's `int` type, however, if a raw @@ -243,7 +243,7 @@ impl From for c_int { /// /// This is a newtype wrapper around an integer which provides a nicer API in /// addition to an injection point for documentation. Convenience constants such -/// as [`Type::STREAM`], [`Type::DGRAM`], etc, are provided to avoid reaching +/// as [`Type::STREAM`], [`Type::DGRAM`], etc., are provided to avoid reaching /// into libc for various constants. /// /// This type is freely interconvertible with C's `int` type, however, if a raw diff --git a/src/sockaddr.rs b/src/sockaddr.rs index c80dccf3..477b885a 100644 --- a/src/sockaddr.rs +++ b/src/sockaddr.rs @@ -51,7 +51,7 @@ impl SockAddr { /// let mut addr_storage: libc::sockaddr_storage = unsafe { mem::zeroed() }; /// let mut len = mem::size_of_val(&addr_storage) as libc::socklen_t; /// - /// // The `getsockname(2)` system call will intiliase `storage` for + /// // The `getsockname(2)` system call will initialise `storage` for /// // us, setting `len` to the correct length. /// let res = unsafe { /// libc::getsockname( @@ -105,7 +105,7 @@ impl SockAddr { /// // Initialise a `SocketAddr` byte calling `getsockname(2)`. /// let (_, address) = unsafe { /// SockAddr::try_init(|addr_storage, len| { - /// // The `getsockname(2)` system call will intiliase `storage` for + /// // The `getsockname(2)` system call will initialise `storage` for /// // us, setting `len` to the correct length. /// if libc::getsockname(socket.as_raw_fd(), addr_storage.cast(), len) == -1 { /// Err(io::Error::last_os_error()) @@ -183,7 +183,7 @@ impl SockAddr { ptr::addr_of!(self.storage).cast() } - /// Retuns the address as the storage. + /// Returns the address as the storage. pub const fn as_storage(self) -> sockaddr_storage { self.storage } @@ -258,7 +258,7 @@ impl SockAddr { /// Returns the initialised storage bytes. fn as_bytes(&self) -> &[u8] { // SAFETY: `self.storage` is a C struct which can always be treated a - // slice of bytes. Furthermore, we ensure we don't read any unitialised + // slice of bytes. Furthermore, we ensure we don't read any uninitialized // bytes by using `self.len`. unsafe { std::slice::from_raw_parts(self.as_ptr().cast(), self.len as usize) } } @@ -550,18 +550,18 @@ mod tests { #[allow(clippy::eq_op)] // allow a0 == a0 check fn test_eq(a0: SockAddr, a1: SockAddr, b: SockAddr) { - assert!(a0 == a0); - assert!(a0 == a1); - assert!(a1 == a0); - assert!(a0 != b); - assert!(b != a0); + assert_eq!(a0, a0); + assert_eq!(a0, a1); + assert_eq!(a1, a0); + assert_ne!(a0, b); + assert_ne!(b, a0); } fn test_hash(a0: SockAddr, a1: SockAddr, b: SockAddr) { - assert!(calculate_hash(&a0) == calculate_hash(&a0)); - assert!(calculate_hash(&a0) == calculate_hash(&a1)); + assert_eq!(calculate_hash(&a0), calculate_hash(&a0)); + assert_eq!(calculate_hash(&a0), calculate_hash(&a1)); // technically unequal values can have the same hash, in this case x != z and both have different hashes - assert!(calculate_hash(&a0) != calculate_hash(&b)); + assert_ne!(calculate_hash(&a0), calculate_hash(&b)); } fn calculate_hash(x: &SockAddr) -> u64 { diff --git a/src/socket.rs b/src/socket.rs index 8d517b47..ace79818 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -95,7 +95,7 @@ impl Socket { // memory unsafe, so it's not desired but never memory unsafe or // causes UB. // - // However there is one exception. We use `TcpStream` to + // However, there is one exception. We use `TcpStream` to // represent the `Socket` internally (see `Inner` type), // `TcpStream` has a layout optimisation that doesn't allow for // negative file descriptors (as those are always invalid). @@ -206,7 +206,7 @@ impl Socket { sys::connect(self.as_raw(), address) } - /// Initiate a connection on this socket to the specified address, only + /// Initiate a connection on this socket to the specified address, /// only waiting for a certain period of time for the connection to be /// established. /// @@ -408,7 +408,7 @@ impl Socket { /// /// Normally casting a `&mut [u8]` to `&mut [MaybeUninit]` would be /// unsound, as that allows us to write uninitialised bytes to the buffer. - /// However this implementation promises to not write uninitialised bytes to + /// However, this implementation promises to not write uninitialised bytes to /// the `buf`fer and passes it directly to `recv(2)` system call. This /// promise ensures that this function can be called using a `buf`fer of /// type `&mut [u8]`. @@ -461,7 +461,7 @@ impl Socket { /// # Safety /// /// Normally casting a `IoSliceMut` to `MaybeUninitSlice` would be unsound, - /// as that allows us to write uninitialised bytes to the buffer. However + /// as that allows us to write uninitialised bytes to the buffer. However, /// this implementation promises to not write uninitialised bytes to the /// `bufs` and passes it directly to `recvmsg(2)` system call. This promise /// ensures that this function can be called using `bufs` of type `&mut @@ -500,7 +500,7 @@ impl Socket { sys::recv_vectored(self.as_raw(), bufs, flags) } - /// Receives data on the socket from the remote adress to which it is + /// Receives data on the socket from the remote address to which it is /// connected, without removing that data from the queue. On success, /// returns the number of bytes peeked. /// @@ -602,7 +602,7 @@ impl Socket { /// # Note: Datagram Sockets /// For datagram sockets, the behavior of this method when `buf` is smaller than /// the datagram at the head of the receive queue differs between Windows and - /// Unix-like platforms (Linux, macOS, BSDs, etc: colloquially termed "*nix"). + /// Unix-like platforms (Linux, macOS, BSDs, etc.: colloquially termed "*nix"). /// /// On *nix platforms, the datagram is truncated to the length of `buf`. /// @@ -927,7 +927,7 @@ impl Socket { /// On most OSs the duration only has a precision of seconds and will be /// silently truncated. /// - /// On Apple platforms (e.g. macOS, iOS, etc) this uses `SO_LINGER_SEC`. + /// On Apple platforms (e.g. macOS, iOS, etc.) this uses `SO_LINGER_SEC`. pub fn set_linger(&self, linger: Option) -> io::Result<()> { let linger = into_linger(linger); unsafe { setsockopt(self.as_raw(), sys::SOL_SOCKET, sys::SO_LINGER, linger) } @@ -1512,7 +1512,7 @@ impl Socket { /// Set the value of the `IP_MULTICAST_LOOP` option for this socket. /// /// If enabled, multicast packets will be looped back to the local socket. - /// Note that this may not have any affect on IPv6 sockets. + /// Note that this may not have any effect on IPv6 sockets. pub fn set_multicast_loop_v4(&self, loop_v4: bool) -> io::Result<()> { unsafe { setsockopt( @@ -1542,7 +1542,7 @@ impl Socket { /// this socket. The default value is 1 which means that multicast packets /// don't leave the local network unless explicitly requested. /// - /// Note that this may not have any affect on IPv6 sockets. + /// Note that this may not have any effect on IPv6 sockets. pub fn set_multicast_ttl_v4(&self, ttl: u32) -> io::Result<()> { unsafe { setsockopt( @@ -1878,7 +1878,7 @@ impl Socket { /// Set the value of the `IPV6_MULTICAST_LOOP` option for this socket. /// /// Controls whether this socket sees the multicast packets it sends itself. - /// Note that this may not have any affect on IPv4 sockets. + /// Note that this may not have any effect on IPv4 sockets. pub fn set_multicast_loop_v6(&self, loop_v6: bool) -> io::Result<()> { unsafe { setsockopt( @@ -2219,7 +2219,7 @@ impl Read for Socket { fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result { // Safety: both `IoSliceMut` and `MaybeUninitSlice` promise to have the // same layout, that of `iovec`/`WSABUF`. Furthermore, `recv_vectored` - // promises to not write unitialised bytes to the `bufs` and pass it + // promises to not write uninitialized bytes to the `bufs` and pass it // directly to the `recvmsg` system call, so this is safe. let bufs = unsafe { &mut *(bufs as *mut [IoSliceMut<'_>] as *mut [MaybeUninitSlice<'_>]) }; self.recv_vectored(bufs).map(|(n, _)| n) diff --git a/src/sys/unix.rs b/src/sys/unix.rs index c84438ca..ff527676 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -819,7 +819,7 @@ impl SockAddr { .unwrap_or_default() } - /// Returns the underlying `sockaddr_un` object if this addres is from the `AF_UNIX` family, + /// Returns the underlying `sockaddr_un` object if this address is from the `AF_UNIX` family, /// otherwise returns `None`. pub(crate) fn as_sockaddr_un(&self) -> Option<&libc::sockaddr_un> { self.is_unix().then(|| { @@ -838,7 +838,7 @@ impl SockAddr { self.len() as usize - offset_of_path(storage) - 1 } - /// Get a u8 slice for the bytes of the pathname or abstract name. + /// Get an u8 slice for the bytes of the pathname or abstract name. /// /// Should not be called on unnamed addresses. fn path_bytes(&self, storage: &libc::sockaddr_un, abstract_name: bool) -> &[u8] { @@ -954,10 +954,10 @@ pub(crate) fn poll_connect(socket: &crate::Socket, timeout: Duration) -> io::Res Ok(_) => { // Error or hang up indicates an error (or failure to connect). if (pollfd.revents & libc::POLLHUP) != 0 || (pollfd.revents & libc::POLLERR) != 0 { - match socket.take_error() { - Ok(Some(err)) | Err(err) => return Err(err), + return match socket.take_error() { + Ok(Some(err)) | Err(err) => Err(err), Ok(None) => { - return Err(io::Error::new( + Err(io::Error::new( io::ErrorKind::Other, "no error set after POLLHUP", )) @@ -1099,7 +1099,7 @@ pub(crate) fn recv_from_vectored( flags: c_int, ) -> io::Result<(usize, RecvFlags, SockAddr)> { let mut msg = MsgHdrMut::new().with_buffers(bufs); - // SAFETY: `recvmsg` initialises the address storage and we set the length + // SAFETY: `recvmsg` initialises the address storage, and we set the length // manually. let (n, addr) = unsafe { SockAddr::try_init(|storage, len| { @@ -1635,7 +1635,7 @@ impl crate::Socket { /// Set the value of the `SO_RCVLOWAT` option on this socket. /// /// This option sets the minimum number of bytes that must be available in the - /// receive buffer before a recv() or read() call will return. + /// receive buffer before a `recv()` or `read()` call will return. // TODO: add Redox support, but I'm not sure if Redox supports SO_RCVLOWAT #[cfg(all(feature = "all", not(target_os = "redox")))] pub fn set_rcv_lowat(&self, rcv_lowat: u32) -> io::Result<()> { @@ -1666,7 +1666,7 @@ impl crate::Socket { /// Set the value of the `SO_SNDLOWAT` option on this socket. /// /// This option sets the minimum number of bytes that must be available in the send buffer - /// before a send() or write() call will return. + /// before a `send()` or `write()` call will return. // TODO: add Redox support, but I'm not sure if Redox supports SO_SNDLOWAT #[cfg(all(feature = "all", not(target_os = "redox")))] pub fn set_snd_lowat(&self, snd_lowat: u32) -> io::Result<()> { @@ -2903,7 +2903,7 @@ impl crate::Socket { /// Set the value of the `IPV6_TCLASS` option for this socket. /// - /// Specifies the traffic class field that is used in every packets + /// Specifies the traffic class field that is used in every packet /// sent from this socket. #[cfg(all( feature = "all", @@ -2973,7 +2973,7 @@ impl crate::Socket { /// Specifies the TCP congestion control algorithm to use for this socket. /// /// The value must be a valid TCP congestion control algorithm name of the - /// platform. For example, Linux may supports "reno", "cubic". + /// platform. For example, Linux may support "reno", "cubic". #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))] #[cfg_attr( docsrs, @@ -3305,7 +3305,7 @@ from!(crate::Socket, UnixDatagram); fn in_addr_convertion() { let ip = Ipv4Addr::new(127, 0, 0, 1); let raw = to_in_addr(&ip); - // NOTE: `in_addr` is packed on NetBSD and it's unsafe to borrow. + // NOTE: `in_addr` is packed on NetBSD, and it's unsafe to borrow. let a = raw.s_addr; assert_eq!(a, u32::from_ne_bytes([127, 0, 0, 1])); assert_eq!(from_in_addr(raw), ip); diff --git a/src/sys/windows.rs b/src/sys/windows.rs index 11f2b7b0..afe7834f 100644 --- a/src/sys/windows.rs +++ b/src/sys/windows.rs @@ -306,15 +306,15 @@ pub(crate) fn poll_connect(socket: &crate::Socket, timeout: Duration) -> io::Res if (fd_array.revents & POLLERR as i16) != 0 || (fd_array.revents & POLLHUP as i16) != 0 { - match socket.take_error() { - Ok(Some(err)) => return Err(err), + return match socket.take_error() { + Ok(Some(err)) => Err(err), Ok(None) => { - return Err(io::Error::new( + Err(io::Error::new( io::ErrorKind::Other, "no error set after POLLHUP", )) } - Err(err) => return Err(err), + Err(err) => Err(err), } } return Ok(()); diff --git a/tests/socket.rs b/tests/socket.rs index 2300f0ed..509d2049 100644 --- a/tests/socket.rs +++ b/tests/socket.rs @@ -472,7 +472,7 @@ where const DATA: &[u8] = b"hello world"; #[test] -fn connect_timeout_unrouteable() { +fn connect_timeout_unroutable() { // This IP is unroutable, so connections should always time out. let addr = "10.255.255.1:80".parse::().unwrap().into(); @@ -1634,7 +1634,7 @@ fn tcp_congestion() { socket .set_tcp_congestion(&origin_tcp_ca) .expect("failed to set tcp congestion algorithm"); - // Return a Err when set a non-exist tcp_ca + // Return an Err when set a non-exist tcp_ca socket .set_tcp_congestion(b"tcp_congestion_does_not_exist") .unwrap_err(); @@ -1676,16 +1676,13 @@ fn dccp() { let listener = Socket::new(Domain::IPV4, Type::DCCP, Some(Protocol::DCCP)).unwrap(); let addr = "127.0.0.1:0".parse::().unwrap().into(); listener.set_dccp_service(45).unwrap(); - assert!(listener.dccp_service().unwrap() == 45); + assert_eq!(listener.dccp_service().unwrap(), 45); assert!(listener.dccp_cur_mps().unwrap() > 0); assert!(listener.dccp_available_ccids::<4>().unwrap().len() >= 3); - assert!( - listener.dccp_send_cscov().unwrap() == 0, - "sender cscov should be zero by default" - ); + assert_eq!(listener.dccp_send_cscov().unwrap(), 0, "sender cscov should be zero by default"); listener.set_dccp_ccid(2).unwrap(); listener.set_dccp_qpolicy_txqlen(6).unwrap(); - assert!(listener.dccp_qpolicy_txqlen().unwrap() == 6); + assert_eq!(listener.dccp_qpolicy_txqlen().unwrap(), 6); listener.bind(&addr).unwrap(); listener.listen(10).unwrap(); @@ -1695,9 +1692,9 @@ fn dccp() { let (mut accepted, _) = listener.accept().unwrap(); let msg = "Hello World!"; - assert!(client.write(msg.as_bytes()).unwrap() == msg.len()); + assert_eq!(client.write(msg.as_bytes()).unwrap(), msg.len()); let mut recv_buf = [0_u8; 64]; - assert!(accepted.read(&mut recv_buf).unwrap() == msg.len()); + assert_eq!(accepted.read(&mut recv_buf).unwrap(), msg.len()); } #[test] From 4c63b3a4b8d614eb75780ebaadbd61374b19bbfb Mon Sep 17 00:00:00 2001 From: kleinmarb Date: Sat, 10 Aug 2024 10:52:12 +0200 Subject: [PATCH 6/8] Remove .idea from .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index b1980d9d..75c73087 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ Cargo.lock .vscode/ -.idea/ From d6c2f8b33a868f549a4c3db5035fb5188fe5b416 Mon Sep 17 00:00:00 2001 From: kleinmarb Date: Sat, 10 Aug 2024 11:09:40 +0200 Subject: [PATCH 7/8] Fix bugs and remove TODO's --- src/sys/unix.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/sys/unix.rs b/src/sys/unix.rs index ff527676..e32cc9fa 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -1623,7 +1623,6 @@ impl crate::Socket { /// For more information about this option, see [`set_rcv_lowat`]. /// /// [`set_rcv_lowat`]: crate::Socket::set_rcv_lowat - // TODO: add Redox support, but I'm not sure if Redox supports SO_RCVLOWAT #[cfg(all(feature = "all", not(target_os = "redox")))] pub fn rcv_lowat(&self) -> io::Result { unsafe { @@ -1636,7 +1635,6 @@ impl crate::Socket { /// /// This option sets the minimum number of bytes that must be available in the /// receive buffer before a `recv()` or `read()` call will return. - // TODO: add Redox support, but I'm not sure if Redox supports SO_RCVLOWAT #[cfg(all(feature = "all", not(target_os = "redox")))] pub fn set_rcv_lowat(&self, rcv_lowat: u32) -> io::Result<()> { unsafe { @@ -1654,11 +1652,10 @@ impl crate::Socket { /// For more information about this option, see [`set_snd_lowat`]. /// /// [`set_snd_lowat`]: crate::Socket::set_snd_lowat - // TODO: add Redox support, but I'm not sure if Redox supports SO_SNDLOWAT #[cfg(all(feature = "all", not(target_os = "redox")))] pub fn snd_lowat(&self) -> io::Result { unsafe { - getsockopt::(self.as_raw(), libc::SOL_SOCKET, libc::SO_RCVLOWAT) + getsockopt::(self.as_raw(), libc::SOL_SOCKET, libc::SO_SNDLOWAT) .map(|snd_lowat| { snd_lowat as u32}) } } @@ -1667,14 +1664,13 @@ impl crate::Socket { /// /// This option sets the minimum number of bytes that must be available in the send buffer /// before a `send()` or `write()` call will return. - // TODO: add Redox support, but I'm not sure if Redox supports SO_SNDLOWAT #[cfg(all(feature = "all", not(target_os = "redox")))] pub fn set_snd_lowat(&self, snd_lowat: u32) -> io::Result<()> { unsafe { setsockopt::( self.as_raw(), libc::SOL_SOCKET, - libc::SO_RCVLOWAT, + libc::SO_SNDLOWAT, snd_lowat as c_int, ) } From b07a11ac6585f4f422d3e0f7f58d247fc95bba7d Mon Sep 17 00:00:00 2001 From: kleinmarb Date: Sat, 10 Aug 2024 11:10:15 +0200 Subject: [PATCH 8/8] Add tests for all new features --- tests/socket.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/socket.rs b/tests/socket.rs index 509d2049..022f8ca3 100644 --- a/tests/socket.rs +++ b/tests/socket.rs @@ -1347,6 +1347,26 @@ test!( set_send_buffer_size(SET_BUF_SIZE), GET_BUF_SIZE ); +#[cfg(all( + feature = "all", + not(any(target_os = "redox", target_os = "windows")) +))] +test!(dont_route, set_dont_route(true)); +#[cfg(all( + feature = "all", + not(any(target_os = "redox", target_os = "windows")) +))] +test!(debug, set_debug(true)); +#[cfg(all( + feature = "all", + not(any(target_os = "redox", target_os = "windows")) +))] +test!(snd_lowat, set_snd_lowat(1024)); +#[cfg(all( + feature = "all", + not(any(target_os = "redox", target_os = "windows")) +))] +test!(rcv_lowat, set_rcv_lowat(1024)); #[cfg(not(target_os = "redox"))] test!(out_of_band_inline, set_out_of_band_inline(true)); test!(reuse_address, set_reuse_address(true));