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 0655554 commit 9a9f245
Show file tree
Hide file tree
Showing 7 changed files with 786 additions and 156 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),
}
18 changes: 17 additions & 1 deletion src/tracing/net/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ pub fn dispatch_tcp_probe(
PortDirection::FixedDest(dest_port) => (probe.sequence.0, dest_port.0),
PortDirection::FixedBoth(_, _) | PortDirection::None => unimplemented!(),
};
let socket = platform::make_stream_socket_ipv4()?;
#[allow(unused_mut)]
let mut socket = platform::make_stream_socket_ipv4()?;
let local_addr = SocketAddr::new(IpAddr::V4(src_addr), src_port);
socket.bind(local_addr)?;
socket.set_ttl(u32::from(probe.ttl.0))?;
Expand All @@ -180,6 +181,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 +206,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
25 changes: 23 additions & 2 deletions src/tracing/net/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ pub fn dispatch_tcp_probe(
PortDirection::FixedDest(dest_port) => (probe.sequence.0, dest_port.0),
PortDirection::FixedBoth(_, _) | PortDirection::None => unimplemented!(),
};
let 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);
#[allow(unused_mut)]
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 +135,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 +162,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
Loading

0 comments on commit 9a9f245

Please sign in to comment.