Skip to content

Commit

Permalink
feat(net): add support for ping sockets (#101) - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Aug 12, 2023
1 parent a297db1 commit 1316094
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/caps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
11 changes: 8 additions & 3 deletions src/tracing/net/channel.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -48,10 +48,15 @@ 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)?;

// TODO why do we always create both of these?
let icmp_send_socket = make_icmp_send_socket(config.source_addr)?;
let udp_send_socket = make_udp_send_socket(config.source_addr)?;

let recv_socket = make_recv_socket(config.source_addr)?;
Ok(Self {
protocol: config.protocol,
Expand Down
10 changes: 8 additions & 2 deletions src/tracing/net/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ impl Socket {
impl TracerSocket for Socket {
#[instrument]
fn new_icmp_send_socket_ipv4() -> IoResult<Self> {
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)
Expand All @@ -198,6 +200,8 @@ impl TracerSocket for Socket {
}
#[instrument]
fn new_udp_send_socket_ipv4() -> IoResult<Self> {
// TODO
// let socket = Self::new(Domain::IPV4, Type::DGRAM, Protocol::UDP)?;
let socket = Self::new_raw_ipv4(Protocol::from(nix::libc::IPPROTO_RAW))?;
socket.set_nonblocking(true)?;
socket.set_header_included(true)?;
Expand All @@ -211,7 +215,9 @@ impl TracerSocket for Socket {
}
#[instrument]
fn new_recv_socket_ipv4(addr: Ipv4Addr) -> IoResult<Self> {
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)
Expand Down

0 comments on commit 1316094

Please sign in to comment.