UDP socket should set different IP_TRANSPARENT and IPV6_TRANSPARENT

This commit is contained in:
zonyitoo
2020-11-03 01:17:47 +08:00
parent a4d684c81d
commit 2a6451f0fe
2 changed files with 18 additions and 9 deletions
+1 -1
View File
@@ -101,7 +101,7 @@ fn create_redir_listener(addr: &SocketAddr) -> io::Result<TcpListener> {
let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))?;
// For Linux 2.4+ TPROXY
// Sockets have to set IP_TRANSPARENT for retrieving original destination by getsockname()
// Sockets have to set IP_TRANSPARENT, IPV6_TRANSPARENT for retrieving original destination by getsockname()
unsafe {
let fd = socket.as_raw_fd();
+17 -8
View File
@@ -103,14 +103,23 @@ fn set_socket_before_bind(addr: &SocketAddr, socket: &Socket) -> io::Result<()>
let enable: libc::c_int = 1;
unsafe {
// 1. Set IP_TRANSPARENT to allow binding to non-local addresses
let ret = libc::setsockopt(
fd,
libc::SOL_IP,
libc::IP_TRANSPARENT,
&enable as *const _ as *const _,
mem::size_of_val(&enable) as libc::socklen_t,
);
// 1. Set IP_TRANSPARENT, IPV6_TRANSPARENT to allow binding to non-local addresses
let ret = match *addr {
SocketAddr::V4(..) => libc::setsockopt(
fd,
libc::SOL_IP,
libc::IP_TRANSPARENT,
&enable as *const _ as *const _,
mem::size_of_val(&enable) as libc::socklen_t,
),
SocketAddr::V6(..) => libc::setsockopt(
fd,
libc::SOL_IPV6,
libc::IPV6_TRANSPARENT,
&enable as *const _ as *const _,
mem::size_of_val(&enable) as libc::socklen_t,
),
};
if ret != 0 {
return Err(Error::last_os_error());
}