70,021
社区成员




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);
}