0

DNS 応答メッセージをブラウザーに送信しようとしています。今、いくつかの構造体を作成し、drupal.org の Web サイト用に入力しました。

私が応答を送信しているときに、Wireshark はそれが不正であると言っています。誰か見てもらえますか?

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;

敬具、

4

2 に答える 2

0

あなたのアプローチには根本的な欠陥があります。DNS パケットの文字列は可変長であるため、DNS パケットを構造体で表現することはできません。つまり、文字列に続くフィールドは、前の文字列の長さに応じてパケット内の異なるオフセットになります。

構造体には各文字列の代わりに文字ポインターがあり、各ポインターは通常、メモリ内の他の場所を指す 32 ビット値です。したがって、メモリ内で表現されている構造体を送信しようとすると、文字列の代わりに多かれ少なかれランダムな 32 ビット値が送信されます。

以下は、DNS パケットがどのように見えるべきかについてのかなり説明的なガイドです: http://www.tcpipguide.com/free/t_DNSMessageProcessingandGeneralMessageFormat.htm

于 2013-09-11T11:51:42.680 に答える
0

プロトコルレベルでのDNSはわかりませんが、あなたの問題は

malloc(sizeof("string"))

文字列を複製するのではなく、複製に必要なバイトのスペースを予約するだけです -- ヒント: strdup

編集:ああ、私はnosのコメントの「dns仕様を取得する」部分だけを読みました。重複して申し訳ありません。

于 2012-11-22T15:59:24.390 に答える