2012-11-21 111 views
0

我想創建一個DNS響應發送到我的瀏覽器。我已經創造了一些結構像在RFC:創建DNS響應消息

//DNS header 
struct DNS_HEADER 
{ 
    unsigned short id; 
    unsigned char rd :1; 
    unsigned char tc :1; 
    unsigned char aa :1; 
    unsigned char opcode :4; 
    unsigned char qr :1; 

    unsigned char rcode :4; 
    unsigned char cd :1; 
    unsigned char ad :1; 
    unsigned char z :1; 
    unsigned char ra :1; 

    unsigned short q_count; 
    unsigned short ans_count; 
    unsigned short auth_count; 
    unsigned short add_count; 
}; 

#pragma pack(push, 1) 
struct R_DATA 
{ 
    unsigned short type; 
    unsigned short _class; 
    unsigned int ttl; 
    unsigned short data_len; 
}; 
#pragma pack(pop) 

struct RES_RECORD 
{ 
    unsigned char *name; 
    struct R_DATA *resource; 
    unsigned char *rdata; 
}; 

現在我試圖填補這個結構,所以我可以發送一個有效的DNS響應。我試圖發送例如www.google.com與ipaddres 112.12.12.12(只是爲了好玩)。

這是我有:

dns = (DNS_HEADER*)malloc(sizeof(DNS_HEADER)); 
dns->id = (unsigned short) htons(GetCurrentProcessId()); // ID 
dns->qr = 1; // We give a response, Volgens RFC: (= query (0), or a response (1).) 
dns->opcode = 0; // default 
dns->aa = 0; //Not Authoritative,RFC: (= Authoritative Answer - this bit is valid in responses, and specifies that the responding name server is an authority for the domain name in question section.) 
dns->tc = 0; // Not truncated 
dns->rd = 1; // Enable recursion 
dns->ra = 0; // Nameserver supports recursion? 
dns->z = 0; // RFC: (= Reserved for future use. Must be zero in all queries and responses.) 
dns->rcode = 0; // No error condition 
dns->q_count = 0; // No questions! 
dns->ad = 0; // How man resource records? 
dns->cd = 0; // !checking 
dns->ans_count = 1; // We give 1 answer 
dns->auth_count = 0; // How many authority entries? 
dns->add_count = 0; // How many resource entries? 

但你可以看到我對自己在什麼來填補一些問題 另外,R_DATA和res_record我無法通過RFC找出什麼填寫我已經做出的隨機迴應...

有人可以幫助我嗎?

回答

0

幾個指針一覽:您的回覆中的id需要是您在查詢中收到的標識。 q_count應爲1並重復收到的查詢(在您的示例中,例如\x03www\x06google\x03com\x00\x00\x01\x00\x01www.google.com IN A)。 RFC1035第3.4.1節(在你的例子中它將是\x70\x0c\x0c\x0c)解釋了rdata需要去的內容。

0

您的方法存在根本上的缺陷。由於DNS數據包中的字符串長度可變,因此不能使用結構表示DNS數據包,因爲根據前面字符串的長度,字符串後面的字段將處於不同的偏移量。

你的結構有char指針代替每個字符串,每個指針通常是指向內存中某個其他位置的32位值。因此,當您嘗試發送內存中表示的結構時,您將發送更多或更少的隨機32位值來代替字符串。

這裏是一個相當說明性的指導什麼DNS數據包應該看起來像:http://www.tcpipguide.com/free/t_DNSMessageProcessingandGeneralMessageFormat.htm