Skip to content

Add methods to TCP and UDP sockets to modify hop limit (refresh of #94678) #138744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@
#![feature(hashmap_internals)]
#![feature(hint_must_use)]
#![feature(ip)]
#![feature(ipv6_hop_limit)]
#![feature(lazy_get)]
#![feature(maybe_uninit_slice)]
#![feature(maybe_uninit_write_slice)]
Expand Down
78 changes: 78 additions & 0 deletions library/std/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,46 @@ impl TcpStream {
self.0.ttl()
}

/// Sets the value for the `IPV6_UNICAST_HOPS` option on this socket.
///
/// This value sets the unicast hop limit field that is used in every packet
/// sent from this socket.
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::TcpStream;
///
/// let stream = TcpStream::connect("[::1]:12345")
/// .expect("Couldn't connect to the server...");
/// stream.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "139166")]
pub fn set_hop_limit_v6(&self, limit: u8) -> io::Result<()> {
self.0.set_hop_limit_v6(limit)
}

/// Gets the value of the `IPV6_UNICAST_HOPS` option on this socket.
///
/// For more information about this option, see [`TcpStream::set_hop_limit_v6`].
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::TcpStream;
///
/// let stream = TcpStream::connect("[::1]:12345")
/// .expect("Couldn't connect to the server...");
/// stream.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
/// assert_eq!(stream.hop_limit_v6().unwrap(), 88);
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "139166")]
pub fn hop_limit_v6(&self) -> io::Result<u8> {
self.0.hop_limit_v6()
}

/// Gets the value of the `SO_ERROR` option on this socket.
///
/// This will retrieve the stored error in the underlying socket, clearing
Expand Down Expand Up @@ -942,6 +982,44 @@ impl TcpListener {
self.0.ttl()
}

/// Sets the value for the `IPV6_UNICAST_HOPS` option on this socket.
///
/// This value sets the unicast hop limit field that is used in every packet
/// sent from this socket.
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::TcpListener;
///
/// let listener = TcpListener::bind("[::1]:12345").unwrap();
/// listener.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "139166")]
pub fn set_hop_limit_v6(&self, limit: u8) -> io::Result<()> {
self.0.set_hop_limit_v6(limit)
}

/// Gets the value of the `IPV6_UNICAST_HOPS` option on this socket.
///
/// For more information about this option, see [`TcpListener::set_hop_limit_v6`].
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::TcpListener;
///
/// let listener = TcpListener::bind("[::1]:12345").unwrap();
/// listener.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
/// assert_eq!(listener.hop_limit_v6().unwrap(), 88);
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "139166")]
pub fn hop_limit_v6(&self) -> io::Result<u8> {
self.0.hop_limit_v6()
}

#[stable(feature = "net2_mutators", since = "1.9.0")]
#[deprecated(since = "1.16.0", note = "this option can only be set before the socket is bound")]
#[allow(missing_docs)]
Expand Down
17 changes: 17 additions & 0 deletions library/std/src/net/tcp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,23 @@ fn ttl() {
assert_eq!(ttl, t!(stream.ttl()));
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)]
fn hop_limit() {
let hlim = 100;

let addr = next_test_ip6();
let listener = t!(TcpListener::bind(&addr));

t!(listener.set_hop_limit_v6(hlim));
assert_eq!(hlim, t!(listener.hop_limit_v6()));

let stream = t!(TcpStream::connect(&addr));

t!(stream.set_hop_limit_v6(hlim));
assert_eq!(hlim, t!(stream.hop_limit_v6()));
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)]
fn set_nonblocking() {
Expand Down
76 changes: 76 additions & 0 deletions library/std/src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,82 @@ impl UdpSocket {
self.0.ttl()
}

/// Sets the value for the `IPV6_UNICAST_HOPS` option on this socket.
///
/// This value sets the unicast hop limit field that is used in every packet
/// sent from this socket.
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::UdpSocket;
///
/// let socket = UdpSocket::bind("[::1]:12345").expect("couldn't bind to address");
/// socket.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "139166")]
pub fn set_hop_limit_v6(&self, limit: u8) -> io::Result<()> {
self.0.set_hop_limit_v6(limit)
}

/// Gets the value of the `IPV6_UNICAST_HOPS` option on this socket.
///
/// For more information about this option, see [`UdpSocket::set_hop_limit_v6`].
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::UdpSocket;
///
/// let socket = UdpSocket::bind("[::1]:12345").expect("couldn't bind to address");
/// socket.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
/// assert_eq!(socket.hop_limit_v6().unwrap(), 88);
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "139166")]
pub fn hop_limit_v6(&self) -> io::Result<u8> {
self.0.hop_limit_v6()
}

/// Sets the value for the `IPV6_MULTICAST_HOPS` option on this socket.
///
/// This value sets the hop limit field for outgoing multicast packets
/// sent from this socket.
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::UdpSocket;
///
/// let socket = UdpSocket::bind("[::1]:12345").expect("couldn't bind to address");
/// socket.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "139166")]
pub fn set_multicast_hop_limit_v6(&self, limit: u8) -> io::Result<()> {
self.0.set_multicast_hop_limit_v6(limit)
}

/// Gets the value of the `IPV6_MULTICAST_HOPS` option on this socket.
///
/// For more information about this option, see [`UdpSocket::set_multicast_hop_limit_v6`].
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::UdpSocket;
///
/// let socket = UdpSocket::bind("[::1]:12345").expect("couldn't bind to address");
/// socket.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
/// assert_eq!(socket.multicast_hop_limit_v6().unwrap(), 88);
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "139166")]
pub fn multicast_hop_limit_v6(&self) -> io::Result<u8> {
self.0.multicast_hop_limit_v6()
}

