我正在尝试将DNS标头和问题发送到UDP套接字。我已经调用了以下命令来建立连接:button.accordions {
max-height: 0;
transition: max-height 0.5s ease;
}
button.accordions:hover {
max-height: 75px;
}
,socket(PF_INET, SOCK_DGRAM, 0)
使用端口53和inet_aton(temp, &servAddr.sin_addr)
。
我遇到的问题是编写包含标题和DNS查询问题的消息。到目前为止,我的DNS标头结构看起来像这样:
connect(sock, (const sockaddr*) &servAddr, sizeof(servAddr))
我创建DNS查询标题:
struct DNS_Header {
uint16_t id;
struct {
uint8_t rd: 1; // recursion desired
uint8_t tc: 1; // message was truncated
uint8_t aa: 1; // authoritative answer
uint8_t opcode: 4; // query type
// 0 a standard query (QUERY); 1 an inverse query (IQUERY);
// 2 a server status request (STATUS);
// 3 - 15 reserved for futre use
uint8_t qr: 1; // query(0) or response (1)
uint8_t rcode: 4; // response code
// 0: No error condition; 1:Format error; 2:Server failure;
// 3: Name Error (authoritative server, name doesn't exist)
// 4: Not implemented; 5: Refused
uint8_t z: 3; // reserved for future use. Must be zero
uint8_t ra: 1; // recursion
} flags;
uint16_t gdcount;
uint16_t ancount;
uint16_t nscount;
uint16_t arcount;
};
我知道我需要将DNS问题的名称部分的主机名更改为标签/数据格式。我这样做是这样的:
packetHeader.id = htons(1337);
packetHeader.flags.rd = 0;
packetHeader.flags.tc = 0;
packetHeader.flags.aa = 0;
packetHeader.flags.opcode = htons(0);
packetHeader.flags.qr = 0;
packetHeader.flags.rcode = htons(0);
packetHeader.flags.z = htons(0);
packetHeader.flags.ra = 0;
packetHeader.gdcount = htons(1);
packetHeader.ancount = htons(0);
packetHeader.nscount = htons(0);
packetHeader.arcount = htons(0);
我使用DNS_Question结构将所有关于DNS查询问题的内容组合在一起。该结构不包括DNS查询问题名称。我将名称存储在变量static const char* const hex = "0123456789ABCDEF";
hostname.push_back(' ');
int len = hostname.length();
int segCount = 0;
std::vector<std::string> storage;
std::string temp = "";
for (int i = 0; i < len; i++) {
if (hostname[i] == '.' || hostname[i] == ' ') {
storage.push_back(std::to_string(0));
storage.push_back(std::to_string(segCount));
storage.push_back(temp);
// reset
segCount = 0;
temp = "";
continue;
}
const unsigned char c = hostname[i];
std::cout << c << " : " << hex[c >> 4] << " " << hex[c & 15] << std::endl;
temp.push_back(hex[c >> 4]);
temp.push_back(hex[c & 15]);
segCount ++;
continue;
}
storage.push_back(std::to_string(0));
storage.push_back(std::to_string(0));
for (int i = 0; i < storage.size(); i++) { qname_labelFormat.append(storage[i]); }
std::cout << "The hex representation of qname TOTAL: " << qname_labelFormat << std::endl
中。查看下面的DNS查询问题结构:
qname_lableFormat
要填充结构DNS_Question,我已经这样做:
struct DNS_Question {
uint16_t qtype;
uint16_t qclass;
};
完成所有这些操作后,我使用套接字 int queryTypeNum = 0;
if (queryType == "A") { queryTypeNum = 1; }
if (queryType == "NS") { queryTypeNum = 2; }
if (queryType == "CNAME") { queryTypeNum = 5; }
if (queryType == "SOA") { queryTypeNum = 6; }
if (queryType == "WKS") { queryTypeNum = 11; }
if (queryType == "PTR") { queryTypeNum = 12; }
if (queryType == "MX") { queryTypeNum = 15; }
if (queryType == "SRV") { queryTypeNum = 33; }
if (queryType == "AAAA") { queryTypeNum = 28; }
if (queryType == "") { queryTypeNum = 1; }
packetQuestion.qtype = htons(queryTypeNum);
std::cout << "QType value set to: " << queryTypeNum << std::endl;
packetQuestion.qclass = htons(1);
,我相信这是发生错误的地方。没有报告错误消息,但是当我调用write(int fd, const void *buf, size_t count);
命令时,什么都没有收到。我的写命令如下。
read(int fd, void *buf, size_t count);
如果有人能够提供帮助,我将非常感谢。