Skip to content

Commit c39e32b

Browse files
authored
Overhaul the net types. (#691)
* Overhaul the `net` types. - Factor out per-backend src/backend/*/net/types.rs into a common src/net/types.rs source file. - Add `NETLINK_*`, `ETH_P_*`, and other protocol constants. - Remove `Protocol::default()` and change `socket`'s protocol argument to be `Option<Protocol>`, to better model how `0` means to use the default protocol. Fixes #688. * Add more documentation links. * Encode the `ETH_P_*` constants as 16-bit big-endian.
1 parent eb68ba3 commit c39e32b

File tree

24 files changed

+1454
-1015
lines changed

24 files changed

+1454
-1015
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ once_cell = { version = "1.5.2", optional = true }
3838
# libc backend can be selected via adding `--cfg=rustix_use_libc` to
3939
# `RUSTFLAGS` or enabling the `use-libc` cargo feature.
4040
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "mips", target_arch = "mips64", target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"))))'.dependencies]
41-
linux-raw-sys = { version = "0.4.1", default-features = false, features = ["general", "errno", "ioctl", "no_std"] }
41+
linux-raw-sys = { version = "0.4.2", default-features = false, features = ["general", "errno", "ioctl", "no_std"] }
4242
libc_errno = { package = "errno", version = "0.3.1", default-features = false, optional = true }
4343
libc = { version = "0.2.144", features = ["extra_traits"], optional = true }
4444

@@ -55,7 +55,7 @@ libc = { version = "0.2.144", features = ["extra_traits"] }
5555
# Some syscalls do not have libc wrappers, such as in `io_uring`. For these,
5656
# the libc backend uses the linux-raw-sys ABI and `libc::syscall`.
5757
[target.'cfg(all(any(target_os = "android", target_os = "linux"), any(rustix_use_libc, miri, not(all(target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "mips", target_arch = "mips64", target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64")))))))'.dependencies]
58-
linux-raw-sys = { version = "0.4.1", default-features = false, features = ["general", "ioctl", "no_std"] }
58+
linux-raw-sys = { version = "0.4.2", default-features = false, features = ["general", "ioctl", "no_std"] }
5959

6060
# For the libc backend on Windows, use the Winsock2 API in windows-sys.
6161
[target.'cfg(windows)'.dependencies.windows-sys]
@@ -148,7 +148,7 @@ fs = []
148148
io_uring = ["fs", "net", "linux-raw-sys/io_uring"]
149149

150150
# Enable `rustix::net::*`.
151-
net = ["linux-raw-sys/net"]
151+
net = ["linux-raw-sys/net", "linux-raw-sys/netlink", "linux-raw-sys/if_ether"]
152152

153153
# Enable `rustix::thread::*`.
154154
thread = ["linux-raw-sys/prctl"]

src/backend/libc/c.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,56 @@ pub(crate) const PROC_SUPER_MAGIC: u32 = 0x0000_9fa0;
1212
#[cfg(all(linux_kernel, target_env = "musl"))]
1313
pub(crate) const NFS_SUPER_MAGIC: u32 = 0x0000_6969;
1414

15+
// Submitted upstream in https://github.com/rust-lang/libc/pull/3272.
16+
#[cfg(all(linux_kernel, feature = "net"))]
17+
pub(crate) const IPPROTO_ETHERNET: c_int = linux_raw_sys::net::IPPROTO_ETHERNET as _;
18+
19+
// TODO: Upstream these.
20+
#[cfg(all(linux_kernel, feature = "net"))]
21+
pub(crate) const ETH_P_TSN: c_int = linux_raw_sys::if_ether::ETH_P_TSN as _;
22+
#[cfg(all(linux_kernel, feature = "net"))]
23+
pub(crate) const ETH_P_ERSPAN2: c_int = linux_raw_sys::if_ether::ETH_P_ERSPAN2 as _;
24+
#[cfg(all(linux_kernel, feature = "net"))]
25+
pub(crate) const ETH_P_ERSPAN: c_int = linux_raw_sys::if_ether::ETH_P_ERSPAN as _;
26+
#[cfg(all(linux_kernel, feature = "net"))]
27+
pub(crate) const ETH_P_PROFINET: c_int = linux_raw_sys::if_ether::ETH_P_PROFINET as _;
28+
#[cfg(all(linux_kernel, feature = "net"))]
29+
pub(crate) const ETH_P_REALTEK: c_int = linux_raw_sys::if_ether::ETH_P_REALTEK as _;
30+
#[cfg(all(linux_kernel, feature = "net"))]
31+
pub(crate) const ETH_P_ETHERCAT: c_int = linux_raw_sys::if_ether::ETH_P_ETHERCAT as _;
32+
#[cfg(all(linux_kernel, feature = "net"))]
33+
pub(crate) const ETH_P_PREAUTH: c_int = linux_raw_sys::if_ether::ETH_P_PREAUTH as _;
34+
#[cfg(all(linux_kernel, feature = "net"))]
35+
pub(crate) const ETH_P_LLDP: c_int = linux_raw_sys::if_ether::ETH_P_LLDP as _;
36+
#[cfg(all(linux_kernel, feature = "net"))]
37+
pub(crate) const ETH_P_MRP: c_int = linux_raw_sys::if_ether::ETH_P_MRP as _;
38+
#[cfg(all(linux_kernel, feature = "net"))]
39+
pub(crate) const ETH_P_NCSI: c_int = linux_raw_sys::if_ether::ETH_P_NCSI as _;
40+
#[cfg(all(linux_kernel, feature = "net"))]
41+
pub(crate) const ETH_P_CFM: c_int = linux_raw_sys::if_ether::ETH_P_CFM as _;
42+
#[cfg(all(linux_kernel, feature = "net"))]
43+
pub(crate) const ETH_P_IBOE: c_int = linux_raw_sys::if_ether::ETH_P_IBOE as _;
44+
#[cfg(all(linux_kernel, feature = "net"))]
45+
pub(crate) const ETH_P_HSR: c_int = linux_raw_sys::if_ether::ETH_P_HSR as _;
46+
#[cfg(all(linux_kernel, feature = "net"))]
47+
pub(crate) const ETH_P_NSH: c_int = linux_raw_sys::if_ether::ETH_P_NSH as _;
48+
#[cfg(all(linux_kernel, feature = "net"))]
49+
pub(crate) const ETH_P_DSA_8021Q: c_int = linux_raw_sys::if_ether::ETH_P_DSA_8021Q as _;
50+
#[cfg(all(linux_kernel, feature = "net"))]
51+
pub(crate) const ETH_P_DSA_A5PSW: c_int = linux_raw_sys::if_ether::ETH_P_DSA_A5PSW as _;
52+
#[cfg(all(linux_kernel, feature = "net"))]
53+
pub(crate) const ETH_P_IFE: c_int = linux_raw_sys::if_ether::ETH_P_IFE as _;
54+
#[cfg(all(linux_kernel, feature = "net"))]
55+
pub(crate) const ETH_P_CAN: c_int = linux_raw_sys::if_ether::ETH_P_CAN as _;
56+
#[cfg(all(linux_kernel, feature = "net"))]
57+
pub(crate) const ETH_P_CANXL: c_int = linux_raw_sys::if_ether::ETH_P_CANXL as _;
58+
#[cfg(all(linux_kernel, feature = "net"))]
59+
pub(crate) const ETH_P_XDSA: c_int = linux_raw_sys::if_ether::ETH_P_XDSA as _;
60+
#[cfg(all(linux_kernel, feature = "net"))]
61+
pub(crate) const ETH_P_MAP: c_int = linux_raw_sys::if_ether::ETH_P_MAP as _;
62+
#[cfg(all(linux_kernel, feature = "net"))]
63+
pub(crate) const ETH_P_MCTP: c_int = linux_raw_sys::if_ether::ETH_P_MCTP as _;
64+
1565
#[cfg(all(
1666
linux_kernel,
1767
any(

src/backend/libc/event/epoll.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
//! use rustix::fd::AsFd;
1414
//! use rustix::io::{ioctl_fionbio, read, write};
1515
//! use rustix::net::{
16-
//! accept, bind_v4, listen, socket, AddressFamily, Ipv4Addr, Protocol, SocketAddrV4,
17-
//! SocketType,
16+
//! accept, bind_v4, listen, socket, AddressFamily, Ipv4Addr, SocketAddrV4, SocketType,
1817
//! };
1918
//! use std::collections::HashMap;
2019
//! use std::os::unix::io::AsRawFd;
2120
//!
2221
//! // Create a socket and listen on it.
23-
//! let listen_sock = socket(AddressFamily::INET, SocketType::STREAM, Protocol::default())?;
22+
//! let listen_sock = socket(AddressFamily::INET, SocketType::STREAM, None)?;
2423
//! bind_v4(&listen_sock, &SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0))?;
2524
//! listen(&listen_sock, 1)?;
2625
//!

src/backend/libc/net/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ pub(crate) mod msghdr;
55
pub(crate) mod read_sockaddr;
66
pub(crate) mod send_recv;
77
pub(crate) mod syscalls;
8-
pub(crate) mod types;
98
pub(crate) mod write_sockaddr;

src/backend/libc/net/syscalls.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use {
2020
use {
2121
super::read_sockaddr::{initialize_family_to_unspec, maybe_read_sockaddr_os, read_sockaddr_os},
2222
super::send_recv::{RecvFlags, SendFlags},
23-
super::types::{AddressFamily, Protocol, Shutdown, SocketFlags, SocketType},
2423
super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6},
24+
crate::net::{AddressFamily, Protocol, Shutdown, SocketFlags, SocketType},
2525
core::ptr::null_mut,
2626
};
2727

@@ -142,13 +142,17 @@ pub(crate) fn sendto_unix(
142142
pub(crate) fn socket(
143143
domain: AddressFamily,
144144
type_: SocketType,
145-
protocol: Protocol,
145+
protocol: Option<Protocol>,
146146
) -> io::Result<OwnedFd> {
147+
let raw_protocol = match protocol {
148+
Some(p) => p.0.get(),
149+
None => 0,
150+
};
147151
unsafe {
148152
ret_owned_fd(c::socket(
149153
domain.0 as c::c_int,
150154
type_.0 as c::c_int,
151-
protocol.0,
155+
raw_protocol as c::c_int,
152156
))
153157
}
154158
}
@@ -158,13 +162,17 @@ pub(crate) fn socket_with(
158162
domain: AddressFamily,
159163
type_: SocketType,
160164
flags: SocketFlags,
161-
protocol: Protocol,
165+
protocol: Option<Protocol>,
162166
) -> io::Result<OwnedFd> {
167+
let raw_protocol = match protocol {
168+
Some(p) => p.0.get(),
169+
None => 0,
170+
};
163171
unsafe {
164172
ret_owned_fd(c::socket(
165173
domain.0 as c::c_int,
166-
type_.0 as c::c_int | flags.bits(),
167-
protocol.0,
174+
(type_.0 | flags.bits()) as c::c_int,
175+
raw_protocol as c::c_int,
168176
))
169177
}
170178
}
@@ -339,7 +347,7 @@ pub(crate) fn accept_with(sockfd: BorrowedFd<'_>, flags: SocketFlags) -> io::Res
339347
borrowed_fd(sockfd),
340348
null_mut(),
341349
null_mut(),
342-
flags.bits(),
350+
flags.bits() as c::c_int,
343351
))?;
344352
Ok(owned_fd)
345353
}
@@ -380,7 +388,7 @@ pub(crate) fn acceptfrom_with(
380388
borrowed_fd(sockfd),
381389
storage.as_mut_ptr().cast(),
382390
&mut len,
383-
flags.bits(),
391+
flags.bits() as c::c_int,
384392
))?;
385393
Ok((
386394
owned_fd,
@@ -447,14 +455,18 @@ pub(crate) fn socketpair(
447455
domain: AddressFamily,
448456
type_: SocketType,
449457
flags: SocketFlags,
450-
protocol: Protocol,
458+
protocol: Option<Protocol>,
451459
) -> io::Result<(OwnedFd, OwnedFd)> {
460+
let raw_protocol = match protocol {
461+
Some(p) => p.0.get(),
462+
None => 0,
463+
};
452464
unsafe {
453465
let mut fds = MaybeUninit::<[OwnedFd; 2]>::uninit();
454466
ret(c::socketpair(
455467
c::c_int::from(domain.0),
456-
type_.0 as c::c_int | flags.bits(),
457-
protocol.0,
468+
(type_.0 | flags.bits()) as c::c_int,
469+
raw_protocol as c::c_int,
458470
fds.as_mut_ptr().cast::<c::c_int>(),
459471
))?;
460472

0 commit comments

Comments
 (0)