Skip to content

Commit 4c95612

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

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-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
}

test/sys/test_socket.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,3 +2685,32 @@ pub fn test_txtime() {
26852685
recvmsg::<()>(rsock.as_raw_fd(), &mut iov2, None, MsgFlags::empty())
26862686
.unwrap();
26872687
}
2688+
2689+
#[test]
2690+
pub fn test_icmp() {
2691+
// _ICMP_ECHO_REQUEST = 8
2692+
// _ICMP_ECHO_REPLY = 0
2693+
use nix::sys::socket::{
2694+
bind, recv, sendto, setsockopt, socket, sockopt, ControlMessage,
2695+
MsgFlags, SockFlag, SockType, SockaddrIn, SockProtocol
2696+
};
2697+
2698+
const ECHO_REQUEST: &[u8] = &[
2699+
// ICMP Type = Echo Request
2700+
0x08,
2701+
// ICMP Code = 0 (N/A)
2702+
0x00,
2703+
// Checksum
2704+
0x8f, 0x53,
2705+
// Identifier
2706+
0x12, 0x34,
2707+
// Sequence Number
2708+
0x56, 0x78,
2709+
];
2710+
2711+
let sock = socket(AddressFamily::Inet, SockType::Raw, SockFlag::empty(), SockProtocol::Icmp).unwrap();
2712+
let sock_addr = SockaddrIn::new(127, 0, 0, 1, 0);
2713+
2714+
let rc = sendto(sock.as_raw_fd(), ECHO_REQUEST, &sock_addr, MsgFlags::empty()).unwrap();
2715+
assert_eq!(rc, ECHO_REQUEST.len());
2716+
}

0 commit comments

Comments
 (0)