【问题标题】:C++ Build & Send DNS PacketC++ 构建和发送 DNS 数据包
【发布时间】:2013-10-18 03:40:39
【问题描述】:

我正在使用 C++ 开发 DNS 解析器,构建 DNS 标头给我带来了问题。根据我收集到的信息,可以通过结构构造标题。

typedef struct {
  unsigned short id;

  unsigned char qr :1; // 0 query, 1 response
  unsigned char opcode :4; // 0 standard query
  unsigned char aa :1; // authoritive answer
  unsigned char tc :1; // truncated message
  unsigned char rd :1; // recursion desired

  unsigned char ra :1; // recursion available
  unsigned char z :3; // reserved
  unsigned char rcode :4;  // response code

  unsigned q_count :16; // number of question entries
  unsigned ans_count :16; // number of answer entries
  unsigned auth_count :16; // number of authority entries
  unsigned add_count :16; // number of resource entries
} HEADER;

typedef union {
  HEADER header;
  unsigned char packet_data[1024];
} DNS_PACKET;

我设置了标头值,将标头放在 DNS_PACKET 中,转换为 (void *) 并使用 sendto() 发送数据包。

在 Wireshark 中查看其中一个发送的数据包表明这些位未设置在正确的位置。例如,我将 q_count 设置为 1,但 Wireshark 显示的数据包包含 256 个问题。

在我阅读的所有文档中,这似乎是构建数据包的有效途径。我做错了吗?有没有更好的方法来构造数据包?

【问题讨论】:

    标签: c++ c sockets dns


    【解决方案1】:

    我建议你可以使用 libnet 来构建 DNS 数据包。这会容易得多,不用担心标题的格式。

    你确定你已经完成了 hton() 或 htonl() 吗?

    【讨论】:

    • 我忘了使用 hton,看来你让我回到了正确的轨道上。谢谢。
    • 我的荣幸,呵呵:)
    猜你喜欢
    • 2011-08-13
    • 2018-04-14
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 2011-02-07
    • 1970-01-01
    相关资源
    最近更新 更多