Skip to content

Commit 199878e

Browse files
duskmoon314taiki-e
andauthored
net: add tos and set_tos methods to TCP and UDP sockets (#4877)
Co-authored-by: Campbell He <kp.campbell.he@duskmoon314.com> Co-authored-by: Taiki Endo <te316e89@gmail.com>
1 parent 5ab6aaf commit 199878e

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

tokio/src/net/tcp/socket.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,61 @@ impl TcpSocket {
398398
self.inner.linger()
399399
}
400400

401+
/// Gets the value of the `IP_TOS` option for this socket.
402+
///
403+
/// For more information about this option, see [`set_tos`].
404+
///
405+
/// **NOTE:** On Windows, `IP_TOS` is only supported on [Windows 8+ or
406+
/// Windows Server 2012+.](https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options)
407+
///
408+
/// [`set_tos`]: Self::set_tos
409+
// https://docs.rs/socket2/0.4.2/src/socket2/socket.rs.html#1178
410+
#[cfg(not(any(
411+
target_os = "fuchsia",
412+
target_os = "redox",
413+
target_os = "solaris",
414+
target_os = "illumos",
415+
)))]
416+
#[cfg_attr(
417+
docsrs,
418+
doc(cfg(not(any(
419+
target_os = "fuchsia",
420+
target_os = "redox",
421+
target_os = "solaris",
422+
target_os = "illumos",
423+
))))
424+
)]
425+
pub fn tos(&self) -> io::Result<u32> {
426+
self.inner.tos()
427+
}
428+
429+
/// Sets the value for the `IP_TOS` option on this socket.
430+
///
431+
/// This value sets the time-to-live field that is used in every packet sent
432+
/// from this socket.
433+
///
434+
/// **NOTE:** On Windows, `IP_TOS` is only supported on [Windows 8+ or
435+
/// Windows Server 2012+.](https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options)
436+
// https://docs.rs/socket2/0.4.2/src/socket2/socket.rs.html#1178
437+
#[cfg(not(any(
438+
target_os = "fuchsia",
439+
target_os = "redox",
440+
target_os = "solaris",
441+
target_os = "illumos",
442+
)))]
443+
#[cfg_attr(
444+
docsrs,
445+
doc(cfg(not(any(
446+
target_os = "fuchsia",
447+
target_os = "redox",
448+
target_os = "solaris",
449+
target_os = "illumos",
450+
))))
451+
)]
452+
pub fn set_tos(&self, tos: u32) -> io::Result<()> {
453+
self.inner.set_tos(tos)
454+
}
455+
401456
/// Gets the local address of this socket.
402457
///
403458
/// Will fail on windows if called before `bind`.

tokio/src/net/udp.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ impl UdpSocket {
259259
}
260260
}
261261

262+
fn as_socket(&self) -> socket2::SockRef<'_> {
263+
socket2::SockRef::from(self)
264+
}
265+
262266
/// Returns the local address that this socket is bound to.
263267
///
264268
/// # Example
@@ -1510,6 +1514,61 @@ impl UdpSocket {
15101514
self.io.set_ttl(ttl)
15111515
}
15121516

1517+
/// Gets the value of the `IP_TOS` option for this socket.
1518+
///
1519+
/// For more information about this option, see [`set_tos`].
1520+
///
1521+
/// **NOTE:** On Windows, `IP_TOS` is only supported on [Windows 8+ or
1522+
/// Windows Server 2012+.](https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options)
1523+
///
1524+
/// [`set_tos`]: Self::set_tos
1525+
// https://docs.rs/socket2/0.4.2/src/socket2/socket.rs.html#1178
1526+
#[cfg(not(any(
1527+
target_os = "fuchsia",
1528+
target_os = "redox",
1529+
target_os = "solaris",
1530+
target_os = "illumos",
1531+
)))]
1532+
#[cfg_attr(
1533+
docsrs,
1534+
doc(cfg(not(any(
1535+
target_os = "fuchsia",
1536+
target_os = "redox",
1537+
target_os = "solaris",
1538+
target_os = "illumos",
1539+
))))
1540+
)]
1541+
pub fn tos(&self) -> io::Result<u32> {
1542+
self.as_socket().tos()
1543+
}
1544+
1545+
/// Sets the value for the `IP_TOS` option on this socket.
1546+
///
1547+
/// This value sets the time-to-live field that is used in every packet sent
1548+
/// from this socket.
1549+
///
1550+
/// **NOTE:** On Windows, `IP_TOS` is only supported on [Windows 8+ or
1551+
/// Windows Server 2012+.](https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options)
1552+
// https://docs.rs/socket2/0.4.2/src/socket2/socket.rs.html#1178
1553+
#[cfg(not(any(
1554+
target_os = "fuchsia",
1555+
target_os = "redox",
1556+
target_os = "solaris",
1557+
target_os = "illumos",
1558+
)))]
1559+
#[cfg_attr(
1560+
docsrs,
1561+
doc(cfg(not(any(
1562+
target_os = "fuchsia",
1563+
target_os = "redox",
1564+
target_os = "solaris",
1565+
target_os = "illumos",
1566+
))))
1567+
)]
1568+
pub fn set_tos(&self, tos: u32) -> io::Result<()> {
1569+
self.as_socket().set_tos(tos)
1570+
}
1571+
15131572
/// Executes an operation of the `IP_ADD_MEMBERSHIP` type.
15141573
///
15151574
/// This function specifies a new multicast group for this socket to join.

0 commit comments

Comments
 (0)