关于DNS查询包的格式

leihelin5201314 leihelin5201314 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
typedef struct//报文head
{
unsigned short id;
unsigned short flags;
unsigned short ques;
unsigned short answer;
unsigned short author;
unsigned short addition;
}DNSHEAD,*pDNSHEAD;

typedef struct//报文的查询部分
{
//unsigned char *domain;
unsigned short type;
unsigned short class;
}SEARCH,*pSEARCH;

void genDNSPacket(char *addr,char *dnssendbuff)
{ //将head加到dnssendbuff里面,结果dnssendbuff里面没有这部分
pDNSHEAD pdnshead = (pDNSHEAD)malloc(sizeof(DNSHEAD));
pSEARCH psearch = (pSEARCH)malloc(sizeof(SEARCH));

pdnshead->id = htons(0x0000);
pdnshead->flags = htons(0x000);
pdnshead->ques = htons(0x0000);
pdnshead->answer = htons(0x0000);
pdnshead->author = htons(0x0000);
pdnshead->addition = htons(0x0000);

memcpy(dnssendbuff,pdnshead,sizeof(DNSHEAD));
printf("pdnshead=%s\n",(char *)pdnshead);
printf("sizeof(DNSHEAD)=%d\n",sizeof(DNSHEAD));
printf("dnssendbuff=%s\n",dnssendbuff);

//将domain www.hao123.com加到报文
char *domain = (char *)malloc(15 *sizeof(char)); //3www6hao1233com0
char num;
char *pbuf;

int length = 0;
num = 0x03;
pbuf = "www";
memcpy(domain + length,&num,sizeof(num));
length += sizeof(num);
memcpy(domain+ length,pbuf,strlen(pbuf));
length += strlen(pbuf);

num = 0x06;
pbuf = "dnspod";
memcpy(domain + length,&num,sizeof(num));
length += sizeof(num);
memcpy(domain + length,pbuf,strlen(pbuf));
length += strlen(pbuf);

num = 0x03;
pbuf = "com";
memcpy(domain + length,&num,sizeof(num));
length += sizeof(num);
memcpy(domain + length,pbuf,strlen(pbuf));
length += strlen(pbuf);
printf("domain=%s\n",domain);

num = 0x00;
memcpy(domain + length,&num,sizeof(num));
length += sizeof(num);
memcpy(dnssendbuff + sizeof(DNSHEAD),domain,16);
printf("strlen(domain)=%d\n",strlen(domain));
memcpy( dnssendbuff, domain, 16);

/将search加到dnssendbuff里面,结果dnssendbuff里面没有这部分
psearch->type = htons(0x0001);
psearch->class = htons(0x0001);

memcpy(dnssendbuff + sizeof(DNSHEAD) + 16,psearch,sizeof(SEARCH));
printf("strlen(dnssendbuff)=%d\n",strlen(dnssendbuff));
printf("dnssendbuff=%s\n",dnssendbuff);
}

我ping hao123.com,然后wireshark抓包得到的结构看了下,发现我在程序里边的head部分和最后面的查询类和查询类型部分没加到报文里边,报文打出来之后只出现了个 3www6hao1233com ,没发现head和search部分,估计是我的这两部分的程序不正确,希望牛人帮我看下,谢谢!
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
leihelin5201314
C语言 70,040 成员 243,237 内容