十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
#include#include#include#include#include#include#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;
};
//client sendto dns server
int dns_create_header(struct dns_header* header) {
if (header == NULL)return -1;
memset(header, 0, sizeof(struct dns_header));
//random
srandom(time(NULL));
header->id = random();
header->flags = htons(0x0100);//转化成网络字节序
header->questions = htons(1);
}
//hostname: www.baidu.com
//name: 3www5baidu3com0
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); //strdup -->malloc
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));
//printf("cooect: %d\n", ret);
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);
//request
int slen = sendto(sockfd, request, length, 0, (struct sockaddr*)&servaddr, sizeof(struct sockaddr));
//recvfrom
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);
//printf("recvfrom: %d,%s\n", n, response);
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]);
}
原博客:Linux C/C++实现DNS请求器(UDP编程)_别,爱℡的博客-博客_linux 发送dns请求
成都创新互联:公司2013年成立为各行业开拓出企业自己的“网站建设”服务,为数千家公司企业提供了专业的网站建设、网站制作、网页设计和网站推广服务, 按需定制由设计师亲自精心设计,设计的效果完全按照客户的要求,并适当的提出合理的建议,拥有的视觉效果,策划师分析客户的同行竞争对手,根据客户的实际情况给出合理的网站构架,制作客户同行业具有领先地位的。你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