0

我正在尝试向我的浏览器发送一条 dns 响应消息,现在我创建了一些结构并为 drupal.org 的网站填写了一个。

当我发送响应时,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;

亲切的问候,

4

2 回答 2

0

我不知道协议级别的 dns,但你的问题是

malloc(sizeof("string"))

不复制字符串,它只为需要的字节保留空间——提示:strdup

编辑:d'oh,我只阅读了 nos 评论的“获取 dns 规范”部分。对不起,重复。

于 2012-11-22T15:59:24.390 回答
0

你的方法从根本上是有缺陷的。你不能用结构体来表达DNS数据包,因为DNS数据包中的字符串是可变长度的,即字符串后面的字段在数据包中的偏移量取决于前面字符串的长度。

您的结构用字符指针代替每个字符串,每个指针通常是一个 32 位值,指向内存中的某个其他位置。因此,当您尝试发送内存中表示的结构时,您将发送或多或少的随机 32 位值来代替字符串。

这是一个关于 DNS 数据包应该是什么样子的相当说明性的指南: http ://www.tcpipguide.com/free/t_DNSMessageProcessingandGeneralMessageFormat.htm

于 2013-09-11T11:51:42.680 回答