rust-lang / socket2

Advanced configuration options for sockets.
https://docs.rs/socket2
Apache License 2.0
683 stars 227 forks source link

Currently socket2 has no way to set the DF bit on packets. The attached patch adds it. #487

Open jcdutton opened 10 months ago

jcdutton commented 10 months ago

socket2.diff.txt Hi, Currently socket2 has no way to set the DF bit on packets. The DF bit is needed for Path MTU discovery and also for creating arbitrary ICMP packets with the DF bit set. Please see the attached patch that will add it. I have only been able to test it on Linux, not Windows. This might also be needed to enable Path MTU discovery on TCP sessions when using socket2.

For example, some sample code to send an ICMP packet with the DF bit set: let domain = Domain::IPV4; let ty = Type::DGRAM; let protocol = Some(Protocol::ICMPV4); let mut sock1 = create_socket(domain, ty, protocol); let mut sock1r = sock1.unwrap(); let res1 = Socket::set_mtu_discover(&sock1r, true); let mut sock2 = sock1r; sock2.set_read_timeout(Some(timeout))?; let destination = SocketAddr::new(addr.clone(), 0); // destination let sockaddr1: SockAddr = destination.into(); let result = make_data(&data, size3); let payload = result.unwrap(); let res2 = sock2.send_to(&payload, &sockaddr1);

Thomasdezeeuw commented 10 months ago

Pull request would be welcome.

jcdutton commented 10 months ago

@Thomasdezeeuw

Pull request would be welcome.

I would like someone else to check it does not break socket2 on other platforms first (e.g. windows), and maybe re-craft it to work on more platforms before we commit it. For example, I don't know if I have put "use libc;" in a place that will work on windows platforms. I have tested it on Linux x86_64 but nothing else.

Thomasdezeeuw commented 10 months ago

@jcdutton we have our CI setup for that. It tests on 3 platforms and builds on a large number of others. You can use it if you send a pull request.