C ++ Build&发送DNS数据包 [英] C++ Build & Send DNS Packet

查看:283
本文介绍了C ++ Build&发送DNS数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++中的DNS解析器,构建DNS头是给我问题的。从我可以收集到的东西,可以通过一个结构体来构建标题。

  typedef struct {
无符号短号;

unsigned char qr:1; // 0 query,1 response
unsigned char操作码:4; // 0标准查询
unsigned char aa:1; // authoritive answer
unsigned char tc:1; // truncated message
unsigned char rd:1; //递归需要

unsigned char ra:1; //递归可用
unsigned char z:3; //保留
unsigned char rcode:4; // response code

unsigned q_count:16; //问题条目数
unsigned ans_count:16; //答案数量
unsigned auth_count:16; //权限条目数
unsigned add_count:16; //资源条目数
} HEADER;

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

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



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



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

解决方案

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



您确定已完成hton()或htonl()吗?


I'm working on a DNS resolver in C++, constructing the DNS header is giving me problems. From what I've been able to gather, the header can be constructed through a struct.

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;

I set the header values, place the header in DNS_PACKET, cast as (void *) and send the packet using sendto().

Viewing one of the sent packets in Wireshark shows that the bits are not being set in the correct locations. For example I set q_count to 1, but Wireshark displays the packet with 256 questions.

In all the documentation I've read, this appears to be a valid route to constructing a packet. Am I going about this incorrectly? Is there a better method to construct a packet?

解决方案

I suggest you can use libnet to build the DNS packet. It will be much easier, never worry about the header's format.

and you sure you have done the hton() or htonl() ?

这篇关于C ++ Build&发送DNS数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