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 26, 2023
1 parent 0bc68a1 commit 7b52b87
Show file tree
Hide file tree
Showing 3 changed files with 18 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
10 changes: 7 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 @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions src/tracing/net/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,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 @@ -212,7 +214,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 7b52b87

Please sign in to comment.