3

I'm trying to write a simple packet crafter with asio. And I need to send raw data by different protocols.

I'm trying to change ping example from asio documentation. In this example icpm-packet is sent via socket_.send_to method. This method encapsulates icmp-packet to ip-packet.

But I need to send ip header, then icmp header and then body of packet without any encapsulation! I'm trying to do it with socket.send method. But it throws an exception.

here's the code. In my class I have:

boost::asio::basic_raw_socket<raw> socket_;

When 'raw' is class of raw data protocol. This class is like 'icmp' class from asio with protocol_ equal IPPROTO_RAW.

Then I send data:

     IPV4header ip_header;
 //fill ip_header

 ICMPHeader echo_request;
 //fill icmp-header

 std::string body("---");

 // Encode the request packet.
 boost::asio::streambuf request_buffer;
 std::ostream os(&request_buffer);
 os << ip_header << echo_request << body;

 socket_.send(request_buffer.data(), request_buffer.size());

3 Answers 3

2

Use setsockopt to set value IPPROTO_IP IP_HDRINCL to 0. It should help

2

I don't believe you can send an IP header like that using Asio raw sockets, or the Asio library in general. Also you need to specify a destination endpoint, and if you're trying to send over the socket without first connecting, you need to use the send_to() method as opposed to the send() method.

I created a github repository with an example of how to use Asio raw sockets with a custom transport layer header: https://github.com/kevincon/asio-rawsockets-example

If you're looking to send your own IP header, you might look into the packet injection capabilities of libpnet6: http://pnet6.sourceforge.net/

2

"Use setsockopt to set value IPPROTO_IP IP_HDRINCL to 0. It should help",

It should be set IPPROTO_IP IP_HDRINCL to 1 if need to send ip header without any encapsulation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.