Skip to content

Commit 7fe1bff

Browse files
jjnicolaThomasdezeeuw
authored andcommitted
Add: header_included_v6() and set_header_included_v6()
Also, deprecate header_included() and set_header_included() in favor of header_included_v4() and set_header_included_v4().
1 parent 6a13053 commit 7fe1bff

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

src/socket.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,16 @@ const fn into_linger(duration: Option<Duration>) -> sys::linger {
11381138
/// * Linux: <https://man7.org/linux/man-pages/man7/ip.7.html>
11391139
/// * Windows: <https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options>
11401140
impl Socket {
1141+
/// This method is deprecated, use [`crate::Socket::header_included_v4`].
1142+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
1143+
#[cfg_attr(
1144+
docsrs,
1145+
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
1146+
)]
1147+
#[deprecated = "Use `Socket::header_included_v4` instead"]
1148+
pub fn header_included(&self) -> io::Result<bool> {
1149+
self.header_included_v4()
1150+
}
11411151
/// Get the value of the `IP_HDRINCL` option on this socket.
11421152
///
11431153
/// For more information about this option, see [`set_header_included`].
@@ -1148,13 +1158,28 @@ impl Socket {
11481158
docsrs,
11491159
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
11501160
)]
1151-
pub fn header_included(&self) -> io::Result<bool> {
1161+
pub fn header_included_v4(&self) -> io::Result<bool> {
11521162
unsafe {
11531163
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, sys::IP_HDRINCL)
11541164
.map(|included| included != 0)
11551165
}
11561166
}
11571167

1168+
/// This method is deprecated, use [`crate::Socket::set_header_included_v4`].
1169+
#[cfg_attr(
1170+
any(target_os = "fuchsia", target_os = "illumos", target_os = "solaris"),
1171+
allow(rustdoc::broken_intra_doc_links)
1172+
)]
1173+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
1174+
#[cfg_attr(
1175+
docsrs,
1176+
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
1177+
)]
1178+
#[deprecated = "Use `Socket::set_header_included_v4` instead"]
1179+
pub fn set_header_included(&self, included: bool) -> io::Result<()> {
1180+
self.set_header_included_v4(included)
1181+
}
1182+
11581183
/// Set the value of the `IP_HDRINCL` option on this socket.
11591184
///
11601185
/// If enabled, the user supplies an IP header in front of the user data.
@@ -1175,7 +1200,7 @@ impl Socket {
11751200
docsrs,
11761201
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
11771202
)]
1178-
pub fn set_header_included(&self, included: bool) -> io::Result<()> {
1203+
pub fn set_header_included_v4(&self, included: bool) -> io::Result<()> {
11791204
unsafe {
11801205
setsockopt(
11811206
self.as_raw(),
@@ -1651,6 +1676,51 @@ impl Socket {
16511676
/// * Linux: <https://man7.org/linux/man-pages/man7/ipv6.7.html>
16521677
/// * Windows: <https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options>
16531678
impl Socket {
1679+
/// Get the value of the `IP_HDRINCL` option on this socket.
1680+
///
1681+
/// For more information about this option, see [`set_header_included`].
1682+
///
1683+
/// [`set_header_included`]: Socket::set_header_included
1684+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
1685+
#[cfg_attr(
1686+
docsrs,
1687+
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
1688+
)]
1689+
pub fn header_included_v6(&self) -> io::Result<bool> {
1690+
unsafe {
1691+
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, sys::IP_HDRINCL)
1692+
.map(|included| included != 0)
1693+
}
1694+
}
1695+
1696+
/// Set the value of the `IP_HDRINCL` option on this socket.
1697+
///
1698+
/// If enabled, the user supplies an IP header in front of the user data.
1699+
/// Valid only for [`SOCK_RAW`] sockets; see [raw(7)] for more information.
1700+
/// When this flag is enabled, the values set by `IP_OPTIONS` are ignored.
1701+
///
1702+
/// [`SOCK_RAW`]: Type::RAW
1703+
/// [raw(7)]: https://man7.org/linux/man-pages/man7/raw.7.html
1704+
#[cfg_attr(
1705+
any(target_os = "fuchsia", target_os = "illumos", target_os = "solaris"),
1706+
allow(rustdoc::broken_intra_doc_links)
1707+
)]
1708+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
1709+
#[cfg_attr(
1710+
docsrs,
1711+
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
1712+
)]
1713+
pub fn set_header_included_v6(&self, included: bool) -> io::Result<()> {
1714+
unsafe {
1715+
setsockopt(
1716+
self.as_raw(),
1717+
sys::IPPROTO_IPV6,
1718+
sys::IP_HDRINCL,
1719+
included as c_int,
1720+
)
1721+
}
1722+
}
1723+
16541724
/// Join a multicast group using `IPV6_ADD_MEMBERSHIP` option on this socket.
16551725
///
16561726
/// Some OSs use `IPV6_JOIN_GROUP` for this option.

tests/socket.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,14 +1539,38 @@ fn header_included() {
15391539
};
15401540

15411541
let initial = socket
1542-
.header_included()
1542+
.header_included_v4()
15431543
.expect("failed to get initial value");
15441544
assert_eq!(initial, false, "initial value and argument are the same");
15451545

15461546
socket
1547-
.set_header_included(true)
1547+
.set_header_included_v4(true)
15481548
.expect("failed to set option");
1549-
let got = socket.header_included().expect("failed to get value");
1549+
let got = socket.header_included_v4().expect("failed to get value");
1550+
assert_eq!(got, true, "set and get values differ");
1551+
}
1552+
1553+
#[test]
1554+
#[cfg(all(feature = "all", not(target_os = "redox")))]
1555+
fn header_included_ipv6() {
1556+
let socket = match Socket::new(Domain::IPV6, Type::RAW, None) {
1557+
Ok(socket) => socket,
1558+
// Need certain permissions to create a raw sockets.
1559+
Err(ref err) if err.kind() == io::ErrorKind::PermissionDenied => return,
1560+
#[cfg(unix)]
1561+
Err(ref err) if err.raw_os_error() == Some(libc::EPROTONOSUPPORT) => return,
1562+
Err(err) => panic!("unexpected error creating socket: {}", err),
1563+
};
1564+
1565+
let initial = socket
1566+
.header_included_v6()
1567+
.expect("failed to get initial value");
1568+
assert_eq!(initial, false, "initial value and argument are the same");
1569+
1570+
socket
1571+
.set_header_included_v6(true)
1572+
.expect("failed to set option");
1573+
let got = socket.header_included_v6().expect("failed to get value");
15501574
assert_eq!(got, true, "set and get values differ");
15511575
}
15521576

0 commit comments

Comments
 (0)