views:

121

answers:

1

Hi,

I have been trying to set the TTL on ICMP packets using the boost::asio::ip::unicast::hops option (using Boost 1.43) and then reading it out with get_option.

get_option gets 1 regardless what I use in set_option. And when inspecting the packets sent using wireshark, the TTL is 128. Am I missing something here? Should I use another option to set the TTL? Is it at all possible through Asio?

Regards, Peter

Update 2010-08-01 17:37 UTC: Here is the code I am 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 not set properly. Should be 2 but was set to 1."

A: 

Linux platform? Based on the documentation you appear to be doing it correctly.

Sam Miller
Windows Vista and MinGW with g++ version 4.5.0.
Peter Jansson
your code compiles and runs fine on my system, Mac OS 10.6.3, with boost 1.43. I had to execute it as root, maybe you need to do the equivalent on Windows?
Sam Miller
Yes, I have to run it as Administrator. But I do get the exception about the failed setting of TTL when running the program. So, this is a platform issue then. Should perhaps report a bug to the proper bug tracker.
Peter Jansson
are you sure it's an asio bug? If you get the native socket handle, then try to set the TTL (setsockopt?), do you get the same error?
Sam Miller
Peter Jansson
possibly, you'll most likely get better help from MiniGW than the Boost ticket tracker. Good luck!
Sam Miller
Thank you, I'll try that.
Peter Jansson