欢迎来到别爱的CSDN
前言
我们今天要实现一个DNS请求器,运用到的知识主要是UDP编程,以及了解DNS协议,以及协议解析。
一、DNS
1.DNS是什么
域名系统(英文:Domain Name System,缩写:DNS)是互联网的一项服务。它作为将域名和 IP 地址相互映射的一个分布式数据库,能够使人更方便地访问互联网。DNS 使用 TCP 和 UDP 端口 53。当前,对于每一级域名长度的限制是 63 个字符,域名总长度则不能超过 253 个字符。
域名系统(英文:Domain Name System,缩写:DNS)的作用是将人类可读的域名(如,www.example.com) 转换为机器可读的 IP 地址 (如,192.0.2.44)。
域名是由一串用点分隔符 . 组成的互联网上某一台计算机或计算机组的名称,用于在数据传输时标识计算机的方位。域名可以说是一个 IP 地址的代称,目的是为了便于记忆后者。例如,www.0voice.com 是一个域名,和 IP 地址 122.152.222.180 相对应。人们可以直接访问www.0voice.com 来代替 IP 地址,然后域名系统(DNS)就会将它转化 成便于机器识别的 IP 地址。这样,人们只需要记忆 www.0voice.com 这一串带有特殊含义的字符,而不需要记忆没有含义的数字。
1.DNS协议报文格式
1. 头部
会话标识(2 字节):是 DNS 报文的 ID 标识,对于请求报文和其对应的应答报文,这个字段是相同的,通过它可以区分 DNS 应答报文是哪个请求的响
标志(2 字节)
QR(1bit)查询/响应标志,0 为查询,1 为响应 opcode(4bit)0 表示标准查询,1 表示反向查询,2 表示服务器状态请求 AA(1bit)表示授权回答 TC(1bit)表示可截断的 RD(1bit)表示期望递归 RA(1bit)表示可用递归 rcode(4bit)表示返回码,0 表示没有差错,3 表示名字差错,2 表示服务器错误(Server Failure)
数量字段(总共 8 字节):Questions、Answer RRs、Authority RRs、Additional RRs 各自表示后面的四个区域的数目。Questions 表示查询问题区域节的数量,Answers 表示回答区域的数量,Authoritative namesversers 表示授权区域的数量,Additional recoreds 表示附加区域的数量
2.Queries
查询名:长度不固定,且不使用填充字节,一般该字段表示的就是需要查询的域名(如果是 反向查询,则为 IP,反向查询即由 IP 地址反查域名),一般的格式如下图所示。
3.查询类型
4.查询类
通常为 1,表明是 Internet 数据
二、代码实现
1.DNS请求头的定义
我们可以通过Wireshark捕获www.baidu.com 可以看到:
代码如下(示例):
struct dns_header {
unsigned short id;
unsigned short flags;
unsigned short questions;
unsigned short answer;
unsigned short authority;
unsigned short additional;
};
struct dns_queries {
int length;
unsigned short qtype;
unsigned short qclass;
unsigned char* name;
};
struct dns_item {
char* domain;
char* ip;
};
2.头部数据
代码如下(示例):
int dns_create_header(struct dns_header* header) {
if (header == NULL)return -1;
memset(header, 0, sizeof(struct dns_header));
srandom(time(NULL));
header->id = random();
header->flags = htons(0x0100);
header->questions = htons(1);
}
3.queries数据
代码如下(示例):
int dns_create_queries(struct dns_queries* question, const char* hostname) {
if (question == NULL || hostname == NULL)return -1;
memset(question, 0, sizeof(struct dns_queries));
question->name = (char*)malloc(strlen(hostname) + 2);
if (question->name == NULL) {
return -2;
}
question->length = strlen(hostname) + 2;
question->qtype = htons(1);
question->qclass = htons(1);
const char delim[2] = ".";
char* qname = question->name;
char* hostname_dup = strdup(hostname);
char* token = strtok(hostname_dup, delim);
while (token != NULL) {
size_t len = strlen(token);
*qname = len;
qname++;
strncpy(qname, token, len + 1);
qname += len;
token = strtok(NULL, delim);
}
free(hostname_dup);
}
4.将头部和queries合并
代码如下(示例):
int dns_build_request(struct dns_header* header, struct dns_queries* question, char* request,int rlen) {
if (header == NULL || question == NULL || request == NULL)return -1;
int offset = 0;
memset(request, 0, rlen);
memcpy(request, header, sizeof(struct dns_header));
offset = sizeof(struct dns_header);
memcpy(request + offset, question->name, question->length);
offset += question->length;
memcpy(request + offset, &question->qtype, sizeof(question->qtype));
offset += sizeof(question->qtype);
memcpy(request + offset, &question->qclass, sizeof(question->qclass));
offset += sizeof(question->qclass);
return offset;
}
5.解析数据
代码如下(示例):
static int is_pointer(int in) {
return ((in & 0xC0) == 0xC0);
}
static void dns_parse_name(unsigned char* chunk, unsigned char* ptr, char* out, int* len) {
int flag = 0, n = 0, alen = 0;
char* pos = out + (*len);
while (1) {
flag = (int)ptr[0];
if (flag == 0) break;
if (is_pointer(flag)) {
n = (int)ptr[1];
ptr = chunk + n;
dns_parse_name(chunk, ptr, out, len);
break;
}
else {
ptr++;
memcpy(pos, ptr, flag);
pos += flag;
ptr += flag;
*len += flag;
if ((int)ptr[0] != 0) {
memcpy(pos, ".", 1);
pos += 1;
(*len) += 1;
}
}
}
}
static int dns_parse_response(char* buffer, struct dns_item** domains) {
int i = 0;
unsigned char* ptr = buffer;
ptr += 4;
int querys = ntohs(*(unsigned short*)ptr);
ptr += 2;
int answers = ntohs(*(unsigned short*)ptr);
ptr += 6;
for (i = 0; i < querys; i++) {
while (1) {
int flag = (int)ptr[0];
ptr += (flag + 1);
if (flag == 0) break;
}
ptr += 4;
}
char cname[128], aname[128], ip[20], netip[4];
int len, type, ttl, datalen;
int cnt = 0;
struct dns_item* list = (struct dns_item*)calloc(answers, sizeof(struct dns_item));
if (list == NULL) {
return -1;
}
for (i = 0; i < answers; i++) {
bzero(aname, sizeof(aname));
len = 0;
dns_parse_name(buffer, ptr, aname, &len);
ptr += 2;
type = htons(*(unsigned short*)ptr);
ptr += 4;
ttl = htons(*(unsigned short*)ptr);
ptr += 4;
datalen = ntohs(*(unsigned short*)ptr);
ptr += 2;
if (type == DNS_CNAME) {
bzero(cname, sizeof(cname));
len = 0;
dns_parse_name(buffer, ptr, cname, &len);
ptr += datalen;
}
else if (type == DNS_HOST) {
bzero(ip, sizeof(ip));
if (datalen == 4) {
memcpy(netip, ptr, datalen);
inet_ntop(AF_INET, netip, ip, sizeof(struct sockaddr));
printf("%s has address %s\n", aname, ip);
printf("\tTime to live: %d minutes , %d seconds\n", ttl / 60, ttl % 60);
list[cnt].domain = (char*)calloc(strlen(aname) + 1, 1);
memcpy(list[cnt].domain, aname, strlen(aname));
list[cnt].ip = (char*)calloc(strlen(ip) + 1, 1);
memcpy(list[cnt].ip, ip, strlen(ip));
cnt++;
}
ptr += datalen;
}
}
*domains = list;
ptr += 2;
return cnt;
}
6.发送请求和获取结果
代码如下(示例):
int dns_client_commit(const char* domain) {
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
return -1;
}
struct sockaddr_in servaddr = { 0 };
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(DNS_SERVER_PORT);
servaddr.sin_addr.s_addr = inet_addr(DNS_SERVER_IP);
int ret = connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
struct dns_header header = { 0 };
dns_create_header(&header);
struct dns_queries question = { 0 };
dns_create_queries(&question, domain);
char request[1024] = { 0 };
int length = dns_build_request(&header, &question, request,1024);
int slen = sendto(sockfd, request, length, 0, (struct sockaddr*)&servaddr, sizeof(struct sockaddr));
char response[1024] = { 0 };
struct sockaddr_in addr = { 0 };
size_t addr_len = sizeof(struct sockaddr_in);
int n = recvfrom(sockfd, response, sizeof(response), 0, (struct sockaddr*)&addr, (socklen_t*)&addr_len);
struct dns_item* dns_domain = NULL;
dns_parse_response(response, &dns_domain);
free(dns_domain);
return n;
}
7.全部代码
代码如下(示例):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#define DNS_SERVER_PORT 53
#define DNS_SERVER_IP "114.114.114.114"
#define DNS_HOST 0x01
#define DNS_CNAME 0x05
struct dns_header {
unsigned short id;
unsigned short flags;
unsigned short questions;
unsigned short answer;
unsigned short authority;
unsigned short additional;
};
struct dns_queries {
int length;
unsigned short qtype;
unsigned short qclass;
unsigned char* name;
};
struct dns_item {
char* domain;
char* ip;
};
int dns_create_header(struct dns_header* header) {
if (header == NULL)return -1;
memset(header, 0, sizeof(struct dns_header));
srandom(time(NULL));
header->id = random();
header->flags = htons(0x0100);
header->questions = htons(1);
}
int dns_create_queries(struct dns_queries* question, const char* hostname) {
if (question == NULL || hostname == NULL)return -1;
memset(question, 0, sizeof(struct dns_queries));
question->name = (char*)malloc(strlen(hostname) + 2);
if (question->name == NULL) {
return -2;
}
question->length = strlen(hostname) + 2;
question->qtype = htons(1);
question->qclass = htons(1);
const char delim[2] = ".";
char* qname = question->name;
char* hostname_dup = strdup(hostname);
char* token = strtok(hostname_dup, delim);
while (token != NULL) {
size_t len = strlen(token);
*qname = len;
qname++;
strncpy(qname, token, len + 1);
qname += len;
token = strtok(NULL, delim);
}
free(hostname_dup);
}
int dns_build_request(struct dns_header* header, struct dns_queries* question, char* request,int rlen) {
if (header == NULL || question == NULL || request == NULL)return -1;
int offset = 0;
memset(request, 0, rlen);
memcpy(request, header, sizeof(struct dns_header));
offset = sizeof(struct dns_header);
memcpy(request + offset, question->name, question->length);
offset += question->length;
memcpy(request + offset, &question->qtype, sizeof(question->qtype));
offset += sizeof(question->qtype);
memcpy(request + offset, &question->qclass, sizeof(question->qclass));
offset += sizeof(question->qclass);
return offset;
}
static int is_pointer(int in) {
return ((in & 0xC0) == 0xC0);
}
static void dns_parse_name(unsigned char* chunk, unsigned char* ptr, char* out, int* len) {
int flag = 0, n = 0, alen = 0;
char* pos = out + (*len);
while (1) {
flag = (int)ptr[0];
if (flag == 0) break;
if (is_pointer(flag)) {
n = (int)ptr[1];
ptr = chunk + n;
dns_parse_name(chunk, ptr, out, len);
break;
}
else {
ptr++;
memcpy(pos, ptr, flag);
pos += flag;
ptr += flag;
*len += flag;
if ((int)ptr[0] != 0) {
memcpy(pos, ".", 1);
pos += 1;
(*len) += 1;
}
}
}
}
static int dns_parse_response(char* buffer, struct dns_item** domains) {
int i = 0;
unsigned char* ptr = buffer;
ptr += 4;
int querys = ntohs(*(unsigned short*)ptr);
ptr += 2;
int answers = ntohs(*(unsigned short*)ptr);
ptr += 6;
for (i = 0; i < querys; i++) {
while (1) {
int flag = (int)ptr[0];
ptr += (flag + 1);
if (flag == 0) break;
}
ptr += 4;
}
char cname[128], aname[128], ip[20], netip[4];
int len, type, ttl, datalen;
int cnt = 0;
struct dns_item* list = (struct dns_item*)calloc(answers, sizeof(struct dns_item));
if (list == NULL) {
return -1;
}
for (i = 0; i < answers; i++) {
bzero(aname, sizeof(aname));
len = 0;
dns_parse_name(buffer, ptr, aname, &len);
ptr += 2;
type = htons(*(unsigned short*)ptr);
ptr += 4;
ttl = htons(*(unsigned short*)ptr);
ptr += 4;
datalen = ntohs(*(unsigned short*)ptr);
ptr += 2;
if (type == DNS_CNAME) {
bzero(cname, sizeof(cname));
len = 0;
dns_parse_name(buffer, ptr, cname, &len);
ptr += datalen;
}
else if (type == DNS_HOST) {
bzero(ip, sizeof(ip));
if (datalen == 4) {
memcpy(netip, ptr, datalen);
inet_ntop(AF_INET, netip, ip, sizeof(struct sockaddr));
printf("%s has address %s\n", aname, ip);
printf("\tTime to live: %d minutes , %d seconds\n", ttl / 60, ttl % 60);
list[cnt].domain = (char*)calloc(strlen(aname) + 1, 1);
memcpy(list[cnt].domain, aname, strlen(aname));
list[cnt].ip = (char*)calloc(strlen(ip) + 1, 1);
memcpy(list[cnt].ip, ip, strlen(ip));
cnt++;
}
ptr += datalen;
}
}
*domains = list;
ptr += 2;
return cnt;
}
int dns_client_commit(const char* domain) {
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
return -1;
}
struct sockaddr_in servaddr = { 0 };
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(DNS_SERVER_PORT);
servaddr.sin_addr.s_addr = inet_addr(DNS_SERVER_IP);
int ret = connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
struct dns_header header = { 0 };
dns_create_header(&header);
struct dns_queries question = { 0 };
dns_create_queries(&question, domain);
char request[1024] = { 0 };
int length = dns_build_request(&header, &question, request,1024);
int slen = sendto(sockfd, request, length, 0, (struct sockaddr*)&servaddr, sizeof(struct sockaddr));
char response[1024] = { 0 };
struct sockaddr_in addr = { 0 };
size_t addr_len = sizeof(struct sockaddr_in);
int n = recvfrom(sockfd, response, sizeof(response), 0, (struct sockaddr*)&addr, (socklen_t*)&addr_len);
struct dns_item* dns_domain = NULL;
dns_parse_response(response, &dns_domain);
free(dns_domain);
return n;
}
int main(int argc,char* argv[]) {
if (argc < 2)return -1;
dns_client_commit(argv[1]);
}
总结
关于今天的学习,我们主要了解的DNS协议,以及协议的解析,主要使用了UDP的编程。
参考资料
C/C++ Linux高级开发课程
|