Skip to content

Commit 35200c5

Browse files
committed
Add back TTL stuff
1 parent f22c0e0 commit 35200c5

File tree

3 files changed

+185
-4
lines changed

3 files changed

+185
-4
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,6 @@ harness = false
107107
[[test]]
108108
name = "test-prctl"
109109
path = "test/sys/test_prctl.rs"
110+
111+
[patch.crates-io]
112+
libc = { git = "https://github.com/larseggert/libc.git", branch = "feat-ttl" }

src/sys/socket/mod.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,27 @@ pub enum ControlMessageOwned {
854854
#[cfg(feature = "net")]
855855
Ipv6TClass(u8),
856856

857+
#[cfg(any(
858+
target_os = "freebsd",
859+
apple_targets,
860+
target_os = "netbsd",
861+
target_os = "openbsd",
862+
target_os = "android",
863+
target_os = "linux",
864+
))]
865+
#[cfg(feature = "net")]
866+
IpTtl(u8),
867+
#[cfg(any(
868+
target_os = "freebsd",
869+
apple_targets,
870+
target_os = "netbsd",
871+
target_os = "openbsd",
872+
target_os = "android",
873+
target_os = "linux",
874+
))]
875+
#[cfg(feature = "net")]
876+
Ipv6HopLimit(u8),
877+
857878
/// Catch-all variant for unimplemented cmsg types.
858879
#[doc(hidden)]
859880
Unknown(UnknownCmsg),
@@ -1076,6 +1097,39 @@ impl ControlMessageOwned {
10761097
let tclass = unsafe { ptr::read_unaligned(p as *const u8) };
10771098
ControlMessageOwned::Ipv6TClass(tclass.into())
10781099
},
1100+
#[cfg(any(
1101+
apple_targets,
1102+
target_os = "freebsd",
1103+
target_os = "netbsd",
1104+
target_os = "openbsd",
1105+
))]
1106+
#[cfg(feature = "net")]
1107+
(libc::IPPROTO_IP, libc::IP_RECVTTL) => {
1108+
let ttl = unsafe { ptr::read_unaligned(p as *const u8) };
1109+
ControlMessageOwned::IpTtl(ttl.into())
1110+
},
1111+
#[cfg(any(
1112+
target_os = "android",
1113+
target_os = "linux",
1114+
))]
1115+
#[cfg(feature = "net")]
1116+
(libc::IPPROTO_IP, libc::IP_TTL) => {
1117+
let ttl = unsafe { ptr::read_unaligned(p as *const u8) };
1118+
ControlMessageOwned::IpTtl(ttl.into())
1119+
},
1120+
#[cfg(any(
1121+
target_os = "freebsd",
1122+
apple_targets,
1123+
target_os = "netbsd",
1124+
target_os = "openbsd",
1125+
target_os = "android",
1126+
target_os = "linux",
1127+
))]
1128+
#[cfg(feature = "net")]
1129+
(libc::IPPROTO_IPV6, libc::IPV6_HOPLIMIT) => {
1130+
let hop_limit = unsafe { ptr::read_unaligned(p as *const u8) };
1131+
ControlMessageOwned::Ipv6HopLimit(hop_limit.into())
1132+
},
10791133
(_, _) => {
10801134
let sl = unsafe { std::slice::from_raw_parts(p, len) };
10811135
let ucmsg = UnknownCmsg(*header, Vec::<u8>::from(sl));
@@ -1282,6 +1336,17 @@ pub enum ControlMessage<'a> {
12821336
))]
12831337
#[cfg(feature = "net")]
12841338
Ipv6TClass(&'a libc::c_int),
1339+
1340+
#[cfg(any(
1341+
target_os = "freebsd",
1342+
apple_targets,
1343+
target_os = "netbsd",
1344+
target_os = "openbsd",
1345+
target_os = "android",
1346+
target_os = "linux",
1347+
))]
1348+
#[cfg(feature = "net")]
1349+
IpTtl(&'a libc::c_int),
12851350
}
12861351

12871352
// An opaque structure used to prevent cmsghdr from being a public type
@@ -1415,6 +1480,18 @@ impl<'a> ControlMessage<'a> {
14151480
ControlMessage::Ipv6TClass(tclass) => {
14161481
tclass as *const _ as *const u8
14171482
},
1483+
#[cfg(any(
1484+
target_os = "freebsd",
1485+
apple_targets,
1486+
target_os = "netbsd",
1487+
target_os = "openbsd",
1488+
target_os = "android",
1489+
target_os = "linux",
1490+
))]
1491+
#[cfg(feature = "net")]
1492+
ControlMessage::IpTtl(ttl) => {
1493+
ttl as *const _ as *const u8
1494+
},
14181495
};
14191496
unsafe {
14201497
ptr::copy_nonoverlapping(
@@ -1508,6 +1585,18 @@ impl<'a> ControlMessage<'a> {
15081585
ControlMessage::Ipv6TClass(tclass) => {
15091586
mem::size_of_val(tclass)
15101587
},
1588+
#[cfg(any(
1589+
target_os = "freebsd",
1590+
apple_targets,
1591+
target_os = "netbsd",
1592+
target_os = "openbsd",
1593+
target_os = "android",
1594+
target_os = "linux",
1595+
))]
1596+
#[cfg(feature = "net")]
1597+
ControlMessage::IpTtl(ttl) => {
1598+
mem::size_of_val(ttl)
1599+
},
15111600
}
15121601
}
15131602

