Skip to content

Commit

Permalink
add support for tproxy(udp)
Browse files Browse the repository at this point in the history
  • Loading branch information
VendettaReborn committed Mar 27, 2024
1 parent 60b63cb commit cd3e4ec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub struct RecvMeta {
pub dst_local_ip: Option<IpAddr>,
/// interface index that datagram was received on
pub ifindex: u32,
pub orig_dst: Option<SocketAddr>,
}

impl Default for RecvMeta {
Expand All @@ -89,6 +90,7 @@ impl Default for RecvMeta {
dst_ip: None,
dst_local_ip: None,
ifindex: 0,
orig_dst: None,
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ fn decode_recv(
let mut ifindex = 0;
#[allow(unused_mut)] // only mutable on Linux
let mut stride = len;
let mut orig_dst = None;

let cmsg_iter = unsafe { cmsg::Iter::new(hdr) };
for cmsg in cmsg_iter {
Expand Down Expand Up @@ -1118,6 +1119,13 @@ fn decode_recv(
(libc::SOL_UDP, libc::UDP_GRO) => unsafe {
stride = cmsg::decode::<libc::c_int>(cmsg) as usize;
},
#[cfg(target_os = "linux")]
(libc::SOL_IP, libc::IP_ORIGDSTADDR) => unsafe {
let addr_in = cmsg::decode::<libc::sockaddr_in>(cmsg);
let addr = Ipv4Addr::from(addr_in.sin_addr.s_addr.to_ne_bytes());
let port = u16::from_be(addr_in.sin_port);
orig_dst = Some(SocketAddr::V4(SocketAddrV4::new(addr, port)));
},
_ => {}
}
}
Expand Down Expand Up @@ -1153,6 +1161,7 @@ fn decode_recv(
dst_ip,
dst_local_ip,
ifindex,
orig_dst,
}
}

Expand Down

0 comments on commit cd3e4ec

Please sign in to comment.