DNS域名解析

本文介绍了域名解析的基础知识,并提供了使用gethostbyname和gethostent函数进行域名解析的示例代码。通过这些函数,可以将域名转换为IP地址。
	struct hostent
	{
		char *h_name;         //正式主机名
		char **h_aliases;     //主机别名,别名,字符串数组
		int h_addrtype;       //协议类型:IPV4-AF_INET
		int h_length;		  //网络地址大小,对于IPv4是四字节,即32位
		char **h_addr_list;	  //指向网络地址的指针
	};
	

用到的函数原型:

        #include <netdb.h>
       extern int h_errno;

       struct hostent *gethostbyname(const char *name);

       #include <sys/socket.h>       /* for AF_INET */
       struct hostent *gethostbyaddr(const void *addr,
                                     socklen_t len, int type);

       void sethostent(int stayopen);

       void endhostent(void);

       void herror(const char *s);

       const char *hstrerror(int err);

       /* System V/POSIX extension */
       struct hostent *gethostent(void);

       /* GNU extensions */
       struct hostent *gethostbyname2(const char *name, int af);

       int gethostent_r(
               struct hostent *ret, char *buf, size_t buflen,
               struct hostent **result, int *h_errnop);

       int gethostbyaddr_r(const void *addr, socklen_t len, int type,
               struct hostent *ret, char *buf, size_t buflen,
               struct hostent **result, int *h_errnop);

       int gethostbyname_r(const char *name,
               struct hostent *ret, char *buf, size_t buflen,
               struct hostent **result, int *h_errnop);

       int gethostbyname2_r(const char *name, int af,
               struct hostent *ret, char *buf, size_t buflen,
               struct hostent **result, int *h_errnop);

 

域名服务器,查询域名返回的是IP地址:
例如:查询www.google.com返回的是66.249.89.99

使用 more /etc/hosts查看自己的域名:

andrew@andrew-Thurley:~/work/network$ more /etc/hosts
127.0.0.1	localhost
127.0.1.1	andrew-Thurley

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

可以看到其实环回地址就是127.0.0.1也就是localhost地址

gethostbyname仅仅支持IPV4并且在多线程中会出现问题,在多线程中可以考虑使用gethostent来解析域名:

使用gethostbyname解析dns信息代码:

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <arpa/inet.h>

void out_addr(struct hostent *h);


int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		printf("usage: %s host\n", argv[0]);
		exit(1);
	}
	struct hostent *h;
	h = gethostbyname(argv[1]);
	if(h != NULL)
	{
		out_addr(h);
	}
	else
	{
		printf("no %s exits \n", argv[1]);
	}
        endhostent();


	return 0;

}




void out_addr(struct hostent *h)
{
	printf("hostname: %s\n", h->h_name);
	printf("addrtype: %s\n", h->h_addrtype == AF_INET ? "IPV4" : "IPV6");
	char ip[16];
	memset(ip, 0, sizeof(ip));
	inet_ntop(h->h_addrtype, h->h_addr_list[0], ip, sizeof(ip));
	printf("ip address: %s\n", ip);
	int i = 0;
	while(h->h_aliases[i] != NULL)
	{
		printf("aliase: %s\n", h->h_aliases[i]);
		i++;
	}




}

使用域名解析函数gethostent函数解析dns代码:

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <arpa/inet.h>

void out_addr(struct hostent *h);


int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		printf("usage: %s host\n", argv[0]);
		exit(1);
	}
	struct hostent *h;
    while((h = gethostent()) != NULL)
    {
        if(!strcmp(argv[1], h->h_name))
        {
            out_addr(h);
        }
        else
        {
            int i = 0;
            while(h->h_aliases[i] != NULL)
            {
                if(!strcmp(argv[1], h->h_aliases[i]))
                {
                    out_addr(h);
                    exit(0);
                }
                i++;
            }
        }
    }
	endhostent();
	printf("no %s exist \n", argv[1]);


	return 0;

}




void out_addr(struct hostent *h)
{
	printf("hostname: %s\n", h->h_name);
	printf("addrtype: %s\n", h->h_addrtype == AF_INET ? "IPV4" : "IPV6");
	char ip[16];
	memset(ip, 0, sizeof(ip));
	inet_ntop(h->h_addrtype, h->h_addr_list[0], ip, sizeof(ip));
	printf("ip address: %s\n", ip);
}




 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

andrewbytecoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值