Commit Graph

201 Commits

Author SHA1 Message Date
Alan Somers
2b827ad79c Cleanup old Clippy directives. 2022-12-04 16:15:28 -07:00
Alex Saveau
fb802462a6
Fix clippy lints
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-12-04 13:44:50 -08:00
Alex Saveau
59f66d128b
Move some pure formatting changes out of #1863
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-12-04 11:54:02 -08:00
Alex Saveau
f3aa1affb0
Nuke deprecated net APIs
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-12-03 11:40:05 -08:00
Emils
d08d4e2f15 Add routing socket type 2022-11-29 14:13:10 +00:00
Alan Somers
8e91b28b64 Fix UB in the SO_TYPE sockopt
When reading a value into an enum from getsockopt, we must validate it.
Failing to do so can lead to UB for example with SOCK_PACKET on Linux.

Perform the validation in GetSockOpt::get.  Currently SockType is the
only type that requires validation.

Fixes #1819
2022-11-28 23:07:54 -07:00
Alan Somers
0ae109ddad Clippy cleanup with the latest nightly. 2022-11-25 08:27:55 -07:00
StackOverflowExcept1on
38597988f4
Added SockProtocol::Raw = libc::IPPROTO_RAW for raw sockets 2022-11-21 21:13:59 +03:00
Alan Somers
0696987995 Suppress deprecation warnings from the latest libc
https://github.com/rust-lang/libc/pull/2963
2022-10-23 10:18:50 -06:00
Michael Baikov
d13c2ccf99 include docs 2022-10-14 10:13:43 -04:00
Michael Baikov
475819c22b disable a flaky test on aarch64
See https://github.com/nix-rust/nix/pull/1744 for more details
2022-10-14 09:02:05 -04:00
Michael Baikov
19c83afbbf reimplement recvmsg/sendmmsg
New implementation performs no allocations after all the necessary
structures are created, removes potentially unsound code that
was used by the old version (see below) and adds a bit more
documentation about bugs in how timeout is actually handled

```
    let timeout = if let Some(mut t) = timeout {
        t.as_mut() as *mut libc::timespec
    } else {
        ptr::null_mut()
    };
```
2022-10-14 08:57:41 -04:00
Michael Baikov
d600ddd860 pack_mhdr_to_receive: pass iovec ptr and capacity separately
We'll be using that to reinitialize buffers later
2022-10-14 08:57:41 -04:00
Michael Baikov
dd1d4fd5eb pack_mhdr_to_receive: pass pointer and capacity directly
This is already an unsafe function, dealing with pointers directly
does not make it much more unsafe but simplifies lifetimes later on
and similar to a previous commit allows to alocate a single buffer
to store all the control messages
2022-10-14 08:57:41 -04:00
Michael Baikov
482a6b1046 socket: stop setting vector length inside read_mhdr
CMSG_FIRSTHDR/CMSG_NEXTHDR operate in terms of pointers contained
inside msghdr structure, vector capacity doesn't matter for them.

This would change external behavior of recvmsg/recvmmsg in a sense
that buffer passed to store controll messages won't have it's length
updated but intended way to receive control messages is with cmsgs
iterator on `RecvMsg` which would still work.

This change is required to allow using a single vector to store
control messages from multiple packets
2022-10-14 08:57:41 -04:00
Ryan Zoeller
6e7bddd154 Fix clippy warnings on nightly
Clippy is now smarter about detecting unnecessary casts and
useless conversions, which means we need to be more explicit
about when the conversions are needed for a subset of platforms.

Required changes found by repeatedly running the following command
against a list of the supported platforms.

`xargs -t -I {} sh -c "cargo clippy -Zbuild-std --target {} --all-targets -- -D warnings || exit 255"`

I removed the casts it complained about, and then restored them
with an `#[allow]` if a later target needed the cast.
2022-10-08 14:08:54 -05:00
Alan Somers
3aedbfa08e Clippy cleanup 2022-09-11 16:25:33 -06:00
bors[bot]
ffc793ed4d
Merge #1776
1776: Add support for the IP_SENDSRCADDR control message r=rtzoeller a=matttpt

This control message is available on FreeBSD, NetBSD, and OpenBSD. When used with `sendmsg`, it sets the IPv4 source address. This adds support through a new `ControlMessage::Ipv4SendSrcAddr` variant that complements `ControlMessageOwned::Ipv4RecvDstAddr`.

