c++ - 在传出 ICMP 数据包上设置 TTL?

标签 c++ boost boost-asio

我一直在尝试使用 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。”

最佳答案

Linux 平台?基于documentation你似乎做得对。

关于c++ - 在传出 ICMP 数据包上设置 TTL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3382691/

相关文章:

c++ - 试图编译 boost 依赖库 - "is private"错误

c++ - 这个 boost::asio 和 boost::coroutine 使用模式有什么问题?

c++ - Boost UDP async_receive_from 不等待超时或接收

c++ - Qt - QPushButtons 代替 QTreeView 项目

c++ - 试图理解 boost 队列示例

c++ - Boost.Bind - 理解占位符

c++ - Boost、asio、https 和主机/证书验证

c++ - 这个 while 循环是做什么的?

c++ - 在 C++ 中,表达式和文字有什么区别?

c++ - QT中如何加入url