rust-lang / socket2

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

Is there any way to get the IP header content on the socket created with Type::DGRAM? #521

Closed feynhang closed 4 months ago

feynhang commented 4 months ago

The socket created with Type::RAW requires elevated privileges, so I finally chose to use Type::DGRAM to create a socket to implement the ICMP protocol without elevated privileges, but this seems to cause me to be unable to get the IP header (TTL) when receiving the message? However, on Windows, win32 api can be used to implement ICMP without elevated privileges, and the ICMP_ECHO_REPLY structure containing IP Options is provided to convert the received buffer to it. (I can get TTL by the ICMP_ECHO_REPLY.Options) On Unix-like platforms, is there any way to get the IP header when a message received on the socket created with Type::DGRAM (mainly to get TTL)?

feynhang commented 4 months ago

Finally I decided to give up on getting the IP header (TTL) using a socket created with Type::DGRAM. DGRAM sockets don't seem to provide this functionality. Maybe it would be a good idea to use the OS's native ping program to do this.

Thomasdezeeuw commented 4 months ago

I'm pretty sure if you're using a raw socket the OS does nothing for you. It can even know if it's an IP protocol or not. I don't think the OS provides any API to get the ip, or really any other info, from the socket. Since it's a raw socket you have to parse the packets yourself.