// 一個簡單的DNS客戶端demo
// DNS協議參考:http://www.cnblogs.com/topdog/archive/2011/11/15/2250185.html
// 代碼參考:http://www.isayme.org/socket-udp-dns-ping-ip.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//#define USE_TCP // DNS協議默認使用UDP,但其實使用TCP也是可以的,試試就知道。
#define SRV_PORT 53
#define BUF_SIZE 1024
const char srv_ip[] = "208.67.222.222"; // 可用的DNS服務器地址
typedef unsigned short U16;
typedef struct _DNS_HDR
{
U16 id;
U16 tag;
U16 numq;
U16 numa;
U16 numa1;
U16 numa2;
} DNS_HDR;
typedef struct _DNS_QER
{
U16 type;
U16 classes;
} DNS_QER;
int main(int argc, char** argv)
{
int clifd,len = 0,i;
struct sockaddr_in servaddr;
#ifdef USE_TCP
int socklen = sizeof(servaddr);
#endif
char buf[BUF_SIZE];
char *p;
DNS_HDR *dnshdr = (DNS_HDR *)buf;
DNS_QER *dnsqer = (DNS_QER *)(buf + sizeof(DNS_HDR));
if (argc != 2)
{
printf("todo:usage\n");
return -1;
}
if ((clifd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("create socket error!\n");
return -1;
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_aton(srv_ip, &servaddr.sin_addr);
servaddr.sin_port = htons(SRV_PORT);
#ifdef USE_TCP
if (connect(clifd, (struct sockaddr *)&servaddr, socklen) < 0)
{
printf("can't connect to %s!\n ", argv[1]);
return -1;
}
#endif
memset(buf, 0, BUF_SIZE);
dnshdr->id = htons(0x0001); // 一個用戶發送查詢的時候定義的隨機數,當服務器返回結果的時候,返回包的ID與用戶發送的一致
dnshdr->tag = htons(0x0100); // 一個期望遞歸的標準查詢請求
dnshdr->numq = htons(0x0001); // 報文請求段中的問題記錄數
dnshdr->numa = 0; // 報文回答段中的回答記錄數,由服務器返回時設置
// 以下按照DNS協議的規則填充要查詢的域名
strcpy(buf + sizeof(DNS_HDR) + 1, argv[1]);
p = buf + sizeof(DNS_HDR) + 1;
i = 0;
while (p < (buf + sizeof(DNS_HDR) + 1 + strlen(argv[1])))
{
if (*p == '.')
{
*(p - i - 1) = i;
i = 0;
}
else
{
i++;
}
p++;
}
*(p - i - 1) = i;
dnsqer = (DNS_QER *)(buf + sizeof(DNS_HDR) + 2 + strlen(argv[1]));
dnsqer->type = htons(0x0001); // 查詢的資源記錄類型
dnsqer->classes = htons(0x0001); // 指定信息的協議組
#ifdef USE_TCP
len = send(clifd, buf, sizeof(DNS_HDR) + sizeof(DNS_QER) + strlen(argv[1]) + 2, 0);
len = recv(clifd, buf, BUF_SIZE, 0);
#else
len = sendto(clifd, buf, sizeof(DNS_HDR) + sizeof(DNS_QER) + strlen(argv[1]) + 2, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
i = sizeof(struct sockaddr_in);
len = recvfrom(clifd, buf, BUF_SIZE, 0, (struct sockaddr *)&servaddr, (socklen_t *)&i);
#endif
if (len < 0)
{
printf("send or recv error\n");
return -1;
}
if (dnshdr->numa == 0)
{
printf("ack error\n");
return -1;
}
p = buf + len - 4;
printf("%s ==> %u.%u.%u.%u\n", argv[1],
(unsigned char)(*p),
(unsigned char)(*(p + 1)),
(unsigned char)(*(p + 2)),
(unsigned char)(*(p + 3))
);
close(clifd);
return 0;
}
一個簡單的DNS客戶端demo
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.