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

UdpSocket::set_ttl does not set IPv6 hoplimit field #47727

Open
eleksaggr opened this issue Jan 24, 2018 · 4 comments
Open

UdpSocket::set_ttl does not set IPv6 hoplimit field #47727

eleksaggr opened this issue Jan 24, 2018 · 4 comments
Assignees
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@eleksaggr
Copy link

eleksaggr commented Jan 24, 2018

It is not possible to set a hoplimit in an IPv6 packet sent over UDP. UdpSocket::set_ttl does not modify the hoplimit in the IPv6 packet and there seems to be no other way to do so.

I tried this code:

use std::net::{Ipv6Addr, SocketAddrV6, UdpSocket};

fn main() {
    let laddr = SocketAddrV6::new(Ipv6Addr::new(0,0,0,0,0,0,0,1), 50000, 0, 0);
    let destination = SocketAddrV6::new(Ipv6Addr::new(0,0,0,0,0,0,0,1), 50000, 0, 0);

    let socket = UdpSocket::bind(laddr).unwrap();
    socket.connect(destination).unwrap();
    socket.set_ttl(10).unwrap();
    println!("{:?}", socket.ttl().unwrap());

    let payload = [1,2,3,4,5,6,7,8];
    socket.send(&payload).unwrap();
}

I expected to see this happen:
Since UdpSocket has no other function that would set the hoplimit in an IPv6 packet, I believe set_ttl should be doing this. I expected to capture a UDP packet with an IPv6 hoplimit field of 10.

Instead, this happened:
The output of the program is as expected:

10

I used tcpdump -v -i lo udp port 50000 to capture the packet:

tcpdump: listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
23:43:02.146680 IP6 (flowlabel 0x487a1, hlim 64, next-header UDP (17) payload length: 16) localhost.localdomain.50000 > localhost.localdomain.50000: [bad udp cksum 0x0023 -> 0x6917!] UDP, length 8

The hoplimit field of the packet is still set to the default value of 64.

Meta

rustc --version --verbose:

rustc 1.23.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.23.0
LLVM version: 4.0
@sfackler
Copy link
Member

set_ttl maps directly to setsockopt with the IP_TTL argument. If you want to set the IPv6 hop limit we should add a method that does that.

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Feb 6, 2018
@kckeiks
Copy link
Contributor

kckeiks commented Mar 3, 2022

@rustbot claim

@mfelsche
Copy link

On macOS Big Sur (11.6.5) using the exact snippet from the issue description is actually failing for me with the following output:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 22, kind: InvalidInput, message: "Invalid argument" }', src/main.rs:9:24

I was able to verify the same behaviour for TcpSocket.

I think at least the documentation on set_ttl should document possible error conditions and should state that this only intended to work on ipv4 sockets.

Should I report this as a separate issue?

@kckeiks
Copy link
Contributor

kckeiks commented Mar 31, 2022

I would say, yes. This issue is for adding functionality.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 3, 2022
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants