• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > linux中查询dns示例

linux中查询dns示例

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了linux 查询dns,数据库查询条件示例,linux dns配置,linux dns,linux dns服务器配置等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

dns.c

//List of DNS Servers registered on the system
char dns_servers[10][100];
int dns_server_count = 0;
//Types of DNS resource records :)

#define T_A 1 //Ipv4 address
#define T_NS 2 //Nameserver
#define T_CNAME 5 // canonical name
#define T_SOA 6 /* start of authority zone */
#define T_PTR 12 /* domain name pointer */
#define T_MX 15 //Mail server

//Function Prototypes
void ngethostbyname (unsigned char* , int);
void ChangetoDnsNameFormat (unsigned char*,unsigned char*);
unsigned char* ReadName (unsigned char*,unsigned char*,int*);
void get_dns_servers();

//DNS header structure
struct DNS_HEADER
{
 unsigned short id; // identification number

 unsigned char rd :1; // recursion desired
 unsigned char tc :1; // truncated message
 unsigned char aa :1; // authoritive answer
 unsigned char opcode :4; // purpose of message
 unsigned char qr :1; // query/response flag

 unsigned char rcode :4; // response code
 unsigned char cd :1; // checking disabled
 unsigned char ad :1; // authenticated data
 unsigned char z :1; // its z! reserved
 unsigned char ra :1; // recursion available

 unsigned short q_count; // number of question entries
 unsigned short ans_count; // number of answer entries
 unsigned short auth_count; // number of authority entries
 unsigned short add_count; // number of resource entries
};

//Constant sized fields of query structure
struct QUESTION
{
 unsigned short qtype;
 unsigned short qclass;
};

//Constant sized fields of the resource record structure
#pragma pack(push, 1)
struct R_DATA
{
 unsigned short type;
 unsigned short _class;
 unsigned int ttl;
 unsigned short data_len;
};
#pragma pack(pop)

//Pointers to resource record contents
struct RES_RECORD
{
 unsigned char *name;
 struct R_DATA *resource;
 unsigned char *rdata;
};

//Structure of a Query
typedef struct
{
 unsigned char *name;
 struct QUESTION *ques;
} QUERY;

int main( int argc , char *argv[])
{
 unsigned char hostname[100];

 //Get the DNS servers from the resolv.conf file
 get_dns_servers();

 //Get the hostname from the terminal
 printf("Enter Hostname to Lookup : ");
 scanf("%s" , hostname);

 //Now get the ip of this hostname , A record
 ngethostbyname(hostname , T_A);

 return 0;
}

/*
 * Perform a DNS query by sending a packet
 * */
