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."