加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

查看Linux下DNS查找的例子

发布时间:2023-10-12 13:12:05 所属栏目:Linux 来源:
导读:查看Linux下DNS查找的例子:dns.c代码如下:/* * DNS Query Program on Linux * * Author : ismdeep@live.com * * *///Header Files#include<stdio.h> //printf#include<string.h> //strlen#include<stdlib.h> //mall

查看Linux下DNS查找的例子:

dns.c

代码如下:

/*

 * DNS Query Program on Linux

 *

 * Author : ismdeep@live.com

 *

 * */

//Header Files

#include<stdio.h> //printf

#include<string.h> //strlen

#include<stdlib.h> //malloc

#include<sys/socket.h> //you know what this is for

#include<arpa/inet.h> //inet_addr , inet_ntoa , ntohs etc

#include<netinet/in.h>

#include<unistd.h> //getpid

//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

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章