/// Executes an operation of the `IP_ADD_MEMBERSHIP` type.
///
/// This function specifies a new multicast group for this socket to join.
Expand Down
12 changes: 12 additions & 0 deletions library/std/src/net/udp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,18 @@ fn ttl() {
assert_eq!(ttl, t!(stream.ttl()));
}

#[test]
fn hop_limit() {
let hlim = 100;

let addr = next_test_ip6();

let stream = t!(UdpSocket::bind(&addr));

t!(stream.set_hop_limit_v6(hlim));
assert_eq!(hlim, t!(stream.hop_limit_v6()));
}

#[test]
fn set_nonblocking() {
each_ip(&mut |addr, _| {
Expand Down
33 changes: 33 additions & 0 deletions library/std/src/sys/net/connection/sgx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::time::Duration;
use crate::{error, fmt};

const DEFAULT_FAKE_TTL: u32 = 64;
const DEFAULT_FAKE_HLIM: u8 = 64;

#[derive(Debug, Clone)]
pub struct Socket {
Expand Down Expand Up @@ -199,6 +200,14 @@ impl TcpStream {
sgx_ineffective(DEFAULT_FAKE_TTL)
}

pub fn set_hop_limit_v6(&self, _: u8) -> io::Result<()> {
sgx_ineffective(())
}

pub fn hop_limit_v6(&self) -> io::Result<u8> {
sgx_ineffective(DEFAULT_FAKE_HLIM)
}

pub fn take_error(&self) -> io::Result<Option<io::Error>> {
Ok(None)
}
Expand Down Expand Up @@ -277,6 +286,14 @@ impl TcpListener {
sgx_ineffective(DEFAULT_FAKE_TTL)
}

pub fn set_hop_limit_v6(&self, _: u8) -> io::Result<()> {
sgx_ineffective(())
}

pub fn hop_limit_v6(&self) -> io::Result<u8> {
sgx_ineffective(DEFAULT_FAKE_HLIM)
}

pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
sgx_ineffective(())
}
Expand Down Expand Up @@ -416,6 +433,22 @@ impl UdpSocket {
self.0
}

pub fn set_hop_limit_v6(&self, _: u8) -> io::Result<()> {
self.0
}

pub fn hop_limit_v6(&self) -> io::Result<u8> {
self.0
}

pub fn set_multicast_hop_limit_v6(&self, _: u8) -> io::Result<()> {
self.0
}

pub fn multicast_hop_limit_v6(&self) -> io::Result<u8> {
self.0
}

pub fn take_error(&self) -> io::Result<Option<io::Error>> {
self.0
}
Expand Down
36 changes: 36 additions & 0 deletions library/std/src/sys/net/connection/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ impl TcpStream {
Ok(raw as u32)
}

pub fn set_hop_limit_v6(&self, limit: u8) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_UNICAST_HOPS, limit as c_int)
}

pub fn hop_limit_v6(&self) -> io::Result<u8> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_UNICAST_HOPS)?;
Ok(raw as u8)
}

pub fn take_error(&self) -> io::Result<Option<io::Error>> {
self.inner.take_error()
}
Expand Down Expand Up @@ -581,6 +590,15 @@ impl TcpListener {
Ok(raw as u32)
}

pub fn set_hop_limit_v6(&self, limit: u8) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_UNICAST_HOPS, limit as c_int)
}

pub fn hop_limit_v6(&self) -> io::Result<u8> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_UNICAST_HOPS)?;
Ok(raw as u8)
}

pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY, only_v6 as c_int)
}
Expand Down Expand Up @@ -786,6 +804,24 @@ impl UdpSocket {
Ok(raw as u32)
}

pub fn set_hop_limit_v6(&self, limit: u8) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_UNICAST_HOPS, limit as c_int)
}

pub fn hop_limit_v6(&self) -> io::Result<u8> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_UNICAST_HOPS)?;
Ok(raw as u8)
}

pub fn set_multicast_hop_limit_v6(&self, limit: u8) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_HOPS, limit as c_int)
}

pub fn multicast_hop_limit_v6(&self) -> io::Result<u8> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_HOPS)?;
Ok(raw as u8)
}

pub fn take_error(&self) -> io::Result<Option<io::Error>> {
self.inner.take_error()
}
Expand Down
8 changes: 4 additions & 4 deletions library/std/src/sys/net/connection/socket/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ pub(super) mod netc {
pub use crate::sys::c::{
ADDRESS_FAMILY as sa_family_t, ADDRINFOA as addrinfo, IP_ADD_MEMBERSHIP,
IP_DROP_MEMBERSHIP, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_TTL, IPPROTO_IP, IPPROTO_IPV6,
IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_MULTICAST_LOOP, IPV6_V6ONLY, SO_BROADCAST,
SO_RCVTIMEO, SO_SNDTIMEO, SOCK_DGRAM, SOCK_STREAM, SOCKADDR as sockaddr,
SOCKADDR_STORAGE as sockaddr_storage, SOL_SOCKET, bind, connect, freeaddrinfo, getpeername,
getsockname, getsockopt, listen, setsockopt,
IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_MULTICAST_HOPS, IPV6_MULTICAST_LOOP,
IPV6_UNICAST_HOPS, IPV6_V6ONLY, SO_BROADCAST, SO_RCVTIMEO, SO_SNDTIMEO, SOCK_DGRAM,
SOCK_STREAM, SOCKADDR as sockaddr, SOCKADDR_STORAGE as sockaddr_storage, SOL_SOCKET, bind,
connect, freeaddrinfo, getpeername, getsockname, getsockopt, listen, setsockopt,
};

#[allow(non_camel_case_types)]
Expand Down
Loading
Loading