Skip to content

Commit 27bbb53

Browse files
authored
Rollup merge of rust-lang#94678 - kckeiks:set_ipv6_sock_hop_limit, r=joshtriplett
add methods to TCP and UDP sockets to modify hop limit * adds methods to set value of IPV6_UNICAST_HOPS and IPV6_MULTICAST_HOPS on ipv6 sockets * I added some noop methods for different systems to keep things consistent. Maybe someone could please clarify if these noop funcs are needed? and, if so, why? Just for my own learning. This is my first addition of a new feature so let me know if I missed something in the process. I read the rustc dev guide about implementing new features but since the change here is simple, it suggested that a PR would be enough. Fixes rust-lang#47727
2 parents 0922559 + b3e9e94 commit 27bbb53

File tree

12 files changed

+559
-0
lines changed

12 files changed

+559
-0
lines changed

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
#![feature(exhaustive_patterns)]
254254
#![feature(if_let_guard)]
255255
#![feature(intra_doc_pointers)]
256+
#![feature(ipv6_hop_limit)]
256257
#![feature(lang_items)]
257258
#![feature(let_chains)]
258259
#![feature(linkage)]

library/std/src/net/tcp.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,86 @@ impl TcpStream {
541541
self.0.ttl()
542542
}
543543

544+
/// Sets the value for the `IPV6_UNICAST_HOPS` option on this socket.
545+
///
546+
/// This value sets the unicast hop limit field that is used in every packet
547+
/// sent from this socket.
548+
///
549+
/// # Examples
550+
///
551+
/// ```no_run
552+
/// #![feature(ipv6_hop_limit)]
553+
/// use std::net::TcpStream;
554+
///
555+
/// let stream = TcpStream::connect("127.0.0.1:54321")
556+
/// .expect("Couldn't connect to the server...");
557+
/// stream.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
558+
/// ```
559+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
560+
pub fn set_hop_limit_v6(&self, limit: u32) -> io::Result<()> {
561+
self.0.set_hop_limit_v6(limit)
562+
}
563+
564+
/// Gets the value of the `IPV6_UNICAST_HOPS` option on this socket.
565+
///
566+
/// For more information about this option, see [`TcpStream::set_hop_limit_v6`].
567+
///
568+
/// # Examples
569+
///
570+
/// ```no_run
571+
/// #![feature(ipv6_hop_limit)]
572+
/// use std::net::TcpStream;
573+
///
574+
/// let stream = TcpStream::connect("127.0.0.1:54321")
575+
/// .expect("Couldn't connect to the server...");
576+
/// stream.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
577+
/// assert_eq!(stream.hop_limit_v6().unwrap(), 88);
578+
/// ```
579+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
580+
pub fn hop_limit_v6(&self) -> io::Result<u32> {
581+
self.0.hop_limit_v6()
582+
}
583+
584+
/// Sets the value for the `IPV6_MULTICAST_HOPS` option on this socket.
585+
///
586+
/// This value sets the hop limit field for outgoing multicast packets
587+
/// sent from this socket.
588+
///
589+
/// # Examples
590+
///
591+
/// ```no_run
592+
/// #![feature(ipv6_hop_limit)]
593+
/// use std::net::TcpStream;
594+
///
595+
/// let stream = TcpStream::connect("127.0.0.1:54321")
596+
/// .expect("Couldn't connect to the server...");
597+
/// stream.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
598+
/// ```
599+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
600+
pub fn set_multicast_hop_limit_v6(&self, limit: u32) -> io::Result<()> {
601+
self.0.set_multicast_hop_limit_v6(limit)
602+
}
603+
604+
/// Gets the value of the `IPV6_MULTICAST_HOPS` option on this socket.
605+
///
606+
/// For more information about this option, see [`TcpStream::set_multicast_hop_limit_v6`].
607+
///
608+
/// # Examples
609+
///
610+
/// ```no_run
611+
/// #![feature(ipv6_hop_limit)]
612+
/// use std::net::TcpStream;
613+
///
614+
/// let stream = TcpStream::connect("127.0.0.1:54321")
615+
/// .expect("Couldn't connect to the server...");
616+
/// stream.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
617+
/// assert_eq!(stream.multicast_hop_limit_v6().unwrap(), 88);
618+
/// ```
619+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
620+
pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
621+
self.0.multicast_hop_limit_v6()
622+
}
623+
544624
/// Gets the value of the `SO_ERROR` option on this socket.
545625
///
546626
/// This will retrieve the stored error in the underlying socket, clearing
@@ -915,6 +995,82 @@ impl TcpListener {
915995
self.0.ttl()
916996
}
917997

