可能重复:
C - 字节数组到结构(dns 查询)
我有这些结构:
typedef struct dnsQuery {
char header[12];
struct dnsQuerySection *querySection;
} TdnsQuery;
typedef struct dnsQuerySection {
unsigned char *name;
struct dnsQueryQuestion *question;
} TdnsQuerySection;
typedef struct dnsQueryQuestion {
unsigned short qtype;
unsigned short qclass;
} TdnsQueryQuestion;
我在一个字节数组中有一个 DNSbuf
查询recvfrom
。我正在尝试从字节数组中获取结构,如下所示:
TdnsQuery* dnsQuery = (TdnsQuery*)buf;
当我尝试像这样访问 qtype 时:
printf("%u", dnsQuery->querySection->question.qtype);
我得到段错误 11。
有人可以帮我处理这些结构吗?他们怎么了?我试图添加结构:
typedef struct udpPacket {
char header[8];
structr dnsQuery query;
}
并从字节数组映射这个结构,但它没有帮助。有人可以帮我处理这些结构吗?对于使用 UDP 协议的 DNS 查询,它们应该是什么样子?
编辑:我的结构现在看起来像这样:
typedef struct {
unsigned short qtype;
unsigned short qclass;
} dnsQueryQuestion;
typedef struct {
dnsQueryQuestion *question;
unsigned char *data[0];
} dnsQuerySection;
typedef struct {
char header[12];
dnsQuerySection querySection[0];
} dnsQuery;
typedef struct udpPacket {
char header[8];
dnsQuery query[0];
} TudpPacket;
我添加了解析功能:
void parse(unsigned char *data, unsigned short *qtype, unsigned short *qclass) {
int i = 0;
while (data[i]) {
int len = data[i];
i += len + 1;
}
*qtype = (unsigned short) data[i+1];
*qclass = (unsigned short) data[i+3];
return;
}
并试图解析:
TudpPacket udpPack = (TudpPacket)buf;
parse(udpPack.query.querySection.data, &(udpPack.query.querySection.question.qtype), &(udpPack.query.querySection.question.qclass));
printf("%u\n", udpPack.query.querySection.question.qtype);