@@ -1567,6 +1656,16 @@ impl<'a> ControlMessage<'a> {
15671656
))]
15681657
#[cfg(feature = "net")]
15691658
ControlMessage::Ipv6TClass(_) => libc::IPPROTO_IPV6,
1659+
#[cfg(any(
1660+
target_os = "freebsd",
1661+
apple_targets,
1662+
target_os = "netbsd",
1663+
target_os = "openbsd",
1664+
target_os = "android",
1665+
target_os = "linux",
1666+
))]
1667+
#[cfg(feature = "net")]
1668+
ControlMessage::IpTtl(_) => libc::IPPROTO_IP,
15701669
}
15711670
}
15721671

@@ -1641,6 +1740,16 @@ impl<'a> ControlMessage<'a> {
16411740
))]
16421741
#[cfg(feature = "net")]
16431742
ControlMessage::Ipv6TClass(_) => libc::IPV6_TCLASS,
1743+
#[cfg(any(
1744+
target_os = "freebsd",
1745+
apple_targets,
1746+
target_os = "netbsd",
1747+
target_os = "openbsd",
1748+
target_os = "android",
1749+
target_os = "linux",
1750+
))]
1751+
#[cfg(feature = "net")]
1752+
ControlMessage::IpTtl(_) => libc::IP_TTL,
16441753
}
16451754
}
16461755

src/sys/socket/sockopt.rs

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,78 @@ sockopt_impl!(
10561056
libc::IP_TTL,
10571057
libc::c_int
10581058
);
1059+
#[cfg(any(
1060+
apple_targets,
1061+
target_os = "freebsd",
1062+
target_os = "netbsd",
1063+
target_os = "openbsd",
1064+
target_os = "android",
1065+
target_os = "linux",
1066+
))]
1067+
#[cfg(feature = "net")]
1068+
sockopt_impl!(
1069+
/// Retrieve the current time-to-live field for every
1070+
/// IPv4 packet received on this socket.
1071+
IpRecvTtl,
1072+
Both,
1073+
libc::IPPROTO_IP,
1074+
libc::IP_RECVTTL,
1075+
bool
1076+
);
1077+
#[cfg(any(
1078+
apple_targets,
1079+
target_os = "freebsd",
1080+
target_os = "netbsd",
1081+
target_os = "openbsd",
1082+
target_os = "android",
1083+
target_os = "linux",
1084+
))]
1085+
#[cfg(feature = "net")]
1086+
sockopt_impl!(
1087+
/// Retrieve the current time-to-live field for every
1088+
/// IPv4 packet received on this socket.
1089+
IpTtl,
1090+
Both,
1091+
libc::IPPROTO_IP,
1092+
libc::IP_TTL,
1093+
bool
1094+
);
1095+
#[cfg(any(
1096+
apple_targets,
1097+
target_os = "freebsd",
1098+
target_os = "netbsd",
1099+
target_os = "openbsd",
1100+
target_os = "android",
1101+
target_os = "linux",
1102+
))]
1103+
#[cfg(feature = "net")]
1104+
sockopt_impl!(
1105+
/// Retrieve the current hop limit field for every
1106+
/// IPv6 packet received on this socket.
1107+
Ipv6RecvHopLimit,
1108+
Both,
1109+
libc::IPPROTO_IPV6,
1110+
libc::IPV6_RECVHOPLIMIT,
1111+
bool
1112+
);
1113+
#[cfg(any(
1114+
apple_targets,
1115+
target_os = "freebsd",
1116+
target_os = "netbsd",
1117+
target_os = "openbsd",
1118+
target_os = "android",
1119+
target_os = "linux",
1120+
))]
1121+
#[cfg(feature = "net")]
1122+
sockopt_impl!(
1123+
/// Retrieve the current hop limit field for every
1124+
/// IPv6 packet received on this socket.
1125+
Ipv6HopLimit,
1126+
Both,
1127+
libc::IPPROTO_IPV6,
1128+
libc::IPV6_HOPLIMIT,
1129+
bool
1130+
);
10591131
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
10601132
sockopt_impl!(
10611133
/// Set the unicast hop limit for the socket.
@@ -1092,10 +1164,7 @@ sockopt_impl!(
10921164
libc::IP_DONTFRAG,
10931165
bool
10941166
);
1095-
#[cfg(any(
1096-
target_os = "android",
1097-
target_os = "linux",
1098-
))]
1167+
#[cfg(any(target_os = "android", target_os = "linux",))]
10991168
#[cfg(feature = "net")]
11001169
sockopt_impl!(
11011170
/// Set "don't fragment packet" flag on the IP packet.

0 commit comments

Comments
 (0)