Configure TTL on outgoing ICMP packets?

I am trying to set TTL on ICMP packets using boost :: asio :: ip :: unicast :: hops (using Boost 1.43) and then reading using get_option.

get_option gets 1 no matter what i use in set_option. And when checking packets sent using wirehark, TTL is 128. Am I missing something? Should I use another option to set TTL? Is this possible through Asio?

Regards, Peter

Update 2010-08-01 17:37 UTC: Here is the code I'm using:

#include <sstream>
#include <stdexcept>
#include <boost/asio.hpp>

class MyClass: public boost::noncopyable
{
   public:
      MyClass(const char* host):
         io(),
         resolver(io),
         query( boost::asio::ip::icmp::v4(), host, "" ),
         socket(io, boost::asio::ip::icmp::v4())
   {
      destination = *resolver.resolve(query);
   }
      ~MyClass()
      {
         socket.close();
      }
      void run()
      {
         const int ttl = 2;

         // set TTL ?
         const boost::asio::ip::unicast::hops option( ttl );
         socket.set_option(option);

         boost::asio::ip::unicast::hops op;
         socket.get_option(op);
         if( op.value() != ttl )
         {
            std::ostringstream o;
            o << "TTL not set properly. Should be " << ttl << " but was set"
               " to " << op.value() << '.';
            throw std::runtime_error( o.str() );
         }
      }

   private:
      boost::asio::io_service io;
      boost::asio::ip::icmp::resolver resolver;
      boost::asio::ip::icmp::resolver::query query;
      boost::asio::ip::icmp::socket socket ;
      boost::asio::ip::icmp::endpoint destination;
};



#include <iostream>
int main( int argc, char** argv)
{
   try
   {
      if( argc != 2 )
      {
         throw std::invalid_argument("Missing argument. First argument = host");
      }
      MyClass T( argv[1] );
      T.run();
   }
   catch( const std::exception& e )
   {
      std::cerr << "Exception: " << e.what() << '\n';
   }
}

From this I get:

"Exception: TTL is not set correctly. It must be 2, but it was set to 1.

+3
source share
1 answer

Linux-? , , .

+2

Source: https://habr.com/ru/post/1757558/


All Articles