使用libevent库进行dns解析获得的ip地址无法用来登入网页
问题描述:
用libevent库中的evdns_resolve_ipv4()函数来进行dns解析,回调函数中显示解析后的ip地址
event_base *base=event_init();
103 evdns_init();
104 evdns_resolve_ipv4(ourl->domain,0,callback,ourl);//调用了evdns_ bas\
105 e_resolve_ipv4函数
106 event_dispatch();
107 event_base_free(base);
回调函数
//dns解析回调函数
64 static void callback(int result,char type,int count,int ttl,void *addresses,void *arg){
65 Url *ourl=(Url *)arg;
66 struct in_addr *addrs=(in_addr *) addresses;
67
68 if(result!=DNS_ERR_NONE||count==0){//如果结果出错或者ip地址数量为0
69 LOG(S_ERROR,"dns 解析出错:%s \n",ourl->domain);
70 }
71 else{
72 char *ip=inet_ntoa(addrs[0]);
73 LOG(S_DEBUG,"dns 解析成功:%s \n",ourl->domain);
74 host_ip_map[ourl->domain]=strdup(ip);
75 ourl->ip=strdup(ip);
76 LOG(S_INFO,"ip 地址为:%s \n",ourl->ip);
77 push_ourlqueue(ourl);
78 printf("回调函数完成.\n");
79 }
80
81
82 event_loopexit(NULL);
83
84 }
[2016/11/30 17:21][DEBUG][file:url.h][line:73]dns 解析成功:baidu.com
[2016/11/30 17:21][INFO][file:url.h][line:76]ip 地址为:111.13.101.208
编译后解析baidu.com ,可以得到ip 为 111.13.101.208 。
[2016/11/30 17:21][DEBUG][file:url.h][line:73]dns 解析成功:blog.****.net
[2016/11/30 17:21][INFO][file:url.h][line:76]ip 地址为:60.205.8.179
解析blog.****.net 获得的ip 为 60.205.8.179 。
用解析baidu的ip可以访问baidu.com
用下面的ip地址却只能获得一个403 的错误提示。
求教这是什麽原因
答
HTTP 服务端对HostName 做了限制了。
简单点说就是服务器只认可blog.csdn.net 这样的Hostname,对 60.205.8.179 这样的Hostname 不认。
建议补习一下HTTP 协议啊。