我一直在尝试使用。在ICMP数据包上设置TTL boost :: asio :: ip :: unicast :: hops选项(使用Boost 1.43)然后用get_option读取它。
无论我在set_option中使用什么,get_option都会得到1。并在检查时 使用wireshark发送的数据包,TTL是128.我在这里遗漏了什么? 我应该使用其他选项来设置TTL吗?通过Asio可以吗?
此致 彼得
更新2010-08-01 17:37 UTC:这是我正在使用的代码:
#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';
}
}
从此我得到:
“异常:TTL未正确设置。应为2但设置为1。”