2012-11-22 88 views
0

我試圖發送一個DNS響應消息給我的瀏覽器,現在我創建了一些結構併爲drupal.org的網站填充了一個。格式不正確的DNS響應

當我發送迴應wiresharks sais時,它是格式不正確, 有人可以看看?

dnsresponse response; 
    unsigned char buf[sizeof response]; 


    response.id = (unsigned short) htons(GetCurrentProcessId()); 
    response.response = 1; 
    response.opCode = 0; 
    response.authoritative = 0; 
    response.truncated = 0; 
    response.recursion = 1; 
    response.recursionAvField = 1; 
    response.z = 0; 

    response.replyCode = 0; 

    response.questions = 1; 
    response.answer = 1; 
    response.authorityRRS = 0; 
    response.additionalRRS = 0; 

    response.qName = (unsigned char *)malloc(sizeof("www.drupal.org")); 
    response.qType = 1; 
    response.qClass = 1; 

    response.aName = (unsigned char *)malloc(sizeof("www.drupal.org")); 
    response.aType = 1; 
    response.aClass = 1; 
    response.ttl = 0; 
    response.dataLength = 9; 
    response.addr = 2362640912; 

    memcpy(buf, &response, sizeof response); 

我的結構如下:

typedef struct 
{ 
unsigned short id; // ID nummer 
unsigned short response :1; // 1 is reply 0 is query 
unsigned short opCode :4; 
unsigned short authoritative :1; // DNS server is authoritative server 
unsigned short truncated :1; 
unsigned short recursion :1; // Recursie of niet 
unsigned short recursionAvField :1; // Recursie in reply 
unsigned short z :3; 
//unsigned short aa; 
//unsigned short nAD; 
unsigned short replyCode :4; 

unsigned short questions; 
unsigned short answer; 
unsigned short authorityRRS; 
unsigned short additionalRRS; 

unsigned char * qName; 
unsigned short qType; 
unsigned short qClass; 

unsigned char * aName; 
unsigned short aType; 
unsigned short aClass; 
int ttl :32; 
unsigned short dataLength; 
unsigned int addr :32; 
}dnsresponse; 

親切的問候,

+0

您需要閱讀DNS的RFC文檔。編碼DNS消息並不是那麼簡單。 (特別是,你有指針指針,把指針值發送出去是沒有意義的,指針只在你自己的程序中有用) – nos

回答

0

我不知道在協議層的DNS,但你的問題是,

malloc(sizeof("string")) 

是否不是複製一個字符串,它只保留字節的空間需要這樣做 - 提示:strdup

編輯:德哦,我只讀了「得到dns規範」部分的評論。對不起,重複。

0

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

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

這是一個相當說明性指導,以DNS數據包應該是什麼樣子: http://www.tcpipguide.com/free/t_DNSMessageProcessingandGeneralMessageFormat.htm