Skip to content

Commit 173bde1

Browse files
authored
Add SockProtocol variants for ICMP{,v6} (#2103)
1 parent 9f4e877 commit 173bde1

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
3838

3939
- Simplified the function signatures of `recvmmsg` and `sendmmsg`
4040

41+
### Added
42+
- Added `Icmp` and `IcmpV6` to `SockProtocol`.
43+
(#[2103](https://github.com/nix-rust/nix/pull/2103))
44+
4145
## [0.27.1] - 2023-08-28
4246

4347
### Fixed

src/sys/socket/mod.rs

+16-8
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"))]
@@ -231,20 +226,33 @@ pub enum SockProtocol {
231226
#[cfg(any(target_os = "android", target_os = "linux"))]
232227
#[cfg_attr(docsrs, doc(cfg(all())))]
233228
EthAll = (libc::ETH_P_ALL as u16).to_be() as i32,
229+
/// ICMP protocol ([icmp(7)](https://man7.org/linux/man-pages/man7/icmp.7.html))
230+
Icmp = libc::IPPROTO_ICMP,
231+
/// ICMPv6 protocol (ICMP over IPv6)
232+
IcmpV6 = libc::IPPROTO_ICMPV6,
233+
}
234+
235+
impl SockProtocol {
234236
/// The Controller Area Network raw socket protocol
235237
/// ([ref](https://docs.kernel.org/networking/can.html#how-to-use-socketcan))
236238
#[cfg(target_os = "linux")]
237239
#[cfg_attr(docsrs, doc(cfg(all())))]
238-
CanRaw = libc::CAN_RAW,
239-
}
240+
#[allow(non_upper_case_globals)]
241+
pub const CanRaw: SockProtocol = SockProtocol::Icmp; // Matches libc::CAN_RAW
240242

241-
impl SockProtocol {
242243
/// The Controller Area Network broadcast manager protocol
243244
/// ([ref](https://docs.kernel.org/networking/can.html#how-to-use-socketcan))
244245
#[cfg(target_os = "linux")]
245246
#[cfg_attr(docsrs, doc(cfg(all())))]
246247
#[allow(non_upper_case_globals)]
247248
pub const CanBcm: SockProtocol = SockProtocol::NetlinkUserSock; // Matches libc::CAN_BCM
249+
250+
/// Allows applications and other KEXTs to be notified when certain kernel events occur
251+
/// ([ref](https://developer.apple.com/library/content/documentation/Darwin/Conceptual/NKEConceptual/control/control.html))
252+
#[cfg(any(target_os = "ios", target_os = "macos"))]
253+
#[cfg_attr(docsrs, doc(cfg(all())))]
254+
#[allow(non_upper_case_globals)]
255+
pub const KextEvent: SockProtocol = SockProtocol::Icmp; // Matches libc::SYSPROTO_EVENT
248256
}
249257
#[cfg(any(target_os = "android", target_os = "linux"))]
250258
libc_bitflags! {

test/sys/test_socket.rs

+33
Original file line numberDiff line numberDiff line change
@@ -2685,3 +2685,36 @@ pub fn test_txtime() {
26852685
recvmsg::<()>(rsock.as_raw_fd(), &mut iov2, None, MsgFlags::empty())
26862686
.unwrap();
26872687
}
2688+
2689+
// cfg needed for capability check.
2690+
#[cfg(any(target_os = "android", target_os = "linux"))]
2691+
#[test]
2692+
fn test_icmp_protocol() {
2693+
use nix::sys::socket::{
2694+
sendto, socket, AddressFamily, MsgFlags, SockFlag, SockProtocol,
2695+
SockType, SockaddrIn,
2696+
};
2697+
2698+
require_capability!("test_icmp_protocol", CAP_NET_RAW);
2699+
2700+
let owned_fd = socket(
2701+
AddressFamily::Inet,
2702+
SockType::Raw,
2703+
SockFlag::empty(),
2704+
SockProtocol::Icmp,
2705+
)
2706+
.unwrap();
2707+
2708+
// Send a minimal ICMP packet with no payload.
2709+
let packet = [
2710+
0x08, // Type
2711+
0x00, // Code
2712+
0x84, 0x85, // Checksum
2713+
0x73, 0x8a, // ID
2714+
0x00, 0x00, // Sequence Number
2715+
];
2716+
2717+
let dest_addr = SockaddrIn::new(127, 0, 0, 1, 0);
2718+
sendto(owned_fd.as_raw_fd(), &packet, &dest_addr, MsgFlags::empty())
2719+
.unwrap();
2720+
}

0 commit comments

Comments
 (0)