Skip to content

Commit bc52d14

Browse files
committed
Add SockProtocol variants for ICMP{,v6}
1 parent 2d18d63 commit bc52d14

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
2929
- Removed `flock` from `::nix::fcntl` on Solaris. ([#2082](https://github.com/nix-rust/nix/pull/2082))
3030
- Use I/O safety with `copy_file_range`, and expose it on FreeBSD.
3131
(#[1906](https://github.com/nix-rust/nix/pull/1906))
32+
- Added `Icmp` and `IcmpV6` to `SockProtocol`.
33+
(#[2103](https://github.com/nix-rust/nix/pull/2103))
3234

3335
### Changed
3436

src/sys/socket/mod.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,6 @@ pub enum SockProtocol {
132132
Udp = libc::IPPROTO_UDP,
133133
/// Raw sockets ([raw(7)](https://man7.org/linux/man-pages/man7/raw.7.html))
134134
Raw = libc::IPPROTO_RAW,
135-
/// Allows applications and other KEXTs to be notified when certain kernel events occur
136-
/// ([ref](https://developer.apple.com/library/content/documentation/Darwin/Conceptual/NKEConceptual/control/control.html))
137-
#[cfg(any(target_os = "ios", target_os = "macos"))]
138-
#[cfg_attr(docsrs, doc(cfg(all())))]
139-
KextEvent = libc::SYSPROTO_EVENT,
140135
/// Allows applications to configure and control a KEXT
141136
/// ([ref](https://developer.apple.com/library/content/documentation/Darwin/Conceptual/NKEConceptual/control/control.html))
142137
#[cfg(any(target_os = "ios", target_os = "macos"))]
@@ -221,20 +216,33 @@ pub enum SockProtocol {
221216
#[cfg(any(target_os = "android", target_os = "linux"))]
222217
#[cfg_attr(docsrs, doc(cfg(all())))]
223218
EthAll = (libc::ETH_P_ALL as u16).to_be() as i32,
219+
/// ICMP protocol ([icmp(7)](https://man7.org/linux/man-pages/man7/icmp.7.html))
220+
Icmp = libc::IPPROTO_ICMP,
221+
/// ICMPv6 protocol (ICMP over IPv6)
222+
IvmpV6 = libc::IPPROTO_ICMPV6,
223+
}
224+
225+
impl SockProtocol {
224226
/// The Controller Area Network raw socket protocol
225227
/// ([ref](https://docs.kernel.org/networking/can.html#how-to-use-socketcan))
226228
#[cfg(target_os = "linux")]
227229
#[cfg_attr(docsrs, doc(cfg(all())))]
228-
CanRaw = libc::CAN_RAW,
229-
}
230+
#[allow(non_upper_case_globals)]
231+
pub const CanRaw: SockProtocol = SockProtocol::Icmp; // Matches libc::CAN_RAW
230232

231-
impl SockProtocol {
232233
/// The Controller Area Network broadcast manager protocol
233234
/// ([ref](https://docs.kernel.org/networking/can.html#how-to-use-socketcan))
234235
#[cfg(target_os = "linux")]
235236
#[cfg_attr(docsrs, doc(cfg(all())))]
236237
#[allow(non_upper_case_globals)]
237238
pub const CanBcm: SockProtocol = SockProtocol::NetlinkUserSock; // Matches libc::CAN_BCM
239+
240+
/// Allows applications and other KEXTs to be notified when certain kernel events occur
241+
/// ([ref](https://developer.apple.com/library/content/documentation/Darwin/Conceptual/NKEConceptual/control/control.html))
242+
#[cfg(any(target_os = "ios", target_os = "macos"))]
243+
#[cfg_attr(docsrs, doc(cfg(all())))]
244+
#[allow(non_upper_case_globals)]
245+
pub const KextEvent: SockProtocol = SockProtocol::Icmp; // Matches libc::SYSPROTO_EVENT
238246
}
239247
#[cfg(any(target_os = "android", target_os = "linux"))]
240248
libc_bitflags! {
@@ -2441,4 +2449,15 @@ mod tests {
24412449
)
24422450
.expect("Failed to open routing socket");
24432451
}
2452+
#[test]
2453+
fn sock_protocol_constant_matches() {
2454+
#[cfg(target_os = "linux")]
2455+
assert_eq!(super::SockProtocol::CanRaw as libc::c_int, libc::CAN_RAW);
2456+
2457+
#[cfg(target_os = "linux")]
2458+
assert_eq!(super::SockProtocol::CanBcm as libc::c_int, libc::CAN_BCM);
2459+
2460+
#[cfg(any(target_os = "ios", target_os = "macos"))]
2461+
assert_eq!(super::SockProtocol::KextEvent as libc::c_int, libc::SYSPROTO_EVENT);
2462+
}
24442463
}

0 commit comments

Comments
 (0)