Skip to content

Commit

Permalink
fixup: WIP change to recv_icmp_probe
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Dec 29, 2022
1 parent 939aedf commit a32806e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 43 deletions.
15 changes: 0 additions & 15 deletions src/tracing/net/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ pub fn dispatch_tcp_probe(
Ok(socket)
}

#[cfg(unix)]
pub fn recv_icmp_probe(
recv_socket: &mut Socket,
protocol: TracerProtocol,
Expand All @@ -205,20 +204,6 @@ pub fn recv_icmp_probe(
}
}

#[cfg(windows)]
pub fn recv_icmp_probe(
recv_socket: &mut Socket,
protocol: TracerProtocol,
multipath_strategy: MultipathStrategy,
direction: PortDirection,
) -> TraceResult<Option<ProbeResponse>> {
let bytes = &recv_socket.buf_bytes();
let ipv4 = Ipv4Packet::new_view(bytes).req()?;
// post the WSARecvFrom again, so that the next OVERLAPPED event can get triggered
recv_socket.recv_from()?;
extract_probe_resp(protocol, multipath_strategy, direction, &ipv4)
}

pub fn recv_tcp_socket(
tcp_socket: &Socket,
sequence: Sequence,
Expand Down
20 changes: 0 additions & 20 deletions src/tracing/net/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ pub fn dispatch_tcp_probe(
Ok(socket)
}

#[cfg(unix)]
pub fn recv_icmp_probe(
recv_socket: &mut Socket,
protocol: TracerProtocol,
Expand All @@ -161,25 +160,6 @@ pub fn recv_icmp_probe(
}
}

#[cfg(windows)]
#[allow(unsafe_code)]
pub fn recv_icmp_probe(
recv_socket: &mut Socket,
protocol: TracerProtocol,
direction: PortDirection,
) -> TraceResult<Option<ProbeResponse>> {
let bytes = &recv_socket.buf_bytes();
let icmp_v6 = IcmpPacket::new_view(bytes).req()?;
let addr = recv_socket.from()?;
// post the WSARecvFrom again, so that the next OVERLAPPED event can get triggered
recv_socket.recv_from()?;
if let IpAddr::V6(src_addr) = addr {
extract_probe_resp(protocol, direction, &icmp_v6, src_addr)
} else {
Err(TracerError::InvalidSourceAddr(addr))
}
}

pub fn recv_tcp_socket(
tcp_socket: &Socket,
sequence: Sequence,
Expand Down
25 changes: 17 additions & 8 deletions src/tracing/net/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::convert;
use std::alloc::{alloc, dealloc, Layout};
use std::ffi::c_void;
use std::fmt::{self};
use std::io;
use std::io::{Error, ErrorKind, Result};
use std::mem::MaybeUninit;
use std::mem::{align_of, size_of};
Expand Down Expand Up @@ -64,12 +65,20 @@ impl Socket {
Ok(s)
}

pub fn recv_from(&mut self, buf: &mut [u8]) -> Result<(usize, Option<SocketAddr>)> {
let addr = self.from().map_err(|_| io::Error::last_os_error())?;
let len = self.read(buf)?;
Ok((len, Some(SocketAddr::new(addr, 0))))
}

#[allow(unsafe_code)]
#[must_use]
pub fn buf_bytes(&self) -> Vec<u8> {
let buf = self.wbuf.buf.as_ptr();
let slice = unsafe { std::slice::from_raw_parts(buf, self.wbuf.len as usize) };
slice.to_owned()
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let buf_ptr = self.wbuf.buf.as_ptr();
let slice = unsafe { std::slice::from_raw_parts(buf_ptr, self.wbuf.len as usize) };
buf.copy_from_slice(slice);
self.post_recv_from()
.map_err(|_| io::Error::last_os_error())?;
Ok(self.wbuf.len as usize)
}

pub fn from(&mut self) -> TraceResult<IpAddr> {
Expand Down Expand Up @@ -236,7 +245,7 @@ impl Socket {

#[allow(unsafe_code)]
#[allow(clippy::cast_possible_wrap)]
pub fn recv_from(&mut self) -> TraceResult<()> {
pub fn post_recv_from(&mut self) -> TraceResult<()> {
let mut fromlen = std::mem::size_of::<SOCKADDR_STORAGE>() as i32;
let ret = unsafe {
WSARecvFrom(
Expand Down Expand Up @@ -684,7 +693,7 @@ pub fn make_udp_send_socket_ipv4() -> TraceResult<Socket> {
pub fn make_recv_socket_ipv4(src_addr: Ipv4Addr) -> TraceResult<Socket> {
let mut sock = Socket::create(AF_INET, SOCK_RAW, IPPROTO_ICMP)?;
sock.bind(SocketAddr::new(IpAddr::V4(src_addr), 0))?;
sock.recv_from()?;
sock.post_recv_from()?;
sock.set_non_blocking(true)?;
sock.set_header_included(true)?;
Ok(sock)
Expand All @@ -705,7 +714,7 @@ pub fn make_udp_send_socket_ipv6() -> TraceResult<Socket> {
pub fn make_recv_socket_ipv6(src_addr: Ipv6Addr) -> TraceResult<Socket> {
let mut sock = Socket::create(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)?;
sock.bind(SocketAddr::new(IpAddr::V6(src_addr), 0))?;
sock.recv_from()?;
sock.post_recv_from()?;
sock.set_non_blocking(true)?;
Ok(sock)
}
Expand Down

0 comments on commit a32806e

Please sign in to comment.