Skip to content

Commit 4c9eede

Browse files
committed
Add TPROXY-related Linux things
Should close #1023. # Conflicts: # src/sys/socket/mod.rs
1 parent 90f9301 commit 4c9eede

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/sys/socket/mod.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,17 @@ pub enum ControlMessageOwned {
463463
target_os = "openbsd",
464464
))]
465465
Ipv4RecvDstAddr(libc::in_addr),
466+
#[cfg(any(
467+
target_os = "linux",
468+
target_os = "android",
469+
))]
470+
OrigDstAddrV4(libc::sockaddr_in),
471+
#[cfg(any(
472+
target_os = "linux",
473+
target_os = "android",
474+
))]
475+
OrigDstAddrV6(libc::sockaddr_in6),
476+
466477
/// Catch-all variant for unimplemented cmsg types.
467478
#[doc(hidden)]
468479
Unknown(UnknownCmsg),
@@ -546,6 +557,16 @@ impl ControlMessageOwned {
546557
let dl = ptr::read_unaligned(p as *const libc::in_addr);
547558
ControlMessageOwned::Ipv4RecvDstAddr(dl)
548559
},
560+
#[cfg(any(target_os = "android", target_os = "linux"))]
561+
(libc::IPPROTO_IP, libc::IP_ORIGDSTADDR) => {
562+
let dl = ptr::read_unaligned(p as *const libc::sockaddr_in);
563+
ControlMessageOwned::OrigDstAddrV4(dl)
564+
},
565+
#[cfg(any(target_os = "android", target_os = "linux"))]
566+
(libc::IPPROTO_IPV6, libc::IPV6_ORIGDSTADDR) => {
567+
let dl = ptr::read_unaligned(p as *const libc::sockaddr_in6);
568+
ControlMessageOwned::OrigDstAddrV6(dl)
569+
},
549570
(_, _) => {
550571
let sl = slice::from_raw_parts(p, len);
551572
let ucmsg = UnknownCmsg(*header, Vec::<u8>::from(&sl[..]));
@@ -587,6 +608,17 @@ pub enum ControlMessage<'a> {
587608
#[cfg(any(target_os = "android", target_os = "linux"))]
588609
ScmCredentials(&'a UnixCredentials),
589610

611+
#[cfg(any(
612+
target_os = "linux",
613+
target_os = "android",
614+
))]
615+
OrigDstAddrV4(&'a libc::sockaddr_in),
616+
#[cfg(any(
617+
target_os = "linux",
618+
target_os = "android",
619+
))]
620+
OrigDstAddrV6(&'a libc::sockaddr_in6),
621+
590622
/// Set IV for `AF_ALG` crypto API.
591623
///
592624
/// For further information, please refer to the
@@ -655,6 +687,20 @@ impl<'a> ControlMessage<'a> {
655687
ControlMessage::ScmCredentials(creds) => {
656688
&creds.0 as *const libc::ucred as *const u8
657689
}
690+
#[cfg(any(
691+
target_os = "linux",
692+
target_os = "android",
693+
))]
694+
ControlMessage::OrigDstAddrV4(origaddr) => {
695+
origaddr as *const libc::sockaddr_in as *const u8
696+
},
697+
#[cfg(any(
698+
target_os = "linux",
699+
target_os = "android",
700+
))]
701+
ControlMessage::OrigDstAddrV6(origaddr) => {
702+
origaddr as *const libc::sockaddr_in6 as *const u8
703+
},
658704
#[cfg(any(target_os = "android", target_os = "linux"))]
659705
ControlMessage::AlgSetIv(iv) => {
660706
unsafe {
@@ -696,6 +742,20 @@ impl<'a> ControlMessage<'a> {
696742
ControlMessage::ScmCredentials(creds) => {
697743
mem::size_of_val(creds)
698744
}
745+
#[cfg(any(
746+
target_os = "linux",
747+
target_os = "android",
748+
))]
749+
ControlMessage::OrigDstAddrV4(origaddr) => {
750+
mem::size_of_val(origaddr)
751+
},
752+
#[cfg(any(
753+
target_os = "linux",
754+
target_os = "android",
755+
))]
756+
ControlMessage::OrigDstAddrV6(origaddr) => {
757+
mem::size_of_val(origaddr)
758+
},
699759
#[cfg(any(target_os = "android", target_os = "linux"))]
700760
ControlMessage::AlgSetIv(iv) => {
701761
mem::size_of::<libc::af_alg_iv>() + iv.len()
@@ -720,6 +780,10 @@ impl<'a> ControlMessage<'a> {
720780
#[cfg(any(target_os = "android", target_os = "linux"))]
721781
ControlMessage::AlgSetIv(_) | ControlMessage::AlgSetOp(_) |
722782
ControlMessage::AlgSetAeadAssoclen(_) => libc::SOL_ALG ,
783+
#[cfg(any(target_os = "android", target_os = "linux"))]
784+
ControlMessage::OrigDstAddrV4(_) => libc::IPPROTO_IP,
785+
#[cfg(any(target_os = "android", target_os = "linux"))]
786+
ControlMessage::OrigDstAddrV6(_) => libc::IPPROTO_IPV6,
723787
}
724788
}
725789

@@ -734,6 +798,14 @@ impl<'a> ControlMessage<'a> {
734798
libc::ALG_SET_IV
735799
},
736800
#[cfg(any(target_os = "android", target_os = "linux"))]
801+
ControlMessage::OrigDstAddrV4(_) => {
802+
libc::IP_ORIGDSTADDR
803+
},
804+
#[cfg(any(target_os = "android", target_os = "linux"))]
805+
ControlMessage::OrigDstAddrV6(_) => {
806+
libc::IPV6_ORIGDSTADDR
807+
},
808+
#[cfg(any(target_os = "android", target_os = "linux"))]
737809
ControlMessage::AlgSetOp(_) => {
738810
libc::ALG_SET_OP
739811
},

src/sys/socket/sockopt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ sockopt_impl!(GetOnly, OriginalDst, libc::SOL_IP, libc::SO_ORIGINAL_DST, libc::s
261261
sockopt_impl!(Both, ReceiveTimestamp, libc::SOL_SOCKET, libc::SO_TIMESTAMP, bool);
262262
#[cfg(any(target_os = "android", target_os = "linux"))]
263263
sockopt_impl!(Both, IpTransparent, libc::SOL_IP, libc::IP_TRANSPARENT, bool);
264+
#[cfg(any(target_os = "android", target_os = "linux"))]
265+
sockopt_impl!(Both, RecvOrigDstAddrV4, libc::SOL_IP, libc::IP_RECVORIGDSTADDR, bool);
266+
#[cfg(any(target_os = "android", target_os = "linux"))]
267+
sockopt_impl!(Both, RecvOrigDstAddrV6, libc::SOL_IPV6, libc::IPV6_RECVORIGDSTADDR, bool);
264268
#[cfg(target_os = "openbsd")]
265269
sockopt_impl!(Both, BindAny, libc::SOL_SOCKET, libc::SO_BINDANY, bool);
266270
#[cfg(target_os = "freebsd")]

0 commit comments

Comments
 (0)