A few notes:
* `IP_SENDSRCADDR` is actually just an alias for `IP_RECVDSTADDR` (though the code doesn't depend on this).
* On NetBSD, `IP_PKTINFO` can be used to accomplish the same thing and is already supported by nix. On FreeBSD and OpenBSD, though, `IP_SENDSRCADDR` is the only method I'm aware of.
* The accompanying test binds a UDP socket to all local interfaces (0.0.0.0). If this is not acceptable, please let me know; however, FreeBSD requires this to use `IP_SENDSRCADDR`.

I'll add a change-log entry once I see the PR number.

Thanks!

Co-authored-by: Matthew Ingwersen <matttpt@gmail.com>
2022-08-12 04:38:44 +00:00
SteveLauC
d458ecd4e1 remove deprecated items 2022-08-09 12:15:52 +08:00
Matthew Ingwersen
6bf07fdbb9 Add support for the IP_SENDSRCADDR control message
This control message (actually just an alias for IP_RECVDSTADDR) sets
the IPv4 source address when used with sendmsg. It is available on
FreeBSD, NetBSD, OpenBSD, and DragonFlyBSD.
2022-08-05 12:35:57 -04:00
bors[bot]
854a5469c0
Merge #1772
1772: Add support for RecvOrigDstAddr on Linux r=asomers a=brianmay

Fixes #1767

Co-authored-by: Brian May <brian@linuxpenguins.xyz>
2022-07-26 23:08:12 +00:00
Brian May
c45cd74f42 Add support for RecvOrigDstAddr on Linux
Fixes #1767
2022-07-26 07:39:00 +10:00
valdaarhun
1040d97036 Add ETH_P_ALL protocol number to SockProtocol
Add note to Changelog.md

Make changes in comments

Co-authored-by: Alan Somers <asomers@gmail.com>

Add Android as target os for ETH_P_ALL
2022-07-24 21:25:01 +05:30
Alan Somers
098aba2c4a Fix SockaddrLike::from_raw with unaligned inputs
The major users of this function are functions like gethostname, which
will always properly align their buffers.  But out-of-crate consumers
could manually construct an unaligned buffer.  Handle that correctly.

Enable Clippy's cast_ptr_alignment lint.  It's disabled by default as it
reports many false positives, but it would've caught this problem.

Reported-by:	Miri
Fixes:		1769
2022-07-23 14:46:42 -06:00
Alan Somers
e0e768e7b9 Fix a buffer overflow in sys::socket::recvfrom
IPv4 and stream sockets are unaffected, but for datagram sockets of
other address types libc::recvfrom might overwrite part of the stack.

Fixes #1762
2022-07-14 11:37:56 -06:00
Al Hoang
d02e27476c add haiku support
* enabled as much functionality and defines that match
  updated libc definitions for haiku
2022-05-14 18:45:18 -05:00
not_a_seagull
0b58f29772 Replace the IoVec type with IoSlice and IoSliceMut 2022-04-08 12:39:16 -07:00
Alan Somers
fbeabf3730 Deprecate IpAddr, Ipv4Addr, and Ipv6Addr
Because they're redundant with types in the standard library.

Fixes #1681
2022-03-22 21:09:52 -06:00
Alan Somers
76d70b4b25 Replace the Sockaddr enum with a union
The SockAddr enum is quite large, and the user must allocate space for
the whole thing even though he usually knows what type he needs.
Furthermore, thanks to the sa_family field, the sockaddr types are
basically an enum even in C.

So replace the ungainly enum with a SockaddrLike trait implemented by
all sockaddr types and  a SockaddrStorage union that has safe accessors.

Also, deprecate InetAddr, which only existed to support SockAddr.

Supplants #1504
Fixes #1544
2022-03-21 20:50:24 -06:00
bin liu
84d89e4fc2 docs: fix link in doc for cmsg_space in sys/socket
Link to cmsg_space macro for recvmmsg and recvmsg should
be the doc's root of crate as macro is an exported macro.

Signed-off-by: bin liu <liubin0329@gmail.com>
2022-03-09 15:59:15 +08:00
i509VCB
a092c5ccc7
Add MsgFlag::MSG_NOSIGNAL 2022-03-06 16:27:48 -06:00
Ryan Zoeller
9269056e8e Add accept4 on supported platforms 2022-02-04 19:46:25 -06:00
bors[bot]
b5a23c775c
Merge #1618
1618: Refactor UnixAddr r=asomers a=asomers

* Within UnixAddr, replace the path_len variable (length of the sun_path
    field) with sun_len (length of the whole structure).  This is more
    similar to how other sockaddr types work, and it's the same way that the
    BSDs use the sun_len field.
    
    Also, don't require that sun_path be nul-terminated. The OS doesn't
    require it.

* On BSD-derived operating systems, struct sockaddr has a sa_len field
    that holds the length of the structure.  UnixAddr's path_len field is
    redundant.  Remove path_len on BSD-derived OSes, retaining it only for
    Illumos and Linux-based OSes.

Co-authored-by: Alan Somers <asomers@gmail.com>
2021-12-31 20:36:17 +00:00
Alessandro Ghedini
a9829853df Add support for the SO_TXTIME sockopt and SCM_TXTIME control message 2021-12-29 14:15:54 +00:00
Michael Baikov
9aa61ef975 feat: Add glibc::SOF_TIMESTAMPING_* support 2021-12-23 06:41:30 +08:00
Alan Somers
6d07477fac UnixAddr: replace path_len with sun_len
Within UnixAddr, replace the path_len variable (length of the sun_path
field) with sun_len (length of the whole structure).  This is more
similar to how other sockaddr types work, and it's the same way that the
BSDs use the sun_len field.

Also, don't require that sun_path be nul-terminated. The OS doesn't
require it.
2021-12-21 18:14:49 -07:00
Vincent Dagonneau
5f5b7d4d7a feature-gate most Nix functions
Using features reduces build time and size for consumer crates.  By
default all features are enabled.
2021-12-20 18:47:16 -07:00
Alan Somers
a09b1c8ac6 Clippy cleanup
And this time, start running Clippy in CI
2021-09-19 07:58:15 -06:00
bors[bot]
4f58c8c8e3
Merge #1496
1496: Rework UnixAddr to fix soundness issues r=asomers a=coolreader18

Fixes #1494

I went with making `sun_path` always nul-terminated since that just seems to make things easier, since (at least according to linux man pages) `sockaddr_un`s returned by the kernel will always be nul-terminated.


Co-authored-by: Noa <33094578+coolreader18@users.noreply.github.com>
2021-09-19 02:04:04 +00:00
Alan Somers
af29f2e958 Add docs for all sockopts that are documented by their respective OSes 2021-09-12 16:55:03 -06:00
Alan Somers
920809e41a Add the ability to set doc strings in sockopts' definitions 2021-09-12 11:00:57 -06:00
Conrad Meyer
d23e7c7575 Add support for IP_RECVERR and IPV6_RECVERR
Setting these options enables receiving errors, such as ICMP errors from
the network, via `recvmsg()` with `MSG_ERRQUEUE`.

Adds new `Ipv{4,6}RecvErr` variants to `ControlMessageOwned`.  These
control messages are produced when `Ipv4RecvErr` or `Ipv6RecvErr`
options are enabled on a raw or datagram socket.

New tests for the functionality can be run with `cargo test --test test
test_recverr`.

This commit builds on an earlier draft of the functionality authored by
Matthew McPherrin <git@mcpherrin.ca>.
2021-09-06 16:59:35 -07:00
Alan Somers
a757be75fc Fix some broken internal doc links in the socket code 2021-09-04 12:11:41 -06:00
Noa
8e963a093f
Rework UnixAddr to fix soundness issues 2021-08-29 17:25:41 -05:00
Kyle Huey
d133d3db76 Relax assertions in sockaddr_storage_to_addr to match the documentation.
Fixes #1479
2021-08-17 19:26:12 -07:00
Alan Somers
0d3bc089d5 Add support for LOCAL_PEER_CRED
On FreeBSD and its derivatives, this socket option gets the credentials
of the connected peer.
2021-08-10 17:57:32 -06:00
bors[bot]
3c45e08eae
Merge #1473 #1474 #1476
1473: sys/stat: add a safe wrapper for mknodat(2) r=asomers a=lucab

This introduces a new `mknodat` helper.

Ref: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html

1474: Mark most C-derived enums as non_exhaustive r=asomers a=asomers

Since libc may add new variants at any time, Nix's consumers should not
use exhaustive match patterns.

Fixes #1182

1476: Constify many functions r=asomers a=asomers

Constify most functions that can be constified.  The exceptions are
mostly accessors for structs that have no const constructor.

Co-authored-by: Luca BRUNO <luca.bruno@coreos.com>
Co-authored-by: Alan Somers <asomers@gmail.com>
2021-07-29 01:33:10 +00:00
Alan Somers
86acc26cbc Constify many functions
Constify most functions that can be constified.  The exceptions are
mostly accessors for structs that have no const constructor.
2021-07-24 18:25:44 -06:00
Alan Somers
358adafa27 Mark most C-derived enums as non_exhaustive
Since libc may add new variants at any time, Nix's consumers should not
use exhaustive match patterns.

Fixes #1182
2021-07-24 14:44:11 -06:00
Junho Choi
0b596a11e0 Support SO_RXQ_OVFL socket option (android/fuchsia/linux)
This PR implements support of RXQ_OVFL flag and parsing ControlMessage
to get the packet drop counter of UDP socket.
2021-07-08 16:00:39 -07:00