998+
/// Sets the value for the `IPV6_UNICAST_HOPS` option on this socket.
999+
///
1000+
/// This value sets the unicast hop limit field that is used in every packet
1001+
/// sent from this socket.
1002+
///
1003+
/// # Examples
1004+
///
1005+
/// ```no_run
1006+
/// #![feature(ipv6_hop_limit)]
1007+
/// use std::net::TcpListener;
1008+
///
1009+
/// let listener = TcpListener::bind("127.0.0.1:54321").unwrap();
1010+
/// listener.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
1011+
/// ```
1012+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
1013+
pub fn set_hop_limit_v6(&self, limit: u32) -> io::Result<()> {
1014+
self.0.set_hop_limit_v6(limit)
1015+
}
1016+
1017+
/// Gets the value of the `IPV6_UNICAST_HOPS` option on this socket.
1018+
///
1019+
/// For more information about this option, see [`TcpListener::set_hop_limit_v6`].
1020+
///
1021+
/// # Examples
1022+
///
1023+
/// ```no_run
1024+
/// #![feature(ipv6_hop_limit)]
1025+
/// use std::net::TcpListener;
1026+
///
1027+
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
1028+
/// listener.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
1029+
/// assert_eq!(listener.hop_limit_v6().unwrap(), 88);
1030+
/// ```
1031+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
1032+
pub fn hop_limit_v6(&self) -> io::Result<u32> {
1033+
self.0.hop_limit_v6()
1034+
}
1035+
1036+
/// Sets the value for the `IPV6_MULTICAST_HOPS` option on this socket.
1037+
///
1038+
/// This value sets the hop limit field for outgoing multicast packets
1039+
/// sent from this socket.
1040+
///
1041+
/// # Examples
1042+
///
1043+
/// ```no_run
1044+
/// #![feature(ipv6_hop_limit)]
1045+
/// use std::net::TcpListener;
1046+
///
1047+
/// let listener = TcpListener::bind("127.0.0.1:54321").unwrap();
1048+
/// listener.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
1049+
/// ```
1050+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
1051+
pub fn set_multicast_hop_limit_v6(&self, limit: u32) -> io::Result<()> {
1052+
self.0.set_multicast_hop_limit_v6(limit)
1053+
}
1054+
1055+
/// Gets the value of the `IPV6_MULTICAST_HOPS` option on this socket.
1056+
///
1057+
/// For more information about this option, see [`TcpListener::set_multicast_hop_limit_v6`].
1058+
///
1059+
/// # Examples
1060+
///
1061+
/// ```no_run
1062+
/// #![feature(ipv6_hop_limit)]
1063+
/// use std::net::TcpListener;
1064+
///
1065+
/// let listener = TcpListener::bind("127.0.0.1:54321").unwrap();
1066+
/// listener.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
1067+
/// assert_eq!(listener.multicast_hop_limit_v6().unwrap(), 88);
1068+
/// ```
1069+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
1070+
pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
1071+
self.0.multicast_hop_limit_v6()
1072+
}
1073+
9181074
#[stable(feature = "net2_mutators", since = "1.9.0")]
9191075
#[deprecated(since = "1.16.0", note = "this option can only be set before the socket is bound")]
9201076
#[allow(missing_docs)]

library/std/src/net/tcp/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,23 @@ fn ttl() {
813813
assert_eq!(ttl, t!(stream.ttl()));
814814
}
815815

