Linux学习_域名解析

域名解析编程

概念

  1. 域名:比如www.google.com,域名与IP是绑定的。
  2. 根据域名查找IP地址:
  • 发送域名给域名解析服务器DNS,找到域名绑定的IP地址
  • 知道了IP地址后就可以去访问该网站

域名解析函数

#include <netdb.h>

struct hostent{
	char	*h_name;	/*正式主机名*/
	char	**h_aliases;	/*别名,字符串数组*/
	int	h_addrtype;	/*协议类型*/
	int	h_length;	/*网络地址大小*/
	char	**h_addr_list;	/*指向网络地址的指针*/
};

struct hostent *gethostent(void); 

struct hostent *gethostbyname(const char *hostname); 

void sethostent(int stayopen);

void endhostent(void);

  • 终端more /etc/host可以查看IP地址和绑定的正式主机名(地址后第一个名字),以及别名(正式主机名后面的字符串名字)
  • hostname以是正式主机名或者别名。
  • gethostent和gethostbyname区别:
    • gethostent()是获得所有域名信息
    • gethostbyname()是获得特定一组域名信息,只支持IPv4,如果搜索的域名不存在则它会自动创建一个域名信息,所以在多线程中不推荐使用。

gethostbyname例子

/*
 * gethost.c
 * 
 */

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

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++]);
	}
}

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("get error");
		exit(1);
	}
	endhostent();
	printf("no %s exist\n",argv[1]);
	
	return 0;
}


输出:

pi@raspberrypi:~/haitong-learning/Linux/homework/网络编程 $ ./gethost xuwenhan
hostname: xuwenhan
addrtype: IPv4
ip address: 192.168.31.174
aliase: www.xu.com
pi@raspberrypi:~/haitong-learning/Linux/homework/网络编程 $ cat /etc/hosts
127.0.0.1	localhost
192.168.31.174	xuwenhan www.xu.com

::1		localhost ip6-localhost ip6-loopback
ff02::1		ip6-allnodes
ff02::2		ip6-allrouters

127.0.1.1		raspberrypi
pi@raspberrypi:~/haitong-learning/Linux/homework/网络编程 $ 

gethostent例子

/*
 * gethost2.c
 * 
 */

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

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++]);
	}
}

int main(int argc, char **argv)
{
	if(argc<2){
		fprintf(stderr,"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);
			exit(0);
		}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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值