Boost raw sockets

1.2k views Asked by At

I know it is a very theoretical question, but forgive me as this is not my specialty.

Looking for an example on how to use raw sockets with boost, I found a question in this same website where it is assured that you cannot know the destination of an UDP message with boost asio. Then having a look at the boost examples, there is an icmp example: http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/icmp/ping.cpp Where you actually can have both IP and ICMP headers by using async_receive and a icmp endpoint.

(I was curious that I couldn't find the word "raw" anywhere in the code) My question is: why does this work for ICMP and not for UDP or TCP? is it because ICMP is level 3?

How can this example work at all? Is it that ICMP socket is equivalent to a raw socket? but I don't think it should be.

1

There are 1 answers

0
Tanner Sansbury On BEST ANSWER

boost::asio::ip::icmp::socket is a raw socket. The ip::icmp type encapsulates flags and types used for ICMP. In particular, the implementation of ip::icmp::type() returns SOCK_RAW:

/// Obtain an identifier for the type of the protocol.
int type() const
{
  return BOOST_ASIO_OS_DEF(SOCK_RAW);
}

As icmp::socket's protocol type is raw, the ICMP example will receive the network layer data (IP header and ICMP). Additionally, the ip::icmp's protocol is IPPROTO_ICMP, and Boost.Asio does not set the IP_HDRINCL socket option, so the kernel will generate the appropriate IP header when sending.