From 7b52b8741e2e536dbfe3a12db11cbe08d1038e97 Mon Sep 17 00:00:00 2001 From: FujiApple Date: Sat, 22 Jul 2023 14:10:44 +0800 Subject: [PATCH] feat(net): add support for ping sockets (#101) - WIP --- src/caps.rs | 9 +++++---- src/tracing/net/channel.rs | 10 +++++++--- src/tracing/net/platform/unix.rs | 8 ++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/caps.rs b/src/caps.rs index cabebbb5a..bc620f4e6 100644 --- a/src/caps.rs +++ b/src/caps.rs @@ -25,10 +25,11 @@ pub fn drop_caps() -> anyhow::Result<()> { #[allow(clippy::unnecessary_wraps)] /// Ensure the effective user is `root`. pub fn ensure_caps() -> anyhow::Result<()> { - if !nix::unistd::Uid::effective().is_root() { - eprintln!("root user required to use raw sockets, see https://github.com/fujiapple852/trippy#privileges"); - std::process::exit(-1); - } + // TODO we need a way to know if we're going to need caps before we check this + // if !nix::unistd::Uid::effective().is_root() { + // eprintln!("root user required to use raw sockets, see https://github.com/fujiapple852/trippy#privileges"); + // std::process::exit(-1); + // } Ok(()) } diff --git a/src/tracing/net/channel.rs b/src/tracing/net/channel.rs index 035fe0629..657790ddf 100644 --- a/src/tracing/net/channel.rs +++ b/src/tracing/net/channel.rs @@ -1,5 +1,5 @@ use crate::tracing::error::{TraceResult, TracerError}; -use crate::tracing::net::platform::Socket; +use crate::tracing::net::platform::{PlatformIpv4FieldByteOrder, Socket}; use crate::tracing::net::socket::TracerSocket as _; use crate::tracing::net::{ipv4, ipv6, platform, Network}; use crate::tracing::probe::ProbeResponse; @@ -47,13 +47,17 @@ impl TracerChannel { ))); } platform::startup()?; - let ipv4_length_order = - platform::PlatformIpv4FieldByteOrder::for_address(config.source_addr)?; + + // TODO Can we probe this without root? + let ipv4_length_order = PlatformIpv4FieldByteOrder::Host; + // platform::PlatformIpv4FieldByteOrder::for_address(config.source_addr)?; + let send_socket = match config.protocol { TracerProtocol::Icmp => Some(make_icmp_send_socket(config.source_addr)?), TracerProtocol::Udp => Some(make_udp_send_socket(config.source_addr)?), TracerProtocol::Tcp => None, }; + let recv_socket = make_recv_socket(config.source_addr)?; Ok(Self { protocol: config.protocol, diff --git a/src/tracing/net/platform/unix.rs b/src/tracing/net/platform/unix.rs index 3c18fa7a5..3f1592df8 100644 --- a/src/tracing/net/platform/unix.rs +++ b/src/tracing/net/platform/unix.rs @@ -186,7 +186,9 @@ impl Socket { impl TracerSocket for Socket { #[instrument] fn new_icmp_send_socket_ipv4() -> IoResult { - let socket = Self::new_raw_ipv4(Protocol::from(nix::libc::IPPROTO_RAW))?; + let socket = Self::new(Domain::IPV4, Type::DGRAM, Protocol::ICMPV4)?; + // TODO + // let socket = Self::new_raw_ipv4(Protocol::from(nix::libc::IPPROTO_RAW))?; socket.set_nonblocking(true)?; socket.set_header_included(true)?; Ok(socket) @@ -212,7 +214,9 @@ impl TracerSocket for Socket { } #[instrument] fn new_recv_socket_ipv4(addr: Ipv4Addr) -> IoResult { - let socket = Self::new_raw_ipv4(Protocol::ICMPV4)?; + let socket = Self::new(Domain::IPV4, Type::DGRAM, Protocol::ICMPV4)?; + // TODO + // let socket = Self::new_raw_ipv4(Protocol::ICMPV4)?; socket.set_nonblocking(true)?; socket.set_header_included(true)?; Ok(socket)