* Fix the value of optlen as passed to libc::setsockopt
Ever since the sockopt macro was written in
979faf0976 we've been passing the wrong
size for u8 and usize sockopts.
* Add tests for sockopt::IpMulticastTtl #2073
On Linux and Android, sockopt::IpMulticastTtl can be set for both IPv4
and IPv6 sockets, but not on FreeBSD.
---------
Co-authored-by: Alan Somers <asomers@gmail.com>
Co-authored-by: Simon B. Gasse <sgasse@users.noreply.github.com>
From [tcp](https://man7.org/linux/man-pages/man7/tcp.7.html) man page:
```
TCP_FASTOPEN_CONNECT (since Linux 4.11)
This option enables an alternative way to perform Fast
Open on the active side (client). When this option is
enabled, connect(2) would behave differently depending on
if a Fast Open cookie is available for the destination.
If a cookie is not available (i.e. first contact to the
destination), connect(2) behaves as usual by sending a SYN
immediately, except the SYN would include an empty Fast
Open cookie option to solicit a cookie.
If a cookie is available, connect(2) would return 0
immediately but the SYN transmission is deferred. A
subsequent write(2) or sendmsg(2) would trigger a SYN with
data plus cookie in the Fast Open option. In other words,
the actual connect operation is deferred until data is
supplied.
Note: While this option is designed for convenience,
enabling it does change the behaviors and certain system
calls might set different errno values. With cookie
present, write(2) or sendmsg(2) must be called right after
connect(2) in order to send out SYN+data to complete 3WHS
and establish connection. Calling read(2) right after
connect(2) without write(2) will cause the blocking socket
to be blocked forever.
The application should either set TCP_FASTOPEN_CONNECT
socket option before write(2) or sendmsg(2), or call
write(2) or sendmsg(2) with MSG_FASTOPEN flag directly,
instead of both on the same connection.
Here is the typical call flow with this new option:
s = socket();
setsockopt(s, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, 1, ...);
connect(s);
write(s); /* write() should always follow connect()
* in order to trigger SYN to go out. */
read(s)/write(s);
/* ... */
close(s);
```
From [man page](https://man7.org/linux/man-pages/man7/ip.7.html):
```
IP_BIND_ADDRESS_NO_PORT (since Linux 4.2)
Inform the kernel to not reserve an ephemeral port when
using bind(2) with a port number of 0. The port will
later be automatically chosen at connect(2) time, in a way
that allows sharing a source port as long as the 4-tuple
is unique.
```
* Add more cfg aliases
Add cfg aliases for linux_android, bsd, and freebsdlike. Use them in
many places, though not everywhere they could theoretically be used.
Fixes#2188
* Use apple_targets in build.rs
Co-authored-by: SteveLauC <stevelauc@outlook.com>
* whitespace
Co-authored-by: SteveLauC <stevelauc@outlook.com>
* Define a "solarish" target alias.
* Describe cfg aliases in CONVENTIONS.md
* solarish in line 803
* solarish in line 845
* fix fmt
---------
Co-authored-by: SteveLauC <stevelauc@outlook.com>
* Added FreeBSD's SCM_REALTIME and SCM_MONOTONIC into sys::socket::ControlMessageOwned.
* Creating a SocketTimestamp enum for the SO_TS_CLOCK setsockopt for FreeBSD.
* Fixing whitespace
* Fixing CI error on cfg attributes
* Removing legacy doc attributes
* Formatting cleanup
* Updating changelog
* Adding tests for new TsClock setsockopt enum and the two new packet timestamp control messages for FreeBSD
* Replacing an assert_eq with an assert in new tests.
* Removing qemu ignore for new FreeBSD tests
* Giving new FreeBSD timestamping tests each a unique socket
* Updating monotonic packet timestamp test to account for monotonicity
* Moving test ports again to line up with changes in #2196
* Attempting checks again
* Wrapping ptr::read_unaligned calls in unsafe blocks
* Add fanotify API wrappers
* Review/fix enum comments
* Rename has_compile_version to check_version
* Fix lint unsafe_block_in_unsafe_fn
* Rename fanotify OFlags to EventFFlags
* Use existing function at_rawfd from fcntl
* Add missing feature guard for docs
* Change FanotifyEvent struct to a simple wrapper over libc structure
* Change FanotifyResponse struct to a simple wrapper over libc structure
* Cast pointer with function 'cast' instead of 'as'
* Add FAN_REPORT_PIDFD and FAN_REPORT_TID fanotify init flags
* Relax lifetime requirements for FdSet::{insert, remove, contains}
Fixes#2130
* Take BorrowedFd as the argument for FdSet::{insert, remove, contains}
&AsFd doesn't work because there are 'static types, like std::fs::File,
which implement AsFd.
* fix changelog & remove unused entries
* fix wrong PR number
---------
Co-authored-by: Steve Lau <stevelauc@outlook.com>
* Cast pointers more carefully
Prefer ptr::{cast, cast_const, cast_mut} over `as`. The latter makes it
too easy to accidentally change both the type and mutability when
changing only one was intended.
This exercise caught an unintended mutability cast in one function, the
BSD version of sendfile. In this case there's no UB because it isn't
possible for anything else to get a reference to the data that was
incorrectly cast.
There was also a type cast that wasn't guaranteed to be correct (but
probably was) due to memory layout guarantees in if_nametoindex.
* Remove unnecessary cast in UnixCredentials::groups
1944: Rework vsock test r=asomers a=stefano-garzarella
We mainly provide VsockAddr, so let's try to test well that VsockAddr mapping to libc::sockaddr_vm is correct.
Let's remove all interactions with the socket, since vsock may or may not be available in the environment.
Testing socket(), bind(), listen(), connect(), etc. caused unexpected failures, and it's out of scope of this crate.
So let's simplify the vsock test focussing on VsockAddr. This should work also on graviton, so let's try to re-enable it.
Fixes#1934
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
Co-authored-by: Stefano Garzarella <sgarzare@redhat.com>
We mainly provide VsockAddr, so let's try to test well that VsockAddr
mapping to libc::sockaddr_vm is correct.
Let's remove all interactions with the socket, since vsock may or may
not be available in the environment.
Testing socket(), bind(), listen(), connect(), etc. caused unexpected
failures, and it's out of scope of this crate.
So let's simplify the vsock test focussing on VsockAddr.
This should work also on graviton, so let's try to re-enable it.
Fixes#1934
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
1913: feat: I/O safety for 'sys/inotify' r=asomers a=SteveLauC
#### What this PR does:
1. Changes the `fd` field of `struct Inotify` from `RawFd` to `OwnedFd`
2. Changes the interfaces of functions in the `impl Inotify {}`
> The type of `self` changes from `Self` to `&mut Self`.
From:
```rust
pub fn add_watch<P: ?Sized + NixPath>(
self,
path: &P,
mask: AddWatchFlags,
) -> Result<WatchDescriptor>
pub fn rm_watch(self, wd: WatchDescriptor) -> Result<()>
pub fn read_events(self) -> Result<Vec<InotifyEvent>>
```
To:
```rust
pub fn add_watch<P: ?Sized + NixPath>(
&mut self,
path: &P,
mask: AddWatchFlags,
) -> Result<WatchDescriptor>
pub fn rm_watch(&mut self, wd: WatchDescriptor) -> Result<()>
pub fn read_events(&mut self) -> Result<Vec<InotifyEvent>>
```
In the previous implementation, these functions can take `self` by value as `struct Inotify` [was `Copy`](https://docs.rs/nix/latest/nix/sys/inotify/struct.Inotify.html#impl-Copy-for-Inotify). With the changes in `1` applied, `struct Inotify` is no longer `Copy`, so we have to take `self` by reference.
-------
Blocks until the merge of #1863 as this PR needs `read(2)` to be I/O-safe.
1926: feat: I/O safety for 'sys/sendfile' r=asomers a=SteveLauC
#### What this PR does:
1. Adds I/O safety for module `sys/sendfile`.
1927: feat: I/O safety for 'sys/statvfs' r=asomers a=SteveLauC
#### What this PR does:
1. Adds I/O safety for module `sys/statvfs`.
1931: feat: I/O safety for 'sys/uid' & 'sched' r=asomers a=SteveLauC
#### What this PR does:
Adds I/O safety for modules:
1. `sys/uio`
2. `sched`
1933: feat: I/O safety for 'sys/timerfd' r=asomers a=SteveLauC
#### What this PR does:
1. Adds I/O safety for module `sys/timerfd`.
Co-authored-by: Steve Lau <stevelauc@outlook.com>