816+
#[test]
817+
#[cfg_attr(target_env = "sgx", ignore)]
818+
fn hop_limit() {
819+
let hlim: u32 = 100;
820+
821+
let addr = next_test_ip6();
822+
let listener = t!(TcpListener::bind(&addr));
823+
824+
t!(listener.set_hop_limit_v6(hlim));
825+
assert_eq!(hlim, t!(listener.hop_limit_v6()));
826+
827+
let stream = t!(TcpStream::connect(&addr));
828+
829+
t!(stream.set_hop_limit_v6(hlim));
830+
assert_eq!(hlim, t!(stream.hop_limit_v6()));
831+
}
832+
816833
#[test]
817834
#[cfg_attr(target_env = "sgx", ignore)]
818835
fn set_nonblocking() {

library/std/src/net/udp.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,82 @@ impl UdpSocket {
554554
self.0.ttl()
555555
}
556556

557+
/// Sets the value for the `IPV6_UNICAST_HOPS` option on this socket.
558+
///
559+
/// This value sets the unicast hop limit field that is used in every packet
560+
/// sent from this socket.
561+
///
562+
/// # Examples
563+
///
564+
/// ```no_run
565+
/// #![feature(ipv6_hop_limit)]
566+
/// use std::net::UdpSocket;
567+
///
568+
/// let socket = UdpSocket::bind("127.0.0.1:54321").expect("couldn't bind to address");
569+
/// socket.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
570+
/// ```
571+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
572+
pub fn set_hop_limit_v6(&self, limit: u32) -> io::Result<()> {
573+
self.0.set_hop_limit_v6(limit)
574+
}
575+
576+
/// Gets the value of the `IPV6_UNICAST_HOPS` option on this socket.
577+
///
578+
/// For more information about this option, see [`UdpSocket::set_hop_limit_v6`].
579+
///
580+
/// # Examples
581+
///
582+
/// ```no_run
583+
/// #![feature(ipv6_hop_limit)]
584+
/// use std::net::UdpSocket;
585+
///
586+
/// let socket = UdpSocket::bind("127.0.0.1:54321").expect("couldn't bind to address");
587+
/// socket.set_hop_limit_v6(88).expect("set_hop_limit_v6 call failed");
588+
/// assert_eq!(socket.hop_limit_v6().unwrap(), 88);
589+
/// ```
590+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
591+
pub fn hop_limit_v6(&self) -> io::Result<u32> {
592+
self.0.hop_limit_v6()
593+
}
594+
595+
/// Sets the value for the `IPV6_MULTICAST_HOPS` option on this socket.
596+
///
597+
/// This value sets the hop limit field for outgoing multicast packets
598+
/// sent from this socket.
599+
///
600+
/// # Examples
601+
///
602+
/// ```no_run
603+
/// #![feature(ipv6_hop_limit)]
604+
/// use std::net::UdpSocket;
605+
///
606+
/// let socket = UdpSocket::bind("127.0.0.1:54321").expect("couldn't bind to address");
607+
/// socket.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
608+
/// ```
609+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
610+
pub fn set_multicast_hop_limit_v6(&self, limit: u32) -> io::Result<()> {
611+
self.0.set_multicast_hop_limit_v6(limit)
612+
}
613+
614+
/// Gets the value of the `IPV6_MULTICAST_HOPS` option on this socket.
615+
///
616+
/// For more information about this option, see [`UdpSocket::set_multicast_hop_limit_v6`].
617+
///
618+
/// # Examples
619+
///
620+
/// ```no_run
621+
/// #![feature(ipv6_hop_limit)]
622+
/// use std::net::UdpSocket;
623+
///
624+
/// let socket = UdpSocket::bind("127.0.0.1:54321").expect("couldn't bind to address");
625+
/// socket.set_multicast_hop_limit_v6(88).expect("set_multicast_hop_limit_v6 call failed");
626+
/// assert_eq!(socket.multicast_hop_limit_v6().unwrap(), 88);
627+
/// ```
628+
#[unstable(feature = "ipv6_hop_limit", issue = "47727")]
629+
pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
630+
self.0.multicast_hop_limit_v6()
631+
}
632+
557633
/// Executes an operation of the `IP_ADD_MEMBERSHIP` type.
558634
///
559635
/// This function specifies a new multicast group for this socket to join.

library/std/src/net/udp/tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ fn ttl() {
342342
assert_eq!(ttl, t!(stream.ttl()));
343343
}
344344

345+
#[test]
346+
fn hop_limit() {
347+
let hlim = 100;
348+
349+
let addr = next_test_ip6();
350+
351+
let stream = t!(UdpSocket::bind(&addr));
352+
353+
t!(stream.set_hop_limit_v6(hlim));
354+
assert_eq!(hlim, t!(stream.hop_limit_v6()));
355+
}
356+
345357
#[test]
346358
fn set_nonblocking() {
347359
each_ip(&mut |addr, _| {

library/std/src/sys/hermit/net.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,22 @@ impl TcpStream {
208208
.map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "unable to get TTL"))
209209
}
210210

211+
pub fn set_hop_limit_v6(&self, _: u32) -> io::Result<()> {
212+
unsupported()
213+
}
214+
215+
pub fn hop_limit_v6(&self) -> io::Result<u32> {
216+
unsupported()
217+
}
218+
219+
pub fn set_multicast_hop_limit_v6(&self, _: u32) -> io::Result<()> {
220+
unsupported()
221+
}
222+
223+
pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
224+
unsupported()
225+
}
226+
211227
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
212228
unsupported()
213229
}
@@ -265,6 +281,22 @@ impl TcpListener {
265281
unsupported()
266282
}
267283

284+
pub fn set_hop_limit_v6(&self, _: u32) -> io::Result<()> {
285+
unsupported()
286+
}
287+
288+
pub fn hop_limit_v6(&self) -> io::Result<u32> {
289+
unsupported()
290+
}
291+
292+
pub fn set_multicast_hop_limit_v6(&self, _: u32) -> io::Result<()> {
293+
unsupported()
294+
}
295+
296+
pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
297+
unsupported()
298+
}
299+
268300
pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
269301
unsupported()
270302
}
@@ -391,6 +423,22 @@ impl UdpSocket {
391423
unsupported()
392424
}
393425

426+
pub fn set_hop_limit_v6(&self, _: u32) -> io::Result<()> {
427+
unsupported()
428+
}
429+
430+
pub fn hop_limit_v6(&self) -> io::Result<u32> {
431+
unsupported()
432+
}
433+
434+
pub fn set_multicast_hop_limit_v6(&self, _: u32) -> io::Result<()> {
435+
unsupported()
436+
}
437+
438+
pub fn multicast_hop_limit_v6(&self) -> io::Result<u32> {
439+
unsupported()
440+
}
441+
394442
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
395443
unsupported()
396444
}

0 commit comments

Comments
 (0)