Closed feynhang closed 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.
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.
The socket created with
Type::RAW
requires elevated privileges, so I finally chose to useType::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 theICMP_ECHO_REPLY
structure containing IPOptions
is provided to convert the received buffer to it. (I can get TTL by theICMP_ECHO_REPLY.Options
) On Unix-like platforms, is there any way to get the IP header when a message received on the socket created withType::DGRAM
(mainly to get TTL)?