void ngethostbyname(unsigned char *host , int query_type)
{
 unsigned char buf[65536],*qname,*reader;
 int i , j , stop , s;

 struct sockaddr_in a;

 struct RES_RECORD answers[20],auth[20],addit[20]; //the replies from the DNS server
 struct sockaddr_in dest;

 struct DNS_HEADER *dns = NULL;
 struct QUESTION *qinfo = NULL;

 printf("Resolving %s" , host);

 s = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP); //UDP packet for DNS queries

 dest.sin_family = AF_INET;
 dest.sin_port = htons(53);
 dest.sin_addr.s_addr = inet_addr(dns_servers[0]); //dns servers

 //Set the DNS structure to standard queries
 dns = (struct DNS_HEADER *)&buf;

 dns->id = (unsigned short) htons(getpid());
 dns->qr = 0; //This is a query
 dns->opcode = 0; //This is a standard query
 dns->aa = 0; //Not Authoritative
 dns->tc = 0; //This message is not truncated
 dns->rd = 1; //Recursion Desired
 dns->ra = 0; //Recursion not available! hey we dont have it (lol)
 dns->z = 0;
 dns->ad = 0;
 dns->cd = 0;
 dns->rcode = 0;
 dns->q_count = htons(1); //we have only 1 question
 dns->ans_count = 0;
 dns->auth_count = 0;
 dns->add_count = 0;

 //point to the query portion
 qname =(unsigned char*)&buf[sizeof(struct DNS_HEADER)];

 ChangetoDnsNameFormat(qname , host);
 qinfo =(struct QUESTION*)&buf[sizeof(struct DNS_HEADER) + (strlen((const char*)qname) + 1)]; //fill it

 qinfo->qtype = htons( query_type ); //type of the query , A , MX , CNAME , NS etc
 qinfo->qclass = htons(1); //its internet (lol)

 printf("\nSending Packet...");
 if( sendto(s,(char*)buf,sizeof(struct DNS_HEADER) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION),0,(struct sockaddr*)&dest,sizeof(dest)) < 0)
 {
  perror("sendto failed");
 }
 printf("Done");

 //Receive the answer
 i = sizeof dest;
 printf("\nReceiving answer...");
 if(recvfrom (s,(char*)buf , 65536 , 0 , (struct sockaddr*)&dest , (socklen_t*)&i ) < 0)
 {
  perror("recvfrom failed");
 }
 printf("Done");

 dns = (struct DNS_HEADER*) buf;

 //move ahead of the dns header and the query field
 reader = &buf[sizeof(struct DNS_HEADER) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION)];

 printf("\nThe response contains : ");
 printf("\n %d Questions.",ntohs(dns->q_count));
 printf("\n %d Answers.",ntohs(dns->ans_count));
 printf("\n %d Authoritative Servers.",ntohs(dns->auth_count));
 printf("\n %d Additional records.\n\n",ntohs(dns->add_count));

 //Start reading answers
 stop=0;

 for(i=0;i<ntohs(dns->ans_count);i++)
 {
  answers[i].name=ReadName(reader,buf,&stop);
  reader = reader + stop;

  answers[i].resource = (struct R_DATA*)(reader);
  reader = reader + sizeof(struct R_DATA);

  if(ntohs(answers[i].resource->type) == 1) //if its an ipv4 address
  {
   answers[i].rdata = (unsigned char*)malloc(ntohs(answers[i].resource->data_len));

   for(j=0 ; j<ntohs(answers[i].resource->data_len) ; j++)
   {
    answers[i].rdata[j]=reader[j];
   }

   answers[i].rdata[ntohs(answers[i].resource->data_len)] = '\0';

   reader = reader + ntohs(answers[i].resource->data_len);
  }
  else
  {
   answers[i].rdata = ReadName(reader,buf,&stop);
   reader = reader + stop;
  }
 }

 //read authorities
 for(i=0;i<ntohs(dns->auth_count);i++)
 {
  auth[i].name=ReadName(reader,buf,&stop);
  reader+=stop;

  auth[i].resource=(struct R_DATA*)(reader);
  reader+=sizeof(struct R_DATA);

  auth[i].rdata=ReadName(reader,buf,&stop);
  reader+=stop;
 }

 //read additional
 

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • linux中查询dns示例

相关文章

  • 2017-05-28C++实现Linux下弹出U盘的方法
  • 2017-05-28Cocos2d-x中实现弹出对话框示例
  • 2017-05-28C++输出上三角/下三角/菱形/杨辉三角形(实现代码)
  • 2017-05-28算法详解之分治法具体实现
  • 2017-05-28C++实现数字转换为十六进制字符串的方法
  • 2017-05-28linux使用gcc编译c语言共享库步骤
  • 2017-05-28简单对比C语言中的fputs()函数和fputc()函数
  • 2017-05-28C语言实现牛顿迭代法解方程详解
  • 2017-05-28C语言与JAVA的区别是什么(推荐)
  • 2017-09-20more effective C++在未来时态下发展程序

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C++ 将数据转为字符串的几种方法
    • C语言求两个字符串的最长公共子串
    • Cocos2d-x UI开发之菜单类使用实例
    • C++实现的泛型List类分享
    • 基于C语言sprintf函数的深入理解
    • 浅谈C++日志系统log4cxx的使用小结详解
    • linux c 获得当前进程的进程名和执行路径(示例)
    • 深入理解C++中变量的存储类别和属性
    • C语言解决螺旋矩阵算法问题的代码示例
    • 详解C语言中accept()函数和shutdown()函数的使用

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有