tokio icon indicating copy to clipboard operation
tokio copied to clipboard

Adding support for sending/receiving ICMP packets (and more generally raw IP packets)?

Open gendx opened this issue 3 years ago • 11 comments

Is your feature request related to a problem? Please describe. tokio::net currently supports networking over TCP, UDP and Unix domain sockets. This doesn't include other protocols such as ICMP (ping). It would be interesting to have the ability to send/receive ICMP packets via Tokio's asynchronous interface.

Describe the solution you'd like Adding the ability to handle ICMP packets in tokio::net. I assume the API would be somewhat similar to tokio::net::UdpSocket. Maybe the API should support handling all IP packets at a low level - not only ICMP? I'm not sure whether it would be feasible to implement on all OSes, maybe Linux or Unix should be supported first?

Describe alternatives you've considered It seems that std::net doesn't support ICMP either. I've found the following projects to handle ICMP in Rust. However, none of them seem to support an async interface.

  • https://crates.io/crates/pnet, which looks like the de facto crate to handle low-level networking in Rust.
  • https://github.com/bparli/fastping-rs (which relies on pnet).
  • https://github.com/svartalf/rust-icmp (archived).

Additional context N/A

gendx avatar Jul 15 '20 11:07 gendx

Does the various OSes even provide an async API for this?

Darksonn avatar Jul 20 '20 08:07 Darksonn

You might want to check out something like capsule for packet-level use cases. It's built on top of Tokio (although it looks like it has yet to be updated to the release version of 0.2) and uses DPDK.

As @Darksonn said, I'm not sure if there is a cross-platform way for Tokio to abstract over raw IP packets?

hawkw avatar Jul 20 '20 16:07 hawkw

The next step would be to make a concrete proposal on how to do this.

  • How would this be implemented?
  • What is the portability story?
  • Can this be implemented as a separate library?

I don't believe any of the core maintainers have a need for this functionality, so it would need to be user contributed.

carllerche avatar Jul 25 '20 05:07 carllerche

FWIW:

This is a library implement ICMP ping using std::net and socket2: (Tested on Windows/macOS) https://github.com/aisk/ping

And this is a library implement ICMP ping using tokio 0.1 only support UNIX https://github.com/knsd/tokio-ping

Based on these two libraries, let me try to answer these questions:

How would this be implemented?

If we want to add an IcmpSocket into tokio, we can either depend on socket2 or implement raw socket inside tokio, which I don't think is a good idea either way, because it feels like a big change to tokio.

What is the portability story?

It should be fine for basic pinging functionality, but once you want to implement more complex features, there will be some privilege issues: https://apple.stackexchange.com/questions/312857/how-does-macos-allow-standard-users-to-ping https://stackoverflow.com/questions/4282783/raw-sockets-privilege-for-normal-user

Can this be implemented as a separate library?

I think the answer is Yes.

fluxxu avatar Jul 30 '20 13:07 fluxxu

What is the portability story?

It should be fine for basic pinging functionality, but once you want to implement more complex features, there will be some privilege issues: https://apple.stackexchange.com/questions/312857/how-does-macos-allow-standard-users-to-ping https://stackoverflow.com/questions/4282783/raw-sockets-privilege-for-normal-user

Using raw sockets and/or advanced features would assume that the binary is run as root, but I don't think there should be a restriction that a Tokio-based program cannot be run as root.

gendx avatar Aug 03 '20 09:08 gendx

Hi. Are there any active plans in regards to supporting this? Is there anyway I or others can help progressing this in whatever way is suitable?

Was planning to write it myself, but can't imagine it's going to help anyone if we have yet another packet. For example currently https://github.com/jcgruenhage/tokio-icmp-echo is the best way to go forward I guess to have something.

What would it take to start adding such support directly to tokio, or is this deemed out of scope?

GlenDC avatar May 30 '22 08:05 GlenDC

There are no active plans at this time. In general, I would start by figuring out the minimal support needed by Tokio to make this implementable in external crates. E.g., what does it take to hook up the raw IP packets to the Tokio event loop? Can AsyncFd handle it? What is the story for raw IP packets on windows?

Darksonn avatar May 30 '22 09:05 Darksonn

Any progress?

pplmx avatar Dec 26 '23 09:12 pplmx

No. See my previous comment.

Darksonn avatar Dec 26 '23 14:12 Darksonn

OK, Thanks :)

pplmx avatar Dec 27 '23 01:12 pplmx

What is the portability story?

It should be fine for basic pinging functionality, but once you want to implement more complex features, there will be some privilege issues: https://apple.stackexchange.com/questions/312857/how-does-macos-allow-standard-users-to-ping https://stackoverflow.com/questions/4282783/raw-sockets-privilege-for-normal-user

Using raw sockets and/or advanced features would assume that the binary is run as root, but I don't think there should be a restriction that a Tokio-based program cannot be run as root.

This is not an issue, the program only uses it at runtime, if it fails it fails, the user will restart it with privilege.

irvingoujAtDevolution avatar Jan 03 '24 19:01 irvingoujAtDevolution