Skip to content

Commit

Permalink
feat: add support for Windows (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
zarkdav authored and fujiapple852 committed Dec 28, 2022
1 parent b1ccd00 commit 03bddaf
Show file tree
Hide file tree
Showing 5 changed files with 749 additions and 165 deletions.
2 changes: 2 additions & 0 deletions src/tracing/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ pub enum TracerError {
AddressNotAvailable(SocketAddr),
#[error("invalid source IP address: {0}")]
InvalidSourceAddr(IpAddr),
#[error("error: {0}")]
ErrorString(String),
}
15 changes: 15 additions & 0 deletions src/tracing/net/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub fn dispatch_tcp_probe(
Ok(socket)
}

#[cfg(unix)]
pub fn recv_icmp_probe(
recv_socket: &mut Socket,
protocol: TracerProtocol,
Expand All @@ -204,6 +205,20 @@ 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
24 changes: 22 additions & 2 deletions src/tracing/net/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ pub fn dispatch_tcp_probe(
PortDirection::FixedDest(dest_port) => (probe.sequence.0, dest_port.0),
PortDirection::FixedBoth(_, _) | PortDirection::None => unimplemented!(),
};
let mut socket = platform::make_stream_socket_ipv6()?;
let local_addr = SocketAddr::new(IpAddr::V6(src_addr), src_port);
let remote_addr = SocketAddr::new(IpAddr::V6(dest_addr), dest_port);
let mut socket = platform::make_stream_socket_ipv6()?;
socket.bind(local_addr)?;
socket.set_unicast_hops_v6(probe.ttl.0)?;
let remote_addr = SocketAddr::new(IpAddr::V6(dest_addr), dest_port);
match socket.connect(remote_addr) {
Ok(_) => {}
Err(err) => {
Expand All @@ -134,6 +134,7 @@ pub fn dispatch_tcp_probe(
Ok(socket)
}

#[cfg(unix)]
pub fn recv_icmp_probe(
recv_socket: &mut Socket,
protocol: TracerProtocol,
Expand All @@ -160,6 +161,25 @@ 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
2 changes: 1 addition & 1 deletion src/tracing/net/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl Socket {
self.inner.take_error()
}

#[allow(dead_code, clippy::unused_self)]
#[allow(clippy::unused_self)]
pub fn icmp_error_info(&self) -> io::Result<IpAddr> {
unimplemented!()
}
Expand Down
Loading

0 comments on commit 03bddaf

Please sign in to comment.