How gethostbyname()
or getnameinfo()
work in background?
gethostbyname()或getnameinfo()如何在后台运行?
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
/* paddr: print the IP address in a standard decimal dotted format */
void
paddr(unsigned char *a)
{
printf("%d.%d.%d.%d\n", a[0], a[1], a[2], a[3]);
}
main(int argc, char **argv) {
struct hostent *hp;
char *host = "google.com";
int i;
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "could not obtain address of %s\n", host);
return 0;
}
for (i=0; hp->h_addr_list[i] != 0; i++)
paddr((unsigned char*) hp->h_addr_list[i]);
exit(0);
}
output for google.com:
google.com的输出:
74.125.236.198
74.125.236.199
74.125.236.206
74.125.236.201
74.125.236.200
74.125.236.196
74.125.236.193
74.125.236.197
74.125.236.194
74.125.236.195
74.125.236.192
output for www.google.com:
www.google.com的输出:
74.125.236.210
74.125.236.209
74.125.236.212
74.125.236.208
74.125.236.211
- Will the above program do a check in the internet to resolve into IP?
- 上述程序是否会在互联网上进行检查以解析为IP?
- Why its showing less IP addresses for www.google.com and more for just google.com?
- 为什么它为www.google.com显示的IP地址更少,而google.com更多?
1 个解决方案
#1
1
On a Linux system the gethostbyname() call implemented in the glibc performs lookups according to the configuration files /etc/host.conf and /etc/nsswitch.conf.
在Linux系统上,glibc中实现的gethostbyname()调用根据配置文件/etc/host.conf和/etc/nsswitch.conf执行查找。
Typically in a default configuration it will first look in the /etc/hosts file if a local entry for the given name exists and if so, returns that. Otherwise it will proceed with the DNS protocol that is in turn configured by /etc/resolv.conf where the nameservers are stated.
通常在默认配置中,如果存在给定名称的本地条目,它将首先查看/ etc / hosts文件,如果存在,则返回该文件。否则,它将继续使用DNS协议,该协议由/etc/resolv.conf配置,其中声明了名称服务器。
Much more complex setups can be configured that lookup LDAP servers, databases etc.
可以配置更复杂的设置来查找LDAP服务器,数据库等。
You can also look into some man pages like man 5 nsswitch.conf
.
您还可以查看man 5 nsswitch.conf等手册页。
#1
1
On a Linux system the gethostbyname() call implemented in the glibc performs lookups according to the configuration files /etc/host.conf and /etc/nsswitch.conf.
在Linux系统上,glibc中实现的gethostbyname()调用根据配置文件/etc/host.conf和/etc/nsswitch.conf执行查找。
Typically in a default configuration it will first look in the /etc/hosts file if a local entry for the given name exists and if so, returns that. Otherwise it will proceed with the DNS protocol that is in turn configured by /etc/resolv.conf where the nameservers are stated.
通常在默认配置中,如果存在给定名称的本地条目,它将首先查看/ etc / hosts文件,如果存在,则返回该文件。否则,它将继续使用DNS协议,该协议由/etc/resolv.conf配置,其中声明了名称服务器。
Much more complex setups can be configured that lookup LDAP servers, databases etc.
可以配置更复杂的设置来查找LDAP服务器,数据库等。
You can also look into some man pages like man 5 nsswitch.conf
.
您还可以查看man 5 nsswitch.conf等手册页。