Skip to content
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

add methods to TCP and UDP sockets to modify hop limit #94678

Closed
wants to merge 7 commits into from
Closed
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 @@ -277,6 +277,7 @@
#![feature(hashmap_internals)]
#![feature(int_error_internals)]
#![feature(intra_doc_pointers)]
#![feature(ipv6_hop_limit)]
#![feature(lang_items)]
#![feature(linkage)]
#![feature(log_syntax)]
Expand Down
156 changes: 156 additions & 0 deletions library/std/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,86 @@ 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("127.0.0.1:54321")
/// .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 = "47727")]
pub fn set_hop_limit_v6(&self, limit: u32) -> io::Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hop Limit is an 8bit header field. So using u8 instead of u32 would reflect that better and avoid API misusage like #73158.

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("127.0.0.1:54321")
/// .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 = "47727")]
pub fn hop_limit_v6(&self) -> io::Result<u32> {
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::TcpStream;
///
/// let stream = TcpStream::connect("127.0.0.1:54321")
/// .expect("Couldn't connect to the server...");
/// stream.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
pub fn set_multicast_hop_limit_v6(&self, limit: u32) -> 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 [`TcpStream::set_multicast_hop_limit_v6`].
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::TcpStream;
///
/// let stream = TcpStream::connect("127.0.0.1:54321")
/// .expect("Couldn't connect to the server...");
/// stream.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
/// assert_eq!(stream.multicast_hop_limit_v6().unwrap(), 88);
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
self.0.multicast_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 @@ -914,6 +994,82 @@ 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("127.0.0.1:54321").unwrap();
/// listener.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
pub fn set_hop_limit_v6(&self, limit: u32) -> 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("127.0.0.1:80").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 = "47727")]
pub fn hop_limit_v6(&self) -> io::Result<u32> {
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::TcpListener;
///
/// let listener = TcpListener::bind("127.0.0.1:54321").unwrap();
/// listener.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
pub fn set_multicast_hop_limit_v6(&self, limit: u32) -> 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 [`TcpListener::set_multicast_hop_limit_v6`].
///
/// # Examples
///
/// ```no_run
/// #![feature(ipv6_hop_limit)]
/// use std::net::TcpListener;
///
/// let listener = TcpListener::bind("127.0.0.1:54321").unwrap();
/// listener.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
/// assert_eq!(listener.multicast_hop_limit_v6().unwrap(), 88);
/// ```
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
self.0.multicast_hop_limit_v6()
}

#[stable(feature = "net2_mutators", since = "1.9.0")]
#[rustc_deprecated(
since = "1.16.0",
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 @@ -814,6 +814,23 @@ fn ttl() {
assert_eq!(ttl, t!(stream.ttl()));
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)]
fn hop_limit() {
let hlim: u32 = 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 @@ -554,6 +554,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("127.0.0.1:54321").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 = "47727")]
pub fn set_hop_limit_v6(&self, limit: u32) -> 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("127.0.0.1:54321").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 = "47727")]
pub fn hop_limit_v6(&self) -> io::Result<u32> {
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("127.0.0.1:54321").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 = "47727")]
pub fn set_multicast_hop_limit_v6(&self, limit: u32) -> 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("127.0.0.1:54321").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 = "47727")]
pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
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 @@ -342,6 +342,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
48 changes: 48 additions & 0 deletions library/std/src/sys/hermit/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ impl TcpStream {
.map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "unable to get TTL"))
}

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

pub fn hop_limit_v6(&self) -> io::Result<u32> {
unsupported()
}

pub fn set_multicast_hop_limit_v6(&self, _: u32) -> io::Result<()> {
unsupported()
}

pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
unsupported()
}

pub fn take_error(&self) -> io::Result<Option<io::Error>> {
unsupported()
}
Expand Down Expand Up @@ -266,6 +282,22 @@ impl TcpListener {
unsupported()
}

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

pub fn hop_limit_v6(&self) -> io::Result<u32> {
unsupported()
}

pub fn set_multicast_hop_limit_v6(&self, _: u32) -> io::Result<()> {
unsupported()
}

pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
unsupported()
}

pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
unsupported()
}
Expand Down Expand Up @@ -392,6 +424,22 @@ impl UdpSocket {
unsupported()
}

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

pub fn hop_limit_v6(&self) -> io::Result<u32> {
unsupported()
}

pub fn set_multicast_hop_limit_v6(&self, _: u32) -> io::Result<()> {
unsupported()
}

pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
unsupported()
}

pub fn take_error(&self) -> io::Result<Option<io::Error>> {
unsupported()
}
Expand Down
Loading